ModelValidationTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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-2012, 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-2012, 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. 'ignoreOnSame' => '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 occurred 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 occurred 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 development 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. /**
  684. * Test for 'on' => [create|update] in validation rules.
  685. *
  686. * @return void
  687. */
  688. public function testStateValidation() {
  689. $this->loadFixtures('Article');
  690. $Article = new Article();
  691. $data = array(
  692. 'Article' => array(
  693. 'title' => '',
  694. 'body' => 'Extra Fields Body',
  695. 'published' => '1'
  696. )
  697. );
  698. $Article->validate = array(
  699. 'title' => array(
  700. 'notempty' => array(
  701. 'rule' => 'notEmpty',
  702. 'on' => 'create'
  703. )
  704. )
  705. );
  706. $Article->create($data);
  707. $this->assertFalse($Article->validates());
  708. $Article->save(null, array('validate' => false));
  709. $data['Article']['id'] = $Article->id;
  710. $Article->set($data);
  711. $this->assertTrue($Article->validates());
  712. unset($data['Article']['id']);
  713. $Article->validate = array(
  714. 'title' => array(
  715. 'notempty' => array(
  716. 'rule' => 'notEmpty',
  717. 'on' => 'update'
  718. )
  719. )
  720. );
  721. $Article->create($data);
  722. $this->assertTrue($Article->validates());
  723. $Article->save(null, array('validate' => false));
  724. $data['Article']['id'] = $Article->id;
  725. $Article->set($data);
  726. $this->assertFalse($Article->validates());
  727. }
  728. /**
  729. * Test for 'required' => [create|update] in validation rules.
  730. *
  731. * @return void
  732. */
  733. public function testStateRequiredValidation() {
  734. $this->loadFixtures('Article');
  735. $Article = new Article();
  736. // no title field present
  737. $data = array(
  738. 'Article' => array(
  739. 'body' => 'Extra Fields Body',
  740. 'published' => '1'
  741. )
  742. );
  743. $Article->validate = array(
  744. 'title' => array(
  745. 'notempty' => array(
  746. 'rule' => 'notEmpty',
  747. 'required' => 'create'
  748. )
  749. )
  750. );
  751. $Article->create($data);
  752. $this->assertFalse($Article->validates());
  753. $Article->save(null, array('validate' => false));
  754. $data['Article']['id'] = $Article->id;
  755. $Article->set($data);
  756. $this->assertTrue($Article->validates());
  757. unset($data['Article']['id']);
  758. $Article->validate = array(
  759. 'title' => array(
  760. 'notempty' => array(
  761. 'rule' => 'notEmpty',
  762. 'required' => 'update'
  763. )
  764. )
  765. );
  766. $Article->create($data);
  767. $this->assertTrue($Article->validates());
  768. $Article->save(null, array('validate' => false));
  769. $data['Article']['id'] = $Article->id;
  770. $Article->set($data);
  771. $this->assertFalse($Article->validates());
  772. }
  773. /**
  774. * Test that 'required' and 'on' are not conflicting
  775. *
  776. * @return void
  777. */
  778. public function testOnRequiredConflictValidation() {
  779. $this->loadFixtures('Article');
  780. $Article = new Article();
  781. // no title field present
  782. $data = array(
  783. 'Article' => array(
  784. 'body' => 'Extra Fields Body',
  785. 'published' => '1'
  786. )
  787. );
  788. $Article->validate = array(
  789. 'title' => array(
  790. 'notempty' => array(
  791. 'rule' => 'notEmpty',
  792. 'required' => 'create',
  793. 'on' => 'create'
  794. )
  795. )
  796. );
  797. $Article->create($data);
  798. $this->assertFalse($Article->validates());
  799. $Article->validate = array(
  800. 'title' => array(
  801. 'notempty' => array(
  802. 'rule' => 'notEmpty',
  803. 'required' => 'update',
  804. 'on' => 'create'
  805. )
  806. )
  807. );
  808. $Article->create($data);
  809. $this->assertTrue($Article->validates());
  810. $Article->validate = array(
  811. 'title' => array(
  812. 'notempty' => array(
  813. 'rule' => 'notEmpty',
  814. 'required' => 'create',
  815. 'on' => 'update'
  816. )
  817. )
  818. );
  819. $Article->create($data);
  820. $this->assertTrue($Article->validates());
  821. $Article->validate = array(
  822. 'title' => array(
  823. 'notempty' => array(
  824. 'rule' => 'notEmpty',
  825. 'required' => 'update',
  826. 'on' => 'update'
  827. )
  828. )
  829. );
  830. $Article->create($data);
  831. $this->assertTrue($Article->validates());
  832. $Article->validate = array(
  833. 'title' => array(
  834. 'notempty' => array(
  835. 'rule' => 'notEmpty',
  836. 'required' => 'create',
  837. 'on' => 'create'
  838. )
  839. )
  840. );
  841. $Article->save(null, array('validate' => false));
  842. $data['Article']['id'] = $Article->id;
  843. $Article->set($data);
  844. $this->assertTrue($Article->validates());
  845. $Article->validate = array(
  846. 'title' => array(
  847. 'notempty' => array(
  848. 'rule' => 'notEmpty',
  849. 'required' => 'update',
  850. 'on' => 'create'
  851. )
  852. )
  853. );
  854. $Article->set($data);
  855. $this->assertTrue($Article->validates());
  856. $Article->validate = array(
  857. 'title' => array(
  858. 'notempty' => array(
  859. 'rule' => 'notEmpty',
  860. 'required' => 'create',
  861. 'on' => 'update'
  862. )
  863. )
  864. );
  865. $Article->set($data);
  866. $this->assertTrue($Article->validates());
  867. $Article->validate = array(
  868. 'title' => array(
  869. 'notempty' => array(
  870. 'rule' => 'notEmpty',
  871. 'required' => 'update',
  872. 'on' => 'update'
  873. )
  874. )
  875. );
  876. $Article->set($data);
  877. $this->assertFalse($Article->validates());
  878. }
  879. }