ModelValidationTest.php 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  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 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/2.0/en/development/testing.html 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($expected, $TestModel->validatorParams);
  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($expected, $TestModel->invalidFields());
  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($expected, $TestModel->validatorParams);
  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($expected, $TestModel->validatorParams);
  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($expected, $TestModel->validationErrors);
  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($expected, $TestModel->validationErrors);
  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($expected, $TestModel->validationErrors);
  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($expected, $TestModel->validationErrors);
  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($expected, $TestModel->validationErrors);
  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($data, $result);
  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($data, $result);
  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($data, $result);
  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($data, $result);
  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($data, $result);
  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($data, $result);
  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($data, $result);
  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($data, $result);
  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($data, $result);
  275. $result = $TestModel->validates();
  276. $this->assertFalse($result);
  277. $data = array('TestValidate' => array('modified' => false));
  278. $result = $TestModel->create($data);
  279. $this->assertEquals($data, $result);
  280. $result = $TestModel->validates();
  281. $this->assertFalse($result);
  282. $data = array('TestValidate' => array('modified' => ''));
  283. $result = $TestModel->create($data);
  284. $this->assertEquals($data, $result);
  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($data, $result);
  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($data, $result);
  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($data, $result);
  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($data, $result);
  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($data, $result);
  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($data, $result);
  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($data, $result);
  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($data, $result);
  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($data, $result);
  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($data, $result);
  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($data, $result);
  401. $result = $TestModel->validates();
  402. $this->assertFalse($result);
  403. $data = array('TestValidate' => array('title' => 'new title'));
  404. $result = $TestModel->create($data);
  405. $this->assertEquals($data, $result);
  406. $result = $TestModel->validates();
  407. $this->assertFalse($result);
  408. $data = array('TestValidate' => array('title' => 'title-new'));
  409. $result = $TestModel->create($data);
  410. $this->assertEquals($data, $result);
  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($data, $result);
  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($data, $result);
  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($data, $result);
  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($data, $result);
  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($expectedError, $JoinThing->validationErrors);
  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(0, $joinRecords, '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($expectedError, $JoinThing->validationErrors);
  561. $Something->create();
  562. $result = $Something->saveAll($data, array('validate' => 'first'));
  563. $this->assertFalse($result);
  564. $this->assertEquals($expectedError, $JoinThing->validationErrors);
  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(0, $joinRecords, '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(array(), $TestModel->validationErrors);
  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($expected, $TestModel->validationErrors);
  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($expected, $TestModel->validationErrors);
  682. }
  683. /**
  684. * Test validation message translation
  685. *
  686. * @return void
  687. */
  688. public function testValidationMessageTranslation() {
  689. $lang = Configure::read('Config.language');
  690. Configure::write('Config.language', 'en');
  691. App::build(array(
  692. 'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS),
  693. ), App::RESET);
  694. $TestModel = new ValidationTest1();
  695. $TestModel->validationDomain = 'validation_messages';
  696. $TestModel->validate = array(
  697. 'title' => array(
  698. array(
  699. 'rule' => array('customValidationMethod', 'arg1'),
  700. 'required' => true,
  701. 'message' => 'Validation failed: %s'
  702. )
  703. )
  704. );
  705. $TestModel->create();
  706. $TestModel->invalidFields();
  707. $expected = array(
  708. 'title' => array(
  709. 'Translated validation failed: Translated arg1',
  710. )
  711. );
  712. $this->assertEquals($expected, $TestModel->validationErrors);
  713. $TestModel->validationDomain = 'default';
  714. Configure::write('Config.language', $lang);
  715. App::build();
  716. }
  717. /**
  718. * Test for 'on' => [create|update] in validation rules.
  719. *
  720. * @return void
  721. */
  722. public function testStateValidation() {
  723. $this->loadFixtures('Article');
  724. $Article = new Article();
  725. $data = array(
  726. 'Article' => array(
  727. 'title' => '',
  728. 'body' => 'Extra Fields Body',
  729. 'published' => '1'
  730. )
  731. );
  732. $Article->validate = array(
  733. 'title' => array(
  734. 'notempty' => array(
  735. 'rule' => 'notEmpty',
  736. 'on' => 'create'
  737. )
  738. )
  739. );
  740. $Article->create($data);
  741. $this->assertFalse($Article->validates());
  742. $Article->save(null, array('validate' => false));
  743. $data['Article']['id'] = $Article->id;
  744. $Article->set($data);
  745. $this->assertTrue($Article->validates());
  746. unset($data['Article']['id']);
  747. $Article->validate = array(
  748. 'title' => array(
  749. 'notempty' => array(
  750. 'rule' => 'notEmpty',
  751. 'on' => 'update'
  752. )
  753. )
  754. );
  755. $Article->create($data);
  756. $this->assertTrue($Article->validates());
  757. $Article->save(null, array('validate' => false));
  758. $data['Article']['id'] = $Article->id;
  759. $Article->set($data);
  760. $this->assertFalse($Article->validates());
  761. }
  762. /**
  763. * Test for 'required' => [create|update] in validation rules.
  764. *
  765. * @return void
  766. */
  767. public function testStateRequiredValidation() {
  768. $this->loadFixtures('Article');
  769. $Article = new Article();
  770. // no title field present
  771. $data = array(
  772. 'Article' => array(
  773. 'body' => 'Extra Fields Body',
  774. 'published' => '1'
  775. )
  776. );
  777. $Article->validate = array(
  778. 'title' => array(
  779. 'notempty' => array(
  780. 'rule' => 'notEmpty',
  781. 'required' => 'create'
  782. )
  783. )
  784. );
  785. $Article->create($data);
  786. $this->assertFalse($Article->validates());
  787. $Article->save(null, array('validate' => false));
  788. $data['Article']['id'] = $Article->id;
  789. $Article->set($data);
  790. $this->assertTrue($Article->validates());
  791. unset($data['Article']['id']);
  792. $Article->validate = array(
  793. 'title' => array(
  794. 'notempty' => array(
  795. 'rule' => 'notEmpty',
  796. 'required' => 'update'
  797. )
  798. )
  799. );
  800. $Article->create($data);
  801. $this->assertTrue($Article->validates());
  802. $Article->save(null, array('validate' => false));
  803. $data['Article']['id'] = $Article->id;
  804. $Article->set($data);
  805. $this->assertFalse($Article->validates());
  806. }
  807. /**
  808. * Test that 'required' and 'on' are not conflicting
  809. *
  810. * @return void
  811. */
  812. public function testOnRequiredConflictValidation() {
  813. $this->loadFixtures('Article');
  814. $Article = new Article();
  815. // no title field present
  816. $data = array(
  817. 'Article' => array(
  818. 'body' => 'Extra Fields Body',
  819. 'published' => '1'
  820. )
  821. );
  822. $Article->validate = array(
  823. 'title' => array(
  824. 'notempty' => array(
  825. 'rule' => 'notEmpty',
  826. 'required' => 'create',
  827. 'on' => 'create'
  828. )
  829. )
  830. );
  831. $Article->create($data);
  832. $this->assertFalse($Article->validates());
  833. $Article->validate = array(
  834. 'title' => array(
  835. 'notempty' => array(
  836. 'rule' => 'notEmpty',
  837. 'required' => 'update',
  838. 'on' => 'create'
  839. )
  840. )
  841. );
  842. $Article->create($data);
  843. $this->assertTrue($Article->validates());
  844. $Article->validate = array(
  845. 'title' => array(
  846. 'notempty' => array(
  847. 'rule' => 'notEmpty',
  848. 'required' => 'create',
  849. 'on' => 'update'
  850. )
  851. )
  852. );
  853. $Article->create($data);
  854. $this->assertTrue($Article->validates());
  855. $Article->validate = array(
  856. 'title' => array(
  857. 'notempty' => array(
  858. 'rule' => 'notEmpty',
  859. 'required' => 'update',
  860. 'on' => 'update'
  861. )
  862. )
  863. );
  864. $Article->create($data);
  865. $this->assertTrue($Article->validates());
  866. $Article->validate = array(
  867. 'title' => array(
  868. 'notempty' => array(
  869. 'rule' => 'notEmpty',
  870. 'required' => 'create',
  871. 'on' => 'create'
  872. )
  873. )
  874. );
  875. $Article->save(null, array('validate' => false));
  876. $data['Article']['id'] = $Article->id;
  877. $Article->set($data);
  878. $this->assertTrue($Article->validates());
  879. $Article->validate = array(
  880. 'title' => array(
  881. 'notempty' => array(
  882. 'rule' => 'notEmpty',
  883. 'required' => 'update',
  884. 'on' => 'create'
  885. )
  886. )
  887. );
  888. $Article->set($data);
  889. $this->assertTrue($Article->validates());
  890. $Article->validate = array(
  891. 'title' => array(
  892. 'notempty' => array(
  893. 'rule' => 'notEmpty',
  894. 'required' => 'create',
  895. 'on' => 'update'
  896. )
  897. )
  898. );
  899. $Article->set($data);
  900. $this->assertTrue($Article->validates());
  901. $Article->validate = array(
  902. 'title' => array(
  903. 'notempty' => array(
  904. 'rule' => 'notEmpty',
  905. 'required' => 'update',
  906. 'on' => 'update'
  907. )
  908. )
  909. );
  910. $Article->set($data);
  911. $this->assertFalse($Article->validates());
  912. }
  913. /**
  914. * Tests that altering data in a beforeValidate callback will lead to saving those
  915. * values in database
  916. *
  917. * @return void
  918. */
  919. public function testValidateFirstWithBeforeValidate() {
  920. $this->loadFixtures('Article', 'User');
  921. $model = new CustomArticle();
  922. $model->validate = array(
  923. 'title' => array(
  924. 'notempty' => array(
  925. 'rule' => 'notEmpty',
  926. 'required' => true,
  927. 'allowEmpty' => false
  928. )
  929. )
  930. );
  931. $data = array(
  932. 'CustomArticle' => array(
  933. 'body' => 'foo0'
  934. )
  935. );
  936. $result = $model->saveAll($data, array('validate' => 'first'));
  937. $this->assertTrue($result);
  938. $this->assertFalse($model->findMethods['unPublished'], 'beforeValidate was run twice');
  939. $model->findMethods['unPublished'] = true;
  940. $data = array(
  941. 'CustomArticle' => array(
  942. 'body' => 'foo1'
  943. )
  944. );
  945. $result = $model->saveAll($data, array('validate' => 'first', 'deep' => true));
  946. $this->assertTrue($result);
  947. $title = $model->field('title', array('body' => 'foo1'));
  948. $this->assertEquals('foo', $title);
  949. $this->assertFalse($model->findMethods['unPublished'], 'beforeValidate was run twice');
  950. $data = array(
  951. array('body' => 'foo2'),
  952. array('body' => 'foo3'),
  953. array('body' => 'foo4')
  954. );
  955. $result = $model->saveAll($data, array('validate' => 'first'));
  956. $this->assertTrue($result);
  957. $result = $model->saveAll($data, array('validate' => 'first', 'deep' => true));
  958. $this->assertTrue($result);
  959. $this->assertEquals('foo', $model->field('title', array('body' => 'foo2')));
  960. $this->assertEquals('foo', $model->field('title', array('body' => 'foo3')));
  961. $this->assertEquals('foo', $model->field('title', array('body' => 'foo4')));
  962. }
  963. /**
  964. * Tests that altering data in a beforeValidate callback will lead to saving those
  965. * values in database
  966. *
  967. * @return void
  968. */
  969. public function testValidateFirstAssociatedWithBeforeValidate() {
  970. $this->loadFixtures('Article', 'User');
  971. $model = new CustomArticle();
  972. $model->validate = array(
  973. 'title' => array(
  974. 'notempty' => array(
  975. 'rule' => 'notEmpty',
  976. 'required' => true
  977. )
  978. )
  979. );
  980. $articles = array(
  981. array('body' => 'foo1'),
  982. array('body' => 'foo2'),
  983. array('body' => 'foo3')
  984. );
  985. $user = new User();
  986. $user->hasMany['CustomArticle'] = array('foreignKey' => 'user_id');
  987. $data = array(
  988. 'User' => array('user' => 'foo', 'password' => 'bar'),
  989. 'CustomArticle' => $articles
  990. );
  991. $result = $user->saveAll($data, array('validate' => 'first'));
  992. $this->assertTrue($result);
  993. $this->assertEquals('foo', $model->field('title', array('body' => 'foo1')));
  994. $this->assertEquals('foo', $model->field('title', array('body' => 'foo2')));
  995. $this->assertEquals('foo', $model->field('title', array('body' => 'foo3')));
  996. }
  997. /**
  998. * testValidateFirstWithDefaults method
  999. *
  1000. * return @void
  1001. */
  1002. public function testFirstWithDefaults() {
  1003. $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  1004. $TestModel = new Article();
  1005. $result = $TestModel->find('first', array(
  1006. 'conditions' => array('Article.id' => 1)
  1007. ));
  1008. $expected = array(
  1009. 'Article' => array(
  1010. 'id' => 1,
  1011. 'user_id' => 1,
  1012. 'title' => 'First Article',
  1013. 'body' => 'First Article Body',
  1014. 'published' => 'Y',
  1015. 'created' => '2007-03-18 10:39:23'
  1016. ),
  1017. );
  1018. unset($result['Article']['updated']);
  1019. $this->assertEquals($expected['Article'], $result['Article']);
  1020. $data = array(
  1021. 'Article' => array(
  1022. 'id' => 1,
  1023. 'title' => 'First Article (modified)'
  1024. ),
  1025. 'Comment' => array(
  1026. array('comment' => 'Article comment', 'user_id' => 1)
  1027. )
  1028. );
  1029. $result = $TestModel->saveAll($data, array('validate' => 'first'));
  1030. $this->assertTrue($result);
  1031. $result = $TestModel->find('first', array(
  1032. 'conditions' => array('Article.id' => 1)
  1033. ));
  1034. $expected['Article']['title'] = 'First Article (modified)';
  1035. unset($result['Article']['updated']);
  1036. $this->assertEquals($expected['Article'], $result['Article']);
  1037. }
  1038. }