PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/model/model_validation.test.php

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