ValidatorTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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\TestSuite\TestCase;
  17. use Cake\Validation\ValidationSet;
  18. use Cake\Validation\Validator;
  19. /**
  20. * Tests Validator class
  21. *
  22. */
  23. class ValidatorTest extends TestCase
  24. {
  25. /**
  26. * Testing you can dynamically add rules to a field
  27. *
  28. * @return void
  29. */
  30. public function testAddingRulesToField()
  31. {
  32. $validator = new Validator;
  33. $validator->add('title', 'not-empty', ['rule' => 'notEmpty']);
  34. $set = $validator->field('title');
  35. $this->assertInstanceOf('Cake\Validation\ValidationSet', $set);
  36. $this->assertCount(1, $set);
  37. $validator->add('title', 'another', ['rule' => 'alphanumeric']);
  38. $this->assertCount(2, $set);
  39. $validator->add('body', 'another', ['rule' => 'crazy']);
  40. $this->assertCount(1, $validator->field('body'));
  41. $this->assertCount(2, $validator);
  42. }
  43. /**
  44. * Tests that calling field will create a default validation set for it
  45. *
  46. * @return void
  47. */
  48. public function testFieldDefault()
  49. {
  50. $validator = new Validator;
  51. $this->assertFalse($validator->hasField('foo'));
  52. $field = $validator->field('foo');
  53. $this->assertInstanceOf('Cake\Validation\ValidationSet', $field);
  54. $this->assertCount(0, $field);
  55. $this->assertTrue($validator->hasField('foo'));
  56. }
  57. /**
  58. * Tests that field method can be used as a setter
  59. *
  60. * @return void
  61. */
  62. public function testFieldSetter()
  63. {
  64. $validator = new Validator;
  65. $validationSet = new ValidationSet;
  66. $validator->field('thing', $validationSet);
  67. $this->assertSame($validationSet, $validator->field('thing'));
  68. }
  69. /**
  70. * Tests the remove method
  71. *
  72. * @return void
  73. */
  74. public function testRemove()
  75. {
  76. $validator = new Validator;
  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');
  81. $this->assertCount(0, $validator->field('title'));
  82. $validator->remove('title');
  83. $validator->add('title', 'not-empty', ['rule' => 'notEmpty']);
  84. $validator->add('title', 'foo', ['rule' => 'bar']);
  85. $this->assertCount(2, $validator->field('title'));
  86. $validator->remove('title', 'foo');
  87. $this->assertCount(1, $validator->field('title'));
  88. $this->assertNull($validator->field('title')->rule('foo'));
  89. }
  90. /**
  91. * Tests the requirePresence method
  92. *
  93. * @return void
  94. */
  95. public function testRequirePresence()
  96. {
  97. $validator = new Validator;
  98. $this->assertSame($validator, $validator->requirePresence('title'));
  99. $this->assertTrue($validator->field('title')->isPresenceRequired());
  100. $validator->requirePresence('title', false);
  101. $this->assertFalse($validator->field('title')->isPresenceRequired());
  102. $validator->requirePresence('title', 'create');
  103. $this->assertEquals('create', $validator->field('title')->isPresenceRequired());
  104. $validator->requirePresence('title', 'update');
  105. $this->assertEquals('update', $validator->field('title')->isPresenceRequired());
  106. }
  107. /**
  108. * Tests the isPresenceRequired method
  109. *
  110. * @return void
  111. */
  112. public function testIsPresenceRequired()
  113. {
  114. $validator = new Validator;
  115. $this->assertSame($validator, $validator->requirePresence('title'));
  116. $this->assertTrue($validator->isPresenceRequired('title', true));
  117. $this->assertTrue($validator->isPresenceRequired('title', false));
  118. $validator->requirePresence('title', false);
  119. $this->assertFalse($validator->isPresenceRequired('title', true));
  120. $this->assertFalse($validator->isPresenceRequired('title', false));
  121. $validator->requirePresence('title', 'create');
  122. $this->assertTrue($validator->isPresenceRequired('title', true));
  123. $this->assertFalse($validator->isPresenceRequired('title', false));
  124. $validator->requirePresence('title', 'update');
  125. $this->assertTrue($validator->isPresenceRequired('title', false));
  126. $this->assertFalse($validator->isPresenceRequired('title', true));
  127. }
  128. /**
  129. * Tests errors generated when a field presence is required
  130. *
  131. * @return void
  132. */
  133. public function testErrorsWithPresenceRequired()
  134. {
  135. $validator = new Validator;
  136. $validator->requirePresence('title');
  137. $errors = $validator->errors(['foo' => 'something']);
  138. $expected = ['title' => ['_required' => 'This field is required']];
  139. $this->assertEquals($expected, $errors);
  140. $this->assertEmpty($validator->errors(['title' => 'bar']));
  141. $validator->requirePresence('title', false);
  142. $this->assertEmpty($validator->errors(['foo' => 'bar']));
  143. }
  144. /**
  145. * Tests custom error messages generated when a field presence is required
  146. *
  147. * @return void
  148. */
  149. public function testCustomErrorsWithPresenceRequired()
  150. {
  151. $validator = new Validator;
  152. $validator->requirePresence('title', true, 'Custom message');
  153. $errors = $validator->errors(['foo' => 'something']);
  154. $expected = ['title' => ['_required' => 'Custom message']];
  155. $this->assertEquals($expected, $errors);
  156. }
  157. /**
  158. * Tests the allowEmpty method
  159. *
  160. * @return void
  161. */
  162. public function testAllowEmpty()
  163. {
  164. $validator = new Validator;
  165. $this->assertSame($validator, $validator->allowEmpty('title'));
  166. $this->assertTrue($validator->field('title')->isEmptyAllowed());
  167. $validator->allowEmpty('title', 'create');
  168. $this->assertEquals('create', $validator->field('title')->isEmptyAllowed());
  169. $validator->allowEmpty('title', 'update');
  170. $this->assertEquals('update', $validator->field('title')->isEmptyAllowed());
  171. }
  172. /**
  173. * Tests the allowEmpty method with date/time fields.
  174. *
  175. * @return void
  176. */
  177. public function testAllowEmptyDateTime()
  178. {
  179. $validator = new Validator;
  180. $validator->allowEmpty('created')
  181. ->add('created', 'date', ['rule' => 'date']);
  182. $data = [
  183. 'created' => [
  184. 'year' => '',
  185. 'month' => '',
  186. 'day' => ''
  187. ]
  188. ];
  189. $result = $validator->errors($data);
  190. $this->assertEmpty($result, 'No errors on empty date');
  191. $data = [
  192. 'created' => [
  193. 'year' => '',
  194. 'month' => '',
  195. 'day' => '',
  196. 'hour' => '',
  197. 'minute' => '',
  198. 'second' => '',
  199. 'meridian' => '',
  200. ]
  201. ];
  202. $result = $validator->errors($data);
  203. $this->assertEmpty($result, 'No errors on empty datetime');
  204. $data = [
  205. 'created' => [
  206. 'hour' => '',
  207. 'minute' => '',
  208. 'meridian' => '',
  209. ]
  210. ];
  211. $result = $validator->errors($data);
  212. $this->assertEmpty($result, 'No errors on empty time');
  213. }
  214. /**
  215. * Tests the allowEmpty method with file fields.
  216. *
  217. * @return void
  218. */
  219. public function testAllowEmptyFileFields()
  220. {
  221. $validator = new Validator;
  222. $validator->allowEmpty('picture')
  223. ->add('picture', 'file', ['rule' => 'uploadedFile']);
  224. $data = [
  225. 'picture' => [
  226. 'name' => '',
  227. 'type' => '',
  228. 'tmp_name' => '',
  229. 'error' => UPLOAD_ERR_NO_FILE,
  230. ]
  231. ];
  232. $result = $validator->errors($data);
  233. $this->assertEmpty($result, 'No errors on empty date');
  234. $data = [
  235. 'picture' => [
  236. 'name' => 'fake.png',
  237. 'type' => '',
  238. 'tmp_name' => '',
  239. 'error' => UPLOAD_ERR_OK,
  240. ]
  241. ];
  242. $result = $validator->errors($data);
  243. $this->assertNotEmpty($result, 'Invalid file should be caught still.');
  244. }
  245. /**
  246. * Test the notEmpty() method.
  247. *
  248. * @return void
  249. */
  250. public function testNotEmpty()
  251. {
  252. $validator = new Validator;
  253. $validator->notEmpty('title');
  254. $this->assertFalse($validator->field('title')->isEmptyAllowed());
  255. $validator->allowEmpty('title');
  256. $this->assertTrue($validator->field('title')->isEmptyAllowed());
  257. }
  258. /**
  259. * Test the notEmpty() method.
  260. *
  261. * @return void
  262. */
  263. public function testNotEmptyModes()
  264. {
  265. $validator = new Validator;
  266. $validator->notEmpty('title', 'Need a title', 'create');
  267. $this->assertFalse($validator->isEmptyAllowed('title', true));
  268. $this->assertTrue($validator->isEmptyAllowed('title', false));
  269. $validator->notEmpty('title', 'Need a title', 'update');
  270. $this->assertTrue($validator->isEmptyAllowed('title', true));
  271. $this->assertFalse($validator->isEmptyAllowed('title', false));
  272. $validator->notEmpty('title', 'Need a title');
  273. $this->assertFalse($validator->isEmptyAllowed('title', true));
  274. $this->assertFalse($validator->isEmptyAllowed('title', false));
  275. $validator->notEmpty('title');
  276. $this->assertFalse($validator->isEmptyAllowed('title', true));
  277. $this->assertFalse($validator->isEmptyAllowed('title', false));
  278. }
  279. /**
  280. * Test interactions between notEmpty() and isAllowed().
  281. *
  282. * @return void
  283. */
  284. public function testNotEmptyAndIsAllowed()
  285. {
  286. $validator = new Validator;
  287. $validator->allowEmpty('title')
  288. ->notEmpty('title', 'Need it', 'update');
  289. $this->assertTrue($validator->isEmptyAllowed('title', true));
  290. $this->assertFalse($validator->isEmptyAllowed('title', false));
  291. $validator->allowEmpty('title')
  292. ->notEmpty('title');
  293. $this->assertFalse($validator->isEmptyAllowed('title', true));
  294. $this->assertFalse($validator->isEmptyAllowed('title', false));
  295. $validator->notEmpty('title')
  296. ->allowEmpty('title', 'create');
  297. $this->assertTrue($validator->isEmptyAllowed('title', true));
  298. $this->assertFalse($validator->isEmptyAllowed('title', false));
  299. }
  300. /**
  301. * Tests the allowEmpty method when passing a callback
  302. *
  303. * @return void
  304. */
  305. public function testAllowEmptyCallback()
  306. {
  307. $validator = new Validator;
  308. $allow = true;
  309. $validator->allowEmpty('title', function ($context) use (&$allow) {
  310. $this->assertEquals([], $context['data']);
  311. $this->assertEquals([], $context['providers']);
  312. $this->assertTrue($context['newRecord']);
  313. return $allow;
  314. });
  315. $this->assertTrue($validator->isEmptyAllowed('title', true));
  316. $allow = false;
  317. $this->assertFalse($validator->isEmptyAllowed('title', true));
  318. }
  319. /**
  320. * Tests the notEmpty method when passing a callback
  321. *
  322. * @return void
  323. */
  324. public function testNotEmptyCallback()
  325. {
  326. $validator = new Validator;
  327. $prevent = true;
  328. $validator->notEmpty('title', 'error message', function ($context) use (&$prevent) {
  329. $this->assertEquals([], $context['data']);
  330. $this->assertEquals([], $context['providers']);
  331. $this->assertFalse($context['newRecord']);
  332. return $prevent;
  333. });
  334. $this->assertFalse($validator->isEmptyAllowed('title', false));
  335. $prevent = false;
  336. $this->assertTrue($validator->isEmptyAllowed('title', false));
  337. }
  338. /**
  339. * Tests the isEmptyAllowed method
  340. *
  341. * @return void
  342. */
  343. public function testIsEmptyAllowed()
  344. {
  345. $validator = new Validator;
  346. $this->assertSame($validator, $validator->allowEmpty('title'));
  347. $this->assertTrue($validator->isEmptyAllowed('title', true));
  348. $this->assertTrue($validator->isEmptyAllowed('title', false));
  349. $validator->notEmpty('title');
  350. $this->assertFalse($validator->isEmptyAllowed('title', true));
  351. $this->assertFalse($validator->isEmptyAllowed('title', false));
  352. $validator->allowEmpty('title', 'create');
  353. $this->assertTrue($validator->isEmptyAllowed('title', true));
  354. $this->assertFalse($validator->isEmptyAllowed('title', false));
  355. $validator->allowEmpty('title', 'update');
  356. $this->assertTrue($validator->isEmptyAllowed('title', false));
  357. $this->assertFalse($validator->isEmptyAllowed('title', true));
  358. }
  359. /**
  360. * Tests errors generated when a field is not allowed to be empty
  361. *
  362. * @return void
  363. */
  364. public function testErrorsWithEmptyNotAllowed()
  365. {
  366. $validator = new Validator;
  367. $validator->notEmpty('title');
  368. $errors = $validator->errors(['title' => '']);
  369. $expected = ['title' => ['_empty' => 'This field cannot be left empty']];
  370. $this->assertEquals($expected, $errors);
  371. $errors = $validator->errors(['title' => []]);
  372. $expected = ['title' => ['_empty' => 'This field cannot be left empty']];
  373. $this->assertEquals($expected, $errors);
  374. $errors = $validator->errors(['title' => null]);
  375. $expected = ['title' => ['_empty' => 'This field cannot be left empty']];
  376. $this->assertEquals($expected, $errors);
  377. $errors = $validator->errors(['title' => 0]);
  378. $this->assertEmpty($errors);
  379. $errors = $validator->errors(['title' => '0']);
  380. $this->assertEmpty($errors);
  381. $errors = $validator->errors(['title' => false]);
  382. $this->assertEmpty($errors);
  383. }
  384. /**
  385. * Tests custom error mesages generated when a field is not allowed to be empty
  386. *
  387. * @return void
  388. */
  389. public function testCustomErrorsWithEmptyNotAllowed()
  390. {
  391. $validator = new Validator;
  392. $validator->notEmpty('title', 'Custom message');
  393. $errors = $validator->errors(['title' => '']);
  394. $expected = ['title' => ['_empty' => 'Custom message']];
  395. $this->assertEquals($expected, $errors);
  396. }
  397. /**
  398. * Tests errors generated when a field is allowed to be empty
  399. *
  400. * @return void
  401. */
  402. public function testErrorsWithEmptyAllowed()
  403. {
  404. $validator = new Validator;
  405. $validator->allowEmpty('title');
  406. $errors = $validator->errors(['title' => '']);
  407. $this->assertEmpty($errors);
  408. $errors = $validator->errors(['title' => []]);
  409. $this->assertEmpty($errors);
  410. $errors = $validator->errors(['title' => null]);
  411. $this->assertEmpty($errors);
  412. $errors = $validator->errors(['title' => 0]);
  413. $this->assertEmpty($errors);
  414. $errors = $validator->errors(['title' => 0.0]);
  415. $this->assertEmpty($errors);
  416. $errors = $validator->errors(['title' => '0']);
  417. $this->assertEmpty($errors);
  418. $errors = $validator->errors(['title' => false]);
  419. $this->assertEmpty($errors);
  420. }
  421. /**
  422. * Test the provider() method
  423. *
  424. * @return void
  425. */
  426. public function testProvider()
  427. {
  428. $validator = new Validator;
  429. $object = new \stdClass;
  430. $this->assertSame($validator, $validator->provider('foo', $object));
  431. $this->assertSame($object, $validator->provider('foo'));
  432. $this->assertNull($validator->provider('bar'));
  433. $another = new \stdClass;
  434. $this->assertSame($validator, $validator->provider('bar', $another));
  435. $this->assertSame($another, $validator->provider('bar'));
  436. $this->assertEquals(new \Cake\Validation\RulesProvider, $validator->provider('default'));
  437. }
  438. /**
  439. * Tests errors() method when using validators from the default provider, this proves
  440. * that it returns a default validation message and the custom one set in the rule
  441. *
  442. * @return void
  443. */
  444. public function testErrorsFromDefaultProvider()
  445. {
  446. $validator = new Validator;
  447. $validator
  448. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  449. ->add('email', 'notEmpty', ['rule' => 'notEmpty'])
  450. ->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
  451. $errors = $validator->errors(['email' => 'not an email!']);
  452. $expected = [
  453. 'email' => [
  454. 'alpha' => 'The provided value is invalid',
  455. 'email' => 'Y u no write email?'
  456. ]
  457. ];
  458. $this->assertEquals($expected, $errors);
  459. }
  460. /**
  461. * Tests using validation methods from different providers and returning the error
  462. * as a string
  463. *
  464. * @return void
  465. */
  466. public function testErrorsFromCustomProvider()
  467. {
  468. $validator = new Validator;
  469. $validator
  470. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  471. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  472. $thing = $this->getMock('\stdClass', ['isCool']);
  473. $thing->expects($this->once())->method('isCool')
  474. ->will($this->returnCallback(function ($data, $context) use ($thing) {
  475. $this->assertEquals('bar', $data);
  476. $expected = [
  477. 'default' => new \Cake\Validation\RulesProvider,
  478. 'thing' => $thing
  479. ];
  480. $expected = [
  481. 'newRecord' => true,
  482. 'providers' => $expected,
  483. 'data' => [
  484. 'email' => '!',
  485. 'title' => 'bar'
  486. ],
  487. 'field' => 'title'
  488. ];
  489. $this->assertEquals($expected, $context);
  490. return "That ain't cool, yo";
  491. }));
  492. $validator->provider('thing', $thing);
  493. $errors = $validator->errors(['email' => '!', 'title' => 'bar']);
  494. $expected = [
  495. 'email' => ['alpha' => 'The provided value is invalid'],
  496. 'title' => ['cool' => "That ain't cool, yo"]
  497. ];
  498. $this->assertEquals($expected, $errors);
  499. }
  500. /**
  501. * Tests that it is possible to pass extra arguments to the validation function
  502. * and it still gets the providers as last argument
  503. *
  504. * @return void
  505. */
  506. public function testMethodsWithExtraArguments()
  507. {
  508. $validator = new Validator;
  509. $validator->add('title', 'cool', [
  510. 'rule' => ['isCool', 'and', 'awesome'],
  511. 'provider' => 'thing'
  512. ]);
  513. $thing = $this->getMock('\stdClass', ['isCool']);
  514. $thing->expects($this->once())->method('isCool')
  515. ->will($this->returnCallback(function ($data, $a, $b, $context) use ($thing) {
  516. $this->assertEquals('bar', $data);
  517. $this->assertEquals('and', $a);
  518. $this->assertEquals('awesome', $b);
  519. $expected = [
  520. 'default' => new \Cake\Validation\RulesProvider,
  521. 'thing' => $thing
  522. ];
  523. $expected = [
  524. 'newRecord' => true,
  525. 'providers' => $expected,
  526. 'data' => [
  527. 'email' => '!',
  528. 'title' => 'bar'
  529. ],
  530. 'field' => 'title'
  531. ];
  532. $this->assertEquals($expected, $context);
  533. return "That ain't cool, yo";
  534. }));
  535. $validator->provider('thing', $thing);
  536. $errors = $validator->errors(['email' => '!', 'title' => 'bar']);
  537. $expected = [
  538. 'title' => ['cool' => "That ain't cool, yo"]
  539. ];
  540. $this->assertEquals($expected, $errors);
  541. }
  542. /**
  543. * Tests that it is possible to use a closure as a rule
  544. *
  545. * @return void
  546. */
  547. public function testUsingClosureAsRule()
  548. {
  549. $validator = new Validator;
  550. $validator->add('name', 'myRule', [
  551. 'rule' => function ($data, $provider) {
  552. $this->assertEquals('foo', $data);
  553. return 'You fail';
  554. }
  555. ]);
  556. $expected = ['name' => ['myRule' => 'You fail']];
  557. $this->assertEquals($expected, $validator->errors(['name' => 'foo']));
  558. }
  559. /**
  560. * Tests that setting last to a rule will stop validating the rest of the rules
  561. *
  562. * @return void
  563. */
  564. public function testErrorsWithLastRule()
  565. {
  566. $validator = new Validator;
  567. $validator
  568. ->add('email', 'alpha', ['rule' => 'alphanumeric', 'last' => true])
  569. ->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
  570. $errors = $validator->errors(['email' => 'not an email!']);
  571. $expected = [
  572. 'email' => [
  573. 'alpha' => 'The provided value is invalid'
  574. ]
  575. ];
  576. $this->assertEquals($expected, $errors);
  577. }
  578. /**
  579. * Tests it is possible to get validation sets for a field using an array interface
  580. *
  581. * @return void
  582. */
  583. public function testArrayAccessGet()
  584. {
  585. $validator = new Validator;
  586. $validator
  587. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  588. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  589. $this->assertSame($validator['email'], $validator->field('email'));
  590. $this->assertSame($validator['title'], $validator->field('title'));
  591. }
  592. /**
  593. * Tests it is possible to check for validation sets for a field using an array inteface
  594. *
  595. * @return void
  596. */
  597. public function testArrayAccessExists()
  598. {
  599. $validator = new Validator;
  600. $validator
  601. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  602. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  603. $this->assertTrue(isset($validator['email']));
  604. $this->assertTrue(isset($validator['title']));
  605. $this->assertFalse(isset($validator['foo']));
  606. }
  607. /**
  608. * Tests it is possible to set validation rules for a field using an array inteface
  609. *
  610. * @return void
  611. */
  612. public function testArrayAccessSet()
  613. {
  614. $validator = new Validator;
  615. $validator
  616. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  617. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  618. $validator['name'] = $validator->field('title');
  619. $this->assertSame($validator->field('title'), $validator->field('name'));
  620. $validator['name'] = ['alpha' => ['rule' => 'alphanumeric']];
  621. $this->assertEquals($validator->field('email'), $validator->field('email'));
  622. }
  623. /**
  624. * Tests it is possible to unset validation rules
  625. *
  626. * @return void
  627. */
  628. public function testArrayAccessUset()
  629. {
  630. $validator = new Validator;
  631. $validator
  632. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  633. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  634. $this->assertTrue(isset($validator['title']));
  635. unset($validator['title']);
  636. $this->assertFalse(isset($validator['title']));
  637. }
  638. /**
  639. * Tests the countable interface
  640. *
  641. * @return void
  642. */
  643. public function testCount()
  644. {
  645. $validator = new Validator;
  646. $validator
  647. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  648. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  649. $this->assertCount(2, $validator);
  650. }
  651. /**
  652. * Tests adding rules via alternative syntax
  653. *
  654. * @return void
  655. */
  656. public function testAddMulitple()
  657. {
  658. $validator = new Validator;
  659. $validator->add('title', [
  660. 'notEmpty' => [
  661. 'rule' => 'notEmpty'
  662. ],
  663. 'length' => [
  664. 'rule' => ['minLength', 10],
  665. 'message' => 'Titles need to be at least 10 characters long'
  666. ]
  667. ]);
  668. $set = $validator->field('title');
  669. $this->assertInstanceOf('Cake\Validation\ValidationSet', $set);
  670. $this->assertCount(2, $set);
  671. }
  672. /**
  673. * Integration test for compareWith validator.
  674. *
  675. * @return void
  676. */
  677. public function testCompareWithIntegration()
  678. {
  679. $validator = new Validator;
  680. $validator->add('password', [
  681. 'compare' => [
  682. 'rule' => ['compareWith', 'password_compare']
  683. ],
  684. ]);
  685. $data = [
  686. 'password' => 'test',
  687. 'password_compare' => 'not the same'
  688. ];
  689. $this->assertNotEmpty($validator->errors($data), 'Validation should fail.');
  690. }
  691. }