ModelValidationTest.php 66 KB

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