ModelWriteTest.php 187 KB

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