ValidationTest.php 15 KB

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