ValidatorTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. ];
  354. $this->assertEquals($expected, $context);
  355. return "That ain't cool, yo";
  356. }));
  357. $validator->provider('thing', $thing);
  358. $errors = $validator->errors(['email' => '!', 'title' => 'bar']);
  359. $expected = [
  360. 'email' => ['alpha' => 'The provided value is invalid'],
  361. 'title' => ['cool' => "That ain't cool, yo"]
  362. ];
  363. $this->assertEquals($expected, $errors);
  364. }
  365. /**
  366. * Tests that it is possible to pass extra arguments to the validation function
  367. * and it still gets the providers as last argument
  368. *
  369. * @return void
  370. */
  371. public function testMethodsWithExtraArguments() {
  372. $validator = new Validator;
  373. $validator->add('title', 'cool', [
  374. 'rule' => ['isCool', 'and', 'awesome'],
  375. 'provider' => 'thing'
  376. ]);
  377. $thing = $this->getMock('\stdClass', ['isCool']);
  378. $thing->expects($this->once())->method('isCool')
  379. ->will($this->returnCallback(function($data, $a, $b, $context) use ($thing) {
  380. $this->assertEquals('bar', $data);
  381. $this->assertEquals('and', $a);
  382. $this->assertEquals('awesome', $b);
  383. $expected = [
  384. 'default' => new \Cake\Validation\RulesProvider,
  385. 'thing' => $thing
  386. ];
  387. $expected = [
  388. 'newRecord' => true,
  389. 'providers' => $expected,
  390. 'data' => [
  391. 'email' => '!',
  392. 'title' => 'bar'
  393. ]
  394. ];
  395. $this->assertEquals($expected, $context);
  396. return "That ain't cool, yo";
  397. }));
  398. $validator->provider('thing', $thing);
  399. $errors = $validator->errors(['email' => '!', 'title' => 'bar']);
  400. $expected = [
  401. 'title' => ['cool' => "That ain't cool, yo"]
  402. ];
  403. $this->assertEquals($expected, $errors);
  404. }
  405. /**
  406. * Tests that it is possible to use a closure as a rule
  407. *
  408. * @return void
  409. */
  410. public function testUsingClosureAsRule() {
  411. $validator = new Validator;
  412. $validator->add('name', 'myRule', [
  413. 'rule' => function($data, $provider) {
  414. $this->assertEquals('foo', $data);
  415. return 'You fail';
  416. }
  417. ]);
  418. $expected = ['name' => ['myRule' => 'You fail']];
  419. $this->assertEquals($expected, $validator->errors(['name' => 'foo']));
  420. }
  421. /**
  422. * Tests that setting last to a rule will stop validating the rest of the rules
  423. *
  424. * @return void
  425. */
  426. public function testErrorsWithLastRule() {
  427. $validator = new Validator;
  428. $validator
  429. ->add('email', 'alpha', ['rule' => 'alphanumeric', 'last' => true])
  430. ->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
  431. $errors = $validator->errors(['email' => 'not an email!']);
  432. $expected = [
  433. 'email' => [
  434. 'alpha' => 'The provided value is invalid'
  435. ]
  436. ];
  437. $this->assertEquals($expected, $errors);
  438. }
  439. /**
  440. * Tests it is possible to get validation sets for a field using an array interface
  441. *
  442. * @return void
  443. */
  444. public function testArrayAccessGet() {
  445. $validator = new Validator;
  446. $validator
  447. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  448. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  449. $this->assertSame($validator['email'], $validator->field('email'));
  450. $this->assertSame($validator['title'], $validator->field('title'));
  451. }
  452. /**
  453. * Tests it is possible to check for validation sets for a field using an array inteface
  454. *
  455. * @return void
  456. */
  457. public function testArrayAccessExists() {
  458. $validator = new Validator;
  459. $validator
  460. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  461. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  462. $this->assertTrue(isset($validator['email']));
  463. $this->assertTrue(isset($validator['title']));
  464. $this->assertFalse(isset($validator['foo']));
  465. }
  466. /**
  467. * Tests it is possible to set validation rules for a field using an array inteface
  468. *
  469. * @return void
  470. */
  471. public function testArrayAccessSet() {
  472. $validator = new Validator;
  473. $validator
  474. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  475. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  476. $validator['name'] = $validator->field('title');
  477. $this->assertSame($validator->field('title'), $validator->field('name'));
  478. $validator['name'] = ['alpha' => ['rule' => 'alphanumeric']];
  479. $this->assertEquals($validator->field('email'), $validator->field('email'));
  480. }
  481. /**
  482. * Tests it is possible to unset validation rules
  483. *
  484. * @return void
  485. */
  486. public function testArrayAccessUset() {
  487. $validator = new Validator;
  488. $validator
  489. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  490. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  491. $this->assertTrue(isset($validator['title']));
  492. unset($validator['title']);
  493. $this->assertFalse(isset($validator['title']));
  494. }
  495. /**
  496. * Tests the countable interface
  497. *
  498. * @return void
  499. */
  500. public function testCount() {
  501. $validator = new Validator;
  502. $validator
  503. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  504. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  505. $this->assertCount(2, $validator);
  506. }
  507. /**
  508. * Tests adding rules via alternative syntax
  509. *
  510. * @return void
  511. */
  512. public function testAddMulitple() {
  513. $validator = new Validator;
  514. $validator->add('title', [
  515. 'notEmpty' => [
  516. 'rule' => 'notEmpty'
  517. ],
  518. 'length' => [
  519. 'rule' => ['minLength', 10],
  520. 'message' => 'Titles need to be at least 10 characters long'
  521. ]
  522. ]);
  523. $set = $validator->field('title');
  524. $this->assertInstanceOf('\Cake\Validation\ValidationSet', $set);
  525. $this->assertCount(2, $set);
  526. }
  527. }