You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

337 lines
11 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage form
  7. * @since 2010-04-25
  8. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. require_once dirname(__FILE__) . '/../../validator/iValidator.php';
  12. require_once dirname(__FILE__) . '/../../validator/Validator.php';
  13. require_once dirname(__FILE__) . '/../../validator/NotEmptyValidator.php';
  14. require_once dirname(__FILE__) . '/../../validator/RegexValidator.php';
  15. require_once dirname(__FILE__) . '/../../validator/EmailValidator.php';
  16. require_once dirname(__FILE__) . '/../../form/FormField.php';
  17. require_once dirname(__FILE__) . '/../../exception/InitializationException.php';
  18. class FormFieldTest extends PHPUnit_Framework_TestCase
  19. {
  20. public function testSetRequired()
  21. {
  22. $form_field = new FormField();
  23. $this->assertTrue($form_field->isRequired());
  24. $return_object = $form_field->setRequired(false);
  25. $this->assertInstanceOf('FormField', $return_object);
  26. $this->assertSame($form_field, $return_object);
  27. $this->assertFalse($form_field->isRequired());
  28. }
  29. public function testSetIgnored()
  30. {
  31. $form_field = new FormField();
  32. $this->assertFalse($form_field->isIgnored());
  33. $return_object = $form_field->setIgnored(true);
  34. $this->assertInstanceOf('FormField', $return_object);
  35. $this->assertSame($form_field, $return_object);
  36. $this->assertTrue($form_field->isIgnored());
  37. }
  38. public function testIsIgnored()
  39. {
  40. $class_name = 'FormField';
  41. $form_field = new $class_name();
  42. $this->assertFalse($form_field->isIgnored());
  43. $form_field->setIgnored(true);
  44. $this->assertTrue($form_field->isIgnored());
  45. }
  46. public function testIsRequired()
  47. {
  48. $class_name = 'FormField';
  49. $form_field = new $class_name();
  50. $this->assertTrue($form_field->isRequired());
  51. $form_field->setRequired(false);
  52. $this->assertFalse($form_field->isRequired());
  53. }
  54. public function testAddValidators()
  55. {
  56. $validators = array(
  57. 'NotEmpty' => new NotEmptyValidator(),
  58. 'Email' => new EmailValidator()
  59. );
  60. $this->assertInstanceOf('iValidator', $validators['NotEmpty']);
  61. $this->assertInstanceOf('iValidator', $validators['Email']);
  62. $form_field = new FormField();
  63. $return_object = $form_field->addValidators($validators);
  64. $array = array('NotEmptyValidator' => new NotEmptyValidator(), 'EmailValidator' => new EmailValidator());
  65. $this->assertAttributeEquals($array, 'validators', $form_field);
  66. $this->assertSame($form_field, $return_object);
  67. }
  68. public function testAddValidatorObject()
  69. {
  70. $validator = new NotEmptyValidator();
  71. $array = array('NotEmptyValidator' => new NotEmptyValidator());
  72. $form_field = new FormField();
  73. $return_object = $form_field->addValidator($validator);
  74. $this->assertAttributeEquals($array, 'validators', $form_field);
  75. $this->assertSame($form_field, $return_object);
  76. }
  77. public function testAddValidatorString()
  78. {
  79. $form_field = new FormField();
  80. $return_object = $form_field->addValidator('NotEmpty');
  81. $array = array('NotEmptyValidator' => new NotEmptyValidator());
  82. $this->assertAttributeEquals($array, 'validators', $form_field);
  83. $this->assertSame($form_field, $return_object);
  84. }
  85. public function testAddValidatorElse()
  86. {
  87. $validator = true;
  88. $tmp_form_field = new FormField();
  89. // @TODO Fix exception type
  90. $this->setExpectedException('InitializationException', 'Invalid validator provided to addValidator; must be string or iValidator'); // Text of Exception
  91. $tmp_form_field->addValidator($validator);
  92. }
  93. public function testAddFilters()
  94. {
  95. $array = array('loginFilter' => new loginFilter(), 'passwordFilter' => new passwordFilter());
  96. $this->assertInstanceOf('iFilter', $array['loginFilter']);
  97. $this->assertInstanceOf('iFilter', $array['passwordFilter']);
  98. $form_field = new FormField();
  99. $return_object = $form_field->addFilters($array);
  100. $this->assertAttributeEquals($array, 'filters', $form_field);
  101. $this->assertSame($form_field, $return_object);
  102. }
  103. public function testAddFilterObject()
  104. {
  105. $filter = new loginFilter();
  106. $array = array('loginFilter' => new loginFilter());
  107. $this->assertInstanceOf('iFilter', new loginFilter());
  108. $form_field = new FormField();
  109. $return_object = $form_field->addFilter($filter);
  110. $this->assertAttributeEquals($array, 'filters', $form_field);
  111. $this->assertSame($form_field, $return_object);
  112. }
  113. public function testAddFilterString()
  114. {
  115. $form_field = new FormField();
  116. $array = array('loginFilter' => new loginFilter());
  117. $this->assertInstanceOf('iFilter', new loginFilter());
  118. $return_object = $form_field->addFilter('login');
  119. $this->assertAttributeEquals($array, 'filters', $form_field);
  120. $this->assertSame($form_field, $return_object);
  121. }
  122. public function testAddFilterElse()
  123. {
  124. $filter = new NotEmptyValidator();
  125. $form_field = new FormField();
  126. // @TODO Fix exception type
  127. $this->setExpectedException('InitializationException', 'Invalid filter provided to addFilter; must be string or iFilter'); // Text of exception
  128. $form_field->addFilter($filter);
  129. }
  130. public function testGetValueArray()
  131. {
  132. $test_array = array(
  133. 'login' => 'login',
  134. 'password' => 'password'
  135. );
  136. $form_field = new FormField();
  137. $form_field->addFilter('Login');
  138. $this->assertTrue($form_field->isValid($test_array));
  139. $this->assertAttributeInternalType('array', 'value', $form_field);
  140. $value = $form_field->getValue();
  141. $this->assertArrayHasKey('login', $value);
  142. $this->assertArrayHasKey('password', $value);
  143. $this->assertSame(array('login' => 'login',
  144. 'password' => ''), $value);
  145. }
  146. public function testGetValueStringCorrect()
  147. {
  148. $test_string = 'login';
  149. $form_field = new FormField();
  150. $form_field->addFilter('Login');
  151. $this->assertTrue($form_field->isValid($test_string));
  152. $this->assertAttributeNotInternalType('array', 'value', $form_field);
  153. $this->assertSame('login', $form_field->getValue());
  154. }
  155. public function testGetValueStringIncorrect()
  156. {
  157. $test_string = 'password';
  158. $form_field = new FormField();
  159. $form_field->addFilter('Login');
  160. $this->assertTrue($form_field->isValid($test_string));
  161. $this->assertAttributeNotInternalType('array', 'value', $form_field);
  162. $this->assertSame('', $form_field->getValue());
  163. }
  164. public function testGetMessageDefault()
  165. {
  166. $form_field = new FormField();
  167. $this->assertFalse($form_field->getMessage());
  168. $form_field->addValidator('NotEmpty');
  169. $this->assertFalse($form_field->isValid(''));
  170. $this->assertSame('Value is required and can\'t be empty', $form_field->getMessage());
  171. }
  172. public function testGetMessageCustom()
  173. {
  174. $message = 'Test message';
  175. $form_field = new FormField($message);
  176. $this->assertFalse($form_field->getMessage());
  177. $form_field->addValidator('NotEmpty');
  178. $this->assertFalse($form_field->isValid(''));
  179. $this->assertSame($message, $form_field->getMessage());
  180. }
  181. public function testIsValidArrayMissingDefaultMessage()
  182. {
  183. $test_array = array(
  184. 'login' => '',
  185. 'password' => ''
  186. );
  187. $form_field = new FormField();
  188. $form_field->addValidator('NotEmpty');
  189. $this->setExpectedException('InitializationException', 'Define default message for array fields');
  190. $form_field->isValid($test_array);
  191. }
  192. public function testIsValidArrayMissingCustomMessage()
  193. {
  194. $message = 'Test message';
  195. $test_array = array(
  196. 'login' => '',
  197. 'password' => ''
  198. );
  199. $form_field = new FormField($message);
  200. $return_object = $form_field->addValidator('NotEmpty');
  201. $array = array('NotEmptyValidator' => new NotEmptyValidator());
  202. $this->assertAttributeEquals($array, 'validators', $form_field);
  203. $this->assertSame($form_field, $return_object);
  204. $this->assertFalse($form_field->isValid($test_array));
  205. $this->assertSame($message, $form_field->getMessage());
  206. }
  207. public function testIsValidMissingNotRequired()
  208. {
  209. $form_field = new FormField();
  210. $form_field->setRequired(false);
  211. $this->assertTrue($form_field->isValid(''));
  212. }
  213. public function testIsValidArray()
  214. {
  215. $test_array = array(
  216. 'login' => 'login',
  217. 'password' => 'password'
  218. );
  219. $validator = new NotEmptyValidator();
  220. $form_field = new FormField();
  221. $return_object = $form_field->addValidator($validator);
  222. $this->assertTrue($form_field->isValid($test_array));
  223. }
  224. public function testIsValidScalar()
  225. {
  226. $test = 'password';
  227. $validator = new NotEmptyValidator();
  228. $form_field = new FormField();
  229. $return_object = $form_field->addValidator($validator);
  230. $this->assertTrue($form_field->isValid($test));
  231. }
  232. public function testGetSourceValue()
  233. {
  234. $test_array = array(
  235. 'login' => ' login ',
  236. 'password' => ''
  237. );
  238. $form_field = new FormField('Custom message');
  239. $form_field->addFilter('login');
  240. $form_field->addValidator('NotEmpty');
  241. $this->assertFalse($form_field->isValid($test_array));
  242. $this->assertSame(array('login' => 'login', 'password' => ''), $form_field->getValue());
  243. $this->assertNotSame($test_array, $form_field->getValue());
  244. $this->assertSame($test_array, $form_field->getSourceValue());
  245. }
  246. public function testFilterValue()
  247. {
  248. $input = ' login ';
  249. $form_field = new FormField();
  250. $form_field->isValid($input);
  251. $form_field->addFilter('login');
  252. $lf = new loginFilter();
  253. $this->assertSame($form_field->getValue(), $lf->filter($input));
  254. }
  255. }
  256. interface iFilter
  257. {
  258. public function isValid($value, $context = null);
  259. public function filter($value);
  260. public function getMessage();
  261. }
  262. class loginFilter implements iFilter
  263. {
  264. public function filter($value)
  265. {
  266. $value = trim($value);
  267. if ($value === 'login') {
  268. return $value;
  269. }
  270. return '';
  271. }
  272. public function isValid($value, $context = null)
  273. {
  274. }
  275. public function getMessage()
  276. {
  277. }
  278. }
  279. class passwordFilter implements iFilter
  280. {
  281. public function filter($value)
  282. {
  283. return $value;
  284. }
  285. public function isValid($value, $context = null)
  286. {
  287. }
  288. public function getMessage()
  289. {
  290. }
  291. }