ModelValidationTest.php 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426
  1. <?php
  2. /**
  3. * ModelValidationTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @package Cake.Test.Case.Model
  15. * @since CakePHP(tm) v 1.2.0.4206
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
  19. /**
  20. * ModelValidationTest
  21. *
  22. * @package Cake.Test.Case.Model
  23. */
  24. class ModelValidationTest extends BaseModelTest {
  25. /**
  26. * override locale to the default (eng).
  27. *
  28. * @return void
  29. */
  30. public function setUp() {
  31. parent::setUp();
  32. Configure::write('Config.language', 'eng');
  33. }
  34. /**
  35. * Tests validation parameter order in custom validation methods
  36. *
  37. * @return void
  38. */
  39. public function testValidationParams() {
  40. $TestModel = new ValidationTest1();
  41. $TestModel->validate['title'] = array(
  42. 'rule' => 'customValidatorWithParams',
  43. 'required' => true
  44. );
  45. $TestModel->create(array('title' => 'foo'));
  46. $TestModel->invalidFields();
  47. $expected = array(
  48. 'data' => array(
  49. 'title' => 'foo'
  50. ),
  51. 'validator' => array(
  52. 'rule' => 'customValidatorWithParams',
  53. 'on' => null,
  54. 'last' => true,
  55. 'allowEmpty' => false,
  56. 'required' => true,
  57. 'message' => null
  58. ),
  59. 'or' => true,
  60. 'ignoreOnSame' => 'id'
  61. );
  62. $this->assertEquals($expected, $TestModel->validatorParams);
  63. $TestModel->validate['title'] = array(
  64. 'rule' => 'customValidatorWithMessage',
  65. 'required' => true
  66. );
  67. $expected = array(
  68. 'title' => array('This field will *never* validate! Muhahaha!')
  69. );
  70. $this->assertEquals($expected, $TestModel->invalidFields());
  71. $TestModel->validate['title'] = array(
  72. 'rule' => array('customValidatorWithSixParams', 'one', 'two', null, 'four'),
  73. 'required' => true
  74. );
  75. $TestModel->create(array('title' => 'foo'));
  76. $TestModel->invalidFields();
  77. $expected = array(
  78. 'data' => array(
  79. 'title' => 'foo'
  80. ),
  81. 'one' => 'one',
  82. 'two' => 'two',
  83. 'three' => null,
  84. 'four' => 'four',
  85. 'five' => array(
  86. 'rule' => array(1 => 'one', 2 => 'two', 3 => null, 4 => 'four'),
  87. 'on' => null,
  88. 'last' => true,
  89. 'allowEmpty' => false,
  90. 'required' => true,
  91. 'message' => null
  92. ),
  93. 'six' => 6
  94. );
  95. $this->assertEquals($expected, $TestModel->validatorParams);
  96. $TestModel->validate['title'] = array(
  97. 'rule' => array('customValidatorWithSixParams', 'one', array('two'), null, 'four', array('five' => 5)),
  98. 'required' => true
  99. );
  100. $TestModel->create(array('title' => 'foo'));
  101. $TestModel->invalidFields();
  102. $expected = array(
  103. 'data' => array(
  104. 'title' => 'foo'
  105. ),
  106. 'one' => 'one',
  107. 'two' => array('two'),
  108. 'three' => null,
  109. 'four' => 'four',
  110. 'five' => array('five' => 5),
  111. 'six' => array(
  112. 'rule' => array(1 => 'one', 2 => array('two'), 3 => null, 4 => 'four', 5 => array('five' => 5)),
  113. 'on' => null,
  114. 'last' => true,
  115. 'allowEmpty' => false,
  116. 'required' => true,
  117. 'message' => null
  118. )
  119. );
  120. $this->assertEquals($expected, $TestModel->validatorParams);
  121. }
  122. /**
  123. * Tests validation parameter fieldList in invalidFields
  124. *
  125. * @return void
  126. */
  127. public function testInvalidFieldsWithFieldListParams() {
  128. $TestModel = new ValidationTest1();
  129. $TestModel->validate = $validate = array(
  130. 'title' => array(
  131. 'rule' => 'alphaNumeric',
  132. 'required' => true
  133. ),
  134. 'name' => array(
  135. 'rule' => 'alphaNumeric',
  136. 'required' => true
  137. ));
  138. $TestModel->set(array('title' => '$$', 'name' => '##'));
  139. $TestModel->invalidFields(array('fieldList' => array('title')));
  140. $expected = array(
  141. 'title' => array('This field cannot be left blank')
  142. );
  143. $this->assertEquals($expected, $TestModel->validationErrors);
  144. $TestModel->validationErrors = array();
  145. $TestModel->invalidFields(array('fieldList' => array('name')));
  146. $expected = array(
  147. 'name' => array('This field cannot be left blank')
  148. );
  149. $this->assertEquals($expected, $TestModel->validationErrors);
  150. $TestModel->validationErrors = array();
  151. $TestModel->invalidFields(array('fieldList' => array('name', 'title')));
  152. $expected = array(
  153. 'name' => array('This field cannot be left blank'),
  154. 'title' => array('This field cannot be left blank')
  155. );
  156. $this->assertEquals($expected, $TestModel->validationErrors);
  157. $TestModel->validationErrors = array();
  158. $TestModel->whitelist = array('name');
  159. $TestModel->invalidFields();
  160. $expected = array('name' => array('This field cannot be left blank'));
  161. $this->assertEquals($expected, $TestModel->validationErrors);
  162. $this->assertEquals($TestModel->validate, $validate);
  163. }
  164. /**
  165. * Test that invalidFields() integrates well with save(). And that fieldList can be an empty type.
  166. *
  167. * @return void
  168. */
  169. public function testInvalidFieldsWhitelist() {
  170. $TestModel = new ValidationTest1();
  171. $TestModel->validate = array(
  172. 'title' => array(
  173. 'rule' => 'alphaNumeric',
  174. 'required' => true
  175. ),
  176. 'name' => array(
  177. 'rule' => 'alphaNumeric',
  178. 'required' => true
  179. ));
  180. $TestModel->whitelist = array('name');
  181. $TestModel->save(array('name' => '#$$#', 'title' => '$$$$'));
  182. $expected = array('name' => array('This field cannot be left blank'));
  183. $this->assertEquals($expected, $TestModel->validationErrors);
  184. }
  185. /**
  186. * testValidates method
  187. *
  188. * @return void
  189. */
  190. public function testValidates() {
  191. $TestModel = new TestValidate();
  192. $TestModel->validate = array(
  193. 'user_id' => 'numeric',
  194. 'title' => array('allowEmpty' => false, 'rule' => 'notEmpty'),
  195. 'body' => 'notEmpty'
  196. );
  197. $data = array('TestValidate' => array(
  198. 'user_id' => '1',
  199. 'title' => '',
  200. 'body' => 'body'
  201. ));
  202. $result = $TestModel->create($data);
  203. $this->assertEquals($data, $result);
  204. $result = $TestModel->validates();
  205. $this->assertFalse($result);
  206. $data = array('TestValidate' => array(
  207. 'user_id' => '1',
  208. 'title' => 'title',
  209. 'body' => 'body'
  210. ));
  211. $result = $TestModel->create($data) && $TestModel->validates();
  212. $this->assertTrue($result);
  213. $data = array('TestValidate' => array(
  214. 'user_id' => '1',
  215. 'title' => '0',
  216. 'body' => 'body'
  217. ));
  218. $result = $TestModel->create($data);
  219. $this->assertEquals($data, $result);
  220. $result = $TestModel->validates();
  221. $this->assertTrue($result);
  222. $data = array('TestValidate' => array(
  223. 'user_id' => '1',
  224. 'title' => 0,
  225. 'body' => 'body'
  226. ));
  227. $result = $TestModel->create($data);
  228. $this->assertEquals($data, $result);
  229. $result = $TestModel->validates();
  230. $this->assertTrue($result);
  231. $TestModel->validate['modified'] = array('allowEmpty' => true, 'rule' => 'date');
  232. $data = array('TestValidate' => array(
  233. 'user_id' => '1',
  234. 'title' => 0,
  235. 'body' => 'body',
  236. 'modified' => ''
  237. ));
  238. $result = $TestModel->create($data);
  239. $this->assertEquals($data, $result);
  240. $result = $TestModel->validates();
  241. $this->assertTrue($result);
  242. $data = array('TestValidate' => array(
  243. 'user_id' => '1',
  244. 'title' => 0,
  245. 'body' => 'body',
  246. 'modified' => '2007-05-01'
  247. ));
  248. $result = $TestModel->create($data);
  249. $this->assertEquals($data, $result);
  250. $result = $TestModel->validates();
  251. $this->assertTrue($result);
  252. $data = array('TestValidate' => array(
  253. 'user_id' => '1',
  254. 'title' => 0,
  255. 'body' => 'body',
  256. 'modified' => 'invalid-date-here'
  257. ));
  258. $result = $TestModel->create($data);
  259. $this->assertEquals($data, $result);
  260. $result = $TestModel->validates();
  261. $this->assertFalse($result);
  262. $data = array('TestValidate' => array(
  263. 'user_id' => '1',
  264. 'title' => 0,
  265. 'body' => 'body',
  266. 'modified' => 0
  267. ));
  268. $result = $TestModel->create($data);
  269. $this->assertEquals($data, $result);
  270. $result = $TestModel->validates();
  271. $this->assertFalse($result);
  272. $data = array('TestValidate' => array(
  273. 'user_id' => '1',
  274. 'title' => 0,
  275. 'body' => 'body',
  276. 'modified' => '0'
  277. ));
  278. $result = $TestModel->create($data);
  279. $this->assertEquals($data, $result);
  280. $result = $TestModel->validates();
  281. $this->assertFalse($result);
  282. $TestModel->validate['modified'] = array('allowEmpty' => false, 'rule' => 'date');
  283. $data = array('TestValidate' => array('modified' => null));
  284. $result = $TestModel->create($data);
  285. $this->assertEquals($data, $result);
  286. $result = $TestModel->validates();
  287. $this->assertFalse($result);
  288. $data = array('TestValidate' => array('modified' => false));
  289. $result = $TestModel->create($data);
  290. $this->assertEquals($data, $result);
  291. $result = $TestModel->validates();
  292. $this->assertFalse($result);
  293. $data = array('TestValidate' => array('modified' => ''));
  294. $result = $TestModel->create($data);
  295. $this->assertEquals($data, $result);
  296. $result = $TestModel->validates();
  297. $this->assertFalse($result);
  298. $data = array('TestValidate' => array(
  299. 'modified' => '2007-05-01'
  300. ));
  301. $result = $TestModel->create($data);
  302. $this->assertEquals($data, $result);
  303. $result = $TestModel->validates();
  304. $this->assertTrue($result);
  305. $TestModel->validate['slug'] = array('allowEmpty' => false, 'rule' => array('maxLength', 45));
  306. $data = array('TestValidate' => array(
  307. 'user_id' => '1',
  308. 'title' => 0,
  309. 'body' => 'body',
  310. 'slug' => ''
  311. ));
  312. $result = $TestModel->create($data);
  313. $this->assertEquals($data, $result);
  314. $result = $TestModel->validates();
  315. $this->assertFalse($result);
  316. $data = array('TestValidate' => array(
  317. 'user_id' => '1',
  318. 'title' => 0,
  319. 'body' => 'body',
  320. 'slug' => 'slug-right-here'
  321. ));
  322. $result = $TestModel->create($data);
  323. $this->assertEquals($data, $result);
  324. $result = $TestModel->validates();
  325. $this->assertTrue($result);
  326. $data = array('TestValidate' => array(
  327. 'user_id' => '1',
  328. 'title' => 0,
  329. 'body' => 'body',
  330. 'slug' => 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'
  331. ));
  332. $result = $TestModel->create($data);
  333. $this->assertEquals($data, $result);
  334. $result = $TestModel->validates();
  335. $this->assertFalse($result);
  336. $TestModel->validate = array(
  337. 'number' => array(
  338. 'rule' => 'validateNumber',
  339. 'min' => 3,
  340. 'max' => 5
  341. ),
  342. 'title' => array(
  343. 'allowEmpty' => false,
  344. 'rule' => 'notEmpty'
  345. ));
  346. $data = array('TestValidate' => array(
  347. 'title' => 'title',
  348. 'number' => '0'
  349. ));
  350. $result = $TestModel->create($data);
  351. $this->assertEquals($data, $result);
  352. $result = $TestModel->validates();
  353. $this->assertFalse($result);
  354. $data = array('TestValidate' => array(
  355. 'title' => 'title',
  356. 'number' => 0
  357. ));
  358. $result = $TestModel->create($data);
  359. $this->assertEquals($data, $result);
  360. $result = $TestModel->validates();
  361. $this->assertFalse($result);
  362. $data = array('TestValidate' => array(
  363. 'title' => 'title',
  364. 'number' => '3'
  365. ));
  366. $result = $TestModel->create($data);
  367. $this->assertEquals($data, $result);
  368. $result = $TestModel->validates();
  369. $this->assertTrue($result);
  370. $data = array('TestValidate' => array(
  371. 'title' => 'title',
  372. 'number' => 3
  373. ));
  374. $result = $TestModel->create($data);
  375. $this->assertEquals($data, $result);
  376. $result = $TestModel->validates();
  377. $this->assertTrue($result);
  378. $TestModel->validate = array(
  379. 'number' => array(
  380. 'rule' => 'validateNumber',
  381. 'min' => 5,
  382. 'max' => 10
  383. ),
  384. 'title' => array(
  385. 'allowEmpty' => false,
  386. 'rule' => 'notEmpty'
  387. ));
  388. $data = array('TestValidate' => array(
  389. 'title' => 'title',
  390. 'number' => '3'
  391. ));
  392. $result = $TestModel->create($data);
  393. $this->assertEquals($data, $result);
  394. $result = $TestModel->validates();
  395. $this->assertFalse($result);
  396. $data = array('TestValidate' => array(
  397. 'title' => 'title',
  398. 'number' => 3
  399. ));
  400. $result = $TestModel->create($data);
  401. $this->assertEquals($data, $result);
  402. $result = $TestModel->validates();
  403. $this->assertFalse($result);
  404. $TestModel->validate = array(
  405. 'title' => array(
  406. 'allowEmpty' => false,
  407. 'rule' => 'validateTitle'
  408. ));
  409. $data = array('TestValidate' => array('title' => ''));
  410. $result = $TestModel->create($data);
  411. $this->assertEquals($data, $result);
  412. $result = $TestModel->validates();
  413. $this->assertFalse($result);
  414. $data = array('TestValidate' => array('title' => 'new title'));
  415. $result = $TestModel->create($data);
  416. $this->assertEquals($data, $result);
  417. $result = $TestModel->validates();
  418. $this->assertFalse($result);
  419. $data = array('TestValidate' => array('title' => 'title-new'));
  420. $result = $TestModel->create($data);
  421. $this->assertEquals($data, $result);
  422. $result = $TestModel->validates();
  423. $this->assertTrue($result);
  424. $TestModel->validate = array('title' => array(
  425. 'allowEmpty' => true,
  426. 'rule' => 'validateTitle'
  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. 'length' => array(
  436. 'allowEmpty' => true,
  437. 'rule' => array('maxLength', 10)
  438. )));
  439. $data = array('TestValidate' => array('title' => ''));
  440. $result = $TestModel->create($data);
  441. $this->assertEquals($data, $result);
  442. $result = $TestModel->validates();
  443. $this->assertTrue($result);
  444. $TestModel->validate = array(
  445. 'title' => array(
  446. 'rule' => array('userDefined', 'Article', 'titleDuplicate')
  447. ));
  448. $data = array('TestValidate' => array('title' => 'My Article Title'));
  449. $result = $TestModel->create($data);
  450. $this->assertEquals($data, $result);
  451. $result = $TestModel->validates();
  452. $this->assertFalse($result);
  453. $data = array('TestValidate' => array(
  454. 'title' => 'My Article With a Different Title'
  455. ));
  456. $result = $TestModel->create($data);
  457. $this->assertEquals($data, $result);
  458. $result = $TestModel->validates();
  459. $this->assertTrue($result);
  460. $TestModel->validate = array(
  461. 'title' => array(
  462. 'tooShort' => array('rule' => array('minLength', 50)),
  463. 'onlyLetters' => array('rule' => '/^[a-z]+$/i')
  464. ),
  465. );
  466. $data = array('TestValidate' => array(
  467. 'title' => 'I am a short string'
  468. ));
  469. $TestModel->create($data);
  470. $result = $TestModel->validates();
  471. $this->assertFalse($result);
  472. $result = $TestModel->validationErrors;
  473. $expected = array(
  474. 'title' => array('tooShort')
  475. );
  476. $this->assertEquals($expected, $result);
  477. $TestModel->validate = array(
  478. 'title' => array(
  479. 'tooShort' => array(
  480. 'rule' => array('minLength', 50),
  481. 'last' => false
  482. ),
  483. 'onlyLetters' => array('rule' => '/^[a-z]+$/i')
  484. ),
  485. );
  486. $data = array('TestValidate' => array(
  487. 'title' => 'I am a short string'
  488. ));
  489. $TestModel->create($data);
  490. $result = $TestModel->validates();
  491. $this->assertFalse($result);
  492. $result = $TestModel->validationErrors;
  493. $expected = array(
  494. 'title' => array('tooShort', 'onlyLetters')
  495. );
  496. $this->assertEquals($expected, $result);
  497. $result = $TestModel->validationErrors;
  498. $this->assertEquals($expected, $result);
  499. }
  500. /**
  501. * test that validates() checks all the 'with' associations as well for validation
  502. * as this can cause partial/wrong data insertion.
  503. *
  504. * @return void
  505. */
  506. public function testValidatesWithAssociations() {
  507. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  508. $data = array(
  509. 'Something' => array(
  510. 'id' => 5,
  511. 'title' => 'Extra Fields',
  512. 'body' => 'Extra Fields Body',
  513. 'published' => '1'
  514. ),
  515. 'SomethingElse' => array(
  516. array('something_else_id' => 1, 'doomed' => '')
  517. )
  518. );
  519. $Something = new Something();
  520. $JoinThing = $Something->JoinThing;
  521. $JoinThing->validate = array('doomed' => array('rule' => 'notEmpty'));
  522. $expectedError = array('doomed' => array('This field cannot be left blank'));
  523. $Something->create();
  524. $result = $Something->save($data);
  525. $this->assertFalse($result, 'Save occurred even when with models failed. %s');
  526. $this->assertEquals($expectedError, $JoinThing->validationErrors);
  527. $count = $Something->find('count', array('conditions' => array('Something.id' => $data['Something']['id'])));
  528. $this->assertSame(0, $count);
  529. $data = array(
  530. 'Something' => array(
  531. 'id' => 5,
  532. 'title' => 'Extra Fields',
  533. 'body' => 'Extra Fields Body',
  534. 'published' => '1'
  535. ),
  536. 'SomethingElse' => array(
  537. array('something_else_id' => 1, 'doomed' => 1),
  538. array('something_else_id' => 1, 'doomed' => '')
  539. )
  540. );
  541. $Something->create();
  542. $result = $Something->save($data);
  543. $this->assertFalse($result, 'Save occurred even when with models failed. %s');
  544. $joinRecords = $JoinThing->find('count', array(
  545. 'conditions' => array('JoinThing.something_id' => $data['Something']['id'])
  546. ));
  547. $this->assertEquals(0, $joinRecords, 'Records were saved on the join table. %s');
  548. }
  549. /**
  550. * Test that if a behavior modifies the model's whitelist validation gets triggered
  551. * properly for those fields.
  552. *
  553. * @return void
  554. */
  555. public function testValidateWithFieldListAndBehavior() {
  556. $TestModel = new ValidationTest1();
  557. $TestModel->validate = array(
  558. 'title' => array(
  559. 'rule' => 'notEmpty',
  560. ),
  561. 'name' => array(
  562. 'rule' => 'notEmpty',
  563. ));
  564. $TestModel->Behaviors->attach('ValidationRule', array('fields' => array('name')));
  565. $data = array(
  566. 'title' => '',
  567. 'name' => '',
  568. );
  569. $result = $TestModel->save($data, array('fieldList' => array('title')));
  570. $this->assertFalse($result);
  571. $expected = array('title' => array('This field cannot be left blank'), 'name' => array('This field cannot be left blank'));
  572. $this->assertEquals($expected, $TestModel->validationErrors);
  573. }
  574. /**
  575. * test that saveAll and with models with validation interact well
  576. *
  577. * @return void
  578. */
  579. public function testValidatesWithModelsAndSaveAll() {
  580. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  581. $data = array(
  582. 'Something' => array(
  583. 'id' => 5,
  584. 'title' => 'Extra Fields',
  585. 'body' => 'Extra Fields Body',
  586. 'published' => '1'
  587. ),
  588. 'SomethingElse' => array(
  589. array('something_else_id' => 1, 'doomed' => '')
  590. )
  591. );
  592. $Something = new Something();
  593. $JoinThing = $Something->JoinThing;
  594. $JoinThing->validate = array('doomed' => array('rule' => 'notEmpty'));
  595. $expectedError = array('doomed' => array('This field cannot be left blank'));
  596. $Something->create();
  597. $result = $Something->saveAll($data, array('validate' => 'only'));
  598. $this->assertFalse($result);
  599. $result = $Something->validateAssociated($data);
  600. $this->assertFalse($result);
  601. $this->assertEquals($expectedError, $JoinThing->validationErrors);
  602. $result = $Something->validator()->validateAssociated($data);
  603. $this->assertFalse($result);
  604. $Something->create();
  605. $result = $Something->saveAll($data, array('validate' => 'first'));
  606. $this->assertFalse($result);
  607. $this->assertEquals($expectedError, $JoinThing->validationErrors);
  608. $count = $Something->find('count', array('conditions' => array('Something.id' => $data['Something']['id'])));
  609. $this->assertSame(0, $count);
  610. $joinRecords = $JoinThing->find('count', array(
  611. 'conditions' => array('JoinThing.something_id' => $data['Something']['id'])
  612. ));
  613. $this->assertEquals(0, $joinRecords, 'Records were saved on the join table. %s');
  614. }
  615. /**
  616. * test that saveAll and with models at initial insert (no id has set yet)
  617. * with validation interact well
  618. *
  619. * @return void
  620. */
  621. public function testValidatesWithModelsAndSaveAllWithoutId() {
  622. $this->loadFixtures('Post', 'Author');
  623. $data = array(
  624. 'Author' => array(
  625. 'name' => 'Foo Bar',
  626. ),
  627. 'Post' => array(
  628. array('title' => 'Hello'),
  629. array('title' => 'World'),
  630. )
  631. );
  632. $Author = new Author();
  633. $Post = $Author->Post;
  634. $Post->validate = array('author_id' => array('rule' => 'numeric'));
  635. $Author->create();
  636. $result = $Author->saveAll($data, array('validate' => 'only'));
  637. $this->assertTrue($result);
  638. $result = $Author->validateAssociated($data);
  639. $this->assertTrue($result);
  640. $this->assertTrue($result);
  641. $Author->create();
  642. $result = $Author->saveAll($data, array('validate' => 'first'));
  643. $this->assertTrue($result);
  644. $this->assertNotNull($Author->id);
  645. $id = $Author->id;
  646. $count = $Author->find('count', array('conditions' => array('Author.id' => $id)));
  647. $this->assertSame(1, $count);
  648. $count = $Post->find('count', array(
  649. 'conditions' => array('Post.author_id' => $id)
  650. ));
  651. $this->assertEquals($count, count($data['Post']));
  652. }
  653. /**
  654. * Test that missing validation methods trigger errors in development mode.
  655. * Helps to make development easier.
  656. *
  657. * @expectedException PHPUnit_Framework_Error
  658. * @return void
  659. */
  660. public function testMissingValidationErrorTriggering() {
  661. Configure::write('debug', 2);
  662. $TestModel = new ValidationTest1();
  663. $TestModel->create(array('title' => 'foo'));
  664. $TestModel->validate = array(
  665. 'title' => array(
  666. 'rule' => array('thisOneBringsThePain'),
  667. 'required' => true
  668. )
  669. );
  670. $TestModel->invalidFields(array('fieldList' => array('title')));
  671. }
  672. /**
  673. * Test placeholder replacement when validation message is an array
  674. *
  675. * @return void
  676. */
  677. public function testValidationMessageAsArray() {
  678. $TestModel = new ValidationTest1();
  679. $TestModel->validate = array(
  680. 'title' => array(
  681. 'minLength' => array(
  682. 'rule' => array('minLength', 6),
  683. 'required' => true,
  684. 'message' => 'Minimum length allowed is %d chars',
  685. 'last' => false
  686. ),
  687. 'between' => array(
  688. 'rule' => array('between', 5, 15),
  689. 'message' => array('You may enter up to %s chars (minimum is %s chars)', 14, 6)
  690. )
  691. )
  692. );
  693. $TestModel->create();
  694. $expected = array(
  695. 'title' => array(
  696. 'Minimum length allowed is 6 chars',
  697. )
  698. );
  699. $TestModel->invalidFields();
  700. $this->assertEquals($expected, $TestModel->validationErrors);
  701. $TestModel->create(array('title' => 'foo'));
  702. $expected = array(
  703. 'title' => array(
  704. 'Minimum length allowed is 6 chars',
  705. 'You may enter up to 14 chars (minimum is 6 chars)'
  706. )
  707. );
  708. $TestModel->invalidFields();
  709. $this->assertEquals($expected, $TestModel->validationErrors);
  710. }
  711. /**
  712. * Test validation message translation
  713. *
  714. * @return void
  715. */
  716. public function testValidationMessageTranslation() {
  717. $lang = Configure::read('Config.language');
  718. Configure::write('Config.language', 'en');
  719. App::build(array(
  720. 'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS),
  721. ), App::RESET);
  722. $TestModel = new ValidationTest1();
  723. $TestModel->validationDomain = 'validation_messages';
  724. $TestModel->validate = array(
  725. 'title' => array(
  726. array(
  727. 'rule' => array('customValidationMethod', 'arg1'),
  728. 'required' => true,
  729. 'message' => 'Validation failed: %s'
  730. )
  731. )
  732. );
  733. $TestModel->create();
  734. $expected = array(
  735. 'title' => array(
  736. 'Translated validation failed: Translated arg1',
  737. )
  738. );
  739. $TestModel->invalidFields();
  740. $this->assertEquals($expected, $TestModel->validationErrors);
  741. $TestModel->validationDomain = 'default';
  742. Configure::write('Config.language', $lang);
  743. App::build();
  744. }
  745. /**
  746. * Test for 'on' => [create|update] in validation rules.
  747. *
  748. * @return void
  749. */
  750. public function testStateValidation() {
  751. $this->loadFixtures('Article');
  752. $Article = new Article();
  753. $data = array(
  754. 'Article' => array(
  755. 'title' => '',
  756. 'body' => 'Extra Fields Body',
  757. 'published' => '1'
  758. )
  759. );
  760. $Article->validate = array(
  761. 'title' => array(
  762. 'notempty' => array(
  763. 'rule' => 'notEmpty',
  764. 'on' => 'create'
  765. )
  766. )
  767. );
  768. $Article->create($data);
  769. $this->assertFalse($Article->validates());
  770. $Article->save(null, array('validate' => false));
  771. $data['Article']['id'] = $Article->id;
  772. $Article->set($data);
  773. $this->assertTrue($Article->validates());
  774. unset($data['Article']['id']);
  775. $Article->validate = array(
  776. 'title' => array(
  777. 'notempty' => array(
  778. 'rule' => 'notEmpty',
  779. 'on' => 'update'
  780. )
  781. )
  782. );
  783. $Article->create($data);
  784. $this->assertTrue($Article->validates());
  785. $Article->save(null, array('validate' => false));
  786. $data['Article']['id'] = $Article->id;
  787. $Article->set($data);
  788. $this->assertFalse($Article->validates());
  789. }
  790. /**
  791. * Test for 'required' => [create|update] in validation rules.
  792. *
  793. * @return void
  794. */
  795. public function testStateRequiredValidation() {
  796. $this->loadFixtures('Article');
  797. $Article = new Article();
  798. // no title field present
  799. $data = array(
  800. 'Article' => array(
  801. 'body' => 'Extra Fields Body',
  802. 'published' => '1'
  803. )
  804. );
  805. $Article->validate = array(
  806. 'title' => array(
  807. 'notempty' => array(
  808. 'rule' => 'notEmpty',
  809. 'required' => 'create'
  810. )
  811. )
  812. );
  813. $Article->create($data);
  814. $this->assertFalse($Article->validates());
  815. $Article->save(null, array('validate' => false));
  816. $data['Article']['id'] = $Article->id;
  817. $Article->set($data);
  818. $this->assertTrue($Article->validates());
  819. unset($data['Article']['id']);
  820. $Article->validate = array(
  821. 'title' => array(
  822. 'notempty' => array(
  823. 'rule' => 'notEmpty',
  824. 'required' => 'update'
  825. )
  826. )
  827. );
  828. $Article->create($data);
  829. $this->assertTrue($Article->validates());
  830. $Article->save(null, array('validate' => false));
  831. $data['Article']['id'] = $Article->id;
  832. $Article->set($data);
  833. $this->assertFalse($Article->validates());
  834. }
  835. /**
  836. * Test that 'required' and 'on' are not conflicting
  837. *
  838. * @return void
  839. */
  840. public function testOnRequiredConflictValidation() {
  841. $this->loadFixtures('Article');
  842. $Article = new Article();
  843. // no title field present
  844. $data = array(
  845. 'Article' => array(
  846. 'body' => 'Extra Fields Body',
  847. 'published' => '1'
  848. )
  849. );
  850. $Article->validate = array(
  851. 'title' => array(
  852. 'notempty' => array(
  853. 'rule' => 'notEmpty',
  854. 'required' => 'create',
  855. 'on' => 'create'
  856. )
  857. )
  858. );
  859. $Article->create($data);
  860. $this->assertFalse($Article->validates());
  861. $Article->validate = array(
  862. 'title' => array(
  863. 'notempty' => array(
  864. 'rule' => 'notEmpty',
  865. 'required' => 'update',
  866. 'on' => 'create'
  867. )
  868. )
  869. );
  870. $Article->create($data);
  871. $this->assertTrue($Article->validates());
  872. $Article->validate = array(
  873. 'title' => array(
  874. 'notempty' => array(
  875. 'rule' => 'notEmpty',
  876. 'required' => 'create',
  877. 'on' => 'update'
  878. )
  879. )
  880. );
  881. $Article->create($data);
  882. $this->assertTrue($Article->validates());
  883. $Article->validate = array(
  884. 'title' => array(
  885. 'notempty' => array(
  886. 'rule' => 'notEmpty',
  887. 'required' => 'update',
  888. 'on' => 'update'
  889. )
  890. )
  891. );
  892. $Article->create($data);
  893. $this->assertTrue($Article->validates());
  894. $Article->validate = array(
  895. 'title' => array(
  896. 'notempty' => array(
  897. 'rule' => 'notEmpty',
  898. 'required' => 'create',
  899. 'on' => 'create'
  900. )
  901. )
  902. );
  903. $Article->save(null, array('validate' => false));
  904. $data['Article']['id'] = $Article->id;
  905. $Article->set($data);
  906. $this->assertTrue($Article->validates());
  907. $Article->validate = array(
  908. 'title' => array(
  909. 'notempty' => array(
  910. 'rule' => 'notEmpty',
  911. 'required' => 'update',
  912. 'on' => 'create'
  913. )
  914. )
  915. );
  916. $Article->set($data);
  917. $this->assertTrue($Article->validates());
  918. $Article->validate = array(
  919. 'title' => array(
  920. 'notempty' => array(
  921. 'rule' => 'notEmpty',
  922. 'required' => 'create',
  923. 'on' => 'update'
  924. )
  925. )
  926. );
  927. $Article->set($data);
  928. $this->assertTrue($Article->validates());
  929. $Article->validate = array(
  930. 'title' => array(
  931. 'notempty' => array(
  932. 'rule' => 'notEmpty',
  933. 'required' => 'update',
  934. 'on' => 'update'
  935. )
  936. )
  937. );
  938. $Article->set($data);
  939. $this->assertFalse($Article->validates());
  940. }
  941. /**
  942. * testSaveAllDeepValidateOnly
  943. * tests the validate methods with deeper recursive data
  944. *
  945. * @return void
  946. */
  947. public function testSaveAllDeepValidateOnly() {
  948. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  949. $TestModel = new Article();
  950. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  951. $TestModel->hasAndBelongsToMany = array();
  952. $TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
  953. $TestModel->Comment->validate['comment'] = 'notEmpty';
  954. $data = array(
  955. 'Article' => array('id' => 2),
  956. 'Comment' => array(
  957. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
  958. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  959. )
  960. );
  961. $result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => true));
  962. $this->assertTrue($result);
  963. $result = $TestModel->validateAssociated($data, array('deep' => true));
  964. $this->assertTrue($result);
  965. $data = array(
  966. 'Article' => array('id' => 2),
  967. 'Comment' => array(
  968. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  969. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  970. )
  971. );
  972. $result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => true));
  973. $this->assertFalse($result);
  974. $result = $TestModel->validateAssociated($data, array('deep' => true));
  975. $this->assertFalse($result);
  976. $data = array(
  977. 'Article' => array('id' => 2),
  978. 'Comment' => array(
  979. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
  980. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  981. )
  982. );
  983. $expected = array(
  984. 'Article' => true,
  985. 'Comment' => array(
  986. true,
  987. true
  988. )
  989. );
  990. $result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  991. $this->assertSame($expected, $result);
  992. $result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => true));
  993. $this->assertSame($expected, $result);
  994. $data = array(
  995. 'Article' => array('id' => 2),
  996. 'Comment' => array(
  997. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  998. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  999. )
  1000. );
  1001. $expected = array(
  1002. 'Article' => true,
  1003. 'Comment' => array(
  1004. false,
  1005. true
  1006. )
  1007. );
  1008. $result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  1009. $this->assertSame($expected, $result);
  1010. $result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => true));
  1011. $this->assertSame($expected, $result);
  1012. $data = array(
  1013. 'Article' => array('id' => 2),
  1014. 'Comment' => array(
  1015. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  1016. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
  1017. )
  1018. );
  1019. $result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => true));
  1020. $this->assertTrue($result);
  1021. $result = $TestModel->validateAssociated($data, array('deep' => true));
  1022. $this->assertTrue($result);
  1023. $data = array(
  1024. 'Article' => array('id' => 2),
  1025. 'Comment' => array(
  1026. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  1027. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  1028. )
  1029. );
  1030. $result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => true));
  1031. $this->assertFalse($result);
  1032. $result = $TestModel->validateAssociated($data, array('deep' => true));
  1033. $this->assertFalse($result);
  1034. $data = array(
  1035. 'Article' => array('id' => 2),
  1036. 'Comment' => array(
  1037. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  1038. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsave'))
  1039. )
  1040. );
  1041. $expected = array(
  1042. 'Article' => true,
  1043. 'Comment' => array(
  1044. true,
  1045. true
  1046. )
  1047. );
  1048. $result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  1049. $this->assertSame($expected, $result);
  1050. $result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => true));
  1051. $this->assertSame($expected, $result);
  1052. $data = array(
  1053. 'Article' => array('id' => 2),
  1054. 'Comment' => array(
  1055. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  1056. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  1057. )
  1058. );
  1059. $expected = array(
  1060. 'Article' => true,
  1061. 'Comment' => array(
  1062. true,
  1063. false
  1064. )
  1065. );
  1066. $result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  1067. $this->assertSame($expected, $result);
  1068. $result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => true));
  1069. $this->assertSame($expected, $result);
  1070. $expected = array(
  1071. 'Comment' => array(
  1072. 1 => array(
  1073. 'Attachment' => array(
  1074. 'attachment' => array('This field cannot be left blank')
  1075. )
  1076. )
  1077. )
  1078. );
  1079. $result = $TestModel->validationErrors;
  1080. $this->assertSame($expected, $result);
  1081. $data = array(
  1082. 'Attachment' => array(
  1083. 'attachment' => 'deepsave insert',
  1084. ),
  1085. 'Comment' => array(
  1086. 'comment' => 'First comment deepsave insert',
  1087. 'published' => 'Y',
  1088. 'user_id' => 5,
  1089. 'Article' => array(
  1090. 'title' => 'First Article deepsave insert',
  1091. 'body' => 'First Article Body deepsave insert',
  1092. 'User' => array(
  1093. 'user' => 'deepsave',
  1094. 'password' => 'magic'
  1095. ),
  1096. ),
  1097. )
  1098. );
  1099. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  1100. $this->assertTrue($result);
  1101. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => true));
  1102. $this->assertTrue($result);
  1103. $expected = array(
  1104. 'Attachment' => true,
  1105. 'Comment' => true
  1106. );
  1107. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  1108. $this->assertSame($expected, $result);
  1109. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
  1110. $this->assertSame($expected, $result);
  1111. $data = array(
  1112. 'Attachment' => array(
  1113. 'attachment' => 'deepsave insert',
  1114. ),
  1115. 'Comment' => array(
  1116. 'comment' => 'First comment deepsave insert',
  1117. 'published' => 'Y',
  1118. 'user_id' => 5,
  1119. 'Article' => array(
  1120. 'title' => 'First Article deepsave insert',
  1121. 'body' => 'First Article Body deepsave insert',
  1122. 'User' => array(
  1123. 'user' => '',
  1124. 'password' => 'magic'
  1125. ),
  1126. ),
  1127. )
  1128. );
  1129. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  1130. $this->assertFalse($result);
  1131. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => true));
  1132. $this->assertFalse($result);
  1133. $result = $TestModel->Comment->Attachment->validationErrors;
  1134. $expected = array(
  1135. 'Comment' => array(
  1136. 'Article' => array(
  1137. 'User' => array(
  1138. 'user' => array('This field cannot be left blank')
  1139. )
  1140. )
  1141. )
  1142. );
  1143. $this->assertSame($expected, $result);
  1144. $expected = array(
  1145. 'Attachment' => true,
  1146. 'Comment' => false
  1147. );
  1148. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  1149. $this->assertEquals($expected, $result);
  1150. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
  1151. $this->assertEquals($expected, $result);
  1152. $data['Comment']['Article']['body'] = '';
  1153. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  1154. $this->assertFalse($result);
  1155. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => true));
  1156. $this->assertFalse($result);
  1157. $result = $TestModel->Comment->Attachment->validationErrors;
  1158. $expected = array(
  1159. 'Comment' => array(
  1160. 'Article' => array(
  1161. 'body' => array('This field cannot be left blank'),
  1162. 'User' => array(
  1163. 'user' => array('This field cannot be left blank')
  1164. )
  1165. )
  1166. )
  1167. );
  1168. $this->assertSame($expected, $result);
  1169. $expected = array(
  1170. 'Attachment' => true,
  1171. 'Comment' => false
  1172. );
  1173. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  1174. $this->assertEquals($expected, $result);
  1175. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
  1176. $this->assertEquals($expected, $result);
  1177. $data['Comment']['comment'] = '';
  1178. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  1179. $this->assertFalse($result);
  1180. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => true));
  1181. $this->assertFalse($result);
  1182. $result = $TestModel->Comment->Attachment->validationErrors;
  1183. $expected = array(
  1184. 'Comment' => array(
  1185. 'comment' => array('This field cannot be left blank'),
  1186. 'Article' => array(
  1187. 'body' => array('This field cannot be left blank'),
  1188. 'User' => array(
  1189. 'user' => array('This field cannot be left blank')
  1190. )
  1191. )
  1192. )
  1193. );
  1194. $this->assertSame($expected, $result);
  1195. $expected = array(
  1196. 'Attachment' => true,
  1197. 'Comment' => false
  1198. );
  1199. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  1200. $this->assertEquals($expected, $result);
  1201. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
  1202. $this->assertEquals($expected, $result);
  1203. $data['Attachment']['attachment'] = '';
  1204. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  1205. $this->assertFalse($result);
  1206. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => true));
  1207. $this->assertFalse($result);
  1208. $result = $TestModel->Comment->Attachment->validationErrors;
  1209. $expected = array(
  1210. 'attachment' => array('This field cannot be left blank'),
  1211. 'Comment' => array(
  1212. 'comment' => array('This field cannot be left blank'),
  1213. 'Article' => array(
  1214. 'body' => array('This field cannot be left blank'),
  1215. 'User' => array(
  1216. 'user' => array('This field cannot be left blank')
  1217. )
  1218. )
  1219. )
  1220. );
  1221. $this->assertSame($expected, $result);
  1222. $result = $TestModel->Comment->validationErrors;
  1223. $expected = array(
  1224. 'comment' => array('This field cannot be left blank'),
  1225. 'Article' => array(
  1226. 'body' => array('This field cannot be left blank'),
  1227. 'User' => array(
  1228. 'user' => array('This field cannot be left blank')
  1229. )
  1230. )
  1231. );
  1232. $this->assertSame($expected, $result);
  1233. $expected = array(
  1234. 'Attachment' => false,
  1235. 'Comment' => false
  1236. );
  1237. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  1238. $this->assertEquals($expected, $result);
  1239. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
  1240. $this->assertEquals($expected, $result);
  1241. }
  1242. /**
  1243. * testSaveAllNotDeepValidateOnly
  1244. * tests the validate methods to not validate deeper recursive data
  1245. *
  1246. * @return void
  1247. */
  1248. public function testSaveAllNotDeepValidateOnly() {
  1249. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  1250. $TestModel = new Article();
  1251. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  1252. $TestModel->hasAndBelongsToMany = array();
  1253. $TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
  1254. $TestModel->Comment->validate['comment'] = 'notEmpty';
  1255. $data = array(
  1256. 'Article' => array('id' => 2, 'body' => ''),
  1257. 'Comment' => array(
  1258. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  1259. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  1260. )
  1261. );
  1262. $result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => false));
  1263. $this->assertFalse($result);
  1264. $result = $TestModel->validateAssociated($data, array('deep' => false));
  1265. $this->assertFalse($result);
  1266. $expected = array('body' => array('This field cannot be left blank'));
  1267. $result = $TestModel->validationErrors;
  1268. $this->assertSame($expected, $result);
  1269. $data = array(
  1270. 'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
  1271. 'Comment' => array(
  1272. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  1273. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  1274. )
  1275. );
  1276. $result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => false));
  1277. $this->assertTrue($result);
  1278. $result = $TestModel->validateAssociated($data, array('deep' => false));
  1279. $this->assertTrue($result);
  1280. $data = array(
  1281. 'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
  1282. 'Comment' => array(
  1283. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  1284. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  1285. )
  1286. );
  1287. $expected = array(
  1288. 'Article' => true,
  1289. 'Comment' => array(
  1290. true,
  1291. true
  1292. )
  1293. );
  1294. $result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
  1295. $this->assertSame($expected, $result);
  1296. $result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => false));
  1297. $this->assertSame($expected, $result);
  1298. $data = array(
  1299. 'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
  1300. 'Comment' => array(
  1301. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  1302. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  1303. )
  1304. );
  1305. $result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => false));
  1306. $this->assertTrue($result);
  1307. $result = $TestModel->validateAssociated($data, array('deep' => false));
  1308. $this->assertTrue($result);
  1309. $data = array(
  1310. 'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
  1311. 'Comment' => array(
  1312. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  1313. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  1314. )
  1315. );
  1316. $expected = array(
  1317. 'Article' => true,
  1318. 'Comment' => array(
  1319. true,
  1320. true
  1321. )
  1322. );
  1323. $result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
  1324. $this->assertSame($expected, $result);
  1325. $result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => false));
  1326. $this->assertSame($expected, $result);
  1327. $expected = array();
  1328. $result = $TestModel->validationErrors;
  1329. $this->assertSame($expected, $result);
  1330. $data = array(
  1331. 'Attachment' => array(
  1332. 'attachment' => 'deepsave insert',
  1333. ),
  1334. 'Comment' => array(
  1335. 'comment' => 'First comment deepsave insert',
  1336. 'published' => 'Y',
  1337. 'user_id' => 5,
  1338. 'Article' => array(
  1339. 'title' => 'First Article deepsave insert ignored',
  1340. 'body' => 'First Article Body deepsave insert',
  1341. 'User' => array(
  1342. 'user' => '',
  1343. 'password' => 'magic'
  1344. ),
  1345. ),
  1346. )
  1347. );
  1348. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
  1349. $this->assertTrue($result);
  1350. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => false));
  1351. $this->assertTrue($result);
  1352. $result = $TestModel->Comment->Attachment->validationErrors;
  1353. $expected = array();
  1354. $this->assertSame($expected, $result);
  1355. $expected = array(
  1356. 'Attachment' => true,
  1357. 'Comment' => true
  1358. );
  1359. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
  1360. $this->assertEquals($expected, $result);
  1361. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => false));
  1362. $this->assertEquals($expected, $result);
  1363. $data['Comment']['Article']['body'] = '';
  1364. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
  1365. $this->assertTrue($result);
  1366. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => false));
  1367. $this->assertTrue($result);
  1368. $result = $TestModel->Comment->Attachment->validationErrors;
  1369. $expected = array();
  1370. $this->assertSame($expected, $result);
  1371. $expected = array(
  1372. 'Attachment' => true,
  1373. 'Comment' => true
  1374. );
  1375. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
  1376. $this->assertEquals($expected, $result);
  1377. $result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => false));
  1378. $this->assertEquals($expected, $result);
  1379. }
  1380. /**
  1381. * testValidateAssociated method
  1382. *
  1383. * @return void
  1384. */
  1385. public function testValidateAssociated() {
  1386. $this->loadFixtures('Comment', 'Attachment', 'Article', 'User');
  1387. $TestModel = new Comment();
  1388. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  1389. $data = array(
  1390. 'Comment' => array(
  1391. 'comment' => 'This is the comment'
  1392. ),
  1393. 'Attachment' => array(
  1394. 'attachment' => ''
  1395. )
  1396. );
  1397. $result = $TestModel->saveAll($data, array('validate' => 'only'));
  1398. $this->assertFalse($result);
  1399. $result = $TestModel->validateAssociated($data);
  1400. $this->assertFalse($result);
  1401. $fieldList = array(
  1402. 'Attachment' => array('comment_id')
  1403. );
  1404. $result = $TestModel->saveAll($data, array(
  1405. 'fieldList' => $fieldList, 'validate' => 'only'
  1406. ));
  1407. $this->assertTrue($result);
  1408. $this->assertEmpty($TestModel->validationErrors);
  1409. $result = $TestModel->validateAssociated($data, array('fieldList' => $fieldList));
  1410. $this->assertTrue($result);
  1411. $this->assertEmpty($TestModel->validationErrors);
  1412. $TestModel->validate = array('comment' => 'notEmpty');
  1413. $record = array(
  1414. 'Comment' => array(
  1415. 'user_id' => 1,
  1416. 'article_id' => 1,
  1417. 'comment' => '',
  1418. ),
  1419. 'Attachment' => array(
  1420. 'attachment' => ''
  1421. )
  1422. );
  1423. $result = $TestModel->saveAll($record, array('validate' => 'only'));
  1424. $this->assertFalse($result);
  1425. $result = $TestModel->validateAssociated($record);
  1426. $this->assertFalse($result);
  1427. $fieldList = array(
  1428. 'Comment' => array('id', 'article_id', 'user_id'),
  1429. 'Attachment' => array('comment_id')
  1430. );
  1431. $result = $TestModel->saveAll($record, array(
  1432. 'fieldList' => $fieldList, 'validate' => 'only'
  1433. ));
  1434. $this->assertTrue($result);
  1435. $this->assertEmpty($TestModel->validationErrors);
  1436. $result = $TestModel->validateAssociated($record, array('fieldList' => $fieldList));
  1437. $this->assertTrue($result);
  1438. $this->assertEmpty($TestModel->validationErrors);
  1439. $TestModel = new Article();
  1440. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  1441. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  1442. $data = array(
  1443. 'Article' => array('id' => 2),
  1444. 'Comment' => array(
  1445. array(
  1446. 'id' => 1,
  1447. 'comment' => '',
  1448. 'published' => 'Y',
  1449. 'user_id' => 1,
  1450. ),
  1451. array(
  1452. 'id' => 2,
  1453. 'comment' =>
  1454. 'comment',
  1455. 'published' => 'Y',
  1456. 'user_id' => 1
  1457. ),
  1458. array(
  1459. 'id' => 3,
  1460. 'comment' => '',
  1461. 'published' => 'Y',
  1462. 'user_id' => 1
  1463. )));
  1464. $result = $TestModel->saveAll($data, array('validate' => 'only'));
  1465. $this->assertFalse($result);
  1466. $result = $TestModel->validateAssociated($data);
  1467. $this->assertFalse($result);
  1468. $expected = array(
  1469. 'Article' => true,
  1470. 'Comment' => array(false, true, false)
  1471. );
  1472. $result = $TestModel->saveAll($data, array('atomic' => false, 'validate' => 'only'));
  1473. $this->assertSame($expected, $result);
  1474. $result = $TestModel->validateAssociated($data, array('atomic' => false));
  1475. $this->assertSame($expected, $result);
  1476. $expected = array('Comment' => array(
  1477. 0 => array('comment' => array('This field cannot be left blank')),
  1478. 2 => array('comment' => array('This field cannot be left blank'))
  1479. ));
  1480. $this->assertEquals($expected['Comment'], $TestModel->Comment->validationErrors);
  1481. $model = new Comment();
  1482. $model->deleteAll(true);
  1483. $model->validate = array('comment' => 'notEmpty');
  1484. $model->Attachment->validate = array('attachment' => 'notEmpty');
  1485. $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
  1486. $expected = array(
  1487. 'comment' => array('This field cannot be left blank'),
  1488. 'Attachment' => array(
  1489. 'attachment' => array('This field cannot be left blank')
  1490. )
  1491. );
  1492. $data = array(
  1493. 'Comment' => array('comment' => '', 'article_id' => 1, 'user_id' => 1),
  1494. 'Attachment' => array('attachment' => '')
  1495. );
  1496. $result = $model->saveAll($data, array('validate' => 'only'));
  1497. $this->assertFalse($result);
  1498. $result = $model->validateAssociated($data);
  1499. $this->assertFalse($result);
  1500. $this->assertEquals($expected, $model->validationErrors);
  1501. $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
  1502. }
  1503. /**
  1504. * testValidateMany method
  1505. *
  1506. * @return void
  1507. */
  1508. public function testValidateMany() {
  1509. $TestModel = new Article();
  1510. $TestModel->validate = array('title' => 'notEmpty');
  1511. $data = array(
  1512. 0 => array('title' => ''),
  1513. 1 => array('title' => 'title 1'),
  1514. 2 => array('title' => 'title 2'),
  1515. );
  1516. $expected = array(
  1517. 0 => array('title' => array('This field cannot be left blank')),
  1518. );
  1519. $result = $TestModel->saveAll($data, array('validate' => 'only'));
  1520. $this->assertFalse($result);
  1521. $this->assertEquals($expected, $TestModel->validationErrors);
  1522. $result = $TestModel->validateMany($data);
  1523. $this->assertFalse($result);
  1524. $this->assertEquals($expected, $TestModel->validationErrors);
  1525. $data = array(
  1526. 0 => array('title' => 'title 0'),
  1527. 1 => array('title' => ''),
  1528. 2 => array('title' => 'title 2'),
  1529. );
  1530. $expected = array(
  1531. 1 => array('title' => array('This field cannot be left blank')),
  1532. );
  1533. $result = $TestModel->saveAll($data, array('validate' => 'only'));
  1534. $this->assertFalse($result);
  1535. $this->assertEquals($expected, $TestModel->validationErrors);
  1536. $result = $TestModel->validateMany($data);
  1537. $this->assertFalse($result);
  1538. $this->assertEquals($expected, $TestModel->validationErrors);
  1539. }
  1540. /**
  1541. * testGetMethods method
  1542. *
  1543. * @return void
  1544. */
  1545. public function testGetMethods() {
  1546. $this->loadFixtures('Article', 'Comment');
  1547. $TestModel = new Article();
  1548. $Validator = $TestModel->validator();
  1549. $result = $Validator->getMethods();
  1550. $expected = array_map('strtolower', get_class_methods('Article'));
  1551. $this->assertEquals($expected, array_keys($result));
  1552. }
  1553. /**
  1554. * Tests that methods are refreshed when the list of behaviors change
  1555. *
  1556. * @return void
  1557. */
  1558. public function testGetMethodsRefresh() {
  1559. $this->loadFixtures('Article', 'Comment');
  1560. $TestModel = new Article();
  1561. $Validator = $TestModel->validator();
  1562. $result = $Validator->getMethods();
  1563. $expected = array_map('strtolower', get_class_methods('Article'));
  1564. $this->assertEquals($expected, array_keys($result));
  1565. $TestModel->Behaviors->load('Containable');
  1566. $newList = array(
  1567. 'contain',
  1568. 'resetbindings',
  1569. 'containments',
  1570. 'fielddependencies',
  1571. 'containmentsmap'
  1572. );
  1573. $this->assertEquals(array_merge($expected, $newList), array_keys($Validator->getMethods()));
  1574. $TestModel->Behaviors->unload('Containable');
  1575. $this->assertEquals($expected, array_keys($Validator->getMethods()));
  1576. }
  1577. /**
  1578. * testSetValidationDomain method
  1579. *
  1580. * @return void
  1581. */
  1582. public function testSetValidationDomain() {
  1583. $this->loadFixtures('Article', 'Comment');
  1584. $TestModel = new Article();
  1585. $Validator = $TestModel->validator();
  1586. $result = $Validator->setValidationDomain('default');
  1587. $this->assertEquals('default', $TestModel->validationDomain);
  1588. $result = $Validator->setValidationDomain('other');
  1589. $this->assertEquals('other', $TestModel->validationDomain);
  1590. }
  1591. /**
  1592. * testGetModel method
  1593. *
  1594. * @return void
  1595. */
  1596. public function testGetModel() {
  1597. $TestModel = new Article();
  1598. $Validator = $TestModel->validator();
  1599. $result = $Validator->getModel();
  1600. $this->assertInstanceOf('Article', $result);
  1601. }
  1602. /**
  1603. * Tests it is possible to get validation sets for a field using an array inteface
  1604. *
  1605. * @return void
  1606. */
  1607. public function testArrayAccessGet() {
  1608. $TestModel = new Article();
  1609. $Validator = $TestModel->validator();
  1610. $titleValidator = $Validator['title'];
  1611. $this->assertEquals('title', $titleValidator->field);
  1612. $this->assertCount(1, $titleValidator->getRules());
  1613. $rule = current($titleValidator->getRules());
  1614. $this->assertEquals('notEmpty', $rule->rule);
  1615. $titleValidator = $Validator['body'];
  1616. $this->assertEquals('body', $titleValidator->field);
  1617. $this->assertCount(1, $titleValidator->getRules());
  1618. $rule = current($titleValidator->getRules());
  1619. $this->assertEquals('notEmpty', $rule->rule);
  1620. $titleValidator = $Validator['user_id'];
  1621. $this->assertEquals('user_id', $titleValidator->field);
  1622. $this->assertCount(1, $titleValidator->getRules());
  1623. $rule = current($titleValidator->getRules());
  1624. $this->assertEquals('numeric', $rule->rule);
  1625. }
  1626. /**
  1627. * Tests it is possible to check for validation sets for a field using an array inteface
  1628. *
  1629. * @return void
  1630. */
  1631. public function testArrayAccessExists() {
  1632. $TestModel = new Article();
  1633. $Validator = $TestModel->validator();
  1634. $this->assertTrue(isset($Validator['title']));
  1635. $this->assertTrue(isset($Validator['body']));
  1636. $this->assertTrue(isset($Validator['user_id']));
  1637. $this->assertFalse(isset($Validator['other']));
  1638. }
  1639. /**
  1640. * Tests it is possible to set validation rules for a field using an array inteface
  1641. *
  1642. * @return void
  1643. */
  1644. public function testArrayAccessSet() {
  1645. $TestModel = new Article();
  1646. $Validator = $TestModel->validator();
  1647. $set = array(
  1648. 'numeric' => array('rule' => 'numeric', 'allowEmpty' => false),
  1649. 'range' => array('rule' => array('between', 1, 5), 'allowEmpty' => false),
  1650. );
  1651. $Validator['other'] = $set;
  1652. $rules = $Validator['other'];
  1653. $this->assertEquals('other', $rules->field);
  1654. $validators = $rules->getRules();
  1655. $this->assertCount(2, $validators);
  1656. $this->assertEquals('numeric', $validators['numeric']->rule);
  1657. $this->assertEquals(array('between', 1, 5), $validators['range']->rule);
  1658. $Validator['new'] = new CakeValidationSet('new', $set, array());
  1659. $rules = $Validator['new'];
  1660. $this->assertEquals('new', $rules->field);
  1661. $validators = $rules->getRules();
  1662. $this->assertCount(2, $validators);
  1663. $this->assertEquals('numeric', $validators['numeric']->rule);
  1664. $this->assertEquals(array('between', 1, 5), $validators['range']->rule);
  1665. }
  1666. /**
  1667. * Tests it is possible to unset validation rules
  1668. *
  1669. * @return void
  1670. */
  1671. public function testArrayAccessUset() {
  1672. $TestModel = new Article();
  1673. $Validator = $TestModel->validator();
  1674. $this->assertTrue(isset($Validator['title']));
  1675. unset($Validator['title']);
  1676. $this->assertFalse(isset($Validator['title']));
  1677. }
  1678. /**
  1679. * Tests it is possible to iterate a validation object
  1680. *
  1681. * @return void
  1682. */
  1683. public function testIterator() {
  1684. $TestModel = new Article();
  1685. $Validator = $TestModel->validator();
  1686. $i = 0;
  1687. foreach ($Validator as $field => $rules) {
  1688. if ($i === 0) {
  1689. $this->assertEquals('user_id', $field);
  1690. }
  1691. if ($i === 1) {
  1692. $this->assertEquals('title', $field);
  1693. }
  1694. if ($i === 2) {
  1695. $this->assertEquals('body', $field);
  1696. }
  1697. $this->assertInstanceOf('CakeValidationSet', $rules);
  1698. $i++;
  1699. }
  1700. $this->assertEquals(3, $i);
  1701. }
  1702. /**
  1703. * Tests countable interface in ModelValidator
  1704. *
  1705. * @return void
  1706. */
  1707. public function testCount() {
  1708. $TestModel = new Article();
  1709. $Validator = $TestModel->validator();
  1710. $this->assertCount(3, $Validator);
  1711. $set = array(
  1712. 'numeric' => array('rule' => 'numeric', 'allowEmpty' => false),
  1713. 'range' => array('rule' => array('between', 1, 5), 'allowEmpty' => false),
  1714. );
  1715. $Validator['other'] = $set;
  1716. $this->assertCount(4, $Validator);
  1717. unset($Validator['title']);
  1718. $this->assertCount(3, $Validator);
  1719. unset($Validator['body']);
  1720. $this->assertCount(2, $Validator);
  1721. }
  1722. /**
  1723. * Tests it is possible to add validation rules
  1724. *
  1725. * @return void
  1726. */
  1727. public function testAddRule() {
  1728. $TestModel = new Article();
  1729. $Validator = $TestModel->validator();
  1730. $Validator->add('other', 'numeric', array('rule' => 'numeric', 'allowEmpty' => false));
  1731. $Validator->add('other', 'range', array('rule' => array('between', 1, 5), 'allowEmpty' => false));
  1732. $rules = $Validator['other'];
  1733. $this->assertEquals('other', $rules->field);
  1734. $validators = $rules->getRules();
  1735. $this->assertCount(2, $validators);
  1736. $this->assertEquals('numeric', $validators['numeric']->rule);
  1737. $this->assertEquals(array('between', 1, 5), $validators['range']->rule);
  1738. }
  1739. /**
  1740. * Tests it is possible to remove validation rules
  1741. *
  1742. * @return void
  1743. */
  1744. public function testRemoveRule() {
  1745. $TestModel = new Article();
  1746. $Validator = $TestModel->validator();
  1747. $this->assertTrue(isset($Validator['title']));
  1748. $Validator->remove('title');
  1749. $this->assertFalse(isset($Validator['title']));
  1750. $Validator->add('other', 'numeric', array('rule' => 'numeric', 'allowEmpty' => false));
  1751. $Validator->add('other', 'range', array('rule' => array('between', 1, 5), 'allowEmpty' => false));
  1752. $this->assertTrue(isset($Validator['other']));
  1753. $Validator->remove('other', 'numeric');
  1754. $this->assertTrue(isset($Validator['other']));
  1755. $this->assertFalse(isset($Validator['other']['numeric']));
  1756. $this->assertTrue(isset($Validator['other']['range']));
  1757. }
  1758. /**
  1759. * Tests validation callbacks are triggered
  1760. *
  1761. * @return void
  1762. */
  1763. public function testValidateCallbacks() {
  1764. $TestModel = $this->getMock('Article', array('beforeValidate', 'afterValidate'));
  1765. $TestModel->expects($this->once())->method('beforeValidate');
  1766. $TestModel->expects($this->once())->method('afterValidate');
  1767. $TestModel->set(array('title' => '', 'body' => 'body'));
  1768. $TestModel->validates();
  1769. }
  1770. /**
  1771. * Tests that altering data in a beforeValidate callback will lead to saving those
  1772. * values in database
  1773. *
  1774. * @return void
  1775. */
  1776. public function testValidateFirstWithBeforeValidate() {
  1777. $this->loadFixtures('Article', 'User');
  1778. $model = new CustomArticle();
  1779. $model->validate = array(
  1780. 'title' => array(
  1781. 'notempty' => array(
  1782. 'rule' => 'notEmpty',
  1783. 'required' => true,
  1784. 'allowEmpty' => false
  1785. )
  1786. )
  1787. );
  1788. $data = array(
  1789. 'CustomArticle' => array(
  1790. 'body' => 'foo0'
  1791. )
  1792. );
  1793. $result = $model->saveAll($data, array('validate' => 'first'));
  1794. $this->assertTrue($result);
  1795. $this->assertFalse($model->findMethods['unPublished'], 'beforeValidate was run twice');
  1796. $model->findMethods['unPublished'] = true;
  1797. $data = array(
  1798. 'CustomArticle' => array(
  1799. 'body' => 'foo1'
  1800. )
  1801. );
  1802. $result = $model->saveAll($data, array('validate' => 'first', 'deep' => true));
  1803. $this->assertTrue($result);
  1804. $title = $model->field('title', array('body' => 'foo1'));
  1805. $this->assertEquals('foo', $title);
  1806. $this->assertFalse($model->findMethods['unPublished'], 'beforeValidate was run twice');
  1807. $data = array(
  1808. array('body' => 'foo2'),
  1809. array('body' => 'foo3'),
  1810. array('body' => 'foo4')
  1811. );
  1812. $result = $model->saveAll($data, array('validate' => 'first', 'deep' => true));
  1813. $this->assertTrue($result);
  1814. $this->assertEquals('foo', $model->field('title', array('body' => 'foo2')));
  1815. $this->assertEquals('foo', $model->field('title', array('body' => 'foo3')));
  1816. $this->assertEquals('foo', $model->field('title', array('body' => 'foo4')));
  1817. }
  1818. /**
  1819. * Tests that altering data in a beforeValidate callback will lead to saving those
  1820. * values in database
  1821. *
  1822. * @return void
  1823. */
  1824. public function testValidateFirstAssociatedWithBeforeValidate() {
  1825. $this->loadFixtures('Article', 'User');
  1826. $model = new CustomArticle();
  1827. $model->validate = array(
  1828. 'title' => array(
  1829. 'notempty' => array(
  1830. 'rule' => 'notEmpty',
  1831. 'required' => true
  1832. )
  1833. )
  1834. );
  1835. $articles = array(
  1836. array('body' => 'foo1'),
  1837. array('body' => 'foo2'),
  1838. array('body' => 'foo3')
  1839. );
  1840. $user = new User();
  1841. $user->bindModel(array('hasMany' => array('CustomArticle')));
  1842. $data = array(
  1843. 'User' => array('user' => 'foo', 'password' => 'bar'),
  1844. 'CustomArticle' => $articles
  1845. );
  1846. $result = $user->saveAll($data, array('validate' => 'first'));
  1847. $this->assertTrue($result);
  1848. $this->assertEquals('foo', $model->field('title', array('body' => 'foo1')));
  1849. $this->assertEquals('foo', $model->field('title', array('body' => 'foo2')));
  1850. $this->assertEquals('foo', $model->field('title', array('body' => 'foo3')));
  1851. }
  1852. /**
  1853. * testValidateFirstWithDefaults method
  1854. *
  1855. * @return void
  1856. */
  1857. public function testFirstWithDefaults() {
  1858. $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  1859. $TestModel = new Article();
  1860. $result = $TestModel->find('first', array(
  1861. 'conditions' => array('Article.id' => 1)
  1862. ));
  1863. $expected = array(
  1864. 'Article' => array(
  1865. 'id' => 1,
  1866. 'user_id' => 1,
  1867. 'title' => 'First Article',
  1868. 'body' => 'First Article Body',
  1869. 'published' => 'Y',
  1870. 'created' => '2007-03-18 10:39:23'
  1871. ),
  1872. );
  1873. unset($result['Article']['updated']);
  1874. $this->assertEquals($expected['Article'], $result['Article']);
  1875. $data = array(
  1876. 'Article' => array(
  1877. 'id' => 1,
  1878. 'title' => 'First Article (modified)'
  1879. ),
  1880. 'Comment' => array(
  1881. array('comment' => 'Article comment', 'user_id' => 1)
  1882. )
  1883. );
  1884. $result = $TestModel->saveAll($data, array('validate' => 'first'));
  1885. $this->assertTrue($result);
  1886. $result = $TestModel->find('first', array(
  1887. 'conditions' => array('Article.id' => 1)
  1888. ));
  1889. $expected['Article']['title'] = 'First Article (modified)';
  1890. unset($result['Article']['updated']);
  1891. $this->assertEquals($expected['Article'], $result['Article']);
  1892. }
  1893. public function testAddMultipleRules() {
  1894. $TestModel = new Article();
  1895. $Validator = $TestModel->validator();
  1896. $set = array(
  1897. 'numeric' => array('rule' => 'numeric', 'allowEmpty' => false),
  1898. 'range' => array('rule' => array('between', 1, 5), 'allowEmpty' => false),
  1899. );
  1900. $Validator->add('other', $set);
  1901. $rules = $Validator['other'];
  1902. $this->assertEquals('other', $rules->field);
  1903. $validators = $rules->getRules();
  1904. $this->assertCount(2, $validators);
  1905. $this->assertEquals('numeric', $validators['numeric']->rule);
  1906. $this->assertEquals(array('between', 1, 5), $validators['range']->rule);
  1907. $set = new CakeValidationSet('other', array(
  1908. 'a' => array('rule' => 'numeric', 'allowEmpty' => false),
  1909. 'b' => array('rule' => array('between', 1, 5), 'allowEmpty' => false),
  1910. ));
  1911. $Validator->add('other', $set);
  1912. $this->assertSame($set, $Validator->getField('other'));
  1913. }
  1914. /**
  1915. * Test that rules are parsed correctly when calling getField()
  1916. *
  1917. * @return void
  1918. */
  1919. public function testValidator() {
  1920. $TestModel = new Article();
  1921. $Validator = $TestModel->validator();
  1922. $result = $Validator->getField();
  1923. $expected = array('user_id', 'title', 'body');
  1924. $this->assertEquals($expected, array_keys($result));
  1925. $this->assertTrue($result['user_id'] instanceof CakeValidationSet);
  1926. $result = $TestModel->validator()->getField('title');
  1927. $this->assertTrue($result instanceof CakeValidationSet);
  1928. }
  1929. /**
  1930. * Test that validator override works as expected
  1931. *
  1932. * @return void
  1933. */
  1934. public function testValidatorOverride() {
  1935. $TestModel = new Article();
  1936. $ValidatorA = new ModelValidator($TestModel);
  1937. $ValidatorB = new ModelValidator($TestModel);
  1938. $TestModel->validator($ValidatorA);
  1939. $TestModel->validator($ValidatorB);
  1940. $this->assertSame($ValidatorB, $TestModel->validator());
  1941. $this->assertNotSame($ValidatorA, $TestModel->validator());
  1942. }
  1943. /**
  1944. * Test that type hint exception is thrown
  1945. *
  1946. * @expectedException PHPUnit_Framework_Error
  1947. * @return void
  1948. */
  1949. public function testValidatorTypehintException() {
  1950. new ModelValidator('asdasds');
  1951. }
  1952. /**
  1953. * Tests that altering data in a beforeValidate callback will lead to saving those
  1954. * values in database, this time with belongsTo associations
  1955. *
  1956. * @return void
  1957. */
  1958. public function testValidateFirstAssociatedWithBeforeValidate2() {
  1959. $this->loadFixtures('Article', 'User');
  1960. $model = new CustomArticle();
  1961. $model->validate = array(
  1962. 'title' => array(
  1963. 'notempty' => array(
  1964. 'rule' => 'notEmpty',
  1965. 'required' => true
  1966. )
  1967. )
  1968. );
  1969. $data = array(
  1970. 'User' => array('user' => 'foo', 'password' => 'bar'),
  1971. 'CustomArticle' => array(
  1972. 'body' => 'a test'
  1973. )
  1974. );
  1975. $result = $model->saveAll($data, array('validate' => 'first'));
  1976. $this->assertTrue($result);
  1977. $this->assertEquals('foo', $model->field('title', array('body' => 'a test')));
  1978. }
  1979. /**
  1980. * Testing you can dynamically add rules to a field, added this to dispel doubts
  1981. * after a presentation made to show off this new feature
  1982. *
  1983. * @return void
  1984. */
  1985. public function testDynamicValidationRuleBuilding() {
  1986. $model = new Article;
  1987. $validator = $model->validator();
  1988. $validator->add('body', 'isSpecial', array('rule' => 'special'));
  1989. $rules = $validator['body']->getRules();
  1990. $this->assertCount(2, $rules);
  1991. $this->assertEquals('special', $rules['isSpecial']->rule);
  1992. $validator['body']->setRule('isAwesome', array('rule' => 'awesome'));
  1993. $rules = $validator['body']->getRules();
  1994. $this->assertCount(3, $rules);
  1995. $this->assertEquals('awesome', $rules['isAwesome']->rule);
  1996. }
  1997. /**
  1998. * Test to ensure custom validation methods work with CakeValidationSet
  1999. *
  2000. * @return void
  2001. */
  2002. public function testCustomMethodsWithCakeValidationSet() {
  2003. $TestModel = new TestValidate();
  2004. $Validator = $TestModel->validator();
  2005. $Validator->add('title', 'validateTitle', array(
  2006. 'rule' => 'validateTitle',
  2007. 'message' => 'That aint right',
  2008. ));
  2009. $data = array('title' => 'notatitle');
  2010. $result = $Validator->getField('title')->validate($data);
  2011. $expected = array(0 => 'That aint right');
  2012. $this->assertEquals($expected, $result);
  2013. $data = array('title' => 'title-is-good');
  2014. $result = $Validator->getField('title')->validate($data);
  2015. $expected = array();
  2016. $this->assertEquals($expected, $result);
  2017. }
  2018. public function testCustomMethodWithEmptyValue() {
  2019. $this->loadFixtures('Article');
  2020. $model = $this->getMock('Article', array('isLegit'));
  2021. $model->validate = array(
  2022. 'title' => array(
  2023. 'custom' => array(
  2024. 'rule' => array('isLegit'),
  2025. 'message' => 'is no good'
  2026. )
  2027. )
  2028. );
  2029. $model->expects($this->once())
  2030. ->method('isLegit')
  2031. ->will($this->returnValue(false));
  2032. $model->set(array('title' => ''));
  2033. $this->assertFalse($model->validates());
  2034. }
  2035. /**
  2036. * Test validateAssociated with atomic=false & deep=true
  2037. *
  2038. * @return void
  2039. */
  2040. public function testValidateAssociatedAtomicFalseDeepTrueWithErrors() {
  2041. $this->loadFixtures('Comment', 'Article', 'User', 'Attachment');
  2042. $Attachment = ClassRegistry::init('Attachment');
  2043. $Attachment->Comment->validator()->add('comment', array(
  2044. array('rule' => 'notEmpty')
  2045. ));
  2046. $Attachment->Comment->User->bindModel(array(
  2047. 'hasMany' => array(
  2048. 'Article',
  2049. 'Comment'
  2050. )),
  2051. false
  2052. );
  2053. $data = array(
  2054. 'Attachment' => array(
  2055. 'attachment' => 'text',
  2056. 'Comment' => array(
  2057. 'comment' => '',
  2058. 'published' => 'N',
  2059. 'User' => array(
  2060. 'user' => 'Foo',
  2061. 'password' => 'mypassword',
  2062. 'Comment' => array(
  2063. array(
  2064. 'comment' => ''
  2065. )
  2066. )
  2067. )
  2068. )
  2069. )
  2070. );
  2071. $result = $Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
  2072. $result = $Attachment->validationErrors;
  2073. $expected = array(
  2074. 'Comment' => array(
  2075. 'comment' => array(
  2076. 0 => 'This field cannot be left blank',
  2077. ),
  2078. 'User' => array(
  2079. 'Comment' => array(
  2080. 0 => array(
  2081. 'comment' => array(
  2082. 0 => 'This field cannot be left blank',
  2083. ),
  2084. ),
  2085. ),
  2086. ),
  2087. ),
  2088. );
  2089. $this->assertEquals($expected, $result);
  2090. }
  2091. /**
  2092. * Test validateMany with atomic=false & deep=true
  2093. *
  2094. * @return void
  2095. */
  2096. public function testValidateManyAtomicFalseDeepTrueWithErrors() {
  2097. $this->loadFixtures('Comment', 'Article', 'User');
  2098. $Article = ClassRegistry::init('Article');
  2099. $Article->Comment->validator()->add('comment', array(
  2100. array('rule' => 'notEmpty')
  2101. ));
  2102. $data = array(
  2103. array(
  2104. 'Article' => array(
  2105. 'user_id' => 1,
  2106. 'title' => 'Foo',
  2107. 'body' => 'text',
  2108. 'published' => 'N'
  2109. ),
  2110. 'Comment' => array(
  2111. array(
  2112. 'user_id' => 1,
  2113. 'comment' => 'Baz',
  2114. 'published' => 'N',
  2115. )
  2116. ),
  2117. ),
  2118. array(
  2119. 'Article' => array(
  2120. 'user_id' => 1,
  2121. 'title' => 'Bar',
  2122. 'body' => 'text',
  2123. 'published' => 'N'
  2124. ),
  2125. 'Comment' => array(
  2126. array(
  2127. 'user_id' => 1,
  2128. 'comment' => '',
  2129. 'published' => 'N',
  2130. )
  2131. ),
  2132. ),
  2133. );
  2134. $Article->validateMany($data, array('atomic' => false, 'deep' => true));
  2135. $result = $Article->validationErrors;
  2136. $expected = array(
  2137. 1 => array(
  2138. 'Comment' => array(
  2139. 0 => array(
  2140. 'comment' => array(
  2141. 0 => 'This field cannot be left blank',
  2142. ),
  2143. ),
  2144. ),
  2145. ),
  2146. );
  2147. $this->assertEquals($expected, $result);
  2148. }
  2149. }
  2150. /**
  2151. * Behavior for testing validation rules.
  2152. */
  2153. class ValidationRuleBehavior extends ModelBehavior {
  2154. public function setup(Model $Model, $config = array()) {
  2155. $this->settings[$Model->alias] = $config;
  2156. }
  2157. public function beforeValidate(Model $Model, $options = array()) {
  2158. $fields = $this->settings[$Model->alias]['fields'];
  2159. foreach ($fields as $field) {
  2160. $Model->whitelist[] = $field;
  2161. }
  2162. }
  2163. }