PageRenderTime 57ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Test/Case/Model/ModelValidationTest.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 764 lines | 607 code | 81 blank | 76 comment | 1 complexity | 68d1a101dfdb893c618eb5ac2161130a MD5 | raw file
  1. <?php
  2. /**
  3. * ModelValidationTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Model
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
  20. /**
  21. * ModelValidationTest
  22. *
  23. * @package Cake.Test.Case.Model
  24. */
  25. class ModelValidationTest extends BaseModelTest {
  26. /**
  27. * Tests validation parameter order in custom validation methods
  28. *
  29. * @return void
  30. */
  31. public function testValidationParams() {
  32. $TestModel = new ValidationTest1();
  33. $TestModel->validate['title'] = array(
  34. 'rule' => 'customValidatorWithParams',
  35. 'required' => true
  36. );
  37. $TestModel->create(array('title' => 'foo'));
  38. $TestModel->invalidFields();
  39. $expected = array(
  40. 'data' => array(
  41. 'title' => 'foo'
  42. ),
  43. 'validator' => array(
  44. 'rule' => 'customValidatorWithParams',
  45. 'on' => null,
  46. 'last' => true,
  47. 'allowEmpty' => false,
  48. 'required' => true
  49. ),
  50. 'or' => true,
  51. 'ignore_on_same' => 'id'
  52. );
  53. $this->assertEquals($TestModel->validatorParams, $expected);
  54. $TestModel->validate['title'] = array(
  55. 'rule' => 'customValidatorWithMessage',
  56. 'required' => true
  57. );
  58. $expected = array(
  59. 'title' => array('This field will *never* validate! Muhahaha!')
  60. );
  61. $this->assertEquals($TestModel->invalidFields(), $expected);
  62. $TestModel->validate['title'] = array(
  63. 'rule' => array('customValidatorWithSixParams', 'one', 'two', null, 'four'),
  64. 'required' => true
  65. );
  66. $TestModel->create(array('title' => 'foo'));
  67. $TestModel->invalidFields();
  68. $expected = array(
  69. 'data' => array(
  70. 'title' => 'foo'
  71. ),
  72. 'one' => 'one',
  73. 'two' => 'two',
  74. 'three' => null,
  75. 'four' => 'four',
  76. 'five' => array(
  77. 'rule' => array(1 => 'one', 2 => 'two', 3 => null, 4 => 'four'),
  78. 'on' => null,
  79. 'last' => true,
  80. 'allowEmpty' => false,
  81. 'required' => true
  82. ),
  83. 'six' => 6
  84. );
  85. $this->assertEquals($TestModel->validatorParams, $expected);
  86. $TestModel->validate['title'] = array(
  87. 'rule' => array('customValidatorWithSixParams', 'one', array('two'), null, 'four', array('five' => 5)),
  88. 'required' => true
  89. );
  90. $TestModel->create(array('title' => 'foo'));
  91. $TestModel->invalidFields();
  92. $expected = array(
  93. 'data' => array(
  94. 'title' => 'foo'
  95. ),
  96. 'one' => 'one',
  97. 'two' => array('two'),
  98. 'three' => null,
  99. 'four' => 'four',
  100. 'five' => array('five' => 5),
  101. 'six' => array(
  102. 'rule' => array(1 => 'one', 2 => array('two'), 3 => null, 4 => 'four', 5 => array('five' => 5)),
  103. 'on' => null,
  104. 'last' => true,
  105. 'allowEmpty' => false,
  106. 'required' => true
  107. )
  108. );
  109. $this->assertEquals($TestModel->validatorParams, $expected);
  110. }
  111. /**
  112. * Tests validation parameter fieldList in invalidFields
  113. *
  114. * @return void
  115. */
  116. public function testInvalidFieldsWithFieldListParams() {
  117. $TestModel = new ValidationTest1();
  118. $TestModel->validate = $validate = array(
  119. 'title' => array(
  120. 'rule' => 'alphaNumeric',
  121. 'required' => true
  122. ),
  123. 'name' => array(
  124. 'rule' => 'alphaNumeric',
  125. 'required' => true
  126. ));
  127. $TestModel->set(array('title' => '$$', 'name' => '##'));
  128. $TestModel->invalidFields(array('fieldList' => array('title')));
  129. $expected = array(
  130. 'title' => array('This field cannot be left blank')
  131. );
  132. $this->assertEquals($TestModel->validationErrors, $expected);
  133. $TestModel->validationErrors = array();
  134. $TestModel->invalidFields(array('fieldList' => array('name')));
  135. $expected = array(
  136. 'name' => array('This field cannot be left blank')
  137. );
  138. $this->assertEquals($TestModel->validationErrors, $expected);
  139. $TestModel->validationErrors = array();
  140. $TestModel->invalidFields(array('fieldList' => array('name', 'title')));
  141. $expected = array(
  142. 'name' => array('This field cannot be left blank'),
  143. 'title' => array('This field cannot be left blank')
  144. );
  145. $this->assertEquals($TestModel->validationErrors, $expected);
  146. $TestModel->validationErrors = array();
  147. $TestModel->whitelist = array('name');
  148. $TestModel->invalidFields();
  149. $expected = array('name' => array('This field cannot be left blank'));
  150. $this->assertEquals($TestModel->validationErrors, $expected);
  151. $this->assertEquals($TestModel->validate, $validate);
  152. }
  153. /**
  154. * Test that invalidFields() integrates well with save(). And that fieldList can be an empty type.
  155. *
  156. * @return void
  157. */
  158. public function testInvalidFieldsWhitelist() {
  159. $TestModel = new ValidationTest1();
  160. $TestModel->validate = array(
  161. 'title' => array(
  162. 'rule' => 'alphaNumeric',
  163. 'required' => true
  164. ),
  165. 'name' => array(
  166. 'rule' => 'alphaNumeric',
  167. 'required' => true
  168. ));
  169. $TestModel->whitelist = array('name');
  170. $TestModel->save(array('name' => '#$$#', 'title' => '$$$$'));
  171. $expected = array('name' => array('This field cannot be left blank'));
  172. $this->assertEquals($TestModel->validationErrors, $expected);
  173. }
  174. /**
  175. * testValidates method
  176. *
  177. * @return void
  178. */
  179. public function testValidates() {
  180. $TestModel = new TestValidate();
  181. $TestModel->validate = array(
  182. 'user_id' => 'numeric',
  183. 'title' => array('allowEmpty' => false, 'rule' => 'notEmpty'),
  184. 'body' => 'notEmpty'
  185. );
  186. $data = array('TestValidate' => array(
  187. 'user_id' => '1',
  188. 'title' => '',
  189. 'body' => 'body'
  190. ));
  191. $result = $TestModel->create($data);
  192. $this->assertEquals($result, $data);
  193. $result = $TestModel->validates();
  194. $this->assertFalse($result);
  195. $data = array('TestValidate' => array(
  196. 'user_id' => '1',
  197. 'title' => 'title',
  198. 'body' => 'body'
  199. ));
  200. $result = $TestModel->create($data) && $TestModel->validates();
  201. $this->assertTrue($result);
  202. $data = array('TestValidate' => array(
  203. 'user_id' => '1',
  204. 'title' => '0',
  205. 'body' => 'body'
  206. ));
  207. $result = $TestModel->create($data);
  208. $this->assertEquals($result, $data);
  209. $result = $TestModel->validates();
  210. $this->assertTrue($result);
  211. $data = array('TestValidate' => array(
  212. 'user_id' => '1',
  213. 'title' => 0,
  214. 'body' => 'body'
  215. ));
  216. $result = $TestModel->create($data);
  217. $this->assertEquals($result, $data);
  218. $result = $TestModel->validates();
  219. $this->assertTrue($result);
  220. $TestModel->validate['modified'] = array('allowEmpty' => true, 'rule' => 'date');
  221. $data = array('TestValidate' => array(
  222. 'user_id' => '1',
  223. 'title' => 0,
  224. 'body' => 'body',
  225. 'modified' => ''
  226. ));
  227. $result = $TestModel->create($data);
  228. $this->assertEquals($result, $data);
  229. $result = $TestModel->validates();
  230. $this->assertTrue($result);
  231. $data = array('TestValidate' => array(
  232. 'user_id' => '1',
  233. 'title' => 0,
  234. 'body' => 'body',
  235. 'modified' => '2007-05-01'
  236. ));
  237. $result = $TestModel->create($data);
  238. $this->assertEquals($result, $data);
  239. $result = $TestModel->validates();
  240. $this->assertTrue($result);
  241. $data = array('TestValidate' => array(
  242. 'user_id' => '1',
  243. 'title' => 0,
  244. 'body' => 'body',
  245. 'modified' => 'invalid-date-here'
  246. ));
  247. $result = $TestModel->create($data);
  248. $this->assertEquals($result, $data);
  249. $result = $TestModel->validates();
  250. $this->assertFalse($result);
  251. $data = array('TestValidate' => array(
  252. 'user_id' => '1',
  253. 'title' => 0,
  254. 'body' => 'body',
  255. 'modified' => 0
  256. ));
  257. $result = $TestModel->create($data);
  258. $this->assertEquals($result, $data);
  259. $result = $TestModel->validates();
  260. $this->assertFalse($result);
  261. $data = array('TestValidate' => array(
  262. 'user_id' => '1',
  263. 'title' => 0,
  264. 'body' => 'body',
  265. 'modified' => '0'
  266. ));
  267. $result = $TestModel->create($data);
  268. $this->assertEquals($result, $data);
  269. $result = $TestModel->validates();
  270. $this->assertFalse($result);
  271. $TestModel->validate['modified'] = array('allowEmpty' => false, 'rule' => 'date');
  272. $data = array('TestValidate' => array('modified' => null));
  273. $result = $TestModel->create($data);
  274. $this->assertEquals($result, $data);
  275. $result = $TestModel->validates();
  276. $this->assertFalse($result);
  277. $data = array('TestValidate' => array('modified' => false));
  278. $result = $TestModel->create($data);
  279. $this->assertEquals($result, $data);
  280. $result = $TestModel->validates();
  281. $this->assertFalse($result);
  282. $data = array('TestValidate' => array('modified' => ''));
  283. $result = $TestModel->create($data);
  284. $this->assertEquals($result, $data);
  285. $result = $TestModel->validates();
  286. $this->assertFalse($result);
  287. $data = array('TestValidate' => array(
  288. 'modified' => '2007-05-01'
  289. ));
  290. $result = $TestModel->create($data);
  291. $this->assertEquals($result, $data);
  292. $result = $TestModel->validates();
  293. $this->assertTrue($result);
  294. $TestModel->validate['slug'] = array('allowEmpty' => false, 'rule' => array('maxLength', 45));
  295. $data = array('TestValidate' => array(
  296. 'user_id' => '1',
  297. 'title' => 0,
  298. 'body' => 'body',
  299. 'slug' => ''
  300. ));
  301. $result = $TestModel->create($data);
  302. $this->assertEquals($result, $data);
  303. $result = $TestModel->validates();
  304. $this->assertFalse($result);
  305. $data = array('TestValidate' => array(
  306. 'user_id' => '1',
  307. 'title' => 0,
  308. 'body' => 'body',
  309. 'slug' => 'slug-right-here'
  310. ));
  311. $result = $TestModel->create($data);
  312. $this->assertEquals($result, $data);
  313. $result = $TestModel->validates();
  314. $this->assertTrue($result);
  315. $data = array('TestValidate' => array(
  316. 'user_id' => '1',
  317. 'title' => 0,
  318. 'body' => 'body',
  319. 'slug' => 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'
  320. ));
  321. $result = $TestModel->create($data);
  322. $this->assertEquals($result, $data);
  323. $result = $TestModel->validates();
  324. $this->assertFalse($result);
  325. $TestModel->validate = array(
  326. 'number' => array(
  327. 'rule' => 'validateNumber',
  328. 'min' => 3,
  329. 'max' => 5
  330. ),
  331. 'title' => array(
  332. 'allowEmpty' => false,
  333. 'rule' => 'notEmpty'
  334. ));
  335. $data = array('TestValidate' => array(
  336. 'title' => 'title',
  337. 'number' => '0'
  338. ));
  339. $result = $TestModel->create($data);
  340. $this->assertEquals($result, $data);
  341. $result = $TestModel->validates();
  342. $this->assertFalse($result);
  343. $data = array('TestValidate' => array(
  344. 'title' => 'title',
  345. 'number' => 0
  346. ));
  347. $result = $TestModel->create($data);
  348. $this->assertEquals($result, $data);
  349. $result = $TestModel->validates();
  350. $this->assertFalse($result);
  351. $data = array('TestValidate' => array(
  352. 'title' => 'title',
  353. 'number' => '3'
  354. ));
  355. $result = $TestModel->create($data);
  356. $this->assertEquals($result, $data);
  357. $result = $TestModel->validates();
  358. $this->assertTrue($result);
  359. $data = array('TestValidate' => array(
  360. 'title' => 'title',
  361. 'number' => 3
  362. ));
  363. $result = $TestModel->create($data);
  364. $this->assertEquals($result, $data);
  365. $result = $TestModel->validates();
  366. $this->assertTrue($result);
  367. $TestModel->validate = array(
  368. 'number' => array(
  369. 'rule' => 'validateNumber',
  370. 'min' => 5,
  371. 'max' => 10
  372. ),
  373. 'title' => array(
  374. 'allowEmpty' => false,
  375. 'rule' => 'notEmpty'
  376. ));
  377. $data = array('TestValidate' => array(
  378. 'title' => 'title',
  379. 'number' => '3'
  380. ));
  381. $result = $TestModel->create($data);
  382. $this->assertEquals($result, $data);
  383. $result = $TestModel->validates();
  384. $this->assertFalse($result);
  385. $data = array('TestValidate' => array(
  386. 'title' => 'title',
  387. 'number' => 3
  388. ));
  389. $result = $TestModel->create($data);
  390. $this->assertEquals($result, $data);
  391. $result = $TestModel->validates();
  392. $this->assertFalse($result);
  393. $TestModel->validate = array(
  394. 'title' => array(
  395. 'allowEmpty' => false,
  396. 'rule' => 'validateTitle'
  397. ));
  398. $data = array('TestValidate' => array('title' => ''));
  399. $result = $TestModel->create($data);
  400. $this->assertEquals($result, $data);
  401. $result = $TestModel->validates();
  402. $this->assertFalse($result);
  403. $data = array('TestValidate' => array('title' => 'new title'));
  404. $result = $TestModel->create($data);
  405. $this->assertEquals($result, $data);
  406. $result = $TestModel->validates();
  407. $this->assertFalse($result);
  408. $data = array('TestValidate' => array('title' => 'title-new'));
  409. $result = $TestModel->create($data);
  410. $this->assertEquals($result, $data);
  411. $result = $TestModel->validates();
  412. $this->assertTrue($result);
  413. $TestModel->validate = array('title' => array(
  414. 'allowEmpty' => true,
  415. 'rule' => 'validateTitle'
  416. ));
  417. $data = array('TestValidate' => array('title' => ''));
  418. $result = $TestModel->create($data);
  419. $this->assertEquals($result, $data);
  420. $result = $TestModel->validates();
  421. $this->assertTrue($result);
  422. $TestModel->validate = array(
  423. 'title' => array(
  424. 'length' => array(
  425. 'allowEmpty' => true,
  426. 'rule' => array('maxLength', 10)
  427. )));
  428. $data = array('TestValidate' => array('title' => ''));
  429. $result = $TestModel->create($data);
  430. $this->assertEquals($result, $data);
  431. $result = $TestModel->validates();
  432. $this->assertTrue($result);
  433. $TestModel->validate = array(
  434. 'title' => array(
  435. 'rule' => array('userDefined', 'Article', 'titleDuplicate')
  436. ));
  437. $data = array('TestValidate' => array('title' => 'My Article Title'));
  438. $result = $TestModel->create($data);
  439. $this->assertEquals($result, $data);
  440. $result = $TestModel->validates();
  441. $this->assertFalse($result);
  442. $data = array('TestValidate' => array(
  443. 'title' => 'My Article With a Different Title'
  444. ));
  445. $result = $TestModel->create($data);
  446. $this->assertEquals($result, $data);
  447. $result = $TestModel->validates();
  448. $this->assertTrue($result);
  449. $TestModel->validate = array(
  450. 'title' => array(
  451. 'tooShort' => array('rule' => array('minLength', 50)),
  452. 'onlyLetters' => array('rule' => '/^[a-z]+$/i')
  453. ),
  454. );
  455. $data = array('TestValidate' => array(
  456. 'title' => 'I am a short string'
  457. ));
  458. $TestModel->create($data);
  459. $result = $TestModel->validates();
  460. $this->assertFalse($result);
  461. $result = $TestModel->validationErrors;
  462. $expected = array(
  463. 'title' => array('tooShort')
  464. );
  465. $this->assertEquals($expected, $result);
  466. $TestModel->validate = array(
  467. 'title' => array(
  468. 'tooShort' => array(
  469. 'rule' => array('minLength', 50),
  470. 'last' => false
  471. ),
  472. 'onlyLetters' => array('rule' => '/^[a-z]+$/i')
  473. ),
  474. );
  475. $data = array('TestValidate' => array(
  476. 'title' => 'I am a short string'
  477. ));
  478. $TestModel->create($data);
  479. $result = $TestModel->validates();
  480. $this->assertFalse($result);
  481. $result = $TestModel->validationErrors;
  482. $expected = array(
  483. 'title' => array('tooShort', 'onlyLetters')
  484. );
  485. $this->assertEquals($expected, $result);
  486. }
  487. /**
  488. * test that validates() checks all the 'with' associations as well for validation
  489. * as this can cause partial/wrong data insertion.
  490. *
  491. * @return void
  492. */
  493. public function testValidatesWithAssociations() {
  494. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  495. $data = array(
  496. 'Something' => array(
  497. 'id' => 5,
  498. 'title' => 'Extra Fields',
  499. 'body' => 'Extra Fields Body',
  500. 'published' => '1'
  501. ),
  502. 'SomethingElse' => array(
  503. array('something_else_id' => 1, 'doomed' => '')
  504. )
  505. );
  506. $Something = new Something();
  507. $JoinThing = $Something->JoinThing;
  508. $JoinThing->validate = array('doomed' => array('rule' => 'notEmpty'));
  509. $expectedError = array('doomed' => array('This field cannot be left blank'));
  510. $Something->create();
  511. $result = $Something->save($data);
  512. $this->assertFalse($result, 'Save occured even when with models failed. %s');
  513. $this->assertEquals($JoinThing->validationErrors, $expectedError);
  514. $count = $Something->find('count', array('conditions' => array('Something.id' => $data['Something']['id'])));
  515. $this->assertSame($count, 0);
  516. $data = array(
  517. 'Something' => array(
  518. 'id' => 5,
  519. 'title' => 'Extra Fields',
  520. 'body' => 'Extra Fields Body',
  521. 'published' => '1'
  522. ),
  523. 'SomethingElse' => array(
  524. array('something_else_id' => 1, 'doomed' => 1),
  525. array('something_else_id' => 1, 'doomed' => '')
  526. )
  527. );
  528. $Something->create();
  529. $result = $Something->save($data);
  530. $this->assertFalse($result, 'Save occured even when with models failed. %s');
  531. $joinRecords = $JoinThing->find('count', array(
  532. 'conditions' => array('JoinThing.something_id' => $data['Something']['id'])
  533. ));
  534. $this->assertEquals($joinRecords, 0, 'Records were saved on the join table. %s');
  535. }
  536. /**
  537. * test that saveAll and with models with validation interact well
  538. *
  539. * @return void
  540. */
  541. public function testValidatesWithModelsAndSaveAll() {
  542. $data = array(
  543. 'Something' => array(
  544. 'id' => 5,
  545. 'title' => 'Extra Fields',
  546. 'body' => 'Extra Fields Body',
  547. 'published' => '1'
  548. ),
  549. 'SomethingElse' => array(
  550. array('something_else_id' => 1, 'doomed' => '')
  551. )
  552. );
  553. $Something = new Something();
  554. $JoinThing = $Something->JoinThing;
  555. $JoinThing->validate = array('doomed' => array('rule' => 'notEmpty'));
  556. $expectedError = array('doomed' => array('This field cannot be left blank'));
  557. $Something->create();
  558. $result = $Something->saveAll($data, array('validate' => 'only'));
  559. $this->assertFalse($result);
  560. $this->assertEquals($JoinThing->validationErrors, $expectedError);
  561. $Something->create();
  562. $result = $Something->saveAll($data, array('validate' => 'first'));
  563. $this->assertFalse($result);
  564. $this->assertEquals($JoinThing->validationErrors, $expectedError);
  565. $count = $Something->find('count', array('conditions' => array('Something.id' => $data['Something']['id'])));
  566. $this->assertSame($count, 0);
  567. $joinRecords = $JoinThing->find('count', array(
  568. 'conditions' => array('JoinThing.something_id' => $data['Something']['id'])
  569. ));
  570. $this->assertEquals($joinRecords, 0, 'Records were saved on the join table. %s');
  571. }
  572. /**
  573. * test that saveAll and with models at initial insert (no id has set yet)
  574. * with validation interact well
  575. *
  576. * @return void
  577. */
  578. public function testValidatesWithModelsAndSaveAllWithoutId() {
  579. $this->loadFixtures('Post', 'Author');
  580. $data = array(
  581. 'Author' => array(
  582. 'name' => 'Foo Bar',
  583. ),
  584. 'Post' => array(
  585. array('title' => 'Hello'),
  586. array('title' => 'World'),
  587. )
  588. );
  589. $Author = new Author();
  590. $Post = $Author->Post;
  591. $Post->validate = array('author_id' => array('rule' => 'numeric'));
  592. $Author->create();
  593. $result = $Author->saveAll($data, array('validate' => 'only'));
  594. $this->assertTrue($result);
  595. $Author->create();
  596. $result = $Author->saveAll($data, array('validate' => 'first'));
  597. $this->assertTrue($result);
  598. $this->assertFalse(is_null($Author->id));
  599. $id = $Author->id;
  600. $count = $Author->find('count', array('conditions' => array('Author.id' => $id)));
  601. $this->assertSame($count, 1);
  602. $count = $Post->find('count', array(
  603. 'conditions' => array('Post.author_id' => $id)
  604. ));
  605. $this->assertEquals($count, count($data['Post']));
  606. }
  607. /**
  608. * Test that missing validation methods trigger errors in development mode.
  609. * Helps to make developement easier.
  610. *
  611. * @expectedException PHPUnit_Framework_Error
  612. * @return void
  613. */
  614. public function testMissingValidationErrorTriggering() {
  615. Configure::write('debug', 2);
  616. $TestModel = new ValidationTest1();
  617. $TestModel->create(array('title' => 'foo'));
  618. $TestModel->validate = array(
  619. 'title' => array(
  620. 'rule' => array('thisOneBringsThePain'),
  621. 'required' => true
  622. )
  623. );
  624. $TestModel->invalidFields(array('fieldList' => array('title')));
  625. }
  626. /**
  627. * Test that missing validation methods does not trigger errors in production mode.
  628. *
  629. * @return void
  630. */
  631. public function testMissingValidationErrorNoTriggering() {
  632. Configure::write('debug', 0);
  633. $TestModel = new ValidationTest1();
  634. $TestModel->create(array('title' => 'foo'));
  635. $TestModel->validate = array(
  636. 'title' => array(
  637. 'rule' => array('thisOneBringsThePain'),
  638. 'required' => true
  639. )
  640. );
  641. $TestModel->invalidFields(array('fieldList' => array('title')));
  642. $this->assertEquals($TestModel->validationErrors, array());
  643. }
  644. /**
  645. * Test placeholder replacement when validation message is an array
  646. *
  647. * @return void
  648. */
  649. public function testValidationMessageAsArray() {
  650. $TestModel = new ValidationTest1();
  651. $TestModel->validate = array(
  652. 'title' => array(
  653. 'minLength' => array(
  654. 'rule' => array('minLength', 6),
  655. 'required' => true,
  656. 'message' => 'Minimum length allowed is %d chars',
  657. 'last' => false
  658. ),
  659. 'between' => array(
  660. 'rule' => array('between', 5, 15),
  661. 'message' => array('You may enter up to %s chars (minimum is %s chars)', 14, 6)
  662. )
  663. )
  664. );
  665. $TestModel->create();
  666. $TestModel->invalidFields();
  667. $expected = array(
  668. 'title' => array(
  669. 'Minimum length allowed is 6 chars',
  670. )
  671. );
  672. $this->assertEquals($TestModel->validationErrors, $expected);
  673. $TestModel->create(array('title' => 'foo'));
  674. $TestModel->invalidFields();
  675. $expected = array(
  676. 'title' => array(
  677. 'Minimum length allowed is 6 chars',
  678. 'You may enter up to 14 chars (minimum is 6 chars)'
  679. )
  680. );
  681. $this->assertEquals($TestModel->validationErrors, $expected);
  682. }
  683. }