ModelWriteTest.php 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979
  1. <?php
  2. /**
  3. * ModelWriteTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package cake.tests.cases.libs.model
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
  20. /**
  21. * ModelWriteTest
  22. *
  23. * @package cake.tests.cases.libs.model.operations
  24. */
  25. class ModelWriteTest extends BaseModelTest {
  26. /**
  27. * testInsertAnotherHabtmRecordWithSameForeignKey method
  28. *
  29. * @access public
  30. * @return void
  31. */
  32. function testInsertAnotherHabtmRecordWithSameForeignKey() {
  33. $this->loadFixtures('JoinA', 'JoinB', 'JoinAB', 'JoinC', 'JoinAC');
  34. $TestModel = new JoinA();
  35. $result = $TestModel->JoinAsJoinB->findById(1);
  36. $expected = array(
  37. 'JoinAsJoinB' => array(
  38. 'id' => 1,
  39. 'join_a_id' => 1,
  40. 'join_b_id' => 2,
  41. 'other' => 'Data for Join A 1 Join B 2',
  42. 'created' => '2008-01-03 10:56:33',
  43. 'updated' => '2008-01-03 10:56:33'
  44. ));
  45. $this->assertEqual($result, $expected);
  46. $TestModel->JoinAsJoinB->create();
  47. $data = array(
  48. 'join_a_id' => 1,
  49. 'join_b_id' => 1,
  50. 'other' => 'Data for Join A 1 Join B 1',
  51. 'created' => '2008-01-03 10:56:44',
  52. 'updated' => '2008-01-03 10:56:44'
  53. );
  54. $result = $TestModel->JoinAsJoinB->save($data);
  55. $this->assertEquals($result, array('JoinAsJoinB' => $data));
  56. $lastInsertId = $TestModel->JoinAsJoinB->getLastInsertID();
  57. $this->assertTrue($lastInsertId != null);
  58. $result = $TestModel->JoinAsJoinB->findById(1);
  59. $expected = array(
  60. 'JoinAsJoinB' => array(
  61. 'id' => 1,
  62. 'join_a_id' => 1,
  63. 'join_b_id' => 2,
  64. 'other' => 'Data for Join A 1 Join B 2',
  65. 'created' => '2008-01-03 10:56:33',
  66. 'updated' => '2008-01-03 10:56:33'
  67. ));
  68. $this->assertEqual($result, $expected);
  69. $updatedValue = 'UPDATED Data for Join A 1 Join B 2';
  70. $TestModel->JoinAsJoinB->id = 1;
  71. $result = $TestModel->JoinAsJoinB->saveField('other', $updatedValue, false);
  72. $this->assertFalse(empty($result));
  73. $result = $TestModel->JoinAsJoinB->findById(1);
  74. $this->assertEqual($result['JoinAsJoinB']['other'], $updatedValue);
  75. }
  76. /**
  77. * testSaveDateAsFirstEntry method
  78. *
  79. * @access public
  80. * @return void
  81. */
  82. function testSaveDateAsFirstEntry() {
  83. $this->loadFixtures('Article', 'User', 'Comment', 'Attachment', 'Tag', 'ArticlesTag');
  84. $Article = new Article();
  85. $data = array(
  86. 'Article' => array(
  87. 'created' => array(
  88. 'day' => '1',
  89. 'month' => '1',
  90. 'year' => '2008'
  91. ),
  92. 'title' => 'Test Title',
  93. 'user_id' => 1
  94. ));
  95. $Article->create();
  96. $result = $Article->save($data);
  97. $this->assertFalse(empty($result));
  98. $testResult = $Article->find('first', array('conditions' => array('Article.title' => 'Test Title')));
  99. $this->assertEqual($testResult['Article']['title'], $data['Article']['title']);
  100. $this->assertEqual($testResult['Article']['created'], '2008-01-01 00:00:00');
  101. }
  102. /**
  103. * testUnderscoreFieldSave method
  104. *
  105. * @access public
  106. * @return void
  107. */
  108. function testUnderscoreFieldSave() {
  109. $this->loadFixtures('UnderscoreField');
  110. $UnderscoreField = new UnderscoreField();
  111. $currentCount = $UnderscoreField->find('count');
  112. $this->assertEqual($currentCount, 3);
  113. $data = array('UnderscoreField' => array(
  114. 'user_id' => '1',
  115. 'my_model_has_a_field' => 'Content here',
  116. 'body' => 'Body',
  117. 'published' => 'Y',
  118. 'another_field' => 4
  119. ));
  120. $ret = $UnderscoreField->save($data);
  121. $this->assertFalse(empty($ret));
  122. $currentCount = $UnderscoreField->find('count');
  123. $this->assertEqual($currentCount, 4);
  124. }
  125. /**
  126. * testAutoSaveUuid method
  127. *
  128. * @access public
  129. * @return void
  130. */
  131. function testAutoSaveUuid() {
  132. // SQLite does not support non-integer primary keys
  133. $this->skipIf($this->db instanceof Sqlite);
  134. $this->loadFixtures('Uuid');
  135. $TestModel = new Uuid();
  136. $TestModel->save(array('title' => 'Test record'));
  137. $result = $TestModel->findByTitle('Test record');
  138. $this->assertEqual(
  139. array_keys($result['Uuid']),
  140. array('id', 'title', 'count', 'created', 'updated')
  141. );
  142. $this->assertEqual(strlen($result['Uuid']['id']), 36);
  143. }
  144. /**
  145. * Ensure that if the id key is null but present the save doesn't fail (with an
  146. * x sql error: "Column id specified twice")
  147. *
  148. * @return void
  149. */
  150. public function testSaveUuidNull() {
  151. // SQLite does not support non-integer primary keys
  152. $this->skipIf($this->db instanceof Sqlite);
  153. $this->loadFixtures('Uuid');
  154. $TestModel = new Uuid();
  155. $TestModel->save(array('title' => 'Test record', 'id' => null));
  156. $result = $TestModel->findByTitle('Test record');
  157. $this->assertEqual(
  158. array_keys($result['Uuid']),
  159. array('id', 'title', 'count', 'created', 'updated')
  160. );
  161. $this->assertEqual(strlen($result['Uuid']['id']), 36);
  162. }
  163. /**
  164. * testZeroDefaultFieldValue method
  165. *
  166. * @access public
  167. * @return void
  168. */
  169. function testZeroDefaultFieldValue() {
  170. $this->skipIf(
  171. $this->db instanceof Sqlite,
  172. '%s SQLite uses loose typing, this operation is unsupported'
  173. );
  174. $this->loadFixtures('DataTest');
  175. $TestModel = new DataTest();
  176. $TestModel->create(array());
  177. $TestModel->save();
  178. $result = $TestModel->findById($TestModel->id);
  179. $this->assertEquals($result['DataTest']['count'], 0);
  180. $this->assertEquals($result['DataTest']['float'], 0);
  181. }
  182. /**
  183. * Tests validation parameter order in custom validation methods
  184. *
  185. * @access public
  186. * @return void
  187. */
  188. function testAllowSimulatedFields() {
  189. $TestModel = new ValidationTest1();
  190. $TestModel->create(array(
  191. 'title' => 'foo',
  192. 'bar' => 'baz'
  193. ));
  194. $expected = array(
  195. 'ValidationTest1' => array(
  196. 'title' => 'foo',
  197. 'bar' => 'baz'
  198. ));
  199. $this->assertEqual($TestModel->data, $expected);
  200. }
  201. /**
  202. * test that Caches are getting cleared on save().
  203. * ensure that both inflections of controller names are getting cleared
  204. * as url for controller could be either overallFavorites/index or overall_favorites/index
  205. *
  206. * @return void
  207. */
  208. function testCacheClearOnSave() {
  209. $_back = array(
  210. 'check' => Configure::read('Cache.check'),
  211. 'disable' => Configure::read('Cache.disable'),
  212. );
  213. Configure::write('Cache.check', true);
  214. Configure::write('Cache.disable', false);
  215. $this->loadFixtures('OverallFavorite');
  216. $OverallFavorite = new OverallFavorite();
  217. touch(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php');
  218. touch(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php');
  219. $data = array(
  220. 'OverallFavorite' => array(
  221. 'id' => 22,
  222. 'model_type' => '8-track',
  223. 'model_id' => '3',
  224. 'priority' => '1'
  225. )
  226. );
  227. $OverallFavorite->create($data);
  228. $OverallFavorite->save();
  229. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php'));
  230. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php'));
  231. Configure::write('Cache.check', $_back['check']);
  232. Configure::write('Cache.disable', $_back['disable']);
  233. }
  234. /**
  235. * testSaveWithCounterCache method
  236. *
  237. * @access public
  238. * @return void
  239. */
  240. function testSaveWithCounterCache() {
  241. $this->loadFixtures('Syfile', 'Item', 'Image', 'Portfolio', 'ItemsPortfolio');
  242. $TestModel = new Syfile();
  243. $TestModel2 = new Item();
  244. $result = $TestModel->findById(1);
  245. $this->assertIdentical($result['Syfile']['item_count'], null);
  246. $TestModel2->save(array(
  247. 'name' => 'Item 7',
  248. 'syfile_id' => 1,
  249. 'published' => false
  250. ));
  251. $result = $TestModel->findById(1);
  252. $this->assertEquals($result['Syfile']['item_count'], 2);
  253. $TestModel2->delete(1);
  254. $result = $TestModel->findById(1);
  255. $this->assertEquals($result['Syfile']['item_count'], 1);
  256. $TestModel2->id = 2;
  257. $TestModel2->saveField('syfile_id', 1);
  258. $result = $TestModel->findById(1);
  259. $this->assertEquals($result['Syfile']['item_count'], 2);
  260. $result = $TestModel->findById(2);
  261. $this->assertEquals($result['Syfile']['item_count'], 0);
  262. }
  263. /**
  264. * Tests that counter caches are updated when records are added
  265. *
  266. * @access public
  267. * @return void
  268. */
  269. function testCounterCacheIncrease() {
  270. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  271. $User = new CounterCacheUser();
  272. $Post = new CounterCachePost();
  273. $data = array('Post' => array(
  274. 'id' => 22,
  275. 'title' => 'New Post',
  276. 'user_id' => 66
  277. ));
  278. $Post->save($data);
  279. $user = $User->find('first', array(
  280. 'conditions' => array('id' => 66),
  281. 'recursive' => -1
  282. ));
  283. $result = $user[$User->alias]['post_count'];
  284. $expected = 3;
  285. $this->assertEqual($result, $expected);
  286. }
  287. /**
  288. * Tests that counter caches are updated when records are deleted
  289. *
  290. * @access public
  291. * @return void
  292. */
  293. function testCounterCacheDecrease() {
  294. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  295. $User = new CounterCacheUser();
  296. $Post = new CounterCachePost();
  297. $Post->delete(2);
  298. $user = $User->find('first', array(
  299. 'conditions' => array('id' => 66),
  300. 'recursive' => -1
  301. ));
  302. $result = $user[$User->alias]['post_count'];
  303. $expected = 1;
  304. $this->assertEqual($result, $expected);
  305. }
  306. /**
  307. * Tests that counter caches are updated when foreign keys of counted records change
  308. *
  309. * @access public
  310. * @return void
  311. */
  312. function testCounterCacheUpdated() {
  313. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  314. $User = new CounterCacheUser();
  315. $Post = new CounterCachePost();
  316. $data = $Post->find('first', array(
  317. 'conditions' => array('id' => 1),
  318. 'recursive' => -1
  319. ));
  320. $data[$Post->alias]['user_id'] = 301;
  321. $Post->save($data);
  322. $users = $User->find('all',array('order' => 'User.id'));
  323. $this->assertEqual($users[0]['User']['post_count'], 1);
  324. $this->assertEqual($users[1]['User']['post_count'], 2);
  325. }
  326. /**
  327. * Test counter cache with models that use a non-standard (i.e. not using 'id')
  328. * as their primary key.
  329. *
  330. * @access public
  331. * @return void
  332. */
  333. function testCounterCacheWithNonstandardPrimaryKey() {
  334. $this->loadFixtures(
  335. 'CounterCacheUserNonstandardPrimaryKey',
  336. 'CounterCachePostNonstandardPrimaryKey'
  337. );
  338. $User = new CounterCacheUserNonstandardPrimaryKey();
  339. $Post = new CounterCachePostNonstandardPrimaryKey();
  340. $data = $Post->find('first', array(
  341. 'conditions' => array('pid' => 1),
  342. 'recursive' => -1
  343. ));
  344. $data[$Post->alias]['uid'] = 301;
  345. $Post->save($data);
  346. $users = $User->find('all',array('order' => 'User.uid'));
  347. $this->assertEqual($users[0]['User']['post_count'], 1);
  348. $this->assertEqual($users[1]['User']['post_count'], 2);
  349. }
  350. /**
  351. * test Counter Cache With Self Joining table
  352. *
  353. * @return void
  354. */
  355. public function testCounterCacheWithSelfJoin() {
  356. $skip = $this->skipIf(
  357. ($this->db instanceof Sqlite),
  358. 'SQLite 2.x does not support ALTER TABLE ADD COLUMN'
  359. );
  360. if ($skip) {
  361. return;
  362. }
  363. $this->loadFixtures('CategoryThread');
  364. $this->db->query('ALTER TABLE '. $this->db->fullTableName('category_threads') . " ADD COLUMN child_count INTEGER");
  365. $Category = new CategoryThread();
  366. $result = $Category->updateAll(array('CategoryThread.name' => "'updated'"), array('CategoryThread.parent_id' => 5));
  367. $this->assertFalse(empty($result));
  368. $Category = new CategoryThread();
  369. $Category->belongsTo['ParentCategory']['counterCache'] = 'child_count';
  370. $Category->updateCounterCache(array('parent_id' => 5));
  371. $result = Set::extract($Category->find('all', array('conditions' => array('CategoryThread.id' => 5))), '{n}.CategoryThread.child_count');
  372. $expected = array_fill(0, 1, 1);
  373. $this->assertEqual($result, $expected);
  374. }
  375. /**
  376. * testSaveWithCounterCacheScope method
  377. *
  378. * @access public
  379. * @return void
  380. */
  381. function testSaveWithCounterCacheScope() {
  382. $this->loadFixtures('Syfile', 'Item', 'Image', 'ItemsPortfolio', 'Portfolio');
  383. $TestModel = new Syfile();
  384. $TestModel2 = new Item();
  385. $TestModel2->belongsTo['Syfile']['counterCache'] = true;
  386. $TestModel2->belongsTo['Syfile']['counterScope'] = array('published' => true);
  387. $result = $TestModel->findById(1);
  388. $this->assertIdentical($result['Syfile']['item_count'], null);
  389. $TestModel2->save(array(
  390. 'name' => 'Item 7',
  391. 'syfile_id' => 1,
  392. 'published'=> true
  393. ));
  394. $result = $TestModel->findById(1);
  395. $this->assertEquals($result['Syfile']['item_count'], 1);
  396. $TestModel2->id = 1;
  397. $TestModel2->saveField('published', true);
  398. $result = $TestModel->findById(1);
  399. $this->assertEquals($result['Syfile']['item_count'], 2);
  400. $TestModel2->save(array(
  401. 'id' => 1,
  402. 'syfile_id' => 1,
  403. 'published'=> false
  404. ));
  405. $result = $TestModel->findById(1);
  406. $this->assertEquals($result['Syfile']['item_count'], 1);
  407. }
  408. /**
  409. * test that beforeValidate returning false can abort saves.
  410. *
  411. * @return void
  412. */
  413. function testBeforeValidateSaveAbortion() {
  414. $this->loadFixtures('Post');
  415. $Model = new CallbackPostTestModel();
  416. $Model->beforeValidateReturn = false;
  417. $data = array(
  418. 'title' => 'new article',
  419. 'body' => 'this is some text.'
  420. );
  421. $Model->create();
  422. $result = $Model->save($data);
  423. $this->assertFalse($result);
  424. }
  425. /**
  426. * test that beforeSave returning false can abort saves.
  427. *
  428. * @return void
  429. */
  430. function testBeforeSaveSaveAbortion() {
  431. $this->loadFixtures('Post');
  432. $Model = new CallbackPostTestModel();
  433. $Model->beforeSaveReturn = false;
  434. $data = array(
  435. 'title' => 'new article',
  436. 'body' => 'this is some text.'
  437. );
  438. $Model->create();
  439. $result = $Model->save($data);
  440. $this->assertFalse($result);
  441. }
  442. /**
  443. * testSaveField method
  444. *
  445. * @access public
  446. * @return void
  447. */
  448. function testSaveField() {
  449. $this->loadFixtures('Article');
  450. $TestModel = new Article();
  451. $TestModel->id = 1;
  452. $result = $TestModel->saveField('title', 'New First Article');
  453. $this->assertFalse(empty($result));
  454. $TestModel->recursive = -1;
  455. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  456. $expected = array('Article' => array(
  457. 'id' => '1',
  458. 'user_id' => '1',
  459. 'title' => 'New First Article',
  460. 'body' => 'First Article Body'
  461. ));
  462. $this->assertEqual($result, $expected);
  463. $TestModel->id = 1;
  464. $result = $TestModel->saveField('title', '');
  465. $this->assertFalse(empty($result));
  466. $TestModel->recursive = -1;
  467. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  468. $expected = array('Article' => array(
  469. 'id' => '1',
  470. 'user_id' => '1',
  471. 'title' => '',
  472. 'body' => 'First Article Body'
  473. ));
  474. $result['Article']['title'] = trim($result['Article']['title']);
  475. $this->assertEqual($result, $expected);
  476. $TestModel->id = 1;
  477. $TestModel->set('body', 'Messed up data');
  478. $result = $TestModel->saveField('title', 'First Article');
  479. $this->assertFalse(empty($result));
  480. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  481. $expected = array('Article' => array(
  482. 'id' => '1',
  483. 'user_id' => '1',
  484. 'title' => 'First Article',
  485. 'body' => 'First Article Body'
  486. ));
  487. $this->assertEqual($result, $expected);
  488. $TestModel->recursive = -1;
  489. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  490. $TestModel->id = 1;
  491. $result = $TestModel->saveField('title', '', true);
  492. $this->assertFalse($result);
  493. $this->loadFixtures('Node', 'Dependency');
  494. $Node = new Node();
  495. $Node->set('id', 1);
  496. $result = $Node->read();
  497. $this->assertEqual(Set::extract('/ParentNode/name', $result), array('Second'));
  498. $Node->saveField('state', 10);
  499. $result = $Node->read();
  500. $this->assertEqual(Set::extract('/ParentNode/name', $result), array('Second'));
  501. }
  502. /**
  503. * testSaveWithCreate method
  504. *
  505. * @access public
  506. * @return void
  507. */
  508. function testSaveWithCreate() {
  509. $this->loadFixtures(
  510. 'User',
  511. 'Article',
  512. 'User',
  513. 'Comment',
  514. 'Tag',
  515. 'ArticlesTag',
  516. 'Attachment'
  517. );
  518. $TestModel = new User();
  519. $data = array('User' => array(
  520. 'user' => 'user',
  521. 'password' => ''
  522. ));
  523. $result = $TestModel->save($data);
  524. $this->assertFalse($result);
  525. $this->assertTrue(!empty($TestModel->validationErrors));
  526. $TestModel = new Article();
  527. $data = array('Article' => array(
  528. 'user_id' => '',
  529. 'title' => '',
  530. 'body' => ''
  531. ));
  532. $result = $TestModel->create($data) && $TestModel->save();
  533. $this->assertFalse($result);
  534. $this->assertTrue(!empty($TestModel->validationErrors));
  535. $data = array('Article' => array(
  536. 'id' => 1,
  537. 'user_id' => '1',
  538. 'title' => 'New First Article',
  539. 'body' => ''
  540. ));
  541. $result = $TestModel->create($data) && $TestModel->save();
  542. $this->assertFalse($result);
  543. $data = array('Article' => array(
  544. 'id' => 1,
  545. 'title' => 'New First Article'
  546. ));
  547. $result = $TestModel->create() && $TestModel->save($data, false);
  548. $this->assertFalse(empty($result));
  549. $TestModel->recursive = -1;
  550. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
  551. $expected = array('Article' => array(
  552. 'id' => '1',
  553. 'user_id' => '1',
  554. 'title' => 'New First Article',
  555. 'body' => 'First Article Body',
  556. 'published' => 'N'
  557. ));
  558. $this->assertEqual($result, $expected);
  559. $data = array('Article' => array(
  560. 'id' => 1,
  561. 'user_id' => '2',
  562. 'title' => 'First Article',
  563. 'body' => 'New First Article Body',
  564. 'published' => 'Y'
  565. ));
  566. $result = $TestModel->create() && $TestModel->save($data, true, array('id', 'title', 'published'));
  567. $this->assertFalse(empty($result));
  568. $TestModel->recursive = -1;
  569. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
  570. $expected = array('Article' => array(
  571. 'id' => '1',
  572. 'user_id' => '1',
  573. 'title' => 'First Article',
  574. 'body' => 'First Article Body',
  575. 'published' => 'Y'
  576. ));
  577. $this->assertEqual($result, $expected);
  578. $data = array(
  579. 'Article' => array(
  580. 'user_id' => '2',
  581. 'title' => 'New Article',
  582. 'body' => 'New Article Body',
  583. 'created' => '2007-03-18 14:55:23',
  584. 'updated' => '2007-03-18 14:57:31'
  585. ),
  586. 'Tag' => array('Tag' => array(1, 3))
  587. );
  588. $TestModel->create();
  589. $result = $TestModel->create() && $TestModel->save($data);
  590. $this->assertFalse(empty($result));
  591. $TestModel->recursive = 2;
  592. $result = $TestModel->read(null, 4);
  593. $expected = array(
  594. 'Article' => array(
  595. 'id' => '4',
  596. 'user_id' => '2',
  597. 'title' => 'New Article',
  598. 'body' => 'New Article Body',
  599. 'published' => 'N',
  600. 'created' => '2007-03-18 14:55:23',
  601. 'updated' => '2007-03-18 14:57:31'
  602. ),
  603. 'User' => array(
  604. 'id' => '2',
  605. 'user' => 'nate',
  606. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  607. 'created' => '2007-03-17 01:18:23',
  608. 'updated' => '2007-03-17 01:20:31'
  609. ),
  610. 'Comment' => array(),
  611. 'Tag' => array(
  612. array(
  613. 'id' => '1',
  614. 'tag' => 'tag1',
  615. 'created' => '2007-03-18 12:22:23',
  616. 'updated' => '2007-03-18 12:24:31'
  617. ),
  618. array(
  619. 'id' => '3',
  620. 'tag' => 'tag3',
  621. 'created' => '2007-03-18 12:26:23',
  622. 'updated' => '2007-03-18 12:28:31'
  623. )));
  624. $this->assertEqual($result, $expected);
  625. $data = array('Comment' => array(
  626. 'article_id' => '4',
  627. 'user_id' => '1',
  628. 'comment' => 'Comment New Article',
  629. 'published' => 'Y',
  630. 'created' => '2007-03-18 14:57:23',
  631. 'updated' => '2007-03-18 14:59:31'
  632. ));
  633. $result = $TestModel->Comment->create() && $TestModel->Comment->save($data);
  634. $this->assertFalse(empty($result));
  635. $data = array('Attachment' => array(
  636. 'comment_id' => '7',
  637. 'attachment' => 'newattachment.zip',
  638. 'created' => '2007-03-18 15:02:23',
  639. 'updated' => '2007-03-18 15:04:31'
  640. ));
  641. $result = $TestModel->Comment->Attachment->save($data);
  642. $this->assertFalse(empty($result));
  643. $TestModel->recursive = 2;
  644. $result = $TestModel->read(null, 4);
  645. $expected = array(
  646. 'Article' => array(
  647. 'id' => '4',
  648. 'user_id' => '2',
  649. 'title' => 'New Article',
  650. 'body' => 'New Article Body',
  651. 'published' => 'N',
  652. 'created' => '2007-03-18 14:55:23',
  653. 'updated' => '2007-03-18 14:57:31'
  654. ),
  655. 'User' => array(
  656. 'id' => '2',
  657. 'user' => 'nate',
  658. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  659. 'created' => '2007-03-17 01:18:23',
  660. 'updated' => '2007-03-17 01:20:31'
  661. ),
  662. 'Comment' => array(
  663. array(
  664. 'id' => '7',
  665. 'article_id' => '4',
  666. 'user_id' => '1',
  667. 'comment' => 'Comment New Article',
  668. 'published' => 'Y',
  669. 'created' => '2007-03-18 14:57:23',
  670. 'updated' => '2007-03-18 14:59:31',
  671. 'Article' => array(
  672. 'id' => '4',
  673. 'user_id' => '2',
  674. 'title' => 'New Article',
  675. 'body' => 'New Article Body',
  676. 'published' => 'N',
  677. 'created' => '2007-03-18 14:55:23',
  678. 'updated' => '2007-03-18 14:57:31'
  679. ),
  680. 'User' => array(
  681. 'id' => '1',
  682. 'user' => 'mariano',
  683. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  684. 'created' => '2007-03-17 01:16:23',
  685. 'updated' => '2007-03-17 01:18:31'
  686. ),
  687. 'Attachment' => array(
  688. 'id' => '2',
  689. 'comment_id' => '7',
  690. 'attachment' => 'newattachment.zip',
  691. 'created' => '2007-03-18 15:02:23',
  692. 'updated' => '2007-03-18 15:04:31'
  693. ))),
  694. 'Tag' => array(
  695. array(
  696. 'id' => '1',
  697. 'tag' => 'tag1',
  698. 'created' => '2007-03-18 12:22:23',
  699. 'updated' => '2007-03-18 12:24:31'
  700. ),
  701. array(
  702. 'id' => '3',
  703. 'tag' => 'tag3',
  704. 'created' => '2007-03-18 12:26:23',
  705. 'updated' => '2007-03-18 12:28:31'
  706. )));
  707. $this->assertEqual($result, $expected);
  708. }
  709. /**
  710. * test that a null Id doesn't cause errors
  711. *
  712. * @return void
  713. */
  714. function testSaveWithNullId() {
  715. $this->loadFixtures('User');
  716. $User = new User();
  717. $User->read(null, 1);
  718. $User->data['User']['id'] = null;
  719. $result = $User->save(array('password' => 'test'));
  720. $this->assertFalse(empty($result));
  721. $this->assertTrue($User->id > 0);
  722. $User->read(null, 2);
  723. $User->data['User']['id'] = null;
  724. $result = $User->save(array('password' => 'test'));
  725. $this->assertFalse(empty($result));
  726. $this->assertTrue($User->id > 0);
  727. $User->data['User'] = array('password' => 'something');
  728. $result = $User->save();
  729. $this->assertFalse(empty($result));
  730. $result = $User->read();
  731. $this->assertEqual($User->data['User']['password'], 'something');
  732. }
  733. /**
  734. * testSaveWithSet method
  735. *
  736. * @access public
  737. * @return void
  738. */
  739. function testSaveWithSet() {
  740. $this->loadFixtures('Article');
  741. $TestModel = new Article();
  742. // Create record we will be updating later
  743. $data = array('Article' => array(
  744. 'user_id' => '1',
  745. 'title' => 'Fourth Article',
  746. 'body' => 'Fourth Article Body',
  747. 'published' => 'Y'
  748. ));
  749. $result = $TestModel->create() && $TestModel->save($data);
  750. $this->assertFalse(empty($result));
  751. // Check record we created
  752. $TestModel->recursive = -1;
  753. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  754. $expected = array('Article' => array(
  755. 'id' => '4',
  756. 'user_id' => '1',
  757. 'title' => 'Fourth Article',
  758. 'body' => 'Fourth Article Body',
  759. 'published' => 'Y'
  760. ));
  761. $this->assertEqual($result, $expected);
  762. // Create new record just to overlap Model->id on previously created record
  763. $data = array('Article' => array(
  764. 'user_id' => '4',
  765. 'title' => 'Fifth Article',
  766. 'body' => 'Fifth Article Body',
  767. 'published' => 'Y'
  768. ));
  769. $result = $TestModel->create() && $TestModel->save($data);
  770. $this->assertFalse(empty($result));
  771. $TestModel->recursive = -1;
  772. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  773. $expected = array('Article' => array(
  774. 'id' => '5',
  775. 'user_id' => '4',
  776. 'title' => 'Fifth Article',
  777. 'body' => 'Fifth Article Body',
  778. 'published' => 'Y'
  779. ));
  780. $this->assertEqual($result, $expected);
  781. // Go back and edit the first article we created, starting by checking it's still there
  782. $TestModel->recursive = -1;
  783. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  784. $expected = array('Article' => array(
  785. 'id' => '4',
  786. 'user_id' => '1',
  787. 'title' => 'Fourth Article',
  788. 'body' => 'Fourth Article Body',
  789. 'published' => 'Y'
  790. ));
  791. $this->assertEqual($result, $expected);
  792. // And now do the update with set()
  793. $data = array('Article' => array(
  794. 'id' => '4',
  795. 'title' => 'Fourth Article - New Title',
  796. 'published' => 'N'
  797. ));
  798. $result = $TestModel->set($data) && $TestModel->save();
  799. $this->assertFalse(empty($result));
  800. $TestModel->recursive = -1;
  801. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  802. $expected = array('Article' => array(
  803. 'id' => '4',
  804. 'user_id' => '1',
  805. 'title' => 'Fourth Article - New Title',
  806. 'body' => 'Fourth Article Body',
  807. 'published' => 'N'
  808. ));
  809. $this->assertEqual($result, $expected);
  810. $TestModel->recursive = -1;
  811. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  812. $expected = array('Article' => array(
  813. 'id' => '5',
  814. 'user_id' => '4',
  815. 'title' => 'Fifth Article',
  816. 'body' => 'Fifth Article Body',
  817. 'published' => 'Y'
  818. ));
  819. $this->assertEqual($result, $expected);
  820. $data = array('Article' => array('id' => '5', 'title' => 'Fifth Article - New Title 5'));
  821. $result = ($TestModel->set($data) && $TestModel->save());
  822. $this->assertFalse(empty($result));
  823. $TestModel->recursive = -1;
  824. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  825. $expected = array('Article' => array(
  826. 'id' => '5',
  827. 'user_id' => '4',
  828. 'title' => 'Fifth Article - New Title 5',
  829. 'body' => 'Fifth Article Body',
  830. 'published' => 'Y'
  831. ));
  832. $this->assertEqual($result, $expected);
  833. $TestModel->recursive = -1;
  834. $result = $TestModel->find('all', array('fields' => array('id', 'title')));
  835. $expected = array(
  836. array('Article' => array('id' => 1, 'title' => 'First Article' )),
  837. array('Article' => array('id' => 2, 'title' => 'Second Article' )),
  838. array('Article' => array('id' => 3, 'title' => 'Third Article' )),
  839. array('Article' => array('id' => 4, 'title' => 'Fourth Article - New Title' )),
  840. array('Article' => array('id' => 5, 'title' => 'Fifth Article - New Title 5' ))
  841. );
  842. $this->assertEqual($result, $expected);
  843. }
  844. /**
  845. * testSaveWithNonExistentFields method
  846. *
  847. * @access public
  848. * @return void
  849. */
  850. function testSaveWithNonExistentFields() {
  851. $this->loadFixtures('Article');
  852. $TestModel = new Article();
  853. $TestModel->recursive = -1;
  854. $data = array(
  855. 'non_existent' => 'This field does not exist',
  856. 'user_id' => '1',
  857. 'title' => 'Fourth Article - New Title',
  858. 'body' => 'Fourth Article Body',
  859. 'published' => 'N'
  860. );
  861. $result = $TestModel->create() && $TestModel->save($data);
  862. $this->assertFalse(empty($result));
  863. $expected = array('Article' => array(
  864. 'id' => '4',
  865. 'user_id' => '1',
  866. 'title' => 'Fourth Article - New Title',
  867. 'body' => 'Fourth Article Body',
  868. 'published' => 'N'
  869. ));
  870. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  871. $this->assertEqual($result, $expected);
  872. $data = array(
  873. 'user_id' => '1',
  874. 'non_existent' => 'This field does not exist',
  875. 'title' => 'Fiveth Article - New Title',
  876. 'body' => 'Fiveth Article Body',
  877. 'published' => 'N'
  878. );
  879. $result = $TestModel->create() && $TestModel->save($data);
  880. $this->assertFalse(empty($result));
  881. $expected = array('Article' => array(
  882. 'id' => '5',
  883. 'user_id' => '1',
  884. 'title' => 'Fiveth Article - New Title',
  885. 'body' => 'Fiveth Article Body',
  886. 'published' => 'N'
  887. ));
  888. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  889. $this->assertEqual($result, $expected);
  890. }
  891. /**
  892. * testSaveFromXml method
  893. *
  894. * @access public
  895. * @return void
  896. */
  897. function testSaveFromXml() {
  898. $this->markTestSkipped('This feature needs to be fixed or dropped');
  899. $this->loadFixtures('Article');
  900. App::uses('Xml', 'Utility');
  901. $Article = new Article();
  902. $result = $Article->save(Xml::build('<article title="test xml" user_id="5" />'));
  903. $this->assertFalse(empty($result));
  904. $results = $Article->find('first', array('conditions' => array('Article.title' => 'test xml')));
  905. $this->assertFalse(empty($results));
  906. $result = $Article->save(Xml::build('<article><title>testing</title><user_id>6</user_id></article>'));
  907. $this->assertFalse(empty($result));
  908. $results = $Article->find('first', array('conditions' => array('Article.title' => 'testing')));
  909. $this->assertFalse(empty($results));
  910. $result = $Article->save(Xml::build('<article><title>testing with DOMDocument</title><user_id>7</user_id></article>', array('return' => 'domdocument')));
  911. $this->assertFalse(empty($result));
  912. $results = $Article->find('first', array('conditions' => array('Article.title' => 'testing with DOMDocument')));
  913. $this->assertFalse(empty($results));
  914. }
  915. /**
  916. * testSaveHabtm method
  917. *
  918. * @access public
  919. * @return void
  920. */
  921. function testSaveHabtm() {
  922. $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
  923. $TestModel = new Article();
  924. $result = $TestModel->findById(2);
  925. $expected = array(
  926. 'Article' => array(
  927. 'id' => '2',
  928. 'user_id' => '3',
  929. 'title' => 'Second Article',
  930. 'body' => 'Second Article Body',
  931. 'published' => 'Y',
  932. 'created' => '2007-03-18 10:41:23',
  933. 'updated' => '2007-03-18 10:43:31'
  934. ),
  935. 'User' => array(
  936. 'id' => '3',
  937. 'user' => 'larry',
  938. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  939. 'created' => '2007-03-17 01:20:23',
  940. 'updated' => '2007-03-17 01:22:31'
  941. ),
  942. 'Comment' => array(
  943. array(
  944. 'id' => '5',
  945. 'article_id' => '2',
  946. 'user_id' => '1',
  947. 'comment' => 'First Comment for Second Article',
  948. 'published' => 'Y',
  949. 'created' => '2007-03-18 10:53:23',
  950. 'updated' => '2007-03-18 10:55:31'
  951. ),
  952. array(
  953. 'id' => '6',
  954. 'article_id' => '2',
  955. 'user_id' => '2',
  956. 'comment' => 'Second Comment for Second Article',
  957. 'published' => 'Y',
  958. 'created' => '2007-03-18 10:55:23',
  959. 'updated' => '2007-03-18 10:57:31'
  960. )),
  961. 'Tag' => array(
  962. array(
  963. 'id' => '1',
  964. 'tag' => 'tag1',
  965. 'created' => '2007-03-18 12:22:23',
  966. 'updated' => '2007-03-18 12:24:31'
  967. ),
  968. array(
  969. 'id' => '3',
  970. 'tag' => 'tag3',
  971. 'created' => '2007-03-18 12:26:23',
  972. 'updated' => '2007-03-18 12:28:31'
  973. )
  974. )
  975. );
  976. $this->assertEqual($result, $expected);
  977. $data = array(
  978. 'Article' => array(
  979. 'id' => '2',
  980. 'title' => 'New Second Article'
  981. ),
  982. 'Tag' => array('Tag' => array(1, 2))
  983. );
  984. $result = $TestModel->set($data);
  985. $this->assertFalse(empty($result));
  986. $result = $TestModel->save();
  987. $this->assertFalse(empty($result));
  988. $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
  989. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  990. $expected = array(
  991. 'Article' => array(
  992. 'id' => '2',
  993. 'user_id' => '3',
  994. 'title' => 'New Second Article',
  995. 'body' => 'Second Article Body'
  996. ),
  997. 'Tag' => array(
  998. array(
  999. 'id' => '1',
  1000. 'tag' => 'tag1',
  1001. 'created' => '2007-03-18 12:22:23',
  1002. 'updated' => '2007-03-18 12:24:31'
  1003. ),
  1004. array(
  1005. 'id' => '2',
  1006. 'tag' => 'tag2',
  1007. 'created' => '2007-03-18 12:24:23',
  1008. 'updated' => '2007-03-18 12:26:31'
  1009. )));
  1010. $this->assertEqual($result, $expected);
  1011. $data = array('Article' => array('id' => '2'), 'Tag' => array('Tag' => array(2, 3)));
  1012. $result = $TestModel->set($data);
  1013. $this->assertFalse(empty($result));
  1014. $result = $TestModel->save();
  1015. $this->assertFalse(empty($result));
  1016. $TestModel->unbindModel(array(
  1017. 'belongsTo' => array('User'),
  1018. 'hasMany' => array('Comment')
  1019. ));
  1020. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1021. $expected = array(
  1022. 'Article' => array(
  1023. 'id' => '2',
  1024. 'user_id' => '3',
  1025. 'title' => 'New Second Article',
  1026. 'body' => 'Second Article Body'
  1027. ),
  1028. 'Tag' => array(
  1029. array(
  1030. 'id' => '2',
  1031. 'tag' => 'tag2',
  1032. 'created' => '2007-03-18 12:24:23',
  1033. 'updated' => '2007-03-18 12:26:31'
  1034. ),
  1035. array(
  1036. 'id' => '3',
  1037. 'tag' => 'tag3',
  1038. 'created' => '2007-03-18 12:26:23',
  1039. 'updated' => '2007-03-18 12:28:31'
  1040. )));
  1041. $this->assertEqual($result, $expected);
  1042. $data = array('Tag' => array('Tag' => array(1, 2, 3)));
  1043. $result = $TestModel->set($data);
  1044. $this->assertFalse(empty($result));
  1045. $result = $TestModel->save();
  1046. $this->assertFalse(empty($result));
  1047. $TestModel->unbindModel(array(
  1048. 'belongsTo' => array('User'),
  1049. 'hasMany' => array('Comment')
  1050. ));
  1051. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1052. $expected = array(
  1053. 'Article' => array(
  1054. 'id' => '2',
  1055. 'user_id' => '3',
  1056. 'title' => 'New Second Article',
  1057. 'body' => 'Second Article Body'
  1058. ),
  1059. 'Tag' => array(
  1060. array(
  1061. 'id' => '1',
  1062. 'tag' => 'tag1',
  1063. 'created' => '2007-03-18 12:22:23',
  1064. 'updated' => '2007-03-18 12:24:31'
  1065. ),
  1066. array(
  1067. 'id' => '2',
  1068. 'tag' => 'tag2',
  1069. 'created' => '2007-03-18 12:24:23',
  1070. 'updated' => '2007-03-18 12:26:31'
  1071. ),
  1072. array(
  1073. 'id' => '3',
  1074. 'tag' => 'tag3',
  1075. 'created' => '2007-03-18 12:26:23',
  1076. 'updated' => '2007-03-18 12:28:31'
  1077. )));
  1078. $this->assertEqual($result, $expected);
  1079. $data = array('Tag' => array('Tag' => array()));
  1080. $result = $TestModel->set($data);
  1081. $this->assertFalse(empty($result));
  1082. $result = $TestModel->save();
  1083. $this->assertFalse(empty($result));
  1084. $data = array('Tag' => array('Tag' => ''));
  1085. $result = $TestModel->set($data);
  1086. $this->assertFalse(empty($result));
  1087. $result = $TestModel->save();
  1088. $this->assertFalse(empty($result));
  1089. $TestModel->unbindModel(array(
  1090. 'belongsTo' => array('User'),
  1091. 'hasMany' => array('Comment')
  1092. ));
  1093. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1094. $expected = array(
  1095. 'Article' => array(
  1096. 'id' => '2',
  1097. 'user_id' => '3',
  1098. 'title' => 'New Second Article',
  1099. 'body' => 'Second Article Body'
  1100. ),
  1101. 'Tag' => array()
  1102. );
  1103. $this->assertEqual($result, $expected);
  1104. $data = array('Tag' => array('Tag' => array(2, 3)));
  1105. $result = $TestModel->set($data);
  1106. $this->assertFalse(empty($result));
  1107. $result = $TestModel->save();
  1108. $this->assertFalse(empty($result));
  1109. $TestModel->unbindModel(array(
  1110. 'belongsTo' => array('User'),
  1111. 'hasMany' => array('Comment')
  1112. ));
  1113. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1114. $expected = array(
  1115. 'Article' => array(
  1116. 'id' => '2',
  1117. 'user_id' => '3',
  1118. 'title' => 'New Second Article',
  1119. 'body' => 'Second Article Body'
  1120. ),
  1121. 'Tag' => array(
  1122. array(
  1123. 'id' => '2',
  1124. 'tag' => 'tag2',
  1125. 'created' => '2007-03-18 12:24:23',
  1126. 'updated' => '2007-03-18 12:26:31'
  1127. ),
  1128. array(
  1129. 'id' => '3',
  1130. 'tag' => 'tag3',
  1131. 'created' => '2007-03-18 12:26:23',
  1132. 'updated' => '2007-03-18 12:28:31'
  1133. )));
  1134. $this->assertEqual($result, $expected);
  1135. $data = array(
  1136. 'Tag' => array(
  1137. 'Tag' => array(1, 2)
  1138. ),
  1139. 'Article' => array(
  1140. 'id' => '2',
  1141. 'title' => 'New Second Article'
  1142. ));
  1143. $result = $TestModel->set($data);
  1144. $this->assertFalse(empty($result));
  1145. $result = $TestModel->save();
  1146. $this->assertFalse(empty($result));
  1147. $TestModel->unbindModel(array(
  1148. 'belongsTo' => array('User'),
  1149. 'hasMany' => array('Comment')
  1150. ));
  1151. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1152. $expected = array(
  1153. 'Article' => array(
  1154. 'id' => '2',
  1155. 'user_id' => '3',
  1156. 'title' => 'New Second Article',
  1157. 'body' => 'Second Article Body'
  1158. ),
  1159. 'Tag' => array(
  1160. array(
  1161. 'id' => '1',
  1162. 'tag' => 'tag1',
  1163. 'created' => '2007-03-18 12:22:23',
  1164. 'updated' => '2007-03-18 12:24:31'
  1165. ),
  1166. array(
  1167. 'id' => '2',
  1168. 'tag' => 'tag2',
  1169. 'created' => '2007-03-18 12:24:23',
  1170. 'updated' => '2007-03-18 12:26:31'
  1171. )));
  1172. $this->assertEqual($result, $expected);
  1173. $data = array(
  1174. 'Tag' => array(
  1175. 'Tag' => array(1, 2)
  1176. ),
  1177. 'Article' => array(
  1178. 'id' => '2',
  1179. 'title' => 'New Second Article Title'
  1180. ));
  1181. $result = $TestModel->set($data);
  1182. $this->assertFalse(empty($result));
  1183. $result = $TestModel->save();
  1184. $this->assertFalse(empty($result));
  1185. $TestModel->unbindModel(array(
  1186. 'belongsTo' => array('User'),
  1187. 'hasMany' => array('Comment')
  1188. ));
  1189. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1190. $expected = array(
  1191. 'Article' => array(
  1192. 'id' => '2',
  1193. 'user_id' => '3',
  1194. 'title' => 'New Second Article Title',
  1195. 'body' => 'Second Article Body'
  1196. ),
  1197. 'Tag' => array(
  1198. array(
  1199. 'id' => '1',
  1200. 'tag' => 'tag1',
  1201. 'created' => '2007-03-18 12:22:23',
  1202. 'updated' => '2007-03-18 12:24:31'
  1203. ),
  1204. array(
  1205. 'id' => '2',
  1206. 'tag' => 'tag2',
  1207. 'created' => '2007-03-18 12:24:23',
  1208. 'updated' => '2007-03-18 12:26:31'
  1209. )
  1210. )
  1211. );
  1212. $this->assertEqual($result, $expected);
  1213. $data = array(
  1214. 'Tag' => array(
  1215. 'Tag' => array(2, 3)
  1216. ),
  1217. 'Article' => array(
  1218. 'id' => '2',
  1219. 'title' => 'Changed Second Article'
  1220. ));
  1221. $result = $TestModel->set($data);
  1222. $this->assertFalse(empty($result));
  1223. $result = $TestModel->save();
  1224. $this->assertFalse(empty($result));
  1225. $TestModel->unbindModel(array(
  1226. 'belongsTo' => array('User'),
  1227. 'hasMany' => array('Comment')
  1228. ));
  1229. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1230. $expected = array(
  1231. 'Article' => array(
  1232. 'id' => '2',
  1233. 'user_id' => '3',
  1234. 'title' => 'Changed Second Article',
  1235. 'body' => 'Second Article Body'
  1236. ),
  1237. 'Tag' => array(
  1238. array(
  1239. 'id' => '2',
  1240. 'tag' => 'tag2',
  1241. 'created' => '2007-03-18 12:24:23',
  1242. 'updated' => '2007-03-18 12:26:31'
  1243. ),
  1244. array(
  1245. 'id' => '3',
  1246. 'tag' => 'tag3',
  1247. 'created' => '2007-03-18 12:26:23',
  1248. 'updated' => '2007-03-18 12:28:31'
  1249. )
  1250. )
  1251. );
  1252. $this->assertEqual($result, $expected);
  1253. $data = array(
  1254. 'Tag' => array(
  1255. 'Tag' => array(1, 3)
  1256. ),
  1257. 'Article' => array('id' => '2'),
  1258. );
  1259. $result = $TestModel->set($data);
  1260. $this->assertFalse(empty($result));
  1261. $result = $TestModel->save();
  1262. $this->assertFalse(empty($result));
  1263. $TestModel->unbindModel(array(
  1264. 'belongsTo' => array('User'),
  1265. 'hasMany' => array('Comment')
  1266. ));
  1267. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1268. $expected = array(
  1269. 'Article' => array(
  1270. 'id' => '2',
  1271. 'user_id' => '3',
  1272. 'title' => 'Changed Second Article',
  1273. 'body' => 'Second Article Body'
  1274. ),
  1275. 'Tag' => array(
  1276. array(
  1277. 'id' => '1',
  1278. 'tag' => 'tag1',
  1279. 'created' => '2007-03-18 12:22:23',
  1280. 'updated' => '2007-03-18 12:24:31'
  1281. ),
  1282. array(
  1283. 'id' => '3',
  1284. 'tag' => 'tag3',
  1285. 'created' => '2007-03-18 12:26:23',
  1286. 'updated' => '2007-03-18 12:28:31'
  1287. )));
  1288. $this->assertEqual($result, $expected);
  1289. $data = array(
  1290. 'Article' => array(
  1291. 'id' => 10,
  1292. 'user_id' => '2',
  1293. 'title' => 'New Article With Tags and fieldList',
  1294. 'body' => 'New Article Body with Tags and fieldList',
  1295. 'created' => '2007-03-18 14:55:23',
  1296. 'updated' => '2007-03-18 14:57:31'
  1297. ),
  1298. 'Tag' => array(
  1299. 'Tag' => array(1, 2, 3)
  1300. ));
  1301. $result = $TestModel->create()
  1302. && $TestModel->save($data, true, array('user_id', 'title', 'published'));
  1303. $this->assertFalse(empty($result));
  1304. $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
  1305. $result = $TestModel->read();
  1306. $expected = array(
  1307. 'Article' => array(
  1308. 'id' => 4,
  1309. 'user_id' => 2,
  1310. 'title' => 'New Article With Tags and fieldList',
  1311. 'body' => '',
  1312. 'published' => 'N',
  1313. 'created' => '',
  1314. 'updated' => ''
  1315. ),
  1316. 'Tag' => array(
  1317. 0 => array(
  1318. 'id' => 1,
  1319. 'tag' => 'tag1',
  1320. 'created' => '2007-03-18 12:22:23',
  1321. 'updated' => '2007-03-18 12:24:31'
  1322. ),
  1323. 1 => array(
  1324. 'id' => 2,
  1325. 'tag' => 'tag2',
  1326. 'created' => '2007-03-18 12:24:23',
  1327. 'updated' => '2007-03-18 12:26:31'
  1328. ),
  1329. 2 => array(
  1330. 'id' => 3,
  1331. 'tag' => 'tag3',
  1332. 'created' => '2007-03-18 12:26:23',
  1333. 'updated' => '2007-03-18 12:28:31'
  1334. )));
  1335. $this->assertEqual($result, $expected);
  1336. $this->loadFixtures('JoinA', 'JoinC', 'JoinAC', 'JoinB', 'JoinAB');
  1337. $TestModel = new JoinA();
  1338. $TestModel->hasBelongsToMany = array('JoinC' => array('unique' => true));
  1339. $data = array(
  1340. 'JoinA' => array(
  1341. 'id' => 1,
  1342. 'name' => 'Join A 1',
  1343. 'body' => 'Join A 1 Body',
  1344. ),
  1345. 'JoinC' => array(
  1346. 'JoinC' => array(
  1347. array('join_c_id' => 2, 'other' => 'new record'),
  1348. array('join_c_id' => 3, 'other' => 'new record')
  1349. )
  1350. )
  1351. );
  1352. $TestModel->save($data);
  1353. $result = $TestModel->read(null, 1);
  1354. $expected = array(4, 5);
  1355. $this->assertEqual(Set::extract('/JoinC/JoinAsJoinC/id', $result), $expected);
  1356. $expected = array('new record', 'new record');
  1357. $this->assertEqual(Set::extract('/JoinC/JoinAsJoinC/other', $result), $expected);
  1358. }
  1359. /**
  1360. * testSaveHabtmCustomKeys method
  1361. *
  1362. * @access public
  1363. * @return void
  1364. */
  1365. function testSaveHabtmCustomKeys() {
  1366. $this->loadFixtures('Story', 'StoriesTag', 'Tag');
  1367. $Story = new Story();
  1368. $data = array(
  1369. 'Story' => array('story' => '1'),
  1370. 'Tag' => array(
  1371. 'Tag' => array(2, 3)
  1372. ));
  1373. $result = $Story->set($data);
  1374. $this->assertFalse(empty($result));
  1375. $result = $Story->save();
  1376. $this->assertFalse(empty($result));
  1377. $result = $Story->find('all', array('order' => array('Story.story')));
  1378. $expected = array(
  1379. array(
  1380. 'Story' => array(
  1381. 'story' => 1,
  1382. 'title' => 'First Story'
  1383. ),
  1384. 'Tag' => array(
  1385. array(
  1386. 'id' => 2,
  1387. 'tag' => 'tag2',
  1388. 'created' => '2007-03-18 12:24:23',
  1389. 'updated' => '2007-03-18 12:26:31'
  1390. ),
  1391. array(
  1392. 'id' => 3,
  1393. 'tag' => 'tag3',
  1394. 'created' => '2007-03-18 12:26:23',
  1395. 'updated' => '2007-03-18 12:28:31'
  1396. ))),
  1397. array(
  1398. 'Story' => array(
  1399. 'story' => 2,
  1400. 'title' => 'Second Story'
  1401. ),
  1402. 'Tag' => array()
  1403. ));
  1404. $this->assertEqual($result, $expected);
  1405. }
  1406. /**
  1407. * test that saving habtm records respects conditions set in the 'conditions' key
  1408. * for the association.
  1409. *
  1410. * @return void
  1411. */
  1412. function testHabtmSaveWithConditionsInAssociation() {
  1413. $this->loadFixtures('JoinThing', 'Something', 'SomethingElse');
  1414. $Something = new Something();
  1415. $Something->unbindModel(array('hasAndBelongsToMany' => array('SomethingElse')), false);
  1416. $Something->bindModel(array(
  1417. 'hasAndBelongsToMany' => array(
  1418. 'DoomedSomethingElse' => array(
  1419. 'className' => 'SomethingElse',
  1420. 'joinTable' => 'join_things',
  1421. 'conditions' => array('JoinThing.doomed' => true),
  1422. 'unique' => true
  1423. ),
  1424. 'NotDoomedSomethingElse' => array(
  1425. 'className' => 'SomethingElse',
  1426. 'joinTable' => 'join_things',
  1427. 'conditions' => array('JoinThing.doomed' => 0),
  1428. 'unique' => true
  1429. )
  1430. )
  1431. ), false);
  1432. $result = $Something->read(null, 1);
  1433. $this->assertTrue(empty($result['NotDoomedSomethingElse']));
  1434. $this->assertEqual(count($result['DoomedSomethingElse']), 1);
  1435. $data = array(
  1436. 'Something' => array('id' => 1),
  1437. 'NotDoomedSomethingElse' => array(
  1438. 'NotDoomedSomethingElse' => array(
  1439. array('something_else_id' => 2, 'doomed' => 0),
  1440. array('something_else_id' => 3, 'doomed' => 0)
  1441. )
  1442. )
  1443. );
  1444. $Something->create($data);
  1445. $result = $Something->save();
  1446. $this->assertFalse(empty($result));
  1447. $result = $Something->read(null, 1);
  1448. $this->assertEqual(count($result['NotDoomedSomethingElse']), 2);
  1449. $this->assertEqual(count($result['DoomedSomethingElse']), 1);
  1450. }
  1451. /**
  1452. * testHabtmSaveKeyResolution method
  1453. *
  1454. * @access public
  1455. * @return void
  1456. */
  1457. function testHabtmSaveKeyResolution() {
  1458. $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies');
  1459. $ThePaper = new ThePaper();
  1460. $ThePaper->id = 1;
  1461. $ThePaper->save(array('Monkey' => array(2, 3)));
  1462. $result = $ThePaper->findById(1);
  1463. $expected = array(
  1464. array(
  1465. 'id' => '2',
  1466. 'device_type_id' => '1',
  1467. 'name' => 'Device 2',
  1468. 'typ' => '1'
  1469. ),
  1470. array(
  1471. 'id' => '3',
  1472. 'device_type_id' => '1',
  1473. 'name' => 'Device 3',
  1474. 'typ' => '2'
  1475. ));
  1476. $this->assertEqual($result['Monkey'], $expected);
  1477. $ThePaper->id = 2;
  1478. $ThePaper->save(array('Monkey' => array(1, 2, 3)));
  1479. $result = $ThePaper->findById(2);
  1480. $expected = array(
  1481. array(
  1482. 'id' => '1',
  1483. 'device_type_id' => '1',
  1484. 'name' => 'Device 1',
  1485. 'typ' => '1'
  1486. ),
  1487. array(
  1488. 'id' => '2',
  1489. 'device_type_id' => '1',
  1490. 'name' => 'Device 2',
  1491. 'typ' => '1'
  1492. ),
  1493. array(
  1494. 'id' => '3',
  1495. 'device_type_id' => '1',
  1496. 'name' => 'Device 3',
  1497. 'typ' => '2'
  1498. ));
  1499. $this->assertEqual($result['Monkey'], $expected);
  1500. $ThePaper->id = 2;
  1501. $ThePaper->save(array('Monkey' => array(1, 3)));
  1502. $result = $ThePaper->findById(2);
  1503. $expected = array(
  1504. array(
  1505. 'id' => '1',
  1506. 'device_type_id' => '1',
  1507. 'name' => 'Device 1',
  1508. 'typ' => '1'
  1509. ),
  1510. array(
  1511. 'id' => '3',
  1512. 'device_type_id' => '1',
  1513. 'name' => 'Device 3',
  1514. 'typ' => '2'
  1515. ));
  1516. $this->assertEqual($result['Monkey'], $expected);
  1517. $result = $ThePaper->findById(1);
  1518. $expected = array(
  1519. array(
  1520. 'id' => '2',
  1521. 'device_type_id' => '1',
  1522. 'name' => 'Device 2',
  1523. 'typ' => '1'
  1524. ),
  1525. array(
  1526. 'id' => '3',
  1527. 'device_type_id' => '1',
  1528. 'name' => 'Device 3',
  1529. 'typ' => '2'
  1530. ));
  1531. $this->assertEqual($result['Monkey'], $expected);
  1532. }
  1533. /**
  1534. * testCreationOfEmptyRecord method
  1535. *
  1536. * @access public
  1537. * @return void
  1538. */
  1539. function testCreationOfEmptyRecord() {
  1540. $this->loadFixtures('Author');
  1541. $TestModel = new Author();
  1542. $this->assertEqual($TestModel->find('count'), 4);
  1543. $TestModel->deleteAll(true, false, false);
  1544. $this->assertEqual($TestModel->find('count'), 0);
  1545. $result = $TestModel->save();
  1546. $this->assertTrue(isset($result['Author']['created']));
  1547. $this->assertTrue(isset($result['Author']['updated']));
  1548. $this->assertEqual($TestModel->find('count'), 1);
  1549. }
  1550. /**
  1551. * testCreateWithPKFiltering method
  1552. *
  1553. * @access public
  1554. * @return void
  1555. */
  1556. function testCreateWithPKFiltering() {
  1557. $TestModel = new Article();
  1558. $data = array(
  1559. 'id' => 5,
  1560. 'user_id' => 2,
  1561. 'title' => 'My article',
  1562. 'body' => 'Some text'
  1563. );
  1564. $result = $TestModel->create($data);
  1565. $expected = array(
  1566. 'Article' => array(
  1567. 'published' => 'N',
  1568. 'id' => 5,
  1569. 'user_id' => 2,
  1570. 'title' => 'My article',
  1571. 'body' => 'Some text'
  1572. ));
  1573. $this->assertEqual($result, $expected);
  1574. $this->assertEqual($TestModel->id, 5);
  1575. $result = $TestModel->create($data, true);
  1576. $expected = array(
  1577. 'Article' => array(
  1578. 'published' => 'N',
  1579. 'id' => false,
  1580. 'user_id' => 2,
  1581. 'title' => 'My article',
  1582. 'body' => 'Some text'
  1583. ));
  1584. $this->assertEqual($result, $expected);
  1585. $this->assertFalse($TestModel->id);
  1586. $result = $TestModel->create(array('Article' => $data), true);
  1587. $expected = array(
  1588. 'Article' => array(
  1589. 'published' => 'N',
  1590. 'id' => false,
  1591. 'user_id' => 2,
  1592. 'title' => 'My article',
  1593. 'body' => 'Some text'
  1594. ));
  1595. $this->assertEqual($result, $expected);
  1596. $this->assertFalse($TestModel->id);
  1597. $data = array(
  1598. 'id' => 6,
  1599. 'user_id' => 2,
  1600. 'title' => 'My article',
  1601. 'body' => 'Some text',
  1602. 'created' => '1970-01-01 00:00:00',
  1603. 'updated' => '1970-01-01 12:00:00',
  1604. 'modified' => '1970-01-01 12:00:00'
  1605. );
  1606. $result = $TestModel->create($data);
  1607. $expected = array(
  1608. 'Article' => array(
  1609. 'published' => 'N',
  1610. 'id' => 6,
  1611. 'user_id' => 2,
  1612. 'title' => 'My article',
  1613. 'body' => 'Some text',
  1614. 'created' => '1970-01-01 00:00:00',
  1615. 'updated' => '1970-01-01 12:00:00',
  1616. 'modified' => '1970-01-01 12:00:00'
  1617. ));
  1618. $this->assertEqual($result, $expected);
  1619. $this->assertEqual($TestModel->id, 6);
  1620. $result = $TestModel->create(array(
  1621. 'Article' => array_diff_key($data, array(
  1622. 'created' => true,
  1623. 'updated' => true,
  1624. 'modified' => true
  1625. ))), true);
  1626. $expected = array(
  1627. 'Article' => array(
  1628. 'published' => 'N',
  1629. 'id' => false,
  1630. 'user_id' => 2,
  1631. 'title' => 'My article',
  1632. 'body' => 'Some text'
  1633. ));
  1634. $this->assertEqual($result, $expected);
  1635. $this->assertFalse($TestModel->id);
  1636. }
  1637. /**
  1638. * testCreationWithMultipleData method
  1639. *
  1640. * @access public
  1641. * @return void
  1642. */
  1643. function testCreationWithMultipleData() {
  1644. $this->loadFixtures('Article', 'Comment');
  1645. $Article = new Article();
  1646. $Comment = new Comment();
  1647. $articles = $Article->find('all', array(
  1648. 'fields' => array('id','title'),
  1649. 'recursive' => -1
  1650. ));
  1651. $comments = $Comment->find('all', array(
  1652. 'fields' => array('id','article_id','user_id','comment','published'), 'recursive' => -1));
  1653. $this->assertEqual($articles, array(
  1654. array('Article' => array(
  1655. 'id' => 1,
  1656. 'title' => 'First Article'
  1657. )),
  1658. array('Article' => array(
  1659. 'id' => 2,
  1660. 'title' => 'Second Article'
  1661. )),
  1662. array('Article' => array(
  1663. 'id' => 3,
  1664. 'title' => 'Third Article'
  1665. ))));
  1666. $this->assertEqual($comments, array(
  1667. array('Comment' => array(
  1668. 'id' => 1,
  1669. 'article_id' => 1,
  1670. 'user_id' => 2,
  1671. 'comment' => 'First Comment for First Article',
  1672. 'published' => 'Y'
  1673. )),
  1674. array('Comment' => array(
  1675. 'id' => 2,
  1676. 'article_id' => 1,
  1677. 'user_id' => 4,
  1678. 'comment' => 'Second Comment for First Article',
  1679. 'published' => 'Y'
  1680. )),
  1681. array('Comment' => array(
  1682. 'id' => 3,
  1683. 'article_id' => 1,
  1684. 'user_id' => 1,
  1685. 'comment' => 'Third Comment for First Article',
  1686. 'published' => 'Y'
  1687. )),
  1688. array('Comment' => array(
  1689. 'id' => 4,
  1690. 'article_id' => 1,
  1691. 'user_id' => 1,
  1692. 'comment' => 'Fourth Comment for First Article',
  1693. 'published' => 'N'
  1694. )),
  1695. array('Comment' => array(
  1696. 'id' => 5,
  1697. 'article_id' => 2,
  1698. 'user_id' => 1,
  1699. 'comment' => 'First Comment for Second Article',
  1700. 'published' => 'Y'
  1701. )),
  1702. array('Comment' => array(
  1703. 'id' => 6,
  1704. 'article_id' => 2,
  1705. 'user_id' => 2,
  1706. 'comment' => 'Second Comment for Second Article',
  1707. 'published' => 'Y'
  1708. ))));
  1709. $data = array(
  1710. 'Comment' => array(
  1711. 'article_id' => 2,
  1712. 'user_id' => 4,
  1713. 'comment' => 'Brand New Comment',
  1714. 'published' => 'N'
  1715. ),
  1716. 'Article' => array(
  1717. 'id' => 2,
  1718. 'title' => 'Second Article Modified'
  1719. ));
  1720. $result = $Comment->create($data);
  1721. $this->assertFalse(empty($result));
  1722. $result = $Comment->save();
  1723. $this->assertFalse(empty($result));
  1724. $articles = $Article->find('all', array(
  1725. 'fields' => array('id','title'),
  1726. 'recursive' => -1
  1727. ));
  1728. $comments = $Comment->find('all', array(
  1729. 'fields' => array('id','article_id','user_id','comment','published'),
  1730. 'recursive' => -1
  1731. ));
  1732. $this->assertEqual($articles, array(
  1733. array('Article' => array(
  1734. 'id' => 1,
  1735. 'title' => 'First Article'
  1736. )),
  1737. array('Article' => array(
  1738. 'id' => 2,
  1739. 'title' => 'Second Article'
  1740. )),
  1741. array('Article' => array(
  1742. 'id' => 3,
  1743. 'title' => 'Third Article'
  1744. ))));
  1745. $this->assertEqual($comments, array(
  1746. array('Comment' => array(
  1747. 'id' => 1,
  1748. 'article_id' => 1,
  1749. 'user_id' => 2,
  1750. 'comment' => 'First Comment for First Article',
  1751. 'published' => 'Y'
  1752. )),
  1753. array('Comment' => array(
  1754. 'id' => 2,
  1755. 'article_id' => 1,
  1756. 'user_id' => 4,
  1757. 'comment' => 'Second Comment for First Article',
  1758. 'published' => 'Y'
  1759. )),
  1760. array('Comment' => array(
  1761. 'id' => 3,
  1762. 'article_id' => 1,
  1763. 'user_id' => 1,
  1764. 'comment' => 'Third Comment for First Article',
  1765. 'published' => 'Y'
  1766. )),
  1767. array('Comment' => array(
  1768. 'id' => 4,
  1769. 'article_id' => 1,
  1770. 'user_id' => 1,
  1771. 'comment' => 'Fourth Comment for First Article',
  1772. 'published' => 'N'
  1773. )),
  1774. array('Comment' => array(
  1775. 'id' => 5,
  1776. 'article_id' => 2,
  1777. 'user_id' => 1,
  1778. 'comment' => 'First Comment for Second Article',
  1779. 'published' => 'Y'
  1780. )),
  1781. array('Comment' => array(
  1782. 'id' => 6,
  1783. 'article_id' => 2,
  1784. 'user_id' => 2, 'comment' =>
  1785. 'Second Comment for Second Article',
  1786. 'published' => 'Y'
  1787. )),
  1788. array('Comment' => array(
  1789. 'id' => 7,
  1790. 'article_id' => 2,
  1791. 'user_id' => 4,
  1792. 'comment' => 'Brand New Comment',
  1793. 'published' => 'N'
  1794. ))));
  1795. }
  1796. /**
  1797. * testCreationWithMultipleDataSameModel method
  1798. *
  1799. * @access public
  1800. * @return void
  1801. */
  1802. function testCreationWithMultipleDataSameModel() {
  1803. $this->loadFixtures('Article');
  1804. $Article = new Article();
  1805. $SecondaryArticle = new Article();
  1806. $result = $Article->field('title', array('id' => 1));
  1807. $this->assertEqual($result, 'First Article');
  1808. $data = array(
  1809. 'Article' => array(
  1810. 'user_id' => 2,
  1811. 'title' => 'Brand New Article',
  1812. 'body' => 'Brand New Article Body',
  1813. 'published' => 'Y'
  1814. ),
  1815. 'SecondaryArticle' => array(
  1816. 'id' => 1
  1817. ));
  1818. $Article->create();
  1819. $result = $Article->save($data);
  1820. $this->assertFalse(empty($result));
  1821. $result = $Article->getInsertID();
  1822. $this->assertTrue(!empty($result));
  1823. $result = $Article->field('title', array('id' => 1));
  1824. $this->assertEqual($result, 'First Article');
  1825. $articles = $Article->find('all', array(
  1826. 'fields' => array('id','title'),
  1827. 'recursive' => -1
  1828. ));
  1829. $this->assertEqual($articles, array(
  1830. array('Article' => array(
  1831. 'id' => 1,
  1832. 'title' => 'First Article'
  1833. )),
  1834. array('Article' => array(
  1835. 'id' => 2,
  1836. 'title' => 'Second Article'
  1837. )),
  1838. array('Article' => array(
  1839. 'id' => 3,
  1840. 'title' => 'Third Article'
  1841. )),
  1842. array('Article' => array(
  1843. 'id' => 4,
  1844. 'title' => 'Brand New Article'
  1845. ))));
  1846. }
  1847. /**
  1848. * testCreationWithMultipleDataSameModelManualInstances method
  1849. *
  1850. * @access public
  1851. * @return void
  1852. */
  1853. function testCreationWithMultipleDataSameModelManualInstances() {
  1854. $this->loadFixtures('PrimaryModel');
  1855. $Primary = new PrimaryModel();
  1856. $Secondary = new PrimaryModel();
  1857. $result = $Primary->field('primary_name', array('id' => 1));
  1858. $this->assertEqual($result, 'Primary Name Existing');
  1859. $data = array(
  1860. 'PrimaryModel' => array(
  1861. 'primary_name' => 'Primary Name New'
  1862. ),
  1863. 'SecondaryModel' => array(
  1864. 'id' => array(1)
  1865. ));
  1866. $Primary->create();
  1867. $result = $Primary->save($data);
  1868. $this->assertFalse(empty($result));
  1869. $result = $Primary->field('primary_name', array('id' => 1));
  1870. $this->assertEqual($result, 'Primary Name Existing');
  1871. $result = $Primary->getInsertID();
  1872. $this->assertTrue(!empty($result));
  1873. $result = $Primary->field('primary_name', array('id' => $result));
  1874. $this->assertEqual($result, 'Primary Name New');
  1875. $result = $Primary->find('count');
  1876. $this->assertEqual($result, 2);
  1877. }
  1878. /**
  1879. * testRecordExists method
  1880. *
  1881. * @access public
  1882. * @return void
  1883. */
  1884. function testRecordExists() {
  1885. $this->loadFixtures('User');
  1886. $TestModel = new User();
  1887. $this->assertFalse($TestModel->exists());
  1888. $TestModel->read(null, 1);
  1889. $this->assertTrue($TestModel->exists());
  1890. $TestModel->create();
  1891. $this->assertFalse($TestModel->exists());
  1892. $TestModel->id = 4;
  1893. $this->assertTrue($TestModel->exists());
  1894. $TestModel = new TheVoid();
  1895. $this->assertFalse($TestModel->exists());
  1896. $TestModel->id = 5;
  1897. $this->expectError();
  1898. ob_start();
  1899. $this->assertFalse($TestModel->exists());
  1900. $output = ob_get_clean();
  1901. }
  1902. /**
  1903. * testUpdateExisting method
  1904. *
  1905. * @access public
  1906. * @return void
  1907. */
  1908. function testUpdateExisting() {
  1909. $this->loadFixtures('User', 'Article', 'Comment');
  1910. $TestModel = new User();
  1911. $TestModel->create();
  1912. $TestModel->save(array(
  1913. 'User' => array(
  1914. 'user' => 'some user',
  1915. 'password' => 'some password'
  1916. )));
  1917. $this->assertTrue(is_int($TestModel->id) || (intval($TestModel->id) === 5));
  1918. $id = $TestModel->id;
  1919. $TestModel->save(array(
  1920. 'User' => array(
  1921. 'user' => 'updated user'
  1922. )));
  1923. $this->assertEqual($TestModel->id, $id);
  1924. $result = $TestModel->findById($id);
  1925. $this->assertEqual($result['User']['user'], 'updated user');
  1926. $this->assertEqual($result['User']['password'], 'some password');
  1927. $Article = new Article();
  1928. $Comment = new Comment();
  1929. $data = array(
  1930. 'Comment' => array(
  1931. 'id' => 1,
  1932. 'comment' => 'First Comment for First Article'
  1933. ),
  1934. 'Article' => array(
  1935. 'id' => 2,
  1936. 'title' => 'Second Article'
  1937. ));
  1938. $result = $Article->save($data);
  1939. $this->assertFalse(empty($result));
  1940. $result = $Comment->save($data);
  1941. $this->assertFalse(empty($result));
  1942. }
  1943. /**
  1944. * test updating records and saving blank values.
  1945. *
  1946. * @return void
  1947. */
  1948. function testUpdateSavingBlankValues() {
  1949. $this->loadFixtures('Article');
  1950. $Article = new Article();
  1951. $Article->validate = array();
  1952. $Article->create();
  1953. $result = $Article->save(array(
  1954. 'id' => 1,
  1955. 'title' => '',
  1956. 'body' => ''
  1957. ));
  1958. $this->assertTrue((bool)$result);
  1959. $result = $Article->find('first', array('conditions' => array('Article.id' => 1)));
  1960. $this->assertEqual('', $result['Article']['title'], 'Title is not blank');
  1961. $this->assertEqual('', $result['Article']['body'], 'Body is not blank');
  1962. }
  1963. /**
  1964. * testUpdateMultiple method
  1965. *
  1966. * @access public
  1967. * @return void
  1968. */
  1969. function testUpdateMultiple() {
  1970. $this->loadFixtures('Comment', 'Article', 'User', 'CategoryThread');
  1971. $TestModel = new Comment();
  1972. $result = Set::extract($TestModel->find('all'), '{n}.Comment.user_id');
  1973. $expected = array('2', '4', '1', '1', '1', '2');
  1974. $this->assertEqual($result, $expected);
  1975. $TestModel->updateAll(array('Comment.user_id' => 5), array('Comment.user_id' => 2));
  1976. $result = Set::combine($TestModel->find('all'), '{n}.Comment.id', '{n}.Comment.user_id');
  1977. $expected = array(1 => 5, 2 => 4, 3 => 1, 4 => 1, 5 => 1, 6 => 5);
  1978. $this->assertEqual($result, $expected);
  1979. $result = $TestModel->updateAll(
  1980. array('Comment.comment' => "'Updated today'"),
  1981. array('Comment.user_id' => 5)
  1982. );
  1983. $this->assertFalse(empty($result));
  1984. $result = Set::extract(
  1985. $TestModel->find('all', array(
  1986. 'conditions' => array(
  1987. 'Comment.user_id' => 5
  1988. ))),
  1989. '{n}.Comment.comment'
  1990. );
  1991. $expected = array_fill(0, 2, 'Updated today');
  1992. $this->assertEqual($result, $expected);
  1993. }
  1994. /**
  1995. * testHabtmUuidWithUuidId method
  1996. *
  1997. * @access public
  1998. * @return void
  1999. */
  2000. function testHabtmUuidWithUuidId() {
  2001. $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolio', 'UuiditemsUuidportfolioNumericid');
  2002. $TestModel = new Uuidportfolio();
  2003. $data = array('Uuidportfolio' => array('name' => 'Portfolio 3'));
  2004. $data['Uuiditem']['Uuiditem'] = array('483798c8-c7cc-430e-8cf9-4fcc40cf8569');
  2005. $TestModel->create($data);
  2006. $TestModel->save();
  2007. $id = $TestModel->id;
  2008. $result = $TestModel->read(null, $id);
  2009. $this->assertEqual(1, count($result['Uuiditem']));
  2010. $this->assertEqual(strlen($result['Uuiditem'][0]['UuiditemsUuidportfolio']['id']), 36);
  2011. }
  2012. /**
  2013. * test HABTM saving when join table has no primary key and only 2 columns.
  2014. *
  2015. * @return void
  2016. */
  2017. function testHabtmSavingWithNoPrimaryKeyUuidJoinTable() {
  2018. $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
  2019. $Fruit = new Fruit();
  2020. $data = array(
  2021. 'Fruit' => array(
  2022. 'color' => 'Red',
  2023. 'shape' => 'Heart-shaped',
  2024. 'taste' => 'sweet',
  2025. 'name' => 'Strawberry',
  2026. ),
  2027. 'UuidTag' => array(
  2028. 'UuidTag' => array(
  2029. '481fc6d0-b920-43e0-e50f-6d1740cf8569'
  2030. )
  2031. )
  2032. );
  2033. $result = $Fruit->save($data);
  2034. $this->assertFalse(empty($result));
  2035. }
  2036. /**
  2037. * test HABTM saving when join table has no primary key and only 2 columns, no with model is used.
  2038. *
  2039. * @return void
  2040. */
  2041. function testHabtmSavingWithNoPrimaryKeyUuidJoinTableNoWith() {
  2042. $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
  2043. $Fruit = new FruitNoWith();
  2044. $data = array(
  2045. 'Fruit' => array(
  2046. 'color' => 'Red',
  2047. 'shape' => 'Heart-shaped',
  2048. 'taste' => 'sweet',
  2049. 'name' => 'Strawberry',
  2050. ),
  2051. 'UuidTag' => array(
  2052. 'UuidTag' => array(
  2053. '481fc6d0-b920-43e0-e50f-6d1740cf8569'
  2054. )
  2055. )
  2056. );
  2057. $result = $Fruit->save($data);
  2058. $this->assertFalse(empty($result));
  2059. }
  2060. /**
  2061. * testHabtmUuidWithNumericId method
  2062. *
  2063. * @access public
  2064. * @return void
  2065. */
  2066. function testHabtmUuidWithNumericId() {
  2067. $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolioNumericid');
  2068. $TestModel = new Uuiditem();
  2069. $data = array('Uuiditem' => array('name' => 'Item 7', 'published' => 0));
  2070. $data['Uuidportfolio']['Uuidportfolio'] = array('480af662-eb8c-47d3-886b-230540cf8569');
  2071. $TestModel->create($data);
  2072. $TestModel->save();
  2073. $id = $TestModel->id;
  2074. $result = $TestModel->read(null, $id);
  2075. $this->assertEqual(1, count($result['Uuidportfolio']));
  2076. }
  2077. /**
  2078. * testSaveMultipleHabtm method
  2079. *
  2080. * @access public
  2081. * @return void
  2082. */
  2083. function testSaveMultipleHabtm() {
  2084. $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC');
  2085. $TestModel = new JoinA();
  2086. $result = $TestModel->findById(1);
  2087. $expected = array(
  2088. 'JoinA' => array(
  2089. 'id' => 1,
  2090. 'name' => 'Join A 1',
  2091. 'body' => 'Join A 1 Body',
  2092. 'created' => '2008-01-03 10:54:23',
  2093. 'updated' => '2008-01-03 10:54:23'
  2094. ),
  2095. 'JoinB' => array(
  2096. 0 => array(
  2097. 'id' => 2,
  2098. 'name' => 'Join B 2',
  2099. 'created' => '2008-01-03 10:55:02',
  2100. 'updated' => '2008-01-03 10:55:02',
  2101. 'JoinAsJoinB' => array(
  2102. 'id' => 1,
  2103. 'join_a_id' => 1,
  2104. 'join_b_id' => 2,
  2105. 'other' => 'Data for Join A 1 Join B 2',
  2106. 'created' => '2008-01-03 10:56:33',
  2107. 'updated' => '2008-01-03 10:56:33'
  2108. ))),
  2109. 'JoinC' => array(
  2110. 0 => array(
  2111. 'id' => 2,
  2112. 'name' => 'Join C 2',
  2113. 'created' => '2008-01-03 10:56:12',
  2114. 'updated' => '2008-01-03 10:56:12',
  2115. 'JoinAsJoinC' => array(
  2116. 'id' => 1,
  2117. 'join_a_id' => 1,
  2118. 'join_c_id' => 2,
  2119. 'other' => 'Data for Join A 1 Join C 2',
  2120. 'created' => '2008-01-03 10:57:22',
  2121. 'updated' => '2008-01-03 10:57:22'
  2122. ))));
  2123. $this->assertEqual($result, $expected);
  2124. $ts = date('Y-m-d H:i:s');
  2125. $TestModel->id = 1;
  2126. $data = array(
  2127. 'JoinA' => array(
  2128. 'id' => '1',
  2129. 'name' => 'New name for Join A 1',
  2130. 'updated' => $ts
  2131. ),
  2132. 'JoinB' => array(
  2133. array(
  2134. 'id' => 1,
  2135. 'join_b_id' => 2,
  2136. 'other' => 'New data for Join A 1 Join B 2',
  2137. 'created' => $ts,
  2138. 'updated' => $ts
  2139. )),
  2140. 'JoinC' => array(
  2141. array(
  2142. 'id' => 1,
  2143. 'join_c_id' => 2,
  2144. 'other' => 'New data for Join A 1 Join C 2',
  2145. 'created' => $ts,
  2146. 'updated' => $ts
  2147. )));
  2148. $TestModel->set($data);
  2149. $TestModel->save();
  2150. $result = $TestModel->findById(1);
  2151. $expected = array(
  2152. 'JoinA' => array(
  2153. 'id' => 1,
  2154. 'name' => 'New name for Join A 1',
  2155. 'body' => 'Join A 1 Body',
  2156. 'created' => '2008-01-03 10:54:23',
  2157. 'updated' => $ts
  2158. ),
  2159. 'JoinB' => array(
  2160. 0 => array(
  2161. 'id' => 2,
  2162. 'name' => 'Join B 2',
  2163. 'created' => '2008-01-03 10:55:02',
  2164. 'updated' => '2008-01-03 10:55:02',
  2165. 'JoinAsJoinB' => array(
  2166. 'id' => 1,
  2167. 'join_a_id' => 1,
  2168. 'join_b_id' => 2,
  2169. 'other' => 'New data for Join A 1 Join B 2',
  2170. 'created' => $ts,
  2171. 'updated' => $ts
  2172. ))),
  2173. 'JoinC' => array(
  2174. 0 => array(
  2175. 'id' => 2,
  2176. 'name' => 'Join C 2',
  2177. 'created' => '2008-01-03 10:56:12',
  2178. 'updated' => '2008-01-03 10:56:12',
  2179. 'JoinAsJoinC' => array(
  2180. 'id' => 1,
  2181. 'join_a_id' => 1,
  2182. 'join_c_id' => 2,
  2183. 'other' => 'New data for Join A 1 Join C 2',
  2184. 'created' => $ts,
  2185. 'updated' => $ts
  2186. ))));
  2187. $this->assertEqual($result, $expected);
  2188. }
  2189. /**
  2190. * testSaveAll method
  2191. *
  2192. * @access public
  2193. * @return void
  2194. */
  2195. function testSaveAll() {
  2196. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  2197. $TestModel = new Post();
  2198. $result = $TestModel->find('all');
  2199. $this->assertEqual(count($result), 3);
  2200. $this->assertFalse(isset($result[3]));
  2201. $ts = date('Y-m-d H:i:s');
  2202. $TestModel->saveAll(array(
  2203. 'Post' => array(
  2204. 'title' => 'Post with Author',
  2205. 'body' => 'This post will be saved with an author'
  2206. ),
  2207. 'Author' => array(
  2208. 'user' => 'bob',
  2209. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
  2210. )));
  2211. $result = $TestModel->find('all');
  2212. $expected = array(
  2213. 'Post' => array(
  2214. 'id' => '4',
  2215. 'author_id' => '5',
  2216. 'title' => 'Post with Author',
  2217. 'body' => 'This post will be saved with an author',
  2218. 'published' => 'N',
  2219. 'created' => $ts,
  2220. 'updated' => $ts
  2221. ),
  2222. 'Author' => array(
  2223. 'id' => '5',
  2224. 'user' => 'bob',
  2225. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
  2226. 'created' => $ts,
  2227. 'updated' => $ts,
  2228. 'test' => 'working'
  2229. ));
  2230. $this->assertEqual($result[3], $expected);
  2231. $this->assertEqual(count($result), 4);
  2232. $TestModel->deleteAll(true);
  2233. $this->assertEqual($TestModel->find('all'), array());
  2234. // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
  2235. $this->db->truncate($TestModel);
  2236. $ts = date('Y-m-d H:i:s');
  2237. $TestModel->saveAll(array(
  2238. array(
  2239. 'title' => 'Multi-record post 1',
  2240. 'body' => 'First multi-record post',
  2241. 'author_id' => 2
  2242. ),
  2243. array(
  2244. 'title' => 'Multi-record post 2',
  2245. 'body' => 'Second multi-record post',
  2246. 'author_id' => 2
  2247. )));
  2248. $result = $TestModel->find('all', array(
  2249. 'recursive' => -1,
  2250. 'order' => 'Post.id ASC'
  2251. ));
  2252. $expected = array(
  2253. array(
  2254. 'Post' => array(
  2255. 'id' => '1',
  2256. 'author_id' => '2',
  2257. 'title' => 'Multi-record post 1',
  2258. 'body' => 'First multi-record post',
  2259. 'published' => 'N',
  2260. 'created' => $ts,
  2261. 'updated' => $ts
  2262. )),
  2263. array(
  2264. 'Post' => array(
  2265. 'id' => '2',
  2266. 'author_id' => '2',
  2267. 'title' => 'Multi-record post 2',
  2268. 'body' => 'Second multi-record post',
  2269. 'published' => 'N',
  2270. 'created' => $ts,
  2271. 'updated' => $ts
  2272. )));
  2273. $this->assertEqual($result, $expected);
  2274. $TestModel = new Comment();
  2275. $ts = date('Y-m-d H:i:s');
  2276. $result = $TestModel->saveAll(array(
  2277. 'Comment' => array(
  2278. 'article_id' => 2,
  2279. 'user_id' => 2,
  2280. 'comment' => 'New comment with attachment',
  2281. 'published' => 'Y'
  2282. ),
  2283. 'Attachment' => array(
  2284. 'attachment' => 'some_file.tgz'
  2285. )));
  2286. $this->assertFalse(empty($result));
  2287. $result = $TestModel->find('all');
  2288. $expected = array(
  2289. 'id' => '7',
  2290. 'article_id' => '2',
  2291. 'user_id' => '2',
  2292. 'comment' => 'New comment with attachment',
  2293. 'published' => 'Y',
  2294. 'created' => $ts,
  2295. 'updated' => $ts
  2296. );
  2297. $this->assertEqual($result[6]['Comment'], $expected);
  2298. $expected = array(
  2299. 'id' => '7',
  2300. 'article_id' => '2',
  2301. 'user_id' => '2',
  2302. 'comment' => 'New comment with attachment',
  2303. 'published' => 'Y',
  2304. 'created' => $ts,
  2305. 'updated' => $ts
  2306. );
  2307. $this->assertEqual($result[6]['Comment'], $expected);
  2308. $expected = array(
  2309. 'id' => '2',
  2310. 'comment_id' => '7',
  2311. 'attachment' => 'some_file.tgz',
  2312. 'created' => $ts,
  2313. 'updated' => $ts
  2314. );
  2315. $this->assertEqual($result[6]['Attachment'], $expected);
  2316. }
  2317. /**
  2318. * Test SaveAll with Habtm relations
  2319. *
  2320. * @access public
  2321. * @return void
  2322. */
  2323. function testSaveAllHabtm() {
  2324. $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  2325. $data = array(
  2326. 'Article' => array(
  2327. 'user_id' => 1,
  2328. 'title' => 'Article Has and belongs to Many Tags'
  2329. ),
  2330. 'Tag' => array(
  2331. 'Tag' => array(1, 2)
  2332. ),
  2333. 'Comment' => array(
  2334. array(
  2335. 'comment' => 'Article comment',
  2336. 'user_id' => 1
  2337. )));
  2338. $Article = new Article();
  2339. $result = $Article->saveAll($data);
  2340. $this->assertFalse(empty($result));
  2341. $result = $Article->read();
  2342. $this->assertEqual(count($result['Tag']), 2);
  2343. $this->assertEqual($result['Tag'][0]['tag'], 'tag1');
  2344. $this->assertEqual(count($result['Comment']), 1);
  2345. $this->assertEqual(count($result['Comment'][0]['comment']['Article comment']), 1);
  2346. }
  2347. /**
  2348. * Test SaveAll with Habtm relations and extra join table fields
  2349. *
  2350. * @access public
  2351. * @return void
  2352. */
  2353. function testSaveAllHabtmWithExtraJoinTableFields() {
  2354. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  2355. $data = array(
  2356. 'Something' => array(
  2357. 'id' => 4,
  2358. 'title' => 'Extra Fields',
  2359. 'body' => 'Extra Fields Body',
  2360. 'published' => '1'
  2361. ),
  2362. 'SomethingElse' => array(
  2363. array('something_else_id' => 1, 'doomed' => '1'),
  2364. array('something_else_id' => 2, 'doomed' => '0'),
  2365. array('something_else_id' => 3, 'doomed' => '1')
  2366. )
  2367. );
  2368. $Something = new Something();
  2369. $result = $Something->saveAll($data);
  2370. $this->assertFalse(empty($result));
  2371. $result = $Something->read();
  2372. $this->assertEqual(count($result['SomethingElse']), 3);
  2373. $this->assertTrue(Set::matches('/Something[id=4]', $result));
  2374. $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
  2375. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
  2376. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
  2377. $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
  2378. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
  2379. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
  2380. $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
  2381. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
  2382. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
  2383. }
  2384. /**
  2385. * testSaveAllHasOne method
  2386. *
  2387. * @access public
  2388. * @return void
  2389. */
  2390. function testSaveAllHasOne() {
  2391. $model = new Comment();
  2392. $model->deleteAll(true);
  2393. $this->assertEqual($model->find('all'), array());
  2394. $model->Attachment->deleteAll(true);
  2395. $this->assertEqual($model->Attachment->find('all'), array());
  2396. $this->assertTrue($model->saveAll(array(
  2397. 'Comment' => array(
  2398. 'comment' => 'Comment with attachment',
  2399. 'article_id' => 1,
  2400. 'user_id' => 1
  2401. ),
  2402. 'Attachment' => array(
  2403. 'attachment' => 'some_file.zip'
  2404. ))));
  2405. $result = $model->find('all', array('fields' => array(
  2406. 'Comment.id', 'Comment.comment', 'Attachment.id',
  2407. 'Attachment.comment_id', 'Attachment.attachment'
  2408. )));
  2409. $expected = array(array(
  2410. 'Comment' => array(
  2411. 'id' => '1',
  2412. 'comment' => 'Comment with attachment'
  2413. ),
  2414. 'Attachment' => array(
  2415. 'id' => '1',
  2416. 'comment_id' => '1',
  2417. 'attachment' => 'some_file.zip'
  2418. )));
  2419. $this->assertEqual($result, $expected);
  2420. $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
  2421. $data = array(
  2422. 'Comment' => array(
  2423. 'comment' => 'Comment with attachment',
  2424. 'article_id' => 1,
  2425. 'user_id' => 1
  2426. ),
  2427. 'Attachment' => array(
  2428. 'attachment' => 'some_file.zip'
  2429. ));
  2430. $this->assertTrue($model->saveAll($data, array('validate' => 'first')));
  2431. }
  2432. /**
  2433. * testSaveAllBelongsTo method
  2434. *
  2435. * @access public
  2436. * @return void
  2437. */
  2438. function testSaveAllBelongsTo() {
  2439. $model = new Comment();
  2440. $model->deleteAll(true);
  2441. $this->assertEqual($model->find('all'), array());
  2442. $model->Article->deleteAll(true);
  2443. $this->assertEqual($model->Article->find('all'), array());
  2444. $this->assertTrue($model->saveAll(array(
  2445. 'Comment' => array(
  2446. 'comment' => 'Article comment',
  2447. 'article_id' => 1,
  2448. 'user_id' => 1
  2449. ),
  2450. 'Article' => array(
  2451. 'title' => 'Model Associations 101',
  2452. 'user_id' => 1
  2453. ))));
  2454. $result = $model->find('all', array('fields' => array(
  2455. 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
  2456. )));
  2457. $expected = array(array(
  2458. 'Comment' => array(
  2459. 'id' => '1',
  2460. 'article_id' => '1',
  2461. 'comment' => 'Article comment'
  2462. ),
  2463. 'Article' => array(
  2464. 'id' => '1',
  2465. 'title' => 'Model Associations 101'
  2466. )));
  2467. $this->assertEqual($result, $expected);
  2468. }
  2469. /**
  2470. * testSaveAllHasOneValidation method
  2471. *
  2472. * @access public
  2473. * @return void
  2474. */
  2475. function testSaveAllHasOneValidation() {
  2476. $model = new Comment();
  2477. $model->deleteAll(true);
  2478. $this->assertEqual($model->find('all'), array());
  2479. $model->Attachment->deleteAll(true);
  2480. $this->assertEqual($model->Attachment->find('all'), array());
  2481. $model->validate = array('comment' => 'notEmpty');
  2482. $model->Attachment->validate = array('attachment' => 'notEmpty');
  2483. $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
  2484. $this->assertEquals($model->saveAll(
  2485. array(
  2486. 'Comment' => array(
  2487. 'comment' => '',
  2488. 'article_id' => 1,
  2489. 'user_id' => 1
  2490. ),
  2491. 'Attachment' => array('attachment' => '')
  2492. ),
  2493. array('validate' => 'first')
  2494. ), false);
  2495. $expected = array(
  2496. 'Comment' => array('comment' => array('This field cannot be left blank')),
  2497. 'Attachment' => array('attachment' => array('This field cannot be left blank'))
  2498. );
  2499. $this->assertEqual($model->validationErrors, $expected['Comment']);
  2500. $this->assertEqual($model->Attachment->validationErrors, $expected['Attachment']);
  2501. $this->assertFalse($model->saveAll(
  2502. array(
  2503. 'Comment' => array('comment' => '', 'article_id' => 1, 'user_id' => 1),
  2504. 'Attachment' => array('attachment' => '')
  2505. ),
  2506. array('validate' => 'only')
  2507. ));
  2508. $this->assertEqual($model->validationErrors, $expected['Comment']);
  2509. $this->assertEqual($model->Attachment->validationErrors, $expected['Attachment']);
  2510. }
  2511. /**
  2512. * testSaveAllAtomic method
  2513. *
  2514. * @access public
  2515. * @return void
  2516. */
  2517. function testSaveAllAtomic() {
  2518. $this->loadFixtures('Article', 'User');
  2519. $TestModel = new Article();
  2520. $result = $TestModel->saveAll(array(
  2521. 'Article' => array(
  2522. 'title' => 'Post with Author',
  2523. 'body' => 'This post will be saved with an author',
  2524. 'user_id' => 2
  2525. ),
  2526. 'Comment' => array(
  2527. array('comment' => 'First new comment', 'user_id' => 2))
  2528. ), array('atomic' => false));
  2529. $this->assertIdentical($result, array('Article' => true, 'Comment' => array(true)));
  2530. $result = $TestModel->saveAll(array(
  2531. array(
  2532. 'id' => '1',
  2533. 'title' => 'Baleeted First Post',
  2534. 'body' => 'Baleeted!',
  2535. 'published' => 'N'
  2536. ),
  2537. array(
  2538. 'id' => '2',
  2539. 'title' => 'Just update the title'
  2540. ),
  2541. array(
  2542. 'title' => 'Creating a fourth post',
  2543. 'body' => 'Fourth post body',
  2544. 'user_id' => 2
  2545. )
  2546. ), array('atomic' => false));
  2547. $this->assertIdentical($result, array(true, true, true));
  2548. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  2549. $result = $TestModel->saveAll(array(
  2550. array(
  2551. 'id' => '1',
  2552. 'title' => 'Un-Baleeted First Post',
  2553. 'body' => 'Not Baleeted!',
  2554. 'published' => 'Y'
  2555. ),
  2556. array(
  2557. 'id' => '2',
  2558. 'title' => '',
  2559. 'body' => 'Trying to get away with an empty title'
  2560. )
  2561. ), array('validate' => true, 'atomic' => false));
  2562. $this->assertIdentical($result, array(true, false));
  2563. $result = $TestModel->saveAll(array(
  2564. 'Article' => array('id' => 2),
  2565. 'Comment' => array(
  2566. array(
  2567. 'comment' => 'First new comment',
  2568. 'published' => 'Y',
  2569. 'user_id' => 1
  2570. ),
  2571. array(
  2572. 'comment' => 'Second new comment',
  2573. 'published' => 'Y',
  2574. 'user_id' => 2
  2575. ))
  2576. ), array('validate' => true, 'atomic' => false));
  2577. $this->assertIdentical($result, array('Article' => true, 'Comment' => array(true, true)));
  2578. }
  2579. /**
  2580. * testSaveAllHasMany method
  2581. *
  2582. * @access public
  2583. * @return void
  2584. */
  2585. function testSaveAllHasMany() {
  2586. $this->loadFixtures('Article', 'Comment');
  2587. $TestModel = new Article();
  2588. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  2589. $result = $TestModel->saveAll(array(
  2590. 'Article' => array('id' => 2),
  2591. 'Comment' => array(
  2592. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  2593. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  2594. )
  2595. ));
  2596. $this->assertFalse(empty($result));
  2597. $result = $TestModel->findById(2);
  2598. $expected = array(
  2599. 'First Comment for Second Article',
  2600. 'Second Comment for Second Article',
  2601. 'First new comment',
  2602. 'Second new comment'
  2603. );
  2604. $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected);
  2605. $result = $TestModel->saveAll(
  2606. array(
  2607. 'Article' => array('id' => 2),
  2608. 'Comment' => array(
  2609. array(
  2610. 'comment' => 'Third new comment',
  2611. 'published' => 'Y',
  2612. 'user_id' => 1
  2613. ))),
  2614. array('atomic' => false)
  2615. );
  2616. $this->assertFalse(empty($result));
  2617. $result = $TestModel->findById(2);
  2618. $expected = array(
  2619. 'First Comment for Second Article',
  2620. 'Second Comment for Second Article',
  2621. 'First new comment',
  2622. 'Second new comment',
  2623. 'Third new comment'
  2624. );
  2625. $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected);
  2626. $TestModel->beforeSaveReturn = false;
  2627. $result = $TestModel->saveAll(
  2628. array(
  2629. 'Article' => array('id' => 2),
  2630. 'Comment' => array(
  2631. array(
  2632. 'comment' => 'Fourth new comment',
  2633. 'published' => 'Y',
  2634. 'user_id' => 1
  2635. ))),
  2636. array('atomic' => false)
  2637. );
  2638. $this->assertEqual($result, array('Article' => false));
  2639. $result = $TestModel->findById(2);
  2640. $expected = array(
  2641. 'First Comment for Second Article',
  2642. 'Second Comment for Second Article',
  2643. 'First new comment',
  2644. 'Second new comment',
  2645. 'Third new comment'
  2646. );
  2647. $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected);
  2648. }
  2649. /**
  2650. * testSaveAllHasManyValidation method
  2651. *
  2652. * @access public
  2653. * @return void
  2654. */
  2655. function testSaveAllHasManyValidation() {
  2656. $this->loadFixtures('Article', 'Comment');
  2657. $TestModel = new Article();
  2658. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  2659. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  2660. $result = $TestModel->saveAll(array(
  2661. 'Article' => array('id' => 2),
  2662. 'Comment' => array(
  2663. array('comment' => '', 'published' => 'Y', 'user_id' => 1),
  2664. )
  2665. ), array('validate' => true));
  2666. $expected = array('Comment' => array(false));
  2667. $this->assertEqual($result, $expected);
  2668. $expected = array('Comment' => array(
  2669. array('comment' => array('This field cannot be left blank'))
  2670. ));
  2671. $this->assertEqual($TestModel->validationErrors, $expected);
  2672. $expected = array(
  2673. array('comment' => array('This field cannot be left blank'))
  2674. );
  2675. $this->assertEqual($TestModel->Comment->validationErrors, $expected);
  2676. $result = $TestModel->saveAll(array(
  2677. 'Article' => array('id' => 2),
  2678. 'Comment' => array(
  2679. array(
  2680. 'comment' => '',
  2681. 'published' => 'Y',
  2682. 'user_id' => 1
  2683. ))
  2684. ), array('validate' => 'first'));
  2685. $this->assertFalse($result);
  2686. }
  2687. /**
  2688. * test saveAll with transactions and ensure there is no missing rollback.
  2689. *
  2690. * @return void
  2691. */
  2692. function testSaveAllManyRowsTransactionNoRollback() {
  2693. $this->loadFixtures('Post');
  2694. $this->getMock('DboSource', array(), array(), 'MockTransactionDboSource');
  2695. $db = ConnectionManager::create('mock_transaction', array(
  2696. 'datasource' => 'MockTransactionDboSource',
  2697. ));
  2698. $db->expects($this->once())
  2699. ->method('describe')
  2700. ->will($this->returnValue(array()));
  2701. $db->expects($this->once())->method('rollback');
  2702. $Post = new Post('mock_transaction');
  2703. $Post->validate = array(
  2704. 'title' => array('rule' => array('notEmpty'))
  2705. );
  2706. $data = array(
  2707. array('author_id' => 1, 'title' => 'New Fourth Post'),
  2708. array('author_id' => 1, 'title' => '')
  2709. );
  2710. $Post->saveAll($data, array('atomic' => true));
  2711. }
  2712. /**
  2713. * test saveAll with transactions and ensure there is no missing rollback.
  2714. *
  2715. * @return void
  2716. */
  2717. function testSaveAllAssociatedTransactionNoRollback() {
  2718. $testDb = ConnectionManager::getDataSource('test');
  2719. $mock = $this->getMock('DboSource', array(), array(), 'MockTransactionAssociatedDboSource', false);
  2720. $db = ConnectionManager::create('mock_transaction_assoc', array(
  2721. 'datasource' => 'MockTransactionAssociatedDboSource',
  2722. ));
  2723. $this->mockObjects[] = $db;
  2724. $db->columns = $testDb->columns;
  2725. $db->expects($this->once())->method('rollback');
  2726. $db->expects($this->any())->method('describe')
  2727. ->will($this->returnValue(array(
  2728. 'id' => array('type' => 'integer'),
  2729. 'title' => array('type' => 'string'),
  2730. 'body' => array('type' => 'text'),
  2731. 'published' => array('type' => 'string')
  2732. )));
  2733. $Post = new Post();
  2734. $Post->useDbConfig = 'mock_transaction_assoc';
  2735. $Post->Author->useDbConfig = 'mock_transaction_assoc';
  2736. $Post->Author->validate = array(
  2737. 'user' => array('rule' => array('notEmpty'))
  2738. );
  2739. $data = array(
  2740. 'Post' => array(
  2741. 'title' => 'New post',
  2742. 'body' => 'Content',
  2743. 'published' => 'Y'
  2744. ),
  2745. 'Author' => array(
  2746. 'user' => '',
  2747. 'password' => "sekret"
  2748. )
  2749. );
  2750. $Post->saveAll($data);
  2751. }
  2752. /**
  2753. * test saveAll with nested saveAll call.
  2754. *
  2755. * @return void
  2756. */
  2757. function testSaveAllNestedSaveAll() {
  2758. $this->loadFixtures('Sample');
  2759. $TransactionTestModel = new TransactionTestModel();
  2760. $data = array(
  2761. array('apple_id' => 1, 'name' => 'sample5'),
  2762. );
  2763. $this->assertTrue($TransactionTestModel->saveAll($data, array('atomic' => true)));
  2764. }
  2765. /**
  2766. * testSaveAllTransaction method
  2767. *
  2768. * @access public
  2769. * @return void
  2770. */
  2771. function testSaveAllTransaction() {
  2772. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  2773. $TestModel = new Post();
  2774. $TestModel->validate = array('title' => 'notEmpty');
  2775. $data = array(
  2776. array('author_id' => 1, 'title' => 'New Fourth Post'),
  2777. array('author_id' => 1, 'title' => 'New Fifth Post'),
  2778. array('author_id' => 1, 'title' => '')
  2779. );
  2780. $ts = date('Y-m-d H:i:s');
  2781. $this->assertFalse($TestModel->saveAll($data));
  2782. $result = $TestModel->find('all', array('recursive' => -1));
  2783. $expected = array(
  2784. array('Post' => array(
  2785. 'id' => '1',
  2786. 'author_id' => 1,
  2787. 'title' => 'First Post',
  2788. 'body' => 'First Post Body',
  2789. 'published' => 'Y',
  2790. 'created' => '2007-03-18 10:39:23',
  2791. 'updated' => '2007-03-18 10:41:31'
  2792. )),
  2793. array('Post' => array(
  2794. 'id' => '2',
  2795. 'author_id' => 3,
  2796. 'title' => 'Second Post',
  2797. 'body' => 'Second Post Body',
  2798. 'published' => 'Y',
  2799. 'created' => '2007-03-18 10:41:23',
  2800. 'updated' => '2007-03-18 10:43:31'
  2801. )),
  2802. array('Post' => array(
  2803. 'id' => '3',
  2804. 'author_id' => 1,
  2805. 'title' => 'Third Post',
  2806. 'body' => 'Third Post Body',
  2807. 'published' => 'Y',
  2808. 'created' => '2007-03-18 10:43:23',
  2809. 'updated' => '2007-03-18 10:45:31'
  2810. )));
  2811. if (count($result) != 3) {
  2812. // Database doesn't support transactions
  2813. $expected[] = array(
  2814. 'Post' => array(
  2815. 'id' => '4',
  2816. 'author_id' => 1,
  2817. 'title' => 'New Fourth Post',
  2818. 'body' => null,
  2819. 'published' => 'N',
  2820. 'created' => $ts,
  2821. 'updated' => $ts
  2822. ));
  2823. $expected[] = array(
  2824. 'Post' => array(
  2825. 'id' => '5',
  2826. 'author_id' => 1,
  2827. 'title' => 'New Fifth Post',
  2828. 'body' => null,
  2829. 'published' => 'N',
  2830. 'created' => $ts,
  2831. 'updated' => $ts
  2832. ));
  2833. $this->assertEqual($result, $expected);
  2834. // Skip the rest of the transactional tests
  2835. return;
  2836. }
  2837. $this->assertEqual($result, $expected);
  2838. $data = array(
  2839. array('author_id' => 1, 'title' => 'New Fourth Post'),
  2840. array('author_id' => 1, 'title' => ''),
  2841. array('author_id' => 1, 'title' => 'New Sixth Post')
  2842. );
  2843. $ts = date('Y-m-d H:i:s');
  2844. $this->assertFalse($TestModel->saveAll($data));
  2845. $result = $TestModel->find('all', array('recursive' => -1));
  2846. $expected = array(
  2847. array('Post' => array(
  2848. 'id' => '1',
  2849. 'author_id' => 1,
  2850. 'title' => 'First Post',
  2851. 'body' => 'First Post Body',
  2852. 'published' => 'Y',
  2853. 'created' => '2007-03-18 10:39:23',
  2854. 'updated' => '2007-03-18 10:41:31'
  2855. )),
  2856. array('Post' => array(
  2857. 'id' => '2',
  2858. 'author_id' => 3,
  2859. 'title' => 'Second Post',
  2860. 'body' => 'Second Post Body',
  2861. 'published' => 'Y',
  2862. 'created' => '2007-03-18 10:41:23',
  2863. 'updated' => '2007-03-18 10:43:31'
  2864. )),
  2865. array('Post' => array(
  2866. 'id' => '3',
  2867. 'author_id' => 1,
  2868. 'title' => 'Third Post',
  2869. 'body' => 'Third Post Body',
  2870. 'published' => 'Y',
  2871. 'created' => '2007-03-18 10:43:23',
  2872. 'updated' => '2007-03-18 10:45:31'
  2873. )));
  2874. if (count($result) != 3) {
  2875. // Database doesn't support transactions
  2876. $expected[] = array(
  2877. 'Post' => array(
  2878. 'id' => '4',
  2879. 'author_id' => 1,
  2880. 'title' => 'New Fourth Post',
  2881. 'body' => 'Third Post Body',
  2882. 'published' => 'N',
  2883. 'created' => $ts,
  2884. 'updated' => $ts
  2885. ));
  2886. $expected[] = array(
  2887. 'Post' => array(
  2888. 'id' => '5',
  2889. 'author_id' => 1,
  2890. 'title' => 'Third Post',
  2891. 'body' => 'Third Post Body',
  2892. 'published' => 'N',
  2893. 'created' => $ts,
  2894. 'updated' => $ts
  2895. ));
  2896. }
  2897. $this->assertEqual($result, $expected);
  2898. $TestModel->validate = array('title' => 'notEmpty');
  2899. $data = array(
  2900. array('author_id' => 1, 'title' => 'New Fourth Post'),
  2901. array('author_id' => 1, 'title' => 'New Fifth Post'),
  2902. array('author_id' => 1, 'title' => 'New Sixth Post')
  2903. );
  2904. $this->assertTrue($TestModel->saveAll($data));
  2905. $result = $TestModel->find('all', array(
  2906. 'recursive' => -1,
  2907. 'fields' => array('author_id', 'title','body','published')
  2908. ));
  2909. $expected = array(
  2910. array('Post' => array(
  2911. 'author_id' => 1,
  2912. 'title' => 'First Post',
  2913. 'body' => 'First Post Body',
  2914. 'published' => 'Y'
  2915. )),
  2916. array('Post' => array(
  2917. 'author_id' => 3,
  2918. 'title' => 'Second Post',
  2919. 'body' => 'Second Post Body',
  2920. 'published' => 'Y'
  2921. )),
  2922. array('Post' => array(
  2923. 'author_id' => 1,
  2924. 'title' => 'Third Post',
  2925. 'body' => 'Third Post Body',
  2926. 'published' => 'Y'
  2927. )),
  2928. array('Post' => array(
  2929. 'author_id' => 1,
  2930. 'title' => 'New Fourth Post',
  2931. 'body' => '',
  2932. 'published' => 'N'
  2933. )),
  2934. array('Post' => array(
  2935. 'author_id' => 1,
  2936. 'title' => 'New Fifth Post',
  2937. 'body' => '',
  2938. 'published' => 'N'
  2939. )),
  2940. array('Post' => array(
  2941. 'author_id' => 1,
  2942. 'title' => 'New Sixth Post',
  2943. 'body' => '',
  2944. 'published' => 'N'
  2945. )));
  2946. $this->assertEqual($result, $expected);
  2947. }
  2948. /**
  2949. * testSaveAllValidation method
  2950. *
  2951. * @access public
  2952. * @return void
  2953. */
  2954. function testSaveAllValidation() {
  2955. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  2956. $TestModel = new Post();
  2957. $data = array(
  2958. array(
  2959. 'id' => '1',
  2960. 'title' => 'Baleeted First Post',
  2961. 'body' => 'Baleeted!',
  2962. 'published' => 'N'
  2963. ),
  2964. array(
  2965. 'id' => '2',
  2966. 'title' => 'Just update the title'
  2967. ),
  2968. array(
  2969. 'title' => 'Creating a fourth post',
  2970. 'body' => 'Fourth post body',
  2971. 'author_id' => 2
  2972. ));
  2973. $this->assertTrue($TestModel->saveAll($data));
  2974. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  2975. $ts = date('Y-m-d H:i:s');
  2976. $expected = array(
  2977. array(
  2978. 'Post' => array(
  2979. 'id' => '1',
  2980. 'author_id' => '1',
  2981. 'title' => 'Baleeted First Post',
  2982. 'body' => 'Baleeted!',
  2983. 'published' => 'N',
  2984. 'created' => '2007-03-18 10:39:23',
  2985. 'updated' => $ts
  2986. )),
  2987. array(
  2988. 'Post' => array(
  2989. 'id' => '2',
  2990. 'author_id' => '3',
  2991. 'title' => 'Just update the title',
  2992. 'body' => 'Second Post Body',
  2993. 'published' => 'Y',
  2994. 'created' => '2007-03-18 10:41:23', 'updated' => $ts
  2995. )),
  2996. array(
  2997. 'Post' => array(
  2998. 'id' => '3',
  2999. 'author_id' => '1',
  3000. 'title' => 'Third Post',
  3001. 'body' => 'Third Post Body',
  3002. 'published' => 'Y',
  3003. 'created' => '2007-03-18 10:43:23',
  3004. 'updated' => '2007-03-18 10:45:31'
  3005. )),
  3006. array(
  3007. 'Post' => array(
  3008. 'id' => '4',
  3009. 'author_id' => '2',
  3010. 'title' => 'Creating a fourth post',
  3011. 'body' => 'Fourth post body',
  3012. 'published' => 'N',
  3013. 'created' => $ts,
  3014. 'updated' => $ts
  3015. )));
  3016. $this->assertEqual($result, $expected);
  3017. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  3018. $data = array(
  3019. array(
  3020. 'id' => '1',
  3021. 'title' => 'Un-Baleeted First Post',
  3022. 'body' => 'Not Baleeted!',
  3023. 'published' => 'Y'
  3024. ),
  3025. array(
  3026. 'id' => '2',
  3027. 'title' => '',
  3028. 'body' => 'Trying to get away with an empty title'
  3029. ));
  3030. $result = $TestModel->saveAll($data);
  3031. $this->assertFalse($result);
  3032. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3033. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  3034. $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
  3035. if (!$transactionWorked) {
  3036. $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
  3037. $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
  3038. }
  3039. $this->assertEqual($TestModel->validationErrors, $errors);
  3040. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  3041. $data = array(
  3042. array(
  3043. 'id' => '1',
  3044. 'title' => 'Un-Baleeted First Post',
  3045. 'body' => 'Not Baleeted!',
  3046. 'published' => 'Y'
  3047. ),
  3048. array(
  3049. 'id' => '2',
  3050. 'title' => '',
  3051. 'body' => 'Trying to get away with an empty title'
  3052. ));
  3053. $result = $TestModel->saveAll($data, array('validate' => true, 'atomic' => false));
  3054. $this->assertEqual($result, array(true, false));
  3055. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3056. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  3057. $newTs = date('Y-m-d H:i:s');
  3058. $expected = array(
  3059. array(
  3060. 'Post' => array(
  3061. 'id' => '1',
  3062. 'author_id' => '1',
  3063. 'title' => 'Un-Baleeted First Post',
  3064. 'body' => 'Not Baleeted!',
  3065. 'published' => 'Y',
  3066. 'created' => '2007-03-18 10:39:23',
  3067. 'updated' => $newTs
  3068. )),
  3069. array(
  3070. 'Post' => array(
  3071. 'id' => '2',
  3072. 'author_id' => '3',
  3073. 'title' => 'Just update the title',
  3074. 'body' => 'Second Post Body',
  3075. 'published' => 'Y',
  3076. 'created' => '2007-03-18 10:41:23',
  3077. 'updated' => $ts
  3078. )),
  3079. array(
  3080. 'Post' => array(
  3081. 'id' => '3',
  3082. 'author_id' => '1',
  3083. 'title' => 'Third Post',
  3084. 'body' => 'Third Post Body',
  3085. 'published' => 'Y',
  3086. 'created' => '2007-03-18 10:43:23',
  3087. 'updated' => '2007-03-18 10:45:31'
  3088. )),
  3089. array(
  3090. 'Post' => array(
  3091. 'id' => '4',
  3092. 'author_id' => '2',
  3093. 'title' => 'Creating a fourth post',
  3094. 'body' => 'Fourth post body',
  3095. 'published' => 'N',
  3096. 'created' => $ts,
  3097. 'updated' => $ts
  3098. )));
  3099. $this->assertEqual($result, $expected);
  3100. $this->assertEqual($TestModel->validationErrors, $errors);
  3101. $data = array(
  3102. array(
  3103. 'id' => '1',
  3104. 'title' => 'Re-Baleeted First Post',
  3105. 'body' => 'Baleeted!',
  3106. 'published' => 'N'
  3107. ),
  3108. array(
  3109. 'id' => '2',
  3110. 'title' => '',
  3111. 'body' => 'Trying to get away with an empty title'
  3112. ));
  3113. $this->assertFalse($TestModel->saveAll($data, array('validate' => 'first')));
  3114. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3115. $this->assertEqual($result, $expected);
  3116. $this->assertEqual($TestModel->validationErrors, $errors);
  3117. $data = array(
  3118. array(
  3119. 'title' => 'First new post',
  3120. 'body' => 'Woohoo!',
  3121. 'published' => 'Y'
  3122. ),
  3123. array(
  3124. 'title' => 'Empty body',
  3125. 'body' => ''
  3126. ));
  3127. $TestModel->validate['body'] = 'notEmpty';
  3128. }
  3129. /**
  3130. * testSaveAllValidationOnly method
  3131. *
  3132. * @access public
  3133. * @return void
  3134. */
  3135. function testSaveAllValidationOnly() {
  3136. $this->loadFixtures('Comment', 'Attachment');
  3137. $TestModel = new Comment();
  3138. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  3139. $data = array(
  3140. 'Comment' => array(
  3141. 'comment' => 'This is the comment'
  3142. ),
  3143. 'Attachment' => array(
  3144. 'attachment' => ''
  3145. )
  3146. );
  3147. $result = $TestModel->saveAll($data, array('validate' => 'only'));
  3148. $this->assertFalse($result);
  3149. $TestModel = new Article();
  3150. $TestModel->validate = array('title' => 'notEmpty');
  3151. $result = $TestModel->saveAll(
  3152. array(
  3153. 0 => array('title' => ''),
  3154. 1 => array('title' => 'title 1'),
  3155. 2 => array('title' => 'title 2'),
  3156. ),
  3157. array('validate'=>'only')
  3158. );
  3159. $this->assertFalse($result);
  3160. $expected = array(
  3161. 0 => array('title' => array('This field cannot be left blank')),
  3162. );
  3163. $this->assertEqual($TestModel->validationErrors, $expected);
  3164. $result = $TestModel->saveAll(
  3165. array(
  3166. 0 => array('title' => 'title 0'),
  3167. 1 => array('title' => ''),
  3168. 2 => array('title' => 'title 2'),
  3169. ),
  3170. array('validate'=>'only')
  3171. );
  3172. $this->assertFalse($result);
  3173. $expected = array(
  3174. 1 => array('title' => array('This field cannot be left blank')),
  3175. );
  3176. $this->assertEqual($TestModel->validationErrors, $expected);
  3177. }
  3178. /**
  3179. * testSaveAllValidateFirst method
  3180. *
  3181. * @access public
  3182. * @return void
  3183. */
  3184. function testSaveAllValidateFirst() {
  3185. $this->loadFixtures('Article', 'Comment', 'Attachment');
  3186. $model = new Article();
  3187. $model->deleteAll(true);
  3188. $model->Comment->validate = array('comment' => 'notEmpty');
  3189. $result = $model->saveAll(array(
  3190. 'Article' => array(
  3191. 'title' => 'Post with Author',
  3192. 'body' => 'This post will be saved author'
  3193. ),
  3194. 'Comment' => array(
  3195. array('comment' => 'First new comment'),
  3196. array('comment' => '')
  3197. )
  3198. ), array('validate' => 'first'));
  3199. $this->assertFalse($result);
  3200. $result = $model->find('all');
  3201. $this->assertEqual($result, array());
  3202. $expected = array('Comment' => array(
  3203. 1 => array('comment' => array('This field cannot be left blank'))
  3204. ));
  3205. $this->assertEqual($model->Comment->validationErrors, $expected['Comment']);
  3206. $this->assertIdentical($model->Comment->find('count'), 0);
  3207. $result = $model->saveAll(
  3208. array(
  3209. 'Article' => array(
  3210. 'title' => 'Post with Author',
  3211. 'body' => 'This post will be saved with an author',
  3212. 'user_id' => 2
  3213. ),
  3214. 'Comment' => array(
  3215. array(
  3216. 'comment' => 'Only new comment',
  3217. 'user_id' => 2
  3218. ))),
  3219. array('validate' => 'first')
  3220. );
  3221. $this->assertIdentical($result, true);
  3222. $result = $model->Comment->find('all');
  3223. $this->assertIdentical(count($result), 1);
  3224. $result = Set::extract('/Comment/article_id', $result);
  3225. $this->assertEquals($result[0], 4);
  3226. $model->deleteAll(true);
  3227. $data = array(
  3228. 'Article' => array(
  3229. 'title' => 'Post with Author saveAlled from comment',
  3230. 'body' => 'This post will be saved with an author',
  3231. 'user_id' => 2
  3232. ),
  3233. 'Comment' => array(
  3234. 'comment' => 'Only new comment', 'user_id' => 2
  3235. ));
  3236. $result = $model->Comment->saveAll($data, array('validate' => 'first'));
  3237. $this->assertFalse(empty($result));
  3238. $result = $model->find('all');
  3239. $this->assertEqual(
  3240. $result[0]['Article']['title'],
  3241. 'Post with Author saveAlled from comment'
  3242. );
  3243. $this->assertEqual($result[0]['Comment'][0]['comment'], 'Only new comment');
  3244. }
  3245. /**
  3246. * test saveAll()'s return is correct when using atomic = false and validate = first.
  3247. *
  3248. * @return void
  3249. */
  3250. function testSaveAllValidateFirstAtomicFalse() {
  3251. $Something = new Something();
  3252. $invalidData = array(
  3253. array(
  3254. 'title' => 'foo',
  3255. 'body' => 'bar',
  3256. 'published' => 'baz',
  3257. ),
  3258. array(
  3259. 'body' => 3,
  3260. 'published' =>'sd',
  3261. ),
  3262. );
  3263. $Something->create();
  3264. $Something->validate = array(
  3265. 'title' => array(
  3266. 'rule' => 'alphaNumeric',
  3267. 'required' => true,
  3268. ),
  3269. 'body' => array(
  3270. 'rule' => 'alphaNumeric',
  3271. 'required' => true,
  3272. 'allowEmpty' => true,
  3273. ),
  3274. );
  3275. $result = $Something->saveAll($invalidData, array(
  3276. 'atomic' => false,
  3277. 'validate' => 'first',
  3278. ));
  3279. $expected = array(true, false);
  3280. $this->assertEqual($result, $expected);
  3281. $Something = new Something();
  3282. $validData = array(
  3283. array(
  3284. 'title' => 'title value',
  3285. 'body' => 'body value',
  3286. 'published' => 'baz',
  3287. ),
  3288. array(
  3289. 'title' => 'valid',
  3290. 'body' => 'this body',
  3291. 'published' =>'sd',
  3292. ),
  3293. );
  3294. $Something->create();
  3295. $result = $Something->saveAll($validData, array(
  3296. 'atomic' => false,
  3297. 'validate' => 'first',
  3298. ));
  3299. $expected = array(true, true);
  3300. $this->assertEqual($result, $expected);
  3301. }
  3302. /**
  3303. * testUpdateWithCalculation method
  3304. *
  3305. * @access public
  3306. * @return void
  3307. */
  3308. function testUpdateWithCalculation() {
  3309. $this->loadFixtures('DataTest');
  3310. $model = new DataTest();
  3311. $model->deleteAll(true);
  3312. $result = $model->saveAll(array(
  3313. array('count' => 5, 'float' => 1.1),
  3314. array('count' => 3, 'float' => 1.2),
  3315. array('count' => 4, 'float' => 1.3),
  3316. array('count' => 1, 'float' => 2.0),
  3317. ));
  3318. $this->assertFalse(empty($result));
  3319. $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count')));
  3320. $this->assertEqual($result, array(5, 3, 4, 1));
  3321. $this->assertTrue($model->updateAll(array('count' => 'count + 2')));
  3322. $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count')));
  3323. $this->assertEqual($result, array(7, 5, 6, 3));
  3324. $this->assertTrue($model->updateAll(array('DataTest.count' => 'DataTest.count - 1')));
  3325. $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count')));
  3326. $this->assertEqual($result, array(6, 4, 5, 2));
  3327. }
  3328. /**
  3329. * testSaveAllHasManyValidationOnly method
  3330. *
  3331. * @access public
  3332. * @return void
  3333. */
  3334. function testSaveAllHasManyValidationOnly() {
  3335. $this->loadFixtures('Article', 'Comment', 'Attachment');
  3336. $TestModel = new Article();
  3337. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  3338. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  3339. $result = $TestModel->saveAll(
  3340. array(
  3341. 'Article' => array('id' => 2),
  3342. 'Comment' => array(
  3343. array(
  3344. 'id' => 1,
  3345. 'comment' => '',
  3346. 'published' => 'Y',
  3347. 'user_id' => 1),
  3348. array(
  3349. 'id' => 2,
  3350. 'comment' =>
  3351. 'comment',
  3352. 'published' => 'Y',
  3353. 'user_id' => 1
  3354. ))),
  3355. array('validate' => 'only')
  3356. );
  3357. $this->assertFalse($result);
  3358. $result = $TestModel->saveAll(
  3359. array(
  3360. 'Article' => array('id' => 2),
  3361. 'Comment' => array(
  3362. array(
  3363. 'id' => 1,
  3364. 'comment' => '',
  3365. 'published' => 'Y',
  3366. 'user_id' => 1
  3367. ),
  3368. array(
  3369. 'id' => 2,
  3370. 'comment' => 'comment',
  3371. 'published' => 'Y',
  3372. 'user_id' => 1
  3373. ),
  3374. array(
  3375. 'id' => 3,
  3376. 'comment' => '',
  3377. 'published' => 'Y',
  3378. 'user_id' => 1
  3379. ))),
  3380. array(
  3381. 'validate' => 'only',
  3382. 'atomic' => false
  3383. ));
  3384. $expected = array(
  3385. 'Article' => true,
  3386. 'Comment' => array(false, true, false)
  3387. );
  3388. $this->assertIdentical($result, $expected);
  3389. $expected = array('Comment' => array(
  3390. 0 => array('comment' => array('This field cannot be left blank')),
  3391. 2 => array('comment' => array('This field cannot be left blank'))
  3392. ));
  3393. $this->assertEqual($TestModel->validationErrors, $expected);
  3394. $expected = array(
  3395. 0 => array('comment' => array('This field cannot be left blank')),
  3396. 2 => array('comment' => array('This field cannot be left blank'))
  3397. );
  3398. $this->assertEqual($TestModel->Comment->validationErrors, $expected);
  3399. }
  3400. /**
  3401. * TestFindAllWithoutForeignKey
  3402. *
  3403. * @access public
  3404. * @return void
  3405. */
  3406. function testFindAllForeignKey() {
  3407. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  3408. $ProductUpdateAll = new ProductUpdateAll();
  3409. $conditions = array('Group.name' => 'group one');
  3410. $ProductUpdateAll->bindModel(array(
  3411. 'belongsTo' => array(
  3412. 'Group' => array('className' => 'GroupUpdateAll')
  3413. )
  3414. ));
  3415. $ProductUpdateAll->belongsTo = array(
  3416. 'Group' => array('className' => 'GroupUpdateAll', 'foreignKey' => 'group_id')
  3417. );
  3418. $results = $ProductUpdateAll->find('all', compact('conditions'));
  3419. $this->assertTrue(!empty($results));
  3420. $ProductUpdateAll->bindModel(array('belongsTo'=>array('Group')));
  3421. $ProductUpdateAll->belongsTo = array(
  3422. 'Group' => array(
  3423. 'className' => 'GroupUpdateAll',
  3424. 'foreignKey' => false,
  3425. 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
  3426. ));
  3427. $resultsFkFalse = $ProductUpdateAll->find('all', compact('conditions'));
  3428. $this->assertTrue(!empty($resultsFkFalse));
  3429. $expected = array(
  3430. '0' => array(
  3431. 'ProductUpdateAll' => array(
  3432. 'id' => 1,
  3433. 'name' => 'product one',
  3434. 'groupcode' => 120,
  3435. 'group_id' => 1),
  3436. 'Group' => array(
  3437. 'id' => 1,
  3438. 'name' => 'group one',
  3439. 'code' => 120)
  3440. ),
  3441. '1' => array(
  3442. 'ProductUpdateAll' => array(
  3443. 'id' => 2,
  3444. 'name' => 'product two',
  3445. 'groupcode' => 120,
  3446. 'group_id' => 1),
  3447. 'Group' => array(
  3448. 'id' => 1,
  3449. 'name' => 'group one',
  3450. 'code' => 120)
  3451. )
  3452. );
  3453. $this->assertEqual($results, $expected);
  3454. $this->assertEqual($resultsFkFalse, $expected);
  3455. }
  3456. /**
  3457. * test updateAll with empty values.
  3458. *
  3459. * @return void
  3460. */
  3461. function testUpdateAllEmptyValues() {
  3462. $this->loadFixtures('Author', 'Post');
  3463. $model = new Author();
  3464. $result = $model->updateAll(array('user' => '""'));
  3465. $this->assertTrue($result);
  3466. }
  3467. /**
  3468. * testUpdateAllWithJoins
  3469. *
  3470. * @access public
  3471. * @return void
  3472. */
  3473. function testUpdateAllWithJoins() {
  3474. $this->skipIf(
  3475. !$this->db instanceof Mysql,
  3476. '%s Currently, there is no way of doing joins in an update statement in postgresql or sqlite'
  3477. );
  3478. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  3479. $ProductUpdateAll = new ProductUpdateAll();
  3480. $conditions = array('Group.name' => 'group one');
  3481. $ProductUpdateAll->bindModel(array('belongsTo' => array(
  3482. 'Group' => array('className' => 'GroupUpdateAll')))
  3483. );
  3484. $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
  3485. $results = $ProductUpdateAll->find('all', array(
  3486. 'conditions' => array('ProductUpdateAll.name' => 'new product')
  3487. ));
  3488. $expected = array(
  3489. '0' => array(
  3490. 'ProductUpdateAll' => array(
  3491. 'id' => 1,
  3492. 'name' => 'new product',
  3493. 'groupcode' => 120,
  3494. 'group_id' => 1),
  3495. 'Group' => array(
  3496. 'id' => 1,
  3497. 'name' => 'group one',
  3498. 'code' => 120)
  3499. ),
  3500. '1' => array(
  3501. 'ProductUpdateAll' => array(
  3502. 'id' => 2,
  3503. 'name' => 'new product',
  3504. 'groupcode' => 120,
  3505. 'group_id' => 1),
  3506. 'Group' => array(
  3507. 'id' => 1,
  3508. 'name' => 'group one',
  3509. 'code' => 120)));
  3510. $this->assertEqual($results, $expected);
  3511. }
  3512. /**
  3513. * testUpdateAllWithoutForeignKey
  3514. *
  3515. * @access public
  3516. * @return void
  3517. */
  3518. function testUpdateAllWithoutForeignKey() {
  3519. $this->skipIf(
  3520. !$this->db instanceof Mysql,
  3521. '%s Currently, there is no way of doing joins in an update statement in postgresql'
  3522. );
  3523. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  3524. $ProductUpdateAll = new ProductUpdateAll();
  3525. $conditions = array('Group.name' => 'group one');
  3526. $ProductUpdateAll->bindModel(array('belongsTo' => array(
  3527. 'Group' => array('className' => 'GroupUpdateAll')
  3528. )));
  3529. $ProductUpdateAll->belongsTo = array(
  3530. 'Group' => array(
  3531. 'className' => 'GroupUpdateAll',
  3532. 'foreignKey' => false,
  3533. 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
  3534. )
  3535. );
  3536. $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
  3537. $resultsFkFalse = $ProductUpdateAll->find('all', array('conditions' => array('ProductUpdateAll.name'=>'new product')));
  3538. $expected = array(
  3539. '0' => array(
  3540. 'ProductUpdateAll' => array(
  3541. 'id' => 1,
  3542. 'name' => 'new product',
  3543. 'groupcode' => 120,
  3544. 'group_id' => 1),
  3545. 'Group' => array(
  3546. 'id' => 1,
  3547. 'name' => 'group one',
  3548. 'code' => 120)
  3549. ),
  3550. '1' => array(
  3551. 'ProductUpdateAll' => array(
  3552. 'id' => 2,
  3553. 'name' => 'new product',
  3554. 'groupcode' => 120,
  3555. 'group_id' => 1),
  3556. 'Group' => array(
  3557. 'id' => 1,
  3558. 'name' => 'group one',
  3559. 'code' => 120)));
  3560. $this->assertEqual($resultsFkFalse, $expected);
  3561. }
  3562. /**
  3563. * test that saveAll behaves like plain save() when suplied empty data
  3564. *
  3565. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  3566. * @access public
  3567. * @return void
  3568. */
  3569. function testSaveAllEmptyData() {
  3570. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  3571. $model = new Article();
  3572. $result = $model->saveAll(array(), array('validate' => 'first'));
  3573. $this->assertFalse(empty($result));
  3574. $model = new ProductUpdateAll();
  3575. $result = $model->saveAll(array());
  3576. $this->assertFalse($result);
  3577. }
  3578. /**
  3579. * test writing floats in german locale.
  3580. *
  3581. * @return void
  3582. */
  3583. function testWriteFloatAsGerman() {
  3584. $restore = setlocale(LC_ALL, null);
  3585. setlocale(LC_ALL, 'de_DE');
  3586. $model = new DataTest();
  3587. $result = $model->save(array(
  3588. 'count' => 1,
  3589. 'float' => 3.14593
  3590. ));
  3591. $this->assertTrue((bool)$result);
  3592. setlocale(LC_ALL, $restore);
  3593. }
  3594. }