ValidationTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. <?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
  2. /**
  3. * Tests the Validation lib that's shipped with Kohana
  4. *
  5. * @group kohana
  6. * @group kohana.core
  7. * @group kohana.core.validation
  8. *
  9. * @package Kohana
  10. * @category Tests
  11. * @author Kohana Team
  12. * @author BRMatt <[email protected]>
  13. * @copyright (c) 2008-2012 Kohana Team
  14. * @license http://kohanaframework.org/license
  15. */
  16. class Kohana_ValidationTest extends Unittest_TestCase
  17. {
  18. /**
  19. * Tests Validation::factory()
  20. *
  21. * Makes sure that the factory method returns an instance of Validation lib
  22. * and that it uses the variables passed
  23. *
  24. * @test
  25. */
  26. public function test_factory_method_returns_instance_with_values()
  27. {
  28. $values = array(
  29. 'this' => 'something else',
  30. 'writing tests' => 'sucks',
  31. 'why the hell' => 'amIDoingThis',
  32. );
  33. $instance = Validation::factory($values);
  34. $this->assertTrue($instance instanceof Validation);
  35. $this->assertSame(
  36. $values,
  37. $instance->data()
  38. );
  39. }
  40. /**
  41. * When we copy() a validation object, we should have a new validation object
  42. * with the exact same attributes, apart from the data, which should be the
  43. * same as the array we pass to copy()
  44. *
  45. * @test
  46. * @covers Validation::copy
  47. */
  48. public function test_copy_copies_all_attributes_except_data()
  49. {
  50. $validation = new Validation(array('foo' => 'bar', 'fud' => 'fear, uncertainty, doubt', 'num' => 9));
  51. $validation->rule('num', 'is_int')->rule('foo', 'is_string');
  52. $copy_data = array('foo' => 'no', 'fud' => 'maybe', 'num' => 42);
  53. $copy = $validation->copy($copy_data);
  54. $this->assertNotSame($validation, $copy);
  55. foreach (array('_rules', '_bound', '_labels', '_empty_rules', '_errors') as $attribute)
  56. {
  57. // This is just an easy way to check that the attributes are identical
  58. // Without hardcoding the expected values
  59. $this->assertAttributeSame(
  60. self::readAttribute($validation, $attribute),
  61. $attribute,
  62. $copy
  63. );
  64. }
  65. $this->assertSame($copy_data, $copy->data());
  66. }
  67. /**
  68. * When the validation object is initially created there should be no labels
  69. * specified
  70. *
  71. * @test
  72. */
  73. public function test_initially_there_are_no_labels()
  74. {
  75. $validation = new Validation(array());
  76. $this->assertAttributeSame(array(), '_labels', $validation);
  77. }
  78. /**
  79. * Adding a label to a field should set it in the labels array
  80. * If the label already exists it should overwrite it
  81. *
  82. * In both cases thefunction should return a reference to $this
  83. *
  84. * @test
  85. * @covers Validation::label
  86. */
  87. public function test_label_adds_and_overwrites_label_and_returns_this()
  88. {
  89. $validation = new Validation(array());
  90. $this->assertSame($validation, $validation->label('email', 'Email Address'));
  91. $this->assertAttributeSame(array('email' => 'Email Address'), '_labels', $validation);
  92. $this->assertSame($validation, $validation->label('email', 'Your Email'));
  93. $validation->label('name', 'Your Name');
  94. $this->assertAttributeSame(
  95. array('email' => 'Your Email', 'name' => 'Your Name'),
  96. '_labels',
  97. $validation
  98. );
  99. }
  100. /**
  101. * Using labels() we should be able to add / overwrite multiple labels
  102. *
  103. * The function should also return $this for chaining purposes
  104. *
  105. * @test
  106. * @covers Validation::labels
  107. */
  108. public function test_labels_adds_and_overwrites_multiple_labels_and_returns_this()
  109. {
  110. $validation = new Validation(array());
  111. $initial_data = array('kung fu' => 'fighting', 'fast' => 'cheetah');
  112. $this->assertSame($validation, $validation->labels($initial_data));
  113. $this->assertAttributeSame($initial_data, '_labels', $validation);
  114. $this->assertSame($validation, $validation->labels(array('fast' => 'lightning')));
  115. $this->assertAttributeSame(
  116. array('fast' => 'lightning', 'kung fu' => 'fighting'),
  117. '_labels',
  118. $validation
  119. );
  120. }
  121. /**
  122. * Using bind() we should be able to add / overwrite multiple bound variables
  123. *
  124. * The function should also return $this for chaining purposes
  125. *
  126. * @test
  127. * @covers Validation::bind
  128. */
  129. public function test_bind_adds_and_overwrites_multiple_variables_and_returns_this()
  130. {
  131. $validation = new Validation(array());
  132. $data = array('kung fu' => 'fighting', 'fast' => 'cheetah');
  133. $bound = array(':foo' => 'some value');
  134. // Test binding an array of values
  135. $this->assertSame($validation, $validation->bind($bound));
  136. $this->assertAttributeSame($bound, '_bound', $validation);
  137. // Test binding one value
  138. $this->assertSame($validation, $validation->bind(':foo', 'some other value'));
  139. $this->assertAttributeSame(array(':foo' => 'some other value'), '_bound', $validation);
  140. }
  141. /**
  142. * We should be able to used bound variables in callbacks
  143. *
  144. * @test
  145. * @covers Validation::check
  146. */
  147. public function test_bound_callback()
  148. {
  149. $data = array(
  150. 'kung fu' => 'fighting',
  151. 'fast' => 'cheetah',
  152. );
  153. $validation = new Validation($data);
  154. $validation->bind(':class', 'Valid')
  155. // Use the bound value in a callback
  156. ->rule('fast', array(':class', 'max_length'), array(':value', 2));
  157. // The rule should have run and check() should fail
  158. $this->assertSame($validation->check(), FALSE);
  159. }
  160. /**
  161. * Provides test data for test_check
  162. *
  163. * @return array
  164. */
  165. public function provider_check()
  166. {
  167. // $data_array, $rules, $labels, $first_expected, $expected_error
  168. return array(
  169. array(
  170. array('foo' => 'bar'),
  171. array('foo' => array(array('not_empty', NULL))),
  172. array(),
  173. TRUE,
  174. array(),
  175. ),
  176. array(
  177. array('unit' => 'test'),
  178. array(
  179. 'foo' => array(array('not_empty', NULL)),
  180. 'unit' => array(array('min_length', array(':value', 6))
  181. ),
  182. ),
  183. array(),
  184. FALSE,
  185. array(
  186. 'foo' => 'foo must not be empty',
  187. 'unit' => 'unit must be at least 6 characters long'
  188. ),
  189. ),
  190. array(
  191. array('foo' => 'bar'),
  192. array(
  193. // Tests wildcard rules
  194. TRUE => array(array('min_length', array(':value', 4))),
  195. 'foo' => array(
  196. array('not_empty', NULL),
  197. // Tests the array syntax for callbacks
  198. array(array('Valid', 'exact_length'), array(':value', 3)),
  199. // Tests the Class::method syntax for callbacks
  200. array('Valid::exact_length', array(':value', 3)),
  201. // Tests the lambda function syntax for callbacks
  202. // Commented out for PHP 5.2 support
  203. // array(function($value){return TRUE;}, array(':value')),
  204. // Tests using a function as a rule
  205. array('is_string', array(':value')),
  206. ),
  207. // Tests that rules do not run on empty fields unless they are in _empty_rules
  208. 'unit' => array(array('exact_length', array(':value', 4))),
  209. ),
  210. array(),
  211. FALSE,
  212. array('foo' => 'foo must be at least 4 characters long'),
  213. ),
  214. // Switch things around and make :value an array
  215. array(
  216. array('foo' => array('test', 'data')),
  217. array('foo' => array(array('in_array', array('kohana', ':value')))),
  218. array(),
  219. FALSE,
  220. array('foo' => 'foo must be one of the available options'),
  221. ),
  222. // Test wildcard rules with no other rules
  223. array(
  224. array('foo' => array('test')),
  225. array(TRUE => array(array('is_string', array(':value')))),
  226. array('foo' => 'foo'),
  227. FALSE,
  228. array('foo' => '1.foo.is_string'),
  229. ),
  230. // Test array rules use method as error name
  231. array(
  232. array('foo' => 'test'),
  233. array('foo' => array(array(array('Valid', 'min_length'), array(':value', 10)))),
  234. array(),
  235. FALSE,
  236. array('foo' => 'foo must be at least 10 characters long'),
  237. ),
  238. );
  239. }
  240. /**
  241. * Tests Validation::check()
  242. *
  243. * @test
  244. * @covers Validation::check
  245. * @covers Validation::rule
  246. * @covers Validation::rules
  247. * @covers Validation::errors
  248. * @covers Validation::error
  249. * @dataProvider provider_check
  250. * @param array $array The array of data
  251. * @param array $rules The array of rules
  252. * @param array $labels The array of labels
  253. * @param boolean $expected Is it valid?
  254. * @param boolean $expected_errors Array of expected errors
  255. */
  256. public function test_check($array, $rules, $labels, $expected, $expected_errors)
  257. {
  258. $validation = new Validation($array);
  259. foreach ($labels as $field => $label)
  260. {
  261. $validation->label($field, $label);
  262. }
  263. foreach ($rules as $field => $field_rules)
  264. {
  265. foreach ($field_rules as $rule)
  266. $validation->rule($field, $rule[0], $rule[1]);
  267. }
  268. $status = $validation->check();
  269. $errors = $validation->errors(TRUE);
  270. $this->assertSame($expected, $status);
  271. $this->assertSame($expected_errors, $errors);
  272. $validation = new validation($array);
  273. foreach ($rules as $field => $rules)
  274. {
  275. $validation->rules($field, $rules);
  276. }
  277. $validation->labels($labels);
  278. $this->assertSame($expected, $validation->check());
  279. }
  280. /**
  281. * Tests Validation::check()
  282. *
  283. * @test
  284. * @covers Validation::check
  285. */
  286. public function test_check_stops_when_error_added_by_callback()
  287. {
  288. $validation = new Validation(array(
  289. 'foo' => 'foo',
  290. ));
  291. $validation
  292. ->rule('foo', array($this, '_validation_callback'), array(':validation'))
  293. // This rule should never run
  294. ->rule('foo', 'min_length', array(':value', 20));
  295. $validation->check();
  296. $errors = $validation->errors();
  297. $expected = array(
  298. 'foo' => array(
  299. 0 => '_validation_callback',
  300. 1 => NULL,
  301. ),
  302. );
  303. $this->assertSame($errors, $expected);
  304. }
  305. public function _validation_callback(Validation $object)
  306. {
  307. // Simply add the error
  308. $object->error('foo', '_validation_callback');
  309. }
  310. /**
  311. * Provides test data for test_errors()
  312. *
  313. * @return array
  314. */
  315. public function provider_errors()
  316. {
  317. // [data, rules, expected], ...
  318. return array(
  319. // No Error
  320. array(
  321. array('username' => 'frank'),
  322. array('username' => array(array('not_empty', NULL))),
  323. array(),
  324. ),
  325. // Error from message file
  326. array(
  327. array('username' => ''),
  328. array('username' => array(array('not_empty', NULL))),
  329. array('username' => 'username must not be empty'),
  330. ),
  331. // No error message exists, display the path expected
  332. array(
  333. array('username' => 'John'),
  334. array('username' => array(array('strpos', array(':value', 'Kohana')))),
  335. array('username' => 'Validation.username.strpos'),
  336. ),
  337. );
  338. }
  339. /**
  340. * Tests Validation::errors()
  341. *
  342. * @test
  343. * @covers Validation::errors
  344. * @dataProvider provider_errors
  345. * @param array $array The array of data
  346. * @param array $rules The array of rules
  347. * @param array $expected Array of expected errors
  348. */
  349. public function test_errors($array, $rules, $expected)
  350. {
  351. $validation = Validation::factory($array);
  352. foreach ($rules as $field => $field_rules)
  353. {
  354. $validation->rules($field, $field_rules);
  355. }
  356. $validation->check();
  357. $this->assertSame($expected, $validation->errors('Validation', FALSE));
  358. // Should be able to get raw errors array
  359. $this->assertAttributeSame($validation->errors(NULL), '_errors', $validation);
  360. }
  361. /**
  362. * Provides test data for test_translated_errors()
  363. *
  364. * @return array
  365. */
  366. public function provider_translated_errors()
  367. {
  368. // [data, rules, expected], ...
  369. return array(
  370. array(
  371. array('Spanish' => ''),
  372. array('Spanish' => array(array('not_empty', NULL))),
  373. // Errors are not translated yet so only the label will translate
  374. array('Spanish' => 'Español must not be empty'),
  375. array('Spanish' => 'Spanish must not be empty'),
  376. ),
  377. );
  378. }
  379. /**
  380. * Tests Validation::errors()
  381. *
  382. * @test
  383. * @covers Validation::errors
  384. * @dataProvider provider_translated_errors
  385. * @param array $data The array of data to test
  386. * @param array $rules The array of rules to add
  387. * @param array $translated_expected The array of expected errors when translated
  388. * @param array $untranslated_expected The array of expected errors when not translated
  389. */
  390. public function test_translated_errors($data, $rules, $translated_expected, $untranslated_expected)
  391. {
  392. $validation = Validation::factory($data);
  393. $current = i18n::lang();
  394. i18n::lang('es');
  395. foreach ($rules as $field => $field_rules)
  396. {
  397. $validation->rules($field, $field_rules);
  398. }
  399. $validation->check();
  400. $result_1 = $validation->errors('Validation', TRUE);
  401. $result_2 = $validation->errors('Validation', 'en');
  402. $result_3 = $validation->errors('Validation', FALSE);
  403. // Restore the current language
  404. i18n::lang($current);
  405. $this->assertSame($translated_expected, $result_1);
  406. $this->assertSame($translated_expected, $result_2);
  407. $this->assertSame($untranslated_expected, $result_3);
  408. }
  409. /**
  410. * Tests Validation::errors()
  411. *
  412. * @test
  413. * @covers Validation::errors
  414. */
  415. public function test_parameter_labels()
  416. {
  417. $validation = Validation::factory(array('foo' => 'bar'))
  418. ->rule('foo', 'equals', array(':value', 'something'))
  419. ->label('something', 'Spanish');
  420. $current = i18n::lang();
  421. i18n::lang('es');
  422. $validation->check();
  423. $translated_expected = array('foo' => 'foo must equal Español');
  424. $untranslated_expected = array('foo' => 'foo must equal Spanish');
  425. $result_1 = $validation->errors('Validation', TRUE);
  426. $result_2 = $validation->errors('Validation', 'en');
  427. $result_3 = $validation->errors('Validation', FALSE);
  428. // Restore the current language
  429. i18n::lang($current);
  430. $this->assertSame($translated_expected, $result_1);
  431. $this->assertSame($translated_expected, $result_2);
  432. $this->assertSame($untranslated_expected, $result_3);
  433. }
  434. /**
  435. * Tests Validation::errors()
  436. *
  437. * @test
  438. * @covers Validation::errors
  439. */
  440. public function test_arrays_in_parameters()
  441. {
  442. $validation = Validation::factory(array('foo' => 'bar'))
  443. ->rule('foo', 'equals', array(':value', array('one', 'two')));
  444. $validation->check();
  445. $expected = array('foo' => 'foo must equal one, two');
  446. $this->assertSame($expected, $validation->errors('Validation', FALSE));
  447. }
  448. /**
  449. * Tests Validation::check()
  450. *
  451. * @test
  452. * @covers Validation::check
  453. */
  454. public function test_data_stays_unaltered()
  455. {
  456. $validation = Validation::factory(array('foo' => 'bar'))
  457. ->rule('something', 'not_empty');
  458. $before = $validation->data();
  459. $validation->check();
  460. $after = $validation->data();
  461. $expected = array('foo' => 'bar');
  462. $this->assertSame($expected, $before);
  463. $this->assertSame($expected, $after);
  464. }
  465. /**
  466. * Tests Validation::errors()
  467. *
  468. * @test
  469. * @covers Validation::errors
  470. */
  471. public function test_object_parameters_not_in_messages()
  472. {
  473. $validation = Validation::factory(array('foo' => 'foo'))
  474. ->rule('bar', 'matches', array(':validation', ':field', 'foo'));
  475. $validation->check();
  476. $errors = $validation->errors('validation');
  477. $expected = array('bar' => 'bar must be the same as foo');
  478. $this->assertSame($expected, $errors);
  479. }
  480. /**
  481. * Tests Validation::as_array()
  482. *
  483. * @test
  484. * @covers Validation::as_array
  485. */
  486. public function test_as_array_returns_original_array()
  487. {
  488. $data = array(
  489. 'one' => 'hello',
  490. 'two' => 'world',
  491. 'ten' => '',
  492. );
  493. $validation = Validation::factory($data);
  494. $this->assertSame($data, $validation->as_array());
  495. }
  496. /**
  497. * Tests Validation::data()
  498. *
  499. * @test
  500. * @covers Validation::data
  501. */
  502. public function test_data_returns_original_array()
  503. {
  504. $data = array(
  505. 'one' => 'hello',
  506. 'two' => 'world',
  507. 'ten' => '',
  508. );
  509. $validation = Validation::factory($data);
  510. $this->assertSame($data, $validation->data());
  511. }
  512. // @codingStandardsIgnoreStart
  513. public function test_offsetExists()
  514. // @codingStandardsIgnoreEnd
  515. {
  516. $array = array(
  517. 'one' => 'Hello',
  518. 'two' => 'World',
  519. 'ten' => NULL,
  520. );
  521. $validation = Validation::factory($array);
  522. $this->assertTrue(isset($validation['one']));
  523. $this->assertFalse(isset($validation['ten']));
  524. $this->assertFalse(isset($validation['five']));
  525. }
  526. // @codingStandardsIgnoreStart
  527. public function test_offsetSet_throws_exception()
  528. // @codingStandardsIgnoreEnd
  529. {
  530. $this->setExpectedException('Kohana_Exception');
  531. $validation = Validation::factory(array());
  532. // Validation is read-only
  533. $validation['field'] = 'something';
  534. }
  535. // @codingStandardsIgnoreStart
  536. public function test_offsetGet()
  537. // @codingStandardsIgnoreEnd
  538. {
  539. $array = array(
  540. 'one' => 'Hello',
  541. 'two' => 'World',
  542. 'ten' => NULL,
  543. );
  544. $validation = Validation::factory($array);
  545. $this->assertSame($array['one'], $validation['one']);
  546. $this->assertSame($array['two'], $validation['two']);
  547. $this->assertSame($array['ten'], $validation['ten']);
  548. }
  549. // @codingStandardsIgnoreStart
  550. public function test_offsetUnset()
  551. // @codingStandardsIgnoreEnd
  552. {
  553. $this->setExpectedException('Kohana_Exception');
  554. $validation = Validation::factory(array(
  555. 'one' => 'Hello, World!',
  556. ));
  557. // Validation is read-only
  558. unset($validation['one']);
  559. }
  560. /**
  561. * http://dev.kohanaframework.org/issues/4365
  562. *
  563. * @test
  564. * @covers Validation::errors
  565. */
  566. public function test_error_type_check()
  567. {
  568. $array = array(
  569. 'email' => 'not an email address',
  570. );
  571. $validation = Validation::factory($array)
  572. ->rule('email', 'not_empty')
  573. ->rule('email', 'email')
  574. ;
  575. $validation->check();
  576. $errors = $validation->errors('tests/validation/error_type_check');
  577. $this->assertSame($errors, $validation->errors('validation'));
  578. }
  579. }