ModelWriteTest.php 139 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332
  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.Test.Case.Model
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
  20. /**
  21. * ModelWriteTest
  22. *
  23. * @package Cake.Test.Case.Model
  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. $lastInsertId = $TestModel->JoinAsJoinB->getLastInsertID();
  56. $data['id'] = $lastInsertId;
  57. $this->assertEquals($result, array('JoinAsJoinB' => $data));
  58. $this->assertTrue($lastInsertId != null);
  59. $result = $TestModel->JoinAsJoinB->findById(1);
  60. $expected = array(
  61. 'JoinAsJoinB' => array(
  62. 'id' => 1,
  63. 'join_a_id' => 1,
  64. 'join_b_id' => 2,
  65. 'other' => 'Data for Join A 1 Join B 2',
  66. 'created' => '2008-01-03 10:56:33',
  67. 'updated' => '2008-01-03 10:56:33'
  68. ));
  69. $this->assertEqual($expected, $result);
  70. $updatedValue = 'UPDATED Data for Join A 1 Join B 2';
  71. $TestModel->JoinAsJoinB->id = 1;
  72. $result = $TestModel->JoinAsJoinB->saveField('other', $updatedValue, false);
  73. $this->assertFalse(empty($result));
  74. $result = $TestModel->JoinAsJoinB->findById(1);
  75. $this->assertEqual($result['JoinAsJoinB']['other'], $updatedValue);
  76. }
  77. /**
  78. * testSaveDateAsFirstEntry method
  79. *
  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. * @return void
  106. */
  107. public function testUnderscoreFieldSave() {
  108. $this->loadFixtures('UnderscoreField');
  109. $UnderscoreField = new UnderscoreField();
  110. $currentCount = $UnderscoreField->find('count');
  111. $this->assertEqual($currentCount, 3);
  112. $data = array('UnderscoreField' => array(
  113. 'user_id' => '1',
  114. 'my_model_has_a_field' => 'Content here',
  115. 'body' => 'Body',
  116. 'published' => 'Y',
  117. 'another_field' => 4
  118. ));
  119. $ret = $UnderscoreField->save($data);
  120. $this->assertFalse(empty($ret));
  121. $currentCount = $UnderscoreField->find('count');
  122. $this->assertEqual($currentCount, 4);
  123. }
  124. /**
  125. * testAutoSaveUuid method
  126. *
  127. * @return void
  128. */
  129. public function testAutoSaveUuid() {
  130. // SQLite does not support non-integer primary keys
  131. $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with SQLite.');
  132. $this->loadFixtures('Uuid');
  133. $TestModel = new Uuid();
  134. $TestModel->save(array('title' => 'Test record'));
  135. $result = $TestModel->findByTitle('Test record');
  136. $this->assertEqual(
  137. array_keys($result['Uuid']),
  138. array('id', 'title', 'count', 'created', 'updated')
  139. );
  140. $this->assertEqual(strlen($result['Uuid']['id']), 36);
  141. }
  142. /**
  143. * Ensure that if the id key is null but present the save doesn't fail (with an
  144. * x sql error: "Column id specified twice")
  145. *
  146. * @return void
  147. */
  148. public function testSaveUuidNull() {
  149. // SQLite does not support non-integer primary keys
  150. $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with SQLite.');
  151. $this->loadFixtures('Uuid');
  152. $TestModel = new Uuid();
  153. $TestModel->save(array('title' => 'Test record', 'id' => null));
  154. $result = $TestModel->findByTitle('Test record');
  155. $this->assertEqual(
  156. array_keys($result['Uuid']),
  157. array('id', 'title', 'count', 'created', 'updated')
  158. );
  159. $this->assertEqual(strlen($result['Uuid']['id']), 36);
  160. }
  161. /**
  162. * testZeroDefaultFieldValue method
  163. *
  164. * @return void
  165. */
  166. public function testZeroDefaultFieldValue() {
  167. $this->skipIf($this->db instanceof Sqlite, 'SQLite uses loose typing, this operation is unsupported.');
  168. $this->loadFixtures('DataTest');
  169. $TestModel = new DataTest();
  170. $TestModel->create(array());
  171. $TestModel->save();
  172. $result = $TestModel->findById($TestModel->id);
  173. $this->assertEquals($result['DataTest']['count'], 0);
  174. $this->assertEquals($result['DataTest']['float'], 0);
  175. }
  176. /**
  177. * Tests validation parameter order in custom validation methods
  178. *
  179. * @return void
  180. */
  181. public function testAllowSimulatedFields() {
  182. $TestModel = new ValidationTest1();
  183. $TestModel->create(array(
  184. 'title' => 'foo',
  185. 'bar' => 'baz'
  186. ));
  187. $expected = array(
  188. 'ValidationTest1' => array(
  189. 'title' => 'foo',
  190. 'bar' => 'baz'
  191. ));
  192. $this->assertEqual($TestModel->data, $expected);
  193. }
  194. /**
  195. * test that Caches are getting cleared on save().
  196. * ensure that both inflections of controller names are getting cleared
  197. * as url for controller could be either overallFavorites/index or overall_favorites/index
  198. *
  199. * @return void
  200. */
  201. public function testCacheClearOnSave() {
  202. $_back = array(
  203. 'check' => Configure::read('Cache.check'),
  204. 'disable' => Configure::read('Cache.disable'),
  205. );
  206. Configure::write('Cache.check', true);
  207. Configure::write('Cache.disable', false);
  208. $this->loadFixtures('OverallFavorite');
  209. $OverallFavorite = new OverallFavorite();
  210. touch(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php');
  211. touch(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php');
  212. $data = array(
  213. 'OverallFavorite' => array(
  214. 'id' => 22,
  215. 'model_type' => '8-track',
  216. 'model_id' => '3',
  217. 'priority' => '1'
  218. )
  219. );
  220. $OverallFavorite->create($data);
  221. $OverallFavorite->save();
  222. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php'));
  223. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php'));
  224. Configure::write('Cache.check', $_back['check']);
  225. Configure::write('Cache.disable', $_back['disable']);
  226. }
  227. /**
  228. * testSaveWithCounterCache method
  229. *
  230. * @return void
  231. */
  232. public function testSaveWithCounterCache() {
  233. $this->loadFixtures('Syfile', 'Item', 'Image', 'Portfolio', 'ItemsPortfolio');
  234. $TestModel = new Syfile();
  235. $TestModel2 = new Item();
  236. $result = $TestModel->findById(1);
  237. $this->assertIdentical($result['Syfile']['item_count'], null);
  238. $TestModel2->save(array(
  239. 'name' => 'Item 7',
  240. 'syfile_id' => 1,
  241. 'published' => false
  242. ));
  243. $result = $TestModel->findById(1);
  244. $this->assertEquals($result['Syfile']['item_count'], 2);
  245. $TestModel2->delete(1);
  246. $result = $TestModel->findById(1);
  247. $this->assertEquals($result['Syfile']['item_count'], 1);
  248. $TestModel2->id = 2;
  249. $TestModel2->saveField('syfile_id', 1);
  250. $result = $TestModel->findById(1);
  251. $this->assertEquals($result['Syfile']['item_count'], 2);
  252. $result = $TestModel->findById(2);
  253. $this->assertEquals($result['Syfile']['item_count'], 0);
  254. }
  255. /**
  256. * Tests that counter caches are updated when records are added
  257. *
  258. * @return void
  259. */
  260. public function testCounterCacheIncrease() {
  261. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  262. $User = new CounterCacheUser();
  263. $Post = new CounterCachePost();
  264. $data = array('Post' => array(
  265. 'id' => 22,
  266. 'title' => 'New Post',
  267. 'user_id' => 66
  268. ));
  269. $Post->save($data);
  270. $user = $User->find('first', array(
  271. 'conditions' => array('id' => 66),
  272. 'recursive' => -1
  273. ));
  274. $result = $user[$User->alias]['post_count'];
  275. $expected = 3;
  276. $this->assertEqual($expected, $result);
  277. }
  278. /**
  279. * Tests that counter caches are updated when records are deleted
  280. *
  281. * @return void
  282. */
  283. public function testCounterCacheDecrease() {
  284. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  285. $User = new CounterCacheUser();
  286. $Post = new CounterCachePost();
  287. $Post->delete(2);
  288. $user = $User->find('first', array(
  289. 'conditions' => array('id' => 66),
  290. 'recursive' => -1
  291. ));
  292. $result = $user[$User->alias]['post_count'];
  293. $expected = 1;
  294. $this->assertEqual($expected, $result);
  295. }
  296. /**
  297. * Tests that counter caches are updated when foreign keys of counted records change
  298. *
  299. * @return void
  300. */
  301. public function testCounterCacheUpdated() {
  302. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  303. $User = new CounterCacheUser();
  304. $Post = new CounterCachePost();
  305. $data = $Post->find('first', array(
  306. 'conditions' => array('id' => 1),
  307. 'recursive' => -1
  308. ));
  309. $data[$Post->alias]['user_id'] = 301;
  310. $Post->save($data);
  311. $users = $User->find('all',array('order' => 'User.id'));
  312. $this->assertEqual($users[0]['User']['post_count'], 1);
  313. $this->assertEqual($users[1]['User']['post_count'], 2);
  314. }
  315. /**
  316. * Test counter cache with models that use a non-standard (i.e. not using 'id')
  317. * as their primary key.
  318. *
  319. * @return void
  320. */
  321. public function testCounterCacheWithNonstandardPrimaryKey() {
  322. $this->loadFixtures(
  323. 'CounterCacheUserNonstandardPrimaryKey',
  324. 'CounterCachePostNonstandardPrimaryKey'
  325. );
  326. $User = new CounterCacheUserNonstandardPrimaryKey();
  327. $Post = new CounterCachePostNonstandardPrimaryKey();
  328. $data = $Post->find('first', array(
  329. 'conditions' => array('pid' => 1),
  330. 'recursive' => -1
  331. ));
  332. $data[$Post->alias]['uid'] = 301;
  333. $Post->save($data);
  334. $users = $User->find('all',array('order' => 'User.uid'));
  335. $this->assertEqual($users[0]['User']['post_count'], 1);
  336. $this->assertEqual($users[1]['User']['post_count'], 2);
  337. }
  338. /**
  339. * test Counter Cache With Self Joining table
  340. *
  341. * @return void
  342. */
  343. public function testCounterCacheWithSelfJoin() {
  344. $this->skipIf($this->db instanceof Sqlite, 'SQLite 2.x does not support ALTER TABLE ADD COLUMN');
  345. $this->loadFixtures('CategoryThread');
  346. $column = 'COLUMN ';
  347. if ($this->db instanceof Sqlserver) {
  348. $column = '';
  349. }
  350. $column .= $this->db->buildColumn(array('name' => 'child_count', 'type' => 'integer'));
  351. $this->db->query('ALTER TABLE '. $this->db->fullTableName('category_threads') . ' ADD ' . $column);
  352. $this->db->flushMethodCache();
  353. $Category = new CategoryThread();
  354. $result = $Category->updateAll(array('CategoryThread.name' => "'updated'"), array('CategoryThread.parent_id' => 5));
  355. $this->assertFalse(empty($result));
  356. $Category = new CategoryThread();
  357. $Category->belongsTo['ParentCategory']['counterCache'] = 'child_count';
  358. $Category->updateCounterCache(array('parent_id' => 5));
  359. $result = Set::extract($Category->find('all', array('conditions' => array('CategoryThread.id' => 5))), '{n}.CategoryThread.child_count');
  360. $expected = array(1);
  361. $this->assertEqual($expected, $result);
  362. }
  363. /**
  364. * testSaveWithCounterCacheScope method
  365. *
  366. * @return void
  367. */
  368. public function testSaveWithCounterCacheScope() {
  369. $this->loadFixtures('Syfile', 'Item', 'Image', 'ItemsPortfolio', 'Portfolio');
  370. $TestModel = new Syfile();
  371. $TestModel2 = new Item();
  372. $TestModel2->belongsTo['Syfile']['counterCache'] = true;
  373. $TestModel2->belongsTo['Syfile']['counterScope'] = array('published' => true);
  374. $result = $TestModel->findById(1);
  375. $this->assertIdentical($result['Syfile']['item_count'], null);
  376. $TestModel2->save(array(
  377. 'name' => 'Item 7',
  378. 'syfile_id' => 1,
  379. 'published'=> true
  380. ));
  381. $result = $TestModel->findById(1);
  382. $this->assertEquals($result['Syfile']['item_count'], 1);
  383. $TestModel2->id = 1;
  384. $TestModel2->saveField('published', true);
  385. $result = $TestModel->findById(1);
  386. $this->assertEquals($result['Syfile']['item_count'], 2);
  387. $TestModel2->save(array(
  388. 'id' => 1,
  389. 'syfile_id' => 1,
  390. 'published'=> false
  391. ));
  392. $result = $TestModel->findById(1);
  393. $this->assertEquals($result['Syfile']['item_count'], 1);
  394. }
  395. /**
  396. * Tests having multiple counter caches for an associated model
  397. *
  398. * @access public
  399. * @return void
  400. */
  401. public function testCounterCacheMultipleCaches() {
  402. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  403. $User = new CounterCacheUser();
  404. $Post = new CounterCachePost();
  405. $Post->unbindModel(array('belongsTo' => array('User')), false);
  406. $Post->bindModel(array(
  407. 'belongsTo' => array(
  408. 'User' => array(
  409. 'className' => 'CounterCacheUser',
  410. 'foreignKey' => 'user_id',
  411. 'counterCache' => array(
  412. true,
  413. 'posts_published' => array('Post.published' => true)
  414. )
  415. )
  416. )
  417. ), false);
  418. // Count Increase
  419. $user = $User->find('first', array(
  420. 'conditions' => array('id' => 66),
  421. 'recursive' => -1
  422. ));
  423. $data = array('Post' => array(
  424. 'id' => 22,
  425. 'title' => 'New Post',
  426. 'user_id' => 66,
  427. 'published' => true
  428. ));
  429. $Post->save($data);
  430. $result = $User->find('first', array(
  431. 'conditions' => array('id' => 66),
  432. 'recursive' => -1
  433. ));
  434. $this->assertEquals(3, $result[$User->alias]['post_count']);
  435. $this->assertEquals(2, $result[$User->alias]['posts_published']);
  436. // Count decrease
  437. $Post->delete(1);
  438. $result = $User->find('first', array(
  439. 'conditions' => array('id' => 66),
  440. 'recursive' => -1
  441. ));
  442. $this->assertEquals(2, $result[$User->alias]['post_count']);
  443. $this->assertEquals(2, $result[$User->alias]['posts_published']);
  444. // Count update
  445. $data = $Post->find('first', array(
  446. 'conditions' => array('id' => 1),
  447. 'recursive' => -1
  448. ));
  449. $data[$Post->alias]['user_id'] = 301;
  450. $Post->save($data);
  451. $result = $User->find('all',array('order' => 'User.id'));
  452. $this->assertEquals(2, $result[0]['User']['post_count']);
  453. $this->assertEquals(1, $result[1]['User']['posts_published']);
  454. }
  455. /**
  456. * test that beforeValidate returning false can abort saves.
  457. *
  458. * @return void
  459. */
  460. public function testBeforeValidateSaveAbortion() {
  461. $this->loadFixtures('Post');
  462. $Model = new CallbackPostTestModel();
  463. $Model->beforeValidateReturn = false;
  464. $data = array(
  465. 'title' => 'new article',
  466. 'body' => 'this is some text.'
  467. );
  468. $Model->create();
  469. $result = $Model->save($data);
  470. $this->assertFalse($result);
  471. }
  472. /**
  473. * test that beforeSave returning false can abort saves.
  474. *
  475. * @return void
  476. */
  477. public function testBeforeSaveSaveAbortion() {
  478. $this->loadFixtures('Post');
  479. $Model = new CallbackPostTestModel();
  480. $Model->beforeSaveReturn = false;
  481. $data = array(
  482. 'title' => 'new article',
  483. 'body' => 'this is some text.'
  484. );
  485. $Model->create();
  486. $result = $Model->save($data);
  487. $this->assertFalse($result);
  488. }
  489. /**
  490. * testSaveField method
  491. *
  492. * @return void
  493. */
  494. public function testSaveField() {
  495. $this->loadFixtures('Article');
  496. $TestModel = new Article();
  497. $TestModel->id = 1;
  498. $result = $TestModel->saveField('title', 'New First Article');
  499. $this->assertFalse(empty($result));
  500. $TestModel->recursive = -1;
  501. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  502. $expected = array('Article' => array(
  503. 'id' => '1',
  504. 'user_id' => '1',
  505. 'title' => 'New First Article',
  506. 'body' => 'First Article Body'
  507. ));
  508. $this->assertEqual($expected, $result);
  509. $TestModel->id = 1;
  510. $result = $TestModel->saveField('title', '');
  511. $this->assertFalse(empty($result));
  512. $TestModel->recursive = -1;
  513. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  514. $expected = array('Article' => array(
  515. 'id' => '1',
  516. 'user_id' => '1',
  517. 'title' => '',
  518. 'body' => 'First Article Body'
  519. ));
  520. $result['Article']['title'] = trim($result['Article']['title']);
  521. $this->assertEqual($expected, $result);
  522. $TestModel->id = 1;
  523. $TestModel->set('body', 'Messed up data');
  524. $result = $TestModel->saveField('title', 'First Article');
  525. $this->assertFalse(empty($result));
  526. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  527. $expected = array('Article' => array(
  528. 'id' => '1',
  529. 'user_id' => '1',
  530. 'title' => 'First Article',
  531. 'body' => 'First Article Body'
  532. ));
  533. $this->assertEqual($expected, $result);
  534. $TestModel->recursive = -1;
  535. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  536. $TestModel->id = 1;
  537. $result = $TestModel->saveField('title', '', true);
  538. $this->assertFalse($result);
  539. $this->loadFixtures('Node', 'Dependency');
  540. $Node = new Node();
  541. $Node->set('id', 1);
  542. $result = $Node->read();
  543. $this->assertEqual(Set::extract('/ParentNode/name', $result), array('Second'));
  544. $Node->saveField('state', 10);
  545. $result = $Node->read();
  546. $this->assertEqual(Set::extract('/ParentNode/name', $result), array('Second'));
  547. }
  548. /**
  549. * testSaveWithCreate method
  550. *
  551. * @return void
  552. */
  553. public function testSaveWithCreate() {
  554. $this->loadFixtures(
  555. 'User',
  556. 'Article',
  557. 'User',
  558. 'Comment',
  559. 'Tag',
  560. 'ArticlesTag',
  561. 'Attachment'
  562. );
  563. $TestModel = new User();
  564. $data = array('User' => array(
  565. 'user' => 'user',
  566. 'password' => ''
  567. ));
  568. $result = $TestModel->save($data);
  569. $this->assertFalse($result);
  570. $this->assertTrue(!empty($TestModel->validationErrors));
  571. $TestModel = new Article();
  572. $data = array('Article' => array(
  573. 'user_id' => '',
  574. 'title' => '',
  575. 'body' => ''
  576. ));
  577. $result = $TestModel->create($data) && $TestModel->save();
  578. $this->assertFalse($result);
  579. $this->assertTrue(!empty($TestModel->validationErrors));
  580. $data = array('Article' => array(
  581. 'id' => 1,
  582. 'user_id' => '1',
  583. 'title' => 'New First Article',
  584. 'body' => ''
  585. ));
  586. $result = $TestModel->create($data) && $TestModel->save();
  587. $this->assertFalse($result);
  588. $data = array('Article' => array(
  589. 'id' => 1,
  590. 'title' => 'New First Article'
  591. ));
  592. $result = $TestModel->create() && $TestModel->save($data, false);
  593. $this->assertFalse(empty($result));
  594. $TestModel->recursive = -1;
  595. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
  596. $expected = array('Article' => array(
  597. 'id' => '1',
  598. 'user_id' => '1',
  599. 'title' => 'New First Article',
  600. 'body' => 'First Article Body',
  601. 'published' => 'N'
  602. ));
  603. $this->assertEqual($expected, $result);
  604. $data = array('Article' => array(
  605. 'id' => 1,
  606. 'user_id' => '2',
  607. 'title' => 'First Article',
  608. 'body' => 'New First Article Body',
  609. 'published' => 'Y'
  610. ));
  611. $result = $TestModel->create() && $TestModel->save($data, true, array('id', 'title', 'published'));
  612. $this->assertFalse(empty($result));
  613. $TestModel->recursive = -1;
  614. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
  615. $expected = array('Article' => array(
  616. 'id' => '1',
  617. 'user_id' => '1',
  618. 'title' => 'First Article',
  619. 'body' => 'First Article Body',
  620. 'published' => 'Y'
  621. ));
  622. $this->assertEqual($expected, $result);
  623. $data = array(
  624. 'Article' => array(
  625. 'user_id' => '2',
  626. 'title' => 'New Article',
  627. 'body' => 'New Article Body',
  628. 'created' => '2007-03-18 14:55:23',
  629. 'updated' => '2007-03-18 14:57:31'
  630. ),
  631. 'Tag' => array('Tag' => array(1, 3))
  632. );
  633. $TestModel->create();
  634. $result = $TestModel->create() && $TestModel->save($data);
  635. $this->assertFalse(empty($result));
  636. $TestModel->recursive = 2;
  637. $result = $TestModel->read(null, 4);
  638. $expected = array(
  639. 'Article' => array(
  640. 'id' => '4',
  641. 'user_id' => '2',
  642. 'title' => 'New Article',
  643. 'body' => 'New Article Body',
  644. 'published' => 'N',
  645. 'created' => '2007-03-18 14:55:23',
  646. 'updated' => '2007-03-18 14:57:31'
  647. ),
  648. 'User' => array(
  649. 'id' => '2',
  650. 'user' => 'nate',
  651. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  652. 'created' => '2007-03-17 01:18:23',
  653. 'updated' => '2007-03-17 01:20:31'
  654. ),
  655. 'Comment' => array(),
  656. 'Tag' => array(
  657. array(
  658. 'id' => '1',
  659. 'tag' => 'tag1',
  660. 'created' => '2007-03-18 12:22:23',
  661. 'updated' => '2007-03-18 12:24:31'
  662. ),
  663. array(
  664. 'id' => '3',
  665. 'tag' => 'tag3',
  666. 'created' => '2007-03-18 12:26:23',
  667. 'updated' => '2007-03-18 12:28:31'
  668. )));
  669. $this->assertEqual($expected, $result);
  670. $data = array('Comment' => array(
  671. 'article_id' => '4',
  672. 'user_id' => '1',
  673. 'comment' => 'Comment New Article',
  674. 'published' => 'Y',
  675. 'created' => '2007-03-18 14:57:23',
  676. 'updated' => '2007-03-18 14:59:31'
  677. ));
  678. $result = $TestModel->Comment->create() && $TestModel->Comment->save($data);
  679. $this->assertFalse(empty($result));
  680. $data = array('Attachment' => array(
  681. 'comment_id' => '7',
  682. 'attachment' => 'newattachment.zip',
  683. 'created' => '2007-03-18 15:02:23',
  684. 'updated' => '2007-03-18 15:04:31'
  685. ));
  686. $result = $TestModel->Comment->Attachment->save($data);
  687. $this->assertFalse(empty($result));
  688. $TestModel->recursive = 2;
  689. $result = $TestModel->read(null, 4);
  690. $expected = array(
  691. 'Article' => array(
  692. 'id' => '4',
  693. 'user_id' => '2',
  694. 'title' => 'New Article',
  695. 'body' => 'New Article Body',
  696. 'published' => 'N',
  697. 'created' => '2007-03-18 14:55:23',
  698. 'updated' => '2007-03-18 14:57:31'
  699. ),
  700. 'User' => array(
  701. 'id' => '2',
  702. 'user' => 'nate',
  703. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  704. 'created' => '2007-03-17 01:18:23',
  705. 'updated' => '2007-03-17 01:20:31'
  706. ),
  707. 'Comment' => array(
  708. array(
  709. 'id' => '7',
  710. 'article_id' => '4',
  711. 'user_id' => '1',
  712. 'comment' => 'Comment New Article',
  713. 'published' => 'Y',
  714. 'created' => '2007-03-18 14:57:23',
  715. 'updated' => '2007-03-18 14:59:31',
  716. 'Article' => array(
  717. 'id' => '4',
  718. 'user_id' => '2',
  719. 'title' => 'New Article',
  720. 'body' => 'New Article Body',
  721. 'published' => 'N',
  722. 'created' => '2007-03-18 14:55:23',
  723. 'updated' => '2007-03-18 14:57:31'
  724. ),
  725. 'User' => array(
  726. 'id' => '1',
  727. 'user' => 'mariano',
  728. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  729. 'created' => '2007-03-17 01:16:23',
  730. 'updated' => '2007-03-17 01:18:31'
  731. ),
  732. 'Attachment' => array(
  733. 'id' => '2',
  734. 'comment_id' => '7',
  735. 'attachment' => 'newattachment.zip',
  736. 'created' => '2007-03-18 15:02:23',
  737. 'updated' => '2007-03-18 15:04:31'
  738. ))),
  739. 'Tag' => array(
  740. array(
  741. 'id' => '1',
  742. 'tag' => 'tag1',
  743. 'created' => '2007-03-18 12:22:23',
  744. 'updated' => '2007-03-18 12:24:31'
  745. ),
  746. array(
  747. 'id' => '3',
  748. 'tag' => 'tag3',
  749. 'created' => '2007-03-18 12:26:23',
  750. 'updated' => '2007-03-18 12:28:31'
  751. )));
  752. $this->assertEqual($expected, $result);
  753. }
  754. /**
  755. * test that a null Id doesn't cause errors
  756. *
  757. * @return void
  758. */
  759. public function testSaveWithNullId() {
  760. $this->loadFixtures('User');
  761. $User = new User();
  762. $User->read(null, 1);
  763. $User->data['User']['id'] = null;
  764. $result = $User->save(array('password' => 'test'));
  765. $this->assertFalse(empty($result));
  766. $this->assertTrue($User->id > 0);
  767. $User->read(null, 2);
  768. $User->data['User']['id'] = null;
  769. $result = $User->save(array('password' => 'test'));
  770. $this->assertFalse(empty($result));
  771. $this->assertTrue($User->id > 0);
  772. $User->data['User'] = array('password' => 'something');
  773. $result = $User->save();
  774. $this->assertFalse(empty($result));
  775. $result = $User->read();
  776. $this->assertEqual($User->data['User']['password'], 'something');
  777. }
  778. /**
  779. * testSaveWithSet method
  780. *
  781. * @return void
  782. */
  783. public function testSaveWithSet() {
  784. $this->loadFixtures('Article');
  785. $TestModel = new Article();
  786. // Create record we will be updating later
  787. $data = array('Article' => array(
  788. 'user_id' => '1',
  789. 'title' => 'Fourth Article',
  790. 'body' => 'Fourth Article Body',
  791. 'published' => 'Y'
  792. ));
  793. $result = $TestModel->create() && $TestModel->save($data);
  794. $this->assertFalse(empty($result));
  795. // Check record we created
  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',
  802. 'body' => 'Fourth Article Body',
  803. 'published' => 'Y'
  804. ));
  805. $this->assertEqual($expected, $result);
  806. // Create new record just to overlap Model->id on previously created record
  807. $data = array('Article' => array(
  808. 'user_id' => '4',
  809. 'title' => 'Fifth Article',
  810. 'body' => 'Fifth Article Body',
  811. 'published' => 'Y'
  812. ));
  813. $result = $TestModel->create() && $TestModel->save($data);
  814. $this->assertFalse(empty($result));
  815. $TestModel->recursive = -1;
  816. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  817. $expected = array('Article' => array(
  818. 'id' => '5',
  819. 'user_id' => '4',
  820. 'title' => 'Fifth Article',
  821. 'body' => 'Fifth Article Body',
  822. 'published' => 'Y'
  823. ));
  824. $this->assertEqual($expected, $result);
  825. // Go back and edit the first article we created, starting by checking it's still there
  826. $TestModel->recursive = -1;
  827. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  828. $expected = array('Article' => array(
  829. 'id' => '4',
  830. 'user_id' => '1',
  831. 'title' => 'Fourth Article',
  832. 'body' => 'Fourth Article Body',
  833. 'published' => 'Y'
  834. ));
  835. $this->assertEqual($expected, $result);
  836. // And now do the update with set()
  837. $data = array('Article' => array(
  838. 'id' => '4',
  839. 'title' => 'Fourth Article - New Title',
  840. 'published' => 'N'
  841. ));
  842. $result = $TestModel->set($data) && $TestModel->save();
  843. $this->assertFalse(empty($result));
  844. $TestModel->recursive = -1;
  845. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  846. $expected = array('Article' => array(
  847. 'id' => '4',
  848. 'user_id' => '1',
  849. 'title' => 'Fourth Article - New Title',
  850. 'body' => 'Fourth Article Body',
  851. 'published' => 'N'
  852. ));
  853. $this->assertEqual($expected, $result);
  854. $TestModel->recursive = -1;
  855. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  856. $expected = array('Article' => array(
  857. 'id' => '5',
  858. 'user_id' => '4',
  859. 'title' => 'Fifth Article',
  860. 'body' => 'Fifth Article Body',
  861. 'published' => 'Y'
  862. ));
  863. $this->assertEqual($expected, $result);
  864. $data = array('Article' => array('id' => '5', 'title' => 'Fifth Article - New Title 5'));
  865. $result = ($TestModel->set($data) && $TestModel->save());
  866. $this->assertFalse(empty($result));
  867. $TestModel->recursive = -1;
  868. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  869. $expected = array('Article' => array(
  870. 'id' => '5',
  871. 'user_id' => '4',
  872. 'title' => 'Fifth Article - New Title 5',
  873. 'body' => 'Fifth Article Body',
  874. 'published' => 'Y'
  875. ));
  876. $this->assertEqual($expected, $result);
  877. $TestModel->recursive = -1;
  878. $result = $TestModel->find('all', array('fields' => array('id', 'title')));
  879. $expected = array(
  880. array('Article' => array('id' => 1, 'title' => 'First Article' )),
  881. array('Article' => array('id' => 2, 'title' => 'Second Article' )),
  882. array('Article' => array('id' => 3, 'title' => 'Third Article' )),
  883. array('Article' => array('id' => 4, 'title' => 'Fourth Article - New Title' )),
  884. array('Article' => array('id' => 5, 'title' => 'Fifth Article - New Title 5' ))
  885. );
  886. $this->assertEqual($expected, $result);
  887. }
  888. /**
  889. * testSaveWithNonExistentFields method
  890. *
  891. * @return void
  892. */
  893. public function testSaveWithNonExistentFields() {
  894. $this->loadFixtures('Article');
  895. $TestModel = new Article();
  896. $TestModel->recursive = -1;
  897. $data = array(
  898. 'non_existent' => 'This field does not exist',
  899. 'user_id' => '1',
  900. 'title' => 'Fourth Article - New Title',
  901. 'body' => 'Fourth Article Body',
  902. 'published' => 'N'
  903. );
  904. $result = $TestModel->create() && $TestModel->save($data);
  905. $this->assertFalse(empty($result));
  906. $expected = array('Article' => array(
  907. 'id' => '4',
  908. 'user_id' => '1',
  909. 'title' => 'Fourth Article - New Title',
  910. 'body' => 'Fourth Article Body',
  911. 'published' => 'N'
  912. ));
  913. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  914. $this->assertEqual($expected, $result);
  915. $data = array(
  916. 'user_id' => '1',
  917. 'non_existent' => 'This field does not exist',
  918. 'title' => 'Fiveth Article - New Title',
  919. 'body' => 'Fiveth Article Body',
  920. 'published' => 'N'
  921. );
  922. $result = $TestModel->create() && $TestModel->save($data);
  923. $this->assertFalse(empty($result));
  924. $expected = array('Article' => array(
  925. 'id' => '5',
  926. 'user_id' => '1',
  927. 'title' => 'Fiveth Article - New Title',
  928. 'body' => 'Fiveth Article Body',
  929. 'published' => 'N'
  930. ));
  931. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  932. $this->assertEqual($expected, $result);
  933. }
  934. /**
  935. * testSaveFromXml method
  936. *
  937. * @return void
  938. */
  939. public function testSaveFromXml() {
  940. $this->markTestSkipped('This feature needs to be fixed or dropped');
  941. $this->loadFixtures('Article');
  942. App::uses('Xml', 'Utility');
  943. $Article = new Article();
  944. $result = $Article->save(Xml::build('<article title="test xml" user_id="5" />'));
  945. $this->assertFalse(empty($result));
  946. $results = $Article->find('first', array('conditions' => array('Article.title' => 'test xml')));
  947. $this->assertFalse(empty($results));
  948. $result = $Article->save(Xml::build('<article><title>testing</title><user_id>6</user_id></article>'));
  949. $this->assertFalse(empty($result));
  950. $results = $Article->find('first', array('conditions' => array('Article.title' => 'testing')));
  951. $this->assertFalse(empty($results));
  952. $result = $Article->save(Xml::build('<article><title>testing with DOMDocument</title><user_id>7</user_id></article>', array('return' => 'domdocument')));
  953. $this->assertFalse(empty($result));
  954. $results = $Article->find('first', array('conditions' => array('Article.title' => 'testing with DOMDocument')));
  955. $this->assertFalse(empty($results));
  956. }
  957. /**
  958. * testSaveHabtm method
  959. *
  960. * @return void
  961. */
  962. public function testSaveHabtm() {
  963. $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
  964. $TestModel = new Article();
  965. $result = $TestModel->findById(2);
  966. $expected = array(
  967. 'Article' => array(
  968. 'id' => '2',
  969. 'user_id' => '3',
  970. 'title' => 'Second Article',
  971. 'body' => 'Second Article Body',
  972. 'published' => 'Y',
  973. 'created' => '2007-03-18 10:41:23',
  974. 'updated' => '2007-03-18 10:43:31'
  975. ),
  976. 'User' => array(
  977. 'id' => '3',
  978. 'user' => 'larry',
  979. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  980. 'created' => '2007-03-17 01:20:23',
  981. 'updated' => '2007-03-17 01:22:31'
  982. ),
  983. 'Comment' => array(
  984. array(
  985. 'id' => '5',
  986. 'article_id' => '2',
  987. 'user_id' => '1',
  988. 'comment' => 'First Comment for Second Article',
  989. 'published' => 'Y',
  990. 'created' => '2007-03-18 10:53:23',
  991. 'updated' => '2007-03-18 10:55:31'
  992. ),
  993. array(
  994. 'id' => '6',
  995. 'article_id' => '2',
  996. 'user_id' => '2',
  997. 'comment' => 'Second Comment for Second Article',
  998. 'published' => 'Y',
  999. 'created' => '2007-03-18 10:55:23',
  1000. 'updated' => '2007-03-18 10:57:31'
  1001. )),
  1002. 'Tag' => array(
  1003. array(
  1004. 'id' => '1',
  1005. 'tag' => 'tag1',
  1006. 'created' => '2007-03-18 12:22:23',
  1007. 'updated' => '2007-03-18 12:24:31'
  1008. ),
  1009. array(
  1010. 'id' => '3',
  1011. 'tag' => 'tag3',
  1012. 'created' => '2007-03-18 12:26:23',
  1013. 'updated' => '2007-03-18 12:28:31'
  1014. )
  1015. )
  1016. );
  1017. $this->assertEqual($expected, $result);
  1018. $data = array(
  1019. 'Article' => array(
  1020. 'id' => '2',
  1021. 'title' => 'New Second Article'
  1022. ),
  1023. 'Tag' => array('Tag' => array(1, 2))
  1024. );
  1025. $result = $TestModel->set($data);
  1026. $this->assertFalse(empty($result));
  1027. $result = $TestModel->save();
  1028. $this->assertFalse(empty($result));
  1029. $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
  1030. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1031. $expected = array(
  1032. 'Article' => array(
  1033. 'id' => '2',
  1034. 'user_id' => '3',
  1035. 'title' => 'New Second Article',
  1036. 'body' => 'Second Article Body'
  1037. ),
  1038. 'Tag' => array(
  1039. array(
  1040. 'id' => '1',
  1041. 'tag' => 'tag1',
  1042. 'created' => '2007-03-18 12:22:23',
  1043. 'updated' => '2007-03-18 12:24:31'
  1044. ),
  1045. array(
  1046. 'id' => '2',
  1047. 'tag' => 'tag2',
  1048. 'created' => '2007-03-18 12:24:23',
  1049. 'updated' => '2007-03-18 12:26:31'
  1050. )));
  1051. $this->assertEqual($expected, $result);
  1052. $data = array('Article' => array('id' => '2'), 'Tag' => array('Tag' => array(2, 3)));
  1053. $result = $TestModel->set($data);
  1054. $this->assertFalse(empty($result));
  1055. $result = $TestModel->save();
  1056. $this->assertFalse(empty($result));
  1057. $TestModel->unbindModel(array(
  1058. 'belongsTo' => array('User'),
  1059. 'hasMany' => array('Comment')
  1060. ));
  1061. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1062. $expected = array(
  1063. 'Article' => array(
  1064. 'id' => '2',
  1065. 'user_id' => '3',
  1066. 'title' => 'New Second Article',
  1067. 'body' => 'Second Article Body'
  1068. ),
  1069. 'Tag' => array(
  1070. array(
  1071. 'id' => '2',
  1072. 'tag' => 'tag2',
  1073. 'created' => '2007-03-18 12:24:23',
  1074. 'updated' => '2007-03-18 12:26:31'
  1075. ),
  1076. array(
  1077. 'id' => '3',
  1078. 'tag' => 'tag3',
  1079. 'created' => '2007-03-18 12:26:23',
  1080. 'updated' => '2007-03-18 12:28:31'
  1081. )));
  1082. $this->assertEqual($expected, $result);
  1083. $data = array('Tag' => array('Tag' => array(1, 2, 3)));
  1084. $result = $TestModel->set($data);
  1085. $this->assertFalse(empty($result));
  1086. $result = $TestModel->save();
  1087. $this->assertFalse(empty($result));
  1088. $TestModel->unbindModel(array(
  1089. 'belongsTo' => array('User'),
  1090. 'hasMany' => array('Comment')
  1091. ));
  1092. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1093. $expected = array(
  1094. 'Article' => array(
  1095. 'id' => '2',
  1096. 'user_id' => '3',
  1097. 'title' => 'New Second Article',
  1098. 'body' => 'Second Article Body'
  1099. ),
  1100. 'Tag' => array(
  1101. array(
  1102. 'id' => '1',
  1103. 'tag' => 'tag1',
  1104. 'created' => '2007-03-18 12:22:23',
  1105. 'updated' => '2007-03-18 12:24:31'
  1106. ),
  1107. array(
  1108. 'id' => '2',
  1109. 'tag' => 'tag2',
  1110. 'created' => '2007-03-18 12:24:23',
  1111. 'updated' => '2007-03-18 12:26:31'
  1112. ),
  1113. array(
  1114. 'id' => '3',
  1115. 'tag' => 'tag3',
  1116. 'created' => '2007-03-18 12:26:23',
  1117. 'updated' => '2007-03-18 12:28:31'
  1118. )));
  1119. $this->assertEqual($expected, $result);
  1120. $data = array('Tag' => array('Tag' => array()));
  1121. $result = $TestModel->set($data);
  1122. $this->assertFalse(empty($result));
  1123. $result = $TestModel->save();
  1124. $this->assertFalse(empty($result));
  1125. $data = array('Tag' => array('Tag' => ''));
  1126. $result = $TestModel->set($data);
  1127. $this->assertFalse(empty($result));
  1128. $result = $TestModel->save();
  1129. $this->assertFalse(empty($result));
  1130. $TestModel->unbindModel(array(
  1131. 'belongsTo' => array('User'),
  1132. 'hasMany' => array('Comment')
  1133. ));
  1134. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1135. $expected = array(
  1136. 'Article' => array(
  1137. 'id' => '2',
  1138. 'user_id' => '3',
  1139. 'title' => 'New Second Article',
  1140. 'body' => 'Second Article Body'
  1141. ),
  1142. 'Tag' => array()
  1143. );
  1144. $this->assertEqual($expected, $result);
  1145. $data = array('Tag' => array('Tag' => array(2, 3)));
  1146. $result = $TestModel->set($data);
  1147. $this->assertFalse(empty($result));
  1148. $result = $TestModel->save();
  1149. $this->assertFalse(empty($result));
  1150. $TestModel->unbindModel(array(
  1151. 'belongsTo' => array('User'),
  1152. 'hasMany' => array('Comment')
  1153. ));
  1154. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1155. $expected = array(
  1156. 'Article' => array(
  1157. 'id' => '2',
  1158. 'user_id' => '3',
  1159. 'title' => 'New Second Article',
  1160. 'body' => 'Second Article Body'
  1161. ),
  1162. 'Tag' => array(
  1163. array(
  1164. 'id' => '2',
  1165. 'tag' => 'tag2',
  1166. 'created' => '2007-03-18 12:24:23',
  1167. 'updated' => '2007-03-18 12:26:31'
  1168. ),
  1169. array(
  1170. 'id' => '3',
  1171. 'tag' => 'tag3',
  1172. 'created' => '2007-03-18 12:26:23',
  1173. 'updated' => '2007-03-18 12:28:31'
  1174. )));
  1175. $this->assertEqual($expected, $result);
  1176. $data = array(
  1177. 'Tag' => array(
  1178. 'Tag' => array(1, 2)
  1179. ),
  1180. 'Article' => array(
  1181. 'id' => '2',
  1182. 'title' => 'New Second Article'
  1183. ));
  1184. $result = $TestModel->set($data);
  1185. $this->assertFalse(empty($result));
  1186. $result = $TestModel->save();
  1187. $this->assertFalse(empty($result));
  1188. $TestModel->unbindModel(array(
  1189. 'belongsTo' => array('User'),
  1190. 'hasMany' => array('Comment')
  1191. ));
  1192. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1193. $expected = array(
  1194. 'Article' => array(
  1195. 'id' => '2',
  1196. 'user_id' => '3',
  1197. 'title' => 'New Second Article',
  1198. 'body' => 'Second Article Body'
  1199. ),
  1200. 'Tag' => array(
  1201. array(
  1202. 'id' => '1',
  1203. 'tag' => 'tag1',
  1204. 'created' => '2007-03-18 12:22:23',
  1205. 'updated' => '2007-03-18 12:24:31'
  1206. ),
  1207. array(
  1208. 'id' => '2',
  1209. 'tag' => 'tag2',
  1210. 'created' => '2007-03-18 12:24:23',
  1211. 'updated' => '2007-03-18 12:26:31'
  1212. )));
  1213. $this->assertEqual($expected, $result);
  1214. $data = array(
  1215. 'Tag' => array(
  1216. 'Tag' => array(1, 2)
  1217. ),
  1218. 'Article' => array(
  1219. 'id' => '2',
  1220. 'title' => 'New Second Article Title'
  1221. ));
  1222. $result = $TestModel->set($data);
  1223. $this->assertFalse(empty($result));
  1224. $result = $TestModel->save();
  1225. $this->assertFalse(empty($result));
  1226. $TestModel->unbindModel(array(
  1227. 'belongsTo' => array('User'),
  1228. 'hasMany' => array('Comment')
  1229. ));
  1230. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1231. $expected = array(
  1232. 'Article' => array(
  1233. 'id' => '2',
  1234. 'user_id' => '3',
  1235. 'title' => 'New Second Article Title',
  1236. 'body' => 'Second Article Body'
  1237. ),
  1238. 'Tag' => array(
  1239. array(
  1240. 'id' => '1',
  1241. 'tag' => 'tag1',
  1242. 'created' => '2007-03-18 12:22:23',
  1243. 'updated' => '2007-03-18 12:24:31'
  1244. ),
  1245. array(
  1246. 'id' => '2',
  1247. 'tag' => 'tag2',
  1248. 'created' => '2007-03-18 12:24:23',
  1249. 'updated' => '2007-03-18 12:26:31'
  1250. )
  1251. )
  1252. );
  1253. $this->assertEqual($expected, $result);
  1254. $data = array(
  1255. 'Tag' => array(
  1256. 'Tag' => array(2, 3)
  1257. ),
  1258. 'Article' => array(
  1259. 'id' => '2',
  1260. 'title' => 'Changed Second Article'
  1261. ));
  1262. $result = $TestModel->set($data);
  1263. $this->assertFalse(empty($result));
  1264. $result = $TestModel->save();
  1265. $this->assertFalse(empty($result));
  1266. $TestModel->unbindModel(array(
  1267. 'belongsTo' => array('User'),
  1268. 'hasMany' => array('Comment')
  1269. ));
  1270. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1271. $expected = array(
  1272. 'Article' => array(
  1273. 'id' => '2',
  1274. 'user_id' => '3',
  1275. 'title' => 'Changed Second Article',
  1276. 'body' => 'Second Article Body'
  1277. ),
  1278. 'Tag' => array(
  1279. array(
  1280. 'id' => '2',
  1281. 'tag' => 'tag2',
  1282. 'created' => '2007-03-18 12:24:23',
  1283. 'updated' => '2007-03-18 12:26:31'
  1284. ),
  1285. array(
  1286. 'id' => '3',
  1287. 'tag' => 'tag3',
  1288. 'created' => '2007-03-18 12:26:23',
  1289. 'updated' => '2007-03-18 12:28:31'
  1290. )
  1291. )
  1292. );
  1293. $this->assertEqual($expected, $result);
  1294. $data = array(
  1295. 'Tag' => array(
  1296. 'Tag' => array(1, 3)
  1297. ),
  1298. 'Article' => array('id' => '2'),
  1299. );
  1300. $result = $TestModel->set($data);
  1301. $this->assertFalse(empty($result));
  1302. $result = $TestModel->save();
  1303. $this->assertFalse(empty($result));
  1304. $TestModel->unbindModel(array(
  1305. 'belongsTo' => array('User'),
  1306. 'hasMany' => array('Comment')
  1307. ));
  1308. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1309. $expected = array(
  1310. 'Article' => array(
  1311. 'id' => '2',
  1312. 'user_id' => '3',
  1313. 'title' => 'Changed Second Article',
  1314. 'body' => 'Second Article Body'
  1315. ),
  1316. 'Tag' => array(
  1317. array(
  1318. 'id' => '1',
  1319. 'tag' => 'tag1',
  1320. 'created' => '2007-03-18 12:22:23',
  1321. 'updated' => '2007-03-18 12:24:31'
  1322. ),
  1323. array(
  1324. 'id' => '3',
  1325. 'tag' => 'tag3',
  1326. 'created' => '2007-03-18 12:26:23',
  1327. 'updated' => '2007-03-18 12:28:31'
  1328. )));
  1329. $this->assertEqual($expected, $result);
  1330. $data = array(
  1331. 'Article' => array(
  1332. 'id' => 10,
  1333. 'user_id' => '2',
  1334. 'title' => 'New Article With Tags and fieldList',
  1335. 'body' => 'New Article Body with Tags and fieldList',
  1336. 'created' => '2007-03-18 14:55:23',
  1337. 'updated' => '2007-03-18 14:57:31'
  1338. ),
  1339. 'Tag' => array(
  1340. 'Tag' => array(1, 2, 3)
  1341. ));
  1342. $result = $TestModel->create()
  1343. && $TestModel->save($data, true, array('user_id', 'title', 'published'));
  1344. $this->assertFalse(empty($result));
  1345. $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
  1346. $result = $TestModel->read();
  1347. $expected = array(
  1348. 'Article' => array(
  1349. 'id' => 4,
  1350. 'user_id' => 2,
  1351. 'title' => 'New Article With Tags and fieldList',
  1352. 'body' => '',
  1353. 'published' => 'N',
  1354. 'created' => '',
  1355. 'updated' => ''
  1356. ),
  1357. 'Tag' => array(
  1358. 0 => array(
  1359. 'id' => 1,
  1360. 'tag' => 'tag1',
  1361. 'created' => '2007-03-18 12:22:23',
  1362. 'updated' => '2007-03-18 12:24:31'
  1363. ),
  1364. 1 => array(
  1365. 'id' => 2,
  1366. 'tag' => 'tag2',
  1367. 'created' => '2007-03-18 12:24:23',
  1368. 'updated' => '2007-03-18 12:26:31'
  1369. ),
  1370. 2 => array(
  1371. 'id' => 3,
  1372. 'tag' => 'tag3',
  1373. 'created' => '2007-03-18 12:26:23',
  1374. 'updated' => '2007-03-18 12:28:31'
  1375. )));
  1376. $this->assertEqual($expected, $result);
  1377. $this->loadFixtures('JoinA', 'JoinC', 'JoinAC', 'JoinB', 'JoinAB');
  1378. $TestModel = new JoinA();
  1379. $TestModel->hasBelongsToMany = array('JoinC' => array('unique' => true));
  1380. $data = array(
  1381. 'JoinA' => array(
  1382. 'id' => 1,
  1383. 'name' => 'Join A 1',
  1384. 'body' => 'Join A 1 Body',
  1385. ),
  1386. 'JoinC' => array(
  1387. 'JoinC' => array(
  1388. array('join_c_id' => 2, 'other' => 'new record'),
  1389. array('join_c_id' => 3, 'other' => 'new record')
  1390. )
  1391. )
  1392. );
  1393. $TestModel->save($data);
  1394. $result = $TestModel->read(null, 1);
  1395. $expected = array(4, 5);
  1396. $this->assertEqual(Set::extract('/JoinC/JoinAsJoinC/id', $result), $expected);
  1397. $expected = array('new record', 'new record');
  1398. $this->assertEqual(Set::extract('/JoinC/JoinAsJoinC/other', $result), $expected);
  1399. }
  1400. /**
  1401. * testSaveHabtmCustomKeys method
  1402. *
  1403. * @return void
  1404. */
  1405. public function testSaveHabtmCustomKeys() {
  1406. $this->loadFixtures('Story', 'StoriesTag', 'Tag');
  1407. $Story = new Story();
  1408. $data = array(
  1409. 'Story' => array('story' => '1'),
  1410. 'Tag' => array(
  1411. 'Tag' => array(2, 3)
  1412. ));
  1413. $result = $Story->set($data);
  1414. $this->assertFalse(empty($result));
  1415. $result = $Story->save();
  1416. $this->assertFalse(empty($result));
  1417. $result = $Story->find('all', array('order' => array('Story.story')));
  1418. $expected = array(
  1419. array(
  1420. 'Story' => array(
  1421. 'story' => 1,
  1422. 'title' => 'First Story'
  1423. ),
  1424. 'Tag' => array(
  1425. array(
  1426. 'id' => 2,
  1427. 'tag' => 'tag2',
  1428. 'created' => '2007-03-18 12:24:23',
  1429. 'updated' => '2007-03-18 12:26:31'
  1430. ),
  1431. array(
  1432. 'id' => 3,
  1433. 'tag' => 'tag3',
  1434. 'created' => '2007-03-18 12:26:23',
  1435. 'updated' => '2007-03-18 12:28:31'
  1436. ))),
  1437. array(
  1438. 'Story' => array(
  1439. 'story' => 2,
  1440. 'title' => 'Second Story'
  1441. ),
  1442. 'Tag' => array()
  1443. ));
  1444. $this->assertEqual($expected, $result);
  1445. }
  1446. /**
  1447. * test that saving habtm records respects conditions set in the 'conditions' key
  1448. * for the association.
  1449. *
  1450. * @return void
  1451. */
  1452. public function testHabtmSaveWithConditionsInAssociation() {
  1453. $this->loadFixtures('JoinThing', 'Something', 'SomethingElse');
  1454. $Something = new Something();
  1455. $Something->unbindModel(array('hasAndBelongsToMany' => array('SomethingElse')), false);
  1456. $Something->bindModel(array(
  1457. 'hasAndBelongsToMany' => array(
  1458. 'DoomedSomethingElse' => array(
  1459. 'className' => 'SomethingElse',
  1460. 'joinTable' => 'join_things',
  1461. 'conditions' => array('JoinThing.doomed' => true),
  1462. 'unique' => true
  1463. ),
  1464. 'NotDoomedSomethingElse' => array(
  1465. 'className' => 'SomethingElse',
  1466. 'joinTable' => 'join_things',
  1467. 'conditions' => array('JoinThing.doomed' => 0),
  1468. 'unique' => true
  1469. )
  1470. )
  1471. ), false);
  1472. $result = $Something->read(null, 1);
  1473. $this->assertTrue(empty($result['NotDoomedSomethingElse']));
  1474. $this->assertEqual(count($result['DoomedSomethingElse']), 1);
  1475. $data = array(
  1476. 'Something' => array('id' => 1),
  1477. 'NotDoomedSomethingElse' => array(
  1478. 'NotDoomedSomethingElse' => array(
  1479. array('something_else_id' => 2, 'doomed' => 0),
  1480. array('something_else_id' => 3, 'doomed' => 0)
  1481. )
  1482. )
  1483. );
  1484. $Something->create($data);
  1485. $result = $Something->save();
  1486. $this->assertFalse(empty($result));
  1487. $result = $Something->read(null, 1);
  1488. $this->assertEqual(count($result['NotDoomedSomethingElse']), 2);
  1489. $this->assertEqual(count($result['DoomedSomethingElse']), 1);
  1490. }
  1491. /**
  1492. * testHabtmSaveKeyResolution method
  1493. *
  1494. * @return void
  1495. */
  1496. public function testHabtmSaveKeyResolution() {
  1497. $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies');
  1498. $ThePaper = new ThePaper();
  1499. $ThePaper->id = 1;
  1500. $ThePaper->save(array('Monkey' => array(2, 3)));
  1501. $result = $ThePaper->findById(1);
  1502. $expected = array(
  1503. array(
  1504. 'id' => '2',
  1505. 'device_type_id' => '1',
  1506. 'name' => 'Device 2',
  1507. 'typ' => '1'
  1508. ),
  1509. array(
  1510. 'id' => '3',
  1511. 'device_type_id' => '1',
  1512. 'name' => 'Device 3',
  1513. 'typ' => '2'
  1514. ));
  1515. $this->assertEqual($result['Monkey'], $expected);
  1516. $ThePaper->id = 2;
  1517. $ThePaper->save(array('Monkey' => array(1, 2, 3)));
  1518. $result = $ThePaper->findById(2);
  1519. $expected = array(
  1520. array(
  1521. 'id' => '1',
  1522. 'device_type_id' => '1',
  1523. 'name' => 'Device 1',
  1524. 'typ' => '1'
  1525. ),
  1526. array(
  1527. 'id' => '2',
  1528. 'device_type_id' => '1',
  1529. 'name' => 'Device 2',
  1530. 'typ' => '1'
  1531. ),
  1532. array(
  1533. 'id' => '3',
  1534. 'device_type_id' => '1',
  1535. 'name' => 'Device 3',
  1536. 'typ' => '2'
  1537. ));
  1538. $this->assertEqual($result['Monkey'], $expected);
  1539. $ThePaper->id = 2;
  1540. $ThePaper->save(array('Monkey' => array(1, 3)));
  1541. $result = $ThePaper->findById(2);
  1542. $expected = array(
  1543. array(
  1544. 'id' => '1',
  1545. 'device_type_id' => '1',
  1546. 'name' => 'Device 1',
  1547. 'typ' => '1'
  1548. ),
  1549. array(
  1550. 'id' => '3',
  1551. 'device_type_id' => '1',
  1552. 'name' => 'Device 3',
  1553. 'typ' => '2'
  1554. ));
  1555. $this->assertEqual($result['Monkey'], $expected);
  1556. $result = $ThePaper->findById(1);
  1557. $expected = array(
  1558. array(
  1559. 'id' => '2',
  1560. 'device_type_id' => '1',
  1561. 'name' => 'Device 2',
  1562. 'typ' => '1'
  1563. ),
  1564. array(
  1565. 'id' => '3',
  1566. 'device_type_id' => '1',
  1567. 'name' => 'Device 3',
  1568. 'typ' => '2'
  1569. ));
  1570. $this->assertEqual($result['Monkey'], $expected);
  1571. }
  1572. /**
  1573. * testCreationOfEmptyRecord method
  1574. *
  1575. * @return void
  1576. */
  1577. public function testCreationOfEmptyRecord() {
  1578. $this->loadFixtures('Author');
  1579. $TestModel = new Author();
  1580. $this->assertEqual($TestModel->find('count'), 4);
  1581. $TestModel->deleteAll(true, false, false);
  1582. $this->assertEqual($TestModel->find('count'), 0);
  1583. $result = $TestModel->save();
  1584. $this->assertTrue(isset($result['Author']['created']));
  1585. $this->assertTrue(isset($result['Author']['updated']));
  1586. $this->assertEqual($TestModel->find('count'), 1);
  1587. }
  1588. /**
  1589. * testCreateWithPKFiltering method
  1590. *
  1591. * @return void
  1592. */
  1593. public function testCreateWithPKFiltering() {
  1594. $TestModel = new Article();
  1595. $data = array(
  1596. 'id' => 5,
  1597. 'user_id' => 2,
  1598. 'title' => 'My article',
  1599. 'body' => 'Some text'
  1600. );
  1601. $result = $TestModel->create($data);
  1602. $expected = array(
  1603. 'Article' => array(
  1604. 'published' => 'N',
  1605. 'id' => 5,
  1606. 'user_id' => 2,
  1607. 'title' => 'My article',
  1608. 'body' => 'Some text'
  1609. ));
  1610. $this->assertEqual($expected, $result);
  1611. $this->assertEqual($TestModel->id, 5);
  1612. $result = $TestModel->create($data, true);
  1613. $expected = array(
  1614. 'Article' => array(
  1615. 'published' => 'N',
  1616. 'id' => false,
  1617. 'user_id' => 2,
  1618. 'title' => 'My article',
  1619. 'body' => 'Some text'
  1620. ));
  1621. $this->assertEqual($expected, $result);
  1622. $this->assertFalse($TestModel->id);
  1623. $result = $TestModel->create(array('Article' => $data), true);
  1624. $expected = array(
  1625. 'Article' => array(
  1626. 'published' => 'N',
  1627. 'id' => false,
  1628. 'user_id' => 2,
  1629. 'title' => 'My article',
  1630. 'body' => 'Some text'
  1631. ));
  1632. $this->assertEqual($expected, $result);
  1633. $this->assertFalse($TestModel->id);
  1634. $data = array(
  1635. 'id' => 6,
  1636. 'user_id' => 2,
  1637. 'title' => 'My article',
  1638. 'body' => 'Some text',
  1639. 'created' => '1970-01-01 00:00:00',
  1640. 'updated' => '1970-01-01 12:00:00',
  1641. 'modified' => '1970-01-01 12:00:00'
  1642. );
  1643. $result = $TestModel->create($data);
  1644. $expected = array(
  1645. 'Article' => array(
  1646. 'published' => 'N',
  1647. 'id' => 6,
  1648. 'user_id' => 2,
  1649. 'title' => 'My article',
  1650. 'body' => 'Some text',
  1651. 'created' => '1970-01-01 00:00:00',
  1652. 'updated' => '1970-01-01 12:00:00',
  1653. 'modified' => '1970-01-01 12:00:00'
  1654. ));
  1655. $this->assertEqual($expected, $result);
  1656. $this->assertEqual($TestModel->id, 6);
  1657. $result = $TestModel->create(array(
  1658. 'Article' => array_diff_key($data, array(
  1659. 'created' => true,
  1660. 'updated' => true,
  1661. 'modified' => true
  1662. ))), true);
  1663. $expected = array(
  1664. 'Article' => array(
  1665. 'published' => 'N',
  1666. 'id' => false,
  1667. 'user_id' => 2,
  1668. 'title' => 'My article',
  1669. 'body' => 'Some text'
  1670. ));
  1671. $this->assertEqual($expected, $result);
  1672. $this->assertFalse($TestModel->id);
  1673. }
  1674. /**
  1675. * testCreationWithMultipleData method
  1676. *
  1677. * @return void
  1678. */
  1679. public function testCreationWithMultipleData() {
  1680. $this->loadFixtures('Article', 'Comment');
  1681. $Article = new Article();
  1682. $Comment = new Comment();
  1683. $articles = $Article->find('all', array(
  1684. 'fields' => array('id','title'),
  1685. 'recursive' => -1
  1686. ));
  1687. $comments = $Comment->find('all', array(
  1688. 'fields' => array('id','article_id','user_id','comment','published'), 'recursive' => -1));
  1689. $this->assertEqual($articles, array(
  1690. array('Article' => array(
  1691. 'id' => 1,
  1692. 'title' => 'First Article'
  1693. )),
  1694. array('Article' => array(
  1695. 'id' => 2,
  1696. 'title' => 'Second Article'
  1697. )),
  1698. array('Article' => array(
  1699. 'id' => 3,
  1700. 'title' => 'Third Article'
  1701. ))));
  1702. $this->assertEqual($comments, array(
  1703. array('Comment' => array(
  1704. 'id' => 1,
  1705. 'article_id' => 1,
  1706. 'user_id' => 2,
  1707. 'comment' => 'First Comment for First Article',
  1708. 'published' => 'Y'
  1709. )),
  1710. array('Comment' => array(
  1711. 'id' => 2,
  1712. 'article_id' => 1,
  1713. 'user_id' => 4,
  1714. 'comment' => 'Second Comment for First Article',
  1715. 'published' => 'Y'
  1716. )),
  1717. array('Comment' => array(
  1718. 'id' => 3,
  1719. 'article_id' => 1,
  1720. 'user_id' => 1,
  1721. 'comment' => 'Third Comment for First Article',
  1722. 'published' => 'Y'
  1723. )),
  1724. array('Comment' => array(
  1725. 'id' => 4,
  1726. 'article_id' => 1,
  1727. 'user_id' => 1,
  1728. 'comment' => 'Fourth Comment for First Article',
  1729. 'published' => 'N'
  1730. )),
  1731. array('Comment' => array(
  1732. 'id' => 5,
  1733. 'article_id' => 2,
  1734. 'user_id' => 1,
  1735. 'comment' => 'First Comment for Second Article',
  1736. 'published' => 'Y'
  1737. )),
  1738. array('Comment' => array(
  1739. 'id' => 6,
  1740. 'article_id' => 2,
  1741. 'user_id' => 2,
  1742. 'comment' => 'Second Comment for Second Article',
  1743. 'published' => 'Y'
  1744. ))));
  1745. $data = array(
  1746. 'Comment' => array(
  1747. 'article_id' => 2,
  1748. 'user_id' => 4,
  1749. 'comment' => 'Brand New Comment',
  1750. 'published' => 'N'
  1751. ),
  1752. 'Article' => array(
  1753. 'id' => 2,
  1754. 'title' => 'Second Article Modified'
  1755. ));
  1756. $result = $Comment->create($data);
  1757. $this->assertFalse(empty($result));
  1758. $result = $Comment->save();
  1759. $this->assertFalse(empty($result));
  1760. $articles = $Article->find('all', array(
  1761. 'fields' => array('id','title'),
  1762. 'recursive' => -1
  1763. ));
  1764. $comments = $Comment->find('all', array(
  1765. 'fields' => array('id','article_id','user_id','comment','published'),
  1766. 'recursive' => -1
  1767. ));
  1768. $this->assertEqual($articles, array(
  1769. array('Article' => array(
  1770. 'id' => 1,
  1771. 'title' => 'First Article'
  1772. )),
  1773. array('Article' => array(
  1774. 'id' => 2,
  1775. 'title' => 'Second Article'
  1776. )),
  1777. array('Article' => array(
  1778. 'id' => 3,
  1779. 'title' => 'Third Article'
  1780. ))));
  1781. $this->assertEqual($comments, array(
  1782. array('Comment' => array(
  1783. 'id' => 1,
  1784. 'article_id' => 1,
  1785. 'user_id' => 2,
  1786. 'comment' => 'First Comment for First Article',
  1787. 'published' => 'Y'
  1788. )),
  1789. array('Comment' => array(
  1790. 'id' => 2,
  1791. 'article_id' => 1,
  1792. 'user_id' => 4,
  1793. 'comment' => 'Second Comment for First Article',
  1794. 'published' => 'Y'
  1795. )),
  1796. array('Comment' => array(
  1797. 'id' => 3,
  1798. 'article_id' => 1,
  1799. 'user_id' => 1,
  1800. 'comment' => 'Third Comment for First Article',
  1801. 'published' => 'Y'
  1802. )),
  1803. array('Comment' => array(
  1804. 'id' => 4,
  1805. 'article_id' => 1,
  1806. 'user_id' => 1,
  1807. 'comment' => 'Fourth Comment for First Article',
  1808. 'published' => 'N'
  1809. )),
  1810. array('Comment' => array(
  1811. 'id' => 5,
  1812. 'article_id' => 2,
  1813. 'user_id' => 1,
  1814. 'comment' => 'First Comment for Second Article',
  1815. 'published' => 'Y'
  1816. )),
  1817. array('Comment' => array(
  1818. 'id' => 6,
  1819. 'article_id' => 2,
  1820. 'user_id' => 2, 'comment' =>
  1821. 'Second Comment for Second Article',
  1822. 'published' => 'Y'
  1823. )),
  1824. array('Comment' => array(
  1825. 'id' => 7,
  1826. 'article_id' => 2,
  1827. 'user_id' => 4,
  1828. 'comment' => 'Brand New Comment',
  1829. 'published' => 'N'
  1830. ))));
  1831. }
  1832. /**
  1833. * testCreationWithMultipleDataSameModel method
  1834. *
  1835. * @return void
  1836. */
  1837. public function testCreationWithMultipleDataSameModel() {
  1838. $this->loadFixtures('Article');
  1839. $Article = new Article();
  1840. $SecondaryArticle = new Article();
  1841. $result = $Article->field('title', array('id' => 1));
  1842. $this->assertEqual($result, 'First Article');
  1843. $data = array(
  1844. 'Article' => array(
  1845. 'user_id' => 2,
  1846. 'title' => 'Brand New Article',
  1847. 'body' => 'Brand New Article Body',
  1848. 'published' => 'Y'
  1849. ),
  1850. 'SecondaryArticle' => array(
  1851. 'id' => 1
  1852. ));
  1853. $Article->create();
  1854. $result = $Article->save($data);
  1855. $this->assertFalse(empty($result));
  1856. $result = $Article->getInsertID();
  1857. $this->assertTrue(!empty($result));
  1858. $result = $Article->field('title', array('id' => 1));
  1859. $this->assertEqual($result, 'First Article');
  1860. $articles = $Article->find('all', array(
  1861. 'fields' => array('id','title'),
  1862. 'recursive' => -1
  1863. ));
  1864. $this->assertEqual($articles, array(
  1865. array('Article' => array(
  1866. 'id' => 1,
  1867. 'title' => 'First Article'
  1868. )),
  1869. array('Article' => array(
  1870. 'id' => 2,
  1871. 'title' => 'Second Article'
  1872. )),
  1873. array('Article' => array(
  1874. 'id' => 3,
  1875. 'title' => 'Third Article'
  1876. )),
  1877. array('Article' => array(
  1878. 'id' => 4,
  1879. 'title' => 'Brand New Article'
  1880. ))));
  1881. }
  1882. /**
  1883. * testCreationWithMultipleDataSameModelManualInstances method
  1884. *
  1885. * @return void
  1886. */
  1887. public function testCreationWithMultipleDataSameModelManualInstances() {
  1888. $this->loadFixtures('PrimaryModel');
  1889. $Primary = new PrimaryModel();
  1890. $Secondary = new PrimaryModel();
  1891. $result = $Primary->field('primary_name', array('id' => 1));
  1892. $this->assertEqual($result, 'Primary Name Existing');
  1893. $data = array(
  1894. 'PrimaryModel' => array(
  1895. 'primary_name' => 'Primary Name New'
  1896. ),
  1897. 'SecondaryModel' => array(
  1898. 'id' => array(1)
  1899. ));
  1900. $Primary->create();
  1901. $result = $Primary->save($data);
  1902. $this->assertFalse(empty($result));
  1903. $result = $Primary->field('primary_name', array('id' => 1));
  1904. $this->assertEqual($result, 'Primary Name Existing');
  1905. $result = $Primary->getInsertID();
  1906. $this->assertTrue(!empty($result));
  1907. $result = $Primary->field('primary_name', array('id' => $result));
  1908. $this->assertEqual($result, 'Primary Name New');
  1909. $result = $Primary->find('count');
  1910. $this->assertEqual($result, 2);
  1911. }
  1912. /**
  1913. * testRecordExists method
  1914. *
  1915. * @return void
  1916. */
  1917. public function testRecordExists() {
  1918. $this->loadFixtures('User');
  1919. $TestModel = new User();
  1920. $this->assertFalse($TestModel->exists());
  1921. $TestModel->read(null, 1);
  1922. $this->assertTrue($TestModel->exists());
  1923. $TestModel->create();
  1924. $this->assertFalse($TestModel->exists());
  1925. $TestModel->id = 4;
  1926. $this->assertTrue($TestModel->exists());
  1927. $TestModel = new TheVoid();
  1928. $this->assertFalse($TestModel->exists());
  1929. }
  1930. /**
  1931. * testRecordExistsMissingTable method
  1932. *
  1933. * @expectedException PDOException
  1934. * @return void
  1935. */
  1936. public function testRecordExistsMissingTable() {
  1937. $TestModel = new TheVoid();
  1938. $TestModel->id = 5;
  1939. $TestModel->exists();
  1940. }
  1941. /**
  1942. * testUpdateExisting method
  1943. *
  1944. * @return void
  1945. */
  1946. public function testUpdateExisting() {
  1947. $this->loadFixtures('User', 'Article', 'Comment');
  1948. $TestModel = new User();
  1949. $TestModel->create();
  1950. $TestModel->save(array(
  1951. 'User' => array(
  1952. 'user' => 'some user',
  1953. 'password' => 'some password'
  1954. )));
  1955. $this->assertTrue(is_int($TestModel->id) || (intval($TestModel->id) === 5));
  1956. $id = $TestModel->id;
  1957. $TestModel->save(array(
  1958. 'User' => array(
  1959. 'user' => 'updated user'
  1960. )));
  1961. $this->assertEqual($TestModel->id, $id);
  1962. $result = $TestModel->findById($id);
  1963. $this->assertEqual($result['User']['user'], 'updated user');
  1964. $this->assertEqual($result['User']['password'], 'some password');
  1965. $Article = new Article();
  1966. $Comment = new Comment();
  1967. $data = array(
  1968. 'Comment' => array(
  1969. 'id' => 1,
  1970. 'comment' => 'First Comment for First Article'
  1971. ),
  1972. 'Article' => array(
  1973. 'id' => 2,
  1974. 'title' => 'Second Article'
  1975. ));
  1976. $result = $Article->save($data);
  1977. $this->assertFalse(empty($result));
  1978. $result = $Comment->save($data);
  1979. $this->assertFalse(empty($result));
  1980. }
  1981. /**
  1982. * test updating records and saving blank values.
  1983. *
  1984. * @return void
  1985. */
  1986. public function testUpdateSavingBlankValues() {
  1987. $this->loadFixtures('Article');
  1988. $Article = new Article();
  1989. $Article->validate = array();
  1990. $Article->create();
  1991. $result = $Article->save(array(
  1992. 'id' => 1,
  1993. 'title' => '',
  1994. 'body' => ''
  1995. ));
  1996. $this->assertTrue((bool)$result);
  1997. $result = $Article->find('first', array('conditions' => array('Article.id' => 1)));
  1998. $this->assertEqual('', $result['Article']['title'], 'Title is not blank');
  1999. $this->assertEqual('', $result['Article']['body'], 'Body is not blank');
  2000. }
  2001. /**
  2002. * testUpdateMultiple method
  2003. *
  2004. * @return void
  2005. */
  2006. public function testUpdateMultiple() {
  2007. $this->loadFixtures('Comment', 'Article', 'User', 'CategoryThread');
  2008. $TestModel = new Comment();
  2009. $result = Set::extract($TestModel->find('all'), '{n}.Comment.user_id');
  2010. $expected = array('2', '4', '1', '1', '1', '2');
  2011. $this->assertEqual($expected, $result);
  2012. $TestModel->updateAll(array('Comment.user_id' => 5), array('Comment.user_id' => 2));
  2013. $result = Set::combine($TestModel->find('all'), '{n}.Comment.id', '{n}.Comment.user_id');
  2014. $expected = array(1 => 5, 2 => 4, 3 => 1, 4 => 1, 5 => 1, 6 => 5);
  2015. $this->assertEqual($expected, $result);
  2016. $result = $TestModel->updateAll(
  2017. array('Comment.comment' => "'Updated today'"),
  2018. array('Comment.user_id' => 5)
  2019. );
  2020. $this->assertFalse(empty($result));
  2021. $result = Set::extract(
  2022. $TestModel->find('all', array(
  2023. 'conditions' => array(
  2024. 'Comment.user_id' => 5
  2025. ))),
  2026. '{n}.Comment.comment'
  2027. );
  2028. $expected = array_fill(0, 2, 'Updated today');
  2029. $this->assertEqual($expected, $result);
  2030. }
  2031. /**
  2032. * testHabtmUuidWithUuidId method
  2033. *
  2034. * @return void
  2035. */
  2036. public function testHabtmUuidWithUuidId() {
  2037. $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolio', 'UuiditemsUuidportfolioNumericid');
  2038. $TestModel = new Uuidportfolio();
  2039. $data = array('Uuidportfolio' => array('name' => 'Portfolio 3'));
  2040. $data['Uuiditem']['Uuiditem'] = array('483798c8-c7cc-430e-8cf9-4fcc40cf8569');
  2041. $TestModel->create($data);
  2042. $TestModel->save();
  2043. $id = $TestModel->id;
  2044. $result = $TestModel->read(null, $id);
  2045. $this->assertEqual(1, count($result['Uuiditem']));
  2046. $this->assertEqual(strlen($result['Uuiditem'][0]['UuiditemsUuidportfolio']['id']), 36);
  2047. }
  2048. /**
  2049. * test HABTM saving when join table has no primary key and only 2 columns.
  2050. *
  2051. * @return void
  2052. */
  2053. public function testHabtmSavingWithNoPrimaryKeyUuidJoinTable() {
  2054. $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
  2055. $Fruit = new Fruit();
  2056. $data = array(
  2057. 'Fruit' => array(
  2058. 'color' => 'Red',
  2059. 'shape' => 'Heart-shaped',
  2060. 'taste' => 'sweet',
  2061. 'name' => 'Strawberry',
  2062. ),
  2063. 'UuidTag' => array(
  2064. 'UuidTag' => array(
  2065. '481fc6d0-b920-43e0-e50f-6d1740cf8569'
  2066. )
  2067. )
  2068. );
  2069. $result = $Fruit->save($data);
  2070. $this->assertFalse(empty($result));
  2071. }
  2072. /**
  2073. * test HABTM saving when join table has no primary key and only 2 columns, no with model is used.
  2074. *
  2075. * @return void
  2076. */
  2077. public function testHabtmSavingWithNoPrimaryKeyUuidJoinTableNoWith() {
  2078. $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
  2079. $Fruit = new FruitNoWith();
  2080. $data = array(
  2081. 'Fruit' => array(
  2082. 'color' => 'Red',
  2083. 'shape' => 'Heart-shaped',
  2084. 'taste' => 'sweet',
  2085. 'name' => 'Strawberry',
  2086. ),
  2087. 'UuidTag' => array(
  2088. 'UuidTag' => array(
  2089. '481fc6d0-b920-43e0-e50f-6d1740cf8569'
  2090. )
  2091. )
  2092. );
  2093. $result = $Fruit->save($data);
  2094. $this->assertFalse(empty($result));
  2095. }
  2096. /**
  2097. * testHabtmUuidWithNumericId method
  2098. *
  2099. * @return void
  2100. */
  2101. public function testHabtmUuidWithNumericId() {
  2102. $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolioNumericid');
  2103. $TestModel = new Uuiditem();
  2104. $data = array('Uuiditem' => array('name' => 'Item 7', 'published' => 0));
  2105. $data['Uuidportfolio']['Uuidportfolio'] = array('480af662-eb8c-47d3-886b-230540cf8569');
  2106. $TestModel->create($data);
  2107. $TestModel->save();
  2108. $id = $TestModel->id;
  2109. $result = $TestModel->read(null, $id);
  2110. $this->assertEqual(1, count($result['Uuidportfolio']));
  2111. }
  2112. /**
  2113. * testSaveMultipleHabtm method
  2114. *
  2115. * @return void
  2116. */
  2117. public function testSaveMultipleHabtm() {
  2118. $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC');
  2119. $TestModel = new JoinA();
  2120. $result = $TestModel->findById(1);
  2121. $expected = array(
  2122. 'JoinA' => array(
  2123. 'id' => 1,
  2124. 'name' => 'Join A 1',
  2125. 'body' => 'Join A 1 Body',
  2126. 'created' => '2008-01-03 10:54:23',
  2127. 'updated' => '2008-01-03 10:54:23'
  2128. ),
  2129. 'JoinB' => array(
  2130. 0 => array(
  2131. 'id' => 2,
  2132. 'name' => 'Join B 2',
  2133. 'created' => '2008-01-03 10:55:02',
  2134. 'updated' => '2008-01-03 10:55:02',
  2135. 'JoinAsJoinB' => array(
  2136. 'id' => 1,
  2137. 'join_a_id' => 1,
  2138. 'join_b_id' => 2,
  2139. 'other' => 'Data for Join A 1 Join B 2',
  2140. 'created' => '2008-01-03 10:56:33',
  2141. 'updated' => '2008-01-03 10:56:33'
  2142. ))),
  2143. 'JoinC' => array(
  2144. 0 => array(
  2145. 'id' => 2,
  2146. 'name' => 'Join C 2',
  2147. 'created' => '2008-01-03 10:56:12',
  2148. 'updated' => '2008-01-03 10:56:12',
  2149. 'JoinAsJoinC' => array(
  2150. 'id' => 1,
  2151. 'join_a_id' => 1,
  2152. 'join_c_id' => 2,
  2153. 'other' => 'Data for Join A 1 Join C 2',
  2154. 'created' => '2008-01-03 10:57:22',
  2155. 'updated' => '2008-01-03 10:57:22'
  2156. ))));
  2157. $this->assertEqual($expected, $result);
  2158. $ts = date('Y-m-d H:i:s');
  2159. $TestModel->id = 1;
  2160. $data = array(
  2161. 'JoinA' => array(
  2162. 'id' => '1',
  2163. 'name' => 'New name for Join A 1',
  2164. 'updated' => $ts
  2165. ),
  2166. 'JoinB' => array(
  2167. array(
  2168. 'id' => 1,
  2169. 'join_b_id' => 2,
  2170. 'other' => 'New data for Join A 1 Join B 2',
  2171. 'created' => $ts,
  2172. 'updated' => $ts
  2173. )),
  2174. 'JoinC' => array(
  2175. array(
  2176. 'id' => 1,
  2177. 'join_c_id' => 2,
  2178. 'other' => 'New data for Join A 1 Join C 2',
  2179. 'created' => $ts,
  2180. 'updated' => $ts
  2181. )));
  2182. $TestModel->set($data);
  2183. $TestModel->save();
  2184. $result = $TestModel->findById(1);
  2185. $expected = array(
  2186. 'JoinA' => array(
  2187. 'id' => 1,
  2188. 'name' => 'New name for Join A 1',
  2189. 'body' => 'Join A 1 Body',
  2190. 'created' => '2008-01-03 10:54:23',
  2191. 'updated' => $ts
  2192. ),
  2193. 'JoinB' => array(
  2194. 0 => array(
  2195. 'id' => 2,
  2196. 'name' => 'Join B 2',
  2197. 'created' => '2008-01-03 10:55:02',
  2198. 'updated' => '2008-01-03 10:55:02',
  2199. 'JoinAsJoinB' => array(
  2200. 'id' => 1,
  2201. 'join_a_id' => 1,
  2202. 'join_b_id' => 2,
  2203. 'other' => 'New data for Join A 1 Join B 2',
  2204. 'created' => $ts,
  2205. 'updated' => $ts
  2206. ))),
  2207. 'JoinC' => array(
  2208. 0 => array(
  2209. 'id' => 2,
  2210. 'name' => 'Join C 2',
  2211. 'created' => '2008-01-03 10:56:12',
  2212. 'updated' => '2008-01-03 10:56:12',
  2213. 'JoinAsJoinC' => array(
  2214. 'id' => 1,
  2215. 'join_a_id' => 1,
  2216. 'join_c_id' => 2,
  2217. 'other' => 'New data for Join A 1 Join C 2',
  2218. 'created' => $ts,
  2219. 'updated' => $ts
  2220. ))));
  2221. $this->assertEqual($expected, $result);
  2222. }
  2223. /**
  2224. * testSaveAll method
  2225. *
  2226. * @return void
  2227. */
  2228. public function testSaveAll() {
  2229. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  2230. $TestModel = new Post();
  2231. $result = $TestModel->find('all');
  2232. $this->assertEqual(count($result), 3);
  2233. $this->assertFalse(isset($result[3]));
  2234. $ts = date('Y-m-d H:i:s');
  2235. $TestModel->saveAll(array(
  2236. 'Post' => array(
  2237. 'title' => 'Post with Author',
  2238. 'body' => 'This post will be saved with an author'
  2239. ),
  2240. 'Author' => array(
  2241. 'user' => 'bob',
  2242. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
  2243. )));
  2244. $result = $TestModel->find('all');
  2245. $expected = array(
  2246. 'Post' => array(
  2247. 'id' => '4',
  2248. 'author_id' => '5',
  2249. 'title' => 'Post with Author',
  2250. 'body' => 'This post will be saved with an author',
  2251. 'published' => 'N',
  2252. 'created' => $ts,
  2253. 'updated' => $ts
  2254. ),
  2255. 'Author' => array(
  2256. 'id' => '5',
  2257. 'user' => 'bob',
  2258. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
  2259. 'created' => $ts,
  2260. 'updated' => $ts,
  2261. 'test' => 'working'
  2262. ));
  2263. $this->assertEqual($result[3], $expected);
  2264. $this->assertEqual(count($result), 4);
  2265. $TestModel->deleteAll(true);
  2266. $this->assertEqual($TestModel->find('all'), array());
  2267. // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
  2268. $this->db->truncate($TestModel);
  2269. $ts = date('Y-m-d H:i:s');
  2270. $TestModel->saveAll(array(
  2271. array(
  2272. 'title' => 'Multi-record post 1',
  2273. 'body' => 'First multi-record post',
  2274. 'author_id' => 2
  2275. ),
  2276. array(
  2277. 'title' => 'Multi-record post 2',
  2278. 'body' => 'Second multi-record post',
  2279. 'author_id' => 2
  2280. )));
  2281. $result = $TestModel->find('all', array(
  2282. 'recursive' => -1,
  2283. 'order' => 'Post.id ASC'
  2284. ));
  2285. $expected = array(
  2286. array(
  2287. 'Post' => array(
  2288. 'id' => '1',
  2289. 'author_id' => '2',
  2290. 'title' => 'Multi-record post 1',
  2291. 'body' => 'First multi-record post',
  2292. 'published' => 'N',
  2293. 'created' => $ts,
  2294. 'updated' => $ts
  2295. )),
  2296. array(
  2297. 'Post' => array(
  2298. 'id' => '2',
  2299. 'author_id' => '2',
  2300. 'title' => 'Multi-record post 2',
  2301. 'body' => 'Second multi-record post',
  2302. 'published' => 'N',
  2303. 'created' => $ts,
  2304. 'updated' => $ts
  2305. )));
  2306. $this->assertEqual($expected, $result);
  2307. $TestModel = new Comment();
  2308. $ts = date('Y-m-d H:i:s');
  2309. $result = $TestModel->saveAll(array(
  2310. 'Comment' => array(
  2311. 'article_id' => 2,
  2312. 'user_id' => 2,
  2313. 'comment' => 'New comment with attachment',
  2314. 'published' => 'Y'
  2315. ),
  2316. 'Attachment' => array(
  2317. 'attachment' => 'some_file.tgz'
  2318. )));
  2319. $this->assertFalse(empty($result));
  2320. $result = $TestModel->find('all');
  2321. $expected = array(
  2322. 'id' => '7',
  2323. 'article_id' => '2',
  2324. 'user_id' => '2',
  2325. 'comment' => 'New comment with attachment',
  2326. 'published' => 'Y',
  2327. 'created' => $ts,
  2328. 'updated' => $ts
  2329. );
  2330. $this->assertEqual($result[6]['Comment'], $expected);
  2331. $expected = array(
  2332. 'id' => '7',
  2333. 'article_id' => '2',
  2334. 'user_id' => '2',
  2335. 'comment' => 'New comment with attachment',
  2336. 'published' => 'Y',
  2337. 'created' => $ts,
  2338. 'updated' => $ts
  2339. );
  2340. $this->assertEqual($result[6]['Comment'], $expected);
  2341. $expected = array(
  2342. 'id' => '2',
  2343. 'comment_id' => '7',
  2344. 'attachment' => 'some_file.tgz',
  2345. 'created' => $ts,
  2346. 'updated' => $ts
  2347. );
  2348. $this->assertEqual($result[6]['Attachment'], $expected);
  2349. }
  2350. /**
  2351. * Test SaveAll with Habtm relations
  2352. *
  2353. * @return void
  2354. */
  2355. public function testSaveAllHabtm() {
  2356. $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  2357. $data = array(
  2358. 'Article' => array(
  2359. 'user_id' => 1,
  2360. 'title' => 'Article Has and belongs to Many Tags'
  2361. ),
  2362. 'Tag' => array(
  2363. 'Tag' => array(1, 2)
  2364. ),
  2365. 'Comment' => array(
  2366. array(
  2367. 'comment' => 'Article comment',
  2368. 'user_id' => 1
  2369. )));
  2370. $Article = new Article();
  2371. $result = $Article->saveAll($data);
  2372. $this->assertFalse(empty($result));
  2373. $result = $Article->read();
  2374. $this->assertEqual(count($result['Tag']), 2);
  2375. $this->assertEqual($result['Tag'][0]['tag'], 'tag1');
  2376. $this->assertEqual(count($result['Comment']), 1);
  2377. $this->assertEqual(count($result['Comment'][0]['comment']['Article comment']), 1);
  2378. }
  2379. /**
  2380. * Test SaveAll with Habtm relations and extra join table fields
  2381. *
  2382. * @return void
  2383. */
  2384. public function testSaveAllHabtmWithExtraJoinTableFields() {
  2385. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  2386. $data = array(
  2387. 'Something' => array(
  2388. 'id' => 4,
  2389. 'title' => 'Extra Fields',
  2390. 'body' => 'Extra Fields Body',
  2391. 'published' => '1'
  2392. ),
  2393. 'SomethingElse' => array(
  2394. array('something_else_id' => 1, 'doomed' => '1'),
  2395. array('something_else_id' => 2, 'doomed' => '0'),
  2396. array('something_else_id' => 3, 'doomed' => '1')
  2397. )
  2398. );
  2399. $Something = new Something();
  2400. $result = $Something->saveAll($data);
  2401. $this->assertFalse(empty($result));
  2402. $result = $Something->read();
  2403. $this->assertEqual(count($result['SomethingElse']), 3);
  2404. $this->assertTrue(Set::matches('/Something[id=4]', $result));
  2405. $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
  2406. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
  2407. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
  2408. $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
  2409. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
  2410. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
  2411. $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
  2412. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
  2413. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
  2414. }
  2415. /**
  2416. * testSaveAllHasOne method
  2417. *
  2418. * @return void
  2419. */
  2420. public function testSaveAllHasOne() {
  2421. $model = new Comment();
  2422. $model->deleteAll(true);
  2423. $this->assertEqual($model->find('all'), array());
  2424. $model->Attachment->deleteAll(true);
  2425. $this->assertEqual($model->Attachment->find('all'), array());
  2426. $this->assertTrue($model->saveAll(array(
  2427. 'Comment' => array(
  2428. 'comment' => 'Comment with attachment',
  2429. 'article_id' => 1,
  2430. 'user_id' => 1
  2431. ),
  2432. 'Attachment' => array(
  2433. 'attachment' => 'some_file.zip'
  2434. ))));
  2435. $result = $model->find('all', array('fields' => array(
  2436. 'Comment.id', 'Comment.comment', 'Attachment.id',
  2437. 'Attachment.comment_id', 'Attachment.attachment'
  2438. )));
  2439. $expected = array(array(
  2440. 'Comment' => array(
  2441. 'id' => '1',
  2442. 'comment' => 'Comment with attachment'
  2443. ),
  2444. 'Attachment' => array(
  2445. 'id' => '1',
  2446. 'comment_id' => '1',
  2447. 'attachment' => 'some_file.zip'
  2448. )));
  2449. $this->assertEqual($expected, $result);
  2450. $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
  2451. $data = array(
  2452. 'Comment' => array(
  2453. 'comment' => 'Comment with attachment',
  2454. 'article_id' => 1,
  2455. 'user_id' => 1
  2456. ),
  2457. 'Attachment' => array(
  2458. 'attachment' => 'some_file.zip'
  2459. ));
  2460. $this->assertTrue($model->saveAll($data, array('validate' => 'first')));
  2461. }
  2462. /**
  2463. * testSaveAllBelongsTo method
  2464. *
  2465. * @return void
  2466. */
  2467. public function testSaveAllBelongsTo() {
  2468. $model = new Comment();
  2469. $model->deleteAll(true);
  2470. $this->assertEqual($model->find('all'), array());
  2471. $model->Article->deleteAll(true);
  2472. $this->assertEqual($model->Article->find('all'), array());
  2473. $this->assertTrue($model->saveAll(array(
  2474. 'Comment' => array(
  2475. 'comment' => 'Article comment',
  2476. 'article_id' => 1,
  2477. 'user_id' => 1
  2478. ),
  2479. 'Article' => array(
  2480. 'title' => 'Model Associations 101',
  2481. 'user_id' => 1
  2482. ))));
  2483. $result = $model->find('all', array('fields' => array(
  2484. 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
  2485. )));
  2486. $expected = array(array(
  2487. 'Comment' => array(
  2488. 'id' => '1',
  2489. 'article_id' => '1',
  2490. 'comment' => 'Article comment'
  2491. ),
  2492. 'Article' => array(
  2493. 'id' => '1',
  2494. 'title' => 'Model Associations 101'
  2495. )));
  2496. $this->assertEqual($expected, $result);
  2497. }
  2498. /**
  2499. * testSaveAllHasOneValidation method
  2500. *
  2501. * @return void
  2502. */
  2503. public function testSaveAllHasOneValidation() {
  2504. $model = new Comment();
  2505. $model->deleteAll(true);
  2506. $this->assertEqual($model->find('all'), array());
  2507. $model->Attachment->deleteAll(true);
  2508. $this->assertEqual($model->Attachment->find('all'), array());
  2509. $model->validate = array('comment' => 'notEmpty');
  2510. $model->Attachment->validate = array('attachment' => 'notEmpty');
  2511. $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
  2512. $this->assertEquals($model->saveAll(
  2513. array(
  2514. 'Comment' => array(
  2515. 'comment' => '',
  2516. 'article_id' => 1,
  2517. 'user_id' => 1
  2518. ),
  2519. 'Attachment' => array('attachment' => '')
  2520. ),
  2521. array('validate' => 'first')
  2522. ), false);
  2523. $expected = array(
  2524. 'Comment' => array('comment' => array('This field cannot be left blank')),
  2525. 'Attachment' => array('attachment' => array('This field cannot be left blank'))
  2526. );
  2527. $this->assertEqual($model->validationErrors, $expected['Comment']);
  2528. $this->assertEqual($model->Attachment->validationErrors, $expected['Attachment']);
  2529. $this->assertFalse($model->saveAll(
  2530. array(
  2531. 'Comment' => array('comment' => '', 'article_id' => 1, 'user_id' => 1),
  2532. 'Attachment' => array('attachment' => '')
  2533. ),
  2534. array('validate' => 'only')
  2535. ));
  2536. $this->assertEqual($model->validationErrors, $expected['Comment']);
  2537. $this->assertEqual($model->Attachment->validationErrors, $expected['Attachment']);
  2538. }
  2539. /**
  2540. * testSaveAllAtomic method
  2541. *
  2542. * @return void
  2543. */
  2544. public function testSaveAllAtomic() {
  2545. $this->loadFixtures('Article', 'User');
  2546. $TestModel = new Article();
  2547. $result = $TestModel->saveAll(array(
  2548. 'Article' => array(
  2549. 'title' => 'Post with Author',
  2550. 'body' => 'This post will be saved with an author',
  2551. 'user_id' => 2
  2552. ),
  2553. 'Comment' => array(
  2554. array('comment' => 'First new comment', 'user_id' => 2))
  2555. ), array('atomic' => false));
  2556. $this->assertIdentical($result, array('Article' => true, 'Comment' => array(true)));
  2557. $result = $TestModel->saveAll(array(
  2558. array(
  2559. 'id' => '1',
  2560. 'title' => 'Baleeted First Post',
  2561. 'body' => 'Baleeted!',
  2562. 'published' => 'N'
  2563. ),
  2564. array(
  2565. 'id' => '2',
  2566. 'title' => 'Just update the title'
  2567. ),
  2568. array(
  2569. 'title' => 'Creating a fourth post',
  2570. 'body' => 'Fourth post body',
  2571. 'user_id' => 2
  2572. )
  2573. ), array('atomic' => false));
  2574. $this->assertIdentical($result, array(true, true, true));
  2575. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  2576. $result = $TestModel->saveAll(array(
  2577. array(
  2578. 'id' => '1',
  2579. 'title' => 'Un-Baleeted First Post',
  2580. 'body' => 'Not Baleeted!',
  2581. 'published' => 'Y'
  2582. ),
  2583. array(
  2584. 'id' => '2',
  2585. 'title' => '',
  2586. 'body' => 'Trying to get away with an empty title'
  2587. )
  2588. ), array('validate' => true, 'atomic' => false));
  2589. $this->assertIdentical($result, array(true, false));
  2590. $result = $TestModel->saveAll(array(
  2591. 'Article' => array('id' => 2),
  2592. 'Comment' => array(
  2593. array(
  2594. 'comment' => 'First new comment',
  2595. 'published' => 'Y',
  2596. 'user_id' => 1
  2597. ),
  2598. array(
  2599. 'comment' => 'Second new comment',
  2600. 'published' => 'Y',
  2601. 'user_id' => 2
  2602. ))
  2603. ), array('validate' => true, 'atomic' => false));
  2604. $this->assertIdentical($result, array('Article' => true, 'Comment' => array(true, true)));
  2605. }
  2606. /**
  2607. * testSaveAllHasMany method
  2608. *
  2609. * @return void
  2610. */
  2611. public function testSaveAllHasMany() {
  2612. $this->loadFixtures('Article', 'Comment');
  2613. $TestModel = new Article();
  2614. $TestModel->order = array('Article.created' => 'ASC');
  2615. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  2616. $result = $TestModel->saveAll(array(
  2617. 'Article' => array('id' => 2),
  2618. 'Comment' => array(
  2619. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  2620. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  2621. )
  2622. ));
  2623. $this->assertFalse(empty($result));
  2624. $result = $TestModel->findById(2);
  2625. $expected = array(
  2626. 'First Comment for Second Article',
  2627. 'Second Comment for Second Article',
  2628. 'First new comment',
  2629. 'Second new comment'
  2630. );
  2631. $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected);
  2632. $result = $TestModel->saveAll(
  2633. array(
  2634. 'Article' => array('id' => 2),
  2635. 'Comment' => array(
  2636. array(
  2637. 'comment' => 'Third new comment',
  2638. 'published' => 'Y',
  2639. 'user_id' => 1
  2640. ))),
  2641. array('atomic' => false)
  2642. );
  2643. $this->assertFalse(empty($result));
  2644. $result = $TestModel->findById(2);
  2645. $expected = array(
  2646. 'First Comment for Second Article',
  2647. 'Second Comment for Second Article',
  2648. 'First new comment',
  2649. 'Second new comment',
  2650. 'Third new comment'
  2651. );
  2652. $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected);
  2653. $TestModel->beforeSaveReturn = false;
  2654. $result = $TestModel->saveAll(
  2655. array(
  2656. 'Article' => array('id' => 2),
  2657. 'Comment' => array(
  2658. array(
  2659. 'comment' => 'Fourth new comment',
  2660. 'published' => 'Y',
  2661. 'user_id' => 1
  2662. ))),
  2663. array('atomic' => false)
  2664. );
  2665. $this->assertEqual($result, array('Article' => false));
  2666. $result = $TestModel->findById(2);
  2667. $expected = array(
  2668. 'First Comment for Second Article',
  2669. 'Second Comment for Second Article',
  2670. 'First new comment',
  2671. 'Second new comment',
  2672. 'Third new comment'
  2673. );
  2674. $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected);
  2675. }
  2676. /**
  2677. * testSaveAllHasManyValidation method
  2678. *
  2679. * @return void
  2680. */
  2681. public function testSaveAllHasManyValidation() {
  2682. $this->loadFixtures('Article', 'Comment');
  2683. $TestModel = new Article();
  2684. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  2685. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  2686. $result = $TestModel->saveAll(array(
  2687. 'Article' => array('id' => 2),
  2688. 'Comment' => array(
  2689. array('comment' => '', 'published' => 'Y', 'user_id' => 1),
  2690. )
  2691. ), array('validate' => true));
  2692. $this->assertFalse($result);
  2693. $expected = array('Comment' => array(
  2694. array('comment' => array('This field cannot be left blank'))
  2695. ));
  2696. $this->assertEqual($TestModel->validationErrors, $expected);
  2697. $expected = array(
  2698. array('comment' => array('This field cannot be left blank'))
  2699. );
  2700. $this->assertEqual($TestModel->Comment->validationErrors, $expected);
  2701. $result = $TestModel->saveAll(array(
  2702. 'Article' => array('id' => 2),
  2703. 'Comment' => array(
  2704. array(
  2705. 'comment' => '',
  2706. 'published' => 'Y',
  2707. 'user_id' => 1
  2708. ))
  2709. ), array('validate' => 'first'));
  2710. $this->assertFalse($result);
  2711. }
  2712. /**
  2713. * test saveAll with transactions and ensure there is no missing rollback.
  2714. *
  2715. * @return void
  2716. */
  2717. public function testSaveAllManyRowsTransactionNoRollback() {
  2718. $this->loadFixtures('Post');
  2719. $this->getMock('DboSource', array('connect', 'rollback', 'describe'), array(), 'MockTransactionDboSource');
  2720. $db = ConnectionManager::create('mock_transaction', array(
  2721. 'datasource' => 'MockTransactionDboSource',
  2722. ));
  2723. $db->expects($this->once())
  2724. ->method('describe')
  2725. ->will($this->returnValue(array()));
  2726. $db->expects($this->once())->method('rollback');
  2727. $Post = new Post('mock_transaction');
  2728. $Post->validate = array(
  2729. 'title' => array('rule' => array('notEmpty'))
  2730. );
  2731. $data = array(
  2732. array('author_id' => 1, 'title' => 'New Fourth Post'),
  2733. array('author_id' => 1, 'title' => '')
  2734. );
  2735. $Post->saveAll($data, array('atomic' => true));
  2736. }
  2737. /**
  2738. * test saveAll with transactions and ensure there is no missing rollback.
  2739. *
  2740. * @return void
  2741. */
  2742. public function testSaveAllAssociatedTransactionNoRollback() {
  2743. $testDb = ConnectionManager::getDataSource('test');
  2744. $mock = $this->getMock(
  2745. 'DboSource',
  2746. array('connect', 'rollback', 'describe', 'create', 'update', 'begin'),
  2747. array(),
  2748. 'MockTransactionAssociatedDboSource'
  2749. );
  2750. $db = ConnectionManager::create('mock_transaction_assoc', array(
  2751. 'datasource' => 'MockTransactionAssociatedDboSource',
  2752. ));
  2753. $this->mockObjects[] = $db;
  2754. $db->columns = $testDb->columns;
  2755. $db->expects($this->once())->method('rollback');
  2756. $db->expects($this->any())->method('describe')
  2757. ->will($this->returnValue(array(
  2758. 'id' => array('type' => 'integer'),
  2759. 'title' => array('type' => 'string'),
  2760. 'body' => array('type' => 'text'),
  2761. 'published' => array('type' => 'string')
  2762. )));
  2763. $Post = new Post();
  2764. $Post->useDbConfig = 'mock_transaction_assoc';
  2765. $Post->Author->useDbConfig = 'mock_transaction_assoc';
  2766. $Post->Author->validate = array(
  2767. 'user' => array('rule' => array('notEmpty'))
  2768. );
  2769. $data = array(
  2770. 'Post' => array(
  2771. 'title' => 'New post',
  2772. 'body' => 'Content',
  2773. 'published' => 'Y'
  2774. ),
  2775. 'Author' => array(
  2776. 'user' => '',
  2777. 'password' => "sekret"
  2778. )
  2779. );
  2780. $Post->saveAll($data, array('validate' => true));
  2781. }
  2782. /**
  2783. * test saveAll with nested saveAll call.
  2784. *
  2785. * @return void
  2786. */
  2787. public function testSaveAllNestedSaveAll() {
  2788. $this->loadFixtures('Sample');
  2789. $TransactionTestModel = new TransactionTestModel();
  2790. $data = array(
  2791. array('apple_id' => 1, 'name' => 'sample5'),
  2792. );
  2793. $this->assertTrue($TransactionTestModel->saveAll($data, array('atomic' => true)));
  2794. }
  2795. /**
  2796. * testSaveAllTransaction method
  2797. *
  2798. * @return void
  2799. */
  2800. public function testSaveAllTransaction() {
  2801. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  2802. $TestModel = new Post();
  2803. $TestModel->validate = array('title' => 'notEmpty');
  2804. $data = array(
  2805. array('author_id' => 1, 'title' => 'New Fourth Post'),
  2806. array('author_id' => 1, 'title' => 'New Fifth Post'),
  2807. array('author_id' => 1, 'title' => '')
  2808. );
  2809. $ts = date('Y-m-d H:i:s');
  2810. $this->assertFalse($TestModel->saveAll($data));
  2811. $result = $TestModel->find('all', array('recursive' => -1));
  2812. $expected = array(
  2813. array('Post' => array(
  2814. 'id' => '1',
  2815. 'author_id' => 1,
  2816. 'title' => 'First Post',
  2817. 'body' => 'First Post Body',
  2818. 'published' => 'Y',
  2819. 'created' => '2007-03-18 10:39:23',
  2820. 'updated' => '2007-03-18 10:41:31'
  2821. )),
  2822. array('Post' => array(
  2823. 'id' => '2',
  2824. 'author_id' => 3,
  2825. 'title' => 'Second Post',
  2826. 'body' => 'Second Post Body',
  2827. 'published' => 'Y',
  2828. 'created' => '2007-03-18 10:41:23',
  2829. 'updated' => '2007-03-18 10:43:31'
  2830. )),
  2831. array('Post' => array(
  2832. 'id' => '3',
  2833. 'author_id' => 1,
  2834. 'title' => 'Third Post',
  2835. 'body' => 'Third Post Body',
  2836. 'published' => 'Y',
  2837. 'created' => '2007-03-18 10:43:23',
  2838. 'updated' => '2007-03-18 10:45:31'
  2839. )));
  2840. if (count($result) != 3) {
  2841. // Database doesn't support transactions
  2842. $expected[] = array(
  2843. 'Post' => array(
  2844. 'id' => '4',
  2845. 'author_id' => 1,
  2846. 'title' => 'New Fourth Post',
  2847. 'body' => null,
  2848. 'published' => 'N',
  2849. 'created' => $ts,
  2850. 'updated' => $ts
  2851. ));
  2852. $expected[] = array(
  2853. 'Post' => array(
  2854. 'id' => '5',
  2855. 'author_id' => 1,
  2856. 'title' => 'New Fifth Post',
  2857. 'body' => null,
  2858. 'published' => 'N',
  2859. 'created' => $ts,
  2860. 'updated' => $ts
  2861. ));
  2862. $this->assertEqual($expected, $result);
  2863. // Skip the rest of the transactional tests
  2864. return;
  2865. }
  2866. $this->assertEqual($expected, $result);
  2867. $data = array(
  2868. array('author_id' => 1, 'title' => 'New Fourth Post'),
  2869. array('author_id' => 1, 'title' => ''),
  2870. array('author_id' => 1, 'title' => 'New Sixth Post')
  2871. );
  2872. $ts = date('Y-m-d H:i:s');
  2873. $this->assertFalse($TestModel->saveAll($data));
  2874. $result = $TestModel->find('all', array('recursive' => -1));
  2875. $expected = array(
  2876. array('Post' => array(
  2877. 'id' => '1',
  2878. 'author_id' => 1,
  2879. 'title' => 'First Post',
  2880. 'body' => 'First Post Body',
  2881. 'published' => 'Y',
  2882. 'created' => '2007-03-18 10:39:23',
  2883. 'updated' => '2007-03-18 10:41:31'
  2884. )),
  2885. array('Post' => array(
  2886. 'id' => '2',
  2887. 'author_id' => 3,
  2888. 'title' => 'Second Post',
  2889. 'body' => 'Second Post Body',
  2890. 'published' => 'Y',
  2891. 'created' => '2007-03-18 10:41:23',
  2892. 'updated' => '2007-03-18 10:43:31'
  2893. )),
  2894. array('Post' => array(
  2895. 'id' => '3',
  2896. 'author_id' => 1,
  2897. 'title' => 'Third Post',
  2898. 'body' => 'Third Post Body',
  2899. 'published' => 'Y',
  2900. 'created' => '2007-03-18 10:43:23',
  2901. 'updated' => '2007-03-18 10:45:31'
  2902. )));
  2903. if (count($result) != 3) {
  2904. // Database doesn't support transactions
  2905. $expected[] = array(
  2906. 'Post' => array(
  2907. 'id' => '4',
  2908. 'author_id' => 1,
  2909. 'title' => 'New Fourth Post',
  2910. 'body' => 'Third Post Body',
  2911. 'published' => 'N',
  2912. 'created' => $ts,
  2913. 'updated' => $ts
  2914. ));
  2915. $expected[] = array(
  2916. 'Post' => array(
  2917. 'id' => '5',
  2918. 'author_id' => 1,
  2919. 'title' => 'Third Post',
  2920. 'body' => 'Third Post Body',
  2921. 'published' => 'N',
  2922. 'created' => $ts,
  2923. 'updated' => $ts
  2924. ));
  2925. }
  2926. $this->assertEqual($expected, $result);
  2927. $TestModel->validate = array('title' => 'notEmpty');
  2928. $data = array(
  2929. array('author_id' => 1, 'title' => 'New Fourth Post'),
  2930. array('author_id' => 1, 'title' => 'New Fifth Post'),
  2931. array('author_id' => 1, 'title' => 'New Sixth Post')
  2932. );
  2933. $this->assertTrue($TestModel->saveAll($data));
  2934. $result = $TestModel->find('all', array(
  2935. 'recursive' => -1,
  2936. 'fields' => array('author_id', 'title','body','published')
  2937. ));
  2938. $expected = array(
  2939. array('Post' => array(
  2940. 'author_id' => 1,
  2941. 'title' => 'First Post',
  2942. 'body' => 'First Post Body',
  2943. 'published' => 'Y'
  2944. )),
  2945. array('Post' => array(
  2946. 'author_id' => 3,
  2947. 'title' => 'Second Post',
  2948. 'body' => 'Second Post Body',
  2949. 'published' => 'Y'
  2950. )),
  2951. array('Post' => array(
  2952. 'author_id' => 1,
  2953. 'title' => 'Third Post',
  2954. 'body' => 'Third Post Body',
  2955. 'published' => 'Y'
  2956. )),
  2957. array('Post' => array(
  2958. 'author_id' => 1,
  2959. 'title' => 'New Fourth Post',
  2960. 'body' => '',
  2961. 'published' => 'N'
  2962. )),
  2963. array('Post' => array(
  2964. 'author_id' => 1,
  2965. 'title' => 'New Fifth Post',
  2966. 'body' => '',
  2967. 'published' => 'N'
  2968. )),
  2969. array('Post' => array(
  2970. 'author_id' => 1,
  2971. 'title' => 'New Sixth Post',
  2972. 'body' => '',
  2973. 'published' => 'N'
  2974. )));
  2975. $this->assertEqual($expected, $result);
  2976. }
  2977. /**
  2978. * testSaveAllValidation method
  2979. *
  2980. * @return void
  2981. */
  2982. public function testSaveAllValidation() {
  2983. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  2984. $TestModel = new Post();
  2985. $data = array(
  2986. array(
  2987. 'id' => '1',
  2988. 'title' => 'Baleeted First Post',
  2989. 'body' => 'Baleeted!',
  2990. 'published' => 'N'
  2991. ),
  2992. array(
  2993. 'id' => '2',
  2994. 'title' => 'Just update the title'
  2995. ),
  2996. array(
  2997. 'title' => 'Creating a fourth post',
  2998. 'body' => 'Fourth post body',
  2999. 'author_id' => 2
  3000. ));
  3001. $this->assertTrue($TestModel->saveAll($data));
  3002. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3003. $ts = date('Y-m-d H:i:s');
  3004. $expected = array(
  3005. array(
  3006. 'Post' => array(
  3007. 'id' => '1',
  3008. 'author_id' => '1',
  3009. 'title' => 'Baleeted First Post',
  3010. 'body' => 'Baleeted!',
  3011. 'published' => 'N',
  3012. 'created' => '2007-03-18 10:39:23',
  3013. 'updated' => $ts
  3014. )),
  3015. array(
  3016. 'Post' => array(
  3017. 'id' => '2',
  3018. 'author_id' => '3',
  3019. 'title' => 'Just update the title',
  3020. 'body' => 'Second Post Body',
  3021. 'published' => 'Y',
  3022. 'created' => '2007-03-18 10:41:23', 'updated' => $ts
  3023. )),
  3024. array(
  3025. 'Post' => array(
  3026. 'id' => '3',
  3027. 'author_id' => '1',
  3028. 'title' => 'Third Post',
  3029. 'body' => 'Third Post Body',
  3030. 'published' => 'Y',
  3031. 'created' => '2007-03-18 10:43:23',
  3032. 'updated' => '2007-03-18 10:45:31'
  3033. )),
  3034. array(
  3035. 'Post' => array(
  3036. 'id' => '4',
  3037. 'author_id' => '2',
  3038. 'title' => 'Creating a fourth post',
  3039. 'body' => 'Fourth post body',
  3040. 'published' => 'N',
  3041. 'created' => $ts,
  3042. 'updated' => $ts
  3043. )));
  3044. $this->assertEqual($expected, $result);
  3045. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  3046. $data = array(
  3047. array(
  3048. 'id' => '1',
  3049. 'title' => 'Un-Baleeted First Post',
  3050. 'body' => 'Not Baleeted!',
  3051. 'published' => 'Y'
  3052. ),
  3053. array(
  3054. 'id' => '2',
  3055. 'title' => '',
  3056. 'body' => 'Trying to get away with an empty title'
  3057. ));
  3058. $result = $TestModel->saveAll($data);
  3059. $this->assertFalse($result);
  3060. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3061. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  3062. $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
  3063. if (!$transactionWorked) {
  3064. $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
  3065. $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
  3066. }
  3067. $this->assertEqual($TestModel->validationErrors, $errors);
  3068. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  3069. $data = array(
  3070. array(
  3071. 'id' => '1',
  3072. 'title' => 'Un-Baleeted First Post',
  3073. 'body' => 'Not Baleeted!',
  3074. 'published' => 'Y'
  3075. ),
  3076. array(
  3077. 'id' => '2',
  3078. 'title' => '',
  3079. 'body' => 'Trying to get away with an empty title'
  3080. ));
  3081. $result = $TestModel->saveAll($data, array('validate' => true, 'atomic' => false));
  3082. $this->assertEqual($result, array(true, false));
  3083. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3084. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  3085. $newTs = date('Y-m-d H:i:s');
  3086. $expected = array(
  3087. array(
  3088. 'Post' => array(
  3089. 'id' => '1',
  3090. 'author_id' => '1',
  3091. 'title' => 'Un-Baleeted First Post',
  3092. 'body' => 'Not Baleeted!',
  3093. 'published' => 'Y',
  3094. 'created' => '2007-03-18 10:39:23',
  3095. 'updated' => $newTs
  3096. )),
  3097. array(
  3098. 'Post' => array(
  3099. 'id' => '2',
  3100. 'author_id' => '3',
  3101. 'title' => 'Just update the title',
  3102. 'body' => 'Second Post Body',
  3103. 'published' => 'Y',
  3104. 'created' => '2007-03-18 10:41:23',
  3105. 'updated' => $ts
  3106. )),
  3107. array(
  3108. 'Post' => array(
  3109. 'id' => '3',
  3110. 'author_id' => '1',
  3111. 'title' => 'Third Post',
  3112. 'body' => 'Third Post Body',
  3113. 'published' => 'Y',
  3114. 'created' => '2007-03-18 10:43:23',
  3115. 'updated' => '2007-03-18 10:45:31'
  3116. )),
  3117. array(
  3118. 'Post' => array(
  3119. 'id' => '4',
  3120. 'author_id' => '2',
  3121. 'title' => 'Creating a fourth post',
  3122. 'body' => 'Fourth post body',
  3123. 'published' => 'N',
  3124. 'created' => $ts,
  3125. 'updated' => $ts
  3126. )));
  3127. $this->assertEqual($expected, $result);
  3128. $this->assertEqual($TestModel->validationErrors, $errors);
  3129. $data = array(
  3130. array(
  3131. 'id' => '1',
  3132. 'title' => 'Re-Baleeted First Post',
  3133. 'body' => 'Baleeted!',
  3134. 'published' => 'N'
  3135. ),
  3136. array(
  3137. 'id' => '2',
  3138. 'title' => '',
  3139. 'body' => 'Trying to get away with an empty title'
  3140. ));
  3141. $this->assertFalse($TestModel->saveAll($data, array('validate' => 'first')));
  3142. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3143. $this->assertEqual($expected, $result);
  3144. $this->assertEqual($TestModel->validationErrors, $errors);
  3145. }
  3146. /**
  3147. * testSaveAllValidationOnly method
  3148. *
  3149. * @return void
  3150. */
  3151. public function testSaveAllValidationOnly() {
  3152. $this->loadFixtures('Comment', 'Attachment');
  3153. $TestModel = new Comment();
  3154. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  3155. $data = array(
  3156. 'Comment' => array(
  3157. 'comment' => 'This is the comment'
  3158. ),
  3159. 'Attachment' => array(
  3160. 'attachment' => ''
  3161. )
  3162. );
  3163. $result = $TestModel->saveAll($data, array('validate' => 'only'));
  3164. $this->assertFalse($result);
  3165. $TestModel = new Article();
  3166. $TestModel->validate = array('title' => 'notEmpty');
  3167. $result = $TestModel->saveAll(
  3168. array(
  3169. 0 => array('title' => ''),
  3170. 1 => array('title' => 'title 1'),
  3171. 2 => array('title' => 'title 2'),
  3172. ),
  3173. array('validate'=>'only')
  3174. );
  3175. $this->assertFalse($result);
  3176. $expected = array(
  3177. 0 => array('title' => array('This field cannot be left blank')),
  3178. );
  3179. $this->assertEqual($TestModel->validationErrors, $expected);
  3180. $result = $TestModel->saveAll(
  3181. array(
  3182. 0 => array('title' => 'title 0'),
  3183. 1 => array('title' => ''),
  3184. 2 => array('title' => 'title 2'),
  3185. ),
  3186. array('validate'=>'only')
  3187. );
  3188. $this->assertFalse($result);
  3189. $expected = array(
  3190. 1 => array('title' => array('This field cannot be left blank')),
  3191. );
  3192. $this->assertEqual($TestModel->validationErrors, $expected);
  3193. }
  3194. /**
  3195. * testSaveAllValidateFirst method
  3196. *
  3197. * @return void
  3198. */
  3199. public function testSaveAllValidateFirst() {
  3200. $this->loadFixtures('Article', 'Comment', 'Attachment');
  3201. $model = new Article();
  3202. $model->deleteAll(true);
  3203. $model->Comment->validate = array('comment' => 'notEmpty');
  3204. $result = $model->saveAll(array(
  3205. 'Article' => array(
  3206. 'title' => 'Post with Author',
  3207. 'body' => 'This post will be saved author'
  3208. ),
  3209. 'Comment' => array(
  3210. array('comment' => 'First new comment'),
  3211. array('comment' => '')
  3212. )
  3213. ), array('validate' => 'first'));
  3214. $this->assertFalse($result);
  3215. $result = $model->find('all');
  3216. $this->assertEqual($result, array());
  3217. $expected = array('Comment' => array(
  3218. 1 => array('comment' => array('This field cannot be left blank'))
  3219. ));
  3220. $this->assertEqual($model->Comment->validationErrors, $expected['Comment']);
  3221. $this->assertIdentical($model->Comment->find('count'), 0);
  3222. $result = $model->saveAll(
  3223. array(
  3224. 'Article' => array(
  3225. 'title' => 'Post with Author',
  3226. 'body' => 'This post will be saved with an author',
  3227. 'user_id' => 2
  3228. ),
  3229. 'Comment' => array(
  3230. array(
  3231. 'comment' => 'Only new comment',
  3232. 'user_id' => 2
  3233. ))),
  3234. array('validate' => 'first')
  3235. );
  3236. $this->assertIdentical($result, true);
  3237. $result = $model->Comment->find('all');
  3238. $this->assertIdentical(count($result), 1);
  3239. $result = Set::extract('/Comment/article_id', $result);
  3240. $this->assertEquals($result[0], 4);
  3241. $model->deleteAll(true);
  3242. $data = array(
  3243. 'Article' => array(
  3244. 'title' => 'Post with Author saveAlled from comment',
  3245. 'body' => 'This post will be saved with an author',
  3246. 'user_id' => 2
  3247. ),
  3248. 'Comment' => array(
  3249. 'comment' => 'Only new comment', 'user_id' => 2
  3250. ));
  3251. $result = $model->Comment->saveAll($data, array('validate' => 'first'));
  3252. $this->assertFalse(empty($result));
  3253. $result = $model->find('all');
  3254. $this->assertEqual(
  3255. $result[0]['Article']['title'],
  3256. 'Post with Author saveAlled from comment'
  3257. );
  3258. $this->assertEqual($result[0]['Comment'][0]['comment'], 'Only new comment');
  3259. }
  3260. /**
  3261. * test saveAll()'s return is correct when using atomic = false and validate = first.
  3262. *
  3263. * @return void
  3264. */
  3265. public function testSaveAllValidateFirstAtomicFalse() {
  3266. $Something = new Something();
  3267. $invalidData = array(
  3268. array(
  3269. 'title' => 'foo',
  3270. 'body' => 'bar',
  3271. 'published' => 'baz',
  3272. ),
  3273. array(
  3274. 'body' => 3,
  3275. 'published' =>'sd',
  3276. ),
  3277. );
  3278. $Something->create();
  3279. $Something->validate = array(
  3280. 'title' => array(
  3281. 'rule' => 'alphaNumeric',
  3282. 'required' => true,
  3283. ),
  3284. 'body' => array(
  3285. 'rule' => 'alphaNumeric',
  3286. 'required' => true,
  3287. 'allowEmpty' => true,
  3288. ),
  3289. );
  3290. $result = $Something->saveAll($invalidData, array(
  3291. 'atomic' => false,
  3292. 'validate' => 'first',
  3293. ));
  3294. $expected = array(true, false);
  3295. $this->assertEqual($expected, $result);
  3296. $Something = new Something();
  3297. $validData = array(
  3298. array(
  3299. 'title' => 'title value',
  3300. 'body' => 'body value',
  3301. 'published' => 'baz',
  3302. ),
  3303. array(
  3304. 'title' => 'valid',
  3305. 'body' => 'this body',
  3306. 'published' =>'sd',
  3307. ),
  3308. );
  3309. $Something->create();
  3310. $result = $Something->saveAll($validData, array(
  3311. 'atomic' => false,
  3312. 'validate' => 'first',
  3313. ));
  3314. $expected = array(true, true);
  3315. $this->assertEqual($expected, $result);
  3316. }
  3317. /**
  3318. * testSaveAllHasManyValidationOnly method
  3319. *
  3320. * @return void
  3321. */
  3322. public function testSaveAllHasManyValidationOnly() {
  3323. $this->loadFixtures('Article', 'Comment', 'Attachment');
  3324. $TestModel = new Article();
  3325. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  3326. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  3327. $result = $TestModel->saveAll(
  3328. array(
  3329. 'Article' => array('id' => 2),
  3330. 'Comment' => array(
  3331. array(
  3332. 'id' => 1,
  3333. 'comment' => '',
  3334. 'published' => 'Y',
  3335. 'user_id' => 1),
  3336. array(
  3337. 'id' => 2,
  3338. 'comment' =>
  3339. 'comment',
  3340. 'published' => 'Y',
  3341. 'user_id' => 1
  3342. ))),
  3343. array('validate' => 'only')
  3344. );
  3345. $this->assertFalse($result);
  3346. $result = $TestModel->saveAll(
  3347. array(
  3348. 'Article' => array('id' => 2),
  3349. 'Comment' => array(
  3350. array(
  3351. 'id' => 1,
  3352. 'comment' => '',
  3353. 'published' => 'Y',
  3354. 'user_id' => 1
  3355. ),
  3356. array(
  3357. 'id' => 2,
  3358. 'comment' => 'comment',
  3359. 'published' => 'Y',
  3360. 'user_id' => 1
  3361. ),
  3362. array(
  3363. 'id' => 3,
  3364. 'comment' => '',
  3365. 'published' => 'Y',
  3366. 'user_id' => 1
  3367. ))),
  3368. array(
  3369. 'validate' => 'only',
  3370. 'atomic' => false
  3371. ));
  3372. $expected = array(
  3373. 'Article' => true,
  3374. 'Comment' => array(false, true, false)
  3375. );
  3376. $this->assertIdentical($expected, $result);
  3377. $expected = array('Comment' => array(
  3378. 0 => array('comment' => array('This field cannot be left blank')),
  3379. 2 => array('comment' => array('This field cannot be left blank'))
  3380. ));
  3381. $this->assertEqual($TestModel->validationErrors, $expected);
  3382. $expected = array(
  3383. 0 => array('comment' => array('This field cannot be left blank')),
  3384. 2 => array('comment' => array('This field cannot be left blank'))
  3385. );
  3386. $this->assertEqual($TestModel->Comment->validationErrors, $expected);
  3387. }
  3388. /**
  3389. * test that saveAll behaves like plain save() when suplied empty data
  3390. *
  3391. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  3392. * @return void
  3393. */
  3394. public function testSaveAllEmptyData() {
  3395. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  3396. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  3397. $model = new Article();
  3398. $result = $model->saveAll(array(), array('validate' => 'first'));
  3399. $this->assertFalse(empty($result));
  3400. $model = new ProductUpdateAll();
  3401. $result = $model->saveAll(array());
  3402. $this->assertFalse($result);
  3403. }
  3404. /**
  3405. * testSaveAssociated method
  3406. *
  3407. * @return void
  3408. */
  3409. public function testSaveAssociated() {
  3410. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  3411. $TestModel = new Post();
  3412. $result = $TestModel->find('all');
  3413. $this->assertEqual(count($result), 3);
  3414. $this->assertFalse(isset($result[3]));
  3415. $ts = date('Y-m-d H:i:s');
  3416. $TestModel->saveAssociated(array(
  3417. 'Post' => array(
  3418. 'title' => 'Post with Author',
  3419. 'body' => 'This post will be saved with an author'
  3420. ),
  3421. 'Author' => array(
  3422. 'user' => 'bob',
  3423. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
  3424. )));
  3425. $result = $TestModel->find('all');
  3426. $expected = array(
  3427. 'Post' => array(
  3428. 'id' => '4',
  3429. 'author_id' => '5',
  3430. 'title' => 'Post with Author',
  3431. 'body' => 'This post will be saved with an author',
  3432. 'published' => 'N',
  3433. 'created' => $ts,
  3434. 'updated' => $ts
  3435. ),
  3436. 'Author' => array(
  3437. 'id' => '5',
  3438. 'user' => 'bob',
  3439. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
  3440. 'created' => $ts,
  3441. 'updated' => $ts,
  3442. 'test' => 'working'
  3443. ));
  3444. $this->assertEqual($result[3], $expected);
  3445. $this->assertEqual(count($result), 4);
  3446. $ts = date('Y-m-d H:i:s');
  3447. $TestModel = new Comment();
  3448. $ts = date('Y-m-d H:i:s');
  3449. $result = $TestModel->saveAssociated(array(
  3450. 'Comment' => array(
  3451. 'article_id' => 2,
  3452. 'user_id' => 2,
  3453. 'comment' => 'New comment with attachment',
  3454. 'published' => 'Y'
  3455. ),
  3456. 'Attachment' => array(
  3457. 'attachment' => 'some_file.tgz'
  3458. )));
  3459. $this->assertFalse(empty($result));
  3460. $result = $TestModel->find('all');
  3461. $expected = array(
  3462. 'id' => '7',
  3463. 'article_id' => '2',
  3464. 'user_id' => '2',
  3465. 'comment' => 'New comment with attachment',
  3466. 'published' => 'Y',
  3467. 'created' => $ts,
  3468. 'updated' => $ts
  3469. );
  3470. $this->assertEqual($result[6]['Comment'], $expected);
  3471. $expected = array(
  3472. 'id' => '7',
  3473. 'article_id' => '2',
  3474. 'user_id' => '2',
  3475. 'comment' => 'New comment with attachment',
  3476. 'published' => 'Y',
  3477. 'created' => $ts,
  3478. 'updated' => $ts
  3479. );
  3480. $this->assertEqual($result[6]['Comment'], $expected);
  3481. $expected = array(
  3482. 'id' => '2',
  3483. 'comment_id' => '7',
  3484. 'attachment' => 'some_file.tgz',
  3485. 'created' => $ts,
  3486. 'updated' => $ts
  3487. );
  3488. $this->assertEqual($result[6]['Attachment'], $expected);
  3489. }
  3490. /**
  3491. * testSaveMany method
  3492. *
  3493. * @return void
  3494. */
  3495. public function testSaveMany() {
  3496. $this->loadFixtures('Post');
  3497. $TestModel = new Post();
  3498. $TestModel->deleteAll(true);
  3499. $this->assertEqual($TestModel->find('all'), array());
  3500. // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
  3501. $this->db->truncate($TestModel);
  3502. $ts = date('Y-m-d H:i:s');
  3503. $TestModel->saveMany(array(
  3504. array(
  3505. 'title' => 'Multi-record post 1',
  3506. 'body' => 'First multi-record post',
  3507. 'author_id' => 2
  3508. ),
  3509. array(
  3510. 'title' => 'Multi-record post 2',
  3511. 'body' => 'Second multi-record post',
  3512. 'author_id' => 2
  3513. )));
  3514. $result = $TestModel->find('all', array(
  3515. 'recursive' => -1,
  3516. 'order' => 'Post.id ASC'
  3517. ));
  3518. $expected = array(
  3519. array(
  3520. 'Post' => array(
  3521. 'id' => '1',
  3522. 'author_id' => '2',
  3523. 'title' => 'Multi-record post 1',
  3524. 'body' => 'First multi-record post',
  3525. 'published' => 'N',
  3526. 'created' => $ts,
  3527. 'updated' => $ts
  3528. )),
  3529. array(
  3530. 'Post' => array(
  3531. 'id' => '2',
  3532. 'author_id' => '2',
  3533. 'title' => 'Multi-record post 2',
  3534. 'body' => 'Second multi-record post',
  3535. 'published' => 'N',
  3536. 'created' => $ts,
  3537. 'updated' => $ts
  3538. )));
  3539. $this->assertEqual($expected, $result);
  3540. }
  3541. /**
  3542. * Test SaveAssociated with Habtm relations
  3543. *
  3544. * @return void
  3545. */
  3546. public function testSaveAssociatedHabtm() {
  3547. $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  3548. $data = array(
  3549. 'Article' => array(
  3550. 'user_id' => 1,
  3551. 'title' => 'Article Has and belongs to Many Tags'
  3552. ),
  3553. 'Tag' => array(
  3554. 'Tag' => array(1, 2)
  3555. ),
  3556. 'Comment' => array(
  3557. array(
  3558. 'comment' => 'Article comment',
  3559. 'user_id' => 1
  3560. )));
  3561. $Article = new Article();
  3562. $result = $Article->saveAssociated($data);
  3563. $this->assertFalse(empty($result));
  3564. $result = $Article->read();
  3565. $this->assertEqual(count($result['Tag']), 2);
  3566. $this->assertEqual($result['Tag'][0]['tag'], 'tag1');
  3567. $this->assertEqual(count($result['Comment']), 1);
  3568. $this->assertEqual(count($result['Comment'][0]['comment']['Article comment']), 1);
  3569. }
  3570. /**
  3571. * Test SaveAssociated with Habtm relations and extra join table fields
  3572. *
  3573. * @return void
  3574. */
  3575. public function testSaveAssociatedHabtmWithExtraJoinTableFields() {
  3576. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  3577. $data = array(
  3578. 'Something' => array(
  3579. 'id' => 4,
  3580. 'title' => 'Extra Fields',
  3581. 'body' => 'Extra Fields Body',
  3582. 'published' => '1'
  3583. ),
  3584. 'SomethingElse' => array(
  3585. array('something_else_id' => 1, 'doomed' => '1'),
  3586. array('something_else_id' => 2, 'doomed' => '0'),
  3587. array('something_else_id' => 3, 'doomed' => '1')
  3588. )
  3589. );
  3590. $Something = new Something();
  3591. $result = $Something->saveAssociated($data);
  3592. $this->assertFalse(empty($result));
  3593. $result = $Something->read();
  3594. $this->assertEqual(count($result['SomethingElse']), 3);
  3595. $this->assertTrue(Set::matches('/Something[id=4]', $result));
  3596. $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
  3597. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
  3598. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
  3599. $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
  3600. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
  3601. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
  3602. $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
  3603. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
  3604. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
  3605. }
  3606. /**
  3607. * testSaveAssociatedHasOne method
  3608. *
  3609. * @return void
  3610. */
  3611. public function testSaveAssociatedHasOne() {
  3612. $model = new Comment();
  3613. $model->deleteAll(true);
  3614. $this->assertEqual($model->find('all'), array());
  3615. $model->Attachment->deleteAll(true);
  3616. $this->assertEqual($model->Attachment->find('all'), array());
  3617. $this->assertTrue($model->saveAssociated(array(
  3618. 'Comment' => array(
  3619. 'comment' => 'Comment with attachment',
  3620. 'article_id' => 1,
  3621. 'user_id' => 1
  3622. ),
  3623. 'Attachment' => array(
  3624. 'attachment' => 'some_file.zip'
  3625. ))));
  3626. $result = $model->find('all', array('fields' => array(
  3627. 'Comment.id', 'Comment.comment', 'Attachment.id',
  3628. 'Attachment.comment_id', 'Attachment.attachment'
  3629. )));
  3630. $expected = array(array(
  3631. 'Comment' => array(
  3632. 'id' => '1',
  3633. 'comment' => 'Comment with attachment'
  3634. ),
  3635. 'Attachment' => array(
  3636. 'id' => '1',
  3637. 'comment_id' => '1',
  3638. 'attachment' => 'some_file.zip'
  3639. )));
  3640. $this->assertEqual($expected, $result);
  3641. $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
  3642. $data = array(
  3643. 'Comment' => array(
  3644. 'comment' => 'Comment with attachment',
  3645. 'article_id' => 1,
  3646. 'user_id' => 1
  3647. ),
  3648. 'Attachment' => array(
  3649. 'attachment' => 'some_file.zip'
  3650. ));
  3651. $this->assertTrue($model->saveAssociated($data, array('validate' => 'first')));
  3652. }
  3653. /**
  3654. * testSaveAssociatedBelongsTo method
  3655. *
  3656. * @return void
  3657. */
  3658. public function testSaveAssociatedBelongsTo() {
  3659. $model = new Comment();
  3660. $model->deleteAll(true);
  3661. $this->assertEqual($model->find('all'), array());
  3662. $model->Article->deleteAll(true);
  3663. $this->assertEqual($model->Article->find('all'), array());
  3664. $this->assertTrue($model->saveAssociated(array(
  3665. 'Comment' => array(
  3666. 'comment' => 'Article comment',
  3667. 'article_id' => 1,
  3668. 'user_id' => 1
  3669. ),
  3670. 'Article' => array(
  3671. 'title' => 'Model Associations 101',
  3672. 'user_id' => 1
  3673. ))));
  3674. $result = $model->find('all', array('fields' => array(
  3675. 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
  3676. )));
  3677. $expected = array(array(
  3678. 'Comment' => array(
  3679. 'id' => '1',
  3680. 'article_id' => '1',
  3681. 'comment' => 'Article comment'
  3682. ),
  3683. 'Article' => array(
  3684. 'id' => '1',
  3685. 'title' => 'Model Associations 101'
  3686. )));
  3687. $this->assertEqual($expected, $result);
  3688. }
  3689. /**
  3690. * testSaveAssociatedHasOneValidation method
  3691. *
  3692. * @return void
  3693. */
  3694. public function testSaveAssociatedHasOneValidation() {
  3695. $model = new Comment();
  3696. $model->deleteAll(true);
  3697. $this->assertEqual($model->find('all'), array());
  3698. $model->Attachment->deleteAll(true);
  3699. $this->assertEqual($model->Attachment->find('all'), array());
  3700. $model->validate = array('comment' => 'notEmpty');
  3701. $model->Attachment->validate = array('attachment' => 'notEmpty');
  3702. $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
  3703. $this->assertEquals($model->saveAssociated(
  3704. array(
  3705. 'Comment' => array(
  3706. 'comment' => '',
  3707. 'article_id' => 1,
  3708. 'user_id' => 1
  3709. ),
  3710. 'Attachment' => array('attachment' => '')
  3711. ),
  3712. array('validate' => 'first')
  3713. ), false);
  3714. $expected = array(
  3715. 'Comment' => array('comment' => array('This field cannot be left blank')),
  3716. 'Attachment' => array('attachment' => array('This field cannot be left blank'))
  3717. );
  3718. $this->assertEqual($model->validationErrors, $expected['Comment']);
  3719. $this->assertEqual($model->Attachment->validationErrors, $expected['Attachment']);
  3720. }
  3721. /**
  3722. * testSaveAssociatedAtomic method
  3723. *
  3724. * @return void
  3725. */
  3726. public function testSaveAssociatedAtomic() {
  3727. $this->loadFixtures('Article', 'User');
  3728. $TestModel = new Article();
  3729. $result = $TestModel->saveAssociated(array(
  3730. 'Article' => array(
  3731. 'title' => 'Post with Author',
  3732. 'body' => 'This post will be saved with an author',
  3733. 'user_id' => 2
  3734. ),
  3735. 'Comment' => array(
  3736. array('comment' => 'First new comment', 'user_id' => 2))
  3737. ), array('atomic' => false));
  3738. $this->assertIdentical($result, array('Article' => true, 'Comment' => array(true)));
  3739. $result = $TestModel->saveAssociated(array(
  3740. 'Article' => array('id' => 2),
  3741. 'Comment' => array(
  3742. array(
  3743. 'comment' => 'First new comment',
  3744. 'published' => 'Y',
  3745. 'user_id' => 1
  3746. ),
  3747. array(
  3748. 'comment' => 'Second new comment',
  3749. 'published' => 'Y',
  3750. 'user_id' => 2
  3751. ))
  3752. ), array('validate' => true, 'atomic' => false));
  3753. $this->assertIdentical($result, array('Article' => true, 'Comment' => array(true, true)));
  3754. }
  3755. /**
  3756. * testSaveManyAtomic method
  3757. *
  3758. * @return void
  3759. */
  3760. public function testSaveManyAtomic() {
  3761. $this->loadFixtures('Article', 'User');
  3762. $TestModel = new Article();
  3763. $result = $TestModel->saveMany(array(
  3764. array(
  3765. 'id' => '1',
  3766. 'title' => 'Baleeted First Post',
  3767. 'body' => 'Baleeted!',
  3768. 'published' => 'N'
  3769. ),
  3770. array(
  3771. 'id' => '2',
  3772. 'title' => 'Just update the title'
  3773. ),
  3774. array(
  3775. 'title' => 'Creating a fourth post',
  3776. 'body' => 'Fourth post body',
  3777. 'user_id' => 2
  3778. )
  3779. ), array('atomic' => false));
  3780. $this->assertIdentical($result, array(true, true, true));
  3781. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  3782. $result = $TestModel->saveMany(array(
  3783. array(
  3784. 'id' => '1',
  3785. 'title' => 'Un-Baleeted First Post',
  3786. 'body' => 'Not Baleeted!',
  3787. 'published' => 'Y'
  3788. ),
  3789. array(
  3790. 'id' => '2',
  3791. 'title' => '',
  3792. 'body' => 'Trying to get away with an empty title'
  3793. )
  3794. ), array('validate' => true, 'atomic' => false));
  3795. $this->assertIdentical($result, array(true, false));
  3796. }
  3797. /**
  3798. * testSaveAssociatedHasMany method
  3799. *
  3800. * @return void
  3801. */
  3802. public function testSaveAssociatedHasMany() {
  3803. $this->loadFixtures('Article', 'Comment');
  3804. $TestModel = new Article();
  3805. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  3806. $result = $TestModel->saveAssociated(array(
  3807. 'Article' => array('id' => 2),
  3808. 'Comment' => array(
  3809. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  3810. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3811. )
  3812. ));
  3813. $this->assertFalse(empty($result));
  3814. $result = $TestModel->findById(2);
  3815. $expected = array(
  3816. 'First Comment for Second Article',
  3817. 'Second Comment for Second Article',
  3818. 'First new comment',
  3819. 'Second new comment'
  3820. );
  3821. $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected);
  3822. $result = $TestModel->saveAssociated(
  3823. array(
  3824. 'Article' => array('id' => 2),
  3825. 'Comment' => array(
  3826. array(
  3827. 'comment' => 'Third new comment',
  3828. 'published' => 'Y',
  3829. 'user_id' => 1
  3830. ))),
  3831. array('atomic' => false)
  3832. );
  3833. $this->assertFalse(empty($result));
  3834. $result = $TestModel->findById(2);
  3835. $expected = array(
  3836. 'First Comment for Second Article',
  3837. 'Second Comment for Second Article',
  3838. 'First new comment',
  3839. 'Second new comment',
  3840. 'Third new comment'
  3841. );
  3842. $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected);
  3843. $TestModel->beforeSaveReturn = false;
  3844. $result = $TestModel->saveAssociated(
  3845. array(
  3846. 'Article' => array('id' => 2),
  3847. 'Comment' => array(
  3848. array(
  3849. 'comment' => 'Fourth new comment',
  3850. 'published' => 'Y',
  3851. 'user_id' => 1
  3852. ))),
  3853. array('atomic' => false)
  3854. );
  3855. $this->assertEqual($result, array('Article' => false));
  3856. $result = $TestModel->findById(2);
  3857. $expected = array(
  3858. 'First Comment for Second Article',
  3859. 'Second Comment for Second Article',
  3860. 'First new comment',
  3861. 'Second new comment',
  3862. 'Third new comment'
  3863. );
  3864. $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected);
  3865. }
  3866. /**
  3867. * testSaveAssociatedHasManyValidation method
  3868. *
  3869. * @return void
  3870. */
  3871. public function testSaveAssociatedHasManyValidation() {
  3872. $this->loadFixtures('Article', 'Comment');
  3873. $TestModel = new Article();
  3874. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  3875. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  3876. $result = $TestModel->saveAssociated(array(
  3877. 'Article' => array('id' => 2),
  3878. 'Comment' => array(
  3879. array('comment' => '', 'published' => 'Y', 'user_id' => 1),
  3880. )
  3881. ), array('validate' => true));
  3882. $this->assertFalse($result);
  3883. $expected = array('Comment' => array(
  3884. array('comment' => array('This field cannot be left blank'))
  3885. ));
  3886. $this->assertEqual($TestModel->validationErrors, $expected);
  3887. $expected = array(
  3888. array('comment' => array('This field cannot be left blank'))
  3889. );
  3890. $this->assertEqual($TestModel->Comment->validationErrors, $expected);
  3891. $result = $TestModel->saveAssociated(array(
  3892. 'Article' => array('id' => 2),
  3893. 'Comment' => array(
  3894. array(
  3895. 'comment' => '',
  3896. 'published' => 'Y',
  3897. 'user_id' => 1
  3898. ))
  3899. ), array('validate' => 'first'));
  3900. $this->assertFalse($result);
  3901. }
  3902. /**
  3903. * test saveMany with transactions and ensure there is no missing rollback.
  3904. *
  3905. * @return void
  3906. */
  3907. public function testSaveManyTransactionNoRollback() {
  3908. $this->loadFixtures('Post');
  3909. $this->getMock('DboSource', array('connect', 'rollback', 'describe'), array(), 'MockManyTransactionDboSource');
  3910. $db = ConnectionManager::create('mock_many_transaction', array(
  3911. 'datasource' => 'MockManyTransactionDboSource',
  3912. ));
  3913. $db->expects($this->once())
  3914. ->method('describe')
  3915. ->will($this->returnValue(array()));
  3916. $db->expects($this->once())->method('rollback');
  3917. $Post = new Post('mock_many_transaction');
  3918. $Post->validate = array(
  3919. 'title' => array('rule' => array('notEmpty'))
  3920. );
  3921. $data = array(
  3922. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3923. array('author_id' => 1, 'title' => '')
  3924. );
  3925. $Post->saveMany($data);
  3926. }
  3927. /**
  3928. * test saveAssociated with transactions and ensure there is no missing rollback.
  3929. *
  3930. * @return void
  3931. */
  3932. public function testSaveAssociatedTransactionNoRollback() {
  3933. $testDb = ConnectionManager::getDataSource('test');
  3934. $mock = $this->getMock(
  3935. 'DboSource',
  3936. array('connect', 'rollback', 'describe', 'create', 'begin'),
  3937. array(),
  3938. 'MockAssociatedTransactionDboSource',
  3939. false
  3940. );
  3941. $db = ConnectionManager::create('mock_assoc_transaction', array(
  3942. 'datasource' => 'MockAssociatedTransactionDboSource',
  3943. ));
  3944. $this->mockObjects[] = $db;
  3945. $db->columns = $testDb->columns;
  3946. $db->expects($this->once())->method('rollback');
  3947. $db->expects($this->any())->method('describe')
  3948. ->will($this->returnValue(array(
  3949. 'id' => array('type' => 'integer'),
  3950. 'title' => array('type' => 'string'),
  3951. 'body' => array('type' => 'text'),
  3952. 'published' => array('type' => 'string')
  3953. )));
  3954. $Post = new Post();
  3955. $Post->useDbConfig = 'mock_assoc_transaction';
  3956. $Post->Author->useDbConfig = 'mock_assoc_transaction';
  3957. $Post->Author->validate = array(
  3958. 'user' => array('rule' => array('notEmpty'))
  3959. );
  3960. $data = array(
  3961. 'Post' => array(
  3962. 'title' => 'New post',
  3963. 'body' => 'Content',
  3964. 'published' => 'Y'
  3965. ),
  3966. 'Author' => array(
  3967. 'user' => '',
  3968. 'password' => "sekret"
  3969. )
  3970. );
  3971. $Post->saveAssociated($data, array('validate' => true, 'atomic' => true));
  3972. }
  3973. /**
  3974. * test saveMany with nested saveMany call.
  3975. *
  3976. * @return void
  3977. */
  3978. public function testSaveManyNestedSaveMany() {
  3979. $this->loadFixtures('Sample');
  3980. $TransactionManyTestModel = new TransactionManyTestModel();
  3981. $data = array(
  3982. array('apple_id' => 1, 'name' => 'sample5'),
  3983. );
  3984. $this->assertTrue($TransactionManyTestModel->saveMany($data, array('atomic' => true)));
  3985. }
  3986. /**
  3987. * testSaveManyTransaction method
  3988. *
  3989. * @return void
  3990. */
  3991. public function testSaveManyTransaction() {
  3992. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  3993. $TestModel = new Post();
  3994. $TestModel->validate = array('title' => 'notEmpty');
  3995. $data = array(
  3996. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3997. array('author_id' => 1, 'title' => 'New Fifth Post'),
  3998. array('author_id' => 1, 'title' => '')
  3999. );
  4000. $ts = date('Y-m-d H:i:s');
  4001. $this->assertFalse($TestModel->saveMany($data));
  4002. $result = $TestModel->find('all', array('recursive' => -1));
  4003. $expected = array(
  4004. array('Post' => array(
  4005. 'id' => '1',
  4006. 'author_id' => 1,
  4007. 'title' => 'First Post',
  4008. 'body' => 'First Post Body',
  4009. 'published' => 'Y',
  4010. 'created' => '2007-03-18 10:39:23',
  4011. 'updated' => '2007-03-18 10:41:31'
  4012. )),
  4013. array('Post' => array(
  4014. 'id' => '2',
  4015. 'author_id' => 3,
  4016. 'title' => 'Second Post',
  4017. 'body' => 'Second Post Body',
  4018. 'published' => 'Y',
  4019. 'created' => '2007-03-18 10:41:23',
  4020. 'updated' => '2007-03-18 10:43:31'
  4021. )),
  4022. array('Post' => array(
  4023. 'id' => '3',
  4024. 'author_id' => 1,
  4025. 'title' => 'Third Post',
  4026. 'body' => 'Third Post Body',
  4027. 'published' => 'Y',
  4028. 'created' => '2007-03-18 10:43:23',
  4029. 'updated' => '2007-03-18 10:45:31'
  4030. )));
  4031. if (count($result) != 3) {
  4032. // Database doesn't support transactions
  4033. $expected[] = array(
  4034. 'Post' => array(
  4035. 'id' => '4',
  4036. 'author_id' => 1,
  4037. 'title' => 'New Fourth Post',
  4038. 'body' => null,
  4039. 'published' => 'N',
  4040. 'created' => $ts,
  4041. 'updated' => $ts
  4042. ));
  4043. $expected[] = array(
  4044. 'Post' => array(
  4045. 'id' => '5',
  4046. 'author_id' => 1,
  4047. 'title' => 'New Fifth Post',
  4048. 'body' => null,
  4049. 'published' => 'N',
  4050. 'created' => $ts,
  4051. 'updated' => $ts
  4052. ));
  4053. $this->assertEqual($expected, $result);
  4054. // Skip the rest of the transactional tests
  4055. return;
  4056. }
  4057. $this->assertEqual($expected, $result);
  4058. $data = array(
  4059. array('author_id' => 1, 'title' => 'New Fourth Post'),
  4060. array('author_id' => 1, 'title' => ''),
  4061. array('author_id' => 1, 'title' => 'New Sixth Post')
  4062. );
  4063. $ts = date('Y-m-d H:i:s');
  4064. $this->assertFalse($TestModel->saveMany($data));
  4065. $result = $TestModel->find('all', array('recursive' => -1));
  4066. $expected = array(
  4067. array('Post' => array(
  4068. 'id' => '1',
  4069. 'author_id' => 1,
  4070. 'title' => 'First Post',
  4071. 'body' => 'First Post Body',
  4072. 'published' => 'Y',
  4073. 'created' => '2007-03-18 10:39:23',
  4074. 'updated' => '2007-03-18 10:41:31'
  4075. )),
  4076. array('Post' => array(
  4077. 'id' => '2',
  4078. 'author_id' => 3,
  4079. 'title' => 'Second Post',
  4080. 'body' => 'Second Post Body',
  4081. 'published' => 'Y',
  4082. 'created' => '2007-03-18 10:41:23',
  4083. 'updated' => '2007-03-18 10:43:31'
  4084. )),
  4085. array('Post' => array(
  4086. 'id' => '3',
  4087. 'author_id' => 1,
  4088. 'title' => 'Third Post',
  4089. 'body' => 'Third Post Body',
  4090. 'published' => 'Y',
  4091. 'created' => '2007-03-18 10:43:23',
  4092. 'updated' => '2007-03-18 10:45:31'
  4093. )));
  4094. if (count($result) != 3) {
  4095. // Database doesn't support transactions
  4096. $expected[] = array(
  4097. 'Post' => array(
  4098. 'id' => '4',
  4099. 'author_id' => 1,
  4100. 'title' => 'New Fourth Post',
  4101. 'body' => 'Third Post Body',
  4102. 'published' => 'N',
  4103. 'created' => $ts,
  4104. 'updated' => $ts
  4105. ));
  4106. $expected[] = array(
  4107. 'Post' => array(
  4108. 'id' => '5',
  4109. 'author_id' => 1,
  4110. 'title' => 'Third Post',
  4111. 'body' => 'Third Post Body',
  4112. 'published' => 'N',
  4113. 'created' => $ts,
  4114. 'updated' => $ts
  4115. ));
  4116. }
  4117. $this->assertEqual($expected, $result);
  4118. $TestModel->validate = array('title' => 'notEmpty');
  4119. $data = array(
  4120. array('author_id' => 1, 'title' => 'New Fourth Post'),
  4121. array('author_id' => 1, 'title' => 'New Fifth Post'),
  4122. array('author_id' => 1, 'title' => 'New Sixth Post')
  4123. );
  4124. $this->assertTrue($TestModel->saveMany($data));
  4125. $result = $TestModel->find('all', array(
  4126. 'recursive' => -1,
  4127. 'fields' => array('author_id', 'title','body','published')
  4128. ));
  4129. $expected = array(
  4130. array('Post' => array(
  4131. 'author_id' => 1,
  4132. 'title' => 'First Post',
  4133. 'body' => 'First Post Body',
  4134. 'published' => 'Y'
  4135. )),
  4136. array('Post' => array(
  4137. 'author_id' => 3,
  4138. 'title' => 'Second Post',
  4139. 'body' => 'Second Post Body',
  4140. 'published' => 'Y'
  4141. )),
  4142. array('Post' => array(
  4143. 'author_id' => 1,
  4144. 'title' => 'Third Post',
  4145. 'body' => 'Third Post Body',
  4146. 'published' => 'Y'
  4147. )),
  4148. array('Post' => array(
  4149. 'author_id' => 1,
  4150. 'title' => 'New Fourth Post',
  4151. 'body' => '',
  4152. 'published' => 'N'
  4153. )),
  4154. array('Post' => array(
  4155. 'author_id' => 1,
  4156. 'title' => 'New Fifth Post',
  4157. 'body' => '',
  4158. 'published' => 'N'
  4159. )),
  4160. array('Post' => array(
  4161. 'author_id' => 1,
  4162. 'title' => 'New Sixth Post',
  4163. 'body' => '',
  4164. 'published' => 'N'
  4165. )));
  4166. $this->assertEqual($expected, $result);
  4167. }
  4168. /**
  4169. * testSaveManyValidation method
  4170. *
  4171. * @return void
  4172. */
  4173. public function testSaveManyValidation() {
  4174. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  4175. $TestModel = new Post();
  4176. $data = array(
  4177. array(
  4178. 'id' => '1',
  4179. 'title' => 'Baleeted First Post',
  4180. 'body' => 'Baleeted!',
  4181. 'published' => 'N'
  4182. ),
  4183. array(
  4184. 'id' => '2',
  4185. 'title' => 'Just update the title'
  4186. ),
  4187. array(
  4188. 'title' => 'Creating a fourth post',
  4189. 'body' => 'Fourth post body',
  4190. 'author_id' => 2
  4191. ));
  4192. $this->assertTrue($TestModel->saveMany($data));
  4193. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  4194. $ts = date('Y-m-d H:i:s');
  4195. $expected = array(
  4196. array(
  4197. 'Post' => array(
  4198. 'id' => '1',
  4199. 'author_id' => '1',
  4200. 'title' => 'Baleeted First Post',
  4201. 'body' => 'Baleeted!',
  4202. 'published' => 'N',
  4203. 'created' => '2007-03-18 10:39:23',
  4204. 'updated' => $ts
  4205. )),
  4206. array(
  4207. 'Post' => array(
  4208. 'id' => '2',
  4209. 'author_id' => '3',
  4210. 'title' => 'Just update the title',
  4211. 'body' => 'Second Post Body',
  4212. 'published' => 'Y',
  4213. 'created' => '2007-03-18 10:41:23', 'updated' => $ts
  4214. )),
  4215. array(
  4216. 'Post' => array(
  4217. 'id' => '3',
  4218. 'author_id' => '1',
  4219. 'title' => 'Third Post',
  4220. 'body' => 'Third Post Body',
  4221. 'published' => 'Y',
  4222. 'created' => '2007-03-18 10:43:23',
  4223. 'updated' => '2007-03-18 10:45:31'
  4224. )),
  4225. array(
  4226. 'Post' => array(
  4227. 'id' => '4',
  4228. 'author_id' => '2',
  4229. 'title' => 'Creating a fourth post',
  4230. 'body' => 'Fourth post body',
  4231. 'published' => 'N',
  4232. 'created' => $ts,
  4233. 'updated' => $ts
  4234. )));
  4235. $this->assertEqual($expected, $result);
  4236. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  4237. $data = array(
  4238. array(
  4239. 'id' => '1',
  4240. 'title' => 'Un-Baleeted First Post',
  4241. 'body' => 'Not Baleeted!',
  4242. 'published' => 'Y'
  4243. ),
  4244. array(
  4245. 'id' => '2',
  4246. 'title' => '',
  4247. 'body' => 'Trying to get away with an empty title'
  4248. ));
  4249. $result = $TestModel->saveMany($data);
  4250. $this->assertFalse($result);
  4251. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  4252. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  4253. $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
  4254. if (!$transactionWorked) {
  4255. $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
  4256. $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
  4257. }
  4258. $this->assertEqual($TestModel->validationErrors, $errors);
  4259. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  4260. $data = array(
  4261. array(
  4262. 'id' => '1',
  4263. 'title' => 'Un-Baleeted First Post',
  4264. 'body' => 'Not Baleeted!',
  4265. 'published' => 'Y'
  4266. ),
  4267. array(
  4268. 'id' => '2',
  4269. 'title' => '',
  4270. 'body' => 'Trying to get away with an empty title'
  4271. ));
  4272. $result = $TestModel->saveMany($data, array('validate' => true, 'atomic' => false));
  4273. $this->assertEqual($result, array(true, false));
  4274. $result = $TestModel->find('all', array(
  4275. 'fields' => array('id', 'author_id', 'title', 'body', 'published'),
  4276. 'recursive' => -1,
  4277. 'order' => 'Post.id ASC'
  4278. ));
  4279. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  4280. $expected = array(
  4281. array(
  4282. 'Post' => array(
  4283. 'id' => '1',
  4284. 'author_id' => '1',
  4285. 'title' => 'Un-Baleeted First Post',
  4286. 'body' => 'Not Baleeted!',
  4287. 'published' => 'Y',
  4288. )),
  4289. array(
  4290. 'Post' => array(
  4291. 'id' => '2',
  4292. 'author_id' => '3',
  4293. 'title' => 'Just update the title',
  4294. 'body' => 'Second Post Body',
  4295. 'published' => 'Y',
  4296. )),
  4297. array(
  4298. 'Post' => array(
  4299. 'id' => '3',
  4300. 'author_id' => '1',
  4301. 'title' => 'Third Post',
  4302. 'body' => 'Third Post Body',
  4303. 'published' => 'Y',
  4304. )),
  4305. array(
  4306. 'Post' => array(
  4307. 'id' => '4',
  4308. 'author_id' => '2',
  4309. 'title' => 'Creating a fourth post',
  4310. 'body' => 'Fourth post body',
  4311. 'published' => 'N',
  4312. )));
  4313. $this->assertEqual($expected, $result);
  4314. $this->assertEqual($TestModel->validationErrors, $errors);
  4315. $data = array(
  4316. array(
  4317. 'id' => '1',
  4318. 'title' => 'Re-Baleeted First Post',
  4319. 'body' => 'Baleeted!',
  4320. 'published' => 'N'
  4321. ),
  4322. array(
  4323. 'id' => '2',
  4324. 'title' => '',
  4325. 'body' => 'Trying to get away with an empty title'
  4326. ));
  4327. $this->assertFalse($TestModel->saveMany($data, array('validate' => 'first')));
  4328. $result = $TestModel->find('all', array(
  4329. 'fields' => array('id', 'author_id', 'title', 'body', 'published'),
  4330. 'recursive' => -1,
  4331. 'order' => 'Post.id ASC'
  4332. ));
  4333. $this->assertEqual($expected, $result);
  4334. $this->assertEqual($TestModel->validationErrors, $errors);
  4335. }
  4336. /**
  4337. * testValidateMany method
  4338. *
  4339. * @return void
  4340. */
  4341. public function testValidateMany() {
  4342. $TestModel = new Article();
  4343. $TestModel->validate = array('title' => 'notEmpty');
  4344. $result = $TestModel->validateMany(
  4345. array(
  4346. 0 => array('title' => ''),
  4347. 1 => array('title' => 'title 1'),
  4348. 2 => array('title' => 'title 2'),
  4349. ));
  4350. $this->assertFalse($result);
  4351. $expected = array(
  4352. 0 => array('title' => array('This field cannot be left blank')),
  4353. );
  4354. $this->assertEqual($TestModel->validationErrors, $expected);
  4355. $result = $TestModel->validateMany(
  4356. array(
  4357. 0 => array('title' => 'title 0'),
  4358. 1 => array('title' => ''),
  4359. 2 => array('title' => 'title 2'),
  4360. ));
  4361. $this->assertFalse($result);
  4362. $expected = array(
  4363. 1 => array('title' => array('This field cannot be left blank')),
  4364. );
  4365. $this->assertEqual($TestModel->validationErrors, $expected);
  4366. }
  4367. /**
  4368. * testSaveAssociatedValidateFirst method
  4369. *
  4370. * @return void
  4371. */
  4372. public function testSaveAssociatedValidateFirst() {
  4373. $this->loadFixtures('Article', 'Comment', 'Attachment');
  4374. $model = new Article();
  4375. $model->deleteAll(true);
  4376. $model->Comment->validate = array('comment' => 'notEmpty');
  4377. $result = $model->saveAssociated(array(
  4378. 'Article' => array(
  4379. 'title' => 'Post with Author',
  4380. 'body' => 'This post will be saved author'
  4381. ),
  4382. 'Comment' => array(
  4383. array('comment' => 'First new comment'),
  4384. array('comment' => '')
  4385. )
  4386. ), array('validate' => 'first'));
  4387. $this->assertFalse($result);
  4388. $result = $model->find('all');
  4389. $this->assertEqual($result, array());
  4390. $expected = array('Comment' => array(
  4391. 1 => array('comment' => array('This field cannot be left blank'))
  4392. ));
  4393. $this->assertEqual($model->Comment->validationErrors, $expected['Comment']);
  4394. $this->assertIdentical($model->Comment->find('count'), 0);
  4395. $result = $model->saveAssociated(
  4396. array(
  4397. 'Article' => array(
  4398. 'title' => 'Post with Author',
  4399. 'body' => 'This post will be saved with an author',
  4400. 'user_id' => 2
  4401. ),
  4402. 'Comment' => array(
  4403. array(
  4404. 'comment' => 'Only new comment',
  4405. 'user_id' => 2
  4406. ))),
  4407. array('validate' => 'first')
  4408. );
  4409. $this->assertIdentical($result, true);
  4410. $result = $model->Comment->find('all');
  4411. $this->assertIdentical(count($result), 1);
  4412. $result = Set::extract('/Comment/article_id', $result);
  4413. $this->assertEquals($result[0], 4);
  4414. $model->deleteAll(true);
  4415. $data = array(
  4416. 'Article' => array(
  4417. 'title' => 'Post with Author saveAlled from comment',
  4418. 'body' => 'This post will be saved with an author',
  4419. 'user_id' => 2
  4420. ),
  4421. 'Comment' => array(
  4422. 'comment' => 'Only new comment', 'user_id' => 2
  4423. ));
  4424. $result = $model->Comment->saveAssociated($data, array('validate' => 'first'));
  4425. $this->assertFalse(empty($result));
  4426. $result = $model->find('all');
  4427. $this->assertEqual(
  4428. $result[0]['Article']['title'],
  4429. 'Post with Author saveAlled from comment'
  4430. );
  4431. $this->assertEqual($result[0]['Comment'][0]['comment'], 'Only new comment');
  4432. }
  4433. /**
  4434. * test saveMany()'s return is correct when using atomic = false and validate = first.
  4435. *
  4436. * @return void
  4437. */
  4438. public function testSaveManyValidateFirstAtomicFalse() {
  4439. $Something = new Something();
  4440. $invalidData = array(
  4441. array(
  4442. 'title' => 'foo',
  4443. 'body' => 'bar',
  4444. 'published' => 'baz',
  4445. ),
  4446. array(
  4447. 'body' => 3,
  4448. 'published' =>'sd',
  4449. ),
  4450. );
  4451. $Something->create();
  4452. $Something->validate = array(
  4453. 'title' => array(
  4454. 'rule' => 'alphaNumeric',
  4455. 'required' => true,
  4456. ),
  4457. 'body' => array(
  4458. 'rule' => 'alphaNumeric',
  4459. 'required' => true,
  4460. 'allowEmpty' => true,
  4461. ),
  4462. );
  4463. $result = $Something->saveMany($invalidData, array(
  4464. 'atomic' => false,
  4465. 'validate' => 'first',
  4466. ));
  4467. $expected = array(true, false);
  4468. $this->assertEqual($expected, $result);
  4469. $Something = new Something();
  4470. $validData = array(
  4471. array(
  4472. 'title' => 'title value',
  4473. 'body' => 'body value',
  4474. 'published' => 'baz',
  4475. ),
  4476. array(
  4477. 'title' => 'valid',
  4478. 'body' => 'this body',
  4479. 'published' =>'sd',
  4480. ),
  4481. );
  4482. $Something->create();
  4483. $result = $Something->saveMany($validData, array(
  4484. 'atomic' => false,
  4485. 'validate' => 'first',
  4486. ));
  4487. $expected = array(true, true);
  4488. $this->assertEqual($expected, $result);
  4489. }
  4490. /**
  4491. * testValidateAssociated method
  4492. *
  4493. * @return void
  4494. */
  4495. public function testValidateAssociated() {
  4496. $TestModel = new Comment();
  4497. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  4498. $data = array(
  4499. 'Comment' => array(
  4500. 'comment' => 'This is the comment'
  4501. ),
  4502. 'Attachment' => array(
  4503. 'attachment' => ''
  4504. )
  4505. );
  4506. $result = $TestModel->validateAssociated($data);
  4507. $this->assertFalse($result);
  4508. $TestModel = new Article();
  4509. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4510. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  4511. $result = $TestModel->validateAssociated(
  4512. array(
  4513. 'Article' => array('id' => 2),
  4514. 'Comment' => array(
  4515. array(
  4516. 'id' => 1,
  4517. 'comment' => '',
  4518. 'published' => 'Y',
  4519. 'user_id' => 1),
  4520. array(
  4521. 'id' => 2,
  4522. 'comment' =>
  4523. 'comment',
  4524. 'published' => 'Y',
  4525. 'user_id' => 1
  4526. ))));
  4527. $this->assertFalse($result);
  4528. $result = $TestModel->validateAssociated(
  4529. array(
  4530. 'Article' => array('id' => 2),
  4531. 'Comment' => array(
  4532. array(
  4533. 'id' => 1,
  4534. 'comment' => '',
  4535. 'published' => 'Y',
  4536. 'user_id' => 1
  4537. ),
  4538. array(
  4539. 'id' => 2,
  4540. 'comment' => 'comment',
  4541. 'published' => 'Y',
  4542. 'user_id' => 1
  4543. ),
  4544. array(
  4545. 'id' => 3,
  4546. 'comment' => '',
  4547. 'published' => 'Y',
  4548. 'user_id' => 1
  4549. ))),
  4550. array(
  4551. 'atomic' => false
  4552. ));
  4553. $expected = array(
  4554. 'Article' => true,
  4555. 'Comment' => array(false, true, false)
  4556. );
  4557. $this->assertIdentical($expected, $result);
  4558. $expected = array('Comment' => array(
  4559. 0 => array('comment' => array('This field cannot be left blank')),
  4560. 2 => array('comment' => array('This field cannot be left blank'))
  4561. ));
  4562. $this->assertEqual($TestModel->validationErrors, $expected);
  4563. $expected = array(
  4564. 0 => array('comment' => array('This field cannot be left blank')),
  4565. 2 => array('comment' => array('This field cannot be left blank'))
  4566. );
  4567. $this->assertEqual($TestModel->Comment->validationErrors, $expected);
  4568. }
  4569. /**
  4570. * test that saveMany behaves like plain save() when suplied empty data
  4571. *
  4572. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  4573. * @return void
  4574. */
  4575. public function testSaveManyEmptyData() {
  4576. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  4577. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  4578. $model = new Article();
  4579. $result = $model->saveMany(array(), array('validate' => true));
  4580. $this->assertFalse(empty($result));
  4581. $model = new ProductUpdateAll();
  4582. $result = $model->saveMany(array());
  4583. $this->assertFalse($result);
  4584. }
  4585. /**
  4586. * test that saveAssociated behaves like plain save() when suplied empty data
  4587. *
  4588. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  4589. * @return void
  4590. */
  4591. public function testSaveAssociatedEmptyData() {
  4592. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  4593. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  4594. $model = new Article();
  4595. $result = $model->saveAssociated(array(), array('validate' => true));
  4596. $this->assertFalse(empty($result));
  4597. $model = new ProductUpdateAll();
  4598. $result = $model->saveAssociated(array());
  4599. $this->assertFalse($result);
  4600. }
  4601. /**
  4602. * testUpdateWithCalculation method
  4603. *
  4604. * @return void
  4605. */
  4606. public function testUpdateWithCalculation() {
  4607. $this->loadFixtures('DataTest');
  4608. $model = new DataTest();
  4609. $model->deleteAll(true);
  4610. $result = $model->saveMany(array(
  4611. array('count' => 5, 'float' => 1.1),
  4612. array('count' => 3, 'float' => 1.2),
  4613. array('count' => 4, 'float' => 1.3),
  4614. array('count' => 1, 'float' => 2.0),
  4615. ));
  4616. $this->assertFalse(empty($result));
  4617. $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count')));
  4618. $this->assertEqual($result, array(5, 3, 4, 1));
  4619. $this->assertTrue($model->updateAll(array('count' => 'count + 2')));
  4620. $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count')));
  4621. $this->assertEqual($result, array(7, 5, 6, 3));
  4622. $this->assertTrue($model->updateAll(array('DataTest.count' => 'DataTest.count - 1')));
  4623. $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count')));
  4624. $this->assertEqual($result, array(6, 4, 5, 2));
  4625. }
  4626. /**
  4627. * TestFindAllWithoutForeignKey
  4628. *
  4629. * @return void
  4630. */
  4631. public function testFindAllForeignKey() {
  4632. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  4633. $ProductUpdateAll = new ProductUpdateAll();
  4634. $conditions = array('Group.name' => 'group one');
  4635. $ProductUpdateAll->bindModel(array(
  4636. 'belongsTo' => array(
  4637. 'Group' => array('className' => 'GroupUpdateAll')
  4638. )
  4639. ));
  4640. $ProductUpdateAll->belongsTo = array(
  4641. 'Group' => array('className' => 'GroupUpdateAll', 'foreignKey' => 'group_id')
  4642. );
  4643. $results = $ProductUpdateAll->find('all', compact('conditions'));
  4644. $this->assertTrue(!empty($results));
  4645. $ProductUpdateAll->bindModel(array('belongsTo'=>array('Group')));
  4646. $ProductUpdateAll->belongsTo = array(
  4647. 'Group' => array(
  4648. 'className' => 'GroupUpdateAll',
  4649. 'foreignKey' => false,
  4650. 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
  4651. ));
  4652. $resultsFkFalse = $ProductUpdateAll->find('all', compact('conditions'));
  4653. $this->assertTrue(!empty($resultsFkFalse));
  4654. $expected = array(
  4655. '0' => array(
  4656. 'ProductUpdateAll' => array(
  4657. 'id' => 1,
  4658. 'name' => 'product one',
  4659. 'groupcode' => 120,
  4660. 'group_id' => 1),
  4661. 'Group' => array(
  4662. 'id' => 1,
  4663. 'name' => 'group one',
  4664. 'code' => 120)
  4665. ),
  4666. '1' => array(
  4667. 'ProductUpdateAll' => array(
  4668. 'id' => 2,
  4669. 'name' => 'product two',
  4670. 'groupcode' => 120,
  4671. 'group_id' => 1),
  4672. 'Group' => array(
  4673. 'id' => 1,
  4674. 'name' => 'group one',
  4675. 'code' => 120)
  4676. )
  4677. );
  4678. $this->assertEqual($results, $expected);
  4679. $this->assertEqual($resultsFkFalse, $expected);
  4680. }
  4681. /**
  4682. * test updateAll with empty values.
  4683. *
  4684. * @return void
  4685. */
  4686. public function testUpdateAllEmptyValues() {
  4687. $this->skipIf($this->db instanceof Sqlserver || $this->db instanceof Postgres, 'This test is not compatible with Postgres or SQL Server.');
  4688. $this->loadFixtures('Author', 'Post');
  4689. $model = new Author();
  4690. $result = $model->updateAll(array('user' => '""'));
  4691. $this->assertTrue($result);
  4692. }
  4693. /**
  4694. * testUpdateAllWithJoins
  4695. *
  4696. * @return void
  4697. */
  4698. public function testUpdateAllWithJoins() {
  4699. $this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql or sqlite');
  4700. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  4701. $ProductUpdateAll = new ProductUpdateAll();
  4702. $conditions = array('Group.name' => 'group one');
  4703. $ProductUpdateAll->bindModel(array('belongsTo' => array(
  4704. 'Group' => array('className' => 'GroupUpdateAll')))
  4705. );
  4706. $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
  4707. $results = $ProductUpdateAll->find('all', array(
  4708. 'conditions' => array('ProductUpdateAll.name' => 'new product')
  4709. ));
  4710. $expected = array(
  4711. '0' => array(
  4712. 'ProductUpdateAll' => array(
  4713. 'id' => 1,
  4714. 'name' => 'new product',
  4715. 'groupcode' => 120,
  4716. 'group_id' => 1),
  4717. 'Group' => array(
  4718. 'id' => 1,
  4719. 'name' => 'group one',
  4720. 'code' => 120)
  4721. ),
  4722. '1' => array(
  4723. 'ProductUpdateAll' => array(
  4724. 'id' => 2,
  4725. 'name' => 'new product',
  4726. 'groupcode' => 120,
  4727. 'group_id' => 1),
  4728. 'Group' => array(
  4729. 'id' => 1,
  4730. 'name' => 'group one',
  4731. 'code' => 120)));
  4732. $this->assertEqual($results, $expected);
  4733. }
  4734. /**
  4735. * testUpdateAllWithoutForeignKey
  4736. *
  4737. * @return void
  4738. */
  4739. function testUpdateAllWithoutForeignKey() {
  4740. $this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql');
  4741. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  4742. $ProductUpdateAll = new ProductUpdateAll();
  4743. $conditions = array('Group.name' => 'group one');
  4744. $ProductUpdateAll->bindModel(array('belongsTo' => array(
  4745. 'Group' => array('className' => 'GroupUpdateAll')
  4746. )));
  4747. $ProductUpdateAll->belongsTo = array(
  4748. 'Group' => array(
  4749. 'className' => 'GroupUpdateAll',
  4750. 'foreignKey' => false,
  4751. 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
  4752. )
  4753. );
  4754. $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
  4755. $resultsFkFalse = $ProductUpdateAll->find('all', array('conditions' => array('ProductUpdateAll.name'=>'new product')));
  4756. $expected = array(
  4757. '0' => array(
  4758. 'ProductUpdateAll' => array(
  4759. 'id' => 1,
  4760. 'name' => 'new product',
  4761. 'groupcode' => 120,
  4762. 'group_id' => 1),
  4763. 'Group' => array(
  4764. 'id' => 1,
  4765. 'name' => 'group one',
  4766. 'code' => 120)
  4767. ),
  4768. '1' => array(
  4769. 'ProductUpdateAll' => array(
  4770. 'id' => 2,
  4771. 'name' => 'new product',
  4772. 'groupcode' => 120,
  4773. 'group_id' => 1),
  4774. 'Group' => array(
  4775. 'id' => 1,
  4776. 'name' => 'group one',
  4777. 'code' => 120)));
  4778. $this->assertEqual($resultsFkFalse, $expected);
  4779. }
  4780. /**
  4781. * test writing floats in german locale.
  4782. *
  4783. * @return void
  4784. */
  4785. public function testWriteFloatAsGerman() {
  4786. $restore = setlocale(LC_ALL, null);
  4787. setlocale(LC_ALL, 'de_DE');
  4788. $model = new DataTest();
  4789. $result = $model->save(array(
  4790. 'count' => 1,
  4791. 'float' => 3.14593
  4792. ));
  4793. $this->assertTrue((bool)$result);
  4794. setlocale(LC_ALL, $restore);
  4795. }
  4796. /**
  4797. * Test returned array contains primary key when save creates a new record
  4798. *
  4799. * @return void
  4800. */
  4801. public function testPkInReturnArrayForCreate() {
  4802. $this->loadFixtures('Article');
  4803. $TestModel = new Article();
  4804. $data = array('Article' => array(
  4805. 'user_id' => '1',
  4806. 'title' => 'Fourth Article',
  4807. 'body' => 'Fourth Article Body',
  4808. 'published' => 'Y'
  4809. ));
  4810. $result = $TestModel->save($data);
  4811. $this->assertIdentical($result['Article']['id'], $TestModel->id);
  4812. }
  4813. }