ModelValidationTest.php 68 KB

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