ValidatorTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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', 'created');
  157. $this->assertEquals('created', $validator->field('title')->isEmptyAllowed());
  158. $validator->allowEmpty('title', 'updated');
  159. $this->assertEquals('updated', $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. * Tests the isEmptyAllowed method
  175. *
  176. * @return void
  177. */
  178. public function testIsEmptyAllowed() {
  179. $validator = new Validator;
  180. $this->assertSame($validator, $validator->allowEmpty('title'));
  181. $this->assertTrue($validator->isEmptyAllowed('title', true));
  182. $this->assertTrue($validator->isEmptyAllowed('title', false));
  183. $validator->notEmpty('title');
  184. $this->assertFalse($validator->isEmptyAllowed('title', true));
  185. $this->assertFalse($validator->isEmptyAllowed('title', false));
  186. $validator->allowEmpty('title', 'create');
  187. $this->assertTrue($validator->isEmptyAllowed('title', true));
  188. $this->assertFalse($validator->isEmptyAllowed('title', false));
  189. $validator->allowEmpty('title', 'update');
  190. $this->assertTrue($validator->isEmptyAllowed('title', false));
  191. $this->assertFalse($validator->isEmptyAllowed('title', true));
  192. }
  193. /**
  194. * Tests errors generated when a field is not allowed to be empty
  195. *
  196. * @return void
  197. */
  198. public function testErrorsWithEmptyNotAllowed() {
  199. $validator = new Validator;
  200. $validator->notEmpty('title');
  201. $errors = $validator->errors(['title' => '']);
  202. $expected = ['title' => ['This field cannot be left empty']];
  203. $this->assertEquals($expected, $errors);
  204. $errors = $validator->errors(['title' => []]);
  205. $expected = ['title' => ['This field cannot be left empty']];
  206. $this->assertEquals($expected, $errors);
  207. $errors = $validator->errors(['title' => null]);
  208. $expected = ['title' => ['This field cannot be left empty']];
  209. $this->assertEquals($expected, $errors);
  210. $errors = $validator->errors(['title' => 0]);
  211. $this->assertEmpty($errors);
  212. $errors = $validator->errors(['title' => '0']);
  213. $this->assertEmpty($errors);
  214. $errors = $validator->errors(['title' => false]);
  215. $this->assertEmpty($errors);
  216. }
  217. /**
  218. * Tests custom error mesages generated when a field is not allowed to be empty
  219. *
  220. * @return void
  221. */
  222. public function testCustomErrorsWithEmptyNotAllowed() {
  223. $validator = new Validator;
  224. $validator->notEmpty('title', 'Custom message');
  225. $errors = $validator->errors(['title' => '']);
  226. $expected = ['title' => ['Custom message']];
  227. $this->assertEquals($expected, $errors);
  228. }
  229. /**
  230. * Tests errors generated when a field is allowed to be empty
  231. *
  232. * @return void
  233. */
  234. public function testErrorsWithEmptyAllowed() {
  235. $validator = new Validator;
  236. $validator->allowEmpty('title');
  237. $errors = $validator->errors(['title' => '']);
  238. $this->assertEmpty($errors);
  239. $errors = $validator->errors(['title' => []]);
  240. $this->assertEmpty($errors);
  241. $errors = $validator->errors(['title' => null]);
  242. $this->assertEmpty($errors);
  243. $errors = $validator->errors(['title' => 0]);
  244. $this->assertEmpty($errors);
  245. $errors = $validator->errors(['title' => '0']);
  246. $this->assertEmpty($errors);
  247. $errors = $validator->errors(['title' => false]);
  248. $this->assertEmpty($errors);
  249. }
  250. /**
  251. * Test the provider() method
  252. *
  253. * @return void
  254. */
  255. public function testProvider() {
  256. $validator = new Validator;
  257. $object = new \stdClass;
  258. $this->assertSame($validator, $validator->provider('foo', $object));
  259. $this->assertSame($object, $validator->provider('foo'));
  260. $this->assertNull($validator->provider('bar'));
  261. $another = new \stdClass;
  262. $this->assertSame($validator, $validator->provider('bar', $another));
  263. $this->assertSame($another, $validator->provider('bar'));
  264. $this->assertEquals(new \Cake\Validation\RulesProvider, $validator->provider('default'));
  265. }
  266. /**
  267. * Tests errors() method when using validators from the default provider, this proves
  268. * that it returns a default validation message and the custom one set in the rule
  269. *
  270. * @return void
  271. */
  272. public function testErrorsFromDefaultProvider() {
  273. $validator = new Validator;
  274. $validator
  275. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  276. ->add('email', 'notEmpty', ['rule' => 'notEmpty'])
  277. ->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
  278. $errors = $validator->errors(['email' => 'not an email!']);
  279. $expected = [
  280. 'email' => [
  281. 'alpha' => 'The provided value is invalid',
  282. 'email' => 'Y u no write email?'
  283. ]
  284. ];
  285. $this->assertEquals($expected, $errors);
  286. }
  287. /**
  288. * Tests using validation methods from different providers and returning the error
  289. * as a string
  290. *
  291. * @return void
  292. */
  293. public function testErrorsFromCustomProvider() {
  294. $validator = new Validator;
  295. $validator
  296. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  297. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  298. $thing = $this->getMock('\stdClass', ['isCool']);
  299. $thing->expects($this->once())->method('isCool')
  300. ->will($this->returnCallback(function($data, $context) use ($thing) {
  301. $this->assertEquals('bar', $data);
  302. $expected = [
  303. 'default' => new \Cake\Validation\RulesProvider,
  304. 'thing' => $thing
  305. ];
  306. $expected = [
  307. 'newRecord' => true,
  308. 'providers' => $expected,
  309. 'data' => [
  310. 'email' => '!',
  311. 'title' => 'bar'
  312. ]
  313. ];
  314. $this->assertEquals($expected, $context);
  315. return "That ain't cool, yo";
  316. }));
  317. $validator->provider('thing', $thing);
  318. $errors = $validator->errors(['email' => '!', 'title' => 'bar']);
  319. $expected = [
  320. 'email' => ['alpha' => 'The provided value is invalid'],
  321. 'title' => ['cool' => "That ain't cool, yo"]
  322. ];
  323. $this->assertEquals($expected, $errors);
  324. }
  325. /**
  326. * Tests that it is possible to pass extra arguments to the validation function
  327. * and it still gets the providers as last argument
  328. *
  329. * @return void
  330. */
  331. public function testMethodsWithExtraArguments() {
  332. $validator = new Validator;
  333. $validator->add('title', 'cool', [
  334. 'rule' => ['isCool', 'and', 'awesome'],
  335. 'provider' => 'thing'
  336. ]);
  337. $thing = $this->getMock('\stdClass', ['isCool']);
  338. $thing->expects($this->once())->method('isCool')
  339. ->will($this->returnCallback(function($data, $a, $b, $context) use ($thing) {
  340. $this->assertEquals('bar', $data);
  341. $this->assertEquals('and', $a);
  342. $this->assertEquals('awesome', $b);
  343. $expected = [
  344. 'default' => new \Cake\Validation\RulesProvider,
  345. 'thing' => $thing
  346. ];
  347. $expected = [
  348. 'newRecord' => true,
  349. 'providers' => $expected,
  350. 'data' => [
  351. 'email' => '!',
  352. 'title' => 'bar'
  353. ]
  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. 'title' => ['cool' => "That ain't cool, yo"]
  362. ];
  363. $this->assertEquals($expected, $errors);
  364. }
  365. /**
  366. * Tests that it is possible to use a closure as a rule
  367. *
  368. * @return void
  369. */
  370. public function testUsingClosureAsRule() {
  371. $validator = new Validator;
  372. $validator->add('name', 'myRule', [
  373. 'rule' => function($data, $provider) {
  374. $this->assertEquals('foo', $data);
  375. return 'You fail';
  376. }
  377. ]);
  378. $expected = ['name' => ['myRule' => 'You fail']];
  379. $this->assertEquals($expected, $validator->errors(['name' => 'foo']));
  380. }
  381. /**
  382. * Tests that setting last to a rule will stop validating the rest of the rules
  383. *
  384. * @return void
  385. */
  386. public function testErrorsWithLastRule() {
  387. $validator = new Validator;
  388. $validator
  389. ->add('email', 'alpha', ['rule' => 'alphanumeric', 'last' => true])
  390. ->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
  391. $errors = $validator->errors(['email' => 'not an email!']);
  392. $expected = [
  393. 'email' => [
  394. 'alpha' => 'The provided value is invalid'
  395. ]
  396. ];
  397. $this->assertEquals($expected, $errors);
  398. }
  399. /**
  400. * Tests it is possible to get validation sets for a field using an array interface
  401. *
  402. * @return void
  403. */
  404. public function testArrayAccessGet() {
  405. $validator = new Validator;
  406. $validator
  407. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  408. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  409. $this->assertSame($validator['email'], $validator->field('email'));
  410. $this->assertSame($validator['title'], $validator->field('title'));
  411. }
  412. /**
  413. * Tests it is possible to check for validation sets for a field using an array inteface
  414. *
  415. * @return void
  416. */
  417. public function testArrayAccessExists() {
  418. $validator = new Validator;
  419. $validator
  420. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  421. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  422. $this->assertTrue(isset($validator['email']));
  423. $this->assertTrue(isset($validator['title']));
  424. $this->assertFalse(isset($validator['foo']));
  425. }
  426. /**
  427. * Tests it is possible to set validation rules for a field using an array inteface
  428. *
  429. * @return void
  430. */
  431. public function testArrayAccessSet() {
  432. $validator = new Validator;
  433. $validator
  434. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  435. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  436. $validator['name'] = $validator->field('title');
  437. $this->assertSame($validator->field('title'), $validator->field('name'));
  438. $validator['name'] = ['alpha' => ['rule' => 'alphanumeric']];
  439. $this->assertEquals($validator->field('email'), $validator->field('email'));
  440. }
  441. /**
  442. * Tests it is possible to unset validation rules
  443. *
  444. * @return void
  445. */
  446. public function testArrayAccessUset() {
  447. $validator = new Validator;
  448. $validator
  449. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  450. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  451. $this->assertTrue(isset($validator['title']));
  452. unset($validator['title']);
  453. $this->assertFalse(isset($validator['title']));
  454. }
  455. /**
  456. * Tests the countable interface
  457. *
  458. * @return void
  459. */
  460. public function testCount() {
  461. $validator = new Validator;
  462. $validator
  463. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  464. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  465. $this->assertCount(2, $validator);
  466. }
  467. /**
  468. * Tests adding rules via alternative syntax
  469. *
  470. * @return void
  471. */
  472. public function testAddMulitple() {
  473. $validator = new Validator;
  474. $validator->add('title', [
  475. 'notEmpty' => [
  476. 'rule' => 'notEmpty'
  477. ],
  478. 'length' => [
  479. 'rule' => ['minLength', 10],
  480. 'message' => 'Titles need to be at least 10 characters long'
  481. ]
  482. ]);
  483. $set = $validator->field('title');
  484. $this->assertInstanceOf('\Cake\Validation\ValidationSet', $set);
  485. $this->assertCount(2, $set);
  486. }
  487. }