ValidatorTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Validation;
  16. use \Cake\Validation\ValidationSet;
  17. use \Cake\Validation\Validator;
  18. /**
  19. * Tests Validator class
  20. *
  21. */
  22. class ValidatorTest extends \Cake\TestSuite\TestCase {
  23. /**
  24. * Testing you can dynamically add rules to a field
  25. *
  26. * @return void
  27. */
  28. public function testAddingRulesToField() {
  29. $validator = new Validator;
  30. $validator->add('title', 'not-empty', ['rule' => 'notEmpty']);
  31. $set = $validator->field('title');
  32. $this->assertInstanceOf('\Cake\Validation\ValidationSet', $set);
  33. $this->assertCount(1, $set);
  34. $validator->add('title', 'another', ['rule' => 'alphanumeric']);
  35. $this->assertCount(2, $set);
  36. $validator->add('body', 'another', ['rule' => 'crazy']);
  37. $this->assertCount(1, $validator->field('body'));
  38. $this->assertCount(2, $validator);
  39. }
  40. /**
  41. * Tests that calling field will create a default validation set for it
  42. *
  43. * @return void
  44. */
  45. public function testFieldDefault() {
  46. $validator = new Validator;
  47. $this->assertFalse($validator->hasField('foo'));
  48. $field = $validator->field('foo');
  49. $this->assertInstanceOf('\Cake\Validation\ValidationSet', $field);
  50. $this->assertCount(0, $field);
  51. $this->assertTrue($validator->hasField('foo'));
  52. }
  53. /**
  54. * Tests that field method can be used as a setter
  55. *
  56. * @return void
  57. */
  58. public function testFieldSetter() {
  59. $validator = new Validator;
  60. $validationSet = new ValidationSet;
  61. $validator->field('thing', $validationSet);
  62. $this->assertSame($validationSet, $validator->field('thing'));
  63. }
  64. /**
  65. * Tests the remove method
  66. *
  67. * @return void
  68. */
  69. public function testRemove() {
  70. $validator = new Validator;
  71. $validator->add('title', 'not-empty', ['rule' => 'notEmpty']);
  72. $validator->add('title', 'foo', ['rule' => 'bar']);
  73. $this->assertCount(2, $validator->field('title'));
  74. $validator->remove('title');
  75. $this->assertCount(0, $validator->field('title'));
  76. $validator->remove('title');
  77. $validator->add('title', 'not-empty', ['rule' => 'notEmpty']);
  78. $validator->add('title', 'foo', ['rule' => 'bar']);
  79. $this->assertCount(2, $validator->field('title'));
  80. $validator->remove('title', 'foo');
  81. $this->assertCount(1, $validator->field('title'));
  82. $this->assertNull($validator->field('title')->rule('foo'));
  83. }
  84. /**
  85. * Tests the validatePresence method
  86. *
  87. * @return void
  88. */
  89. public function testValidatePresence() {
  90. $validator = new Validator;
  91. $this->assertSame($validator, $validator->validatePresence('title'));
  92. $this->assertTrue($validator->field('title')->isPresenceRequired());
  93. $validator->validatePresence('title', false);
  94. $this->assertFalse($validator->field('title')->isPresenceRequired());
  95. $validator->validatePresence('title', 'create');
  96. $this->assertEquals('create', $validator->field('title')->isPresenceRequired());
  97. $validator->validatePresence('title', 'update');
  98. $this->assertEquals('update', $validator->field('title')->isPresenceRequired());
  99. }
  100. /**
  101. * Tests the isPresenceRequired method
  102. *
  103. * @return void
  104. */
  105. public function testIsPresenceRequired() {
  106. $validator = new Validator;
  107. $this->assertSame($validator, $validator->validatePresence('title'));
  108. $this->assertTrue($validator->isPresenceRequired('title', true));
  109. $this->assertTrue($validator->isPresenceRequired('title', false));
  110. $validator->validatePresence('title', false);
  111. $this->assertFalse($validator->isPresenceRequired('title', true));
  112. $this->assertFalse($validator->isPresenceRequired('title', false));
  113. $validator->validatePresence('title', 'create');
  114. $this->assertTrue($validator->isPresenceRequired('title', true));
  115. $this->assertFalse($validator->isPresenceRequired('title', false));
  116. $validator->validatePresence('title', 'update');
  117. $this->assertTrue($validator->isPresenceRequired('title', false));
  118. $this->assertFalse($validator->isPresenceRequired('title', true));
  119. }
  120. /**
  121. * Tests errors generated when a field presence is required
  122. *
  123. * @return void
  124. */
  125. public function testErrorsWithPresenceRequired() {
  126. $validator = new Validator;
  127. $validator->validatePresence('title');
  128. $errors = $validator->errors(['foo' => 'something']);
  129. $expected = ['title' => ['This field is required']];
  130. $this->assertEquals($expected, $errors);
  131. $this->assertEmpty($validator->errors(['title' => 'bar']));
  132. $validator->validatePresence('title', false);
  133. $this->assertEmpty($validator->errors(['foo' => 'bar']));
  134. }
  135. /**
  136. * Tests custom error messages generated when a field presence is required
  137. *
  138. * @return void
  139. */
  140. public function testCustomErrorsWithPresenceRequired() {
  141. $validator = new Validator;
  142. $validator->validatePresence('title', true, 'Custom message');
  143. $errors = $validator->errors(['foo' => 'something']);
  144. $expected = ['title' => ['Custom message']];
  145. $this->assertEquals($expected, $errors);
  146. }
  147. /**
  148. * Tests the allowEmpty method
  149. *
  150. * @return void
  151. */
  152. public function testAllowEmpty() {
  153. $validator = new Validator;
  154. $this->assertSame($validator, $validator->allowEmpty('title'));
  155. $this->assertTrue($validator->field('title')->isEmptyAllowed());
  156. $validator->allowEmpty('title', 'create');
  157. $this->assertEquals('create', $validator->field('title')->isEmptyAllowed());
  158. $validator->allowEmpty('title', 'update');
  159. $this->assertEquals('update', $validator->field('title')->isEmptyAllowed());
  160. }
  161. /**
  162. * Test the notEmpty() method.
  163. *
  164. * @return void
  165. */
  166. public function testNotEmpty() {
  167. $validator = new Validator;
  168. $validator->notEmpty('title');
  169. $this->assertFalse($validator->field('title')->isEmptyAllowed());
  170. $validator->allowEmpty('title');
  171. $this->assertTrue($validator->field('title')->isEmptyAllowed());
  172. }
  173. /**
  174. * Test the notEmpty() method.
  175. *
  176. * @return void
  177. */
  178. public function testNotEmptyModes() {
  179. $validator = new Validator;
  180. $validator->notEmpty('title', 'Need a title', 'create');
  181. $this->assertFalse($validator->isEmptyAllowed('title', true));
  182. $this->assertTrue($validator->isEmptyAllowed('title', false));
  183. $validator->notEmpty('title', 'Need a title', 'update');
  184. $this->assertTrue($validator->isEmptyAllowed('title', true));
  185. $this->assertFalse($validator->isEmptyAllowed('title', false));
  186. $validator->notEmpty('title', 'Need a title');
  187. $this->assertFalse($validator->isEmptyAllowed('title', true));
  188. $this->assertFalse($validator->isEmptyAllowed('title', false));
  189. $validator->notEmpty('title');
  190. $this->assertFalse($validator->isEmptyAllowed('title', true));
  191. $this->assertFalse($validator->isEmptyAllowed('title', false));
  192. }
  193. /**
  194. * Test interactions between notEmpty() and isAllowed().
  195. *
  196. * @return void
  197. */
  198. public function testNotEmptyAndIsAllowed() {
  199. $validator = new Validator;
  200. $validator->allowEmpty('title')
  201. ->notEmpty('title', 'Need it', 'update');
  202. $this->assertTrue($validator->isEmptyAllowed('title', true));
  203. $this->assertFalse($validator->isEmptyAllowed('title', false));
  204. $validator->allowEmpty('title')
  205. ->notEmpty('title');
  206. $this->assertFalse($validator->isEmptyAllowed('title', true));
  207. $this->assertFalse($validator->isEmptyAllowed('title', false));
  208. $validator->notEmpty('title')
  209. ->allowEmpty('title', 'create');
  210. $this->assertTrue($validator->isEmptyAllowed('title', true));
  211. $this->assertFalse($validator->isEmptyAllowed('title', false));
  212. }
  213. /**
  214. * Tests the isEmptyAllowed method
  215. *
  216. * @return void
  217. */
  218. public function testIsEmptyAllowed() {
  219. $validator = new Validator;
  220. $this->assertSame($validator, $validator->allowEmpty('title'));
  221. $this->assertTrue($validator->isEmptyAllowed('title', true));
  222. $this->assertTrue($validator->isEmptyAllowed('title', false));
  223. $validator->notEmpty('title');
  224. $this->assertFalse($validator->isEmptyAllowed('title', true));
  225. $this->assertFalse($validator->isEmptyAllowed('title', false));
  226. $validator->allowEmpty('title', 'create');
  227. $this->assertTrue($validator->isEmptyAllowed('title', true));
  228. $this->assertFalse($validator->isEmptyAllowed('title', false));
  229. $validator->allowEmpty('title', 'update');
  230. $this->assertTrue($validator->isEmptyAllowed('title', false));
  231. $this->assertFalse($validator->isEmptyAllowed('title', true));
  232. }
  233. /**
  234. * Tests errors generated when a field is not allowed to be empty
  235. *
  236. * @return void
  237. */
  238. public function testErrorsWithEmptyNotAllowed() {
  239. $validator = new Validator;
  240. $validator->notEmpty('title');
  241. $errors = $validator->errors(['title' => '']);
  242. $expected = ['title' => ['This field cannot be left empty']];
  243. $this->assertEquals($expected, $errors);
  244. $errors = $validator->errors(['title' => []]);
  245. $expected = ['title' => ['This field cannot be left empty']];
  246. $this->assertEquals($expected, $errors);
  247. $errors = $validator->errors(['title' => null]);
  248. $expected = ['title' => ['This field cannot be left empty']];
  249. $this->assertEquals($expected, $errors);
  250. $errors = $validator->errors(['title' => 0]);
  251. $this->assertEmpty($errors);
  252. $errors = $validator->errors(['title' => '0']);
  253. $this->assertEmpty($errors);
  254. $errors = $validator->errors(['title' => false]);
  255. $this->assertEmpty($errors);
  256. }
  257. /**
  258. * Tests custom error mesages generated when a field is not allowed to be empty
  259. *
  260. * @return void
  261. */
  262. public function testCustomErrorsWithEmptyNotAllowed() {
  263. $validator = new Validator;
  264. $validator->notEmpty('title', 'Custom message');
  265. $errors = $validator->errors(['title' => '']);
  266. $expected = ['title' => ['Custom message']];
  267. $this->assertEquals($expected, $errors);
  268. }
  269. /**
  270. * Tests errors generated when a field is allowed to be empty
  271. *
  272. * @return void
  273. */
  274. public function testErrorsWithEmptyAllowed() {
  275. $validator = new Validator;
  276. $validator->allowEmpty('title');
  277. $errors = $validator->errors(['title' => '']);
  278. $this->assertEmpty($errors);
  279. $errors = $validator->errors(['title' => []]);
  280. $this->assertEmpty($errors);
  281. $errors = $validator->errors(['title' => null]);
  282. $this->assertEmpty($errors);
  283. $errors = $validator->errors(['title' => 0]);
  284. $this->assertEmpty($errors);
  285. $errors = $validator->errors(['title' => '0']);
  286. $this->assertEmpty($errors);
  287. $errors = $validator->errors(['title' => false]);
  288. $this->assertEmpty($errors);
  289. }
  290. /**
  291. * Test the provider() method
  292. *
  293. * @return void
  294. */
  295. public function testProvider() {
  296. $validator = new Validator;
  297. $object = new \stdClass;
  298. $this->assertSame($validator, $validator->provider('foo', $object));
  299. $this->assertSame($object, $validator->provider('foo'));
  300. $this->assertNull($validator->provider('bar'));
  301. $another = new \stdClass;
  302. $this->assertSame($validator, $validator->provider('bar', $another));
  303. $this->assertSame($another, $validator->provider('bar'));
  304. $this->assertEquals(new \Cake\Validation\RulesProvider, $validator->provider('default'));
  305. }
  306. /**
  307. * Tests errors() method when using validators from the default provider, this proves
  308. * that it returns a default validation message and the custom one set in the rule
  309. *
  310. * @return void
  311. */
  312. public function testErrorsFromDefaultProvider() {
  313. $validator = new Validator;
  314. $validator
  315. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  316. ->add('email', 'notEmpty', ['rule' => 'notEmpty'])
  317. ->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
  318. $errors = $validator->errors(['email' => 'not an email!']);
  319. $expected = [
  320. 'email' => [
  321. 'alpha' => 'The provided value is invalid',
  322. 'email' => 'Y u no write email?'
  323. ]
  324. ];
  325. $this->assertEquals($expected, $errors);
  326. }
  327. /**
  328. * Tests using validation methods from different providers and returning the error
  329. * as a string
  330. *
  331. * @return void
  332. */
  333. public function testErrorsFromCustomProvider() {
  334. $validator = new Validator;
  335. $validator
  336. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  337. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  338. $thing = $this->getMock('\stdClass', ['isCool']);
  339. $thing->expects($this->once())->method('isCool')
  340. ->will($this->returnCallback(function($data, $context) use ($thing) {
  341. $this->assertEquals('bar', $data);
  342. $expected = [
  343. 'default' => new \Cake\Validation\RulesProvider,
  344. 'thing' => $thing
  345. ];
  346. $expected = [
  347. 'newRecord' => true,
  348. 'providers' => $expected,
  349. 'data' => [
  350. 'email' => '!',
  351. 'title' => 'bar'
  352. ],
  353. 'field' => 'title'
  354. ];
  355. $this->assertEquals($expected, $context);
  356. return "That ain't cool, yo";
  357. }));
  358. $validator->provider('thing', $thing);
  359. $errors = $validator->errors(['email' => '!', 'title' => 'bar']);
  360. $expected = [
  361. 'email' => ['alpha' => 'The provided value is invalid'],
  362. 'title' => ['cool' => "That ain't cool, yo"]
  363. ];
  364. $this->assertEquals($expected, $errors);
  365. }
  366. /**
  367. * Tests that it is possible to pass extra arguments to the validation function
  368. * and it still gets the providers as last argument
  369. *
  370. * @return void
  371. */
  372. public function testMethodsWithExtraArguments() {
  373. $validator = new Validator;
  374. $validator->add('title', 'cool', [
  375. 'rule' => ['isCool', 'and', 'awesome'],
  376. 'provider' => 'thing'
  377. ]);
  378. $thing = $this->getMock('\stdClass', ['isCool']);
  379. $thing->expects($this->once())->method('isCool')
  380. ->will($this->returnCallback(function($data, $a, $b, $context) use ($thing) {
  381. $this->assertEquals('bar', $data);
  382. $this->assertEquals('and', $a);
  383. $this->assertEquals('awesome', $b);
  384. $expected = [
  385. 'default' => new \Cake\Validation\RulesProvider,
  386. 'thing' => $thing
  387. ];
  388. $expected = [
  389. 'newRecord' => true,
  390. 'providers' => $expected,
  391. 'data' => [
  392. 'email' => '!',
  393. 'title' => 'bar'
  394. ],
  395. 'field' => 'title'
  396. ];
  397. $this->assertEquals($expected, $context);
  398. return "That ain't cool, yo";
  399. }));
  400. $validator->provider('thing', $thing);
  401. $errors = $validator->errors(['email' => '!', 'title' => 'bar']);
  402. $expected = [
  403. 'title' => ['cool' => "That ain't cool, yo"]
  404. ];
  405. $this->assertEquals($expected, $errors);
  406. }
  407. /**
  408. * Tests that it is possible to use a closure as a rule
  409. *
  410. * @return void
  411. */
  412. public function testUsingClosureAsRule() {
  413. $validator = new Validator;
  414. $validator->add('name', 'myRule', [
  415. 'rule' => function($data, $provider) {
  416. $this->assertEquals('foo', $data);
  417. return 'You fail';
  418. }
  419. ]);
  420. $expected = ['name' => ['myRule' => 'You fail']];
  421. $this->assertEquals($expected, $validator->errors(['name' => 'foo']));
  422. }
  423. /**
  424. * Tests that setting last to a rule will stop validating the rest of the rules
  425. *
  426. * @return void
  427. */
  428. public function testErrorsWithLastRule() {
  429. $validator = new Validator;
  430. $validator
  431. ->add('email', 'alpha', ['rule' => 'alphanumeric', 'last' => true])
  432. ->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
  433. $errors = $validator->errors(['email' => 'not an email!']);
  434. $expected = [
  435. 'email' => [
  436. 'alpha' => 'The provided value is invalid'
  437. ]
  438. ];
  439. $this->assertEquals($expected, $errors);
  440. }
  441. /**
  442. * Tests it is possible to get validation sets for a field using an array interface
  443. *
  444. * @return void
  445. */
  446. public function testArrayAccessGet() {
  447. $validator = new Validator;
  448. $validator
  449. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  450. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  451. $this->assertSame($validator['email'], $validator->field('email'));
  452. $this->assertSame($validator['title'], $validator->field('title'));
  453. }
  454. /**
  455. * Tests it is possible to check for validation sets for a field using an array inteface
  456. *
  457. * @return void
  458. */
  459. public function testArrayAccessExists() {
  460. $validator = new Validator;
  461. $validator
  462. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  463. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  464. $this->assertTrue(isset($validator['email']));
  465. $this->assertTrue(isset($validator['title']));
  466. $this->assertFalse(isset($validator['foo']));
  467. }
  468. /**
  469. * Tests it is possible to set validation rules for a field using an array inteface
  470. *
  471. * @return void
  472. */
  473. public function testArrayAccessSet() {
  474. $validator = new Validator;
  475. $validator
  476. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  477. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  478. $validator['name'] = $validator->field('title');
  479. $this->assertSame($validator->field('title'), $validator->field('name'));
  480. $validator['name'] = ['alpha' => ['rule' => 'alphanumeric']];
  481. $this->assertEquals($validator->field('email'), $validator->field('email'));
  482. }
  483. /**
  484. * Tests it is possible to unset validation rules
  485. *
  486. * @return void
  487. */
  488. public function testArrayAccessUset() {
  489. $validator = new Validator;
  490. $validator
  491. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  492. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  493. $this->assertTrue(isset($validator['title']));
  494. unset($validator['title']);
  495. $this->assertFalse(isset($validator['title']));
  496. }
  497. /**
  498. * Tests the countable interface
  499. *
  500. * @return void
  501. */
  502. public function testCount() {
  503. $validator = new Validator;
  504. $validator
  505. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  506. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  507. $this->assertCount(2, $validator);
  508. }
  509. /**
  510. * Tests adding rules via alternative syntax
  511. *
  512. * @return void
  513. */
  514. public function testAddMulitple() {
  515. $validator = new Validator;
  516. $validator->add('title', [
  517. 'notEmpty' => [
  518. 'rule' => 'notEmpty'
  519. ],
  520. 'length' => [
  521. 'rule' => ['minLength', 10],
  522. 'message' => 'Titles need to be at least 10 characters long'
  523. ]
  524. ]);
  525. $set = $validator->field('title');
  526. $this->assertInstanceOf('\Cake\Validation\ValidationSet', $set);
  527. $this->assertCount(2, $set);
  528. }
  529. }