ModelWriteTest.php 103 KB

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