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.

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