ModelValidationTest.php 67 KB

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