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.

362 lines
10 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
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/Form.php';
  17. require_once dirname(__FILE__) . '/../../form/FormField.php';
  18. class FormFieldTest extends PHPUnit_Framework_TestCase
  19. {
  20. public function setUp()
  21. {
  22. $this->ff = new TestFormForFormField;
  23. $this->obj_fields = $this->ff->getProp('fields');
  24. }
  25. public function testSetRequired()
  26. {
  27. $form_field = new FormField;
  28. $this->assertTrue($form_field->isRequired());
  29. $return_object = $form_field->setRequired(false);
  30. $this->assertInstanceOf('FormField', $return_object);
  31. $this->assertSame($form_field, $return_object);
  32. $this->assertFalse($form_field->isRequired());
  33. }
  34. public function testSetIgnored()
  35. {
  36. $form_field = new FormField;
  37. $this->assertFalse($form_field->isIgnored());
  38. $return_object = $form_field->setIgnored(true);
  39. $this->assertInstanceOf('FormField', $return_object);
  40. $this->assertSame($form_field, $return_object);
  41. $this->assertTrue($form_field->isIgnored());
  42. }
  43. public function testIsIgnored()
  44. {
  45. $class_name = 'FormField';
  46. $form_field = new $class_name;
  47. $this->assertFalse($form_field->isIgnored());
  48. $form_field->setIgnored(true);
  49. $this->assertTrue($form_field->isIgnored());
  50. }
  51. public function testIsRequired()
  52. {
  53. $class_name = 'FormField';
  54. $form_field = new $class_name;
  55. $this->assertTrue($form_field->isRequired());
  56. $form_field->setRequired(false);
  57. $this->assertFalse($form_field->isRequired());
  58. }
  59. public function testAddValidators()
  60. {
  61. $validator = array(
  62. 'NotEmpty' => new NotEmptyValidator(),
  63. 'Email' => new EmailValidator()
  64. );
  65. $this->assertInstanceOf('iValidator', $validator['NotEmpty']);
  66. $this->assertInstanceOf('iValidator', $validator['Email']);
  67. $tmp_form_field = new TmpFormField;
  68. $return_object = $tmp_form_field->addValidators($validator);
  69. $array = array('NotEmptyValidator'=>new NotEmptyValidator,'EmailValidator'=>new EmailValidator);
  70. $this->assertAttributeEquals($array, 'validators', $tmp_form_field);
  71. $this->assertSame($tmp_form_field, $return_object);
  72. }
  73. public function testAddValidatorObject()
  74. {
  75. $validator = new NotEmptyValidator;
  76. $array = array('NotEmptyValidator'=>new NotEmptyValidator);
  77. $this->assertInstanceOf('iValidator', new NotEmptyValidator);
  78. $tmp_form_field = new TmpFormField;
  79. $return_object = $tmp_form_field->addValidator($validator);
  80. $this->assertAttributeEquals($array, 'validators', $tmp_form_field);
  81. $this->assertSame($tmp_form_field, $return_object);
  82. }
  83. public function testAddValidatorString()
  84. {
  85. $tmp_form_field = new TmpFormField;
  86. $return_object = $tmp_form_field->addValidator('NotEmpty');
  87. $this->assertInstanceOf('iValidator', new NotEmptyValidator);
  88. $array = array('NotEmptyValidator'=>new NotEmptyValidator);
  89. $this->assertAttributeEquals($array, 'validators', $tmp_form_field);
  90. $this->assertSame($tmp_form_field, $return_object);
  91. }
  92. public function testAddValidatorElse()
  93. {
  94. $validator = true;
  95. $this->setExpectedException('Exception');
  96. $tmp_form_field = new TmpFormField;
  97. $tmp_form_field->addValidator($validator);
  98. }
  99. public function testAddFilters()
  100. {
  101. $array = array('loginFilter' => new loginFilter(),'passwordFilter' => new passwordFilter());
  102. $this->assertInstanceOf('iFilter', $array['loginFilter']);
  103. $this->assertInstanceOf('iFilter', $array['passwordFilter']);
  104. $tmp_form_field = new TmpFormField;
  105. $return_object = $tmp_form_field->addFilters($array);
  106. $this->assertAttributeEquals($array, 'filters', $tmp_form_field);
  107. $this->assertSame($tmp_form_field, $return_object);
  108. }
  109. public function testAddFilterObject()
  110. {
  111. $filter = new loginFilter;
  112. $array = array('loginFilter' => new loginFilter());
  113. $this->assertInstanceOf('iFilter', new loginFilter);
  114. $tmp_form_field = new TmpFormField;
  115. $return_object = $tmp_form_field->addFilter($filter);
  116. $this->assertAttributeEquals($array, 'filters', $tmp_form_field);
  117. $this->assertSame($tmp_form_field, $return_object);
  118. }
  119. public function testAddFilterString()
  120. {
  121. $tmp_form_field = new TmpFormField;
  122. $array = array('loginFilter' => new loginFilter());
  123. $this->assertInstanceOf('iFilter', new loginFilter);
  124. $return_object = $tmp_form_field->addFilter('login');
  125. $this->assertAttributeEquals($array, 'filters', $tmp_form_field);
  126. $this->assertSame($tmp_form_field, $return_object);
  127. }
  128. public function testAddFilterElse()
  129. {
  130. $this->setExpectedException('Exception');
  131. $filter = new NotEmptyValidator;
  132. $tmp_form_field = new TmpFormField;
  133. $tmp_form_field->addFilter($filter);
  134. }
  135. public function testGetValueArray()
  136. {
  137. $form = new TestFormForFormField();
  138. $test_array = array(
  139. 'login'=> 'login',
  140. 'password'=> 'password'
  141. );
  142. $this->assertSame(1, $form->isValid($test_array));
  143. $tmp_form_field = new TmpFormField;
  144. $this->assertTrue($tmp_form_field->isValid($test_array));
  145. $this->assertAttributeInternalType('array', 'value', $tmp_form_field);
  146. $this->assertArrayHasKey('login', $tmp_form_field->getSourceValue());
  147. }
  148. public function testGetValueString()
  149. {
  150. $test_array = array(
  151. 'login'=> 'login',
  152. 'password'=> 'password'
  153. );
  154. $test_form_field = new FormField();
  155. $this->assertTrue($test_form_field->isValid($test_array['login']));
  156. $this->assertAttributeNotInternalType('array', 'value', $test_form_field);
  157. $this->assertEquals('login',$test_form_field->getSourceValue());
  158. }
  159. public function testGetMessage()
  160. {
  161. $test_form_field = new FormField();
  162. $this->assertNotNull($test_form_field->getMessage());
  163. }
  164. public function testIsValidMissing1()
  165. {
  166. $test_array = array(
  167. 'login'=> '',
  168. 'password'=> ''
  169. );
  170. $test_form_field = new FormField();
  171. $test_form_field->addValidator('NotEmpty');
  172. $this->setExpectedException('Exception','Define default message for array fields');
  173. $this->assertTrue($test_form_field->isValid($test_array));
  174. }
  175. public function testIsValidMissingDefaultMessage()
  176. {
  177. $test_array = array(
  178. 'login'=> '',
  179. 'password'=> ''
  180. );
  181. $test_form_field = new FormField('ssssssss');
  182. $return_object = $test_form_field->addValidator('NotEmpty');
  183. $this->assertInstanceOf('iValidator', new NotEmptyValidator);
  184. $array = array('NotEmptyValidator'=>new NotEmptyValidator);
  185. $this->assertAttributeEquals($array, 'validators', $test_form_field);
  186. $this->assertSame($test_form_field, $return_object);
  187. $this->assertFalse($test_form_field->isValid($test_array));
  188. }
  189. public function testIsValidMissing2()
  190. {
  191. $form_field = new FormField;
  192. $form_field->setRequired(false);
  193. $this->assertTrue($form_field->isValid(''));
  194. }
  195. public function testIsValidMissing3()
  196. {
  197. $form_field = new FormField;
  198. $form_field->setRequired(false);
  199. $form_field->isValid('');
  200. $this->assertFalse($form_field->isRequired());
  201. }
  202. public function testFilterValue()
  203. {
  204. $form_field = new FormField;
  205. $form_field->isValid('login');
  206. $form_field->addFilter('login');
  207. $lf = new loginFilter;
  208. $this->assertSame($form_field->getValue(), $lf->filter('login'));
  209. }
  210. }
  211. class TmpFormField extends FormField
  212. {
  213. public function getProp($name)
  214. {
  215. return $this->$name;
  216. }
  217. }
  218. class TestFormForFormField extends Form
  219. {
  220. public function init()
  221. {
  222. ///*
  223. $validator = new NotEmptyValidator();
  224. $validator->setMessage('Enter login name.');
  225. $this->addField('login')->addValidator($validator);
  226. // User password
  227. $validator = new NotEmptyValidator();
  228. $validator->setMessage('Enter your password.');
  229. $this->addField('password')->addValidator($validator);
  230. }
  231. public function getProp($name)
  232. {
  233. return $this->$name;
  234. }
  235. }
  236. interface iFilter
  237. {
  238. public function isValid($value, $context = null);
  239. public function getMessage();
  240. }
  241. Class loginFilter implements iFilter
  242. {
  243. public function filter($value)
  244. {
  245. return $value;
  246. }
  247. public function isValid($value, $context = null)
  248. {
  249. }
  250. public function getMessage()
  251. {
  252. }
  253. }
  254. Class passwordFilter implements iFilter
  255. {
  256. public function filter($value)
  257. {
  258. return $value;
  259. }
  260. public function isValid($value, $context = null)
  261. {
  262. }
  263. public function getMessage()
  264. {
  265. }
  266. }