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

https://github.com/kunit/cakephp · PHP · 1701 lines · 1393 code · 177 blank · 131 comment · 1 complexity · e466a2727ee0a7dd7068c5651c22d0c9 MD5 · raw file

  1. <?php
  2. /**
  3. * ModelValidationTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  16. * @package Cake.Test.Case.Model
  17. * @since CakePHP(tm) v 1.2.0.4206
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
  21. /**
  22. * ModelValidationTest
  23. *
  24. * @package Cake.Test.Case.Model
  25. */
  26. class ModelValidationTest extends BaseModelTest {
  27. /**
  28. * override locale to the default (eng).
  29. *
  30. * @return void
  31. */
  32. public function setUp() {
  33. parent::setUp();
  34. Configure::write('Config.language', 'eng');
  35. }
  36. /**
  37. * Tests validation parameter order in custom validation methods
  38. *
  39. * @return void
  40. */
  41. public function testValidationParams() {
  42. $TestModel = new ValidationTest1();
  43. $TestModel->validate['title'] = array(
  44. 'rule' => 'customValidatorWithParams',
  45. 'required' => true
  46. );
  47. $TestModel->create(array('title' => 'foo'));
  48. $TestModel->invalidFields();
  49. $expected = array(
  50. 'data' => array(
  51. 'title' => 'foo'
  52. ),
  53. 'validator' => array(
  54. 'rule' => 'customValidatorWithParams',
  55. 'on' => null,
  56. 'last' => true,
  57. 'allowEmpty' => false,
  58. 'required' => true,
  59. 'message' => null
  60. ),
  61. 'or' => true,
  62. 'ignoreOnSame' => 'id'
  63. );
  64. $this->assertEquals($expected, $TestModel->validatorParams);
  65. $TestModel->validate['title'] = array(
  66. 'rule' => 'customValidatorWithMessage',
  67. 'required' => true
  68. );
  69. $expected = array(
  70. 'title' => array('This field will *never* validate! Muhahaha!')
  71. );
  72. $this->assertEquals($expected, $TestModel->invalidFields());
  73. $TestModel->validate['title'] = array(
  74. 'rule' => array('customValidatorWithSixParams', 'one', 'two', null, 'four'),
  75. 'required' => true
  76. );
  77. $TestModel->create(array('title' => 'foo'));
  78. $TestModel->invalidFields();
  79. $expected = array(
  80. 'data' => array(
  81. 'title' => 'foo'
  82. ),
  83. 'one' => 'one',
  84. 'two' => 'two',
  85. 'three' => null,
  86. 'four' => 'four',
  87. 'five' => array(
  88. 'rule' => array(1 => 'one', 2 => 'two', 3 => null, 4 => 'four'),
  89. 'on' => null,
  90. 'last' => true,
  91. 'allowEmpty' => false,
  92. 'required' => true,
  93. 'message' => null
  94. ),
  95. 'six' => 6
  96. );
  97. $this->assertEquals($expected, $TestModel->validatorParams);
  98. $TestModel->validate['title'] = array(
  99. 'rule' => array('customValidatorWithSixParams', 'one', array('two'), null, 'four', array('five' => 5)),
  100. 'required' => true
  101. );
  102. $TestModel->create(array('title' => 'foo'));
  103. $TestModel->invalidFields();
  104. $expected = array(
  105. 'data' => array(
  106. 'title' => 'foo'
  107. ),
  108. 'one' => 'one',
  109. 'two' => array('two'),
  110. 'three' => null,
  111. 'four' => 'four',
  112. 'five' => array('five' => 5),
  113. 'six' => array(
  114. 'rule' => array(1 => 'one', 2 => array('two'), 3 => null, 4 => 'four', 5 => array('five' => 5)),
  115. 'on' => null,
  116. 'last' => true,
  117. 'allowEmpty' => false,
  118. 'required' => true,
  119. 'message' => null
  120. )
  121. );
  122. $this->assertEquals($expected, $TestModel->validatorParams);
  123. }
  124. /**
  125. * Tests validation parameter fieldList in invalidFields
  126. *
  127. * @return void
  128. */
  129. public function testInvalidFieldsWithFieldListParams() {
  130. $TestModel = new ValidationTest1();
  131. $TestModel->validate = $validate = array(
  132. 'title' => array(
  133. 'rule' => 'alphaNumeric',
  134. 'required' => true
  135. ),
  136. 'name' => array(
  137. 'rule' => 'alphaNumeric',
  138. 'required' => true
  139. ));
  140. $TestModel->set(array('title' => '$$', 'name' => '##'));
  141. $TestModel->invalidFields(array('fieldList' => array('title')));
  142. $expected = array(
  143. 'title' => array('This field cannot be left blank')
  144. );
  145. $this->assertEquals($expected, $TestModel->validationErrors);
  146. $TestModel->validationErrors = array();
  147. $TestModel->invalidFields(array('fieldList' => array('name')));
  148. $expected = array(
  149. 'name' => array('This field cannot be left blank')
  150. );
  151. $this->assertEquals($expected, $TestModel->validationErrors);
  152. $TestModel->validationErrors = array();
  153. $TestModel->invalidFields(array('fieldList' => array('name', 'title')));
  154. $expected = array(
  155. 'name' => array('This field cannot be left blank'),
  156. 'title' => array('This field cannot be left blank')
  157. );
  158. $this->assertEquals($expected, $TestModel->validationErrors);
  159. $TestModel->validationErrors = array();
  160. $TestModel->whitelist = array('name');
  161. $TestModel->invalidFields();
  162. $expected = array('name' => array('This field cannot be left blank'));
  163. $this->assertEquals($expected, $TestModel->validationErrors);
  164. $this->assertEquals($TestModel->validate, $validate);
  165. }
  166. /**
  167. * Test that invalidFields() integrates well with save(). And that fieldList can be an empty type.
  168. *
  169. * @return void
  170. */
  171. public function testInvalidFieldsWhitelist() {
  172. $TestModel = new ValidationTest1();
  173. $TestModel->validate = array(
  174. 'title' => array(
  175. 'rule' => 'alphaNumeric',
  176. 'required' => true
  177. ),
  178. 'name' => array(
  179. 'rule' => 'alphaNumeric',
  180. 'required' => true
  181. ));
  182. $TestModel->whitelist = array('name');
  183. $TestModel->save(array('name' => '#$$#', 'title' => '$$$$'));
  184. $expected = array('name' => array('This field cannot be left blank'));
  185. $this->assertEquals($expected, $TestModel->validationErrors);
  186. }
  187. /**
  188. * testValidates method
  189. *
  190. * @return void
  191. */
  192. public function testValidates() {
  193. $TestModel = new TestValidate();
  194. $TestModel->validate = array(
  195. 'user_id' => 'numeric',
  196. 'title' => array('allowEmpty' => false, 'rule' => 'notEmpty'),
  197. 'body' => 'notEmpty'
  198. );
  199. $data = array('TestValidate' => array(
  200. 'user_id' => '1',
  201. 'title' => '',
  202. 'body' => 'body'
  203. ));
  204. $result = $TestModel->create($data);
  205. $this->assertEquals($data, $result);
  206. $result = $TestModel->validates();
  207. $this->assertFalse($result);
  208. $data = array('TestValidate' => array(
  209. 'user_id' => '1',
  210. 'title' => 'title',
  211. 'body' => 'body'
  212. ));
  213. $result = $TestModel->create($data) && $TestModel->validates();
  214. $this->assertTrue($result);
  215. $data = array('TestValidate' => array(
  216. 'user_id' => '1',
  217. 'title' => '0',
  218. 'body' => 'body'
  219. ));
  220. $result = $TestModel->create($data);
  221. $this->assertEquals($data, $result);
  222. $result = $TestModel->validates();
  223. $this->assertTrue($result);
  224. $data = array('TestValidate' => array(
  225. 'user_id' => '1',
  226. 'title' => 0,
  227. 'body' => 'body'
  228. ));
  229. $result = $TestModel->create($data);
  230. $this->assertEquals($data, $result);
  231. $result = $TestModel->validates();
  232. $this->assertTrue($result);
  233. $TestModel->validate['modified'] = array('allowEmpty' => true, 'rule' => 'date');
  234. $data = array('TestValidate' => array(
  235. 'user_id' => '1',
  236. 'title' => 0,
  237. 'body' => 'body',
  238. 'modified' => ''
  239. ));
  240. $result = $TestModel->create($data);
  241. $this->assertEquals($data, $result);
  242. $result = $TestModel->validates();
  243. $this->assertTrue($result);
  244. $data = array('TestValidate' => array(
  245. 'user_id' => '1',
  246. 'title' => 0,
  247. 'body' => 'body',
  248. 'modified' => '2007-05-01'
  249. ));
  250. $result = $TestModel->create($data);
  251. $this->assertEquals($data, $result);
  252. $result = $TestModel->validates();
  253. $this->assertTrue($result);
  254. $data = array('TestValidate' => array(
  255. 'user_id' => '1',
  256. 'title' => 0,
  257. 'body' => 'body',
  258. 'modified' => 'invalid-date-here'
  259. ));
  260. $result = $TestModel->create($data);
  261. $this->assertEquals($data, $result);
  262. $result = $TestModel->validates();
  263. $this->assertFalse($result);
  264. $data = array('TestValidate' => array(
  265. 'user_id' => '1',
  266. 'title' => 0,
  267. 'body' => 'body',
  268. 'modified' => 0
  269. ));
  270. $result = $TestModel->create($data);
  271. $this->assertEquals($data, $result);
  272. $result = $TestModel->validates();
  273. $this->assertFalse($result);
  274. $data = array('TestValidate' => array(
  275. 'user_id' => '1',
  276. 'title' => 0,
  277. 'body' => 'body',
  278. 'modified' => '0'
  279. ));
  280. $result = $TestModel->create($data);
  281. $this->assertEquals($data, $result);
  282. $result = $TestModel->validates();
  283. $this->assertFalse($result);
  284. $TestModel->validate['modified'] = array('allowEmpty' => false, 'rule' => 'date');
  285. $data = array('TestValidate' => array('modified' => null));
  286. $result = $TestModel->create($data);
  287. $this->assertEquals($data, $result);
  288. $result = $TestModel->validates();
  289. $this->assertFalse($result);
  290. $data = array('TestValidate' => array('modified' => false));
  291. $result = $TestModel->create($data);
  292. $this->assertEquals($data, $result);
  293. $result = $TestModel->validates();
  294. $this->assertFalse($result);
  295. $data = array('TestValidate' => array('modified' => ''));
  296. $result = $TestModel->create($data);
  297. $this->assertEquals($data, $result);
  298. $result = $TestModel->validates();
  299. $this->assertFalse($result);
  300. $data = array('TestValidate' => array(
  301. 'modified' => '2007-05-01'
  302. ));
  303. $result = $TestModel->create($data);
  304. $this->assertEquals($data, $result);
  305. $result = $TestModel->validates();
  306. $this->assertTrue($result);
  307. $TestModel->validate['slug'] = array('allowEmpty' => false, 'rule' => array('maxLength', 45));
  308. $data = array('TestValidate' => array(
  309. 'user_id' => '1',
  310. 'title' => 0,
  311. 'body' => 'body',
  312. 'slug' => ''
  313. ));
  314. $result = $TestModel->create($data);
  315. $this->assertEquals($data, $result);
  316. $result = $TestModel->validates();
  317. $this->assertFalse($result);
  318. $data = array('TestValidate' => array(
  319. 'user_id' => '1',
  320. 'title' => 0,
  321. 'body' => 'body',
  322. 'slug' => 'slug-right-here'
  323. ));
  324. $result = $TestModel->create($data);
  325. $this->assertEquals($data, $result);
  326. $result = $TestModel->validates();
  327. $this->assertTrue($result);
  328. $data = array('TestValidate' => array(
  329. 'user_id' => '1',
  330. 'title' => 0,
  331. 'body' => 'body',
  332. 'slug' => 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'
  333. ));
  334. $result = $TestModel->create($data);
  335. $this->assertEquals($data, $result);
  336. $result = $TestModel->validates();
  337. $this->assertFalse($result);
  338. $TestModel->validate = array(
  339. 'number' => array(
  340. 'rule' => 'validateNumber',
  341. 'min' => 3,
  342. 'max' => 5
  343. ),
  344. 'title' => array(
  345. 'allowEmpty' => false,
  346. 'rule' => 'notEmpty'
  347. ));
  348. $data = array('TestValidate' => array(
  349. 'title' => 'title',
  350. 'number' => '0'
  351. ));
  352. $result = $TestModel->create($data);
  353. $this->assertEquals($data, $result);
  354. $result = $TestModel->validates();
  355. $this->assertFalse($result);
  356. $data = array('TestValidate' => array(
  357. 'title' => 'title',
  358. 'number' => 0
  359. ));
  360. $result = $TestModel->create($data);
  361. $this->assertEquals($data, $result);
  362. $result = $TestModel->validates();
  363. $this->assertFalse($result);
  364. $data = array('TestValidate' => array(
  365. 'title' => 'title',
  366. 'number' => '3'
  367. ));
  368. $result = $TestModel->create($data);
  369. $this->assertEquals($data, $result);
  370. $result = $TestModel->validates();
  371. $this->assertTrue($result);
  372. $data = array('TestValidate' => array(
  373. 'title' => 'title',
  374. 'number' => 3
  375. ));
  376. $result = $TestModel->create($data);
  377. $this->assertEquals($data, $result);
  378. $result = $TestModel->validates();
  379. $this->assertTrue($result);
  380. $TestModel->validate = array(
  381. 'number' => array(
  382. 'rule' => 'validateNumber',
  383. 'min' => 5,
  384. 'max' => 10
  385. ),
  386. 'title' => array(
  387. 'allowEmpty' => false,
  388. 'rule' => 'notEmpty'
  389. ));
  390. $data = array('TestValidate' => array(
  391. 'title' => 'title',
  392. 'number' => '3'
  393. ));
  394. $result = $TestModel->create($data);
  395. $this->assertEquals($data, $result);
  396. $result = $TestModel->validates();
  397. $this->assertFalse($result);
  398. $data = array('TestValidate' => array(
  399. 'title' => 'title',
  400. 'number' => 3
  401. ));
  402. $result = $TestModel->create($data);
  403. $this->assertEquals($data, $result);
  404. $result = $TestModel->validates();
  405. $this->assertFalse($result);
  406. $TestModel->validate = array(
  407. 'title' => array(
  408. 'allowEmpty' => false,
  409. 'rule' => 'validateTitle'
  410. ));
  411. $data = array('TestValidate' => array('title' => ''));
  412. $result = $TestModel->create($data);
  413. $this->assertEquals($data, $result);
  414. $result = $TestModel->validates();
  415. $this->assertFalse($result);
  416. $data = array('TestValidate' => array('title' => 'new title'));
  417. $result = $TestModel->create($data);
  418. $this->assertEquals($data, $result);
  419. $result = $TestModel->validates();
  420. $this->assertFalse($result);
  421. $data = array('TestValidate' => array('title' => 'title-new'));
  422. $result = $TestModel->create($data);
  423. $this->assertEquals($data, $result);
  424. $result = $TestModel->validates();
  425. $this->assertTrue($result);
  426. $TestModel->validate = array('title' => array(
  427. 'allowEmpty' => true,
  428. 'rule' => 'validateTitle'
  429. ));
  430. $data = array('TestValidate' => array('title' => ''));
  431. $result = $TestModel->create($data);
  432. $this->assertEquals($data, $result);
  433. $result = $TestModel->validates();
  434. $this->assertTrue($result);
  435. $TestModel->validate = array(
  436. 'title' => array(
  437. 'length' => array(
  438. 'allowEmpty' => true,
  439. 'rule' => array('maxLength', 10)
  440. )));
  441. $data = array('TestValidate' => array('title' => ''));
  442. $result = $TestModel->create($data);
  443. $this->assertEquals($data, $result);
  444. $result = $TestModel->validates();
  445. $this->assertTrue($result);
  446. $TestModel->validate = array(
  447. 'title' => array(
  448. 'rule' => array('userDefined', 'Article', 'titleDuplicate')
  449. ));
  450. $data = array('TestValidate' => array('title' => 'My Article Title'));
  451. $result = $TestModel->create($data);
  452. $this->assertEquals($data, $result);
  453. $result = $TestModel->validates();
  454. $this->assertFalse($result);
  455. $data = array('TestValidate' => array(
  456. 'title' => 'My Article With a Different Title'
  457. ));
  458. $result = $TestModel->create($data);
  459. $this->assertEquals($data, $result);
  460. $result = $TestModel->validates();
  461. $this->assertTrue($result);
  462. $TestModel->validate = array(
  463. 'title' => array(
  464. 'tooShort' => array('rule' => array('minLength', 50)),
  465. 'onlyLetters' => array('rule' => '/^[a-z]+$/i')
  466. ),
  467. );
  468. $data = array('TestValidate' => array(
  469. 'title' => 'I am a short string'
  470. ));
  471. $TestModel->create($data);
  472. $result = $TestModel->validates();
  473. $this->assertFalse($result);
  474. $result = $TestModel->validationErrors;
  475. $expected = array(
  476. 'title' => array('tooShort')
  477. );
  478. $this->assertEquals($expected, $result);
  479. $TestModel->validate = array(
  480. 'title' => array(
  481. 'tooShort' => array(
  482. 'rule' => array('minLength', 50),
  483. 'last' => false
  484. ),
  485. 'onlyLetters' => array('rule' => '/^[a-z]+$/i')
  486. ),
  487. );
  488. $data = array('TestValidate' => array(
  489. 'title' => 'I am a short string'
  490. ));
  491. $TestModel->create($data);
  492. $result = $TestModel->validates();
  493. $this->assertFalse($result);
  494. $result = $TestModel->validationErrors;
  495. $expected = array(
  496. 'title' => array('tooShort', 'onlyLetters')
  497. );
  498. $this->assertEquals($expected, $result);
  499. $result = $TestModel->validationErrors;
  500. $this->assertEquals($expected, $result);
  501. }
  502. /**
  503. * test that validates() checks all the 'with' associations as well for validation
  504. * as this can cause partial/wrong data insertion.
  505. *
  506. * @return void
  507. */
  508. public function testValidatesWithAssociations() {
  509. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  510. $data = array(
  511. 'Something' => array(
  512. 'id' => 5,
  513. 'title' => 'Extra Fields',
  514. 'body' => 'Extra Fields Body',
  515. 'published' => '1'
  516. ),
  517. 'SomethingElse' => array(
  518. array('something_else_id' => 1, 'doomed' => '')
  519. )
  520. );
  521. $Something = new Something();
  522. $JoinThing = $Something->JoinThing;
  523. $JoinThing->validate = array('doomed' => array('rule' => 'notEmpty'));
  524. $expectedError = array('doomed' => array('This field cannot be left blank'));
  525. $Something->create();
  526. $result = $Something->save($data);
  527. $this->assertFalse($result, 'Save occurred even when with models failed. %s');
  528. $this->assertEquals($expectedError, $JoinThing->validationErrors);
  529. $count = $Something->find('count', array('conditions' => array('Something.id' => $data['Something']['id'])));
  530. $this->assertSame($count, 0);
  531. $data = array(
  532. 'Something' => array(
  533. 'id' => 5,
  534. 'title' => 'Extra Fields',
  535. 'body' => 'Extra Fields Body',
  536. 'published' => '1'
  537. ),
  538. 'SomethingElse' => array(
  539. array('something_else_id' => 1, 'doomed' => 1),
  540. array('something_else_id' => 1, 'doomed' => '')
  541. )
  542. );
  543. $Something->create();
  544. $result = $Something->save($data);
  545. $this->assertFalse($result, 'Save occurred even when with models failed. %s');
  546. $joinRecords = $JoinThing->find('count', array(
  547. 'conditions' => array('JoinThing.something_id' => $data['Something']['id'])
  548. ));
  549. $this->assertEquals(0, $joinRecords, 'Records were saved on the join table. %s');
  550. }
  551. /**
  552. * test that saveAll and with models with validation interact well
  553. *
  554. * @return void
  555. */
  556. public function testValidatesWithModelsAndSaveAll() {
  557. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  558. $data = array(
  559. 'Something' => array(
  560. 'id' => 5,
  561. 'title' => 'Extra Fields',
  562. 'body' => 'Extra Fields Body',
  563. 'published' => '1'
  564. ),
  565. 'SomethingElse' => array(
  566. array('something_else_id' => 1, 'doomed' => '')
  567. )
  568. );
  569. $Something = new Something();
  570. $JoinThing = $Something->JoinThing;
  571. $JoinThing->validate = array('doomed' => array('rule' => 'notEmpty'));
  572. $expectedError = array('doomed' => array('This field cannot be left blank'));
  573. $Something->create();
  574. $result = $Something->saveAll($data, array('validate' => 'only'));
  575. $this->assertFalse($result);
  576. $result = $Something->validateAssociated($data);
  577. $this->assertFalse($result);
  578. $this->assertEquals($expectedError, $JoinThing->validationErrors);
  579. $result = $Something->validator()->validateAssociated($data);
  580. $this->assertFalse($result);
  581. $Something->create();
  582. $result = $Something->saveAll($data, array('validate' => 'first'));
  583. $this->assertFalse($result);
  584. $this->assertEquals($expectedError, $JoinThing->validationErrors);
  585. $count = $Something->find('count', array('conditions' => array('Something.id' => $data['Something']['id'])));
  586. $this->assertSame($count, 0);
  587. $joinRecords = $JoinThing->find('count', array(
  588. 'conditions' => array('JoinThing.something_id' => $data['Something']['id'])
  589. ));
  590. $this->assertEquals(0, $joinRecords, 'Records were saved on the join table. %s');
  591. }
  592. /**
  593. * test that saveAll and with models at initial insert (no id has set yet)
  594. * with validation interact well
  595. *
  596. * @return void
  597. */
  598. public function testValidatesWithModelsAndSaveAllWithoutId() {
  599. $this->loadFixtures('Post', 'Author');
  600. $data = array(
  601. 'Author' => array(
  602. 'name' => 'Foo Bar',
  603. ),
  604. 'Post' => array(
  605. array('title' => 'Hello'),
  606. array('title' => 'World'),
  607. )
  608. );
  609. $Author = new Author();
  610. $Post = $Author->Post;
  611. $Post->validate = array('author_id' => array('rule' => 'numeric'));
  612. $Author->create();
  613. $result = $Author->saveAll($data, array('validate' => 'only'));
  614. $this->assertTrue($result);
  615. $result = $Author->validateAssociated($data);
  616. $this->assertTrue($result);
  617. $this->assertTrue($result);
  618. $Author->create();
  619. $result = $Author->saveAll($data, array('validate' => 'first'));
  620. $this->assertTrue($result);
  621. $this->assertFalse(is_null($Author->id));
  622. $id = $Author->id;
  623. $count = $Author->find('count', array('conditions' => array('Author.id' => $id)));
  624. $this->assertSame($count, 1);
  625. $count = $Post->find('count', array(
  626. 'conditions' => array('Post.author_id' => $id)
  627. ));
  628. $this->assertEquals($count, count($data['Post']));
  629. }
  630. /**
  631. * Test that missing validation methods trigger errors in development mode.
  632. * Helps to make development easier.
  633. *
  634. * @expectedException PHPUnit_Framework_Error
  635. * @return void
  636. */
  637. public function testMissingValidationErrorTriggering() {
  638. Configure::write('debug', 2);
  639. $TestModel = new ValidationTest1();
  640. $TestModel->create(array('title' => 'foo'));
  641. $TestModel->validate = array(
  642. 'title' => array(
  643. 'rule' => array('thisOneBringsThePain'),
  644. 'required' => true
  645. )
  646. );
  647. $TestModel->invalidFields(array('fieldList' => array('title')));
  648. }
  649. /**
  650. * Test placeholder replacement when validation message is an array
  651. *
  652. * @return void
  653. */
  654. public function testValidationMessageAsArray() {
  655. $TestModel = new ValidationTest1();
  656. $TestModel->validate = array(
  657. 'title' => array(
  658. 'minLength' => array(
  659. 'rule' => array('minLength', 6),
  660. 'required' => true,
  661. 'message' => 'Minimum length allowed is %d chars',
  662. 'last' => false
  663. ),
  664. 'between' => array(
  665. 'rule' => array('between', 5, 15),
  666. 'message' => array('You may enter up to %s chars (minimum is %s chars)', 14, 6)
  667. )
  668. )
  669. );
  670. $TestModel->create();
  671. $expected = array(
  672. 'title' => array(
  673. 'Minimum length allowed is 6 chars',
  674. )
  675. );
  676. $TestModel->invalidFields();
  677. $this->assertEquals($expected, $TestModel->validationErrors);
  678. $TestModel->create(array('title' => 'foo'));
  679. $expected = array(
  680. 'title' => array(
  681. 'Minimum length allowed is 6 chars',
  682. 'You may enter up to 14 chars (minimum is 6 chars)'
  683. )
  684. );
  685. $TestModel->invalidFields();
  686. $this->assertEquals($expected, $TestModel->validationErrors);
  687. }
  688. /**
  689. * Test validation message translation
  690. *
  691. * @return void
  692. */
  693. public function testValidationMessageTranslation() {
  694. $lang = Configure::read('Config.language');
  695. Configure::write('Config.language', 'en');
  696. App::build(array(
  697. 'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS),
  698. ), App::RESET);
  699. $TestModel = new ValidationTest1();
  700. $TestModel->validationDomain = 'validation_messages';
  701. $TestModel->validate = array(
  702. 'title' => array(
  703. array(
  704. 'rule' => array('customValidationMethod', 'arg1'),
  705. 'required' => true,
  706. 'message' => 'Validation failed: %s'
  707. )
  708. )
  709. );
  710. $TestModel->create();
  711. $expected = array(
  712. 'title' => array(
  713. 'Translated validation failed: Translated arg1',
  714. )
  715. );
  716. $TestModel->invalidFields();
  717. $this->assertEquals($expected, $TestModel->validationErrors);
  718. $TestModel->validationDomain = 'default';
  719. Configure::write('Config.language', $lang);
  720. App::build();
  721. }
  722. /**
  723. * Test for 'on' => [create|update] in validation rules.
  724. *
  725. * @return void
  726. */
  727. public function testStateValidation() {
  728. $this->loadFixtures('Article');
  729. $Article = new Article();
  730. $data = array(
  731. 'Article' => array(
  732. 'title' => '',
  733. 'body' => 'Extra Fields Body',
  734. 'published' => '1'
  735. )
  736. );
  737. $Article->validate = array(
  738. 'title' => array(
  739. 'notempty' => array(
  740. 'rule' => 'notEmpty',
  741. 'on' => 'create'
  742. )
  743. )
  744. );
  745. $Article->create($data);
  746. $this->assertFalse($Article->validates());
  747. $Article->save(null, array('validate' => false));
  748. $data['Article']['id'] = $Article->id;
  749. $Article->set($data);
  750. $this->assertTrue($Article->validates());
  751. unset($data['Article']['id']);
  752. $Article->validate = array(
  753. 'title' => array(
  754. 'notempty' => array(
  755. 'rule' => 'notEmpty',
  756. 'on' => 'update'
  757. )
  758. )
  759. );
  760. $Article->create($data);
  761. $this->assertTrue($Article->validates());
  762. $Article->save(null, array('validate' => false));
  763. $data['Article']['id'] = $Article->id;
  764. $Article->set($data);
  765. $this->assertFalse($Article->validates());
  766. }
  767. /**
  768. * Test for 'required' => [create|update] in validation rules.
  769. *
  770. * @return void
  771. */
  772. public function testStateRequiredValidation() {
  773. $this->loadFixtures('Article');
  774. $Article = new Article();
  775. // no title field present
  776. $data = array(
  777. 'Article' => array(
  778. 'body' => 'Extra Fields Body',
  779. 'published' => '1'
  780. )
  781. );
  782. $Article->validate = array(
  783. 'title' => array(
  784. 'notempty' => array(
  785. 'rule' => 'notEmpty',
  786. 'required' => 'create'
  787. )
  788. )
  789. );
  790. $Article->create($data);
  791. $this->assertFalse($Article->validates());
  792. $Article->save(null, array('validate' => false));
  793. $data['Article']['id'] = $Article->id;
  794. $Article->set($data);
  795. $this->assertTrue($Article->validates());
  796. unset($data['Article']['id']);
  797. $Article->validate = array(
  798. 'title' => array(
  799. 'notempty' => array(
  800. 'rule' => 'notEmpty',
  801. 'required' => 'update'
  802. )
  803. )
  804. );
  805. $Article->create($data);
  806. $this->assertTrue($Article->validates());
  807. $Article->save(null, array('validate' => false));
  808. $data['Article']['id'] = $Article->id;
  809. $Article->set($data);
  810. $this->assertFalse($Article->validates());
  811. }
  812. /**
  813. * Test that 'required' and 'on' are not conflicting
  814. *
  815. * @return void
  816. */
  817. public function testOnRequiredConflictValidation() {
  818. $this->loadFixtures('Article');
  819. $Article = new Article();
  820. // no title field present
  821. $data = array(
  822. 'Article' => array(
  823. 'body' => 'Extra Fields Body',
  824. 'published' => '1'
  825. )
  826. );
  827. $Article->validate = array(
  828. 'title' => array(
  829. 'notempty' => array(
  830. 'rule' => 'notEmpty',
  831. 'required' => 'create',
  832. 'on' => 'create'
  833. )
  834. )
  835. );
  836. $Article->create($data);
  837. $this->assertFalse($Article->validates());
  838. $Article->validate = array(
  839. 'title' => array(
  840. 'notempty' => array(
  841. 'rule' => 'notEmpty',
  842. 'required' => 'update',
  843. 'on' => 'create'
  844. )
  845. )
  846. );
  847. $Article->create($data);
  848. $this->assertTrue($Article->validates());
  849. $Article->validate = array(
  850. 'title' => array(
  851. 'notempty' => array(
  852. 'rule' => 'notEmpty',
  853. 'required' => 'create',
  854. 'on' => 'update'
  855. )
  856. )
  857. );
  858. $Article->create($data);
  859. $this->assertTrue($Article->validates());
  860. $Article->validate = array(
  861. 'title' => array(
  862. 'notempty' => array(
  863. 'rule' => 'notEmpty',
  864. 'required' => 'update',
  865. 'on' => 'update'
  866. )
  867. )
  868. );
  869. $Article->create($data);
  870. $this->assertTrue($Article->validates());
  871. $Article->validate = array(
  872. 'title' => array(
  873. 'notempty' => array(
  874. 'rule' => 'notEmpty',
  875. 'required' => 'create',
  876. 'on' => 'create'
  877. )
  878. )
  879. );
  880. $Article->save(null, array('validate' => false));
  881. $data['Article']['id'] = $Article->id;
  882. $Article->set($data);
  883. $this->assertTrue($Article->validates());
  884. $Article->validate = array(
  885. 'title' => array(
  886. 'notempty' => array(
  887. 'rule' => 'notEmpty',
  888. 'required' => 'update',
  889. 'on' => 'create'
  890. )
  891. )
  892. );
  893. $Article->set($data);
  894. $this->assertTrue($Article->validates());
  895. $Article->validate = array(
  896. 'title' => array(
  897. 'notempty' => array(
  898. 'rule' => 'notEmpty',
  899. 'required' => 'create',
  900. 'on' => 'update'
  901. )
  902. )
  903. );
  904. $Article->set($data);
  905. $this->assertTrue($Article->validates());
  906. $Article->validate = array(
  907. 'title' => array(
  908. 'notempty' => array(
  909. 'rule' => 'notEmpty',
  910. 'required' => 'update',
  911. 'on' => 'update'
  912. )
  913. )
  914. );
  915. $Article->set($data);
  916. $this->assertFalse($Article->validates());
  917. }
  918. /**
  919. * testSaveAllDeepValidateOnly
  920. * tests the validate methods with deeper recursive data
  921. *
  922. * @return void
  923. */
  924. public function testSaveAllDeepValidateOnly() {
  925. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  926. $TestModel = new Article();
  927. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  928. $TestModel->hasAndBelongsToMany = array();
  929. $TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
  930. $TestModel->Comment->validate['comment'] = 'notEmpty';
  931. $data = array(
  932. 'Article' => array('id' => 2),
  933. 'Comment' => array(
  934. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
  935. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  936. )
  937. );
  938. $result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => true));
  939. $this->assertTrue($result);
  940. $result = $TestModel->validateAssociated($data, array('deep' => true));
  941. $this->assertTrue($result);
  942. $data = array(
  943. 'Article' => array('id' => 2),
  944. 'Comment' => array(
  945. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  946. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  947. )
  948. );
  949. $result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => true));
  950. $this->assertFalse($result);
  951. $result = $TestModel->validateAssociated($data, array('deep' => true));
  952. $this->assertFalse($result);
  953. $data = array(
  954. 'Article' => array('id' => 2),
  955. 'Comment' => array(
  956. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
  957. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  958. )
  959. );
  960. $expected = array(
  961. 'Article' => true,
  962. 'Comment' => array(
  963. true,
  964. true
  965. )
  966. );
  967. $result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  968. $this->assertSame($expected, $result);
  969. $result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => true));
  970. $this->assertSame($expected, $result);
  971. $data = array(
  972. 'Article' => array('id' => 2),
  973. 'Comment' => array(
  974. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  975. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  976. )
  977. );
  978. $expected = array(
  979. 'Article' => true,
  980. 'Comment' => array(
  981. false,
  982. true
  983. )
  984. );
  985. $result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  986. $this->assertSame($expected, $result);
  987. $result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => true));
  988. $this->assertSame($expected, $result);
  989. $data = array(
  990. 'Article' => array('id' => 2),
  991. 'Comment' => array(
  992. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  993. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
  994. )
  995. );
  996. $result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => true));
  997. $this->assertTrue($result);
  998. $result = $TestModel->validateAssociated($data, array('deep' => true));
  999. $this->assertTrue($result);
  1000. $data = array(
  1001. 'Article' => array('id' => 2),
  1002. 'Comment' => array(
  1003. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  1004. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  1005. )
  1006. );
  1007. $result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => true));
  1008. $this->assertFalse($result);
  1009. $result = $TestModel->validateAssociated($data, array('deep' => true));
  1010. $this->assertFalse($result);
  1011. $data = array(
  1012. 'Article' => array('id' => 2),
  1013. 'Comment' => array(
  1014. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  1015. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsave'))
  1016. )
  1017. );
  1018. $expected = array(
  1019. 'Article' => true,
  1020. 'Comment' => array(
  1021. true,
  1022. true
  1023. )
  1024. );
  1025. $result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  1026. $this->assertSame($expected, $result);
  1027. $result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => true));
  1028. $this->assertSame($expected, $result);
  1029. $data = array(
  1030. 'Article' => array('id' => 2),
  1031. 'Comment' => array(
  1032. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  1033. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  1034. )
  1035. );
  1036. $expected = array(
  1037. 'Article' => true,
  1038. 'Comment' => array(
  1039. true,
  1040. false
  1041. )
  1042. );
  1043. $result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  1044. $this->assertSame($expected, $result);
  1045. $result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => true));
  1046. $this->assertSame($expected, $result);
  1047. $expected = array(
  1048. 'Comment' => array(
  1049. 1 => array(
  1050. 'Attachment' => array(
  1051. 'attachment' => array('This field cannot be left blank')
  1052. )
  1053. )
  1054. )
  1055. );
  1056. $result = $TestModel->validationErrors;
  1057. $this->assertSame($expected, $result);
  1058. $data = array(
  1059. 'Attachment' => array(
  1060. 'attachment' => 'deepsave insert',
  1061. ),
  1062. 'Comment' => array(
  1063. 'comment' => 'First comment deepsave insert',
  1064. 'published' => 'Y',
  1065. 'user_id' => 5,
  1066. 'Article' => array(
  1067. 'title' => 'First Article deepsave insert',
  1068. 'body' => 'First Article Body deepsave insert',
  1069. 'User' => array(
  1070. 'user' => 'deepsave',
  1071. 'password' => 'magic'
  1072. ),
  1073. ),
  1074. )
  1075. );
  1076. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  1077. $this->assertTrue($result);
  1078. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => true));
  1079. $this->assertTrue($result);
  1080. $expected = array(
  1081. 'Attachment' => true,
  1082. 'Comment' => true
  1083. );
  1084. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  1085. $this->assertSame($expected, $result);
  1086. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
  1087. $this->assertSame($expected, $result);
  1088. $data = array(
  1089. 'Attachment' => array(
  1090. 'attachment' => 'deepsave insert',
  1091. ),
  1092. 'Comment' => array(
  1093. 'comment' => 'First comment deepsave insert',
  1094. 'published' => 'Y',
  1095. 'user_id' => 5,
  1096. 'Article' => array(
  1097. 'title' => 'First Article deepsave insert',
  1098. 'body' => 'First Article Body deepsave insert',
  1099. 'User' => array(
  1100. 'user' => '',
  1101. 'password' => 'magic'
  1102. ),
  1103. ),
  1104. )
  1105. );
  1106. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  1107. $this->assertFalse($result);
  1108. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => true));
  1109. $this->assertFalse($result);
  1110. $result = $TestModel->Comment->Attachment->validationErrors;
  1111. $expected = array(
  1112. 'Comment' => array(
  1113. 'Article' => array(
  1114. 'User' => array(
  1115. 'user' => array('This field cannot be left blank')
  1116. )
  1117. )
  1118. )
  1119. );
  1120. $this->assertSame($expected, $result);
  1121. $expected = array(
  1122. 'Attachment' => true,
  1123. 'Comment' => false
  1124. );
  1125. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  1126. $this->assertEquals($expected, $result);
  1127. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
  1128. $this->assertEquals($expected, $result);
  1129. $data['Comment']['Article']['body'] = '';
  1130. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  1131. $this->assertFalse($result);
  1132. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => true));
  1133. $this->assertFalse($result);
  1134. $result = $TestModel->Comment->Attachment->validationErrors;
  1135. $expected = array(
  1136. 'Comment' => array(
  1137. 'Article' => array(
  1138. 'body' => array('This field cannot be left blank'),
  1139. 'User' => array(
  1140. 'user' => array('This field cannot be left blank')
  1141. )
  1142. )
  1143. )
  1144. );
  1145. $this->assertSame($expected, $result);
  1146. $expected = array(
  1147. 'Attachment' => true,
  1148. 'Comment' => false
  1149. );
  1150. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  1151. $this->assertEquals($expected, $result);
  1152. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
  1153. $this->assertEquals($expected, $result);
  1154. $data['Comment']['comment'] = '';
  1155. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  1156. $this->assertFalse($result);
  1157. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => true));
  1158. $this->assertFalse($result);
  1159. $result = $TestModel->Comment->Attachment->validationErrors;
  1160. $expected = array(
  1161. 'Comment' => array(
  1162. 'comment' => array('This field cannot be left blank'),
  1163. 'Article' => array(
  1164. 'body' => array('This field cannot be left blank'),
  1165. 'User' => array(
  1166. 'user' => array('This field cannot be left blank')
  1167. )
  1168. )
  1169. )
  1170. );
  1171. $this->assertSame($expected, $result);
  1172. $expected = array(
  1173. 'Attachment' => true,
  1174. 'Comment' => false
  1175. );
  1176. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  1177. $this->assertEquals($expected, $result);
  1178. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
  1179. $this->assertEquals($expected, $result);
  1180. $data['Attachment']['attachment'] = '';
  1181. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  1182. $this->assertFalse($result);
  1183. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => true));
  1184. $this->assertFalse($result);
  1185. $result = $TestModel->Comment->Attachment->validationErrors;
  1186. $expected = array(
  1187. 'attachment' => array('This field cannot be left blank'),
  1188. 'Comment' => array(
  1189. 'comment' => array('This field cannot be left blank'),
  1190. 'Article' => array(
  1191. 'body' => array('This field cannot be left blank'),
  1192. 'User' => array(
  1193. 'user' => array('This field cannot be left blank')
  1194. )
  1195. )
  1196. )
  1197. );
  1198. $this->assertSame($expected, $result);
  1199. $result = $TestModel->Comment->validationErrors;
  1200. $expected = array(
  1201. 'comment' => array('This field cannot be left blank'),
  1202. 'Article' => array(
  1203. 'body' => array('This field cannot be left blank'),
  1204. 'User' => array(
  1205. 'user' => array('This field cannot be left blank')
  1206. )
  1207. )
  1208. );
  1209. $this->assertSame($expected, $result);
  1210. $expected = array(
  1211. 'Attachment' => false,
  1212. 'Comment' => false
  1213. );
  1214. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  1215. $this->assertEquals($expected, $result);
  1216. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
  1217. $this->assertEquals($expected, $result);
  1218. }
  1219. /**
  1220. * testSaveAllNotDeepValidateOnly
  1221. * tests the validate methods to not validate deeper recursive data
  1222. *
  1223. * @return void
  1224. */
  1225. public function testSaveAllNotDeepValidateOnly() {
  1226. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  1227. $TestModel = new Article();
  1228. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  1229. $TestModel->hasAndBelongsToMany = array();
  1230. $TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
  1231. $TestModel->Comment->validate['comment'] = 'notEmpty';
  1232. $data = array(
  1233. 'Article' => array('id' => 2, 'body' => ''),
  1234. 'Comment' => array(
  1235. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  1236. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  1237. )
  1238. );
  1239. $result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => false));
  1240. $this->assertFalse($result);
  1241. $result = $TestModel->validateAssociated($data, array('deep' => false));
  1242. $this->assertFalse($result);
  1243. $expected = array('body' => array('This field cannot be left blank'));
  1244. $result = $TestModel->validationErrors;
  1245. $this->assertSame($expected, $result);
  1246. $data = array(
  1247. 'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
  1248. 'Comment' => array(
  1249. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  1250. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  1251. )
  1252. );
  1253. $result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => false));
  1254. $this->assertTrue($result);
  1255. $result = $TestModel->validateAssociated($data, array('deep' => false));
  1256. $this->assertTrue($result);
  1257. $data = array(
  1258. 'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
  1259. 'Comment' => array(
  1260. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  1261. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  1262. )
  1263. );
  1264. $expected = array(
  1265. 'Article' => true,
  1266. 'Comment' => array(
  1267. true,
  1268. true
  1269. )
  1270. );
  1271. $result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
  1272. $this->assertSame($expected, $result);
  1273. $result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => false));
  1274. $this->assertSame($expected, $result);
  1275. $data = array(
  1276. 'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
  1277. 'Comment' => array(
  1278. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  1279. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  1280. )
  1281. );
  1282. $result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => false));
  1283. $this->assertTrue($result);
  1284. $result = $TestModel->validateAssociated($data, array('deep' => false));
  1285. $this->assertTrue($result);
  1286. $data = array(
  1287. 'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
  1288. 'Comment' => array(
  1289. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  1290. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  1291. )
  1292. );
  1293. $expected = array(
  1294. 'Article' => true,
  1295. 'Comment' => array(
  1296. true,
  1297. true
  1298. )
  1299. );
  1300. $result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
  1301. $this->assertSame($expected, $result);
  1302. $result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => false));
  1303. $this->assertSame($expected, $result);
  1304. $expected = array();
  1305. $result = $TestModel->validationErrors;
  1306. $this->assertSame($expected, $result);
  1307. $data = array(
  1308. 'Attachment' => array(
  1309. 'attachment' => 'deepsave insert',
  1310. ),
  1311. 'Comment' => array(
  1312. 'comment' => 'First comment deepsave insert',
  1313. 'published' => 'Y',
  1314. 'user_id' => 5,
  1315. 'Article' => array(
  1316. 'title' => 'First Article deepsave insert ignored',
  1317. 'body' => 'First Article Body deepsave insert',
  1318. 'User' => array(
  1319. 'user' => '',
  1320. 'password' => 'magic'
  1321. ),
  1322. ),
  1323. )
  1324. );
  1325. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
  1326. $this->assertTrue($result);
  1327. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => false));
  1328. $this->assertTrue($result);
  1329. $result = $TestModel->Comment->Attachment->validationErrors;
  1330. $expected = array();
  1331. $this->assertSame($expected, $result);
  1332. $expected = array(
  1333. 'Attachment' => true,
  1334. 'Comment' => true
  1335. );
  1336. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
  1337. $this->assertEquals($expected, $result);
  1338. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => false));
  1339. $this->assertEquals($expected, $result);
  1340. $data['Comment']['Article']['body'] = '';
  1341. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
  1342. $this->assertTrue($result);
  1343. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => false));
  1344. $this->assertTrue($result);
  1345. $result = $TestModel->Comment->Attachment->validationErrors;
  1346. $expected = array();
  1347. $this->assertSame($expected, $result);
  1348. $expected = array(
  1349. 'Attachment' => true,
  1350. 'Comment' => true
  1351. );
  1352. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
  1353. $this->assertEquals($expected, $result);
  1354. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => false));
  1355. $this->assertEquals($expected, $result);
  1356. }
  1357. /**
  1358. * testValidateAssociated method
  1359. *
  1360. * @return void
  1361. */
  1362. public function testValidateAssociated() {
  1363. $this->loadFixtures('Comment', 'Attachment');
  1364. $TestModel = new Comment();
  1365. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  1366. $data = array(
  1367. 'Comment' => array(
  1368. 'comment' => 'This is the comment'
  1369. ),
  1370. 'Attachment' => array(
  1371. 'attachment' => ''
  1372. )
  1373. );
  1374. $result = $TestModel->saveAll($data, array('validate' => 'only'));
  1375. $this->assertFalse($result);
  1376. $result = $TestModel->validateAssociated($data);
  1377. $this->assertFalse($result);
  1378. $TestModel->validate = array('comment' => 'notEmpty');
  1379. $record = array(
  1380. 'Comment' => array(
  1381. 'user_id' => 1,
  1382. 'article_id' => 1,
  1383. 'comment' => '',
  1384. ),
  1385. 'Attachment' => array(
  1386. 'attachment' => ''
  1387. )
  1388. );
  1389. $result = $TestModel->saveAll($record, array('validate' => 'only'));
  1390. $this->assertFalse($result);
  1391. $result = $TestModel->validateAssociated($record);
  1392. $this->assertFalse($result);
  1393. $fieldList = array(
  1394. 'Comment' => array('id', 'article_id', 'user_id'),
  1395. 'Attachment' => array('comment_id')
  1396. );
  1397. $result = $TestModel->saveAll($record, array(
  1398. 'fieldList' => $fieldList, 'validate' => 'only'
  1399. ));
  1400. $this->assertTrue($result);
  1401. $this->assertEmpty($TestModel->validationErrors);
  1402. $result = $TestModel->validateAssociated($record, array('fieldList' => $fieldList));
  1403. $this->assertTrue($result);
  1404. $this->assertEmpty($TestModel->validationErrors);
  1405. $TestModel = new Article();
  1406. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  1407. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  1408. $data = array(
  1409. 'Article' => array('id' => 2),
  1410. 'Comment' => array(
  1411. array(
  1412. 'id' => 1,
  1413. 'comment' => '',
  1414. 'published' => 'Y',
  1415. 'user_id' => 1,
  1416. ),
  1417. array(
  1418. 'id' => 2,
  1419. 'comment' =>
  1420. 'comment',
  1421. 'published' => 'Y',
  1422. 'user_id' => 1
  1423. ),
  1424. array(
  1425. 'id' => 3,
  1426. 'comment' => '',
  1427. 'published' => 'Y',
  1428. 'user_id' => 1
  1429. )));
  1430. $result = $TestModel->saveAll($data, array('validate' => 'only'));
  1431. $this->assertFalse($result);
  1432. $result = $TestModel->validateAssociated($data);
  1433. $this->assertFalse($result);
  1434. $expected = array(
  1435. 'Article' => true,
  1436. 'Comment' => array(false, true, false)
  1437. );
  1438. $result = $TestModel->saveAll($data, array('atomic' => false, 'validate' => 'only'));
  1439. $this->assertSame($expected, $result);
  1440. $result = $TestModel->validateAssociated($data, array('atomic' => false));
  1441. $this->assertSame($expected, $result);
  1442. $expected = array('Comment' => array(
  1443. 0 => array('comment' => array('This field cannot be left blank')),
  1444. 2 => array('comment' => array('This field cannot be left blank'))
  1445. ));
  1446. $this->assertEquals($expected['Comment'], $TestModel->Comment->validationErrors);
  1447. $model = new Comment();
  1448. $model->deleteAll(true);
  1449. $model->validate = array('comment' => 'notEmpty');
  1450. $model->Attachment->validate = array('attachment' => 'notEmpty');
  1451. $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
  1452. $expected = array(
  1453. 'comment' => array('This field cannot be left blank'),
  1454. 'Attachment' => array(
  1455. 'attachment' => array('This field cannot be left blank')
  1456. )
  1457. );
  1458. $data = array(
  1459. 'Comment' => array('comment' => '', 'article_id' => 1, 'user_id' => 1),
  1460. 'Attachment' => array('attachment' => '')
  1461. );
  1462. $result = $model->saveAll($data, array('validate' => 'only'));
  1463. $this->assertFalse($result);
  1464. $result = $model->validateAssociated($data);
  1465. $this->assertFalse($result);
  1466. $this->assertEquals($expected, $model->validationErrors);
  1467. $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
  1468. }
  1469. /**
  1470. * testValidateMany method
  1471. *
  1472. * @return void
  1473. */
  1474. public function testValidateMany() {
  1475. $TestModel = new Article();
  1476. $TestModel->validate = array('title' => 'notEmpty');
  1477. $data = array(
  1478. 0 => array('title' => ''),
  1479. 1 => array('title' => 'title 1'),
  1480. 2 => array('title' => 'title 2'),
  1481. );
  1482. $expected = array(
  1483. 0 => array('title' => array('This field cannot be left blank')),
  1484. );
  1485. $result = $TestModel->saveAll($data, array('validate' => 'only'));
  1486. $this->assertFalse($result);
  1487. $this->assertEquals($expected, $TestModel->validationErrors);
  1488. $result = $TestModel->validateMany($data);
  1489. $this->assertFalse($result);
  1490. $this->assertEquals($expected, $TestModel->validationErrors);
  1491. $data = array(
  1492. 0 => array('title' => 'title 0'),
  1493. 1 => array('title' => ''),
  1494. 2 => array('title' => 'title 2'),
  1495. );
  1496. $expected = array(
  1497. 1 => array('title' => array('This field cannot be left blank')),
  1498. );
  1499. $result = $TestModel->saveAll($data, array('validate' => 'only'));
  1500. $this->assertFalse($result);
  1501. $this->assertEquals($expected, $TestModel->validationErrors);
  1502. $result = $TestModel->validateMany($data);
  1503. $this->assertFalse($result);
  1504. $this->assertEquals($expected, $TestModel->validationErrors);
  1505. }
  1506. /**
  1507. * testGetMethods method
  1508. *
  1509. * @return void
  1510. */
  1511. public function testGetMethods() {
  1512. $this->loadFixtures('Article', 'Comment');
  1513. $TestModel = new Article();
  1514. $Validator = $TestModel->validator();
  1515. $result = $Validator->getMethods();
  1516. $expected = array_map('strtolower', get_class_methods('Article'));
  1517. $this->assertEquals($expected, array_keys($result));
  1518. }
  1519. /**
  1520. * Tests that methods are refreshed when the list of behaviors change
  1521. *
  1522. * @return void
  1523. */
  1524. public function testGetM