ModelWriteTest.php 201 KB

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