ModelWriteTest.php 209 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671
  1. <?php
  2. /**
  3. * ModelWriteTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @package Cake.Test.Case.Model
  15. * @since CakePHP(tm) v 1.2.0.4206
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('MockTransactionDboSource', 'Model/Datasource');
  19. App::uses('MockTransactionAssociatedDboSource', 'Model/Datasource');
  20. App::uses('MockManyTransactionDboSource', 'Model/Datasource');
  21. App::uses('MockAssociatedTransactionDboSource', 'Model/Datasource');
  22. require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
  23. /**
  24. * Helper class for testing with mocked datasources
  25. */
  26. class TestAuthor extends Author {
  27. public $hasMany = array(
  28. 'Post' => array(
  29. 'className' => 'TestPost'
  30. )
  31. );
  32. protected $_dataSourceObject;
  33. public $dataForAfterSave;
  34. /**
  35. * Helper method to set a datasource object
  36. *
  37. * @param Object $object The datasource object
  38. * @return void
  39. */
  40. public function setDataSourceObject($object) {
  41. $this->_dataSourceObject = $object;
  42. }
  43. /**
  44. * Overwritten in order to return the directly set datasource object if
  45. * available
  46. *
  47. * @return DataSource
  48. */
  49. public function getDataSource() {
  50. if ($this->_dataSourceObject !== null) {
  51. return $this->_dataSourceObject;
  52. }
  53. return parent::getDataSource();
  54. }
  55. }
  56. /**
  57. * Helper class for testing with mocked datasources
  58. */
  59. class TestPost extends Post {
  60. public $belongsTo = array(
  61. 'Author' => array(
  62. 'className' => 'TestAuthor'
  63. )
  64. );
  65. protected $_dataSourceObject;
  66. public $dataForAfterSave;
  67. /**
  68. * Helper method to set a datasource object
  69. *
  70. * @param Object $object The datasource object
  71. * @return void
  72. */
  73. public function setDataSourceObject($object) {
  74. $this->_dataSourceObject = $object;
  75. }
  76. /**
  77. * Overwritten in order to return the directly set datasource object if
  78. * available
  79. *
  80. * @return DataSource
  81. */
  82. public function getDataSource() {
  83. if ($this->_dataSourceObject !== null) {
  84. return $this->_dataSourceObject;
  85. }
  86. return parent::getDataSource();
  87. }
  88. }
  89. /**
  90. * ModelWriteTest
  91. *
  92. * @package Cake.Test.Case.Model
  93. */
  94. class ModelWriteTest extends BaseModelTest {
  95. /**
  96. * override locale to the default (eng).
  97. *
  98. * @return void
  99. */
  100. public function setUp() {
  101. parent::setUp();
  102. Configure::write('Config.language', 'eng');
  103. }
  104. /**
  105. * Test save() failing when there is no data.
  106. *
  107. * @return void
  108. */
  109. public function testInsertNoData() {
  110. $this->loadFixtures('Bid');
  111. $Bid = ClassRegistry::init('Bid');
  112. $this->assertFalse($Bid->save());
  113. $result = $Bid->save(array('Bid' => array()));
  114. $this->assertFalse($result);
  115. $result = $Bid->save(array('Bid' => array('not in schema' => 1)));
  116. $this->assertFalse($result);
  117. }
  118. /**
  119. * testInsertAnotherHabtmRecordWithSameForeignKey method
  120. *
  121. * @return void
  122. */
  123. public function testInsertAnotherHabtmRecordWithSameForeignKey() {
  124. $this->loadFixtures('JoinA', 'JoinB', 'JoinAB', 'JoinC', 'JoinAC');
  125. $TestModel = new JoinA();
  126. $result = $TestModel->JoinAsJoinB->findById(1);
  127. $expected = array(
  128. 'JoinAsJoinB' => array(
  129. 'id' => 1,
  130. 'join_a_id' => 1,
  131. 'join_b_id' => 2,
  132. 'other' => 'Data for Join A 1 Join B 2',
  133. 'created' => '2008-01-03 10:56:33',
  134. 'updated' => '2008-01-03 10:56:33'
  135. ));
  136. $this->assertEquals($expected, $result);
  137. $TestModel->JoinAsJoinB->create();
  138. $data = array(
  139. 'join_a_id' => 1,
  140. 'join_b_id' => 1,
  141. 'other' => 'Data for Join A 1 Join B 1',
  142. 'created' => '2008-01-03 10:56:44',
  143. 'updated' => '2008-01-03 10:56:44'
  144. );
  145. $result = $TestModel->JoinAsJoinB->save($data);
  146. $lastInsertId = $TestModel->JoinAsJoinB->getLastInsertID();
  147. $data['id'] = $lastInsertId;
  148. $this->assertEquals(array('JoinAsJoinB' => $data), $result);
  149. $this->assertTrue($lastInsertId > 0);
  150. $result = $TestModel->JoinAsJoinB->findById(1);
  151. $expected = array(
  152. 'JoinAsJoinB' => array(
  153. 'id' => 1,
  154. 'join_a_id' => 1,
  155. 'join_b_id' => 2,
  156. 'other' => 'Data for Join A 1 Join B 2',
  157. 'created' => '2008-01-03 10:56:33',
  158. 'updated' => '2008-01-03 10:56:33'
  159. ));
  160. $this->assertEquals($expected, $result);
  161. $updatedValue = 'UPDATED Data for Join A 1 Join B 2';
  162. $TestModel->JoinAsJoinB->id = 1;
  163. $result = $TestModel->JoinAsJoinB->saveField('other', $updatedValue, false);
  164. $this->assertFalse(empty($result));
  165. $result = $TestModel->JoinAsJoinB->findById(1);
  166. $this->assertEquals($updatedValue, $result['JoinAsJoinB']['other']);
  167. }
  168. /**
  169. * testSaveDateAsFirstEntry method
  170. *
  171. * @return void
  172. */
  173. public function testSaveDateAsFirstEntry() {
  174. $this->loadFixtures('Article', 'User', 'Comment', 'Attachment', 'Tag', 'ArticlesTag');
  175. $Article = new Article();
  176. $data = array(
  177. 'Article' => array(
  178. 'created' => array(
  179. 'day' => '1',
  180. 'month' => '1',
  181. 'year' => '2008'
  182. ),
  183. 'title' => 'Test Title',
  184. 'user_id' => 1
  185. ));
  186. $Article->create();
  187. $result = $Article->save($data);
  188. $this->assertFalse(empty($result));
  189. $testResult = $Article->find('first', array('conditions' => array('Article.title' => 'Test Title')));
  190. $this->assertEquals($data['Article']['title'], $testResult['Article']['title']);
  191. $this->assertEquals('2008-01-01 00:00:00', $testResult['Article']['created']);
  192. }
  193. /**
  194. * testUnderscoreFieldSave method
  195. *
  196. * @return void
  197. */
  198. public function testUnderscoreFieldSave() {
  199. $this->loadFixtures('UnderscoreField');
  200. $UnderscoreField = new UnderscoreField();
  201. $currentCount = $UnderscoreField->find('count');
  202. $this->assertEquals(3, $currentCount);
  203. $data = array('UnderscoreField' => array(
  204. 'user_id' => '1',
  205. 'my_model_has_a_field' => 'Content here',
  206. 'body' => 'Body',
  207. 'published' => 'Y',
  208. 'another_field' => 4
  209. ));
  210. $ret = $UnderscoreField->save($data);
  211. $this->assertFalse(empty($ret));
  212. $currentCount = $UnderscoreField->find('count');
  213. $this->assertEquals(4, $currentCount);
  214. }
  215. /**
  216. * testAutoSaveUuid method
  217. *
  218. * @return void
  219. */
  220. public function testAutoSaveUuid() {
  221. // SQLite does not support non-integer primary keys
  222. $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with SQLite.');
  223. $this->loadFixtures('Uuid');
  224. $TestModel = new Uuid();
  225. $TestModel->save(array('title' => 'Test record'));
  226. $result = $TestModel->findByTitle('Test record');
  227. $this->assertEquals(
  228. array('id', 'title', 'count', 'created', 'updated'),
  229. array_keys($result['Uuid'])
  230. );
  231. $this->assertEquals(36, strlen($result['Uuid']['id']));
  232. }
  233. /**
  234. * Ensure that if the id key is null but present the save doesn't fail (with an
  235. * x sql error: "Column id specified twice")
  236. *
  237. * @return void
  238. */
  239. public function testSaveUuidNull() {
  240. // SQLite does not support non-integer primary keys
  241. $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with SQLite.');
  242. $this->loadFixtures('Uuid');
  243. $TestModel = new Uuid();
  244. $TestModel->save(array('title' => 'Test record', 'id' => null));
  245. $result = $TestModel->findByTitle('Test record');
  246. $this->assertEquals(
  247. array('id', 'title', 'count', 'created', 'updated'),
  248. array_keys($result['Uuid'])
  249. );
  250. $this->assertEquals(36, strlen($result['Uuid']['id']));
  251. }
  252. /**
  253. * testZeroDefaultFieldValue method
  254. *
  255. * @return void
  256. */
  257. public function testZeroDefaultFieldValue() {
  258. $this->skipIf($this->db instanceof Sqlite, 'SQLite uses loose typing, this operation is unsupported.');
  259. $this->loadFixtures('DataTest');
  260. $TestModel = new DataTest();
  261. $TestModel->create(array());
  262. $TestModel->save();
  263. $result = $TestModel->findById($TestModel->id);
  264. $this->assertEquals(0, $result['DataTest']['count']);
  265. $this->assertEquals(0, $result['DataTest']['float']);
  266. }
  267. /**
  268. * Tests validation parameter order in custom validation methods
  269. *
  270. * @return void
  271. */
  272. public function testAllowSimulatedFields() {
  273. $TestModel = new ValidationTest1();
  274. $TestModel->create(array(
  275. 'title' => 'foo',
  276. 'bar' => 'baz'
  277. ));
  278. $expected = array(
  279. 'ValidationTest1' => array(
  280. 'title' => 'foo',
  281. 'bar' => 'baz'
  282. ));
  283. $this->assertEquals($expected, $TestModel->data);
  284. }
  285. /**
  286. * test that Caches are getting cleared on save().
  287. * ensure that both inflections of controller names are getting cleared
  288. * as URL for controller could be either overallFavorites/index or overall_favorites/index
  289. *
  290. * @return void
  291. */
  292. public function testCacheClearOnSave() {
  293. $_back = array(
  294. 'check' => Configure::read('Cache.check'),
  295. 'disable' => Configure::read('Cache.disable'),
  296. );
  297. Configure::write('Cache.check', true);
  298. Configure::write('Cache.disable', false);
  299. $this->loadFixtures('OverallFavorite');
  300. $OverallFavorite = new OverallFavorite();
  301. touch(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php');
  302. touch(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php');
  303. $data = array(
  304. 'OverallFavorite' => array(
  305. 'id' => 22,
  306. 'model_type' => '8-track',
  307. 'model_id' => '3',
  308. 'priority' => '1'
  309. )
  310. );
  311. $OverallFavorite->create($data);
  312. $OverallFavorite->save();
  313. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php'));
  314. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php'));
  315. Configure::write('Cache.check', $_back['check']);
  316. Configure::write('Cache.disable', $_back['disable']);
  317. }
  318. /**
  319. * test that save() resets whitelist on failed save
  320. *
  321. * @return void
  322. */
  323. public function testSaveFieldListResetsWhitelistOnFailedSave() {
  324. $this->loadFixtures('Bidding');
  325. $model = new Bidding();
  326. $whitelist = array('title');
  327. $model->whitelist = $whitelist;
  328. $result = $model->save(
  329. array(),
  330. array('fieldList' => array('body'))
  331. );
  332. $this->assertFalse($result);
  333. $this->assertEquals($whitelist, $model->whitelist);
  334. }
  335. /**
  336. * testSaveWithCounterCache method
  337. *
  338. * @return void
  339. */
  340. public function testSaveWithCounterCache() {
  341. $this->loadFixtures('Syfile', 'Item', 'Image', 'Portfolio', 'ItemsPortfolio');
  342. $TestModel = new Syfile();
  343. $TestModel2 = new Item();
  344. $result = $TestModel->findById(1);
  345. $this->assertNull($result['Syfile']['item_count']);
  346. $TestModel2->save(array(
  347. 'name' => 'Item 7',
  348. 'syfile_id' => 1,
  349. 'published' => false
  350. ));
  351. $result = $TestModel->findById(1);
  352. $this->assertEquals(2, $result['Syfile']['item_count']);
  353. $TestModel2->delete(1);
  354. $result = $TestModel->findById(1);
  355. $this->assertEquals(1, $result['Syfile']['item_count']);
  356. $TestModel2->id = 2;
  357. $TestModel2->saveField('syfile_id', 1);
  358. $result = $TestModel->findById(1);
  359. $this->assertEquals(2, $result['Syfile']['item_count']);
  360. $result = $TestModel->findById(2);
  361. $this->assertEquals(0, $result['Syfile']['item_count']);
  362. }
  363. /**
  364. * Tests that counter caches are updated when records are added
  365. *
  366. * @return void
  367. */
  368. public function testCounterCacheIncrease() {
  369. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  370. $User = new CounterCacheUser();
  371. $Post = new CounterCachePost();
  372. $data = array('Post' => array(
  373. 'id' => 22,
  374. 'title' => 'New Post',
  375. 'user_id' => 66
  376. ));
  377. $Post->save($data);
  378. $user = $User->find('first', array(
  379. 'conditions' => array('id' => 66),
  380. 'recursive' => -1
  381. ));
  382. $result = $user[$User->alias]['post_count'];
  383. $expected = 3;
  384. $this->assertEquals($expected, $result);
  385. }
  386. /**
  387. * Tests that counter caches are updated when records are deleted
  388. *
  389. * @return void
  390. */
  391. public function testCounterCacheDecrease() {
  392. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  393. $User = new CounterCacheUser();
  394. $Post = new CounterCachePost();
  395. $Post->delete(2);
  396. $user = $User->find('first', array(
  397. 'conditions' => array('id' => 66),
  398. 'recursive' => -1
  399. ));
  400. $result = $user[$User->alias]['post_count'];
  401. $expected = 1;
  402. $this->assertEquals($expected, $result);
  403. }
  404. /**
  405. * Tests that counter caches are updated when foreign keys of counted records change
  406. *
  407. * @return void
  408. */
  409. public function testCounterCacheUpdated() {
  410. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  411. $User = new CounterCacheUser();
  412. $Post = new CounterCachePost();
  413. $data = $Post->find('first', array(
  414. 'conditions' => array('id' => 1),
  415. 'recursive' => -1
  416. ));
  417. $data[$Post->alias]['user_id'] = 301;
  418. $Post->save($data);
  419. $users = $User->find('all', array('order' => 'User.id'));
  420. $this->assertEquals(1, $users[0]['User']['post_count']);
  421. $this->assertEquals(2, $users[1]['User']['post_count']);
  422. }
  423. /**
  424. * Test counter cache with models that use a non-standard (i.e. not using 'id')
  425. * as their primary key.
  426. *
  427. * @return void
  428. */
  429. public function testCounterCacheWithNonstandardPrimaryKey() {
  430. $this->loadFixtures(
  431. 'CounterCacheUserNonstandardPrimaryKey',
  432. 'CounterCachePostNonstandardPrimaryKey'
  433. );
  434. $User = new CounterCacheUserNonstandardPrimaryKey();
  435. $Post = new CounterCachePostNonstandardPrimaryKey();
  436. $data = $Post->find('first', array(
  437. 'conditions' => array('pid' => 1),
  438. 'recursive' => -1
  439. ));
  440. $data[$Post->alias]['uid'] = 301;
  441. $Post->save($data);
  442. $users = $User->find('all', array('order' => 'User.uid'));
  443. $this->assertEquals(1, $users[0]['User']['post_count']);
  444. $this->assertEquals(2, $users[1]['User']['post_count']);
  445. }
  446. /**
  447. * test Counter Cache With Self Joining table
  448. *
  449. * @return void
  450. */
  451. public function testCounterCacheWithSelfJoin() {
  452. $this->skipIf($this->db instanceof Sqlite, 'SQLite 2.x does not support ALTER TABLE ADD COLUMN');
  453. $this->loadFixtures('CategoryThread');
  454. $column = 'COLUMN ';
  455. if ($this->db instanceof Sqlserver) {
  456. $column = '';
  457. }
  458. $column .= $this->db->buildColumn(array('name' => 'child_count', 'type' => 'integer'));
  459. $this->db->query('ALTER TABLE ' . $this->db->fullTableName('category_threads') . ' ADD ' . $column);
  460. $this->db->flushMethodCache();
  461. $Category = new CategoryThread();
  462. $result = $Category->updateAll(array('CategoryThread.name' => "'updated'"), array('CategoryThread.parent_id' => 5));
  463. $this->assertFalse(empty($result));
  464. $Category = new CategoryThread();
  465. $Category->belongsTo['ParentCategory']['counterCache'] = 'child_count';
  466. $Category->updateCounterCache(array('parent_id' => 5));
  467. $result = Hash::extract($Category->find('all', array('conditions' => array('CategoryThread.id' => 5))), '{n}.CategoryThread.child_count');
  468. $expected = array(1);
  469. $this->assertEquals($expected, $result);
  470. }
  471. /**
  472. * testSaveWithCounterCacheScope method
  473. *
  474. * @return void
  475. */
  476. public function testSaveWithCounterCacheScope() {
  477. $this->loadFixtures('Syfile', 'Item', 'Image', 'ItemsPortfolio', 'Portfolio');
  478. $TestModel = new Syfile();
  479. $TestModel2 = new Item();
  480. $TestModel2->belongsTo['Syfile']['counterCache'] = true;
  481. $TestModel2->belongsTo['Syfile']['counterScope'] = array('published' => true);
  482. $result = $TestModel->findById(1);
  483. $this->assertNull($result['Syfile']['item_count']);
  484. $TestModel2->save(array(
  485. 'name' => 'Item 7',
  486. 'syfile_id' => 1,
  487. 'published' => true
  488. ));
  489. $result = $TestModel->findById(1);
  490. $this->assertEquals(1, $result['Syfile']['item_count']);
  491. $TestModel2->id = 1;
  492. $TestModel2->saveField('published', true);
  493. $result = $TestModel->findById(1);
  494. $this->assertEquals(2, $result['Syfile']['item_count']);
  495. $TestModel2->save(array(
  496. 'id' => 1,
  497. 'syfile_id' => 1,
  498. 'published' => false
  499. ));
  500. $result = $TestModel->findById(1);
  501. $this->assertEquals(1, $result['Syfile']['item_count']);
  502. }
  503. /**
  504. * Tests having multiple counter caches for an associated model
  505. *
  506. * @return void
  507. */
  508. public function testCounterCacheMultipleCaches() {
  509. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  510. $User = new CounterCacheUser();
  511. $Post = new CounterCachePost();
  512. $Post->unbindModel(array('belongsTo' => array('User')), false);
  513. $Post->bindModel(array(
  514. 'belongsTo' => array(
  515. 'User' => array(
  516. 'className' => 'CounterCacheUser',
  517. 'foreignKey' => 'user_id',
  518. 'counterCache' => array(
  519. true,
  520. 'posts_published' => array('Post.published' => true)
  521. )
  522. )
  523. )
  524. ), false);
  525. // Count Increase
  526. $data = array('Post' => array(
  527. 'id' => 22,
  528. 'title' => 'New Post',
  529. 'user_id' => 66,
  530. 'published' => true
  531. ));
  532. $Post->save($data);
  533. $result = $User->find('first', array(
  534. 'conditions' => array('id' => 66),
  535. 'recursive' => -1
  536. ));
  537. $this->assertEquals(3, $result[$User->alias]['post_count']);
  538. $this->assertEquals(2, $result[$User->alias]['posts_published']);
  539. // Count decrease
  540. $Post->delete(1);
  541. $result = $User->find('first', array(
  542. 'conditions' => array('id' => 66),
  543. 'recursive' => -1
  544. ));
  545. $this->assertEquals(2, $result[$User->alias]['post_count']);
  546. $this->assertEquals(2, $result[$User->alias]['posts_published']);
  547. // Count update
  548. $data = $Post->find('first', array(
  549. 'conditions' => array('id' => 1),
  550. 'recursive' => -1
  551. ));
  552. $data[$Post->alias]['user_id'] = 301;
  553. $Post->save($data);
  554. $result = $User->find('all', array('order' => 'User.id'));
  555. $this->assertEquals(2, $result[0]['User']['post_count']);
  556. $this->assertEquals(1, $result[1]['User']['posts_published']);
  557. }
  558. /**
  559. * Tests that counter caches are unchanged when using 'counterCache' => false
  560. *
  561. * @return void
  562. */
  563. public function testCounterCacheSkip() {
  564. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  565. $User = new CounterCacheUser();
  566. $Post = new CounterCachePost();
  567. $data = $Post->find('first', array(
  568. 'conditions' => array('id' => 1),
  569. 'recursive' => -1
  570. ));
  571. $data[$Post->alias]['user_id'] = 301;
  572. $Post->save($data, array('counterCache' => false));
  573. $users = $User->find('all', array('order' => 'User.id'));
  574. $this->assertEquals(2, $users[0]['User']['post_count']);
  575. $this->assertEquals(1, $users[1]['User']['post_count']);
  576. }
  577. /**
  578. * test that beforeValidate returning false can abort saves.
  579. *
  580. * @return void
  581. */
  582. public function testBeforeValidateSaveAbortion() {
  583. $this->loadFixtures('Post');
  584. $Model = new CallbackPostTestModel();
  585. $Model->beforeValidateReturn = false;
  586. $data = array(
  587. 'title' => 'new article',
  588. 'body' => 'this is some text.'
  589. );
  590. $Model->create();
  591. $result = $Model->save($data);
  592. $this->assertFalse($result);
  593. }
  594. /**
  595. * test that beforeSave returning false can abort saves.
  596. *
  597. * @return void
  598. */
  599. public function testBeforeSaveSaveAbortion() {
  600. $this->loadFixtures('Post');
  601. $Model = new CallbackPostTestModel();
  602. $Model->beforeSaveReturn = false;
  603. $data = array(
  604. 'title' => 'new article',
  605. 'body' => 'this is some text.'
  606. );
  607. $Model->create();
  608. $result = $Model->save($data);
  609. $this->assertFalse($result);
  610. }
  611. /**
  612. * testSaveField method
  613. *
  614. * @return void
  615. */
  616. public function testSaveField() {
  617. $this->loadFixtures('Article');
  618. $TestModel = new Article();
  619. $TestModel->id = 1;
  620. $result = $TestModel->saveField('title', 'New First Article');
  621. $this->assertFalse(empty($result));
  622. $TestModel->recursive = -1;
  623. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  624. $expected = array('Article' => array(
  625. 'id' => '1',
  626. 'user_id' => '1',
  627. 'title' => 'New First Article',
  628. 'body' => 'First Article Body'
  629. ));
  630. $this->assertEquals($expected, $result);
  631. $TestModel->id = 1;
  632. $result = $TestModel->saveField('title', '');
  633. $this->assertFalse(empty($result));
  634. $TestModel->recursive = -1;
  635. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  636. $expected = array('Article' => array(
  637. 'id' => '1',
  638. 'user_id' => '1',
  639. 'title' => '',
  640. 'body' => 'First Article Body'
  641. ));
  642. $result['Article']['title'] = trim($result['Article']['title']);
  643. $this->assertEquals($expected, $result);
  644. $TestModel->id = 1;
  645. $TestModel->set('body', 'Messed up data');
  646. $result = $TestModel->saveField('title', 'First Article');
  647. $this->assertFalse(empty($result));
  648. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  649. $expected = array('Article' => array(
  650. 'id' => '1',
  651. 'user_id' => '1',
  652. 'title' => 'First Article',
  653. 'body' => 'First Article Body'
  654. ));
  655. $this->assertEquals($expected, $result);
  656. $TestModel->recursive = -1;
  657. $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  658. $TestModel->id = 1;
  659. $result = $TestModel->saveField('title', '', true);
  660. $this->assertFalse($result);
  661. $TestModel->recursive = -1;
  662. $TestModel->id = 1;
  663. $result = $TestModel->saveField('user_id', 9999);
  664. $this->assertTrue((bool)$result);
  665. $result = $TestModel->read(array('id', 'user_id'), 1);
  666. $expected = array('Article' => array(
  667. 'id' => '1',
  668. 'user_id' => '9999',
  669. ));
  670. $this->assertEquals($expected, $result);
  671. $this->loadFixtures('Node', 'Dependency');
  672. $Node = new Node();
  673. $Node->set('id', 1);
  674. $result = $Node->read();
  675. $this->assertEquals(array('Second'), Hash::extract($result, 'ParentNode.{n}.name'));
  676. $Node->saveField('state', 10);
  677. $result = $Node->read();
  678. $this->assertEquals(array('Second'), Hash::extract($result, 'ParentNode.{n}.name'));
  679. }
  680. /**
  681. * testSaveWithCreate method
  682. *
  683. * @return void
  684. */
  685. public function testSaveWithCreate() {
  686. $this->loadFixtures(
  687. 'User',
  688. 'Article',
  689. 'User',
  690. 'Comment',
  691. 'Tag',
  692. 'ArticlesTag',
  693. 'Attachment'
  694. );
  695. $TestModel = new User();
  696. $data = array('User' => array(
  697. 'user' => 'user',
  698. 'password' => ''
  699. ));
  700. $result = $TestModel->save($data);
  701. $this->assertFalse($result);
  702. $this->assertTrue(!empty($TestModel->validationErrors));
  703. $TestModel = new Article();
  704. $data = array('Article' => array(
  705. 'user_id' => '',
  706. 'title' => '',
  707. 'body' => ''
  708. ));
  709. $result = $TestModel->create($data) && $TestModel->save();
  710. $this->assertFalse($result);
  711. $this->assertTrue(!empty($TestModel->validationErrors));
  712. $data = array('Article' => array(
  713. 'id' => 1,
  714. 'user_id' => '1',
  715. 'title' => 'New First Article',
  716. 'body' => ''
  717. ));
  718. $result = $TestModel->create($data) && $TestModel->save();
  719. $this->assertFalse($result);
  720. $data = array('Article' => array(
  721. 'id' => 1,
  722. 'title' => 'New First Article'
  723. ));
  724. $result = $TestModel->create() && $TestModel->save($data, false);
  725. $this->assertFalse(empty($result));
  726. $TestModel->recursive = -1;
  727. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
  728. $expected = array('Article' => array(
  729. 'id' => '1',
  730. 'user_id' => '1',
  731. 'title' => 'New First Article',
  732. 'body' => 'First Article Body',
  733. 'published' => 'N'
  734. ));
  735. $this->assertEquals($expected, $result);
  736. $data = array('Article' => array(
  737. 'id' => 1,
  738. 'user_id' => '2',
  739. 'title' => 'First Article',
  740. 'body' => 'New First Article Body',
  741. 'published' => 'Y'
  742. ));
  743. $result = $TestModel->create() && $TestModel->save($data, true, array('id', 'title', 'published'));
  744. $this->assertFalse(empty($result));
  745. $TestModel->recursive = -1;
  746. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
  747. $expected = array('Article' => array(
  748. 'id' => '1',
  749. 'user_id' => '1',
  750. 'title' => 'First Article',
  751. 'body' => 'First Article Body',
  752. 'published' => 'Y'
  753. ));
  754. $this->assertEquals($expected, $result);
  755. $data = array(
  756. 'Article' => array(
  757. 'user_id' => '2',
  758. 'title' => 'New Article',
  759. 'body' => 'New Article Body',
  760. 'created' => '2007-03-18 14:55:23',
  761. 'updated' => '2007-03-18 14:57:31'
  762. ),
  763. 'Tag' => array('Tag' => array(1, 3))
  764. );
  765. $TestModel->create();
  766. $result = $TestModel->create() && $TestModel->save($data);
  767. $this->assertFalse(empty($result));
  768. $TestModel->recursive = 2;
  769. $result = $TestModel->read(null, 4);
  770. $expected = array(
  771. 'Article' => array(
  772. 'id' => '4',
  773. 'user_id' => '2',
  774. 'title' => 'New Article',
  775. 'body' => 'New Article Body',
  776. 'published' => 'N',
  777. 'created' => '2007-03-18 14:55:23',
  778. 'updated' => '2007-03-18 14:57:31'
  779. ),
  780. 'User' => array(
  781. 'id' => '2',
  782. 'user' => 'nate',
  783. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  784. 'created' => '2007-03-17 01:18:23',
  785. 'updated' => '2007-03-17 01:20:31'
  786. ),
  787. 'Comment' => array(),
  788. 'Tag' => array(
  789. array(
  790. 'id' => '1',
  791. 'tag' => 'tag1',
  792. 'created' => '2007-03-18 12:22:23',
  793. 'updated' => '2007-03-18 12:24:31'
  794. ),
  795. array(
  796. 'id' => '3',
  797. 'tag' => 'tag3',
  798. 'created' => '2007-03-18 12:26:23',
  799. 'updated' => '2007-03-18 12:28:31'
  800. )));
  801. $this->assertEquals($expected, $result);
  802. $data = array('Comment' => array(
  803. 'article_id' => '4',
  804. 'user_id' => '1',
  805. 'comment' => 'Comment New Article',
  806. 'published' => 'Y',
  807. 'created' => '2007-03-18 14:57:23',
  808. 'updated' => '2007-03-18 14:59:31'
  809. ));
  810. $result = $TestModel->Comment->create() && $TestModel->Comment->save($data);
  811. $this->assertFalse(empty($result));
  812. $data = array('Attachment' => array(
  813. 'comment_id' => '7',
  814. 'attachment' => 'newattachment.zip',
  815. 'created' => '2007-03-18 15:02:23',
  816. 'updated' => '2007-03-18 15:04:31'
  817. ));
  818. $result = $TestModel->Comment->Attachment->save($data);
  819. $this->assertFalse(empty($result));
  820. $TestModel->recursive = 2;
  821. $result = $TestModel->read(null, 4);
  822. $expected = array(
  823. 'Article' => array(
  824. 'id' => '4',
  825. 'user_id' => '2',
  826. 'title' => 'New Article',
  827. 'body' => 'New Article Body',
  828. 'published' => 'N',
  829. 'created' => '2007-03-18 14:55:23',
  830. 'updated' => '2007-03-18 14:57:31'
  831. ),
  832. 'User' => array(
  833. 'id' => '2',
  834. 'user' => 'nate',
  835. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  836. 'created' => '2007-03-17 01:18:23',
  837. 'updated' => '2007-03-17 01:20:31'
  838. ),
  839. 'Comment' => array(
  840. array(
  841. 'id' => '7',
  842. 'article_id' => '4',
  843. 'user_id' => '1',
  844. 'comment' => 'Comment New Article',
  845. 'published' => 'Y',
  846. 'created' => '2007-03-18 14:57:23',
  847. 'updated' => '2007-03-18 14:59:31',
  848. 'Article' => array(
  849. 'id' => '4',
  850. 'user_id' => '2',
  851. 'title' => 'New Article',
  852. 'body' => 'New Article Body',
  853. 'published' => 'N',
  854. 'created' => '2007-03-18 14:55:23',
  855. 'updated' => '2007-03-18 14:57:31'
  856. ),
  857. 'User' => array(
  858. 'id' => '1',
  859. 'user' => 'mariano',
  860. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  861. 'created' => '2007-03-17 01:16:23',
  862. 'updated' => '2007-03-17 01:18:31'
  863. ),
  864. 'Attachment' => array(
  865. 'id' => '2',
  866. 'comment_id' => '7',
  867. 'attachment' => 'newattachment.zip',
  868. 'created' => '2007-03-18 15:02:23',
  869. 'updated' => '2007-03-18 15:04:31'
  870. ))),
  871. 'Tag' => array(
  872. array(
  873. 'id' => '1',
  874. 'tag' => 'tag1',
  875. 'created' => '2007-03-18 12:22:23',
  876. 'updated' => '2007-03-18 12:24:31'
  877. ),
  878. array(
  879. 'id' => '3',
  880. 'tag' => 'tag3',
  881. 'created' => '2007-03-18 12:26:23',
  882. 'updated' => '2007-03-18 12:28:31'
  883. )));
  884. $this->assertEquals($expected, $result);
  885. }
  886. /**
  887. * test that a null Id doesn't cause errors
  888. *
  889. * @return void
  890. */
  891. public function testSaveWithNullId() {
  892. $this->loadFixtures('User');
  893. $User = new User();
  894. $User->read(null, 1);
  895. $User->data['User']['id'] = null;
  896. $result = $User->save(array('password' => 'test'));
  897. $this->assertFalse(empty($result));
  898. $this->assertTrue($User->id > 0);
  899. $User->read(null, 2);
  900. $User->data['User']['id'] = null;
  901. $result = $User->save(array('password' => 'test'));
  902. $this->assertFalse(empty($result));
  903. $this->assertTrue($User->id > 0);
  904. $User->data['User'] = array('password' => 'something');
  905. $result = $User->save();
  906. $this->assertFalse(empty($result));
  907. $result = $User->read();
  908. $this->assertEquals('something', $User->data['User']['password']);
  909. }
  910. /**
  911. * testSaveWithSet method
  912. *
  913. * @return void
  914. */
  915. public function testSaveWithSet() {
  916. $this->loadFixtures('Article');
  917. $TestModel = new Article();
  918. // Create record we will be updating later
  919. $data = array('Article' => array(
  920. 'user_id' => '1',
  921. 'title' => 'Fourth Article',
  922. 'body' => 'Fourth Article Body',
  923. 'published' => 'Y'
  924. ));
  925. $result = $TestModel->create() && $TestModel->save($data);
  926. $this->assertFalse(empty($result));
  927. // Check record we created
  928. $TestModel->recursive = -1;
  929. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  930. $expected = array('Article' => array(
  931. 'id' => '4',
  932. 'user_id' => '1',
  933. 'title' => 'Fourth Article',
  934. 'body' => 'Fourth Article Body',
  935. 'published' => 'Y'
  936. ));
  937. $this->assertEquals($expected, $result);
  938. // Create new record just to overlap Model->id on previously created record
  939. $data = array('Article' => array(
  940. 'user_id' => '4',
  941. 'title' => 'Fifth Article',
  942. 'body' => 'Fifth Article Body',
  943. 'published' => 'Y'
  944. ));
  945. $result = $TestModel->create() && $TestModel->save($data);
  946. $this->assertFalse(empty($result));
  947. $TestModel->recursive = -1;
  948. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  949. $expected = array('Article' => array(
  950. 'id' => '5',
  951. 'user_id' => '4',
  952. 'title' => 'Fifth Article',
  953. 'body' => 'Fifth Article Body',
  954. 'published' => 'Y'
  955. ));
  956. $this->assertEquals($expected, $result);
  957. // Go back and edit the first article we created, starting by checking it's still there
  958. $TestModel->recursive = -1;
  959. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  960. $expected = array('Article' => array(
  961. 'id' => '4',
  962. 'user_id' => '1',
  963. 'title' => 'Fourth Article',
  964. 'body' => 'Fourth Article Body',
  965. 'published' => 'Y'
  966. ));
  967. $this->assertEquals($expected, $result);
  968. // And now do the update with set()
  969. $data = array('Article' => array(
  970. 'id' => '4',
  971. 'title' => 'Fourth Article - New Title',
  972. 'published' => 'N'
  973. ));
  974. $result = $TestModel->set($data) && $TestModel->save();
  975. $this->assertFalse(empty($result));
  976. $TestModel->recursive = -1;
  977. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  978. $expected = array('Article' => array(
  979. 'id' => '4',
  980. 'user_id' => '1',
  981. 'title' => 'Fourth Article - New Title',
  982. 'body' => 'Fourth Article Body',
  983. 'published' => 'N'
  984. ));
  985. $this->assertEquals($expected, $result);
  986. $TestModel->recursive = -1;
  987. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  988. $expected = array('Article' => array(
  989. 'id' => '5',
  990. 'user_id' => '4',
  991. 'title' => 'Fifth Article',
  992. 'body' => 'Fifth Article Body',
  993. 'published' => 'Y'
  994. ));
  995. $this->assertEquals($expected, $result);
  996. $data = array('Article' => array('id' => '5', 'title' => 'Fifth Article - New Title 5'));
  997. $result = ($TestModel->set($data) && $TestModel->save());
  998. $this->assertFalse(empty($result));
  999. $TestModel->recursive = -1;
  1000. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  1001. $expected = array('Article' => array(
  1002. 'id' => '5',
  1003. 'user_id' => '4',
  1004. 'title' => 'Fifth Article - New Title 5',
  1005. 'body' => 'Fifth Article Body',
  1006. 'published' => 'Y'
  1007. ));
  1008. $this->assertEquals($expected, $result);
  1009. $TestModel->recursive = -1;
  1010. $result = $TestModel->find('all', array(
  1011. 'fields' => array('id', 'title'),
  1012. 'order' => array('Article.id' => 'ASC')
  1013. ));
  1014. $expected = array(
  1015. array('Article' => array('id' => 1, 'title' => 'First Article')),
  1016. array('Article' => array('id' => 2, 'title' => 'Second Article')),
  1017. array('Article' => array('id' => 3, 'title' => 'Third Article')),
  1018. array('Article' => array('id' => 4, 'title' => 'Fourth Article - New Title')),
  1019. array('Article' => array('id' => 5, 'title' => 'Fifth Article - New Title 5'))
  1020. );
  1021. $this->assertEquals($expected, $result);
  1022. }
  1023. /**
  1024. * testSaveWithNonExistentFields method
  1025. *
  1026. * @return void
  1027. */
  1028. public function testSaveWithNonExistentFields() {
  1029. $this->loadFixtures('Article');
  1030. $TestModel = new Article();
  1031. $TestModel->recursive = -1;
  1032. $data = array(
  1033. 'non_existent' => 'This field does not exist',
  1034. 'user_id' => '1',
  1035. 'title' => 'Fourth Article - New Title',
  1036. 'body' => 'Fourth Article Body',
  1037. 'published' => 'N'
  1038. );
  1039. $result = $TestModel->create() && $TestModel->save($data);
  1040. $this->assertFalse(empty($result));
  1041. $expected = array('Article' => array(
  1042. 'id' => '4',
  1043. 'user_id' => '1',
  1044. 'title' => 'Fourth Article - New Title',
  1045. 'body' => 'Fourth Article Body',
  1046. 'published' => 'N'
  1047. ));
  1048. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  1049. $this->assertEquals($expected, $result);
  1050. $data = array(
  1051. 'user_id' => '1',
  1052. 'non_existent' => 'This field does not exist',
  1053. 'title' => 'Fifth Article - New Title',
  1054. 'body' => 'Fifth Article Body',
  1055. 'published' => 'N'
  1056. );
  1057. $result = $TestModel->create() && $TestModel->save($data);
  1058. $this->assertFalse(empty($result));
  1059. $expected = array('Article' => array(
  1060. 'id' => '5',
  1061. 'user_id' => '1',
  1062. 'title' => 'Fifth Article - New Title',
  1063. 'body' => 'Fifth Article Body',
  1064. 'published' => 'N'
  1065. ));
  1066. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  1067. $this->assertEquals($expected, $result);
  1068. }
  1069. /**
  1070. * testSaveFromXml method
  1071. *
  1072. * @return void
  1073. */
  1074. public function testSaveFromXml() {
  1075. $this->markTestSkipped('This feature needs to be fixed or dropped');
  1076. $this->loadFixtures('Article');
  1077. App::uses('Xml', 'Utility');
  1078. $Article = new Article();
  1079. $result = $Article->save(Xml::build('<article title="test xml" user_id="5" />'));
  1080. $this->assertFalse(empty($result));
  1081. $results = $Article->find('first', array('conditions' => array('Article.title' => 'test xml')));
  1082. $this->assertFalse(empty($results));
  1083. $result = $Article->save(Xml::build('<article><title>testing</title><user_id>6</user_id></article>'));
  1084. $this->assertFalse(empty($result));
  1085. $results = $Article->find('first', array('conditions' => array('Article.title' => 'testing')));
  1086. $this->assertFalse(empty($results));
  1087. $result = $Article->save(Xml::build('<article><title>testing with DOMDocument</title><user_id>7</user_id></article>', array('return' => 'domdocument')));
  1088. $this->assertFalse(empty($result));
  1089. $results = $Article->find('first', array('conditions' => array('Article.title' => 'testing with DOMDocument')));
  1090. $this->assertFalse(empty($results));
  1091. }
  1092. /**
  1093. * testSaveHabtm method
  1094. *
  1095. * @return void
  1096. */
  1097. public function testSaveHabtm() {
  1098. $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
  1099. $TestModel = new Article();
  1100. $result = $TestModel->findById(2);
  1101. $expected = array(
  1102. 'Article' => array(
  1103. 'id' => '2',
  1104. 'user_id' => '3',
  1105. 'title' => 'Second Article',
  1106. 'body' => 'Second Article Body',
  1107. 'published' => 'Y',
  1108. 'created' => '2007-03-18 10:41:23',
  1109. 'updated' => '2007-03-18 10:43:31'
  1110. ),
  1111. 'User' => array(
  1112. 'id' => '3',
  1113. 'user' => 'larry',
  1114. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  1115. 'created' => '2007-03-17 01:20:23',
  1116. 'updated' => '2007-03-17 01:22:31'
  1117. ),
  1118. 'Comment' => array(
  1119. array(
  1120. 'id' => '5',
  1121. 'article_id' => '2',
  1122. 'user_id' => '1',
  1123. 'comment' => 'First Comment for Second Article',
  1124. 'published' => 'Y',
  1125. 'created' => '2007-03-18 10:53:23',
  1126. 'updated' => '2007-03-18 10:55:31'
  1127. ),
  1128. array(
  1129. 'id' => '6',
  1130. 'article_id' => '2',
  1131. 'user_id' => '2',
  1132. 'comment' => 'Second Comment for Second Article',
  1133. 'published' => 'Y',
  1134. 'created' => '2007-03-18 10:55:23',
  1135. 'updated' => '2007-03-18 10:57:31'
  1136. )),
  1137. 'Tag' => array(
  1138. array(
  1139. 'id' => '1',
  1140. 'tag' => 'tag1',
  1141. 'created' => '2007-03-18 12:22:23',
  1142. 'updated' => '2007-03-18 12:24:31'
  1143. ),
  1144. array(
  1145. 'id' => '3',
  1146. 'tag' => 'tag3',
  1147. 'created' => '2007-03-18 12:26:23',
  1148. 'updated' => '2007-03-18 12:28:31'
  1149. )
  1150. )
  1151. );
  1152. $this->assertEquals($expected, $result);
  1153. $data = array(
  1154. 'Article' => array(
  1155. 'id' => '2',
  1156. 'title' => 'New Second Article'
  1157. ),
  1158. 'Tag' => array('Tag' => array(1, 2))
  1159. );
  1160. $result = $TestModel->set($data);
  1161. $this->assertFalse(empty($result));
  1162. $result = $TestModel->save();
  1163. $this->assertFalse(empty($result));
  1164. $this->assertEquals($data['Tag'], $result['Tag']);
  1165. $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
  1166. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1167. $expected = array(
  1168. 'Article' => array(
  1169. 'id' => '2',
  1170. 'user_id' => '3',
  1171. 'title' => 'New Second Article',
  1172. 'body' => 'Second Article Body'
  1173. ),
  1174. 'Tag' => array(
  1175. array(
  1176. 'id' => '1',
  1177. 'tag' => 'tag1',
  1178. 'created' => '2007-03-18 12:22:23',
  1179. 'updated' => '2007-03-18 12:24:31'
  1180. ),
  1181. array(
  1182. 'id' => '2',
  1183. 'tag' => 'tag2',
  1184. 'created' => '2007-03-18 12:24:23',
  1185. 'updated' => '2007-03-18 12:26:31'
  1186. )));
  1187. $this->assertEquals($expected, $result);
  1188. $data = array('Article' => array('id' => '2'), 'Tag' => array('Tag' => array(2, 3)));
  1189. $result = $TestModel->set($data);
  1190. $this->assertFalse(empty($result));
  1191. $result = $TestModel->save();
  1192. $this->assertFalse(empty($result));
  1193. $TestModel->unbindModel(array(
  1194. 'belongsTo' => array('User'),
  1195. 'hasMany' => array('Comment')
  1196. ));
  1197. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1198. $expected = array(
  1199. 'Article' => array(
  1200. 'id' => '2',
  1201. 'user_id' => '3',
  1202. 'title' => 'New Second Article',
  1203. 'body' => 'Second Article Body'
  1204. ),
  1205. 'Tag' => array(
  1206. array(
  1207. 'id' => '2',
  1208. 'tag' => 'tag2',
  1209. 'created' => '2007-03-18 12:24:23',
  1210. 'updated' => '2007-03-18 12:26:31'
  1211. ),
  1212. array(
  1213. 'id' => '3',
  1214. 'tag' => 'tag3',
  1215. 'created' => '2007-03-18 12:26:23',
  1216. 'updated' => '2007-03-18 12:28:31'
  1217. )));
  1218. $this->assertEquals($expected, $result);
  1219. $data = array('Tag' => array('Tag' => array(1, 2, 3)));
  1220. $result = $TestModel->set($data);
  1221. $this->assertFalse(empty($result));
  1222. $result = $TestModel->save();
  1223. $this->assertFalse(empty($result));
  1224. $TestModel->unbindModel(array(
  1225. 'belongsTo' => array('User'),
  1226. 'hasMany' => array('Comment')
  1227. ));
  1228. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1229. $expected = array(
  1230. 'Article' => array(
  1231. 'id' => '2',
  1232. 'user_id' => '3',
  1233. 'title' => 'New Second Article',
  1234. 'body' => 'Second Article Body'
  1235. ),
  1236. 'Tag' => array(
  1237. array(
  1238. 'id' => '1',
  1239. 'tag' => 'tag1',
  1240. 'created' => '2007-03-18 12:22:23',
  1241. 'updated' => '2007-03-18 12:24:31'
  1242. ),
  1243. array(
  1244. 'id' => '2',
  1245. 'tag' => 'tag2',
  1246. 'created' => '2007-03-18 12:24:23',
  1247. 'updated' => '2007-03-18 12:26:31'
  1248. ),
  1249. array(
  1250. 'id' => '3',
  1251. 'tag' => 'tag3',
  1252. 'created' => '2007-03-18 12:26:23',
  1253. 'updated' => '2007-03-18 12:28:31'
  1254. )));
  1255. $this->assertEquals($expected, $result);
  1256. $data = array('Tag' => array('Tag' => array()));
  1257. $result = $TestModel->set($data);
  1258. $this->assertFalse(empty($result));
  1259. $result = $TestModel->save();
  1260. $this->assertFalse(empty($result));
  1261. $data = array('Tag' => array('Tag' => ''));
  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' => 'New Second Article',
  1276. 'body' => 'Second Article Body'
  1277. ),
  1278. 'Tag' => array()
  1279. );
  1280. $this->assertEquals($expected, $result);
  1281. $data = array('Tag' => array('Tag' => array(2, 3)));
  1282. $result = $TestModel->set($data);
  1283. $this->assertFalse(empty($result));
  1284. $result = $TestModel->save();
  1285. $this->assertFalse(empty($result));
  1286. $TestModel->unbindModel(array(
  1287. 'belongsTo' => array('User'),
  1288. 'hasMany' => array('Comment')
  1289. ));
  1290. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1291. $expected = array(
  1292. 'Article' => array(
  1293. 'id' => '2',
  1294. 'user_id' => '3',
  1295. 'title' => 'New Second Article',
  1296. 'body' => 'Second Article Body'
  1297. ),
  1298. 'Tag' => array(
  1299. array(
  1300. 'id' => '2',
  1301. 'tag' => 'tag2',
  1302. 'created' => '2007-03-18 12:24:23',
  1303. 'updated' => '2007-03-18 12:26:31'
  1304. ),
  1305. array(
  1306. 'id' => '3',
  1307. 'tag' => 'tag3',
  1308. 'created' => '2007-03-18 12:26:23',
  1309. 'updated' => '2007-03-18 12:28:31'
  1310. )));
  1311. $this->assertEquals($expected, $result);
  1312. $data = array(
  1313. 'Tag' => array(
  1314. 'Tag' => array(1, 2)
  1315. ),
  1316. 'Article' => array(
  1317. 'id' => '2',
  1318. 'title' => 'New Second Article'
  1319. ));
  1320. $result = $TestModel->set($data);
  1321. $this->assertFalse(empty($result));
  1322. $result = $TestModel->save();
  1323. $this->assertFalse(empty($result));
  1324. $TestModel->unbindModel(array(
  1325. 'belongsTo' => array('User'),
  1326. 'hasMany' => array('Comment')
  1327. ));
  1328. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1329. $expected = array(
  1330. 'Article' => array(
  1331. 'id' => '2',
  1332. 'user_id' => '3',
  1333. 'title' => 'New Second Article',
  1334. 'body' => 'Second Article Body'
  1335. ),
  1336. 'Tag' => array(
  1337. array(
  1338. 'id' => '1',
  1339. 'tag' => 'tag1',
  1340. 'created' => '2007-03-18 12:22:23',
  1341. 'updated' => '2007-03-18 12:24:31'
  1342. ),
  1343. array(
  1344. 'id' => '2',
  1345. 'tag' => 'tag2',
  1346. 'created' => '2007-03-18 12:24:23',
  1347. 'updated' => '2007-03-18 12:26:31'
  1348. )));
  1349. $this->assertEquals($expected, $result);
  1350. $data = array(
  1351. 'Tag' => array(
  1352. 'Tag' => array(1, 2)
  1353. ),
  1354. 'Article' => array(
  1355. 'id' => '2',
  1356. 'title' => 'New Second Article Title'
  1357. ));
  1358. $result = $TestModel->set($data);
  1359. $this->assertFalse(empty($result));
  1360. $result = $TestModel->save();
  1361. $this->assertFalse(empty($result));
  1362. $TestModel->unbindModel(array(
  1363. 'belongsTo' => array('User'),
  1364. 'hasMany' => array('Comment')
  1365. ));
  1366. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1367. $expected = array(
  1368. 'Article' => array(
  1369. 'id' => '2',
  1370. 'user_id' => '3',
  1371. 'title' => 'New Second Article Title',
  1372. 'body' => 'Second Article Body'
  1373. ),
  1374. 'Tag' => array(
  1375. array(
  1376. 'id' => '1',
  1377. 'tag' => 'tag1',
  1378. 'created' => '2007-03-18 12:22:23',
  1379. 'updated' => '2007-03-18 12:24:31'
  1380. ),
  1381. array(
  1382. 'id' => '2',
  1383. 'tag' => 'tag2',
  1384. 'created' => '2007-03-18 12:24:23',
  1385. 'updated' => '2007-03-18 12:26:31'
  1386. )
  1387. )
  1388. );
  1389. $this->assertEquals($expected, $result);
  1390. $data = array(
  1391. 'Tag' => array(
  1392. 'Tag' => array(2, 3)
  1393. ),
  1394. 'Article' => array(
  1395. 'id' => '2',
  1396. 'title' => 'Changed Second Article'
  1397. ));
  1398. $result = $TestModel->set($data);
  1399. $this->assertFalse(empty($result));
  1400. $result = $TestModel->save();
  1401. $this->assertFalse(empty($result));
  1402. $TestModel->unbindModel(array(
  1403. 'belongsTo' => array('User'),
  1404. 'hasMany' => array('Comment')
  1405. ));
  1406. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1407. $expected = array(
  1408. 'Article' => array(
  1409. 'id' => '2',
  1410. 'user_id' => '3',
  1411. 'title' => 'Changed Second Article',
  1412. 'body' => 'Second Article Body'
  1413. ),
  1414. 'Tag' => array(
  1415. array(
  1416. 'id' => '2',
  1417. 'tag' => 'tag2',
  1418. 'created' => '2007-03-18 12:24:23',
  1419. 'updated' => '2007-03-18 12:26:31'
  1420. ),
  1421. array(
  1422. 'id' => '3',
  1423. 'tag' => 'tag3',
  1424. 'created' => '2007-03-18 12:26:23',
  1425. 'updated' => '2007-03-18 12:28:31'
  1426. )
  1427. )
  1428. );
  1429. $this->assertEquals($expected, $result);
  1430. $data = array(
  1431. 'Tag' => array(
  1432. 'Tag' => array(1, 3)
  1433. ),
  1434. 'Article' => array('id' => '2'),
  1435. );
  1436. $result = $TestModel->set($data);
  1437. $this->assertFalse(empty($result));
  1438. $result = $TestModel->save();
  1439. $this->assertFalse(empty($result));
  1440. $TestModel->unbindModel(array(
  1441. 'belongsTo' => array('User'),
  1442. 'hasMany' => array('Comment')
  1443. ));
  1444. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1445. $expected = array(
  1446. 'Article' => array(
  1447. 'id' => '2',
  1448. 'user_id' => '3',
  1449. 'title' => 'Changed Second Article',
  1450. 'body' => 'Second Article Body'
  1451. ),
  1452. 'Tag' => array(
  1453. array(
  1454. 'id' => '1',
  1455. 'tag' => 'tag1',
  1456. 'created' => '2007-03-18 12:22:23',
  1457. 'updated' => '2007-03-18 12:24:31'
  1458. ),
  1459. array(
  1460. 'id' => '3',
  1461. 'tag' => 'tag3',
  1462. 'created' => '2007-03-18 12:26:23',
  1463. 'updated' => '2007-03-18 12:28:31'
  1464. )));
  1465. $this->assertEquals($expected, $result);
  1466. $data = array(
  1467. 'Article' => array(
  1468. 'id' => 10,
  1469. 'user_id' => '2',
  1470. 'title' => 'New Article With Tags and fieldList',
  1471. 'body' => 'New Article Body with Tags and fieldList',
  1472. 'created' => '2007-03-18 14:55:23',
  1473. 'updated' => '2007-03-18 14:57:31'
  1474. ),
  1475. 'Tag' => array(
  1476. 'Tag' => array(1, 2, 3)
  1477. )
  1478. );
  1479. $result = $TestModel->create()
  1480. && $TestModel->save($data, true, array('user_id', 'title', 'published'));
  1481. $this->assertFalse(empty($result));
  1482. $TestModel->unbindModel(array(
  1483. 'belongsTo' => array('User'),
  1484. 'hasMany' => array('Comment')
  1485. ));
  1486. $result = $TestModel->read();
  1487. $expected = array(
  1488. 'Article' => array(
  1489. 'id' => 4,
  1490. 'user_id' => 2,
  1491. 'title' => 'New Article With Tags and fieldList',
  1492. 'body' => '',
  1493. 'published' => 'N',
  1494. 'created' => '',
  1495. 'updated' => ''
  1496. ),
  1497. 'Tag' => array(
  1498. 0 => array(
  1499. 'id' => 1,
  1500. 'tag' => 'tag1',
  1501. 'created' => '2007-03-18 12:22:23',
  1502. 'updated' => '2007-03-18 12:24:31'
  1503. ),
  1504. 1 => array(
  1505. 'id' => 2,
  1506. 'tag' => 'tag2',
  1507. 'created' => '2007-03-18 12:24:23',
  1508. 'updated' => '2007-03-18 12:26:31'
  1509. ),
  1510. 2 => array(
  1511. 'id' => 3,
  1512. 'tag' => 'tag3',
  1513. 'created' => '2007-03-18 12:26:23',
  1514. 'updated' => '2007-03-18 12:28:31'
  1515. )));
  1516. $this->assertEquals($expected, $result);
  1517. $this->loadFixtures('JoinA', 'JoinC', 'JoinAC', 'JoinB', 'JoinAB');
  1518. $TestModel = new JoinA();
  1519. $TestModel->hasBelongsToMany = array('JoinC' => array('unique' => true));
  1520. $data = array(
  1521. 'JoinA' => array(
  1522. 'id' => 1,
  1523. 'name' => 'Join A 1',
  1524. 'body' => 'Join A 1 Body',
  1525. ),
  1526. 'JoinC' => array(
  1527. 'JoinC' => array(
  1528. array('join_c_id' => 2, 'other' => 'new record'),
  1529. array('join_c_id' => 3, 'other' => 'new record')
  1530. )
  1531. )
  1532. );
  1533. $TestModel->save($data);
  1534. $result = $TestModel->read(null, 1);
  1535. $expected = array(4, 5);
  1536. $this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.id'));
  1537. $expected = array('new record', 'new record');
  1538. $this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.other'));
  1539. }
  1540. /**
  1541. * testSaveHabtmNoPrimaryData method
  1542. *
  1543. * @return void
  1544. */
  1545. public function testSaveHabtmNoPrimaryData() {
  1546. $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
  1547. $TestModel = new Article();
  1548. $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')), false);
  1549. $result = $TestModel->findById(2);
  1550. $expected = array(
  1551. 'Article' => array(
  1552. 'id' => '2',
  1553. 'user_id' => '3',
  1554. 'title' => 'Second Article',
  1555. 'body' => 'Second Article Body',
  1556. 'published' => 'Y',
  1557. 'created' => '2007-03-18 10:41:23',
  1558. 'updated' => '2007-03-18 10:43:31'
  1559. ),
  1560. 'Tag' => array(
  1561. array(
  1562. 'id' => '1',
  1563. 'tag' => 'tag1',
  1564. 'created' => '2007-03-18 12:22:23',
  1565. 'updated' => '2007-03-18 12:24:31'
  1566. ),
  1567. array(
  1568. 'id' => '3',
  1569. 'tag' => 'tag3',
  1570. 'created' => '2007-03-18 12:26:23',
  1571. 'updated' => '2007-03-18 12:28:31'
  1572. )
  1573. )
  1574. );
  1575. $this->assertEquals($expected, $result);
  1576. $TestModel->id = 2;
  1577. $data = array('Tag' => array('Tag' => array(2)));
  1578. $result = $TestModel->save($data);
  1579. $this->assertEquals($data['Tag'], $result['Tag']);
  1580. $result = $TestModel->findById(2);
  1581. $expected = array(
  1582. 'Article' => array(
  1583. 'id' => '2',
  1584. 'user_id' => '3',
  1585. 'title' => 'Second Article',
  1586. 'body' => 'Second Article Body',
  1587. 'published' => 'Y',
  1588. 'created' => '2007-03-18 10:41:23',
  1589. 'updated' => self::date()
  1590. ),
  1591. 'Tag' => array(
  1592. array(
  1593. 'id' => '2',
  1594. 'tag' => 'tag2',
  1595. 'created' => '2007-03-18 12:24:23',
  1596. 'updated' => '2007-03-18 12:26:31'
  1597. )
  1598. )
  1599. );
  1600. $this->assertEquals($expected, $result);
  1601. $this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio');
  1602. $TestModel = new Portfolio();
  1603. $result = $TestModel->findById(2);
  1604. $expected = array(
  1605. 'Portfolio' => array(
  1606. 'id' => 2,
  1607. 'seller_id' => 1,
  1608. 'name' => 'Portfolio 2'
  1609. ),
  1610. 'Item' => array(
  1611. array(
  1612. 'id' => 2,
  1613. 'syfile_id' => 2,
  1614. 'published' => '',
  1615. 'name' => 'Item 2',
  1616. 'ItemsPortfolio' => array(
  1617. 'id' => 2,
  1618. 'item_id' => 2,
  1619. 'portfolio_id' => 2
  1620. )
  1621. ),
  1622. array(
  1623. 'id' => 6,
  1624. 'syfile_id' => 6,
  1625. 'published' => '',
  1626. 'name' => 'Item 6',
  1627. 'ItemsPortfolio' => array(
  1628. 'id' => 6,
  1629. 'item_id' => 6,
  1630. 'portfolio_id' => 2
  1631. )
  1632. )
  1633. )
  1634. );
  1635. $this->assertEquals($expected, $result);
  1636. $data = array('Item' => array('Item' => array(1, 2)));
  1637. $TestModel->id = 2;
  1638. $TestModel->save($data);
  1639. $result = $TestModel->findById(2);
  1640. $result['Item'] = Hash::sort($result['Item'], '{n}.id', 'asc');
  1641. $expected = array(
  1642. 'Portfolio' => array(
  1643. 'id' => 2,
  1644. 'seller_id' => 1,
  1645. 'name' => 'Portfolio 2'
  1646. ),
  1647. 'Item' => array(
  1648. array(
  1649. 'id' => 1,
  1650. 'syfile_id' => 1,
  1651. 'published' => '',
  1652. 'name' => 'Item 1',
  1653. 'ItemsPortfolio' => array(
  1654. 'id' => 7,
  1655. 'item_id' => 1,
  1656. 'portfolio_id' => 2
  1657. )
  1658. ),
  1659. array(
  1660. 'id' => 2,
  1661. 'syfile_id' => 2,
  1662. 'published' => '',
  1663. 'name' => 'Item 2',
  1664. 'ItemsPortfolio' => array(
  1665. 'id' => 8,
  1666. 'item_id' => 2,
  1667. 'portfolio_id' => 2
  1668. )
  1669. )
  1670. )
  1671. );
  1672. $this->assertEquals($expected, $result);
  1673. }
  1674. /**
  1675. * testSaveHabtmCustomKeys method
  1676. *
  1677. * @return void
  1678. */
  1679. public function testSaveHabtmCustomKeys() {
  1680. $this->loadFixtures('Story', 'StoriesTag', 'Tag');
  1681. $Story = new Story();
  1682. $data = array(
  1683. 'Story' => array('story' => '1'),
  1684. 'Tag' => array(
  1685. 'Tag' => array(2, 3)
  1686. ));
  1687. $result = $Story->set($data);
  1688. $this->assertFalse(empty($result));
  1689. $result = $Story->save();
  1690. $this->assertFalse(empty($result));
  1691. $result = $Story->find('all', array('order' => array('Story.story')));
  1692. $expected = array(
  1693. array(
  1694. 'Story' => array(
  1695. 'story' => 1,
  1696. 'title' => 'First Story'
  1697. ),
  1698. 'Tag' => array(
  1699. array(
  1700. 'id' => 2,
  1701. 'tag' => 'tag2',
  1702. 'created' => '2007-03-18 12:24:23',
  1703. 'updated' => '2007-03-18 12:26:31'
  1704. ),
  1705. array(
  1706. 'id' => 3,
  1707. 'tag' => 'tag3',
  1708. 'created' => '2007-03-18 12:26:23',
  1709. 'updated' => '2007-03-18 12:28:31'
  1710. ))),
  1711. array(
  1712. 'Story' => array(
  1713. 'story' => 2,
  1714. 'title' => 'Second Story'
  1715. ),
  1716. 'Tag' => array()
  1717. ));
  1718. $this->assertEquals($expected, $result);
  1719. }
  1720. /**
  1721. * test that saving habtm records respects conditions set in the 'conditions' key
  1722. * for the association.
  1723. *
  1724. * @return void
  1725. */
  1726. public function testHabtmSaveWithConditionsInAssociation() {
  1727. $this->loadFixtures('JoinThing', 'Something', 'SomethingElse');
  1728. $Something = new Something();
  1729. $Something->unbindModel(array('hasAndBelongsToMany' => array('SomethingElse')), false);
  1730. $Something->bindModel(array(
  1731. 'hasAndBelongsToMany' => array(
  1732. 'DoomedSomethingElse' => array(
  1733. 'className' => 'SomethingElse',
  1734. 'joinTable' => 'join_things',
  1735. 'conditions' => array('JoinThing.doomed' => true),
  1736. 'unique' => true
  1737. ),
  1738. 'NotDoomedSomethingElse' => array(
  1739. 'className' => 'SomethingElse',
  1740. 'joinTable' => 'join_things',
  1741. 'conditions' => array('JoinThing.doomed' => 0),
  1742. 'unique' => true
  1743. )
  1744. )
  1745. ), false);
  1746. $result = $Something->read(null, 1);
  1747. $this->assertTrue(empty($result['NotDoomedSomethingElse']));
  1748. $this->assertEquals(1, count($result['DoomedSomethingElse']));
  1749. $data = array(
  1750. 'Something' => array('id' => 1),
  1751. 'NotDoomedSomethingElse' => array(
  1752. 'NotDoomedSomethingElse' => array(
  1753. array('something_else_id' => 2, 'doomed' => 0),
  1754. array('something_else_id' => 3, 'doomed' => 0)
  1755. )
  1756. )
  1757. );
  1758. $Something->create($data);
  1759. $result = $Something->save();
  1760. $this->assertFalse(empty($result));
  1761. $result = $Something->read(null, 1);
  1762. $this->assertEquals(2, count($result['NotDoomedSomethingElse']));
  1763. $this->assertEquals(1, count($result['DoomedSomethingElse']));
  1764. }
  1765. /**
  1766. * testHabtmSaveKeyResolution method
  1767. *
  1768. * @return void
  1769. */
  1770. public function testHabtmSaveKeyResolution() {
  1771. $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies');
  1772. $ThePaper = new ThePaper();
  1773. $ThePaper->id = 1;
  1774. $ThePaper->save(array('Monkey' => array(2, 3)));
  1775. $result = $ThePaper->findById(1);
  1776. $expected = array(
  1777. array(
  1778. 'id' => '2',
  1779. 'device_type_id' => '1',
  1780. 'name' => 'Device 2',
  1781. 'typ' => '1'
  1782. ),
  1783. array(
  1784. 'id' => '3',
  1785. 'device_type_id' => '1',
  1786. 'name' => 'Device 3',
  1787. 'typ' => '2'
  1788. ));
  1789. $this->assertEquals($expected, $result['Monkey']);
  1790. $ThePaper->id = 2;
  1791. $ThePaper->save(array('Monkey' => array(1, 2, 3)));
  1792. $result = $ThePaper->findById(2);
  1793. $expected = array(
  1794. array(
  1795. 'id' => '1',
  1796. 'device_type_id' => '1',
  1797. 'name' => 'Device 1',
  1798. 'typ' => '1'
  1799. ),
  1800. array(
  1801. 'id' => '2',
  1802. 'device_type_id' => '1',
  1803. 'name' => 'Device 2',
  1804. 'typ' => '1'
  1805. ),
  1806. array(
  1807. 'id' => '3',
  1808. 'device_type_id' => '1',
  1809. 'name' => 'Device 3',
  1810. 'typ' => '2'
  1811. ));
  1812. $this->assertEquals($expected, $result['Monkey']);
  1813. $ThePaper->id = 2;
  1814. $ThePaper->save(array('Monkey' => array(1, 3)));
  1815. $result = $ThePaper->findById(2);
  1816. $expected = array(
  1817. array(
  1818. 'id' => '1',
  1819. 'device_type_id' => '1',
  1820. 'name' => 'Device 1',
  1821. 'typ' => '1'
  1822. ),
  1823. array(
  1824. 'id' => '3',
  1825. 'device_type_id' => '1',
  1826. 'name' => 'Device 3',
  1827. 'typ' => '2'
  1828. ));
  1829. $this->assertEquals($expected, $result['Monkey']);
  1830. $result = $ThePaper->findById(1);
  1831. $expected = array(
  1832. array(
  1833. 'id' => '2',
  1834. 'device_type_id' => '1',
  1835. 'name' => 'Device 2',
  1836. 'typ' => '1'
  1837. ),
  1838. array(
  1839. 'id' => '3',
  1840. 'device_type_id' => '1',
  1841. 'name' => 'Device 3',
  1842. 'typ' => '2'
  1843. ));
  1844. $this->assertEquals($expected, $result['Monkey']);
  1845. }
  1846. /**
  1847. * testCreationOfEmptyRecord method
  1848. *
  1849. * @return void
  1850. */
  1851. public function testCreationOfEmptyRecord() {
  1852. $this->loadFixtures('Author');
  1853. $TestModel = new Author();
  1854. $this->assertEquals(4, $TestModel->find('count'));
  1855. $TestModel->deleteAll(true, false, false);
  1856. $this->assertEquals(0, $TestModel->find('count'));
  1857. $result = $TestModel->save();
  1858. $this->assertTrue(isset($result['Author']['created']));
  1859. $this->assertTrue(isset($result['Author']['updated']));
  1860. $this->assertEquals(1, $TestModel->find('count'));
  1861. }
  1862. /**
  1863. * testCreateWithPKFiltering method
  1864. *
  1865. * @return void
  1866. */
  1867. public function testCreateWithPKFiltering() {
  1868. $TestModel = new Article();
  1869. $data = array(
  1870. 'id' => 5,
  1871. 'user_id' => 2,
  1872. 'title' => 'My article',
  1873. 'body' => 'Some text'
  1874. );
  1875. $result = $TestModel->create($data);
  1876. $expected = array(
  1877. 'Article' => array(
  1878. 'published' => 'N',
  1879. 'id' => 5,
  1880. 'user_id' => 2,
  1881. 'title' => 'My article',
  1882. 'body' => 'Some text'
  1883. ));
  1884. $this->assertEquals($expected, $result);
  1885. $this->assertEquals(5, $TestModel->id);
  1886. $result = $TestModel->create($data, true);
  1887. $expected = array(
  1888. 'Article' => array(
  1889. 'published' => 'N',
  1890. 'id' => false,
  1891. 'user_id' => 2,
  1892. 'title' => 'My article',
  1893. 'body' => 'Some text'
  1894. ));
  1895. $this->assertEquals($expected, $result);
  1896. $this->assertFalse($TestModel->id);
  1897. $result = $TestModel->create(array('Article' => $data), true);
  1898. $expected = array(
  1899. 'Article' => array(
  1900. 'published' => 'N',
  1901. 'id' => false,
  1902. 'user_id' => 2,
  1903. 'title' => 'My article',
  1904. 'body' => 'Some text'
  1905. ));
  1906. $this->assertEquals($expected, $result);
  1907. $this->assertFalse($TestModel->id);
  1908. $data = array(
  1909. 'id' => 6,
  1910. 'user_id' => 2,
  1911. 'title' => 'My article',
  1912. 'body' => 'Some text',
  1913. 'created' => '1970-01-01 00:00:00',
  1914. 'updated' => '1970-01-01 12:00:00',
  1915. 'modified' => '1970-01-01 12:00:00'
  1916. );
  1917. $result = $TestModel->create($data);
  1918. $expected = array(
  1919. 'Article' => array(
  1920. 'published' => 'N',
  1921. 'id' => 6,
  1922. 'user_id' => 2,
  1923. 'title' => 'My article',
  1924. 'body' => 'Some text',
  1925. 'created' => '1970-01-01 00:00:00',
  1926. 'updated' => '1970-01-01 12:00:00',
  1927. 'modified' => '1970-01-01 12:00:00'
  1928. ));
  1929. $this->assertEquals($expected, $result);
  1930. $this->assertEquals(6, $TestModel->id);
  1931. $result = $TestModel->create(array(
  1932. 'Article' => array_diff_key($data, array(
  1933. 'created' => true,
  1934. 'updated' => true,
  1935. 'modified' => true
  1936. ))), true);
  1937. $expected = array(
  1938. 'Article' => array(
  1939. 'published' => 'N',
  1940. 'id' => false,
  1941. 'user_id' => 2,
  1942. 'title' => 'My article',
  1943. 'body' => 'Some text'
  1944. ));
  1945. $this->assertEquals($expected, $result);
  1946. $this->assertFalse($TestModel->id);
  1947. }
  1948. /**
  1949. * testCreationWithMultipleData method
  1950. *
  1951. * @return void
  1952. */
  1953. public function testCreationWithMultipleData() {
  1954. $this->loadFixtures('Article', 'Comment');
  1955. $Article = new Article();
  1956. $Comment = new Comment();
  1957. $articles = $Article->find('all', array(
  1958. 'fields' => array('id', 'title'),
  1959. 'recursive' => -1,
  1960. 'order' => array('Article.id' => 'ASC')
  1961. ));
  1962. $expected = array(
  1963. array('Article' => array(
  1964. 'id' => 1,
  1965. 'title' => 'First Article'
  1966. )),
  1967. array('Article' => array(
  1968. 'id' => 2,
  1969. 'title' => 'Second Article'
  1970. )),
  1971. array('Article' => array(
  1972. 'id' => 3,
  1973. 'title' => 'Third Article'
  1974. )));
  1975. $this->assertEquals($expected, $articles);
  1976. $comments = $Comment->find('all', array(
  1977. 'fields' => array('id', 'article_id', 'user_id', 'comment', 'published'),
  1978. 'recursive' => -1,
  1979. 'order' => array('Comment.id' => 'ASC')
  1980. ));
  1981. $expected = array(
  1982. array('Comment' => array(
  1983. 'id' => 1,
  1984. 'article_id' => 1,
  1985. 'user_id' => 2,
  1986. 'comment' => 'First Comment for First Article',
  1987. 'published' => 'Y'
  1988. )),
  1989. array('Comment' => array(
  1990. 'id' => 2,
  1991. 'article_id' => 1,
  1992. 'user_id' => 4,
  1993. 'comment' => 'Second Comment for First Article',
  1994. 'published' => 'Y'
  1995. )),
  1996. array('Comment' => array(
  1997. 'id' => 3,
  1998. 'article_id' => 1,
  1999. 'user_id' => 1,
  2000. 'comment' => 'Third Comment for First Article',
  2001. 'published' => 'Y'
  2002. )),
  2003. array('Comment' => array(
  2004. 'id' => 4,
  2005. 'article_id' => 1,
  2006. 'user_id' => 1,
  2007. 'comment' => 'Fourth Comment for First Article',
  2008. 'published' => 'N'
  2009. )),
  2010. array('Comment' => array(
  2011. 'id' => 5,
  2012. 'article_id' => 2,
  2013. 'user_id' => 1,
  2014. 'comment' => 'First Comment for Second Article',
  2015. 'published' => 'Y'
  2016. )),
  2017. array('Comment' => array(
  2018. 'id' => 6,
  2019. 'article_id' => 2,
  2020. 'user_id' => 2,
  2021. 'comment' => 'Second Comment for Second Article',
  2022. 'published' => 'Y'
  2023. )));
  2024. $this->assertEquals($expected, $comments);
  2025. $data = array(
  2026. 'Comment' => array(
  2027. 'article_id' => 2,
  2028. 'user_id' => 4,
  2029. 'comment' => 'Brand New Comment',
  2030. 'published' => 'N'
  2031. ),
  2032. 'Article' => array(
  2033. 'id' => 2,
  2034. 'title' => 'Second Article Modified'
  2035. ));
  2036. $result = $Comment->create($data);
  2037. $this->assertFalse(empty($result));
  2038. $result = $Comment->save();
  2039. $this->assertFalse(empty($result));
  2040. $articles = $Article->find('all', array(
  2041. 'fields' => array('id', 'title'),
  2042. 'recursive' => -1,
  2043. 'order' => array('Article.id' => 'ASC')
  2044. ));
  2045. $expected = array(
  2046. array('Article' => array(
  2047. 'id' => 1,
  2048. 'title' => 'First Article'
  2049. )),
  2050. array('Article' => array(
  2051. 'id' => 2,
  2052. 'title' => 'Second Article'
  2053. )),
  2054. array('Article' => array(
  2055. 'id' => 3,
  2056. 'title' => 'Third Article'
  2057. )));
  2058. $this->assertEquals($expected, $articles);
  2059. $comments = $Comment->find('all', array(
  2060. 'fields' => array('id', 'article_id', 'user_id', 'comment', 'published'),
  2061. 'recursive' => -1,
  2062. 'order' => array('Comment.id' => 'ASC')
  2063. ));
  2064. $expected = array(
  2065. array('Comment' => array(
  2066. 'id' => 1,
  2067. 'article_id' => 1,
  2068. 'user_id' => 2,
  2069. 'comment' => 'First Comment for First Article',
  2070. 'published' => 'Y'
  2071. )),
  2072. array('Comment' => array(
  2073. 'id' => 2,
  2074. 'article_id' => 1,
  2075. 'user_id' => 4,
  2076. 'comment' => 'Second Comment for First Article',
  2077. 'published' => 'Y'
  2078. )),
  2079. array('Comment' => array(
  2080. 'id' => 3,
  2081. 'article_id' => 1,
  2082. 'user_id' => 1,
  2083. 'comment' => 'Third Comment for First Article',
  2084. 'published' => 'Y'
  2085. )),
  2086. array('Comment' => array(
  2087. 'id' => 4,
  2088. 'article_id' => 1,
  2089. 'user_id' => 1,
  2090. 'comment' => 'Fourth Comment for First Article',
  2091. 'published' => 'N'
  2092. )),
  2093. array('Comment' => array(
  2094. 'id' => 5,
  2095. 'article_id' => 2,
  2096. 'user_id' => 1,
  2097. 'comment' => 'First Comment for Second Article',
  2098. 'published' => 'Y'
  2099. )),
  2100. array('Comment' => array(
  2101. 'id' => 6,
  2102. 'article_id' => 2,
  2103. 'user_id' => 2, 'comment' =>
  2104. 'Second Comment for Second Article',
  2105. 'published' => 'Y'
  2106. )),
  2107. array('Comment' => array(
  2108. 'id' => 7,
  2109. 'article_id' => 2,
  2110. 'user_id' => 4,
  2111. 'comment' => 'Brand New Comment',
  2112. 'published' => 'N'
  2113. )));
  2114. $this->assertEquals($expected, $comments);
  2115. }
  2116. /**
  2117. * testCreationWithMultipleDataSameModel method
  2118. *
  2119. * @return void
  2120. */
  2121. public function testCreationWithMultipleDataSameModel() {
  2122. $this->loadFixtures('Article');
  2123. $Article = new Article();
  2124. $result = $Article->field('title', array('id' => 1));
  2125. $this->assertEquals('First Article', $result);
  2126. $data = array(
  2127. 'Article' => array(
  2128. 'user_id' => 2,
  2129. 'title' => 'Brand New Article',
  2130. 'body' => 'Brand New Article Body',
  2131. 'published' => 'Y'
  2132. ),
  2133. 'SecondaryArticle' => array(
  2134. 'id' => 1
  2135. ));
  2136. $Article->create();
  2137. $result = $Article->save($data);
  2138. $this->assertFalse(empty($result));
  2139. $result = $Article->getInsertID();
  2140. $this->assertTrue(!empty($result));
  2141. $result = $Article->field('title', array('id' => 1));
  2142. $this->assertEquals('First Article', $result);
  2143. $articles = $Article->find('all', array(
  2144. 'fields' => array('id', 'title'),
  2145. 'recursive' => -1,
  2146. 'order' => array('Article.id' => 'ASC')
  2147. ));
  2148. $expected = array(
  2149. array('Article' => array(
  2150. 'id' => 1,
  2151. 'title' => 'First Article'
  2152. )),
  2153. array('Article' => array(
  2154. 'id' => 2,
  2155. 'title' => 'Second Article'
  2156. )),
  2157. array('Article' => array(
  2158. 'id' => 3,
  2159. 'title' => 'Third Article'
  2160. )),
  2161. array('Article' => array(
  2162. 'id' => 4,
  2163. 'title' => 'Brand New Article'
  2164. )));
  2165. $this->assertEquals($expected, $articles);
  2166. }
  2167. /**
  2168. * testCreationWithMultipleDataSameModelManualInstances method
  2169. *
  2170. * @return void
  2171. */
  2172. public function testCreationWithMultipleDataSameModelManualInstances() {
  2173. $this->loadFixtures('PrimaryModel');
  2174. $Primary = new PrimaryModel();
  2175. $result = $Primary->field('primary_name', array('id' => 1));
  2176. $this->assertEquals('Primary Name Existing', $result);
  2177. $data = array(
  2178. 'PrimaryModel' => array(
  2179. 'primary_name' => 'Primary Name New'
  2180. ),
  2181. 'SecondaryModel' => array(
  2182. 'id' => array(1)
  2183. ));
  2184. $Primary->create();
  2185. $result = $Primary->save($data);
  2186. $this->assertFalse(empty($result));
  2187. $result = $Primary->field('primary_name', array('id' => 1));
  2188. $this->assertEquals('Primary Name Existing', $result);
  2189. $result = $Primary->getInsertID();
  2190. $this->assertTrue(!empty($result));
  2191. $result = $Primary->field('primary_name', array('id' => $result));
  2192. $this->assertEquals('Primary Name New', $result);
  2193. $result = $Primary->find('count');
  2194. $this->assertEquals(2, $result);
  2195. }
  2196. /**
  2197. * testRecordExists method
  2198. *
  2199. * @return void
  2200. */
  2201. public function testRecordExists() {
  2202. $this->loadFixtures('User');
  2203. $TestModel = new User();
  2204. $this->assertFalse($TestModel->exists());
  2205. $TestModel->read(null, 1);
  2206. $this->assertTrue($TestModel->exists());
  2207. $TestModel->create();
  2208. $this->assertFalse($TestModel->exists());
  2209. $TestModel->id = 4;
  2210. $this->assertTrue($TestModel->exists());
  2211. $TestModel = new TheVoid();
  2212. $this->assertFalse($TestModel->exists());
  2213. }
  2214. /**
  2215. * testRecordExistsMissingTable method
  2216. *
  2217. * @expectedException PDOException
  2218. * @return void
  2219. */
  2220. public function testRecordExistsMissingTable() {
  2221. $TestModel = new TheVoid();
  2222. $TestModel->id = 5;
  2223. $TestModel->exists();
  2224. }
  2225. /**
  2226. * testUpdateExisting method
  2227. *
  2228. * @return void
  2229. */
  2230. public function testUpdateExisting() {
  2231. $this->loadFixtures('User', 'Article', 'Comment');
  2232. $TestModel = new User();
  2233. $TestModel->create();
  2234. $TestModel->save(array(
  2235. 'User' => array(
  2236. 'user' => 'some user',
  2237. 'password' => 'some password'
  2238. )));
  2239. $this->assertTrue(is_int($TestModel->id) || (intval($TestModel->id) === 5));
  2240. $id = $TestModel->id;
  2241. $TestModel->save(array(
  2242. 'User' => array(
  2243. 'user' => 'updated user'
  2244. )));
  2245. $this->assertEquals($id, $TestModel->id);
  2246. $result = $TestModel->findById($id);
  2247. $this->assertEquals('updated user', $result['User']['user']);
  2248. $this->assertEquals('some password', $result['User']['password']);
  2249. $Article = new Article();
  2250. $Comment = new Comment();
  2251. $data = array(
  2252. 'Comment' => array(
  2253. 'id' => 1,
  2254. 'comment' => 'First Comment for First Article'
  2255. ),
  2256. 'Article' => array(
  2257. 'id' => 2,
  2258. 'title' => 'Second Article'
  2259. ));
  2260. $result = $Article->save($data);
  2261. $this->assertFalse(empty($result));
  2262. $result = $Comment->save($data);
  2263. $this->assertFalse(empty($result));
  2264. }
  2265. /**
  2266. * test updating records and saving blank values.
  2267. *
  2268. * @return void
  2269. */
  2270. public function testUpdateSavingBlankValues() {
  2271. $this->loadFixtures('Article');
  2272. $Article = new Article();
  2273. $Article->validate = array();
  2274. $Article->create();
  2275. $result = $Article->save(array(
  2276. 'id' => 1,
  2277. 'title' => '',
  2278. 'body' => ''
  2279. ));
  2280. $this->assertTrue((bool)$result);
  2281. $result = $Article->find('first', array('conditions' => array('Article.id' => 1)));
  2282. $this->assertEquals('', $result['Article']['title'], 'Title is not blank');
  2283. $this->assertEquals('', $result['Article']['body'], 'Body is not blank');
  2284. }
  2285. /**
  2286. * testUpdateMultiple method
  2287. *
  2288. * @return void
  2289. */
  2290. public function testUpdateMultiple() {
  2291. $this->loadFixtures('Comment', 'Article', 'User', 'CategoryThread');
  2292. $TestModel = new Comment();
  2293. $result = Hash::extract($TestModel->find('all'), '{n}.Comment.user_id');
  2294. $expected = array('2', '4', '1', '1', '1', '2');
  2295. $this->assertEquals($expected, $result);
  2296. $TestModel->updateAll(array('Comment.user_id' => 5), array('Comment.user_id' => 2));
  2297. $result = Hash::combine($TestModel->find('all'), '{n}.Comment.id', '{n}.Comment.user_id');
  2298. $expected = array(1 => 5, 2 => 4, 3 => 1, 4 => 1, 5 => 1, 6 => 5);
  2299. $this->assertEquals($expected, $result);
  2300. $result = $TestModel->updateAll(
  2301. array('Comment.comment' => "'Updated today'"),
  2302. array('Comment.user_id' => 5)
  2303. );
  2304. $this->assertFalse(empty($result));
  2305. $result = Hash::extract(
  2306. $TestModel->find('all', array(
  2307. 'conditions' => array(
  2308. 'Comment.user_id' => 5
  2309. ))),
  2310. '{n}.Comment.comment'
  2311. );
  2312. $expected = array_fill(0, 2, 'Updated today');
  2313. $this->assertEquals($expected, $result);
  2314. }
  2315. /**
  2316. * testHabtmUuidWithUuidId method
  2317. *
  2318. * @return void
  2319. */
  2320. public function testHabtmUuidWithUuidId() {
  2321. $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolio', 'UuiditemsUuidportfolioNumericid');
  2322. $TestModel = new Uuidportfolio();
  2323. $data = array('Uuidportfolio' => array('name' => 'Portfolio 3'));
  2324. $data['Uuiditem']['Uuiditem'] = array('483798c8-c7cc-430e-8cf9-4fcc40cf8569');
  2325. $TestModel->create($data);
  2326. $TestModel->save();
  2327. $id = $TestModel->id;
  2328. $result = $TestModel->read(null, $id);
  2329. $this->assertEquals(1, count($result['Uuiditem']));
  2330. $this->assertEquals(36, strlen($result['Uuiditem'][0]['UuiditemsUuidportfolio']['id']));
  2331. }
  2332. /**
  2333. * test HABTM saving when join table has no primary key and only 2 columns.
  2334. *
  2335. * @return void
  2336. */
  2337. public function testHabtmSavingWithNoPrimaryKeyUuidJoinTable() {
  2338. $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
  2339. $Fruit = new Fruit();
  2340. $Fruit->FruitsUuidTag->order = null;
  2341. $data = array(
  2342. 'Fruit' => array(
  2343. 'color' => 'Red',
  2344. 'shape' => 'Heart-shaped',
  2345. 'taste' => 'sweet',
  2346. 'name' => 'Strawberry',
  2347. ),
  2348. 'UuidTag' => array(
  2349. 'UuidTag' => array(
  2350. '481fc6d0-b920-43e0-e50f-6d1740cf8569'
  2351. )
  2352. )
  2353. );
  2354. $result = $Fruit->save($data);
  2355. $this->assertFalse(empty($result));
  2356. }
  2357. /**
  2358. * test HABTM saving when join table has no primary key and only 2 columns, no with model is used.
  2359. *
  2360. * @return void
  2361. */
  2362. public function testHabtmSavingWithNoPrimaryKeyUuidJoinTableNoWith() {
  2363. $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
  2364. $Fruit = new FruitNoWith();
  2365. $data = array(
  2366. 'Fruit' => array(
  2367. 'color' => 'Red',
  2368. 'shape' => 'Heart-shaped',
  2369. 'taste' => 'sweet',
  2370. 'name' => 'Strawberry',
  2371. ),
  2372. 'UuidTag' => array(
  2373. 'UuidTag' => array(
  2374. '481fc6d0-b920-43e0-e50f-6d1740cf8569'
  2375. )
  2376. )
  2377. );
  2378. $result = $Fruit->save($data);
  2379. $this->assertFalse(empty($result));
  2380. }
  2381. /**
  2382. * testHabtmUuidWithNumericId method
  2383. *
  2384. * @return void
  2385. */
  2386. public function testHabtmUuidWithNumericId() {
  2387. $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolioNumericid');
  2388. $TestModel = new Uuiditem();
  2389. $data = array('Uuiditem' => array('name' => 'Item 7', 'published' => 0));
  2390. $data['Uuidportfolio']['Uuidportfolio'] = array('480af662-eb8c-47d3-886b-230540cf8569');
  2391. $TestModel->create($data);
  2392. $TestModel->save();
  2393. $id = $TestModel->id;
  2394. $result = $TestModel->read(null, $id);
  2395. $this->assertEquals(1, count($result['Uuidportfolio']));
  2396. }
  2397. /**
  2398. * testSaveMultipleHabtm method
  2399. *
  2400. * @return void
  2401. */
  2402. public function testSaveMultipleHabtm() {
  2403. $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC');
  2404. $TestModel = new JoinA();
  2405. $result = $TestModel->findById(1);
  2406. $expected = array(
  2407. 'JoinA' => array(
  2408. 'id' => 1,
  2409. 'name' => 'Join A 1',
  2410. 'body' => 'Join A 1 Body',
  2411. 'created' => '2008-01-03 10:54:23',
  2412. 'updated' => '2008-01-03 10:54:23'
  2413. ),
  2414. 'JoinB' => array(
  2415. 0 => array(
  2416. 'id' => 2,
  2417. 'name' => 'Join B 2',
  2418. 'created' => '2008-01-03 10:55:02',
  2419. 'updated' => '2008-01-03 10:55:02',
  2420. 'JoinAsJoinB' => array(
  2421. 'id' => 1,
  2422. 'join_a_id' => 1,
  2423. 'join_b_id' => 2,
  2424. 'other' => 'Data for Join A 1 Join B 2',
  2425. 'created' => '2008-01-03 10:56:33',
  2426. 'updated' => '2008-01-03 10:56:33'
  2427. ))),
  2428. 'JoinC' => array(
  2429. 0 => array(
  2430. 'id' => 2,
  2431. 'name' => 'Join C 2',
  2432. 'created' => '2008-01-03 10:56:12',
  2433. 'updated' => '2008-01-03 10:56:12',
  2434. 'JoinAsJoinC' => array(
  2435. 'id' => 1,
  2436. 'join_a_id' => 1,
  2437. 'join_c_id' => 2,
  2438. 'other' => 'Data for Join A 1 Join C 2',
  2439. 'created' => '2008-01-03 10:57:22',
  2440. 'updated' => '2008-01-03 10:57:22'
  2441. ))));
  2442. $this->assertEquals($expected, $result);
  2443. $TestModel->id = 1;
  2444. $data = array(
  2445. 'JoinA' => array(
  2446. 'id' => '1',
  2447. 'name' => 'New name for Join A 1',
  2448. 'updated' => self::date()
  2449. ),
  2450. 'JoinB' => array(
  2451. array(
  2452. 'id' => 1,
  2453. 'join_b_id' => 2,
  2454. 'other' => 'New data for Join A 1 Join B 2',
  2455. 'created' => self::date(),
  2456. 'updated' => self::date()
  2457. )),
  2458. 'JoinC' => array(
  2459. array(
  2460. 'id' => 1,
  2461. 'join_c_id' => 2,
  2462. 'other' => 'New data for Join A 1 Join C 2',
  2463. 'created' => self::date(),
  2464. 'updated' => self::date()
  2465. )));
  2466. $TestModel->set($data);
  2467. $TestModel->save();
  2468. $result = $TestModel->findById(1);
  2469. $expected = array(
  2470. 'JoinA' => array(
  2471. 'id' => 1,
  2472. 'name' => 'New name for Join A 1',
  2473. 'body' => 'Join A 1 Body',
  2474. 'created' => '2008-01-03 10:54:23',
  2475. 'updated' => self::date()
  2476. ),
  2477. 'JoinB' => array(
  2478. 0 => array(
  2479. 'id' => 2,
  2480. 'name' => 'Join B 2',
  2481. 'created' => '2008-01-03 10:55:02',
  2482. 'updated' => '2008-01-03 10:55:02',
  2483. 'JoinAsJoinB' => array(
  2484. 'id' => 1,
  2485. 'join_a_id' => 1,
  2486. 'join_b_id' => 2,
  2487. 'other' => 'New data for Join A 1 Join B 2',
  2488. 'created' => self::date(),
  2489. 'updated' => self::date()
  2490. ))),
  2491. 'JoinC' => array(
  2492. 0 => array(
  2493. 'id' => 2,
  2494. 'name' => 'Join C 2',
  2495. 'created' => '2008-01-03 10:56:12',
  2496. 'updated' => '2008-01-03 10:56:12',
  2497. 'JoinAsJoinC' => array(
  2498. 'id' => 1,
  2499. 'join_a_id' => 1,
  2500. 'join_c_id' => 2,
  2501. 'other' => 'New data for Join A 1 Join C 2',
  2502. 'created' => self::date(),
  2503. 'updated' => self::date()
  2504. ))));
  2505. $this->assertEquals($expected, $result);
  2506. }
  2507. /**
  2508. * testSaveAll method
  2509. *
  2510. * @return void
  2511. */
  2512. public function testSaveAll() {
  2513. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  2514. $TestModel = new Post();
  2515. $result = $TestModel->find('all');
  2516. $this->assertEquals(3, count($result));
  2517. $this->assertFalse(isset($result[3]));
  2518. $TestModel->saveAll(array(
  2519. 'Post' => array(
  2520. 'title' => 'Post with Author',
  2521. 'body' => 'This post will be saved with an author'
  2522. ),
  2523. 'Author' => array(
  2524. 'user' => 'bob',
  2525. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
  2526. )));
  2527. $result = $TestModel->find('all');
  2528. $expected = array(
  2529. 'Post' => array(
  2530. 'id' => '4',
  2531. 'author_id' => '5',
  2532. 'title' => 'Post with Author',
  2533. 'body' => 'This post will be saved with an author',
  2534. 'published' => 'N'
  2535. ),
  2536. 'Author' => array(
  2537. 'id' => '5',
  2538. 'user' => 'bob',
  2539. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
  2540. 'test' => 'working'
  2541. ));
  2542. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  2543. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  2544. $this->assertEquals(self::date(), $result[3]['Author']['created']);
  2545. $this->assertEquals(self::date(), $result[3]['Author']['updated']);
  2546. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  2547. unset($result[3]['Author']['created'], $result[3]['Author']['updated']);
  2548. $this->assertEquals($expected, $result[3]);
  2549. $this->assertEquals(4, count($result));
  2550. $TestModel->deleteAll(true);
  2551. $this->assertEquals(array(), $TestModel->find('all'));
  2552. // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
  2553. $this->db->truncate($TestModel);
  2554. $TestModel->saveAll(array(
  2555. array(
  2556. 'title' => 'Multi-record post 1',
  2557. 'body' => 'First multi-record post',
  2558. 'author_id' => 2
  2559. ),
  2560. array(
  2561. 'title' => 'Multi-record post 2',
  2562. 'body' => 'Second multi-record post',
  2563. 'author_id' => 2
  2564. )));
  2565. $result = $TestModel->find('all', array(
  2566. 'recursive' => -1,
  2567. 'order' => 'Post.id ASC'
  2568. ));
  2569. $expected = array(
  2570. array(
  2571. 'Post' => array(
  2572. 'id' => '1',
  2573. 'author_id' => '2',
  2574. 'title' => 'Multi-record post 1',
  2575. 'body' => 'First multi-record post',
  2576. 'published' => 'N'
  2577. )),
  2578. array(
  2579. 'Post' => array(
  2580. 'id' => '2',
  2581. 'author_id' => '2',
  2582. 'title' => 'Multi-record post 2',
  2583. 'body' => 'Second multi-record post',
  2584. 'published' => 'N'
  2585. )));
  2586. $this->assertEquals(self::date(), $result[0]['Post']['created']);
  2587. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  2588. $this->assertEquals(self::date(), $result[1]['Post']['created']);
  2589. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  2590. unset($result[0]['Post']['created'], $result[0]['Post']['updated']);
  2591. unset($result[1]['Post']['created'], $result[1]['Post']['updated']);
  2592. $this->assertEquals($expected, $result);
  2593. $TestModel = new Comment();
  2594. $result = $TestModel->saveAll(array(
  2595. 'Comment' => array(
  2596. 'article_id' => 2,
  2597. 'user_id' => 2,
  2598. 'comment' => 'New comment with attachment',
  2599. 'published' => 'Y'
  2600. ),
  2601. 'Attachment' => array(
  2602. 'attachment' => 'some_file.tgz'
  2603. )));
  2604. $this->assertFalse(empty($result));
  2605. $result = $TestModel->find('all');
  2606. $expected = array(
  2607. 'id' => '7',
  2608. 'article_id' => '2',
  2609. 'user_id' => '2',
  2610. 'comment' => 'New comment with attachment',
  2611. 'published' => 'Y'
  2612. );
  2613. $this->assertEquals(self::date(), $result[6]['Comment']['created']);
  2614. $this->assertEquals(self::date(), $result[6]['Comment']['updated']);
  2615. unset($result[6]['Comment']['created'], $result[6]['Comment']['updated']);
  2616. $this->assertEquals($expected, $result[6]['Comment']);
  2617. $expected = array(
  2618. 'id' => '2',
  2619. 'comment_id' => '7',
  2620. 'attachment' => 'some_file.tgz'
  2621. );
  2622. $this->assertEquals(self::date(), $result[6]['Attachment']['created']);
  2623. $this->assertEquals(self::date(), $result[6]['Attachment']['updated']);
  2624. unset($result[6]['Attachment']['created'], $result[6]['Attachment']['updated']);
  2625. $this->assertEquals($expected, $result[6]['Attachment']);
  2626. }
  2627. /**
  2628. * Test SaveAll with Habtm relations
  2629. *
  2630. * @return void
  2631. */
  2632. public function testSaveAllHabtm() {
  2633. $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  2634. $data = array(
  2635. 'Article' => array(
  2636. 'user_id' => 1,
  2637. 'title' => 'Article Has and belongs to Many Tags'
  2638. ),
  2639. 'Tag' => array(
  2640. 'Tag' => array(1, 2)
  2641. ),
  2642. 'Comment' => array(
  2643. array(
  2644. 'comment' => 'Article comment',
  2645. 'user_id' => 1
  2646. )));
  2647. $Article = new Article();
  2648. $result = $Article->saveAll($data);
  2649. $this->assertFalse(empty($result));
  2650. $result = $Article->read();
  2651. $this->assertEquals(2, count($result['Tag']));
  2652. $this->assertEquals('tag1', $result['Tag'][0]['tag']);
  2653. $this->assertEquals(1, count($result['Comment']));
  2654. $this->assertEquals(1, count($result['Comment'][0]['comment']));
  2655. }
  2656. /**
  2657. * Test SaveAll with Habtm relations and extra join table fields
  2658. *
  2659. * @return void
  2660. */
  2661. public function testSaveAllHabtmWithExtraJoinTableFields() {
  2662. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  2663. $data = array(
  2664. 'Something' => array(
  2665. 'id' => 4,
  2666. 'title' => 'Extra Fields',
  2667. 'body' => 'Extra Fields Body',
  2668. 'published' => '1'
  2669. ),
  2670. 'SomethingElse' => array(
  2671. array('something_else_id' => 1, 'doomed' => '1'),
  2672. array('something_else_id' => 2, 'doomed' => '0'),
  2673. array('something_else_id' => 3, 'doomed' => '1')
  2674. )
  2675. );
  2676. $Something = new Something();
  2677. $result = $Something->saveAll($data);
  2678. $this->assertFalse(empty($result));
  2679. $result = $Something->read();
  2680. $this->assertEquals(3, count($result['SomethingElse']));
  2681. $this->assertTrue(Set::matches('/Something[id=4]', $result));
  2682. $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
  2683. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
  2684. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
  2685. $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
  2686. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
  2687. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
  2688. $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
  2689. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
  2690. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
  2691. }
  2692. /**
  2693. * testSaveAllHasOne method
  2694. *
  2695. * @return void
  2696. */
  2697. public function testSaveAllHasOne() {
  2698. $model = new Comment();
  2699. $model->deleteAll(true);
  2700. $this->assertEquals(array(), $model->find('all'));
  2701. $model->Attachment->deleteAll(true);
  2702. $this->assertEquals(array(), $model->Attachment->find('all'));
  2703. $this->assertTrue($model->saveAll(array(
  2704. 'Comment' => array(
  2705. 'comment' => 'Comment with attachment',
  2706. 'article_id' => 1,
  2707. 'user_id' => 1
  2708. ),
  2709. 'Attachment' => array(
  2710. 'attachment' => 'some_file.zip'
  2711. ))));
  2712. $result = $model->find('all', array('fields' => array(
  2713. 'Comment.id', 'Comment.comment', 'Attachment.id',
  2714. 'Attachment.comment_id', 'Attachment.attachment'
  2715. )));
  2716. $expected = array(array(
  2717. 'Comment' => array(
  2718. 'id' => '1',
  2719. 'comment' => 'Comment with attachment'
  2720. ),
  2721. 'Attachment' => array(
  2722. 'id' => '1',
  2723. 'comment_id' => '1',
  2724. 'attachment' => 'some_file.zip'
  2725. )));
  2726. $this->assertEquals($expected, $result);
  2727. $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
  2728. $data = array(
  2729. 'Comment' => array(
  2730. 'comment' => 'Comment with attachment',
  2731. 'article_id' => 1,
  2732. 'user_id' => 1
  2733. ),
  2734. 'Attachment' => array(
  2735. 'attachment' => 'some_file.zip'
  2736. ));
  2737. $this->assertTrue($model->saveAll($data, array('validate' => 'first')));
  2738. }
  2739. /**
  2740. * testSaveAllBelongsTo method
  2741. *
  2742. * @return void
  2743. */
  2744. public function testSaveAllBelongsTo() {
  2745. $model = new Comment();
  2746. $model->deleteAll(true);
  2747. $this->assertEquals(array(), $model->find('all'));
  2748. $model->Article->deleteAll(true);
  2749. $this->assertEquals(array(), $model->Article->find('all'));
  2750. $this->assertTrue($model->saveAll(array(
  2751. 'Comment' => array(
  2752. 'comment' => 'Article comment',
  2753. 'article_id' => 1,
  2754. 'user_id' => 1
  2755. ),
  2756. 'Article' => array(
  2757. 'title' => 'Model Associations 101',
  2758. 'user_id' => 1
  2759. ))));
  2760. $result = $model->find('all', array('fields' => array(
  2761. 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
  2762. )));
  2763. $expected = array(array(
  2764. 'Comment' => array(
  2765. 'id' => '1',
  2766. 'article_id' => '1',
  2767. 'comment' => 'Article comment'
  2768. ),
  2769. 'Article' => array(
  2770. 'id' => '1',
  2771. 'title' => 'Model Associations 101'
  2772. )));
  2773. $this->assertEquals($expected, $result);
  2774. }
  2775. /**
  2776. * testSaveAllHasOneValidation method
  2777. *
  2778. * @return void
  2779. */
  2780. public function testSaveAllHasOneValidation() {
  2781. $model = new Comment();
  2782. $model->deleteAll(true);
  2783. $this->assertEquals(array(), $model->find('all'));
  2784. $model->Attachment->deleteAll(true);
  2785. $this->assertEquals(array(), $model->Attachment->find('all'));
  2786. $model->validate = array('comment' => 'notEmpty');
  2787. $model->Attachment->validate = array('attachment' => 'notEmpty');
  2788. $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
  2789. $result = $model->saveAll(
  2790. array(
  2791. 'Comment' => array(
  2792. 'comment' => '',
  2793. 'article_id' => 1,
  2794. 'user_id' => 1
  2795. ),
  2796. 'Attachment' => array('attachment' => '')
  2797. ),
  2798. array('validate' => 'first')
  2799. );
  2800. $this->assertEquals(false, $result);
  2801. $expected = array(
  2802. 'comment' => array('This field cannot be left blank'),
  2803. 'Attachment' => array(
  2804. 'attachment' => array('This field cannot be left blank')
  2805. )
  2806. );
  2807. $this->assertEquals($expected, $model->validationErrors);
  2808. $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
  2809. }
  2810. /**
  2811. * testSaveAllAtomic method
  2812. *
  2813. * @return void
  2814. */
  2815. public function testSaveAllAtomic() {
  2816. $this->loadFixtures('Article', 'User', 'Comment');
  2817. $TestModel = new Article();
  2818. $result = $TestModel->saveAll(array(
  2819. 'Article' => array(
  2820. 'title' => 'Post with Author',
  2821. 'body' => 'This post will be saved with an author',
  2822. 'user_id' => 2
  2823. ),
  2824. 'Comment' => array(
  2825. array('comment' => 'First new comment', 'user_id' => 2))
  2826. ), array('atomic' => false));
  2827. $this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
  2828. $result = $TestModel->saveAll(array(
  2829. array(
  2830. 'id' => '1',
  2831. 'title' => 'Baleeted First Post',
  2832. 'body' => 'Baleeted!',
  2833. 'published' => 'N'
  2834. ),
  2835. array(
  2836. 'id' => '2',
  2837. 'title' => 'Just update the title'
  2838. ),
  2839. array(
  2840. 'title' => 'Creating a fourth post',
  2841. 'body' => 'Fourth post body',
  2842. 'user_id' => 2
  2843. )
  2844. ), array('atomic' => false));
  2845. $this->assertSame($result, array(true, true, true));
  2846. $result = $TestModel->saveAll(array(
  2847. 'Article' => array('id' => 2),
  2848. 'Comment' => array(
  2849. array(
  2850. 'comment' => 'First new comment',
  2851. 'published' => 'Y',
  2852. 'user_id' => 1
  2853. ),
  2854. array(
  2855. 'comment' => 'Second new comment',
  2856. 'published' => 'Y',
  2857. 'user_id' => 2
  2858. ))
  2859. ), array('validate' => true, 'atomic' => false));
  2860. $this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
  2861. $TestModel->validate = array(
  2862. 'title' => 'notEmpty',
  2863. 'author_id' => 'numeric'
  2864. );
  2865. $result = $TestModel->saveAll(array(
  2866. array(
  2867. 'id' => '1',
  2868. 'title' => 'Un-Baleeted First Post',
  2869. 'body' => 'Not Baleeted!',
  2870. 'published' => 'Y'
  2871. ),
  2872. array(
  2873. 'id' => '2',
  2874. 'title' => '',
  2875. 'body' => 'Trying to get away with an empty title'
  2876. )
  2877. ), array('validate' => true, 'atomic' => false));
  2878. $this->assertSame(array(true, false), $result);
  2879. }
  2880. /**
  2881. * testSaveAllDeepAssociated method
  2882. *
  2883. * @return void
  2884. */
  2885. public function testSaveAllDeepAssociated() {
  2886. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  2887. $TestModel = new Article();
  2888. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  2889. $TestModel->hasAndBelongsToMany = array();
  2890. $result = $TestModel->saveAll(array(
  2891. 'Article' => array('id' => 2),
  2892. 'Comment' => array(
  2893. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
  2894. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  2895. )
  2896. ), array('deep' => true));
  2897. $this->assertTrue($result);
  2898. $result = $TestModel->findById(2);
  2899. $expected = array(
  2900. 'First Comment for Second Article',
  2901. 'Second Comment for Second Article',
  2902. 'First new comment',
  2903. 'Second new comment'
  2904. );
  2905. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  2906. $this->assertEquals($expected, $result);
  2907. $result = $TestModel->Comment->User->field('id', array('user' => 'newuser', 'password' => 'newuserpass'));
  2908. $this->assertEquals(5, $result);
  2909. $result = $TestModel->saveAll(array(
  2910. 'Article' => array('id' => 2),
  2911. 'Comment' => array(
  2912. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  2913. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
  2914. )
  2915. ), array('deep' => true));
  2916. $this->assertTrue($result);
  2917. $result = $TestModel->findById(2);
  2918. $expected = array(
  2919. 'First Comment for Second Article',
  2920. 'Second Comment for Second Article',
  2921. 'First new comment',
  2922. 'Second new comment',
  2923. 'Third new comment',
  2924. 'Fourth new comment'
  2925. );
  2926. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  2927. $this->assertEquals($expected, $result);
  2928. $result = $TestModel->Comment->Attachment->field('id', array('attachment' => 'deepsaved'));
  2929. $this->assertEquals(2, $result);
  2930. $data = array(
  2931. 'Attachment' => array(
  2932. 'attachment' => 'deepsave insert',
  2933. ),
  2934. 'Comment' => array(
  2935. 'comment' => 'First comment deepsave insert',
  2936. 'published' => 'Y',
  2937. 'user_id' => 5,
  2938. 'Article' => array(
  2939. 'title' => 'First Article deepsave insert',
  2940. 'body' => 'First Article Body deepsave insert',
  2941. 'User' => array(
  2942. 'user' => '',
  2943. 'password' => 'magic'
  2944. ),
  2945. ),
  2946. )
  2947. );
  2948. $TestModel->Comment->Attachment->create();
  2949. $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => true));
  2950. $this->assertFalse($result);
  2951. $expected = array('User' => array('user' => array('This field cannot be left blank')));
  2952. $this->assertEquals($expected, $TestModel->validationErrors);
  2953. $data['Comment']['Article']['User']['user'] = 'deepsave';
  2954. $TestModel->Comment->Attachment->create();
  2955. $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => true));
  2956. $this->assertTrue($result);
  2957. $result = $TestModel->Comment->Attachment->findById($TestModel->Comment->Attachment->id);
  2958. $expected = array(
  2959. 'Attachment' => array(
  2960. 'id' => '3',
  2961. 'comment_id' => '11',
  2962. 'attachment' => 'deepsave insert',
  2963. ),
  2964. 'Comment' => array(
  2965. 'id' => '11',
  2966. 'article_id' => '4',
  2967. 'user_id' => '5',
  2968. 'comment' => 'First comment deepsave insert',
  2969. 'published' => 'Y',
  2970. )
  2971. );
  2972. unset($result['Attachment']['created'], $result['Attachment']['updated']);
  2973. $this->assertEquals($expected['Attachment'], $result['Attachment']);
  2974. unset($result['Comment']['created'], $result['Comment']['updated']);
  2975. $this->assertEquals($expected['Comment'], $result['Comment']);
  2976. $result = $TestModel->findById($result['Comment']['article_id']);
  2977. $expected = array(
  2978. 'Article' => array(
  2979. 'id' => '4',
  2980. 'user_id' => '6',
  2981. 'title' => 'First Article deepsave insert',
  2982. 'body' => 'First Article Body deepsave insert',
  2983. 'published' => 'N',
  2984. ),
  2985. 'User' => array(
  2986. 'id' => '6',
  2987. 'user' => 'deepsave',
  2988. 'password' => 'magic',
  2989. ),
  2990. 'Comment' => array(
  2991. array(
  2992. 'id' => '11',
  2993. 'article_id' => '4',
  2994. 'user_id' => '5',
  2995. 'comment' => 'First comment deepsave insert',
  2996. 'published' => 'Y',
  2997. )
  2998. )
  2999. );
  3000. unset(
  3001. $result['Article']['created'], $result['Article']['updated'],
  3002. $result['User']['created'], $result['User']['updated'],
  3003. $result['Comment'][0]['created'], $result['Comment'][0]['updated']
  3004. );
  3005. $this->assertEquals($expected, $result);
  3006. }
  3007. /**
  3008. * testSaveAllDeepMany
  3009. * tests the validate methods with deeper recursive data
  3010. *
  3011. * @return void
  3012. */
  3013. public function testSaveAllDeepMany() {
  3014. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  3015. $TestModel = new Article();
  3016. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3017. $TestModel->hasAndBelongsToMany = array();
  3018. $data = array(
  3019. array(
  3020. 'Article' => array('id' => 1),
  3021. 'Comment' => array(
  3022. array('comment' => 'First comment deepsaved article 1', 'published' => 'Y', 'User' => array('user' => 'savemany', 'password' => 'manysaved')),
  3023. array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
  3024. )
  3025. ),
  3026. array(
  3027. 'Article' => array('id' => 2),
  3028. 'Comment' => array(
  3029. array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => 'moresaved')),
  3030. array('comment' => 'Second comment deepsaved article 2', 'published' => 'Y', 'user_id' => 2)
  3031. )
  3032. )
  3033. );
  3034. $result = $TestModel->saveAll($data, array('deep' => true));
  3035. $this->assertTrue($result);
  3036. $data = array(
  3037. array(
  3038. 'id' => 1, 'body' => '',
  3039. 'Comment' => array(
  3040. array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
  3041. array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
  3042. )
  3043. ),
  3044. array(
  3045. 'Article' => array('id' => 2),
  3046. 'Comment' => array(
  3047. array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => '')),
  3048. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  3049. )
  3050. )
  3051. );
  3052. $TestModel->Comment->validate['comment'] = 'notEmpty';
  3053. $result = $TestModel->saveAll($data, array('deep' => true));
  3054. $this->assertFalse($result);
  3055. $expected = array(
  3056. 0 => array(
  3057. 'body' => array('This field cannot be left blank'),
  3058. 'Comment' => array(
  3059. 0 => array(
  3060. 'comment' => array('This field cannot be left blank'),
  3061. 'User' => array(
  3062. 'user' => array('This field cannot be left blank')
  3063. )
  3064. )
  3065. )
  3066. ),
  3067. 1 => array(
  3068. 'Comment' => array(
  3069. 0 => array(
  3070. 'User' => array(
  3071. 'password' => array('This field cannot be left blank')
  3072. )
  3073. ),
  3074. 1 => array(
  3075. 'comment' => array('This field cannot be left blank')
  3076. )
  3077. )
  3078. )
  3079. );
  3080. $result = $TestModel->validationErrors;
  3081. $this->assertSame($expected, $result);
  3082. }
  3083. /**
  3084. * testSaveAllDeepValidateOnly
  3085. * tests the validate methods with deeper recursive data
  3086. *
  3087. * @return void
  3088. */
  3089. public function testSaveAllDeepValidateOnly() {
  3090. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  3091. $TestModel = new Article();
  3092. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3093. $TestModel->hasAndBelongsToMany = array();
  3094. $TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
  3095. $TestModel->Comment->validate['comment'] = 'notEmpty';
  3096. $result = $TestModel->saveAll(
  3097. array(
  3098. 'Article' => array('id' => 2),
  3099. 'Comment' => array(
  3100. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
  3101. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3102. )
  3103. ),
  3104. array('validate' => 'only', 'deep' => true)
  3105. );
  3106. $this->assertTrue($result);
  3107. $result = $TestModel->saveAll(
  3108. array(
  3109. 'Article' => array('id' => 2),
  3110. 'Comment' => array(
  3111. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3112. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3113. )
  3114. ),
  3115. array('validate' => 'only', 'deep' => true)
  3116. );
  3117. $this->assertFalse($result);
  3118. $result = $TestModel->saveAll(
  3119. array(
  3120. 'Article' => array('id' => 2),
  3121. 'Comment' => array(
  3122. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
  3123. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3124. )
  3125. ),
  3126. array('validate' => 'only', 'atomic' => false, 'deep' => true)
  3127. );
  3128. $expected = array(
  3129. 'Article' => true,
  3130. 'Comment' => array(
  3131. true,
  3132. true
  3133. )
  3134. );
  3135. $this->assertSame($expected, $result);
  3136. $result = $TestModel->saveAll(
  3137. array(
  3138. 'Article' => array('id' => 2),
  3139. 'Comment' => array(
  3140. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3141. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3142. )
  3143. ),
  3144. array('validate' => 'only', 'atomic' => false, 'deep' => true)
  3145. );
  3146. $expected = array(
  3147. 'Article' => true,
  3148. 'Comment' => array(
  3149. false,
  3150. true
  3151. )
  3152. );
  3153. $this->assertSame($expected, $result);
  3154. $result = $TestModel->saveAll(array(
  3155. 'Article' => array('id' => 2),
  3156. 'Comment' => array(
  3157. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3158. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
  3159. )
  3160. ),
  3161. array('validate' => 'only', 'deep' => true)
  3162. );
  3163. $this->assertTrue($result);
  3164. $result = $TestModel->saveAll(array(
  3165. 'Article' => array('id' => 2),
  3166. 'Comment' => array(
  3167. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3168. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  3169. )
  3170. ),
  3171. array('validate' => 'only', 'deep' => true)
  3172. );
  3173. $this->assertFalse($result);
  3174. $result = $TestModel->saveAll(array(
  3175. 'Article' => array('id' => 2),
  3176. 'Comment' => array(
  3177. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3178. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsave'))
  3179. )
  3180. ),
  3181. array('validate' => 'only', 'atomic' => false, 'deep' => true)
  3182. );
  3183. $expected = array(
  3184. 'Article' => true,
  3185. 'Comment' => array(
  3186. true,
  3187. true
  3188. )
  3189. );
  3190. $this->assertSame($expected, $result);
  3191. $result = $TestModel->saveAll(array(
  3192. 'Article' => array('id' => 2),
  3193. 'Comment' => array(
  3194. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3195. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  3196. )
  3197. ),
  3198. array('validate' => 'only', 'atomic' => false, 'deep' => true)
  3199. );
  3200. $expected = array(
  3201. 'Article' => true,
  3202. 'Comment' => array(
  3203. true,
  3204. false
  3205. )
  3206. );
  3207. $this->assertSame($expected, $result);
  3208. $expected = array(
  3209. 'Comment' => array(
  3210. 1 => array(
  3211. 'Attachment' => array(
  3212. 'attachment' => array('This field cannot be left blank')
  3213. )
  3214. )
  3215. )
  3216. );
  3217. $result = $TestModel->validationErrors;
  3218. $this->assertSame($expected, $result);
  3219. $data = array(
  3220. 'Attachment' => array(
  3221. 'attachment' => 'deepsave insert',
  3222. ),
  3223. 'Comment' => array(
  3224. 'comment' => 'First comment deepsave insert',
  3225. 'published' => 'Y',
  3226. 'user_id' => 5,
  3227. 'Article' => array(
  3228. 'title' => 'First Article deepsave insert',
  3229. 'body' => 'First Article Body deepsave insert',
  3230. 'User' => array(
  3231. 'user' => 'deepsave',
  3232. 'password' => 'magic'
  3233. ),
  3234. ),
  3235. )
  3236. );
  3237. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3238. $this->assertTrue($result);
  3239. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3240. $expected = array(
  3241. 'Attachment' => true,
  3242. 'Comment' => true
  3243. );
  3244. $this->assertSame($expected, $result);
  3245. $data = array(
  3246. 'Attachment' => array(
  3247. 'attachment' => 'deepsave insert',
  3248. ),
  3249. 'Comment' => array(
  3250. 'comment' => 'First comment deepsave insert',
  3251. 'published' => 'Y',
  3252. 'user_id' => 5,
  3253. 'Article' => array(
  3254. 'title' => 'First Article deepsave insert',
  3255. 'body' => 'First Article Body deepsave insert',
  3256. 'User' => array(
  3257. 'user' => '',
  3258. 'password' => 'magic'
  3259. ),
  3260. ),
  3261. )
  3262. );
  3263. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3264. $this->assertFalse($result);
  3265. $result = $TestModel->Comment->Attachment->validationErrors;
  3266. $expected = array(
  3267. 'Comment' => array(
  3268. 'Article' => array(
  3269. 'User' => array(
  3270. 'user' => array('This field cannot be left blank')
  3271. )
  3272. )
  3273. )
  3274. );
  3275. $this->assertSame($expected, $result);
  3276. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3277. $expected = array(
  3278. 'Attachment' => true,
  3279. 'Comment' => false
  3280. );
  3281. $this->assertEquals($expected, $result);
  3282. $data['Comment']['Article']['body'] = '';
  3283. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3284. $this->assertFalse($result);
  3285. $result = $TestModel->Comment->Attachment->validationErrors;
  3286. $expected = array(
  3287. 'Comment' => array(
  3288. 'Article' => array(
  3289. 'body' => array('This field cannot be left blank'),
  3290. 'User' => array(
  3291. 'user' => array('This field cannot be left blank')
  3292. )
  3293. )
  3294. )
  3295. );
  3296. $this->assertSame($expected, $result);
  3297. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3298. $expected = array(
  3299. 'Attachment' => true,
  3300. 'Comment' => false
  3301. );
  3302. $this->assertEquals($expected, $result);
  3303. $data['Comment']['comment'] = '';
  3304. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3305. $this->assertFalse($result);
  3306. $result = $TestModel->Comment->Attachment->validationErrors;
  3307. $expected = array(
  3308. 'Comment' => array(
  3309. 'comment' => array('This field cannot be left blank'),
  3310. 'Article' => array(
  3311. 'body' => array('This field cannot be left blank'),
  3312. 'User' => array(
  3313. 'user' => array('This field cannot be left blank')
  3314. )
  3315. )
  3316. )
  3317. );
  3318. $this->assertSame($expected, $result);
  3319. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3320. $expected = array(
  3321. 'Attachment' => true,
  3322. 'Comment' => false
  3323. );
  3324. $this->assertEquals($expected, $result);
  3325. $data['Attachment']['attachment'] = '';
  3326. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3327. $this->assertFalse($result);
  3328. $result = $TestModel->Comment->Attachment->validationErrors;
  3329. $expected = array(
  3330. 'attachment' => array('This field cannot be left blank'),
  3331. 'Comment' => array(
  3332. 'comment' => array('This field cannot be left blank'),
  3333. 'Article' => array(
  3334. 'body' => array('This field cannot be left blank'),
  3335. 'User' => array(
  3336. 'user' => array('This field cannot be left blank')
  3337. )
  3338. )
  3339. )
  3340. );
  3341. $this->assertSame($expected, $result);
  3342. $result = $TestModel->Comment->validationErrors;
  3343. $expected = array(
  3344. 'comment' => array('This field cannot be left blank'),
  3345. 'Article' => array(
  3346. 'body' => array('This field cannot be left blank'),
  3347. 'User' => array(
  3348. 'user' => array('This field cannot be left blank')
  3349. )
  3350. )
  3351. );
  3352. $this->assertSame($expected, $result);
  3353. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3354. $expected = array(
  3355. 'Attachment' => false,
  3356. 'Comment' => false
  3357. );
  3358. $this->assertEquals($expected, $result);
  3359. }
  3360. /**
  3361. * testSaveAllNotDeepAssociated method
  3362. * test that only directly associated data gets saved
  3363. *
  3364. * @return void
  3365. */
  3366. public function testSaveAllNotDeepAssociated() {
  3367. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  3368. $TestModel = new Article();
  3369. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3370. $TestModel->hasAndBelongsToMany = array();
  3371. $result = $TestModel->saveAll(array(
  3372. 'Article' => array('id' => 2),
  3373. 'Comment' => array(
  3374. array(
  3375. 'comment' => 'First new comment', 'published' => 'Y', 'user_id' => 2,
  3376. 'User' => array('user' => 'newuser', 'password' => 'newuserpass')
  3377. ),
  3378. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3379. )
  3380. ), array('deep' => false));
  3381. $this->assertTrue($result);
  3382. $result = $TestModel->Comment->User->field('id', array('user' => 'newuser', 'password' => 'newuserpass'));
  3383. $this->assertFalse($result);
  3384. $result = $TestModel->saveAll(array(
  3385. 'Article' => array('id' => 2),
  3386. 'Comment' => array(
  3387. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 4),
  3388. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
  3389. )
  3390. ), array('deep' => false));
  3391. $this->assertTrue($result);
  3392. $result = $TestModel->Comment->Attachment->field('id', array('attachment' => 'deepsaved'));
  3393. $this->assertFalse($result);
  3394. $data = array(
  3395. 'Attachment' => array(
  3396. 'attachment' => 'deepsave insert',
  3397. ),
  3398. 'Comment' => array(
  3399. 'comment' => 'First comment deepsave insert',
  3400. 'published' => 'Y',
  3401. 'user_id' => 4,
  3402. 'article_id' => 1,
  3403. 'Article' => array(
  3404. 'title' => 'First Article deepsave insert',
  3405. 'body' => 'First Article Body deepsave insert',
  3406. 'User' => array(
  3407. 'user' => 'deepsave',
  3408. 'password' => 'magic'
  3409. ),
  3410. ),
  3411. )
  3412. );
  3413. $expected = $TestModel->User->find('count');
  3414. $TestModel->Comment->Attachment->create();
  3415. $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => false));
  3416. $this->assertTrue($result);
  3417. $result = $TestModel->User->find('count');
  3418. $this->assertEquals($expected, $result);
  3419. $result = $TestModel->Comment->Attachment->findById($TestModel->Comment->Attachment->id);
  3420. $expected = array(
  3421. 'Attachment' => array(
  3422. 'id' => '2',
  3423. 'comment_id' => '11',
  3424. 'attachment' => 'deepsave insert',
  3425. ),
  3426. 'Comment' => array(
  3427. 'id' => '11',
  3428. 'article_id' => 1,
  3429. 'user_id' => '4',
  3430. 'comment' => 'First comment deepsave insert',
  3431. 'published' => 'Y',
  3432. )
  3433. );
  3434. unset($result['Attachment']['created'], $result['Attachment']['updated']);
  3435. $this->assertEquals($expected['Attachment'], $result['Attachment']);
  3436. unset($result['Comment']['created'], $result['Comment']['updated']);
  3437. $this->assertEquals($expected['Comment'], $result['Comment']);
  3438. }
  3439. /**
  3440. * testSaveAllNotDeepMany
  3441. * tests the save methods to not save deeper recursive data
  3442. *
  3443. * @return void
  3444. */
  3445. public function testSaveAllNotDeepMany() {
  3446. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  3447. $TestModel = new Article();
  3448. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3449. $TestModel->hasAndBelongsToMany = array();
  3450. $data = array(
  3451. array(
  3452. 'id' => 1,
  3453. 'body' => '',
  3454. 'Comment' => array(
  3455. array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
  3456. array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
  3457. )
  3458. ),
  3459. array(
  3460. 'Article' => array('id' => 2),
  3461. 'Comment' => array(
  3462. array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => '')),
  3463. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  3464. )
  3465. )
  3466. );
  3467. $TestModel->Comment->validate['comment'] = 'notEmpty';
  3468. $result = $TestModel->saveAll($data, array('deep' => false));
  3469. $this->assertFalse($result);
  3470. $expected = array(
  3471. 0 => array(
  3472. 'body' => array('This field cannot be left blank')
  3473. )
  3474. );
  3475. $result = $TestModel->validationErrors;
  3476. $this->assertSame($expected, $result);
  3477. $data = array(
  3478. array(
  3479. 'Article' => array('id' => 1, 'body' => 'Ignore invalid comment'),
  3480. 'Comment' => array(
  3481. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  3482. )
  3483. ),
  3484. array(
  3485. 'Article' => array('id' => 2),
  3486. 'Comment' => array(
  3487. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  3488. )
  3489. )
  3490. );
  3491. $result = $TestModel->saveAll($data, array('deep' => false));
  3492. $this->assertTrue($result);
  3493. }
  3494. /**
  3495. * testSaveAllNotDeepValidateOnly
  3496. * tests the validate methods to not validate deeper recursive data
  3497. *
  3498. * @return void
  3499. */
  3500. public function testSaveAllNotDeepValidateOnly() {
  3501. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  3502. $TestModel = new Article();
  3503. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3504. $TestModel->hasAndBelongsToMany = array();
  3505. $TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
  3506. $TestModel->Comment->validate['comment'] = 'notEmpty';
  3507. $result = $TestModel->saveAll(
  3508. array(
  3509. 'Article' => array('id' => 2, 'body' => ''),
  3510. 'Comment' => array(
  3511. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3512. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3513. )
  3514. ),
  3515. array('validate' => 'only', 'deep' => false)
  3516. );
  3517. $this->assertFalse($result);
  3518. $expected = array('body' => array('This field cannot be left blank'));
  3519. $result = $TestModel->validationErrors;
  3520. $this->assertSame($expected, $result);
  3521. $result = $TestModel->saveAll(
  3522. array(
  3523. 'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
  3524. 'Comment' => array(
  3525. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3526. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3527. )
  3528. ),
  3529. array('validate' => 'only', 'deep' => false)
  3530. );
  3531. $this->assertTrue($result);
  3532. $result = $TestModel->saveAll(
  3533. array(
  3534. 'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
  3535. 'Comment' => array(
  3536. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3537. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3538. )
  3539. ),
  3540. array('validate' => 'only', 'atomic' => false, 'deep' => false)
  3541. );
  3542. $expected = array(
  3543. 'Article' => true,
  3544. 'Comment' => array(
  3545. true,
  3546. true
  3547. )
  3548. );
  3549. $this->assertSame($expected, $result);
  3550. $result = $TestModel->saveAll(array(
  3551. 'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
  3552. 'Comment' => array(
  3553. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3554. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  3555. )
  3556. ),
  3557. array('validate' => 'only', 'deep' => false)
  3558. );
  3559. $this->assertTrue($result);
  3560. $result = $TestModel->saveAll(array(
  3561. 'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
  3562. 'Comment' => array(
  3563. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3564. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  3565. )
  3566. ),
  3567. array('validate' => 'only', 'atomic' => false, 'deep' => false)
  3568. );
  3569. $expected = array(
  3570. 'Article' => true,
  3571. 'Comment' => array(
  3572. true,
  3573. true
  3574. )
  3575. );
  3576. $this->assertSame($expected, $result);
  3577. $expected = array();
  3578. $result = $TestModel->validationErrors;
  3579. $this->assertSame($expected, $result);
  3580. $data = array(
  3581. 'Attachment' => array(
  3582. 'attachment' => 'deepsave insert',
  3583. ),
  3584. 'Comment' => array(
  3585. 'comment' => 'First comment deepsave insert',
  3586. 'published' => 'Y',
  3587. 'user_id' => 5,
  3588. 'Article' => array(
  3589. 'title' => 'First Article deepsave insert ignored',
  3590. 'body' => 'First Article Body deepsave insert',
  3591. 'User' => array(
  3592. 'user' => '',
  3593. 'password' => 'magic'
  3594. ),
  3595. ),
  3596. )
  3597. );
  3598. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
  3599. $this->assertTrue($result);
  3600. $result = $TestModel->Comment->Attachment->validationErrors;
  3601. $expected = array();
  3602. $this->assertSame($expected, $result);
  3603. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
  3604. $expected = array(
  3605. 'Attachment' => true,
  3606. 'Comment' => true
  3607. );
  3608. $this->assertEquals($expected, $result);
  3609. $data['Comment']['Article']['body'] = '';
  3610. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
  3611. $this->assertTrue($result);
  3612. $result = $TestModel->Comment->Attachment->validationErrors;
  3613. $expected = array();
  3614. $this->assertSame($expected, $result);
  3615. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
  3616. $expected = array(
  3617. 'Attachment' => true,
  3618. 'Comment' => true
  3619. );
  3620. $this->assertEquals($expected, $result);
  3621. }
  3622. /**
  3623. * testSaveAllHasMany method
  3624. *
  3625. * @return void
  3626. */
  3627. public function testSaveAllHasMany() {
  3628. $this->loadFixtures('Article', 'Comment');
  3629. $TestModel = new Article();
  3630. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3631. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  3632. $result = $TestModel->saveAll(array(
  3633. 'Article' => array('id' => 2),
  3634. 'Comment' => array(
  3635. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  3636. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3637. )
  3638. ));
  3639. $this->assertFalse(empty($result));
  3640. $result = $TestModel->findById(2);
  3641. $expected = array(
  3642. 'First Comment for Second Article',
  3643. 'Second Comment for Second Article',
  3644. 'First new comment',
  3645. 'Second new comment'
  3646. );
  3647. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  3648. $this->assertEquals($expected, $result);
  3649. $result = $TestModel->saveAll(
  3650. array(
  3651. 'Article' => array('id' => 2),
  3652. 'Comment' => array(
  3653. array(
  3654. 'comment' => 'Third new comment',
  3655. 'published' => 'Y',
  3656. 'user_id' => 1
  3657. ))),
  3658. array('atomic' => false)
  3659. );
  3660. $this->assertFalse(empty($result));
  3661. $result = $TestModel->findById(2);
  3662. $expected = array(
  3663. 'First Comment for Second Article',
  3664. 'Second Comment for Second Article',
  3665. 'First new comment',
  3666. 'Second new comment',
  3667. 'Third new comment'
  3668. );
  3669. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  3670. $this->assertEquals($expected, $result);
  3671. $TestModel->beforeSaveReturn = false;
  3672. $result = $TestModel->saveAll(
  3673. array(
  3674. 'Article' => array('id' => 2),
  3675. 'Comment' => array(
  3676. array(
  3677. 'comment' => 'Fourth new comment',
  3678. 'published' => 'Y',
  3679. 'user_id' => 1
  3680. ))),
  3681. array('atomic' => false)
  3682. );
  3683. $this->assertEquals(array('Article' => false), $result);
  3684. $result = $TestModel->findById(2);
  3685. $expected = array(
  3686. 'First Comment for Second Article',
  3687. 'Second Comment for Second Article',
  3688. 'First new comment',
  3689. 'Second new comment',
  3690. 'Third new comment'
  3691. );
  3692. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  3693. $this->assertEquals($expected, $result);
  3694. }
  3695. /**
  3696. * testSaveAllHasManyValidation method
  3697. *
  3698. * @return void
  3699. */
  3700. public function testSaveAllHasManyValidation() {
  3701. $this->loadFixtures('Article', 'Comment');
  3702. $TestModel = new Article();
  3703. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  3704. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  3705. $result = $TestModel->saveAll(array(
  3706. 'Article' => array('id' => 2),
  3707. 'Comment' => array(
  3708. array('comment' => '', 'published' => 'Y', 'user_id' => 1),
  3709. )
  3710. ), array('validate' => true));
  3711. $this->assertFalse($result);
  3712. $expected = array('Comment' => array(
  3713. array('comment' => array('This field cannot be left blank'))
  3714. ));
  3715. $this->assertEquals($expected, $TestModel->validationErrors);
  3716. $expected = array(
  3717. array('comment' => array('This field cannot be left blank'))
  3718. );
  3719. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  3720. $result = $TestModel->saveAll(array(
  3721. 'Article' => array('id' => 2),
  3722. 'Comment' => array(
  3723. array(
  3724. 'comment' => '',
  3725. 'published' => 'Y',
  3726. 'user_id' => 1
  3727. ))
  3728. ), array('validate' => 'first'));
  3729. $this->assertFalse($result);
  3730. }
  3731. /**
  3732. * test saveAll with transactions and ensure there is no missing rollback.
  3733. *
  3734. * @return void
  3735. */
  3736. public function testSaveAllManyRowsTransactionNoRollback() {
  3737. $this->loadFixtures('Post');
  3738. $Post = new TestPost();
  3739. $Post->validate = array(
  3740. 'title' => array('rule' => array('notEmpty'))
  3741. );
  3742. // If validation error occurs, rollback() should be called.
  3743. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  3744. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  3745. $db->expects($this->never())->method('commit');
  3746. $db->expects($this->once())->method('rollback');
  3747. $Post->setDataSourceObject($db);
  3748. $data = array(
  3749. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3750. array('author_id' => 1, 'title' => '')
  3751. );
  3752. $Post->saveAll($data, array('atomic' => true, 'validate' => true));
  3753. // If exception thrown, rollback() should be called too.
  3754. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  3755. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  3756. $db->expects($this->never())->method('commit');
  3757. $db->expects($this->once())->method('rollback');
  3758. $Post->setDataSourceObject($db);
  3759. $data = array(
  3760. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3761. array('author_id' => 1, 'title' => 'New Fifth Post', 'body' => $db->expression('PDO_EXCEPTION()'))
  3762. );
  3763. try {
  3764. $Post->saveAll($data, array('atomic' => true, 'validate' => true));
  3765. $this->fail('No exception thrown');
  3766. } catch (PDOException $e) {
  3767. }
  3768. // Otherwise, commit() should be called.
  3769. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  3770. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  3771. $db->expects($this->once())->method('commit');
  3772. $db->expects($this->never())->method('rollback');
  3773. $Post->setDataSourceObject($db);
  3774. $data = array(
  3775. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3776. array('author_id' => 1, 'title' => 'New Fifth Post')
  3777. );
  3778. $Post->saveAll($data, array('atomic' => true, 'validate' => true));
  3779. }
  3780. /**
  3781. * test saveAll with transactions and ensure there is no missing rollback.
  3782. *
  3783. * @return void
  3784. */
  3785. public function testSaveAllAssociatedTransactionNoRollback() {
  3786. $this->loadFixtures('Post', 'Author');
  3787. $Post = new TestPost();
  3788. $Post->Author->validate = array(
  3789. 'user' => array('rule' => array('notEmpty'))
  3790. );
  3791. // If validation error occurs, rollback() should be called.
  3792. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  3793. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  3794. $db->expects($this->never())->method('commit');
  3795. $db->expects($this->once())->method('rollback');
  3796. $Post->setDataSourceObject($db);
  3797. $Post->Author->setDataSourceObject($db);
  3798. $data = array(
  3799. 'Post' => array(
  3800. 'title' => 'New post',
  3801. 'body' => 'Content',
  3802. 'published' => 'Y'
  3803. ),
  3804. 'Author' => array(
  3805. 'user' => '',
  3806. 'password' => "sekret"
  3807. )
  3808. );
  3809. $Post->saveAll($data, array('validate' => true));
  3810. // If exception thrown, rollback() should be called too.
  3811. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  3812. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  3813. $db->expects($this->never())->method('commit');
  3814. $db->expects($this->once())->method('rollback');
  3815. $Post->setDataSourceObject($db);
  3816. $Post->Author->setDataSourceObject($db);
  3817. $data = array(
  3818. 'Post' => array(
  3819. 'title' => 'New post',
  3820. 'body' => $db->expression('PDO_EXCEPTION()'),
  3821. 'published' => 'Y'
  3822. ),
  3823. 'Author' => array(
  3824. 'user' => 'New user',
  3825. 'password' => "sekret"
  3826. )
  3827. );
  3828. try {
  3829. $Post->saveAll($data, array('validate' => true));
  3830. $this->fail('No exception thrown');
  3831. } catch (PDOException $e) {
  3832. }
  3833. // Otherwise, commit() should be called.
  3834. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  3835. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  3836. $db->expects($this->once())->method('commit');
  3837. $db->expects($this->never())->method('rollback');
  3838. $Post->setDataSourceObject($db);
  3839. $Post->Author->setDataSourceObject($db);
  3840. $data = array(
  3841. 'Post' => array(
  3842. 'title' => 'New post',
  3843. 'body' => 'Content',
  3844. 'published' => 'Y'
  3845. ),
  3846. 'Author' => array(
  3847. 'user' => 'New user',
  3848. 'password' => "sekret"
  3849. )
  3850. );
  3851. $Post->saveAll($data, array('validate' => true));
  3852. }
  3853. /**
  3854. * test saveAll with nested saveAll call.
  3855. *
  3856. * @return void
  3857. */
  3858. public function testSaveAllNestedSaveAll() {
  3859. $this->loadFixtures('Sample');
  3860. $TransactionTestModel = new TransactionTestModel();
  3861. $data = array(
  3862. array('apple_id' => 1, 'name' => 'sample5'),
  3863. );
  3864. $this->assertTrue($TransactionTestModel->saveAll($data, array('atomic' => true)));
  3865. }
  3866. /**
  3867. * testSaveAllTransaction method
  3868. *
  3869. * @return void
  3870. */
  3871. public function testSaveAllTransaction() {
  3872. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  3873. $TestModel = new Post();
  3874. $TestModel->validate = array('title' => 'notEmpty');
  3875. $data = array(
  3876. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3877. array('author_id' => 1, 'title' => 'New Fifth Post'),
  3878. array('author_id' => 1, 'title' => '')
  3879. );
  3880. $this->assertFalse($TestModel->saveAll($data));
  3881. $result = $TestModel->find('all', array('recursive' => -1));
  3882. $expected = array(
  3883. array('Post' => array(
  3884. 'id' => '1',
  3885. 'author_id' => 1,
  3886. 'title' => 'First Post',
  3887. 'body' => 'First Post Body',
  3888. 'published' => 'Y',
  3889. 'created' => '2007-03-18 10:39:23',
  3890. 'updated' => '2007-03-18 10:41:31'
  3891. )),
  3892. array('Post' => array(
  3893. 'id' => '2',
  3894. 'author_id' => 3,
  3895. 'title' => 'Second Post',
  3896. 'body' => 'Second Post Body',
  3897. 'published' => 'Y',
  3898. 'created' => '2007-03-18 10:41:23',
  3899. 'updated' => '2007-03-18 10:43:31'
  3900. )),
  3901. array('Post' => array(
  3902. 'id' => '3',
  3903. 'author_id' => 1,
  3904. 'title' => 'Third Post',
  3905. 'body' => 'Third Post Body',
  3906. 'published' => 'Y',
  3907. 'created' => '2007-03-18 10:43:23',
  3908. 'updated' => '2007-03-18 10:45:31'
  3909. )));
  3910. if (count($result) != 3) {
  3911. // Database doesn't support transactions
  3912. $expected[] = array(
  3913. 'Post' => array(
  3914. 'id' => '4',
  3915. 'author_id' => 1,
  3916. 'title' => 'New Fourth Post',
  3917. 'body' => null,
  3918. 'published' => 'N',
  3919. 'created' => self::date(),
  3920. 'updated' => self::date()
  3921. ));
  3922. $expected[] = array(
  3923. 'Post' => array(
  3924. 'id' => '5',
  3925. 'author_id' => 1,
  3926. 'title' => 'New Fifth Post',
  3927. 'body' => null,
  3928. 'published' => 'N',
  3929. 'created' => self::date(),
  3930. 'updated' => self::date()
  3931. ));
  3932. $this->assertEquals($expected, $result);
  3933. // Skip the rest of the transactional tests
  3934. return;
  3935. }
  3936. $this->assertEquals($expected, $result);
  3937. $data = array(
  3938. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3939. array('author_id' => 1, 'title' => ''),
  3940. array('author_id' => 1, 'title' => 'New Sixth Post')
  3941. );
  3942. $this->assertFalse($TestModel->saveAll($data));
  3943. $result = $TestModel->find('all', array('recursive' => -1));
  3944. $expected = array(
  3945. array('Post' => array(
  3946. 'id' => '1',
  3947. 'author_id' => 1,
  3948. 'title' => 'First Post',
  3949. 'body' => 'First Post Body',
  3950. 'published' => 'Y',
  3951. 'created' => '2007-03-18 10:39:23',
  3952. 'updated' => '2007-03-18 10:41:31'
  3953. )),
  3954. array('Post' => array(
  3955. 'id' => '2',
  3956. 'author_id' => 3,
  3957. 'title' => 'Second Post',
  3958. 'body' => 'Second Post Body',
  3959. 'published' => 'Y',
  3960. 'created' => '2007-03-18 10:41:23',
  3961. 'updated' => '2007-03-18 10:43:31'
  3962. )),
  3963. array('Post' => array(
  3964. 'id' => '3',
  3965. 'author_id' => 1,
  3966. 'title' => 'Third Post',
  3967. 'body' => 'Third Post Body',
  3968. 'published' => 'Y',
  3969. 'created' => '2007-03-18 10:43:23',
  3970. 'updated' => '2007-03-18 10:45:31'
  3971. )));
  3972. if (count($result) != 3) {
  3973. // Database doesn't support transactions
  3974. $expected[] = array(
  3975. 'Post' => array(
  3976. 'id' => '4',
  3977. 'author_id' => 1,
  3978. 'title' => 'New Fourth Post',
  3979. 'body' => 'Third Post Body',
  3980. 'published' => 'N',
  3981. 'created' => self::date(),
  3982. 'updated' => self::date()
  3983. ));
  3984. $expected[] = array(
  3985. 'Post' => array(
  3986. 'id' => '5',
  3987. 'author_id' => 1,
  3988. 'title' => 'Third Post',
  3989. 'body' => 'Third Post Body',
  3990. 'published' => 'N',
  3991. 'created' => self::date(),
  3992. 'updated' => self::date()
  3993. ));
  3994. }
  3995. $this->assertEquals($expected, $result);
  3996. $TestModel->validate = array('title' => 'notEmpty');
  3997. $data = array(
  3998. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3999. array('author_id' => 1, 'title' => 'New Fifth Post'),
  4000. array('author_id' => 1, 'title' => 'New Sixth Post')
  4001. );
  4002. $this->assertTrue($TestModel->saveAll($data));
  4003. $result = $TestModel->find('all', array(
  4004. 'recursive' => -1,
  4005. 'fields' => array('author_id', 'title', 'body', 'published'),
  4006. 'order' => array('Post.created' => 'ASC')
  4007. ));
  4008. $expected = array(
  4009. array('Post' => array(
  4010. 'author_id' => 1,
  4011. 'title' => 'First Post',
  4012. 'body' => 'First Post Body',
  4013. 'published' => 'Y'
  4014. )),
  4015. array('Post' => array(
  4016. 'author_id' => 3,
  4017. 'title' => 'Second Post',
  4018. 'body' => 'Second Post Body',
  4019. 'published' => 'Y'
  4020. )),
  4021. array('Post' => array(
  4022. 'author_id' => 1,
  4023. 'title' => 'Third Post',
  4024. 'body' => 'Third Post Body',
  4025. 'published' => 'Y'
  4026. )),
  4027. array('Post' => array(
  4028. 'author_id' => 1,
  4029. 'title' => 'New Fourth Post',
  4030. 'body' => '',
  4031. 'published' => 'N'
  4032. )),
  4033. array('Post' => array(
  4034. 'author_id' => 1,
  4035. 'title' => 'New Fifth Post',
  4036. 'body' => '',
  4037. 'published' => 'N'
  4038. )),
  4039. array('Post' => array(
  4040. 'author_id' => 1,
  4041. 'title' => 'New Sixth Post',
  4042. 'body' => '',
  4043. 'published' => 'N'
  4044. )));
  4045. $this->assertEquals($expected, $result);
  4046. }
  4047. /**
  4048. * testSaveAllValidation method
  4049. *
  4050. * @return void
  4051. */
  4052. public function testSaveAllValidation() {
  4053. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  4054. $TestModel = new Post();
  4055. $data = array(
  4056. array(
  4057. 'id' => '1',
  4058. 'title' => 'Baleeted First Post',
  4059. 'body' => 'Baleeted!',
  4060. 'published' => 'N'
  4061. ),
  4062. array(
  4063. 'id' => '2',
  4064. 'title' => 'Just update the title'
  4065. ),
  4066. array(
  4067. 'title' => 'Creating a fourth post',
  4068. 'body' => 'Fourth post body',
  4069. 'author_id' => 2
  4070. ));
  4071. $this->assertTrue($TestModel->saveAll($data));
  4072. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  4073. $expected = array(
  4074. array(
  4075. 'Post' => array(
  4076. 'id' => '1',
  4077. 'author_id' => '1',
  4078. 'title' => 'Baleeted First Post',
  4079. 'body' => 'Baleeted!',
  4080. 'published' => 'N',
  4081. 'created' => '2007-03-18 10:39:23'
  4082. )),
  4083. array(
  4084. 'Post' => array(
  4085. 'id' => '2',
  4086. 'author_id' => '3',
  4087. 'title' => 'Just update the title',
  4088. 'body' => 'Second Post Body',
  4089. 'published' => 'Y',
  4090. 'created' => '2007-03-18 10:41:23'
  4091. )),
  4092. array(
  4093. 'Post' => array(
  4094. 'id' => '3',
  4095. 'author_id' => '1',
  4096. 'title' => 'Third Post',
  4097. 'body' => 'Third Post Body',
  4098. 'published' => 'Y',
  4099. 'created' => '2007-03-18 10:43:23',
  4100. 'updated' => '2007-03-18 10:45:31'
  4101. )),
  4102. array(
  4103. 'Post' => array(
  4104. 'id' => '4',
  4105. 'author_id' => '2',
  4106. 'title' => 'Creating a fourth post',
  4107. 'body' => 'Fourth post body',
  4108. 'published' => 'N'
  4109. )));
  4110. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  4111. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  4112. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  4113. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  4114. unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
  4115. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  4116. $this->assertEquals($expected, $result);
  4117. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  4118. $data = array(
  4119. array(
  4120. 'id' => '1',
  4121. 'title' => 'Un-Baleeted First Post',
  4122. 'body' => 'Not Baleeted!',
  4123. 'published' => 'Y'
  4124. ),
  4125. array(
  4126. 'id' => '2',
  4127. 'title' => '',
  4128. 'body' => 'Trying to get away with an empty title'
  4129. ));
  4130. $result = $TestModel->saveAll($data);
  4131. $this->assertFalse($result);
  4132. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  4133. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  4134. $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
  4135. if (!$transactionWorked) {
  4136. $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
  4137. $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
  4138. }
  4139. $this->assertEquals($errors, $TestModel->validationErrors);
  4140. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  4141. $data = array(
  4142. array(
  4143. 'id' => '1',
  4144. 'title' => 'Un-Baleeted First Post',
  4145. 'body' => 'Not Baleeted!',
  4146. 'published' => 'Y'
  4147. ),
  4148. array(
  4149. 'id' => '2',
  4150. 'title' => '',
  4151. 'body' => 'Trying to get away with an empty title'
  4152. ));
  4153. $result = $TestModel->saveAll($data, array('validate' => true, 'atomic' => false));
  4154. $this->assertEquals(array(true, false), $result);
  4155. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  4156. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  4157. $expected = array(
  4158. array(
  4159. 'Post' => array(
  4160. 'id' => '1',
  4161. 'author_id' => '1',
  4162. 'title' => 'Un-Baleeted First Post',
  4163. 'body' => 'Not Baleeted!',
  4164. 'published' => 'Y',
  4165. 'created' => '2007-03-18 10:39:23'
  4166. )
  4167. ),
  4168. array(
  4169. 'Post' => array(
  4170. 'id' => '2',
  4171. 'author_id' => '3',
  4172. 'title' => 'Just update the title',
  4173. 'body' => 'Second Post Body',
  4174. 'published' => 'Y',
  4175. 'created' => '2007-03-18 10:41:23'
  4176. )
  4177. ),
  4178. array(
  4179. 'Post' => array(
  4180. 'id' => '3',
  4181. 'author_id' => '1',
  4182. 'title' => 'Third Post',
  4183. 'body' => 'Third Post Body',
  4184. 'published' => 'Y',
  4185. 'created' => '2007-03-18 10:43:23',
  4186. 'updated' => '2007-03-18 10:45:31'
  4187. )
  4188. ),
  4189. array(
  4190. 'Post' => array(
  4191. 'id' => '4',
  4192. 'author_id' => '2',
  4193. 'title' => 'Creating a fourth post',
  4194. 'body' => 'Fourth post body',
  4195. 'published' => 'N'
  4196. )
  4197. )
  4198. );
  4199. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  4200. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  4201. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  4202. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  4203. unset(
  4204. $result[0]['Post']['updated'], $result[1]['Post']['updated'],
  4205. $result[3]['Post']['updated'], $result[3]['Post']['created']
  4206. );
  4207. $this->assertEquals($expected, $result);
  4208. $this->assertEquals($errors, $TestModel->validationErrors);
  4209. $data = array(
  4210. array(
  4211. 'id' => '1',
  4212. 'title' => 'Re-Baleeted First Post',
  4213. 'body' => 'Baleeted!',
  4214. 'published' => 'N'
  4215. ),
  4216. array(
  4217. 'id' => '2',
  4218. 'title' => '',
  4219. 'body' => 'Trying to get away with an empty title'
  4220. ));
  4221. $this->assertFalse($TestModel->saveAll($data, array('validate' => 'first')));
  4222. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  4223. unset(
  4224. $result[0]['Post']['updated'], $result[1]['Post']['updated'],
  4225. $result[3]['Post']['updated'], $result[3]['Post']['created']
  4226. );
  4227. $this->assertEquals($expected, $result);
  4228. $this->assertEquals($errors, $TestModel->validationErrors);
  4229. }
  4230. /**
  4231. * testSaveAllValidationOnly method
  4232. *
  4233. * @return void
  4234. */
  4235. public function testSaveAllValidationOnly() {
  4236. $this->loadFixtures('Comment', 'Attachment');
  4237. $TestModel = new Comment();
  4238. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  4239. $data = array(
  4240. 'Comment' => array(
  4241. 'comment' => 'This is the comment'
  4242. ),
  4243. 'Attachment' => array(
  4244. 'attachment' => ''
  4245. )
  4246. );
  4247. $result = $TestModel->saveAll($data, array('validate' => 'only'));
  4248. $this->assertFalse($result);
  4249. $TestModel = new Article();
  4250. $TestModel->validate = array('title' => 'notEmpty');
  4251. $result = $TestModel->saveAll(
  4252. array(
  4253. 0 => array('title' => ''),
  4254. 1 => array('title' => 'title 1'),
  4255. 2 => array('title' => 'title 2'),
  4256. ),
  4257. array('validate' => 'only')
  4258. );
  4259. $this->assertFalse($result);
  4260. $expected = array(
  4261. 0 => array('title' => array('This field cannot be left blank')),
  4262. );
  4263. $this->assertEquals($expected, $TestModel->validationErrors);
  4264. $result = $TestModel->saveAll(
  4265. array(
  4266. 0 => array('title' => 'title 0'),
  4267. 1 => array('title' => ''),
  4268. 2 => array('title' => 'title 2'),
  4269. ),
  4270. array('validate' => 'only')
  4271. );
  4272. $this->assertFalse($result);
  4273. $expected = array(
  4274. 1 => array('title' => array('This field cannot be left blank')),
  4275. );
  4276. $this->assertEquals($expected, $TestModel->validationErrors);
  4277. }
  4278. /**
  4279. * testSaveAllValidateFirst method
  4280. *
  4281. * @return void
  4282. */
  4283. public function testSaveAllValidateFirst() {
  4284. $this->loadFixtures('Article', 'Comment', 'Attachment', 'User', 'ArticlesTag', 'Tag');
  4285. $model = new Article();
  4286. $model->deleteAll(true);
  4287. $model->Comment->validate = array('comment' => 'notEmpty');
  4288. $result = $model->saveAll(array(
  4289. 'Article' => array(
  4290. 'title' => 'Post with Author',
  4291. 'body' => 'This post will be saved author'
  4292. ),
  4293. 'Comment' => array(
  4294. array('comment' => 'First new comment'),
  4295. array('comment' => '')
  4296. )
  4297. ), array('validate' => 'first'));
  4298. $this->assertFalse($result);
  4299. $result = $model->find('all');
  4300. $this->assertSame(array(), $result);
  4301. $expected = array('Comment' => array(
  4302. 1 => array('comment' => array('This field cannot be left blank'))
  4303. ));
  4304. $this->assertEquals($expected['Comment'], $model->Comment->validationErrors);
  4305. $this->assertSame($model->Comment->find('count'), 0);
  4306. $result = $model->saveAll(
  4307. array(
  4308. 'Article' => array(
  4309. 'title' => 'Post with Author',
  4310. 'body' => 'This post will be saved with an author',
  4311. 'user_id' => 2
  4312. ),
  4313. 'Comment' => array(
  4314. array(
  4315. 'comment' => 'Only new comment',
  4316. 'user_id' => 2
  4317. ))),
  4318. array('validate' => 'first')
  4319. );
  4320. $this->assertTrue($result);
  4321. $result = $model->Comment->find('all');
  4322. $this->assertSame(count($result), 1);
  4323. $result = Hash::extract($result, '{n}.Comment.article_id');
  4324. $this->assertEquals(4, $result[0]);
  4325. $model->deleteAll(true);
  4326. $data = array(
  4327. 'Article' => array(
  4328. 'title' => 'Post with Author saveAlled from comment',
  4329. 'body' => 'This post will be saved with an author',
  4330. 'user_id' => 2
  4331. ),
  4332. 'Comment' => array(
  4333. 'comment' => 'Only new comment', 'user_id' => 2
  4334. ));
  4335. $result = $model->Comment->saveAll($data, array('validate' => 'first'));
  4336. $this->assertFalse(empty($result));
  4337. $result = $model->find('all');
  4338. $this->assertEquals(
  4339. $result[0]['Article']['title'],
  4340. 'Post with Author saveAlled from comment'
  4341. );
  4342. $this->assertEquals('Only new comment', $result[0]['Comment'][0]['comment']);
  4343. }
  4344. /**
  4345. * test saveAll()'s return is correct when using atomic = false and validate = first.
  4346. *
  4347. * @return void
  4348. */
  4349. public function testSaveAllValidateFirstAtomicFalse() {
  4350. $this->loadFixtures('Something');
  4351. $Something = new Something();
  4352. $invalidData = array(
  4353. array(
  4354. 'title' => 'foo',
  4355. 'body' => 'bar',
  4356. 'published' => 'baz',
  4357. ),
  4358. array(
  4359. 'body' => 3,
  4360. 'published' => 'sd',
  4361. ),
  4362. );
  4363. $Something->create();
  4364. $Something->validate = array(
  4365. 'title' => array(
  4366. 'rule' => 'alphaNumeric',
  4367. 'required' => true,
  4368. ),
  4369. 'body' => array(
  4370. 'rule' => 'alphaNumeric',
  4371. 'required' => true,
  4372. 'allowEmpty' => true,
  4373. ),
  4374. );
  4375. $result = $Something->saveAll($invalidData, array(
  4376. 'atomic' => false,
  4377. 'validate' => 'first',
  4378. ));
  4379. $expected = array(true, false);
  4380. $this->assertEquals($expected, $result);
  4381. $Something = new Something();
  4382. $validData = array(
  4383. array(
  4384. 'title' => 'title value',
  4385. 'body' => 'body value',
  4386. 'published' => 'baz',
  4387. ),
  4388. array(
  4389. 'title' => 'valid',
  4390. 'body' => 'this body',
  4391. 'published' => 'sd',
  4392. ),
  4393. );
  4394. $Something->create();
  4395. $result = $Something->saveAll($validData, array(
  4396. 'atomic' => false,
  4397. 'validate' => 'first',
  4398. ));
  4399. $expected = array(true, true);
  4400. $this->assertEquals($expected, $result);
  4401. }
  4402. /**
  4403. * testSaveAllHasManyValidationOnly method
  4404. *
  4405. * @return void
  4406. */
  4407. public function testSaveAllHasManyValidationOnly() {
  4408. $this->loadFixtures('Article', 'Comment', 'Attachment');
  4409. $TestModel = new Article();
  4410. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4411. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  4412. $result = $TestModel->saveAll(
  4413. array(
  4414. 'Article' => array('id' => 2),
  4415. 'Comment' => array(
  4416. array(
  4417. 'id' => 1,
  4418. 'comment' => '',
  4419. 'published' => 'Y',
  4420. 'user_id' => 1),
  4421. array(
  4422. 'id' => 2,
  4423. 'comment' =>
  4424. 'comment',
  4425. 'published' => 'Y',
  4426. 'user_id' => 1
  4427. ))),
  4428. array('validate' => 'only')
  4429. );
  4430. $this->assertFalse($result);
  4431. $result = $TestModel->saveAll(
  4432. array(
  4433. 'Article' => array('id' => 2),
  4434. 'Comment' => array(
  4435. array(
  4436. 'id' => 1,
  4437. 'comment' => '',
  4438. 'published' => 'Y',
  4439. 'user_id' => 1
  4440. ),
  4441. array(
  4442. 'id' => 2,
  4443. 'comment' => 'comment',
  4444. 'published' => 'Y',
  4445. 'user_id' => 1
  4446. ),
  4447. array(
  4448. 'id' => 3,
  4449. 'comment' => '',
  4450. 'published' => 'Y',
  4451. 'user_id' => 1
  4452. ))),
  4453. array(
  4454. 'validate' => 'only',
  4455. 'atomic' => false
  4456. ));
  4457. $expected = array(
  4458. 'Article' => true,
  4459. 'Comment' => array(false, true, false)
  4460. );
  4461. $this->assertSame($expected, $result);
  4462. $expected = array('Comment' => array(
  4463. 0 => array('comment' => array('This field cannot be left blank')),
  4464. 2 => array('comment' => array('This field cannot be left blank'))
  4465. ));
  4466. $this->assertEquals($expected, $TestModel->validationErrors);
  4467. $expected = array(
  4468. 0 => array('comment' => array('This field cannot be left blank')),
  4469. 2 => array('comment' => array('This field cannot be left blank'))
  4470. );
  4471. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  4472. }
  4473. /**
  4474. * test that saveAll still behaves like previous versions (does not necessarily need a first argument)
  4475. *
  4476. * @return void
  4477. */
  4478. public function testSaveAllWithSet() {
  4479. $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  4480. $data = array(
  4481. 'Article' => array(
  4482. 'user_id' => 1,
  4483. 'title' => 'Article Has and belongs to Many Tags'
  4484. ),
  4485. 'Tag' => array(
  4486. 'Tag' => array(1, 2)
  4487. ),
  4488. 'Comment' => array(
  4489. array(
  4490. 'comment' => 'Article comment',
  4491. 'user_id' => 1
  4492. )));
  4493. $Article = new Article();
  4494. $Article->set($data);
  4495. $result = $Article->saveAll();
  4496. $this->assertFalse(empty($result));
  4497. }
  4498. /**
  4499. * test that saveAll behaves like plain save() when supplied empty data
  4500. *
  4501. * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  4502. * @return void
  4503. */
  4504. public function testSaveAllEmptyData() {
  4505. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  4506. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  4507. $model = new Article();
  4508. $result = $model->saveAll(array(), array('validate' => 'first'));
  4509. $this->assertFalse(empty($result));
  4510. $model = new ProductUpdateAll();
  4511. $result = $model->saveAll();
  4512. $this->assertFalse($result);
  4513. }
  4514. /**
  4515. * testSaveAssociated method
  4516. *
  4517. * @return void
  4518. */
  4519. public function testSaveAssociated() {
  4520. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  4521. $TestModel = new Post();
  4522. $result = $TestModel->find('all');
  4523. $this->assertEquals(3, count($result));
  4524. $this->assertFalse(isset($result[3]));
  4525. $TestModel->saveAssociated(array(
  4526. 'Post' => array(
  4527. 'title' => 'Post with Author',
  4528. 'body' => 'This post will be saved with an author'
  4529. ),
  4530. 'Author' => array(
  4531. 'user' => 'bob',
  4532. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
  4533. )));
  4534. $result = $TestModel->find('all', array('order' => array('Post.id ' => 'ASC')));
  4535. $expected = array(
  4536. 'Post' => array(
  4537. 'id' => '4',
  4538. 'author_id' => '5',
  4539. 'title' => 'Post with Author',
  4540. 'body' => 'This post will be saved with an author',
  4541. 'published' => 'N'
  4542. ),
  4543. 'Author' => array(
  4544. 'id' => '5',
  4545. 'user' => 'bob',
  4546. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
  4547. 'test' => 'working'
  4548. ));
  4549. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  4550. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  4551. $this->assertEquals(self::date(), $result[3]['Author']['created']);
  4552. $this->assertEquals(self::date(), $result[3]['Author']['updated']);
  4553. unset(
  4554. $result[3]['Post']['updated'], $result[3]['Post']['created'],
  4555. $result[3]['Author']['updated'], $result[3]['Author']['created']
  4556. );
  4557. $this->assertEquals($expected, $result[3]);
  4558. $this->assertEquals(4, count($result));
  4559. $TestModel = new Comment();
  4560. $result = $TestModel->saveAssociated(array(
  4561. 'Comment' => array(
  4562. 'article_id' => 2,
  4563. 'user_id' => 2,
  4564. 'comment' => 'New comment with attachment',
  4565. 'published' => 'Y'
  4566. ),
  4567. 'Attachment' => array(
  4568. 'attachment' => 'some_file.tgz'
  4569. )));
  4570. $this->assertFalse(empty($result));
  4571. $result = $TestModel->find('all');
  4572. $expected = array(
  4573. 'id' => '7',
  4574. 'article_id' => '2',
  4575. 'user_id' => '2',
  4576. 'comment' => 'New comment with attachment',
  4577. 'published' => 'Y'
  4578. );
  4579. $this->assertEquals(self::date(), $result[6]['Comment']['updated']);
  4580. $this->assertEquals(self::date(), $result[6]['Comment']['created']);
  4581. unset($result[6]['Comment']['updated'], $result[6]['Comment']['created']);
  4582. $this->assertEquals($expected, $result[6]['Comment']);
  4583. $expected = array(
  4584. 'id' => '2',
  4585. 'comment_id' => '7',
  4586. 'attachment' => 'some_file.tgz'
  4587. );
  4588. $this->assertEquals(self::date(), $result[6]['Attachment']['updated']);
  4589. $this->assertEquals(self::date(), $result[6]['Attachment']['created']);
  4590. unset($result[6]['Attachment']['updated'], $result[6]['Attachment']['created']);
  4591. $this->assertEquals($expected, $result[6]['Attachment']);
  4592. }
  4593. /**
  4594. * Test that validate = first, atomic = false works when associated records
  4595. * fail validation.
  4596. *
  4597. * @return void
  4598. */
  4599. public function testSaveAssociatedAtomicFalseValidateFirstWithErrors() {
  4600. $this->loadFixtures('Comment', 'Article', 'User');
  4601. $Article = ClassRegistry::init('Article');
  4602. $Article->Comment->validator()->add('comment', array(
  4603. array('rule' => 'notEmpty')
  4604. ));
  4605. $data = array(
  4606. 'Article' => array(
  4607. 'user_id' => 1,
  4608. 'title' => 'Foo',
  4609. 'body' => 'text',
  4610. 'published' => 'N'
  4611. ),
  4612. 'Comment' => array(
  4613. array(
  4614. 'user_id' => 1,
  4615. 'comment' => '',
  4616. 'published' => 'N',
  4617. )
  4618. ),
  4619. );
  4620. $Article->saveAssociated(
  4621. $data,
  4622. array('validate' => 'first', 'atomic' => false)
  4623. );
  4624. $result = $Article->validationErrors;
  4625. $expected = array(
  4626. 'Comment' => array(
  4627. array(
  4628. 'comment' => array('This field cannot be left blank')
  4629. )
  4630. )
  4631. );
  4632. $this->assertEquals($expected, $result);
  4633. }
  4634. /**
  4635. * testSaveMany method
  4636. *
  4637. * @return void
  4638. */
  4639. public function testSaveMany() {
  4640. $this->loadFixtures('Post');
  4641. $TestModel = new Post();
  4642. $TestModel->deleteAll(true);
  4643. $this->assertEquals(array(), $TestModel->find('all'));
  4644. // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
  4645. $this->db->truncate($TestModel);
  4646. $TestModel->saveMany(array(
  4647. array(
  4648. 'title' => 'Multi-record post 1',
  4649. 'body' => 'First multi-record post',
  4650. 'author_id' => 2
  4651. ),
  4652. array(
  4653. 'title' => 'Multi-record post 2',
  4654. 'body' => 'Second multi-record post',
  4655. 'author_id' => 2
  4656. )));
  4657. $result = $TestModel->find('all', array(
  4658. 'recursive' => -1,
  4659. 'order' => 'Post.id ASC'
  4660. ));
  4661. $expected = array(
  4662. array(
  4663. 'Post' => array(
  4664. 'id' => '1',
  4665. 'author_id' => '2',
  4666. 'title' => 'Multi-record post 1',
  4667. 'body' => 'First multi-record post',
  4668. 'published' => 'N'
  4669. )
  4670. ),
  4671. array(
  4672. 'Post' => array(
  4673. 'id' => '2',
  4674. 'author_id' => '2',
  4675. 'title' => 'Multi-record post 2',
  4676. 'body' => 'Second multi-record post',
  4677. 'published' => 'N'
  4678. )
  4679. )
  4680. );
  4681. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  4682. $this->assertEquals(self::date(), $result[0]['Post']['created']);
  4683. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  4684. $this->assertEquals(self::date(), $result[1]['Post']['created']);
  4685. unset($result[0]['Post']['updated'], $result[0]['Post']['created']);
  4686. unset($result[1]['Post']['updated'], $result[1]['Post']['created']);
  4687. $this->assertEquals($expected, $result);
  4688. }
  4689. /**
  4690. * Test SaveMany with validate=false.
  4691. *
  4692. * @return void
  4693. */
  4694. public function testSaveManyValidateFalse() {
  4695. $this->loadFixtures('Post');
  4696. $TestModel = new Post();
  4697. $TestModel->deleteAll(true);
  4698. $data = array(
  4699. array('id' => 1, 'author_id' => 1, 'title' => 'hi'),
  4700. array('id' => 2, 'author_id' => 1, 'title' => 'bye')
  4701. );
  4702. $result = $TestModel->saveAll($data, array('validate' => false));
  4703. $this->assertTrue($result);
  4704. }
  4705. /**
  4706. * Test SaveAssociated with Habtm relations
  4707. *
  4708. * @return void
  4709. */
  4710. public function testSaveAssociatedHabtm() {
  4711. $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  4712. $data = array(
  4713. 'Article' => array(
  4714. 'user_id' => 1,
  4715. 'title' => 'Article Has and belongs to Many Tags'
  4716. ),
  4717. 'Tag' => array(
  4718. 'Tag' => array(1, 2)
  4719. ),
  4720. 'Comment' => array(
  4721. array(
  4722. 'comment' => 'Article comment',
  4723. 'user_id' => 1
  4724. )));
  4725. $Article = new Article();
  4726. $result = $Article->saveAssociated($data);
  4727. $this->assertFalse(empty($result));
  4728. $result = $Article->read();
  4729. $this->assertEquals(2, count($result['Tag']));
  4730. $this->assertEquals('tag1', $result['Tag'][0]['tag']);
  4731. $this->assertEquals(1, count($result['Comment']));
  4732. $this->assertEquals(1, count($result['Comment'][0]['comment']));
  4733. }
  4734. /**
  4735. * Test SaveAssociated with Habtm relations and extra join table fields
  4736. *
  4737. * @return void
  4738. */
  4739. public function testSaveAssociatedHabtmWithExtraJoinTableFields() {
  4740. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  4741. $data = array(
  4742. 'Something' => array(
  4743. 'id' => 4,
  4744. 'title' => 'Extra Fields',
  4745. 'body' => 'Extra Fields Body',
  4746. 'published' => '1'
  4747. ),
  4748. 'SomethingElse' => array(
  4749. array('something_else_id' => 1, 'doomed' => '1'),
  4750. array('something_else_id' => 2, 'doomed' => '0'),
  4751. array('something_else_id' => 3, 'doomed' => '1')
  4752. )
  4753. );
  4754. $Something = new Something();
  4755. $result = $Something->saveAssociated($data);
  4756. $this->assertFalse(empty($result));
  4757. $result = $Something->read();
  4758. $this->assertEquals(3, count($result['SomethingElse']));
  4759. $this->assertTrue(Set::matches('/Something[id=4]', $result));
  4760. $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
  4761. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
  4762. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
  4763. $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
  4764. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
  4765. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
  4766. $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
  4767. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
  4768. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
  4769. }
  4770. /**
  4771. * testSaveAssociatedHasOne method
  4772. *
  4773. * @return void
  4774. */
  4775. public function testSaveAssociatedHasOne() {
  4776. $model = new Comment();
  4777. $model->deleteAll(true);
  4778. $this->assertEquals(array(), $model->find('all'));
  4779. $model->Attachment->deleteAll(true);
  4780. $this->assertEquals(array(), $model->Attachment->find('all'));
  4781. $this->assertTrue($model->saveAssociated(array(
  4782. 'Comment' => array(
  4783. 'comment' => 'Comment with attachment',
  4784. 'article_id' => 1,
  4785. 'user_id' => 1
  4786. ),
  4787. 'Attachment' => array(
  4788. 'attachment' => 'some_file.zip'
  4789. ))));
  4790. $result = $model->find('all', array('fields' => array(
  4791. 'Comment.id', 'Comment.comment', 'Attachment.id',
  4792. 'Attachment.comment_id', 'Attachment.attachment'
  4793. )));
  4794. $expected = array(array(
  4795. 'Comment' => array(
  4796. 'id' => '1',
  4797. 'comment' => 'Comment with attachment'
  4798. ),
  4799. 'Attachment' => array(
  4800. 'id' => '1',
  4801. 'comment_id' => '1',
  4802. 'attachment' => 'some_file.zip'
  4803. )));
  4804. $this->assertEquals($expected, $result);
  4805. $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
  4806. $data = array(
  4807. 'Comment' => array(
  4808. 'comment' => 'Comment with attachment',
  4809. 'article_id' => 1,
  4810. 'user_id' => 1
  4811. ),
  4812. 'Attachment' => array(
  4813. 'attachment' => 'some_file.zip'
  4814. ));
  4815. $this->assertTrue($model->saveAssociated($data, array('validate' => 'first')));
  4816. }
  4817. /**
  4818. * testSaveAssociatedBelongsTo method
  4819. *
  4820. * @return void
  4821. */
  4822. public function testSaveAssociatedBelongsTo() {
  4823. $model = new Comment();
  4824. $model->deleteAll(true);
  4825. $this->assertEquals(array(), $model->find('all'));
  4826. $model->Article->deleteAll(true);
  4827. $this->assertEquals(array(), $model->Article->find('all'));
  4828. $this->assertTrue($model->saveAssociated(array(
  4829. 'Comment' => array(
  4830. 'comment' => 'Article comment',
  4831. 'article_id' => 1,
  4832. 'user_id' => 1
  4833. ),
  4834. 'Article' => array(
  4835. 'title' => 'Model Associations 101',
  4836. 'user_id' => 1
  4837. ))));
  4838. $result = $model->find('all', array('fields' => array(
  4839. 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
  4840. )));
  4841. $expected = array(array(
  4842. 'Comment' => array(
  4843. 'id' => '1',
  4844. 'article_id' => '1',
  4845. 'comment' => 'Article comment'
  4846. ),
  4847. 'Article' => array(
  4848. 'id' => '1',
  4849. 'title' => 'Model Associations 101'
  4850. )));
  4851. $this->assertEquals($expected, $result);
  4852. }
  4853. /**
  4854. * testSaveAssociatedHasOneValidation method
  4855. *
  4856. * @return void
  4857. */
  4858. public function testSaveAssociatedHasOneValidation() {
  4859. $model = new Comment();
  4860. $model->deleteAll(true);
  4861. $this->assertEquals(array(), $model->find('all'));
  4862. $model->Attachment->deleteAll(true);
  4863. $this->assertEquals(array(), $model->Attachment->find('all'));
  4864. $model->validate = array('comment' => 'notEmpty');
  4865. $model->Attachment->validate = array('attachment' => 'notEmpty');
  4866. $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
  4867. $result = $model->saveAssociated(
  4868. array(
  4869. 'Comment' => array(
  4870. 'comment' => '',
  4871. 'article_id' => 1,
  4872. 'user_id' => 1
  4873. ),
  4874. 'Attachment' => array('attachment' => '')
  4875. )
  4876. );
  4877. $this->assertFalse($result);
  4878. $expected = array(
  4879. 'comment' => array(
  4880. 'This field cannot be left blank'
  4881. ),
  4882. 'Attachment' => array(
  4883. 'attachment' => array(
  4884. 'This field cannot be left blank'
  4885. )
  4886. )
  4887. );
  4888. $this->assertEquals($expected, $model->validationErrors);
  4889. $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
  4890. }
  4891. /**
  4892. * testSaveAssociatedAtomic method
  4893. *
  4894. * @return void
  4895. */
  4896. public function testSaveAssociatedAtomic() {
  4897. $this->loadFixtures('Article', 'User');
  4898. $TestModel = new Article();
  4899. $result = $TestModel->saveAssociated(array(
  4900. 'Article' => array(
  4901. 'title' => 'Post with Author',
  4902. 'body' => 'This post will be saved with an author',
  4903. 'user_id' => 2
  4904. ),
  4905. 'Comment' => array(
  4906. array('comment' => 'First new comment', 'user_id' => 2))
  4907. ), array('atomic' => false));
  4908. $this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
  4909. $result = $TestModel->saveAssociated(array(
  4910. 'Article' => array('id' => 2),
  4911. 'Comment' => array(
  4912. array(
  4913. 'comment' => 'First new comment',
  4914. 'published' => 'Y',
  4915. 'user_id' => 1
  4916. ),
  4917. array(
  4918. 'comment' => 'Second new comment',
  4919. 'published' => 'Y',
  4920. 'user_id' => 2
  4921. ))
  4922. ), array('validate' => true, 'atomic' => false));
  4923. $this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
  4924. }
  4925. /**
  4926. * testSaveManyAtomic method
  4927. *
  4928. * @return void
  4929. */
  4930. public function testSaveManyAtomic() {
  4931. $this->loadFixtures('Article', 'User');
  4932. $TestModel = new Article();
  4933. $result = $TestModel->saveMany(array(
  4934. array(
  4935. 'id' => '1',
  4936. 'title' => 'Baleeted First Post',
  4937. 'body' => 'Baleeted!',
  4938. 'published' => 'N'
  4939. ),
  4940. array(
  4941. 'id' => '2',
  4942. 'title' => 'Just update the title'
  4943. ),
  4944. array(
  4945. 'title' => 'Creating a fourth post',
  4946. 'body' => 'Fourth post body',
  4947. 'user_id' => 2
  4948. )
  4949. ), array('atomic' => false));
  4950. $this->assertSame($result, array(true, true, true));
  4951. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  4952. $result = $TestModel->saveMany(array(
  4953. array(
  4954. 'id' => '1',
  4955. 'title' => 'Un-Baleeted First Post',
  4956. 'body' => 'Not Baleeted!',
  4957. 'published' => 'Y'
  4958. ),
  4959. array(
  4960. 'id' => '2',
  4961. 'title' => '',
  4962. 'body' => 'Trying to get away with an empty title'
  4963. )
  4964. ), array('validate' => true, 'atomic' => false));
  4965. $this->assertSame(array(true, false), $result);
  4966. }
  4967. /**
  4968. * testSaveAssociatedHasMany method
  4969. *
  4970. * @return void
  4971. */
  4972. public function testSaveAssociatedHasMany() {
  4973. $this->loadFixtures('Article', 'Comment');
  4974. $TestModel = new Article();
  4975. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4976. $result = $TestModel->saveAssociated(array(
  4977. 'Article' => array('id' => 2),
  4978. 'Comment' => array(
  4979. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  4980. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  4981. )
  4982. ));
  4983. $this->assertFalse(empty($result));
  4984. $result = $TestModel->findById(2);
  4985. $expected = array(
  4986. 'First Comment for Second Article',
  4987. 'Second Comment for Second Article',
  4988. 'First new comment',
  4989. 'Second new comment'
  4990. );
  4991. $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
  4992. $result = $TestModel->saveAssociated(
  4993. array(
  4994. 'Article' => array('id' => 2),
  4995. 'Comment' => array(
  4996. array(
  4997. 'comment' => 'Third new comment',
  4998. 'published' => 'Y',
  4999. 'user_id' => 1
  5000. ))),
  5001. array('atomic' => false)
  5002. );
  5003. $this->assertFalse(empty($result));
  5004. $result = $TestModel->findById(2);
  5005. $expected = array(
  5006. 'First Comment for Second Article',
  5007. 'Second Comment for Second Article',
  5008. 'First new comment',
  5009. 'Second new comment',
  5010. 'Third new comment'
  5011. );
  5012. $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
  5013. $TestModel->beforeSaveReturn = false;
  5014. $result = $TestModel->saveAssociated(
  5015. array(
  5016. 'Article' => array('id' => 2),
  5017. 'Comment' => array(
  5018. array(
  5019. 'comment' => 'Fourth new comment',
  5020. 'published' => 'Y',
  5021. 'user_id' => 1
  5022. ))),
  5023. array('atomic' => false)
  5024. );
  5025. $this->assertEquals(array('Article' => false), $result);
  5026. $result = $TestModel->findById(2);
  5027. $expected = array(
  5028. 'First Comment for Second Article',
  5029. 'Second Comment for Second Article',
  5030. 'First new comment',
  5031. 'Second new comment',
  5032. 'Third new comment'
  5033. );
  5034. $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
  5035. }
  5036. /**
  5037. * testSaveAssociatedHasManyEmpty method
  5038. *
  5039. * @return void
  5040. */
  5041. public function testSaveAssociatedHasManyEmpty() {
  5042. $this->loadFixtures('Article', 'Comment');
  5043. $TestModel = new Article();
  5044. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  5045. $TestModel->validate = $TestModel->Comment->validate = array('user_id' => array('notEmpty' => array('rule' => 'notEmpty', 'required' => true)));
  5046. //empty hasMany data is ignored in save
  5047. $result = $TestModel->saveAssociated(array(
  5048. 'Article' => array('title' => 'title', 'user_id' => 1),
  5049. 'Comment' => array()
  5050. ), array('validate' => true));
  5051. $this->assertTrue($result);
  5052. $result = $TestModel->saveAssociated(array(
  5053. 'Article' => array('title' => 'title', 'user_id' => 1),
  5054. 'Comment' => array()
  5055. ), array('validate' => true, 'atomic' => false));
  5056. $this->assertEquals(array('Article' => true), $result);
  5057. //empty primary data is not ignored
  5058. $result = $TestModel->saveAssociated(array('Article' => array()), array('validate' => true));
  5059. $this->assertFalse($result);
  5060. $result = $TestModel->saveAssociated(array('Article' => array()), array('validate' => true, 'atomic' => false));
  5061. $this->assertEquals(array('Article' => false), $result);
  5062. }
  5063. /**
  5064. * testSaveAssociatedHasManyValidation method
  5065. *
  5066. * @return void
  5067. */
  5068. public function testSaveAssociatedHasManyValidation() {
  5069. $this->loadFixtures('Article', 'Comment');
  5070. $TestModel = new Article();
  5071. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  5072. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  5073. $result = $TestModel->saveAssociated(array(
  5074. 'Article' => array('id' => 2),
  5075. 'Comment' => array(
  5076. array('comment' => '', 'published' => 'Y', 'user_id' => 1),
  5077. )
  5078. ), array('validate' => true));
  5079. $this->assertFalse($result);
  5080. $expected = array('Comment' => array(
  5081. array('comment' => array('This field cannot be left blank'))
  5082. ));
  5083. $this->assertEquals($expected, $TestModel->validationErrors);
  5084. $expected = array(
  5085. array('comment' => array('This field cannot be left blank'))
  5086. );
  5087. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  5088. $result = $TestModel->saveAssociated(array(
  5089. 'Article' => array('id' => 2),
  5090. 'Comment' => array(
  5091. array(
  5092. 'comment' => '',
  5093. 'published' => 'Y',
  5094. 'user_id' => 1
  5095. ))
  5096. ), array('validate' => 'first'));
  5097. $this->assertFalse($result);
  5098. }
  5099. /**
  5100. * test saveMany with transactions and ensure there is no missing rollback.
  5101. *
  5102. * @return void
  5103. */
  5104. public function testSaveManyTransactionNoRollback() {
  5105. $this->loadFixtures('Post');
  5106. $Post = new TestPost();
  5107. $Post->validate = array(
  5108. 'title' => array('rule' => array('notEmpty'))
  5109. );
  5110. // If validation error occurs, rollback() should be called.
  5111. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  5112. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  5113. $db->expects($this->never())->method('commit');
  5114. $db->expects($this->once())->method('rollback');
  5115. $Post->setDataSourceObject($db);
  5116. $data = array(
  5117. array('author_id' => 1, 'title' => 'New Fourth Post'),
  5118. array('author_id' => 1, 'title' => '')
  5119. );
  5120. $Post->saveMany($data, array('validate' => true));
  5121. // If exception thrown, rollback() should be called too.
  5122. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  5123. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  5124. $db->expects($this->never())->method('commit');
  5125. $db->expects($this->once())->method('rollback');
  5126. $Post->setDataSourceObject($db);
  5127. $data = array(
  5128. array('author_id' => 1, 'title' => 'New Fourth Post'),
  5129. array('author_id' => 1, 'title' => 'New Fifth Post', 'body' => $db->expression('PDO_EXCEPTION()'))
  5130. );
  5131. try {
  5132. $Post->saveMany($data, array('validate' => true));
  5133. $this->fail('No exception thrown');
  5134. } catch (PDOException $e) {
  5135. }
  5136. // Otherwise, commit() should be called.
  5137. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  5138. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  5139. $db->expects($this->once())->method('commit');
  5140. $db->expects($this->never())->method('rollback');
  5141. $Post->setDataSourceObject($db);
  5142. $data = array(
  5143. array('author_id' => 1, 'title' => 'New Fourth Post'),
  5144. array('author_id' => 1, 'title' => 'New Fifth Post')
  5145. );
  5146. $Post->saveMany($data, array('validate' => true));
  5147. }
  5148. /**
  5149. * test saveAssociated with transactions and ensure there is no missing rollback.
  5150. *
  5151. * @return void
  5152. */
  5153. public function testSaveAssociatedTransactionNoRollback() {
  5154. $this->loadFixtures('Post', 'Author');
  5155. $Post = new TestPost();
  5156. $Post->Author->validate = array(
  5157. 'user' => array('rule' => array('notEmpty'))
  5158. );
  5159. // If validation error occurs, rollback() should be called.
  5160. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  5161. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  5162. $db->expects($this->never())->method('commit');
  5163. $db->expects($this->once())->method('rollback');
  5164. $Post->setDataSourceObject($db);
  5165. $Post->Author->setDataSourceObject($db);
  5166. $data = array(
  5167. 'Post' => array(
  5168. 'title' => 'New post',
  5169. 'body' => 'Content',
  5170. 'published' => 'Y'
  5171. ),
  5172. 'Author' => array(
  5173. 'user' => '',
  5174. 'password' => "sekret"
  5175. )
  5176. );
  5177. $Post->saveAssociated($data, array('validate' => true, 'atomic' => true));
  5178. // If exception thrown, commit() should be called.
  5179. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  5180. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  5181. $db->expects($this->never())->method('commit');
  5182. $db->expects($this->once())->method('rollback');
  5183. $Post->setDataSourceObject($db);
  5184. $Post->Author->setDataSourceObject($db);
  5185. $data = array(
  5186. 'Post' => array(
  5187. 'title' => 'New post',
  5188. 'body' => $db->expression('PDO_EXCEPTION()'),
  5189. 'published' => 'Y'
  5190. ),
  5191. 'Author' => array(
  5192. 'user' => 'New user',
  5193. 'password' => "sekret"
  5194. )
  5195. );
  5196. try {
  5197. $Post->saveAssociated($data, array('validate' => true, 'atomic' => true));
  5198. $this->fail('No exception thrown');
  5199. } catch (PDOException $e) {
  5200. }
  5201. // Otherwise, commit() should be called.
  5202. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  5203. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  5204. $db->expects($this->once())->method('commit');
  5205. $db->expects($this->never())->method('rollback');
  5206. $Post->setDataSourceObject($db);
  5207. $Post->Author->setDataSourceObject($db);
  5208. $data = array(
  5209. 'Post' => array(
  5210. 'title' => 'New post',
  5211. 'body' => 'Content',
  5212. 'published' => 'Y'
  5213. ),
  5214. 'Author' => array(
  5215. 'user' => 'New user',
  5216. 'password' => "sekret"
  5217. )
  5218. );
  5219. $Post->saveAssociated($data, array('validate' => true, 'atomic' => true));
  5220. }
  5221. /**
  5222. * test saveMany with nested saveMany call.
  5223. *
  5224. * @return void
  5225. */
  5226. public function testSaveManyNestedSaveMany() {
  5227. $this->loadFixtures('Sample');
  5228. $TransactionManyTestModel = new TransactionManyTestModel();
  5229. $data = array(
  5230. array('apple_id' => 1, 'name' => 'sample5'),
  5231. );
  5232. $this->assertTrue($TransactionManyTestModel->saveMany($data, array('atomic' => true)));
  5233. }
  5234. /**
  5235. * testSaveManyTransaction method
  5236. *
  5237. * @return void
  5238. */
  5239. public function testSaveManyTransaction() {
  5240. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  5241. $TestModel = new Post();
  5242. $TestModel->validate = array('title' => 'notEmpty');
  5243. $data = array(
  5244. array('author_id' => 1, 'title' => 'New Fourth Post'),
  5245. array('author_id' => 1, 'title' => 'New Fifth Post'),
  5246. array('author_id' => 1, 'title' => '')
  5247. );
  5248. $this->assertFalse($TestModel->saveMany($data));
  5249. $result = $TestModel->find('all', array('recursive' => -1));
  5250. $expected = array(
  5251. array('Post' => array(
  5252. 'id' => '1',
  5253. 'author_id' => 1,
  5254. 'title' => 'First Post',
  5255. 'body' => 'First Post Body',
  5256. 'published' => 'Y',
  5257. 'created' => '2007-03-18 10:39:23',
  5258. 'updated' => '2007-03-18 10:41:31'
  5259. )),
  5260. array('Post' => array(
  5261. 'id' => '2',
  5262. 'author_id' => 3,
  5263. 'title' => 'Second Post',
  5264. 'body' => 'Second Post Body',
  5265. 'published' => 'Y',
  5266. 'created' => '2007-03-18 10:41:23',
  5267. 'updated' => '2007-03-18 10:43:31'
  5268. )),
  5269. array('Post' => array(
  5270. 'id' => '3',
  5271. 'author_id' => 1,
  5272. 'title' => 'Third Post',
  5273. 'body' => 'Third Post Body',
  5274. 'published' => 'Y',
  5275. 'created' => '2007-03-18 10:43:23',
  5276. 'updated' => '2007-03-18 10:45:31'
  5277. )));
  5278. if (count($result) != 3) {
  5279. // Database doesn't support transactions
  5280. $expected[] = array(
  5281. 'Post' => array(
  5282. 'id' => '4',
  5283. 'author_id' => 1,
  5284. 'title' => 'New Fourth Post',
  5285. 'body' => null,
  5286. 'published' => 'N'
  5287. ));
  5288. $expected[] = array(
  5289. 'Post' => array(
  5290. 'id' => '5',
  5291. 'author_id' => 1,
  5292. 'title' => 'New Fifth Post',
  5293. 'body' => null,
  5294. 'published' => 'N',
  5295. ));
  5296. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  5297. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  5298. $this->assertEquals(self::date(), $result[4]['Post']['created']);
  5299. $this->assertEquals(self::date(), $result[4]['Post']['updated']);
  5300. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  5301. unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
  5302. $this->assertEquals($expected, $result);
  5303. // Skip the rest of the transactional tests
  5304. return;
  5305. }
  5306. $this->assertEquals($expected, $result);
  5307. $data = array(
  5308. array('author_id' => 1, 'title' => 'New Fourth Post'),
  5309. array('author_id' => 1, 'title' => ''),
  5310. array('author_id' => 1, 'title' => 'New Sixth Post')
  5311. );
  5312. $this->assertFalse($TestModel->saveMany($data));
  5313. $result = $TestModel->find('all', array('recursive' => -1));
  5314. $expected = array(
  5315. array('Post' => array(
  5316. 'id' => '1',
  5317. 'author_id' => 1,
  5318. 'title' => 'First Post',
  5319. 'body' => 'First Post Body',
  5320. 'published' => 'Y',
  5321. 'created' => '2007-03-18 10:39:23',
  5322. 'updated' => '2007-03-18 10:41:31'
  5323. )),
  5324. array('Post' => array(
  5325. 'id' => '2',
  5326. 'author_id' => 3,
  5327. 'title' => 'Second Post',
  5328. 'body' => 'Second Post Body',
  5329. 'published' => 'Y',
  5330. 'created' => '2007-03-18 10:41:23',
  5331. 'updated' => '2007-03-18 10:43:31'
  5332. )),
  5333. array('Post' => array(
  5334. 'id' => '3',
  5335. 'author_id' => 1,
  5336. 'title' => 'Third Post',
  5337. 'body' => 'Third Post Body',
  5338. 'published' => 'Y',
  5339. 'created' => '2007-03-18 10:43:23',
  5340. 'updated' => '2007-03-18 10:45:31'
  5341. )));
  5342. if (count($result) != 3) {
  5343. // Database doesn't support transactions
  5344. $expected[] = array(
  5345. 'Post' => array(
  5346. 'id' => '4',
  5347. 'author_id' => 1,
  5348. 'title' => 'New Fourth Post',
  5349. 'body' => 'Third Post Body',
  5350. 'published' => 'N'
  5351. ));
  5352. $expected[] = array(
  5353. 'Post' => array(
  5354. 'id' => '5',
  5355. 'author_id' => 1,
  5356. 'title' => 'Third Post',
  5357. 'body' => 'Third Post Body',
  5358. 'published' => 'N'
  5359. ));
  5360. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  5361. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  5362. $this->assertEquals(self::date(), $result[4]['Post']['created']);
  5363. $this->assertEquals(self::date(), $result[4]['Post']['updated']);
  5364. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  5365. unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
  5366. }
  5367. $this->assertEquals($expected, $result);
  5368. $TestModel->validate = array('title' => 'notEmpty');
  5369. $data = array(
  5370. array('author_id' => 1, 'title' => 'New Fourth Post'),
  5371. array('author_id' => 1, 'title' => 'New Fifth Post'),
  5372. array('author_id' => 1, 'title' => 'New Sixth Post')
  5373. );
  5374. $this->assertTrue($TestModel->saveMany($data));
  5375. $result = $TestModel->find('all', array(
  5376. 'recursive' => -1,
  5377. 'fields' => array('author_id', 'title', 'body', 'published'),
  5378. 'order' => array('Post.created' => 'ASC')
  5379. ));
  5380. $expected = array(
  5381. array('Post' => array(
  5382. 'author_id' => 1,
  5383. 'title' => 'First Post',
  5384. 'body' => 'First Post Body',
  5385. 'published' => 'Y'
  5386. )),
  5387. array('Post' => array(
  5388. 'author_id' => 3,
  5389. 'title' => 'Second Post',
  5390. 'body' => 'Second Post Body',
  5391. 'published' => 'Y'
  5392. )),
  5393. array('Post' => array(
  5394. 'author_id' => 1,
  5395. 'title' => 'Third Post',
  5396. 'body' => 'Third Post Body',
  5397. 'published' => 'Y'
  5398. )),
  5399. array('Post' => array(
  5400. 'author_id' => 1,
  5401. 'title' => 'New Fourth Post',
  5402. 'body' => '',
  5403. 'published' => 'N'
  5404. )),
  5405. array('Post' => array(
  5406. 'author_id' => 1,
  5407. 'title' => 'New Fifth Post',
  5408. 'body' => '',
  5409. 'published' => 'N'
  5410. )),
  5411. array('Post' => array(
  5412. 'author_id' => 1,
  5413. 'title' => 'New Sixth Post',
  5414. 'body' => '',
  5415. 'published' => 'N'
  5416. )));
  5417. $this->assertEquals($expected, $result);
  5418. }
  5419. /**
  5420. * testSaveManyValidation method
  5421. *
  5422. * @return void
  5423. */
  5424. public function testSaveManyValidation() {
  5425. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  5426. $TestModel = new Post();
  5427. $data = array(
  5428. array(
  5429. 'id' => '1',
  5430. 'title' => 'Baleeted First Post',
  5431. 'body' => 'Baleeted!',
  5432. 'published' => 'N'
  5433. ),
  5434. array(
  5435. 'id' => '2',
  5436. 'title' => 'Just update the title'
  5437. ),
  5438. array(
  5439. 'title' => 'Creating a fourth post',
  5440. 'body' => 'Fourth post body',
  5441. 'author_id' => 2
  5442. ));
  5443. $this->assertTrue($TestModel->saveMany($data));
  5444. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  5445. $expected = array(
  5446. array(
  5447. 'Post' => array(
  5448. 'id' => '1',
  5449. 'author_id' => '1',
  5450. 'title' => 'Baleeted First Post',
  5451. 'body' => 'Baleeted!',
  5452. 'published' => 'N',
  5453. 'created' => '2007-03-18 10:39:23'
  5454. )
  5455. ),
  5456. array(
  5457. 'Post' => array(
  5458. 'id' => '2',
  5459. 'author_id' => '3',
  5460. 'title' => 'Just update the title',
  5461. 'body' => 'Second Post Body',
  5462. 'published' => 'Y',
  5463. 'created' => '2007-03-18 10:41:23'
  5464. )
  5465. ),
  5466. array(
  5467. 'Post' => array(
  5468. 'id' => '3',
  5469. 'author_id' => '1',
  5470. 'title' => 'Third Post',
  5471. 'body' => 'Third Post Body',
  5472. 'published' => 'Y',
  5473. 'created' => '2007-03-18 10:43:23',
  5474. 'updated' => '2007-03-18 10:45:31'
  5475. )),
  5476. array(
  5477. 'Post' => array(
  5478. 'id' => '4',
  5479. 'author_id' => '2',
  5480. 'title' => 'Creating a fourth post',
  5481. 'body' => 'Fourth post body',
  5482. 'published' => 'N'
  5483. )
  5484. )
  5485. );
  5486. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  5487. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  5488. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  5489. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  5490. unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
  5491. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  5492. $this->assertEquals($expected, $result);
  5493. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  5494. $data = array(
  5495. array(
  5496. 'id' => '1',
  5497. 'title' => 'Un-Baleeted First Post',
  5498. 'body' => 'Not Baleeted!',
  5499. 'published' => 'Y'
  5500. ),
  5501. array(
  5502. 'id' => '2',
  5503. 'title' => '',
  5504. 'body' => 'Trying to get away with an empty title'
  5505. ));
  5506. $result = $TestModel->saveMany($data);
  5507. $this->assertFalse($result);
  5508. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  5509. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  5510. $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
  5511. if (!$transactionWorked) {
  5512. $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
  5513. $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
  5514. }
  5515. $this->assertEquals($errors, $TestModel->validationErrors);
  5516. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  5517. $data = array(
  5518. array(
  5519. 'id' => '1',
  5520. 'title' => 'Un-Baleeted First Post',
  5521. 'body' => 'Not Baleeted!',
  5522. 'published' => 'Y'
  5523. ),
  5524. array(
  5525. 'id' => '2',
  5526. 'title' => '',
  5527. 'body' => 'Trying to get away with an empty title'
  5528. ));
  5529. $result = $TestModel->saveMany($data, array('validate' => true, 'atomic' => false));
  5530. $this->assertEquals(array(true, false), $result);
  5531. $result = $TestModel->find('all', array(
  5532. 'fields' => array('id', 'author_id', 'title', 'body', 'published'),
  5533. 'recursive' => -1,
  5534. 'order' => 'Post.id ASC'
  5535. ));
  5536. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  5537. $expected = array(
  5538. array(
  5539. 'Post' => array(
  5540. 'id' => '1',
  5541. 'author_id' => '1',
  5542. 'title' => 'Un-Baleeted First Post',
  5543. 'body' => 'Not Baleeted!',
  5544. 'published' => 'Y',
  5545. )),
  5546. array(
  5547. 'Post' => array(
  5548. 'id' => '2',
  5549. 'author_id' => '3',
  5550. 'title' => 'Just update the title',
  5551. 'body' => 'Second Post Body',
  5552. 'published' => 'Y',
  5553. )),
  5554. array(
  5555. 'Post' => array(
  5556. 'id' => '3',
  5557. 'author_id' => '1',
  5558. 'title' => 'Third Post',
  5559. 'body' => 'Third Post Body',
  5560. 'published' => 'Y',
  5561. )),
  5562. array(
  5563. 'Post' => array(
  5564. 'id' => '4',
  5565. 'author_id' => '2',
  5566. 'title' => 'Creating a fourth post',
  5567. 'body' => 'Fourth post body',
  5568. 'published' => 'N',
  5569. )));
  5570. $this->assertEquals($expected, $result);
  5571. $this->assertEquals($errors, $TestModel->validationErrors);
  5572. $data = array(
  5573. array(
  5574. 'id' => '1',
  5575. 'title' => 'Re-Baleeted First Post',
  5576. 'body' => 'Baleeted!',
  5577. 'published' => 'N'
  5578. ),
  5579. array(
  5580. 'id' => '2',
  5581. 'title' => '',
  5582. 'body' => 'Trying to get away with an empty title'
  5583. ));
  5584. $this->assertFalse($TestModel->saveMany($data, array('validate' => 'first')));
  5585. $result = $TestModel->find('all', array(
  5586. 'fields' => array('id', 'author_id', 'title', 'body', 'published'),
  5587. 'recursive' => -1,
  5588. 'order' => 'Post.id ASC'
  5589. ));
  5590. $this->assertEquals($expected, $result);
  5591. $this->assertEquals($errors, $TestModel->validationErrors);
  5592. }
  5593. /**
  5594. * testValidateMany method
  5595. *
  5596. * @return void
  5597. */
  5598. public function testValidateMany() {
  5599. $TestModel = new Article();
  5600. $TestModel->validate = array('title' => 'notEmpty');
  5601. $data = array(
  5602. 0 => array('title' => ''),
  5603. 1 => array('title' => 'title 1'),
  5604. 2 => array('title' => 'title 2'),
  5605. );
  5606. $result = $TestModel->validateMany($data);
  5607. $this->assertFalse($result);
  5608. $expected = array(
  5609. 0 => array('title' => array('This field cannot be left blank')),
  5610. );
  5611. $this->assertEquals($expected, $TestModel->validationErrors);
  5612. $data = array(
  5613. 0 => array('title' => 'title 0'),
  5614. 1 => array('title' => ''),
  5615. 2 => array('title' => 'title 2'),
  5616. );
  5617. $result = $TestModel->validateMany($data);
  5618. $this->assertFalse($result);
  5619. $expected = array(
  5620. 1 => array('title' => array('This field cannot be left blank')),
  5621. );
  5622. $this->assertEquals($expected, $TestModel->validationErrors);
  5623. }
  5624. /**
  5625. * testSaveAssociatedValidateFirst method
  5626. *
  5627. * @return void
  5628. */
  5629. public function testSaveAssociatedValidateFirst() {
  5630. $this->loadFixtures('Article', 'Comment', 'Attachment');
  5631. $model = new Article();
  5632. $model->deleteAll(true);
  5633. $model->Comment->validate = array('comment' => 'notEmpty');
  5634. $result = $model->saveAssociated(array(
  5635. 'Article' => array(
  5636. 'title' => 'Post with Author',
  5637. 'body' => 'This post will be saved author'
  5638. ),
  5639. 'Comment' => array(
  5640. array('comment' => 'First new comment'),
  5641. array('comment' => '')
  5642. )
  5643. ), array('validate' => 'first'));
  5644. $this->assertFalse($result);
  5645. $result = $model->find('all');
  5646. $this->assertSame(array(), $result);
  5647. $expected = array('Comment' => array(
  5648. 1 => array('comment' => array('This field cannot be left blank'))
  5649. ));
  5650. $this->assertEquals($expected['Comment'], $model->Comment->validationErrors);
  5651. $this->assertSame($model->Comment->find('count'), 0);
  5652. $result = $model->saveAssociated(
  5653. array(
  5654. 'Article' => array(
  5655. 'title' => 'Post with Author',
  5656. 'body' => 'This post will be saved with an author',
  5657. 'user_id' => 2
  5658. ),
  5659. 'Comment' => array(
  5660. array(
  5661. 'comment' => 'Only new comment',
  5662. 'user_id' => 2
  5663. ))),
  5664. array('validate' => 'first')
  5665. );
  5666. $this->assertTrue($result);
  5667. $result = $model->Comment->find('all');
  5668. $this->assertSame(count($result), 1);
  5669. $result = Hash::extract($result, '{n}.Comment.article_id');
  5670. $this->assertEquals(4, $result[0]);
  5671. $model->deleteAll(true);
  5672. $data = array(
  5673. 'Article' => array(
  5674. 'title' => 'Post with Author saveAlled from comment',
  5675. 'body' => 'This post will be saved with an author',
  5676. 'user_id' => 2
  5677. ),
  5678. 'Comment' => array(
  5679. 'comment' => 'Only new comment', 'user_id' => 2
  5680. ));
  5681. $result = $model->Comment->saveAssociated($data, array('validate' => 'first'));
  5682. $this->assertFalse(empty($result));
  5683. $result = $model->find('all');
  5684. $this->assertEquals(
  5685. 'Post with Author saveAlled from comment',
  5686. $result[0]['Article']['title']
  5687. );
  5688. $this->assertEquals('Only new comment', $result[0]['Comment'][0]['comment']);
  5689. }
  5690. /**
  5691. * test saveMany()'s return is correct when using atomic = false and validate = first.
  5692. *
  5693. * @return void
  5694. */
  5695. public function testSaveManyValidateFirstAtomicFalse() {
  5696. $Something = new Something();
  5697. $invalidData = array(
  5698. array(
  5699. 'title' => 'foo',
  5700. 'body' => 'bar',
  5701. 'published' => 'baz',
  5702. ),
  5703. array(
  5704. 'body' => 3,
  5705. 'published' => 'sd',
  5706. ),
  5707. );
  5708. $Something->create();
  5709. $Something->validate = array(
  5710. 'title' => array(
  5711. 'rule' => 'alphaNumeric',
  5712. 'required' => true,
  5713. ),
  5714. 'body' => array(
  5715. 'rule' => 'alphaNumeric',
  5716. 'required' => true,
  5717. 'allowEmpty' => true,
  5718. ),
  5719. );
  5720. $result = $Something->saveMany($invalidData, array(
  5721. 'atomic' => false,
  5722. 'validate' => 'first',
  5723. ));
  5724. $expected = array(true, false);
  5725. $this->assertEquals($expected, $result);
  5726. $Something = new Something();
  5727. $validData = array(
  5728. array(
  5729. 'title' => 'title value',
  5730. 'body' => 'body value',
  5731. 'published' => 'baz',
  5732. ),
  5733. array(
  5734. 'title' => 'valid',
  5735. 'body' => 'this body',
  5736. 'published' => 'sd',
  5737. ),
  5738. );
  5739. $Something->create();
  5740. $result = $Something->saveMany($validData, array(
  5741. 'atomic' => false,
  5742. 'validate' => 'first',
  5743. ));
  5744. $expected = array(true, true);
  5745. $this->assertEquals($expected, $result);
  5746. }
  5747. /**
  5748. * testValidateAssociated method
  5749. *
  5750. * @return void
  5751. */
  5752. public function testValidateAssociated() {
  5753. $this->loadFixtures('Attachment', 'Article', 'Comment');
  5754. $TestModel = new Comment();
  5755. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  5756. $data = array(
  5757. 'Comment' => array(
  5758. 'comment' => 'This is the comment'
  5759. ),
  5760. 'Attachment' => array(
  5761. 'attachment' => ''
  5762. )
  5763. );
  5764. $result = $TestModel->validateAssociated($data);
  5765. $this->assertFalse($result);
  5766. $TestModel = new Article();
  5767. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  5768. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  5769. $data = array(
  5770. 'Article' => array('id' => 2),
  5771. 'Comment' => array(
  5772. array(
  5773. 'id' => 1,
  5774. 'comment' => '',
  5775. 'published' => 'Y',
  5776. 'user_id' => 1),
  5777. array(
  5778. 'id' => 2,
  5779. 'comment' =>
  5780. 'comment',
  5781. 'published' => 'Y',
  5782. 'user_id' => 1
  5783. )));
  5784. $result = $TestModel->validateAssociated($data);
  5785. $this->assertFalse($result);
  5786. $data = array(
  5787. 'Article' => array('id' => 2),
  5788. 'Comment' => array(
  5789. array(
  5790. 'id' => 1,
  5791. 'comment' => '',
  5792. 'published' => 'Y',
  5793. 'user_id' => 1
  5794. ),
  5795. array(
  5796. 'id' => 2,
  5797. 'comment' => 'comment',
  5798. 'published' => 'Y',
  5799. 'user_id' => 1
  5800. ),
  5801. array(
  5802. 'id' => 3,
  5803. 'comment' => '',
  5804. 'published' => 'Y',
  5805. 'user_id' => 1
  5806. )));
  5807. $result = $TestModel->validateAssociated($data, array(
  5808. 'atomic' => false
  5809. ));
  5810. $expected = array(
  5811. 'Article' => true,
  5812. 'Comment' => array(false, true, false)
  5813. );
  5814. $this->assertSame($expected, $result);
  5815. $expected = array('Comment' => array(
  5816. 0 => array('comment' => array('This field cannot be left blank')),
  5817. 2 => array('comment' => array('This field cannot be left blank'))
  5818. ));
  5819. $this->assertEquals($expected, $TestModel->validationErrors);
  5820. $expected = array(
  5821. 0 => array('comment' => array('This field cannot be left blank')),
  5822. 2 => array('comment' => array('This field cannot be left blank'))
  5823. );
  5824. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  5825. }
  5826. /**
  5827. * test that saveMany behaves like plain save() when suplied empty data
  5828. *
  5829. * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  5830. * @return void
  5831. */
  5832. public function testSaveManyEmptyData() {
  5833. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  5834. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  5835. $model = new Article();
  5836. $result = $model->saveMany(array(), array('validate' => true));
  5837. $this->assertFalse(empty($result));
  5838. $model = new ProductUpdateAll();
  5839. $result = $model->saveMany(array());
  5840. $this->assertFalse($result);
  5841. }
  5842. /**
  5843. * test that saveAssociated behaves like plain save() when supplied empty data
  5844. *
  5845. * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  5846. * @return void
  5847. */
  5848. public function testSaveAssociatedEmptyData() {
  5849. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  5850. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  5851. $model = new Article();
  5852. $result = $model->saveAssociated(array(), array('validate' => true));
  5853. $this->assertFalse(empty($result));
  5854. $model = new ProductUpdateAll();
  5855. $result = $model->saveAssociated(array());
  5856. $this->assertFalse($result);
  5857. }
  5858. /**
  5859. * Test that saveAssociated will accept expression object values when saving.
  5860. *
  5861. * @return void
  5862. */
  5863. public function testSaveAssociatedExpressionObjects() {
  5864. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  5865. $TestModel = new Post();
  5866. $db = $TestModel->getDataSource();
  5867. $TestModel->saveAssociated(array(
  5868. 'Post' => array(
  5869. 'title' => $db->expression("(SELECT 'Post with Author')"),
  5870. 'body' => 'This post will be saved with an author'
  5871. ),
  5872. 'Author' => array(
  5873. 'user' => 'bob',
  5874. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
  5875. )), array('atomic' => false));
  5876. $result = $TestModel->find('first', array(
  5877. 'order' => array('Post.id ' => 'DESC')
  5878. ));
  5879. $this->assertEquals('Post with Author', $result['Post']['title']);
  5880. }
  5881. /**
  5882. * testUpdateWithCalculation method
  5883. *
  5884. * @return void
  5885. */
  5886. public function testUpdateWithCalculation() {
  5887. $this->loadFixtures('DataTest');
  5888. $model = new DataTest();
  5889. $model->deleteAll(true);
  5890. $result = $model->saveMany(array(
  5891. array('count' => 5, 'float' => 1.1),
  5892. array('count' => 3, 'float' => 1.2),
  5893. array('count' => 4, 'float' => 1.3),
  5894. array('count' => 1, 'float' => 2.0),
  5895. ));
  5896. $this->assertFalse(empty($result));
  5897. $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
  5898. $this->assertEquals(array(5, 3, 4, 1), $result);
  5899. $this->assertTrue($model->updateAll(array('count' => 'count + 2')));
  5900. $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
  5901. $this->assertEquals(array(7, 5, 6, 3), $result);
  5902. $this->assertTrue($model->updateAll(array('DataTest.count' => 'DataTest.count - 1')));
  5903. $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
  5904. $this->assertEquals(array(6, 4, 5, 2), $result);
  5905. }
  5906. /**
  5907. * testToggleBoolFields method
  5908. *
  5909. * @return void
  5910. */
  5911. public function testToggleBoolFields() {
  5912. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  5913. $Post = new CounterCachePost();
  5914. $Post->unbindModel(array('belongsTo' => array('User')), true);
  5915. $true = array('Post' => array('published' => true, 'id' => 2));
  5916. $false = array('Post' => array('published' => false, 'id' => 2));
  5917. $fields = array('Post.published', 'Post.id');
  5918. $updateConditions = array('Post.id' => 2);
  5919. // check its true
  5920. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5921. $this->assertEquals($true, $result);
  5922. // Testing without the alias
  5923. $this->assertTrue($Post->updateAll(array('published' => 'NOT published'), $updateConditions));
  5924. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5925. $this->assertEquals($false, $result);
  5926. $this->assertTrue($Post->updateAll(array('published' => 'NOT published'), $updateConditions));
  5927. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5928. $this->assertEquals($true, $result);
  5929. $db = ConnectionManager::getDataSource('test');
  5930. $alias = $db->name('Post.published');
  5931. // Testing with the alias
  5932. $this->assertTrue($Post->updateAll(array('Post.published' => "NOT $alias"), $updateConditions));
  5933. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5934. $this->assertEquals($false, $result);
  5935. $this->assertTrue($Post->updateAll(array('Post.published' => "NOT $alias"), $updateConditions));
  5936. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5937. $this->assertEquals($true, $result);
  5938. }
  5939. /**
  5940. * TestFindAllWithoutForeignKey
  5941. *
  5942. * @return void
  5943. */
  5944. public function testFindAllForeignKey() {
  5945. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  5946. $ProductUpdateAll = new ProductUpdateAll();
  5947. $conditions = array('Group.name' => 'group one');
  5948. $ProductUpdateAll->bindModel(array(
  5949. 'belongsTo' => array(
  5950. 'Group' => array('className' => 'GroupUpdateAll')
  5951. )
  5952. ));
  5953. $ProductUpdateAll->belongsTo = array(
  5954. 'Group' => array('className' => 'GroupUpdateAll', 'foreignKey' => 'group_id')
  5955. );
  5956. $results = $ProductUpdateAll->find('all', compact('conditions'));
  5957. $this->assertTrue(!empty($results));
  5958. $ProductUpdateAll->bindModel(array('belongsTo' => array('Group')));
  5959. $ProductUpdateAll->belongsTo = array(
  5960. 'Group' => array(
  5961. 'className' => 'GroupUpdateAll',
  5962. 'foreignKey' => false,
  5963. 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
  5964. ));
  5965. $resultsFkFalse = $ProductUpdateAll->find('all', compact('conditions'));
  5966. $this->assertTrue(!empty($resultsFkFalse));
  5967. $expected = array(
  5968. '0' => array(
  5969. 'ProductUpdateAll' => array(
  5970. 'id' => 1,
  5971. 'name' => 'product one',
  5972. 'groupcode' => 120,
  5973. 'group_id' => 1),
  5974. 'Group' => array(
  5975. 'id' => 1,
  5976. 'name' => 'group one',
  5977. 'code' => 120)
  5978. ),
  5979. '1' => array(
  5980. 'ProductUpdateAll' => array(
  5981. 'id' => 2,
  5982. 'name' => 'product two',
  5983. 'groupcode' => 120,
  5984. 'group_id' => 1),
  5985. 'Group' => array(
  5986. 'id' => 1,
  5987. 'name' => 'group one',
  5988. 'code' => 120)
  5989. )
  5990. );
  5991. $this->assertEquals($expected, $results);
  5992. $this->assertEquals($expected, $resultsFkFalse);
  5993. }
  5994. /**
  5995. * test updateAll with empty values.
  5996. *
  5997. * @return void
  5998. */
  5999. public function testUpdateAllEmptyValues() {
  6000. $this->skipIf($this->db instanceof Sqlserver || $this->db instanceof Postgres, 'This test is not compatible with Postgres or SQL Server.');
  6001. $this->loadFixtures('Author', 'Post');
  6002. $model = new Author();
  6003. $result = $model->updateAll(array('user' => '""'));
  6004. $this->assertTrue($result);
  6005. }
  6006. /**
  6007. * testUpdateAllWithJoins
  6008. *
  6009. * @return void
  6010. */
  6011. public function testUpdateAllWithJoins() {
  6012. $this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql or sqlite');
  6013. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  6014. $ProductUpdateAll = new ProductUpdateAll();
  6015. $conditions = array('Group.name' => 'group one');
  6016. $ProductUpdateAll->bindModel(array('belongsTo' => array(
  6017. 'Group' => array('className' => 'GroupUpdateAll')))
  6018. );
  6019. $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
  6020. $results = $ProductUpdateAll->find('all', array(
  6021. 'conditions' => array('ProductUpdateAll.name' => 'new product')
  6022. ));
  6023. $expected = array(
  6024. '0' => array(
  6025. 'ProductUpdateAll' => array(
  6026. 'id' => 1,
  6027. 'name' => 'new product',
  6028. 'groupcode' => 120,
  6029. 'group_id' => 1),
  6030. 'Group' => array(
  6031. 'id' => 1,
  6032. 'name' => 'group one',
  6033. 'code' => 120)
  6034. ),
  6035. '1' => array(
  6036. 'ProductUpdateAll' => array(
  6037. 'id' => 2,
  6038. 'name' => 'new product',
  6039. 'groupcode' => 120,
  6040. 'group_id' => 1),
  6041. 'Group' => array(
  6042. 'id' => 1,
  6043. 'name' => 'group one',
  6044. 'code' => 120)));
  6045. $this->assertEquals($expected, $results);
  6046. }
  6047. /**
  6048. * testUpdateAllWithoutForeignKey
  6049. *
  6050. * @return void
  6051. */
  6052. public function testUpdateAllWithoutForeignKey() {
  6053. $this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql');
  6054. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  6055. $ProductUpdateAll = new ProductUpdateAll();
  6056. $conditions = array('Group.name' => 'group one');
  6057. $ProductUpdateAll->bindModel(array('belongsTo' => array(
  6058. 'Group' => array('className' => 'GroupUpdateAll')
  6059. )));
  6060. $ProductUpdateAll->belongsTo = array(
  6061. 'Group' => array(
  6062. 'className' => 'GroupUpdateAll',
  6063. 'foreignKey' => false,
  6064. 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
  6065. )
  6066. );
  6067. $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
  6068. $resultsFkFalse = $ProductUpdateAll->find('all', array('conditions' => array('ProductUpdateAll.name' => 'new product')));
  6069. $expected = array(
  6070. '0' => array(
  6071. 'ProductUpdateAll' => array(
  6072. 'id' => 1,
  6073. 'name' => 'new product',
  6074. 'groupcode' => 120,
  6075. 'group_id' => 1),
  6076. 'Group' => array(
  6077. 'id' => 1,
  6078. 'name' => 'group one',
  6079. 'code' => 120)
  6080. ),
  6081. '1' => array(
  6082. 'ProductUpdateAll' => array(
  6083. 'id' => 2,
  6084. 'name' => 'new product',
  6085. 'groupcode' => 120,
  6086. 'group_id' => 1),
  6087. 'Group' => array(
  6088. 'id' => 1,
  6089. 'name' => 'group one',
  6090. 'code' => 120)));
  6091. $this->assertEquals($expected, $resultsFkFalse);
  6092. }
  6093. /**
  6094. * test writing floats in german locale.
  6095. *
  6096. * @return void
  6097. */
  6098. public function testWriteFloatAsGerman() {
  6099. $restore = setlocale(LC_NUMERIC, 0);
  6100. $this->skipIf(setlocale(LC_NUMERIC, 'de_DE') === false, "The German locale isn't available.");
  6101. $model = new DataTest();
  6102. $result = $model->save(array(
  6103. 'count' => 1,
  6104. 'float' => 3.14593
  6105. ));
  6106. $this->assertTrue((bool)$result);
  6107. setlocale(LC_NUMERIC, $restore);
  6108. }
  6109. /**
  6110. * Test returned array contains primary key when save creates a new record
  6111. *
  6112. * @return void
  6113. */
  6114. public function testPkInReturnArrayForCreate() {
  6115. $this->loadFixtures('Article');
  6116. $TestModel = new Article();
  6117. $data = array('Article' => array(
  6118. 'user_id' => '1',
  6119. 'title' => 'Fourth Article',
  6120. 'body' => 'Fourth Article Body',
  6121. 'published' => 'Y'
  6122. ));
  6123. $result = $TestModel->save($data);
  6124. $this->assertSame($result['Article']['id'], $TestModel->id);
  6125. }
  6126. /**
  6127. * testSaveAllFieldListValidateBelongsTo
  6128. *
  6129. * @return void
  6130. */
  6131. public function testSaveAllFieldListValidateBelongsTo() {
  6132. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  6133. $TestModel = new Post();
  6134. $result = $TestModel->find('all');
  6135. $this->assertEquals(3, count($result));
  6136. $this->assertFalse(isset($result[3]));
  6137. // test belongsTo
  6138. $fieldList = array(
  6139. 'Post' => array('title'),
  6140. 'Author' => array('user')
  6141. );
  6142. $data = array(
  6143. 'Post' => array(
  6144. 'title' => 'Post without body',
  6145. 'body' => 'This will not be saved',
  6146. ),
  6147. 'Author' => array(
  6148. 'user' => 'bob',
  6149. 'test' => 'This will not be saved',
  6150. ));
  6151. $TestModel->saveAll($data, array('fieldList' => $fieldList));
  6152. $result = $TestModel->find('all', array(
  6153. 'order' => 'Post.id ASC',
  6154. ));
  6155. $expected = array(
  6156. 'Post' => array(
  6157. 'id' => '4',
  6158. 'author_id' => '5',
  6159. 'title' => 'Post without body',
  6160. 'body' => null,
  6161. 'published' => 'N',
  6162. 'created' => self::date(),
  6163. 'updated' => self::date(),
  6164. ),
  6165. 'Author' => array(
  6166. 'id' => '5',
  6167. 'user' => 'bob',
  6168. 'password' => null,
  6169. 'created' => self::date(),
  6170. 'updated' => self::date(),
  6171. 'test' => 'working',
  6172. ),
  6173. );
  6174. $this->assertEquals($expected, $result[3]);
  6175. $this->assertEquals(4, count($result));
  6176. $this->assertEquals('', $result[3]['Post']['body']);
  6177. $this->assertEquals('working', $result[3]['Author']['test']);
  6178. $fieldList = array(
  6179. 'Post' => array('title')
  6180. );
  6181. $data = array(
  6182. 'Post' => array(
  6183. 'title' => 'Post without body 2',
  6184. 'body' => 'This will not be saved'
  6185. ),
  6186. 'Author' => array(
  6187. 'user' => 'jack'
  6188. )
  6189. );
  6190. $TestModel->saveAll($data, array('fieldList' => $fieldList));
  6191. $result = $TestModel->find('all', array(
  6192. 'order' => 'Post.id ASC',
  6193. ));
  6194. $this->assertNull($result[4]['Post']['body']);
  6195. $fieldList = array(
  6196. 'Author' => array('password')
  6197. );
  6198. $data = array(
  6199. 'Post' => array(
  6200. 'id' => '5',
  6201. 'title' => 'Post title',
  6202. 'body' => 'Post body'
  6203. ),
  6204. 'Author' => array(
  6205. 'id' => '6',
  6206. 'user' => 'will not change',
  6207. 'password' => 'foobar'
  6208. )
  6209. );
  6210. $result = $TestModel->saveAll($data, array('fieldList' => $fieldList));
  6211. $this->assertTrue($result);
  6212. $result = $TestModel->find('all', array(
  6213. 'order' => 'Post.id ASC',
  6214. ));
  6215. $expected = array(
  6216. 'Post' => array(
  6217. 'id' => '5',
  6218. 'author_id' => '6',
  6219. 'title' => 'Post title',
  6220. 'body' => 'Post body',
  6221. 'published' => 'N',
  6222. 'created' => self::date(),
  6223. 'updated' => self::date()
  6224. ),
  6225. 'Author' => array(
  6226. 'id' => '6',
  6227. 'user' => 'jack',
  6228. 'password' => 'foobar',
  6229. 'created' => self::date(),
  6230. 'updated' => self::date(),
  6231. 'test' => 'working'
  6232. ),
  6233. );
  6234. $this->assertEquals($expected, $result[4]);
  6235. // test multirecord
  6236. $this->db->truncate($TestModel);
  6237. $fieldList = array('title', 'author_id');
  6238. $TestModel->saveAll(array(
  6239. array(
  6240. 'title' => 'Multi-record post 1',
  6241. 'body' => 'First multi-record post',
  6242. 'author_id' => 2
  6243. ),
  6244. array(
  6245. 'title' => 'Multi-record post 2',
  6246. 'body' => 'Second multi-record post',
  6247. 'author_id' => 2
  6248. )), array('fieldList' => $fieldList));
  6249. $result = $TestModel->find('all', array(
  6250. 'recursive' => -1,
  6251. 'order' => 'Post.id ASC'
  6252. ));
  6253. $expected = array(
  6254. array(
  6255. 'Post' => array(
  6256. 'id' => '1',
  6257. 'author_id' => '2',
  6258. 'title' => 'Multi-record post 1',
  6259. 'body' => '',
  6260. 'published' => 'N',
  6261. 'created' => self::date(),
  6262. 'updated' => self::date()
  6263. )
  6264. ),
  6265. array(
  6266. 'Post' => array(
  6267. 'id' => '2',
  6268. 'author_id' => '2',
  6269. 'title' => 'Multi-record post 2',
  6270. 'body' => '',
  6271. 'published' => 'N',
  6272. 'created' => self::date(),
  6273. 'updated' => self::date()
  6274. )
  6275. )
  6276. );
  6277. $this->assertEquals($expected, $result);
  6278. }
  6279. /**
  6280. * testSaveAllFieldListHasMany method
  6281. *
  6282. * @return void
  6283. */
  6284. public function testSaveAllFieldListHasMany() {
  6285. $this->loadFixtures('Article', 'Comment');
  6286. $TestModel = new Article();
  6287. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  6288. $this->db->truncate($TestModel);
  6289. $this->db->truncate(new Comment());
  6290. $data = array(
  6291. 'Article' => array('title' => 'I will not save'),
  6292. 'Comment' => array(
  6293. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  6294. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  6295. )
  6296. );
  6297. $fieldList = array(
  6298. 'Article' => array('id'),
  6299. 'Comment' => array('article_id', 'user_id')
  6300. );
  6301. $TestModel->saveAll($data, array('fieldList' => $fieldList));
  6302. $result = $TestModel->find('all');
  6303. $this->assertEquals('', $result[0]['Article']['title']);
  6304. $this->assertEquals('', $result[0]['Comment'][0]['comment']);
  6305. $this->assertEquals('', $result[0]['Comment'][1]['comment']);
  6306. $fieldList = array(
  6307. 'Article' => array('id'),
  6308. 'Comment' => array('user_id')
  6309. );
  6310. $TestModel->saveAll($data, array('fieldList' => $fieldList));
  6311. $result = $TestModel->find('all');
  6312. $this->assertEquals('', $result[1]['Article']['title']);
  6313. $this->assertEquals(2, count($result[1]['Comment']));
  6314. $TestModel->whitelist = array('id');
  6315. $TestModel->Comment->whitelist = array('user_id');
  6316. $TestModel->saveAll($data);
  6317. $result = $TestModel->find('all');
  6318. $this->assertEquals('', $result[2]['Article']['title']);
  6319. $this->assertEquals(2, count($result[2]['Comment']));
  6320. }
  6321. /**
  6322. * testSaveAllFieldListHasOne method
  6323. *
  6324. * @return void
  6325. */
  6326. public function testSaveAllFieldListHasOne() {
  6327. $this->loadFixtures('Attachment', 'Comment', 'Article', 'User');
  6328. $TestModel = new Comment();
  6329. $TestModel->validate = array('comment' => 'notEmpty');
  6330. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  6331. $record = array(
  6332. 'Comment' => array(
  6333. 'user_id' => 1,
  6334. 'article_id' => 1,
  6335. 'comment' => '',
  6336. ),
  6337. 'Attachment' => array(
  6338. 'attachment' => ''
  6339. )
  6340. );
  6341. $result = $TestModel->saveAll($record, array('validate' => 'only'));
  6342. $this->assertFalse($result);
  6343. $fieldList = array(
  6344. 'Comment' => array('id', 'article_id', 'user_id'),
  6345. 'Attachment' => array('comment_id')
  6346. );
  6347. $result = $TestModel->saveAll($record, array(
  6348. 'fieldList' => $fieldList, 'validate' => 'only'
  6349. ));
  6350. $this->assertTrue($result);
  6351. $this->assertEmpty($TestModel->validationErrors);
  6352. }
  6353. /**
  6354. * testSaveAllFieldListHasOneAddFkToWhitelist method
  6355. *
  6356. * @return void
  6357. */
  6358. public function testSaveAllFieldListHasOneAddFkToWhitelist() {
  6359. $this->loadFixtures('ArticleFeatured', 'Featured');
  6360. $Article = new ArticleFeatured();
  6361. $Article->belongsTo = $Article->hasMany = array();
  6362. $Article->Featured->validate = array('end_date' => 'notEmpty');
  6363. $record = array(
  6364. 'ArticleFeatured' => array(
  6365. 'user_id' => 1,
  6366. 'title' => 'First Article',
  6367. 'body' => '',
  6368. 'published' => 'Y'
  6369. ),
  6370. 'Featured' => array(
  6371. 'category_id' => 1,
  6372. 'end_date' => ''
  6373. )
  6374. );
  6375. $result = $Article->saveAll($record, array('validate' => 'only'));
  6376. $this->assertFalse($result);
  6377. $expected = array(
  6378. 'body' => array(
  6379. 'This field cannot be left blank'
  6380. ),
  6381. 'Featured' => array(
  6382. 'end_date' => array(
  6383. 'This field cannot be left blank'
  6384. )
  6385. )
  6386. );
  6387. $this->assertEquals($expected, $Article->validationErrors);
  6388. $fieldList = array(
  6389. 'ArticleFeatured' => array('user_id', 'title'),
  6390. 'Featured' => array('category_id')
  6391. );
  6392. $result = $Article->saveAll($record, array(
  6393. 'fieldList' => $fieldList, 'validate' => 'first'
  6394. ));
  6395. $this->assertTrue($result);
  6396. $this->assertEmpty($Article->validationErrors);
  6397. $Article->recursive = 0;
  6398. $result = $Article->find('first', array('order' => array('ArticleFeatured.created' => 'DESC')));
  6399. $this->assertSame($result['ArticleFeatured']['id'], $result['Featured']['article_featured_id']);
  6400. }
  6401. /**
  6402. * testSaveAllDeepFieldListValidateBelongsTo
  6403. *
  6404. * @return void
  6405. */
  6406. public function testSaveAllDeepFieldListValidateBelongsTo() {
  6407. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  6408. $TestModel = new Post();
  6409. $TestModel->Author->bindModel(array('hasMany' => array('Comment' => array('foreignKey' => 'user_id'))), false);
  6410. $TestModel->recursive = 2;
  6411. $result = $TestModel->find('all');
  6412. $this->assertEquals(3, count($result));
  6413. $this->assertFalse(isset($result[3]));
  6414. // test belongsTo
  6415. $fieldList = array(
  6416. 'Post' => array('title', 'author_id'),
  6417. 'Author' => array('user'),
  6418. 'Comment' => array('comment')
  6419. );
  6420. $TestModel->saveAll(array(
  6421. 'Post' => array(
  6422. 'title' => 'Post without body',
  6423. 'body' => 'This will not be saved',
  6424. ),
  6425. 'Author' => array(
  6426. 'user' => 'bob',
  6427. 'test' => 'This will not be saved',
  6428. 'Comment' => array(
  6429. array('id' => 5, 'comment' => 'I am still published', 'published' => 'N'))
  6430. )), array('fieldList' => $fieldList, 'deep' => true));
  6431. $result = $TestModel->Author->Comment->find('first', array(
  6432. 'conditions' => array('Comment.id' => 5),
  6433. 'fields' => array('comment', 'published')
  6434. ));
  6435. $expected = array(
  6436. 'Comment' => array(
  6437. 'comment' => 'I am still published',
  6438. 'published' => 'Y'
  6439. )
  6440. );
  6441. $this->assertEquals($expected, $result);
  6442. }
  6443. /**
  6444. * testSaveAllDeepFieldListHasMany method
  6445. *
  6446. * @return void
  6447. */
  6448. public function testSaveAllDeepFieldListHasMany() {
  6449. $this->loadFixtures('Article', 'Comment', 'User');
  6450. $TestModel = new Article();
  6451. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  6452. $this->db->truncate($TestModel);
  6453. $this->db->truncate(new Comment());
  6454. $fieldList = array(
  6455. 'Article' => array('id'),
  6456. 'Comment' => array('article_id', 'user_id'),
  6457. 'User' => array('user')
  6458. );
  6459. $result = $TestModel->saveAll(array(
  6460. 'Article' => array('id' => 2, 'title' => 'I will not save'),
  6461. 'Comment' => array(
  6462. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  6463. array(
  6464. 'comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2,
  6465. 'User' => array('user' => 'nopassword', 'password' => 'not saved')
  6466. )
  6467. )
  6468. ), array('fieldList' => $fieldList, 'deep' => true));
  6469. $result = $TestModel->Comment->User->find('first', array(
  6470. 'conditions' => array('User.user' => 'nopassword'),
  6471. 'fields' => array('user', 'password')
  6472. ));
  6473. $expected = array(
  6474. 'User' => array(
  6475. 'user' => 'nopassword',
  6476. 'password' => ''
  6477. )
  6478. );
  6479. $this->assertEquals($expected, $result);
  6480. }
  6481. /**
  6482. * testSaveAllDeepHasManyBelongsTo method
  6483. *
  6484. * @return void
  6485. */
  6486. public function testSaveAllDeepHasManyBelongsTo() {
  6487. $this->loadFixtures('Article', 'Comment', 'User');
  6488. $TestModel = new Article();
  6489. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  6490. $this->db->truncate($TestModel);
  6491. $this->db->truncate(new Comment());
  6492. $result = $TestModel->saveAll(array(
  6493. 'Article' => array('id' => 2, 'title' => 'The title'),
  6494. 'Comment' => array(
  6495. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  6496. array(
  6497. 'comment' => 'belongsto', 'published' => 'Y',
  6498. 'User' => array('user' => 'findme', 'password' => 'somepass')
  6499. )
  6500. )
  6501. ), array('deep' => true));
  6502. $result = $TestModel->Comment->User->find('first', array(
  6503. 'conditions' => array('User.user' => 'findme'),
  6504. 'fields' => array('id', 'user', 'password')
  6505. ));
  6506. $expected = array(
  6507. 'User' => array(
  6508. 'id' => 5,
  6509. 'user' => 'findme',
  6510. 'password' => 'somepass',
  6511. )
  6512. );
  6513. $this->assertEquals($expected, $result);
  6514. $result = $TestModel->Comment->find('first', array(
  6515. 'conditions' => array('Comment.user_id' => 5),
  6516. 'fields' => array('id', 'comment', 'published', 'user_id')
  6517. ));
  6518. $expected = array(
  6519. 'Comment' => array(
  6520. 'id' => 2,
  6521. 'comment' => 'belongsto',
  6522. 'published' => 'Y',
  6523. 'user_id' => 5
  6524. )
  6525. );
  6526. $this->assertEquals($expected, $result);
  6527. }
  6528. /**
  6529. * testSaveAllDeepHasManyhasMany method
  6530. *
  6531. * @return void
  6532. */
  6533. public function testSaveAllDeepHasManyHasMany() {
  6534. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  6535. $TestModel = new Article();
  6536. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
  6537. $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
  6538. $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
  6539. $this->db->truncate($TestModel);
  6540. $this->db->truncate(new Comment());
  6541. $this->db->truncate(new Attachment());
  6542. $result = $TestModel->saveAll(array(
  6543. 'Article' => array('id' => 2, 'title' => 'The title'),
  6544. 'Comment' => array(
  6545. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  6546. array(
  6547. 'comment' => 'hasmany', 'published' => 'Y', 'user_id' => 5,
  6548. 'Attachment' => array(
  6549. array('attachment' => 'first deep attachment'),
  6550. array('attachment' => 'second deep attachment'),
  6551. )
  6552. )
  6553. )
  6554. ), array('deep' => true));
  6555. $result = $TestModel->Comment->find('first', array(
  6556. 'conditions' => array('Comment.comment' => 'hasmany'),
  6557. 'fields' => array('id', 'comment', 'published', 'user_id'),
  6558. 'recursive' => -1
  6559. ));
  6560. $expected = array(
  6561. 'Comment' => array(
  6562. 'id' => 2,
  6563. 'comment' => 'hasmany',
  6564. 'published' => 'Y',
  6565. 'user_id' => 5
  6566. )
  6567. );
  6568. $this->assertEquals($expected, $result);
  6569. $result = $TestModel->Comment->Attachment->find('all', array(
  6570. 'fields' => array('attachment', 'comment_id'),
  6571. 'order' => array('Attachment.id' => 'ASC')
  6572. ));
  6573. $expected = array(
  6574. array('Attachment' => array('attachment' => 'first deep attachment', 'comment_id' => 2)),
  6575. array('Attachment' => array('attachment' => 'second deep attachment', 'comment_id' => 2)),
  6576. );
  6577. $this->assertEquals($expected, $result);
  6578. }
  6579. /**
  6580. * testSaveAllDeepOrderHasManyHasMany method
  6581. *
  6582. * @return void
  6583. */
  6584. public function testSaveAllDeepOrderHasManyHasMany() {
  6585. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  6586. $TestModel = new Article();
  6587. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
  6588. $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
  6589. $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
  6590. $this->db->truncate($TestModel);
  6591. $this->db->truncate(new Comment());
  6592. $this->db->truncate(new Attachment());
  6593. $result = $TestModel->saveAll(array(
  6594. 'Article' => array('id' => 2, 'title' => 'Comment has its data after Attachment'),
  6595. 'Comment' => array(
  6596. array(
  6597. 'Attachment' => array(
  6598. array('attachment' => 'attachment should be created with comment_id'),
  6599. array('attachment' => 'comment should be created with article_id'),
  6600. ),
  6601. 'comment' => 'after associated data',
  6602. 'user_id' => 1
  6603. )
  6604. )
  6605. ), array('deep' => true));
  6606. $result = $TestModel->Comment->find('first', array(
  6607. 'conditions' => array('Comment.article_id' => 2),
  6608. ));
  6609. $this->assertEquals(2, $result['Comment']['article_id']);
  6610. $this->assertEquals(2, count($result['Attachment']));
  6611. }
  6612. /**
  6613. * testSaveAllDeepEmptyHasManyHasMany method
  6614. *
  6615. * @return void
  6616. */
  6617. public function testSaveAllDeepEmptyHasManyHasMany() {
  6618. $this->skipIf(!$this->db instanceof Mysql, 'This test is only compatible with Mysql.');
  6619. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  6620. $TestModel = new Article();
  6621. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
  6622. $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
  6623. $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
  6624. $this->db->truncate($TestModel);
  6625. $this->db->truncate(new Comment());
  6626. $this->db->truncate(new Attachment());
  6627. $result = $TestModel->saveAll(array(
  6628. 'Article' => array('id' => 3, 'user_id' => 1, 'title' => 'Comment has no data'),
  6629. 'Comment' => array(
  6630. array(
  6631. 'user_id' => 1,
  6632. 'Attachment' => array(
  6633. array('attachment' => 'attachment should be created with comment_id'),
  6634. array('attachment' => 'comment should be created with article_id'),
  6635. ),
  6636. )
  6637. )
  6638. ), array('deep' => true));
  6639. $result = $TestModel->Comment->find('first', array(
  6640. 'conditions' => array('Comment.article_id' => 3),
  6641. ));
  6642. $this->assertEquals(3, $result['Comment']['article_id']);
  6643. $this->assertEquals(2, count($result['Attachment']));
  6644. }
  6645. /**
  6646. * testUpdateAllBoolean
  6647. *
  6648. * @return void
  6649. */
  6650. public function testUpdateAllBoolean() {
  6651. $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
  6652. $TestModel = new Item();
  6653. $result = $TestModel->updateAll(array('published' => true));
  6654. $this->assertTrue($result);
  6655. $result = $TestModel->find('first', array('fields' => array('id', 'published')));
  6656. $this->assertEquals(true, $result['Item']['published']);
  6657. }
  6658. /**
  6659. * testUpdateAllBooleanConditions
  6660. *
  6661. * @return void
  6662. */
  6663. public function testUpdateAllBooleanConditions() {
  6664. $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
  6665. $TestModel = new Item();
  6666. $result = $TestModel->updateAll(array('published' => true), array('Item.id' => 1));
  6667. $this->assertTrue($result);
  6668. $result = $TestModel->find('first', array(
  6669. 'fields' => array('id', 'published'),
  6670. 'conditions' => array('Item.id' => 1)));
  6671. $this->assertEquals(true, $result['Item']['published']);
  6672. }
  6673. /**
  6674. * testUpdateBoolean
  6675. *
  6676. * @return void
  6677. */
  6678. public function testUpdateBoolean() {
  6679. $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
  6680. $TestModel = new Item();
  6681. $result = $TestModel->save(array('published' => true, 'id' => 1));
  6682. $this->assertTrue((bool)$result);
  6683. $result = $TestModel->find('first', array(
  6684. 'fields' => array('id', 'published'),
  6685. 'conditions' => array('Item.id' => 1)));
  6686. $this->assertEquals(true, $result['Item']['published']);
  6687. }
  6688. /**
  6689. * Test the clear() method.
  6690. *
  6691. * @return void
  6692. */
  6693. public function testClear() {
  6694. $this->loadFixtures('Bid');
  6695. $model = ClassRegistry::init('Bid');
  6696. $model->set(array('name' => 'Testing', 'message_id' => 3));
  6697. $this->assertTrue(isset($model->data['Bid']['name']));
  6698. $this->assertTrue($model->clear());
  6699. $this->assertFalse(isset($model->data['Bid']['name']));
  6700. $this->assertFalse(isset($model->data['Bid']['message_id']));
  6701. }
  6702. /**
  6703. * Test that Model::save() doesn't generate a query with WHERE 1 = 1 on race condition.
  6704. *
  6705. * @link https://github.com/cakephp/cakephp/issues/3857
  6706. * @return void
  6707. */
  6708. public function testSafeUpdateMode() {
  6709. $this->loadFixtures('User');
  6710. $User = ClassRegistry::init('User');
  6711. $this->assertFalse($User->__safeUpdateMode);
  6712. $User->getEventManager()->attach(array($this, 'deleteMe'), 'Model.beforeSave');
  6713. $User->id = 1;
  6714. $User->set(array('user' => 'nobody'));
  6715. $User->save();
  6716. $users = $User->find('list', array('fields' => 'User.user'));
  6717. $expected = array(
  6718. 2 => 'nate',
  6719. 3 => 'larry',
  6720. 4 => 'garrett',
  6721. );
  6722. $this->assertEquals($expected, $users);
  6723. $this->assertFalse($User->__safeUpdateMode);
  6724. $User->id = 2;
  6725. $User->set(array('user' => $User->getDataSource()->expression('PDO_EXCEPTION()')));
  6726. try {
  6727. $User->save(null, false);
  6728. $this->fail('No exception thrown');
  6729. } catch (PDOException $e) {
  6730. $this->assertFalse($User->__safeUpdateMode);
  6731. }
  6732. }
  6733. /**
  6734. * Emulates race condition
  6735. *
  6736. * @param CakeEvent $event containing the Model
  6737. * @return void
  6738. */
  6739. public function deleteMe($event) {
  6740. $Model = $event->subject;
  6741. $Model->getDataSource()->delete($Model, array($Model->alias . '.' . $Model->primaryKey => $Model->id));
  6742. }
  6743. /**
  6744. * Creates a convenient mock DboSource
  6745. *
  6746. * We cannot call several methods via mock DboSource, such as DboSource::value()
  6747. * because mock DboSource has no $_connection.
  6748. * This method helps us to avoid this problem.
  6749. *
  6750. * @param array $methods Configurable method names.
  6751. * @return DboSource
  6752. */
  6753. protected function _getMockDboSource($methods = array()) {
  6754. $testDb = ConnectionManager::getDataSource('test');
  6755. $passthrough = array_diff(array('value', 'begin', 'rollback', 'commit', 'describe', 'lastInsertId', 'execute'), $methods);
  6756. $methods = array_merge($methods, $passthrough);
  6757. if (!in_array('connect', $methods)) {
  6758. $methods[] = 'connect'; // This will be called by DboSource::__construct().
  6759. }
  6760. $db = $this->getMock('DboSource', $methods);
  6761. $db->columns = $testDb->columns;
  6762. $db->startQuote = $testDb->startQuote;
  6763. $db->endQuote = $testDb->endQuote;
  6764. foreach ($passthrough as $method) {
  6765. $db->expects($this->any())
  6766. ->method($method)
  6767. ->will($this->returnCallback(array($testDb, $method)));
  6768. }
  6769. return $db;
  6770. }
  6771. /**
  6772. * Test that transactions behave correctly on nested saveMany calls.
  6773. *
  6774. * @return void
  6775. */
  6776. public function testTransactionOnNestedSaveMany() {
  6777. $this->loadFixtures('Post');
  6778. $Post = new TestPost();
  6779. $Post->getEventManager()->attach(array($this, 'nestedSaveMany'), 'Model.afterSave');
  6780. // begin -> [ begin -> commit ] -> commit
  6781. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  6782. $db->expects($this->exactly(2))->method('begin')->will($this->returnValue(true));
  6783. $db->expects($this->exactly(2))->method('commit');
  6784. $db->expects($this->never())->method('rollback');
  6785. $Post->setDataSourceObject($db);
  6786. $data = array(
  6787. array('author_id' => 1, 'title' => 'Outer Post'),
  6788. );
  6789. $Post->dataForAfterSave = array(
  6790. array('author_id' => 1, 'title' => 'Inner Post'),
  6791. );
  6792. $this->assertTrue($Post->saveMany($data));
  6793. // begin -> [ begin(false) ] -> commit
  6794. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  6795. $db->expects($this->at(0))->method('begin')->will($this->returnValue(true));
  6796. $db->expects($this->at(1))->method('begin')->will($this->returnValue(false));
  6797. $db->expects($this->once())->method('commit');
  6798. $db->expects($this->never())->method('rollback');
  6799. $Post->setDataSourceObject($db);
  6800. $data = array(
  6801. array('author_id' => 1, 'title' => 'Outer Post'),
  6802. );
  6803. $Post->dataForAfterSave = array(
  6804. array('author_id' => 1, 'title' => 'Inner Post'),
  6805. );
  6806. $this->assertTrue($Post->saveMany($data));
  6807. // begin -> [ begin -> rollback ] -> rollback
  6808. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  6809. $db->expects($this->exactly(2))->method('begin')->will($this->returnValue(true));
  6810. $db->expects($this->never())->method('commit');
  6811. $db->expects($this->exactly(2))->method('rollback');
  6812. $Post->setDataSourceObject($db);
  6813. $data = array(
  6814. array('author_id' => 1, 'title' => 'Outer Post'),
  6815. );
  6816. $Post->dataForAfterSave = array(
  6817. array('author_id' => 1, 'title' => 'Inner Post', 'body' => $db->expression('PDO_EXCEPTION()')),
  6818. );
  6819. try {
  6820. $Post->saveMany($data);
  6821. $this->fail('No exception thrown');
  6822. } catch(Exception $e) {
  6823. }
  6824. }
  6825. /**
  6826. * Test that transaction behaves correctly on nested saveAssociated calls.
  6827. *
  6828. * @return void
  6829. */
  6830. public function testTransactionOnNestedSaveAssociated() {
  6831. $this->loadFixtures('Author', 'Post');
  6832. $Author = new TestAuthor();
  6833. $Author->getEventManager()->attach(array($this, 'nestedSaveAssociated'), 'Model.afterSave');
  6834. // begin -> [ begin -> commit ] -> commit
  6835. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  6836. $db->expects($this->exactly(2))->method('begin')->will($this->returnValue(true));
  6837. $db->expects($this->exactly(2))->method('commit');
  6838. $db->expects($this->never())->method('rollback');
  6839. $Author->setDataSourceObject($db);
  6840. $Author->Post->setDataSourceObject($db);
  6841. $data = array(
  6842. 'Author' => array('user' => 'outer'),
  6843. 'Post' => array(
  6844. array('title' => 'Outer Post'),
  6845. )
  6846. );
  6847. $Author->dataForAfterSave = array(
  6848. 'Author' => array('user' => 'inner'),
  6849. 'Post' => array(
  6850. array('title' => 'Inner Post'),
  6851. )
  6852. );
  6853. $this->assertTrue($Author->saveAssociated($data));
  6854. // begin -> [ begin(false) ] -> commit
  6855. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  6856. $db->expects($this->at(0))->method('begin')->will($this->returnValue(true));
  6857. $db->expects($this->at(1))->method('begin')->will($this->returnValue(false));
  6858. $db->expects($this->once())->method('commit');
  6859. $db->expects($this->never())->method('rollback');
  6860. $Author->setDataSourceObject($db);
  6861. $Author->Post->setDataSourceObject($db);
  6862. $data = array(
  6863. 'Author' => array('user' => 'outer'),
  6864. 'Post' => array(
  6865. array('title' => 'Outer Post'),
  6866. )
  6867. );
  6868. $Author->dataForAfterSave = array(
  6869. 'Author' => array('user' => 'inner'),
  6870. 'Post' => array(
  6871. array('title' => 'Inner Post'),
  6872. )
  6873. );
  6874. $this->assertTrue($Author->saveAssociated($data));
  6875. // begin -> [ begin -> rollback ] -> rollback
  6876. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  6877. $db->expects($this->exactly(2))->method('begin')->will($this->returnValue(true));
  6878. $db->expects($this->never())->method('commit');
  6879. $db->expects($this->exactly(2))->method('rollback');
  6880. $Author->setDataSourceObject($db);
  6881. $Author->Post->setDataSourceObject($db);
  6882. $data = array(
  6883. 'Author' => array('user' => 'outer'),
  6884. 'Post' => array(
  6885. array('title' => 'Outer Post'),
  6886. )
  6887. );
  6888. $Author->dataForAfterSave = array(
  6889. 'Author' => array('user' => 'inner', 'password' => $db->expression('PDO_EXCEPTION()')),
  6890. 'Post' => array(
  6891. array('title' => 'Inner Post'),
  6892. )
  6893. );
  6894. try {
  6895. $Author->saveAssociated($data);
  6896. $this->fail('No exception thrown');
  6897. } catch(Exception $e) {
  6898. }
  6899. }
  6900. /**
  6901. * A callback for testing nested saveMany.
  6902. *
  6903. * @param CakeEvent $event containing the Model
  6904. * @return void
  6905. */
  6906. public function nestedSaveMany($event) {
  6907. $Model = $event->subject;
  6908. $Model->saveMany($Model->dataForAfterSave, array('callbacks' => false));
  6909. }
  6910. /**
  6911. * A callback for testing nested saveAssociated.
  6912. *
  6913. * @param CakeEvent $event containing the Model
  6914. * @return void
  6915. */
  6916. public function nestedSaveAssociated($event) {
  6917. $Model = $event->subject;
  6918. $Model->saveAssociated($Model->dataForAfterSave, array('callbacks' => false));
  6919. }
  6920. }