ModelWriteTest.php 186 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834
  1. <?php
  2. /**
  3. * ModelWriteTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-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/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Model
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
  20. /**
  21. * ModelWriteTest
  22. *
  23. * @package Cake.Test.Case.Model
  24. */
  25. class ModelWriteTest extends BaseModelTest {
  26. /**
  27. * testInsertAnotherHabtmRecordWithSameForeignKey method
  28. *
  29. * @access public
  30. * @return void
  31. */
  32. public function testInsertAnotherHabtmRecordWithSameForeignKey() {
  33. $this->loadFixtures('JoinA', 'JoinB', 'JoinAB', 'JoinC', 'JoinAC');
  34. $TestModel = new JoinA();
  35. $result = $TestModel->JoinAsJoinB->findById(1);
  36. $expected = array(
  37. 'JoinAsJoinB' => array(
  38. 'id' => 1,
  39. 'join_a_id' => 1,
  40. 'join_b_id' => 2,
  41. 'other' => 'Data for Join A 1 Join B 2',
  42. 'created' => '2008-01-03 10:56:33',
  43. 'updated' => '2008-01-03 10:56:33'
  44. ));
  45. $this->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 = Set::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'), Set::extract('/ParentNode/name', $result));
  554. $Node->saveField('state', 10);
  555. $result = $Node->read();
  556. $this->assertEquals(array('Second'), Set::extract('/ParentNode/name', $result));
  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, Set::extract('/JoinC/JoinAsJoinC/id', $result));
  1414. $expected = array('new record', 'new record');
  1415. $this->assertEquals($expected, Set::extract('/JoinC/JoinAsJoinC/other', $result));
  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. $ts = date('Y-m-d H:i:s');
  1454. $TestModel->id = 2;
  1455. $data = array('Tag' => array('Tag' => array(2)));
  1456. $TestModel->save($data);
  1457. $result = $TestModel->findById(2);
  1458. $expected = array(
  1459. 'Article' => array(
  1460. 'id' => '2',
  1461. 'user_id' => '3',
  1462. 'title' => 'Second Article',
  1463. 'body' => 'Second Article Body',
  1464. 'published' => 'Y',
  1465. 'created' => '2007-03-18 10:41:23',
  1466. 'updated' => $ts
  1467. ),
  1468. 'Tag' => array(
  1469. array(
  1470. 'id' => '2',
  1471. 'tag' => 'tag2',
  1472. 'created' => '2007-03-18 12:24:23',
  1473. 'updated' => '2007-03-18 12:26:31'
  1474. )
  1475. )
  1476. );
  1477. $this->assertEquals($expected, $result);
  1478. $this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio');
  1479. $TestModel = new Portfolio();
  1480. $result = $TestModel->findById(2);
  1481. $expected = array(
  1482. 'Portfolio' => array(
  1483. 'id' => 2,
  1484. 'seller_id' => 1,
  1485. 'name' => 'Portfolio 2'
  1486. ),
  1487. 'Item' => array(
  1488. array(
  1489. 'id' => 2,
  1490. 'syfile_id' => 2,
  1491. 'published' => '',
  1492. 'name' => 'Item 2',
  1493. 'ItemsPortfolio' => array(
  1494. 'id' => 2,
  1495. 'item_id' => 2,
  1496. 'portfolio_id' => 2
  1497. )
  1498. ),
  1499. array(
  1500. 'id' => 6,
  1501. 'syfile_id' => 6,
  1502. 'published' => '',
  1503. 'name' => 'Item 6',
  1504. 'ItemsPortfolio' => array(
  1505. 'id' => 6,
  1506. 'item_id' => 6,
  1507. 'portfolio_id' => 2
  1508. )
  1509. )
  1510. )
  1511. );
  1512. $this->assertEquals($expected, $result);
  1513. $data = array('Item' => array('Item' => array(1, 2)));
  1514. $TestModel->id = 2;
  1515. $TestModel->save($data);
  1516. $result = $TestModel->findById(2);
  1517. $result['Item'] = Set::sort($result['Item'], '{n}.id', 'asc');
  1518. $expected = array(
  1519. 'Portfolio' => array(
  1520. 'id' => 2,
  1521. 'seller_id' => 1,
  1522. 'name' => 'Portfolio 2'
  1523. ),
  1524. 'Item' => array(
  1525. array(
  1526. 'id' => 1,
  1527. 'syfile_id' => 1,
  1528. 'published' => '',
  1529. 'name' => 'Item 1',
  1530. 'ItemsPortfolio' => array(
  1531. 'id' => 7,
  1532. 'item_id' => 1,
  1533. 'portfolio_id' => 2
  1534. )
  1535. ),
  1536. array(
  1537. 'id' => 2,
  1538. 'syfile_id' => 2,
  1539. 'published' => '',
  1540. 'name' => 'Item 2',
  1541. 'ItemsPortfolio' => array(
  1542. 'id' => 8,
  1543. 'item_id' => 2,
  1544. 'portfolio_id' => 2
  1545. )
  1546. )
  1547. )
  1548. );
  1549. $this->assertEquals($expected, $result);
  1550. }
  1551. /**
  1552. * testSaveHabtmCustomKeys method
  1553. *
  1554. * @return void
  1555. */
  1556. public function testSaveHabtmCustomKeys() {
  1557. $this->loadFixtures('Story', 'StoriesTag', 'Tag');
  1558. $Story = new Story();
  1559. $data = array(
  1560. 'Story' => array('story' => '1'),
  1561. 'Tag' => array(
  1562. 'Tag' => array(2, 3)
  1563. ));
  1564. $result = $Story->set($data);
  1565. $this->assertFalse(empty($result));
  1566. $result = $Story->save();
  1567. $this->assertFalse(empty($result));
  1568. $result = $Story->find('all', array('order' => array('Story.story')));
  1569. $expected = array(
  1570. array(
  1571. 'Story' => array(
  1572. 'story' => 1,
  1573. 'title' => 'First Story'
  1574. ),
  1575. 'Tag' => array(
  1576. array(
  1577. 'id' => 2,
  1578. 'tag' => 'tag2',
  1579. 'created' => '2007-03-18 12:24:23',
  1580. 'updated' => '2007-03-18 12:26:31'
  1581. ),
  1582. array(
  1583. 'id' => 3,
  1584. 'tag' => 'tag3',
  1585. 'created' => '2007-03-18 12:26:23',
  1586. 'updated' => '2007-03-18 12:28:31'
  1587. ))),
  1588. array(
  1589. 'Story' => array(
  1590. 'story' => 2,
  1591. 'title' => 'Second Story'
  1592. ),
  1593. 'Tag' => array()
  1594. ));
  1595. $this->assertEquals($expected, $result);
  1596. }
  1597. /**
  1598. * test that saving habtm records respects conditions set in the 'conditions' key
  1599. * for the association.
  1600. *
  1601. * @return void
  1602. */
  1603. public function testHabtmSaveWithConditionsInAssociation() {
  1604. $this->loadFixtures('JoinThing', 'Something', 'SomethingElse');
  1605. $Something = new Something();
  1606. $Something->unbindModel(array('hasAndBelongsToMany' => array('SomethingElse')), false);
  1607. $Something->bindModel(array(
  1608. 'hasAndBelongsToMany' => array(
  1609. 'DoomedSomethingElse' => array(
  1610. 'className' => 'SomethingElse',
  1611. 'joinTable' => 'join_things',
  1612. 'conditions' => array('JoinThing.doomed' => true),
  1613. 'unique' => true
  1614. ),
  1615. 'NotDoomedSomethingElse' => array(
  1616. 'className' => 'SomethingElse',
  1617. 'joinTable' => 'join_things',
  1618. 'conditions' => array('JoinThing.doomed' => 0),
  1619. 'unique' => true
  1620. )
  1621. )
  1622. ), false);
  1623. $result = $Something->read(null, 1);
  1624. $this->assertTrue(empty($result['NotDoomedSomethingElse']));
  1625. $this->assertEquals(1, count($result['DoomedSomethingElse']));
  1626. $data = array(
  1627. 'Something' => array('id' => 1),
  1628. 'NotDoomedSomethingElse' => array(
  1629. 'NotDoomedSomethingElse' => array(
  1630. array('something_else_id' => 2, 'doomed' => 0),
  1631. array('something_else_id' => 3, 'doomed' => 0)
  1632. )
  1633. )
  1634. );
  1635. $Something->create($data);
  1636. $result = $Something->save();
  1637. $this->assertFalse(empty($result));
  1638. $result = $Something->read(null, 1);
  1639. $this->assertEquals(2, count($result['NotDoomedSomethingElse']));
  1640. $this->assertEquals(1, count($result['DoomedSomethingElse']));
  1641. }
  1642. /**
  1643. * testHabtmSaveKeyResolution method
  1644. *
  1645. * @return void
  1646. */
  1647. public function testHabtmSaveKeyResolution() {
  1648. $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies');
  1649. $ThePaper = new ThePaper();
  1650. $ThePaper->id = 1;
  1651. $ThePaper->save(array('Monkey' => array(2, 3)));
  1652. $result = $ThePaper->findById(1);
  1653. $expected = array(
  1654. array(
  1655. 'id' => '2',
  1656. 'device_type_id' => '1',
  1657. 'name' => 'Device 2',
  1658. 'typ' => '1'
  1659. ),
  1660. array(
  1661. 'id' => '3',
  1662. 'device_type_id' => '1',
  1663. 'name' => 'Device 3',
  1664. 'typ' => '2'
  1665. ));
  1666. $this->assertEquals($expected, $result['Monkey']);
  1667. $ThePaper->id = 2;
  1668. $ThePaper->save(array('Monkey' => array(1, 2, 3)));
  1669. $result = $ThePaper->findById(2);
  1670. $expected = array(
  1671. array(
  1672. 'id' => '1',
  1673. 'device_type_id' => '1',
  1674. 'name' => 'Device 1',
  1675. 'typ' => '1'
  1676. ),
  1677. array(
  1678. 'id' => '2',
  1679. 'device_type_id' => '1',
  1680. 'name' => 'Device 2',
  1681. 'typ' => '1'
  1682. ),
  1683. array(
  1684. 'id' => '3',
  1685. 'device_type_id' => '1',
  1686. 'name' => 'Device 3',
  1687. 'typ' => '2'
  1688. ));
  1689. $this->assertEquals($expected, $result['Monkey']);
  1690. $ThePaper->id = 2;
  1691. $ThePaper->save(array('Monkey' => array(1, 3)));
  1692. $result = $ThePaper->findById(2);
  1693. $expected = array(
  1694. array(
  1695. 'id' => '1',
  1696. 'device_type_id' => '1',
  1697. 'name' => 'Device 1',
  1698. 'typ' => '1'
  1699. ),
  1700. array(
  1701. 'id' => '3',
  1702. 'device_type_id' => '1',
  1703. 'name' => 'Device 3',
  1704. 'typ' => '2'
  1705. ));
  1706. $this->assertEquals($expected, $result['Monkey']);
  1707. $result = $ThePaper->findById(1);
  1708. $expected = array(
  1709. array(
  1710. 'id' => '2',
  1711. 'device_type_id' => '1',
  1712. 'name' => 'Device 2',
  1713. 'typ' => '1'
  1714. ),
  1715. array(
  1716. 'id' => '3',
  1717. 'device_type_id' => '1',
  1718. 'name' => 'Device 3',
  1719. 'typ' => '2'
  1720. ));
  1721. $this->assertEquals($expected, $result['Monkey']);
  1722. }
  1723. /**
  1724. * testCreationOfEmptyRecord method
  1725. *
  1726. * @return void
  1727. */
  1728. public function testCreationOfEmptyRecord() {
  1729. $this->loadFixtures('Author');
  1730. $TestModel = new Author();
  1731. $this->assertEquals(4, $TestModel->find('count'));
  1732. $TestModel->deleteAll(true, false, false);
  1733. $this->assertEquals(0, $TestModel->find('count'));
  1734. $result = $TestModel->save();
  1735. $this->assertTrue(isset($result['Author']['created']));
  1736. $this->assertTrue(isset($result['Author']['updated']));
  1737. $this->assertEquals(1, $TestModel->find('count'));
  1738. }
  1739. /**
  1740. * testCreateWithPKFiltering method
  1741. *
  1742. * @return void
  1743. */
  1744. public function testCreateWithPKFiltering() {
  1745. $TestModel = new Article();
  1746. $data = array(
  1747. 'id' => 5,
  1748. 'user_id' => 2,
  1749. 'title' => 'My article',
  1750. 'body' => 'Some text'
  1751. );
  1752. $result = $TestModel->create($data);
  1753. $expected = array(
  1754. 'Article' => array(
  1755. 'published' => 'N',
  1756. 'id' => 5,
  1757. 'user_id' => 2,
  1758. 'title' => 'My article',
  1759. 'body' => 'Some text'
  1760. ));
  1761. $this->assertEquals($expected, $result);
  1762. $this->assertEquals(5, $TestModel->id);
  1763. $result = $TestModel->create($data, true);
  1764. $expected = array(
  1765. 'Article' => array(
  1766. 'published' => 'N',
  1767. 'id' => false,
  1768. 'user_id' => 2,
  1769. 'title' => 'My article',
  1770. 'body' => 'Some text'
  1771. ));
  1772. $this->assertEquals($expected, $result);
  1773. $this->assertFalse($TestModel->id);
  1774. $result = $TestModel->create(array('Article' => $data), true);
  1775. $expected = array(
  1776. 'Article' => array(
  1777. 'published' => 'N',
  1778. 'id' => false,
  1779. 'user_id' => 2,
  1780. 'title' => 'My article',
  1781. 'body' => 'Some text'
  1782. ));
  1783. $this->assertEquals($expected, $result);
  1784. $this->assertFalse($TestModel->id);
  1785. $data = array(
  1786. 'id' => 6,
  1787. 'user_id' => 2,
  1788. 'title' => 'My article',
  1789. 'body' => 'Some text',
  1790. 'created' => '1970-01-01 00:00:00',
  1791. 'updated' => '1970-01-01 12:00:00',
  1792. 'modified' => '1970-01-01 12:00:00'
  1793. );
  1794. $result = $TestModel->create($data);
  1795. $expected = array(
  1796. 'Article' => array(
  1797. 'published' => 'N',
  1798. 'id' => 6,
  1799. 'user_id' => 2,
  1800. 'title' => 'My article',
  1801. 'body' => 'Some text',
  1802. 'created' => '1970-01-01 00:00:00',
  1803. 'updated' => '1970-01-01 12:00:00',
  1804. 'modified' => '1970-01-01 12:00:00'
  1805. ));
  1806. $this->assertEquals($expected, $result);
  1807. $this->assertEquals(6, $TestModel->id);
  1808. $result = $TestModel->create(array(
  1809. 'Article' => array_diff_key($data, array(
  1810. 'created' => true,
  1811. 'updated' => true,
  1812. 'modified' => true
  1813. ))), true);
  1814. $expected = array(
  1815. 'Article' => array(
  1816. 'published' => 'N',
  1817. 'id' => false,
  1818. 'user_id' => 2,
  1819. 'title' => 'My article',
  1820. 'body' => 'Some text'
  1821. ));
  1822. $this->assertEquals($expected, $result);
  1823. $this->assertFalse($TestModel->id);
  1824. }
  1825. /**
  1826. * testCreationWithMultipleData method
  1827. *
  1828. * @return void
  1829. */
  1830. public function testCreationWithMultipleData() {
  1831. $this->loadFixtures('Article', 'Comment');
  1832. $Article = new Article();
  1833. $Comment = new Comment();
  1834. $articles = $Article->find('all', array(
  1835. 'fields' => array('id','title'),
  1836. 'recursive' => -1,
  1837. 'order' => array('Article.id' => 'ASC')
  1838. ));
  1839. $expected = array(
  1840. array('Article' => array(
  1841. 'id' => 1,
  1842. 'title' => 'First Article'
  1843. )),
  1844. array('Article' => array(
  1845. 'id' => 2,
  1846. 'title' => 'Second Article'
  1847. )),
  1848. array('Article' => array(
  1849. 'id' => 3,
  1850. 'title' => 'Third Article'
  1851. )));
  1852. $this->assertEquals($expected, $articles);
  1853. $comments = $Comment->find('all', array(
  1854. 'fields' => array('id','article_id','user_id','comment','published'),
  1855. 'recursive' => -1,
  1856. 'order' => array('Comment.id' => 'ASC')
  1857. ));
  1858. $expected = array(
  1859. array('Comment' => array(
  1860. 'id' => 1,
  1861. 'article_id' => 1,
  1862. 'user_id' => 2,
  1863. 'comment' => 'First Comment for First Article',
  1864. 'published' => 'Y'
  1865. )),
  1866. array('Comment' => array(
  1867. 'id' => 2,
  1868. 'article_id' => 1,
  1869. 'user_id' => 4,
  1870. 'comment' => 'Second Comment for First Article',
  1871. 'published' => 'Y'
  1872. )),
  1873. array('Comment' => array(
  1874. 'id' => 3,
  1875. 'article_id' => 1,
  1876. 'user_id' => 1,
  1877. 'comment' => 'Third Comment for First Article',
  1878. 'published' => 'Y'
  1879. )),
  1880. array('Comment' => array(
  1881. 'id' => 4,
  1882. 'article_id' => 1,
  1883. 'user_id' => 1,
  1884. 'comment' => 'Fourth Comment for First Article',
  1885. 'published' => 'N'
  1886. )),
  1887. array('Comment' => array(
  1888. 'id' => 5,
  1889. 'article_id' => 2,
  1890. 'user_id' => 1,
  1891. 'comment' => 'First Comment for Second Article',
  1892. 'published' => 'Y'
  1893. )),
  1894. array('Comment' => array(
  1895. 'id' => 6,
  1896. 'article_id' => 2,
  1897. 'user_id' => 2,
  1898. 'comment' => 'Second Comment for Second Article',
  1899. 'published' => 'Y'
  1900. )));
  1901. $this->assertEquals($expected, $comments);
  1902. $data = array(
  1903. 'Comment' => array(
  1904. 'article_id' => 2,
  1905. 'user_id' => 4,
  1906. 'comment' => 'Brand New Comment',
  1907. 'published' => 'N'
  1908. ),
  1909. 'Article' => array(
  1910. 'id' => 2,
  1911. 'title' => 'Second Article Modified'
  1912. ));
  1913. $result = $Comment->create($data);
  1914. $this->assertFalse(empty($result));
  1915. $result = $Comment->save();
  1916. $this->assertFalse(empty($result));
  1917. $articles = $Article->find('all', array(
  1918. 'fields' => array('id','title'),
  1919. 'recursive' => -1,
  1920. 'order' => array('Article.id' => 'ASC')
  1921. ));
  1922. $expected = array(
  1923. array('Article' => array(
  1924. 'id' => 1,
  1925. 'title' => 'First Article'
  1926. )),
  1927. array('Article' => array(
  1928. 'id' => 2,
  1929. 'title' => 'Second Article'
  1930. )),
  1931. array('Article' => array(
  1932. 'id' => 3,
  1933. 'title' => 'Third Article'
  1934. )));
  1935. $this->assertEquals($expected, $articles);
  1936. $comments = $Comment->find('all', array(
  1937. 'fields' => array('id','article_id','user_id','comment','published'),
  1938. 'recursive' => -1,
  1939. 'order' => array('Comment.id' => 'ASC')
  1940. ));
  1941. $expected = array(
  1942. array('Comment' => array(
  1943. 'id' => 1,
  1944. 'article_id' => 1,
  1945. 'user_id' => 2,
  1946. 'comment' => 'First Comment for First Article',
  1947. 'published' => 'Y'
  1948. )),
  1949. array('Comment' => array(
  1950. 'id' => 2,
  1951. 'article_id' => 1,
  1952. 'user_id' => 4,
  1953. 'comment' => 'Second Comment for First Article',
  1954. 'published' => 'Y'
  1955. )),
  1956. array('Comment' => array(
  1957. 'id' => 3,
  1958. 'article_id' => 1,
  1959. 'user_id' => 1,
  1960. 'comment' => 'Third Comment for First Article',
  1961. 'published' => 'Y'
  1962. )),
  1963. array('Comment' => array(
  1964. 'id' => 4,
  1965. 'article_id' => 1,
  1966. 'user_id' => 1,
  1967. 'comment' => 'Fourth Comment for First Article',
  1968. 'published' => 'N'
  1969. )),
  1970. array('Comment' => array(
  1971. 'id' => 5,
  1972. 'article_id' => 2,
  1973. 'user_id' => 1,
  1974. 'comment' => 'First Comment for Second Article',
  1975. 'published' => 'Y'
  1976. )),
  1977. array('Comment' => array(
  1978. 'id' => 6,
  1979. 'article_id' => 2,
  1980. 'user_id' => 2, 'comment' =>
  1981. 'Second Comment for Second Article',
  1982. 'published' => 'Y'
  1983. )),
  1984. array('Comment' => array(
  1985. 'id' => 7,
  1986. 'article_id' => 2,
  1987. 'user_id' => 4,
  1988. 'comment' => 'Brand New Comment',
  1989. 'published' => 'N'
  1990. )));
  1991. $this->assertEquals($expected, $comments);
  1992. }
  1993. /**
  1994. * testCreationWithMultipleDataSameModel method
  1995. *
  1996. * @return void
  1997. */
  1998. public function testCreationWithMultipleDataSameModel() {
  1999. $this->loadFixtures('Article');
  2000. $Article = new Article();
  2001. $SecondaryArticle = new Article();
  2002. $result = $Article->field('title', array('id' => 1));
  2003. $this->assertEquals('First Article', $result);
  2004. $data = array(
  2005. 'Article' => array(
  2006. 'user_id' => 2,
  2007. 'title' => 'Brand New Article',
  2008. 'body' => 'Brand New Article Body',
  2009. 'published' => 'Y'
  2010. ),
  2011. 'SecondaryArticle' => array(
  2012. 'id' => 1
  2013. ));
  2014. $Article->create();
  2015. $result = $Article->save($data);
  2016. $this->assertFalse(empty($result));
  2017. $result = $Article->getInsertID();
  2018. $this->assertTrue(!empty($result));
  2019. $result = $Article->field('title', array('id' => 1));
  2020. $this->assertEquals('First Article', $result);
  2021. $articles = $Article->find('all', array(
  2022. 'fields' => array('id','title'),
  2023. 'recursive' => -1,
  2024. 'order' => array('Article.id' => 'ASC')
  2025. ));
  2026. $expected = array(
  2027. array('Article' => array(
  2028. 'id' => 1,
  2029. 'title' => 'First Article'
  2030. )),
  2031. array('Article' => array(
  2032. 'id' => 2,
  2033. 'title' => 'Second Article'
  2034. )),
  2035. array('Article' => array(
  2036. 'id' => 3,
  2037. 'title' => 'Third Article'
  2038. )),
  2039. array('Article' => array(
  2040. 'id' => 4,
  2041. 'title' => 'Brand New Article'
  2042. )));
  2043. $this->assertEquals($expected, $articles);
  2044. }
  2045. /**
  2046. * testCreationWithMultipleDataSameModelManualInstances method
  2047. *
  2048. * @return void
  2049. */
  2050. public function testCreationWithMultipleDataSameModelManualInstances() {
  2051. $this->loadFixtures('PrimaryModel');
  2052. $Primary = new PrimaryModel();
  2053. $Secondary = new PrimaryModel();
  2054. $result = $Primary->field('primary_name', array('id' => 1));
  2055. $this->assertEquals('Primary Name Existing', $result);
  2056. $data = array(
  2057. 'PrimaryModel' => array(
  2058. 'primary_name' => 'Primary Name New'
  2059. ),
  2060. 'SecondaryModel' => array(
  2061. 'id' => array(1)
  2062. ));
  2063. $Primary->create();
  2064. $result = $Primary->save($data);
  2065. $this->assertFalse(empty($result));
  2066. $result = $Primary->field('primary_name', array('id' => 1));
  2067. $this->assertEquals('Primary Name Existing', $result);
  2068. $result = $Primary->getInsertID();
  2069. $this->assertTrue(!empty($result));
  2070. $result = $Primary->field('primary_name', array('id' => $result));
  2071. $this->assertEquals('Primary Name New', $result);
  2072. $result = $Primary->find('count');
  2073. $this->assertEquals(2, $result);
  2074. }
  2075. /**
  2076. * testRecordExists method
  2077. *
  2078. * @return void
  2079. */
  2080. public function testRecordExists() {
  2081. $this->loadFixtures('User');
  2082. $TestModel = new User();
  2083. $this->assertFalse($TestModel->exists());
  2084. $TestModel->read(null, 1);
  2085. $this->assertTrue($TestModel->exists());
  2086. $TestModel->create();
  2087. $this->assertFalse($TestModel->exists());
  2088. $TestModel->id = 4;
  2089. $this->assertTrue($TestModel->exists());
  2090. $TestModel = new TheVoid();
  2091. $this->assertFalse($TestModel->exists());
  2092. }
  2093. /**
  2094. * testRecordExistsMissingTable method
  2095. *
  2096. * @expectedException PDOException
  2097. * @return void
  2098. */
  2099. public function testRecordExistsMissingTable() {
  2100. $TestModel = new TheVoid();
  2101. $TestModel->id = 5;
  2102. $TestModel->exists();
  2103. }
  2104. /**
  2105. * testUpdateExisting method
  2106. *
  2107. * @return void
  2108. */
  2109. public function testUpdateExisting() {
  2110. $this->loadFixtures('User', 'Article', 'Comment');
  2111. $TestModel = new User();
  2112. $TestModel->create();
  2113. $TestModel->save(array(
  2114. 'User' => array(
  2115. 'user' => 'some user',
  2116. 'password' => 'some password'
  2117. )));
  2118. $this->assertTrue(is_int($TestModel->id) || (intval($TestModel->id) === 5));
  2119. $id = $TestModel->id;
  2120. $TestModel->save(array(
  2121. 'User' => array(
  2122. 'user' => 'updated user'
  2123. )));
  2124. $this->assertEquals($TestModel->id, $id);
  2125. $result = $TestModel->findById($id);
  2126. $this->assertEquals('updated user', $result['User']['user']);
  2127. $this->assertEquals('some password', $result['User']['password']);
  2128. $Article = new Article();
  2129. $Comment = new Comment();
  2130. $data = array(
  2131. 'Comment' => array(
  2132. 'id' => 1,
  2133. 'comment' => 'First Comment for First Article'
  2134. ),
  2135. 'Article' => array(
  2136. 'id' => 2,
  2137. 'title' => 'Second Article'
  2138. ));
  2139. $result = $Article->save($data);
  2140. $this->assertFalse(empty($result));
  2141. $result = $Comment->save($data);
  2142. $this->assertFalse(empty($result));
  2143. }
  2144. /**
  2145. * test updating records and saving blank values.
  2146. *
  2147. * @return void
  2148. */
  2149. public function testUpdateSavingBlankValues() {
  2150. $this->loadFixtures('Article');
  2151. $Article = new Article();
  2152. $Article->validate = array();
  2153. $Article->create();
  2154. $result = $Article->save(array(
  2155. 'id' => 1,
  2156. 'title' => '',
  2157. 'body' => ''
  2158. ));
  2159. $this->assertTrue((bool)$result);
  2160. $result = $Article->find('first', array('conditions' => array('Article.id' => 1)));
  2161. $this->assertEquals('', $result['Article']['title'], 'Title is not blank');
  2162. $this->assertEquals('', $result['Article']['body'], 'Body is not blank');
  2163. }
  2164. /**
  2165. * testUpdateMultiple method
  2166. *
  2167. * @return void
  2168. */
  2169. public function testUpdateMultiple() {
  2170. $this->loadFixtures('Comment', 'Article', 'User', 'CategoryThread');
  2171. $TestModel = new Comment();
  2172. $result = Set::extract($TestModel->find('all'), '{n}.Comment.user_id');
  2173. $expected = array('2', '4', '1', '1', '1', '2');
  2174. $this->assertEquals($expected, $result);
  2175. $TestModel->updateAll(array('Comment.user_id' => 5), array('Comment.user_id' => 2));
  2176. $result = Set::combine($TestModel->find('all'), '{n}.Comment.id', '{n}.Comment.user_id');
  2177. $expected = array(1 => 5, 2 => 4, 3 => 1, 4 => 1, 5 => 1, 6 => 5);
  2178. $this->assertEquals($expected, $result);
  2179. $result = $TestModel->updateAll(
  2180. array('Comment.comment' => "'Updated today'"),
  2181. array('Comment.user_id' => 5)
  2182. );
  2183. $this->assertFalse(empty($result));
  2184. $result = Set::extract(
  2185. $TestModel->find('all', array(
  2186. 'conditions' => array(
  2187. 'Comment.user_id' => 5
  2188. ))),
  2189. '{n}.Comment.comment'
  2190. );
  2191. $expected = array_fill(0, 2, 'Updated today');
  2192. $this->assertEquals($expected, $result);
  2193. }
  2194. /**
  2195. * testHabtmUuidWithUuidId method
  2196. *
  2197. * @return void
  2198. */
  2199. public function testHabtmUuidWithUuidId() {
  2200. $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolio', 'UuiditemsUuidportfolioNumericid');
  2201. $TestModel = new Uuidportfolio();
  2202. $data = array('Uuidportfolio' => array('name' => 'Portfolio 3'));
  2203. $data['Uuiditem']['Uuiditem'] = array('483798c8-c7cc-430e-8cf9-4fcc40cf8569');
  2204. $TestModel->create($data);
  2205. $TestModel->save();
  2206. $id = $TestModel->id;
  2207. $result = $TestModel->read(null, $id);
  2208. $this->assertEquals(1, count($result['Uuiditem']));
  2209. $this->assertEquals(36, strlen($result['Uuiditem'][0]['UuiditemsUuidportfolio']['id']));
  2210. }
  2211. /**
  2212. * test HABTM saving when join table has no primary key and only 2 columns.
  2213. *
  2214. * @return void
  2215. */
  2216. public function testHabtmSavingWithNoPrimaryKeyUuidJoinTable() {
  2217. $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
  2218. $Fruit = new Fruit();
  2219. $data = array(
  2220. 'Fruit' => array(
  2221. 'color' => 'Red',
  2222. 'shape' => 'Heart-shaped',
  2223. 'taste' => 'sweet',
  2224. 'name' => 'Strawberry',
  2225. ),
  2226. 'UuidTag' => array(
  2227. 'UuidTag' => array(
  2228. '481fc6d0-b920-43e0-e50f-6d1740cf8569'
  2229. )
  2230. )
  2231. );
  2232. $result = $Fruit->save($data);
  2233. $this->assertFalse(empty($result));
  2234. }
  2235. /**
  2236. * test HABTM saving when join table has no primary key and only 2 columns, no with model is used.
  2237. *
  2238. * @return void
  2239. */
  2240. public function testHabtmSavingWithNoPrimaryKeyUuidJoinTableNoWith() {
  2241. $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
  2242. $Fruit = new FruitNoWith();
  2243. $data = array(
  2244. 'Fruit' => array(
  2245. 'color' => 'Red',
  2246. 'shape' => 'Heart-shaped',
  2247. 'taste' => 'sweet',
  2248. 'name' => 'Strawberry',
  2249. ),
  2250. 'UuidTag' => array(
  2251. 'UuidTag' => array(
  2252. '481fc6d0-b920-43e0-e50f-6d1740cf8569'
  2253. )
  2254. )
  2255. );
  2256. $result = $Fruit->save($data);
  2257. $this->assertFalse(empty($result));
  2258. }
  2259. /**
  2260. * testHabtmUuidWithNumericId method
  2261. *
  2262. * @return void
  2263. */
  2264. public function testHabtmUuidWithNumericId() {
  2265. $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolioNumericid');
  2266. $TestModel = new Uuiditem();
  2267. $data = array('Uuiditem' => array('name' => 'Item 7', 'published' => 0));
  2268. $data['Uuidportfolio']['Uuidportfolio'] = array('480af662-eb8c-47d3-886b-230540cf8569');
  2269. $TestModel->create($data);
  2270. $TestModel->save();
  2271. $id = $TestModel->id;
  2272. $result = $TestModel->read(null, $id);
  2273. $this->assertEquals(1, count($result['Uuidportfolio']));
  2274. }
  2275. /**
  2276. * testSaveMultipleHabtm method
  2277. *
  2278. * @return void
  2279. */
  2280. public function testSaveMultipleHabtm() {
  2281. $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC');
  2282. $TestModel = new JoinA();
  2283. $result = $TestModel->findById(1);
  2284. $expected = array(
  2285. 'JoinA' => array(
  2286. 'id' => 1,
  2287. 'name' => 'Join A 1',
  2288. 'body' => 'Join A 1 Body',
  2289. 'created' => '2008-01-03 10:54:23',
  2290. 'updated' => '2008-01-03 10:54:23'
  2291. ),
  2292. 'JoinB' => array(
  2293. 0 => array(
  2294. 'id' => 2,
  2295. 'name' => 'Join B 2',
  2296. 'created' => '2008-01-03 10:55:02',
  2297. 'updated' => '2008-01-03 10:55:02',
  2298. 'JoinAsJoinB' => array(
  2299. 'id' => 1,
  2300. 'join_a_id' => 1,
  2301. 'join_b_id' => 2,
  2302. 'other' => 'Data for Join A 1 Join B 2',
  2303. 'created' => '2008-01-03 10:56:33',
  2304. 'updated' => '2008-01-03 10:56:33'
  2305. ))),
  2306. 'JoinC' => array(
  2307. 0 => array(
  2308. 'id' => 2,
  2309. 'name' => 'Join C 2',
  2310. 'created' => '2008-01-03 10:56:12',
  2311. 'updated' => '2008-01-03 10:56:12',
  2312. 'JoinAsJoinC' => array(
  2313. 'id' => 1,
  2314. 'join_a_id' => 1,
  2315. 'join_c_id' => 2,
  2316. 'other' => 'Data for Join A 1 Join C 2',
  2317. 'created' => '2008-01-03 10:57:22',
  2318. 'updated' => '2008-01-03 10:57:22'
  2319. ))));
  2320. $this->assertEquals($expected, $result);
  2321. $ts = date('Y-m-d H:i:s');
  2322. $TestModel->id = 1;
  2323. $data = array(
  2324. 'JoinA' => array(
  2325. 'id' => '1',
  2326. 'name' => 'New name for Join A 1',
  2327. 'updated' => $ts
  2328. ),
  2329. 'JoinB' => array(
  2330. array(
  2331. 'id' => 1,
  2332. 'join_b_id' => 2,
  2333. 'other' => 'New data for Join A 1 Join B 2',
  2334. 'created' => $ts,
  2335. 'updated' => $ts
  2336. )),
  2337. 'JoinC' => array(
  2338. array(
  2339. 'id' => 1,
  2340. 'join_c_id' => 2,
  2341. 'other' => 'New data for Join A 1 Join C 2',
  2342. 'created' => $ts,
  2343. 'updated' => $ts
  2344. )));
  2345. $TestModel->set($data);
  2346. $TestModel->save();
  2347. $result = $TestModel->findById(1);
  2348. $expected = array(
  2349. 'JoinA' => array(
  2350. 'id' => 1,
  2351. 'name' => 'New name for Join A 1',
  2352. 'body' => 'Join A 1 Body',
  2353. 'created' => '2008-01-03 10:54:23',
  2354. 'updated' => $ts
  2355. ),
  2356. 'JoinB' => array(
  2357. 0 => array(
  2358. 'id' => 2,
  2359. 'name' => 'Join B 2',
  2360. 'created' => '2008-01-03 10:55:02',
  2361. 'updated' => '2008-01-03 10:55:02',
  2362. 'JoinAsJoinB' => array(
  2363. 'id' => 1,
  2364. 'join_a_id' => 1,
  2365. 'join_b_id' => 2,
  2366. 'other' => 'New data for Join A 1 Join B 2',
  2367. 'created' => $ts,
  2368. 'updated' => $ts
  2369. ))),
  2370. 'JoinC' => array(
  2371. 0 => array(
  2372. 'id' => 2,
  2373. 'name' => 'Join C 2',
  2374. 'created' => '2008-01-03 10:56:12',
  2375. 'updated' => '2008-01-03 10:56:12',
  2376. 'JoinAsJoinC' => array(
  2377. 'id' => 1,
  2378. 'join_a_id' => 1,
  2379. 'join_c_id' => 2,
  2380. 'other' => 'New data for Join A 1 Join C 2',
  2381. 'created' => $ts,
  2382. 'updated' => $ts
  2383. ))));
  2384. $this->assertEquals($expected, $result);
  2385. }
  2386. /**
  2387. * testSaveAll method
  2388. *
  2389. * @return void
  2390. */
  2391. public function testSaveAll() {
  2392. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  2393. $TestModel = new Post();
  2394. $result = $TestModel->find('all');
  2395. $this->assertEquals(3, count($result));
  2396. $this->assertFalse(isset($result[3]));
  2397. $ts = date('Y-m-d H:i:s');
  2398. $TestModel->saveAll(array(
  2399. 'Post' => array(
  2400. 'title' => 'Post with Author',
  2401. 'body' => 'This post will be saved with an author'
  2402. ),
  2403. 'Author' => array(
  2404. 'user' => 'bob',
  2405. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
  2406. )));
  2407. $result = $TestModel->find('all');
  2408. $expected = array(
  2409. 'Post' => array(
  2410. 'id' => '4',
  2411. 'author_id' => '5',
  2412. 'title' => 'Post with Author',
  2413. 'body' => 'This post will be saved with an author',
  2414. 'published' => 'N'
  2415. ),
  2416. 'Author' => array(
  2417. 'id' => '5',
  2418. 'user' => 'bob',
  2419. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
  2420. 'test' => 'working'
  2421. ));
  2422. $this->assertTrue($result[3]['Post']['created'] >= $ts);
  2423. $this->assertTrue($result[3]['Post']['updated'] >= $ts);
  2424. $this->assertTrue($result[3]['Author']['created'] >= $ts);
  2425. $this->assertTrue($result[3]['Author']['updated'] >= $ts);
  2426. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  2427. unset($result[3]['Author']['created'], $result[3]['Author']['updated']);
  2428. $this->assertEquals($expected, $result[3]);
  2429. $this->assertEquals(4, count($result));
  2430. $TestModel->deleteAll(true);
  2431. $this->assertEquals(array(), $TestModel->find('all'));
  2432. // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
  2433. $this->db->truncate($TestModel);
  2434. $ts = date('Y-m-d H:i:s');
  2435. $TestModel->saveAll(array(
  2436. array(
  2437. 'title' => 'Multi-record post 1',
  2438. 'body' => 'First multi-record post',
  2439. 'author_id' => 2
  2440. ),
  2441. array(
  2442. 'title' => 'Multi-record post 2',
  2443. 'body' => 'Second multi-record post',
  2444. 'author_id' => 2
  2445. )));
  2446. $result = $TestModel->find('all', array(
  2447. 'recursive' => -1,
  2448. 'order' => 'Post.id ASC'
  2449. ));
  2450. $expected = array(
  2451. array(
  2452. 'Post' => array(
  2453. 'id' => '1',
  2454. 'author_id' => '2',
  2455. 'title' => 'Multi-record post 1',
  2456. 'body' => 'First multi-record post',
  2457. 'published' => 'N'
  2458. )),
  2459. array(
  2460. 'Post' => array(
  2461. 'id' => '2',
  2462. 'author_id' => '2',
  2463. 'title' => 'Multi-record post 2',
  2464. 'body' => 'Second multi-record post',
  2465. 'published' => 'N'
  2466. )));
  2467. $this->assertTrue($result[0]['Post']['created'] >= $ts);
  2468. $this->assertTrue($result[0]['Post']['updated'] >= $ts);
  2469. $this->assertTrue($result[1]['Post']['created'] >= $ts);
  2470. $this->assertTrue($result[1]['Post']['updated'] >= $ts);
  2471. unset($result[0]['Post']['created'], $result[0]['Post']['updated']);
  2472. unset($result[1]['Post']['created'], $result[1]['Post']['updated']);
  2473. $this->assertEquals($expected, $result);
  2474. $TestModel = new Comment();
  2475. $ts = date('Y-m-d H:i:s');
  2476. $result = $TestModel->saveAll(array(
  2477. 'Comment' => array(
  2478. 'article_id' => 2,
  2479. 'user_id' => 2,
  2480. 'comment' => 'New comment with attachment',
  2481. 'published' => 'Y'
  2482. ),
  2483. 'Attachment' => array(
  2484. 'attachment' => 'some_file.tgz'
  2485. )));
  2486. $this->assertFalse(empty($result));
  2487. $result = $TestModel->find('all');
  2488. $expected = array(
  2489. 'id' => '7',
  2490. 'article_id' => '2',
  2491. 'user_id' => '2',
  2492. 'comment' => 'New comment with attachment',
  2493. 'published' => 'Y'
  2494. );
  2495. $this->assertTrue($result[6]['Comment']['created'] >= $ts);
  2496. $this->assertTrue($result[6]['Comment']['updated'] >= $ts);
  2497. unset($result[6]['Comment']['created'], $result[6]['Comment']['updated']);
  2498. $this->assertEquals($expected, $result[6]['Comment']);
  2499. $expected = array(
  2500. 'id' => '2',
  2501. 'comment_id' => '7',
  2502. 'attachment' => 'some_file.tgz'
  2503. );
  2504. $this->assertTrue($result[6]['Attachment']['created'] >= $ts);
  2505. $this->assertTrue($result[6]['Attachment']['updated'] >= $ts);
  2506. unset($result[6]['Attachment']['created'], $result[6]['Attachment']['updated']);
  2507. $this->assertEquals($expected, $result[6]['Attachment']);
  2508. }
  2509. /**
  2510. * Test SaveAll with Habtm relations
  2511. *
  2512. * @return void
  2513. */
  2514. public function testSaveAllHabtm() {
  2515. $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  2516. $data = array(
  2517. 'Article' => array(
  2518. 'user_id' => 1,
  2519. 'title' => 'Article Has and belongs to Many Tags'
  2520. ),
  2521. 'Tag' => array(
  2522. 'Tag' => array(1, 2)
  2523. ),
  2524. 'Comment' => array(
  2525. array(
  2526. 'comment' => 'Article comment',
  2527. 'user_id' => 1
  2528. )));
  2529. $Article = new Article();
  2530. $result = $Article->saveAll($data);
  2531. $this->assertFalse(empty($result));
  2532. $result = $Article->read();
  2533. $this->assertEquals(2, count($result['Tag']));
  2534. $this->assertEquals('tag1', $result['Tag'][0]['tag']);
  2535. $this->assertEquals(1, count($result['Comment']));
  2536. $this->assertEquals(1, count($result['Comment'][0]['comment']));
  2537. }
  2538. /**
  2539. * Test SaveAll with Habtm relations and extra join table fields
  2540. *
  2541. * @return void
  2542. */
  2543. public function testSaveAllHabtmWithExtraJoinTableFields() {
  2544. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  2545. $data = array(
  2546. 'Something' => array(
  2547. 'id' => 4,
  2548. 'title' => 'Extra Fields',
  2549. 'body' => 'Extra Fields Body',
  2550. 'published' => '1'
  2551. ),
  2552. 'SomethingElse' => array(
  2553. array('something_else_id' => 1, 'doomed' => '1'),
  2554. array('something_else_id' => 2, 'doomed' => '0'),
  2555. array('something_else_id' => 3, 'doomed' => '1')
  2556. )
  2557. );
  2558. $Something = new Something();
  2559. $result = $Something->saveAll($data);
  2560. $this->assertFalse(empty($result));
  2561. $result = $Something->read();
  2562. $this->assertEquals(3, count($result['SomethingElse']));
  2563. $this->assertTrue(Set::matches('/Something[id=4]', $result));
  2564. $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
  2565. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
  2566. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
  2567. $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
  2568. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
  2569. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
  2570. $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
  2571. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
  2572. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
  2573. }
  2574. /**
  2575. * testSaveAllHasOne method
  2576. *
  2577. * @return void
  2578. */
  2579. public function testSaveAllHasOne() {
  2580. $model = new Comment();
  2581. $model->deleteAll(true);
  2582. $this->assertEquals(array(), $model->find('all'));
  2583. $model->Attachment->deleteAll(true);
  2584. $this->assertEquals(array(), $model->Attachment->find('all'));
  2585. $this->assertTrue($model->saveAll(array(
  2586. 'Comment' => array(
  2587. 'comment' => 'Comment with attachment',
  2588. 'article_id' => 1,
  2589. 'user_id' => 1
  2590. ),
  2591. 'Attachment' => array(
  2592. 'attachment' => 'some_file.zip'
  2593. ))));
  2594. $result = $model->find('all', array('fields' => array(
  2595. 'Comment.id', 'Comment.comment', 'Attachment.id',
  2596. 'Attachment.comment_id', 'Attachment.attachment'
  2597. )));
  2598. $expected = array(array(
  2599. 'Comment' => array(
  2600. 'id' => '1',
  2601. 'comment' => 'Comment with attachment'
  2602. ),
  2603. 'Attachment' => array(
  2604. 'id' => '1',
  2605. 'comment_id' => '1',
  2606. 'attachment' => 'some_file.zip'
  2607. )));
  2608. $this->assertEquals($expected, $result);
  2609. $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
  2610. $data = array(
  2611. 'Comment' => array(
  2612. 'comment' => 'Comment with attachment',
  2613. 'article_id' => 1,
  2614. 'user_id' => 1
  2615. ),
  2616. 'Attachment' => array(
  2617. 'attachment' => 'some_file.zip'
  2618. ));
  2619. $this->assertTrue($model->saveAll($data, array('validate' => 'first')));
  2620. }
  2621. /**
  2622. * testSaveAllBelongsTo method
  2623. *
  2624. * @return void
  2625. */
  2626. public function testSaveAllBelongsTo() {
  2627. $model = new Comment();
  2628. $model->deleteAll(true);
  2629. $this->assertEquals(array(), $model->find('all'));
  2630. $model->Article->deleteAll(true);
  2631. $this->assertEquals(array(), $model->Article->find('all'));
  2632. $this->assertTrue($model->saveAll(array(
  2633. 'Comment' => array(
  2634. 'comment' => 'Article comment',
  2635. 'article_id' => 1,
  2636. 'user_id' => 1
  2637. ),
  2638. 'Article' => array(
  2639. 'title' => 'Model Associations 101',
  2640. 'user_id' => 1
  2641. ))));
  2642. $result = $model->find('all', array('fields' => array(
  2643. 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
  2644. )));
  2645. $expected = array(array(
  2646. 'Comment' => array(
  2647. 'id' => '1',
  2648. 'article_id' => '1',
  2649. 'comment' => 'Article comment'
  2650. ),
  2651. 'Article' => array(
  2652. 'id' => '1',
  2653. 'title' => 'Model Associations 101'
  2654. )));
  2655. $this->assertEquals($expected, $result);
  2656. }
  2657. /**
  2658. * testSaveAllHasOneValidation method
  2659. *
  2660. * @return void
  2661. */
  2662. public function testSaveAllHasOneValidation() {
  2663. $model = new Comment();
  2664. $model->deleteAll(true);
  2665. $this->assertEquals(array(), $model->find('all'));
  2666. $model->Attachment->deleteAll(true);
  2667. $this->assertEquals(array(), $model->Attachment->find('all'));
  2668. $model->validate = array('comment' => 'notEmpty');
  2669. $model->Attachment->validate = array('attachment' => 'notEmpty');
  2670. $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
  2671. $this->assertEquals($model->saveAll(
  2672. array(
  2673. 'Comment' => array(
  2674. 'comment' => '',
  2675. 'article_id' => 1,
  2676. 'user_id' => 1
  2677. ),
  2678. 'Attachment' => array('attachment' => '')
  2679. ),
  2680. array('validate' => 'first')
  2681. ), false);
  2682. $expected = array(
  2683. 'Comment' => array('comment' => array('This field cannot be left blank')),
  2684. 'Attachment' => array('attachment' => array('This field cannot be left blank'))
  2685. );
  2686. $this->assertEquals($expected['Comment'], $model->validationErrors);
  2687. $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
  2688. $this->assertFalse($model->saveAll(
  2689. array(
  2690. 'Comment' => array('comment' => '', 'article_id' => 1, 'user_id' => 1),
  2691. 'Attachment' => array('attachment' => '')
  2692. ),
  2693. array('validate' => 'only')
  2694. ));
  2695. $this->assertEquals($expected['Comment'], $model->validationErrors);
  2696. $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
  2697. }
  2698. /**
  2699. * testSaveAllAtomic method
  2700. *
  2701. * @return void
  2702. */
  2703. public function testSaveAllAtomic() {
  2704. $this->loadFixtures('Article', 'User', 'Comment');
  2705. $TestModel = new Article();
  2706. $result = $TestModel->saveAll(array(
  2707. 'Article' => array(
  2708. 'title' => 'Post with Author',
  2709. 'body' => 'This post will be saved with an author',
  2710. 'user_id' => 2
  2711. ),
  2712. 'Comment' => array(
  2713. array('comment' => 'First new comment', 'user_id' => 2))
  2714. ), array('atomic' => false));
  2715. $this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
  2716. $result = $TestModel->saveAll(array(
  2717. array(
  2718. 'id' => '1',
  2719. 'title' => 'Baleeted First Post',
  2720. 'body' => 'Baleeted!',
  2721. 'published' => 'N'
  2722. ),
  2723. array(
  2724. 'id' => '2',
  2725. 'title' => 'Just update the title'
  2726. ),
  2727. array(
  2728. 'title' => 'Creating a fourth post',
  2729. 'body' => 'Fourth post body',
  2730. 'user_id' => 2
  2731. )
  2732. ), array('atomic' => false));
  2733. $this->assertSame($result, array(true, true, true));
  2734. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  2735. $result = $TestModel->saveAll(array(
  2736. array(
  2737. 'id' => '1',
  2738. 'title' => 'Un-Baleeted First Post',
  2739. 'body' => 'Not Baleeted!',
  2740. 'published' => 'Y'
  2741. ),
  2742. array(
  2743. 'id' => '2',
  2744. 'title' => '',
  2745. 'body' => 'Trying to get away with an empty title'
  2746. )
  2747. ), array('validate' => true, 'atomic' => false));
  2748. $this->assertSame(array(true, false), $result);
  2749. $result = $TestModel->saveAll(array(
  2750. 'Article' => array('id' => 2),
  2751. 'Comment' => array(
  2752. array(
  2753. 'comment' => 'First new comment',
  2754. 'published' => 'Y',
  2755. 'user_id' => 1
  2756. ),
  2757. array(
  2758. 'comment' => 'Second new comment',
  2759. 'published' => 'Y',
  2760. 'user_id' => 2
  2761. ))
  2762. ), array('validate' => true, 'atomic' => false));
  2763. $this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
  2764. }
  2765. /**
  2766. * testSaveAllDeepAssociated method
  2767. *
  2768. * @return void
  2769. */
  2770. public function testSaveAllDeepAssociated() {
  2771. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  2772. $TestModel = new Article();
  2773. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  2774. $TestModel->hasAndBelongsToMany = array();
  2775. $result = $TestModel->saveAll(array(
  2776. 'Article' => array('id' => 2),
  2777. 'Comment' => array(
  2778. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
  2779. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  2780. )
  2781. ), array('deep' => true));
  2782. $this->assertTrue($result);
  2783. $result = $TestModel->findById(2);
  2784. $expected = array(
  2785. 'First Comment for Second Article',
  2786. 'Second Comment for Second Article',
  2787. 'First new comment',
  2788. 'Second new comment'
  2789. );
  2790. $result = Set::extract(Set::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  2791. $this->assertEquals($expected, $result);
  2792. $result = $TestModel->Comment->User->field('id', array('user' => 'newuser', 'password' => 'newuserpass'));
  2793. $this->assertEquals(5, $result);
  2794. $result = $TestModel->saveAll(array(
  2795. 'Article' => array('id' => 2),
  2796. 'Comment' => array(
  2797. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  2798. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
  2799. )
  2800. ), array('deep' => true));
  2801. $this->assertTrue($result);
  2802. $result = $TestModel->findById(2);
  2803. $expected = array(
  2804. 'First Comment for Second Article',
  2805. 'Second Comment for Second Article',
  2806. 'First new comment',
  2807. 'Second new comment',
  2808. 'Third new comment',
  2809. 'Fourth new comment'
  2810. );
  2811. $result = Set::extract(Set::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  2812. $this->assertEquals($expected, $result);
  2813. $result = $TestModel->Comment->Attachment->field('id', array('attachment' => 'deepsaved'));
  2814. $this->assertEquals(2, $result);
  2815. $data = array(
  2816. 'Attachment' => array(
  2817. 'attachment' => 'deepsave insert',
  2818. ),
  2819. 'Comment' => array(
  2820. 'comment' => 'First comment deepsave insert',
  2821. 'published' => 'Y',
  2822. 'user_id' => 5,
  2823. 'Article' => array(
  2824. 'title' => 'First Article deepsave insert',
  2825. 'body' => 'First Article Body deepsave insert',
  2826. 'User' => array(
  2827. 'user' => '',
  2828. 'password' => 'magic'
  2829. ),
  2830. ),
  2831. )
  2832. );
  2833. $TestModel->Comment->Attachment->create();
  2834. $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => true));
  2835. $this->assertFalse($result);
  2836. $expected = array('User' => array('user' => array('This field cannot be left blank')));
  2837. $this->assertEquals($expected, $TestModel->validationErrors);
  2838. $data['Comment']['Article']['User']['user'] = 'deepsave';
  2839. $TestModel->Comment->Attachment->create();
  2840. $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => true));
  2841. $this->assertTrue($result);
  2842. $result = $TestModel->Comment->Attachment->findById($TestModel->Comment->Attachment->id);
  2843. $expected = array(
  2844. 'Attachment' => array(
  2845. 'id' => '3',
  2846. 'comment_id' => '11',
  2847. 'attachment' => 'deepsave insert',
  2848. ),
  2849. 'Comment' => array(
  2850. 'id' => '11',
  2851. 'article_id' => '4',
  2852. 'user_id' => '5',
  2853. 'comment' => 'First comment deepsave insert',
  2854. 'published' => 'Y',
  2855. )
  2856. );
  2857. unset($result['Attachment']['created'], $result['Attachment']['updated']);
  2858. $this->assertEquals($expected['Attachment'], $result['Attachment']);
  2859. unset($result['Comment']['created'], $result['Comment']['updated']);
  2860. $this->assertEquals($expected['Comment'], $result['Comment']);
  2861. $result = $TestModel->findById($result['Comment']['article_id']);
  2862. $expected = array(
  2863. 'Article' => array(
  2864. 'id' => '4',
  2865. 'user_id' => '6',
  2866. 'title' => 'First Article deepsave insert',
  2867. 'body' => 'First Article Body deepsave insert',
  2868. 'published' => 'N',
  2869. ),
  2870. 'User' => array(
  2871. 'id' => '6',
  2872. 'user' => 'deepsave',
  2873. 'password' => 'magic',
  2874. ),
  2875. 'Comment' => array(
  2876. array(
  2877. 'id' => '11',
  2878. 'article_id' => '4',
  2879. 'user_id' => '5',
  2880. 'comment' => 'First comment deepsave insert',
  2881. 'published' => 'Y',
  2882. )
  2883. )
  2884. );
  2885. unset(
  2886. $result['Article']['created'], $result['Article']['updated'],
  2887. $result['User']['created'], $result['User']['updated'],
  2888. $result['Comment'][0]['created'], $result['Comment'][0]['updated']
  2889. );
  2890. $this->assertEquals($expected, $result);
  2891. }
  2892. /**
  2893. * testSaveAllDeepMany
  2894. * tests the validate methods with deeper recursive data
  2895. *
  2896. * @return void
  2897. */
  2898. public function testSaveAllDeepMany() {
  2899. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  2900. $TestModel = new Article();
  2901. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  2902. $TestModel->hasAndBelongsToMany = array();
  2903. $data = array(
  2904. array(
  2905. 'id' => 1, 'body' => '',
  2906. 'Comment' => array(
  2907. array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
  2908. array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
  2909. )
  2910. ),
  2911. array(
  2912. 'Article' => array('id' => 2),
  2913. 'Comment' => array(
  2914. array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => '')),
  2915. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  2916. )
  2917. )
  2918. );
  2919. $TestModel->Comment->validate['comment'] = 'notEmpty';
  2920. $result = $TestModel->saveAll($data, array('deep' => true));
  2921. $this->assertFalse($result);
  2922. $expected = array(
  2923. 0 => array(
  2924. 'body' => array('This field cannot be left blank')
  2925. ),
  2926. 1 => array(
  2927. 'Comment' => array(
  2928. 0 => array(
  2929. 'User' => array(
  2930. 'password' => array('This field cannot be left blank')
  2931. )
  2932. ),
  2933. 1 => array(
  2934. 'comment' => array('This field cannot be left blank')
  2935. )
  2936. )
  2937. )
  2938. );
  2939. $result = $TestModel->validationErrors;
  2940. $this->assertSame($expected, $result);
  2941. $data = array(
  2942. array(
  2943. 'Article' => array('id' => 1),
  2944. 'Comment' => array(
  2945. array('comment' => 'First comment deepsaved article 1', 'published' => 'Y', 'User' => array('user' => 'savemany', 'password' => 'manysaved')),
  2946. array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
  2947. )
  2948. ),
  2949. array(
  2950. 'Article' => array('id' => 2),
  2951. 'Comment' => array(
  2952. array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => 'moresaved')),
  2953. array('comment' => 'Second comment deepsaved article 2', 'published' => 'Y', 'user_id' => 2)
  2954. )
  2955. )
  2956. );
  2957. $result = $TestModel->saveAll($data, array('deep' => true));
  2958. $this->assertTrue($result);
  2959. }
  2960. /**
  2961. * testSaveAllDeepValidateOnly
  2962. * tests the validate methods with deeper recursive data
  2963. *
  2964. * @return void
  2965. */
  2966. public function testSaveAllDeepValidateOnly() {
  2967. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  2968. $TestModel = new Article();
  2969. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  2970. $TestModel->hasAndBelongsToMany = array();
  2971. $TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
  2972. $TestModel->Comment->validate['comment'] = 'notEmpty';
  2973. $result = $TestModel->saveAll(
  2974. array(
  2975. 'Article' => array('id' => 2),
  2976. 'Comment' => array(
  2977. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
  2978. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  2979. )
  2980. ),
  2981. array('validate' => 'only', 'deep' => true)
  2982. );
  2983. $this->assertTrue($result);
  2984. $result = $TestModel->saveAll(
  2985. array(
  2986. 'Article' => array('id' => 2),
  2987. 'Comment' => array(
  2988. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  2989. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  2990. )
  2991. ),
  2992. array('validate' => 'only', 'deep' => true)
  2993. );
  2994. $this->assertFalse($result);
  2995. $result = $TestModel->saveAll(
  2996. array(
  2997. 'Article' => array('id' => 2),
  2998. 'Comment' => array(
  2999. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
  3000. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3001. )
  3002. ),
  3003. array('validate' => 'only', 'atomic' => false, 'deep' => true)
  3004. );
  3005. $expected = array(
  3006. 'Article' => true,
  3007. 'Comment' => array(
  3008. true,
  3009. true
  3010. )
  3011. );
  3012. $this->assertSame($expected, $result);
  3013. $result = $TestModel->saveAll(
  3014. array(
  3015. 'Article' => array('id' => 2),
  3016. 'Comment' => array(
  3017. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3018. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3019. )
  3020. ),
  3021. array('validate' => 'only', 'atomic' => false, 'deep' => true)
  3022. );
  3023. $expected = array(
  3024. 'Article' => true,
  3025. 'Comment' => array(
  3026. false,
  3027. true
  3028. )
  3029. );
  3030. $this->assertSame($expected, $result);
  3031. $result = $TestModel->saveAll(array(
  3032. 'Article' => array('id' => 2),
  3033. 'Comment' => array(
  3034. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3035. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
  3036. )
  3037. ),
  3038. array('validate' => 'only', 'deep' => true)
  3039. );
  3040. $this->assertTrue($result);
  3041. $result = $TestModel->saveAll(array(
  3042. 'Article' => array('id' => 2),
  3043. 'Comment' => array(
  3044. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3045. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  3046. )
  3047. ),
  3048. array('validate' => 'only', 'deep' => true)
  3049. );
  3050. $this->assertFalse($result);
  3051. $result = $TestModel->saveAll(array(
  3052. 'Article' => array('id' => 2),
  3053. 'Comment' => array(
  3054. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3055. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsave'))
  3056. )
  3057. ),
  3058. array('validate' => 'only', 'atomic' => false, 'deep' => true)
  3059. );
  3060. $expected = array(
  3061. 'Article' => true,
  3062. 'Comment' => array(
  3063. true,
  3064. true
  3065. )
  3066. );
  3067. $this->assertSame($expected, $result);
  3068. $result = $TestModel->saveAll(array(
  3069. 'Article' => array('id' => 2),
  3070. 'Comment' => array(
  3071. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3072. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  3073. )
  3074. ),
  3075. array('validate' => 'only', 'atomic' => false, 'deep' => true)
  3076. );
  3077. $expected = array(
  3078. 'Article' => true,
  3079. 'Comment' => array(
  3080. true,
  3081. false
  3082. )
  3083. );
  3084. $this->assertSame($expected, $result);
  3085. $expected = array(
  3086. 'Comment' => array(
  3087. 1 => array(
  3088. 'Attachment' => array(
  3089. 'attachment' => array('This field cannot be left blank')
  3090. )
  3091. )
  3092. )
  3093. );
  3094. $result = $TestModel->validationErrors;
  3095. $this->assertSame($expected, $result);
  3096. $data = array(
  3097. 'Attachment' => array(
  3098. 'attachment' => 'deepsave insert',
  3099. ),
  3100. 'Comment' => array(
  3101. 'comment' => 'First comment deepsave insert',
  3102. 'published' => 'Y',
  3103. 'user_id' => 5,
  3104. 'Article' => array(
  3105. 'title' => 'First Article deepsave insert',
  3106. 'body' => 'First Article Body deepsave insert',
  3107. 'User' => array(
  3108. 'user' => 'deepsave',
  3109. 'password' => 'magic'
  3110. ),
  3111. ),
  3112. )
  3113. );
  3114. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3115. $this->assertTrue($result);
  3116. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3117. $expected = array(
  3118. 'Attachment' => true,
  3119. 'Comment' => true
  3120. );
  3121. $this->assertSame($expected, $result);
  3122. $data = array(
  3123. 'Attachment' => array(
  3124. 'attachment' => 'deepsave insert',
  3125. ),
  3126. 'Comment' => array(
  3127. 'comment' => 'First comment deepsave insert',
  3128. 'published' => 'Y',
  3129. 'user_id' => 5,
  3130. 'Article' => array(
  3131. 'title' => 'First Article deepsave insert',
  3132. 'body' => 'First Article Body deepsave insert',
  3133. 'User' => array(
  3134. 'user' => '',
  3135. 'password' => 'magic'
  3136. ),
  3137. ),
  3138. )
  3139. );
  3140. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3141. $this->assertFalse($result);
  3142. $result = $TestModel->Comment->Attachment->validationErrors;
  3143. $expected = array(
  3144. 'Comment' => array(
  3145. 'Article' => array(
  3146. 'User' => array(
  3147. 'user' => array('This field cannot be left blank')
  3148. )
  3149. )
  3150. )
  3151. );
  3152. $this->assertSame($expected, $result);
  3153. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3154. $expected = array(
  3155. 'Attachment' => true,
  3156. 'Comment' => false
  3157. );
  3158. $this->assertEquals($expected, $result);
  3159. $data['Comment']['Article']['body'] = '';
  3160. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3161. $this->assertFalse($result);
  3162. $result = $TestModel->Comment->Attachment->validationErrors;
  3163. $expected = array(
  3164. 'Comment' => array(
  3165. 'Article' => array(
  3166. 'body' => array('This field cannot be left blank')
  3167. )
  3168. )
  3169. );
  3170. $this->assertSame($expected, $result);
  3171. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3172. $expected = array(
  3173. 'Attachment' => true,
  3174. 'Comment' => false
  3175. );
  3176. $this->assertEquals($expected, $result);
  3177. $data['Comment']['comment'] = '';
  3178. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3179. $this->assertFalse($result);
  3180. $result = $TestModel->Comment->Attachment->validationErrors;
  3181. $expected = array(
  3182. 'Comment' => array(
  3183. 'comment' => array('This field cannot be left blank')
  3184. )
  3185. );
  3186. $this->assertSame($expected, $result);
  3187. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3188. $expected = array(
  3189. 'Attachment' => true,
  3190. 'Comment' => false
  3191. );
  3192. $this->assertEquals($expected, $result);
  3193. $data['Attachment']['attachment'] = '';
  3194. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3195. $this->assertFalse($result);
  3196. $result = $TestModel->Comment->Attachment->validationErrors;
  3197. $expected = array('attachment' => array('This field cannot be left blank'));
  3198. $this->assertSame($expected, $result);
  3199. $result = $TestModel->Comment->validationErrors;
  3200. $expected = array('comment' => array('This field cannot be left blank'));
  3201. $this->assertSame($expected, $result);
  3202. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3203. $expected = array(
  3204. 'Attachment' => false,
  3205. 'Comment' => false
  3206. );
  3207. $this->assertEquals($expected, $result);
  3208. }
  3209. /**
  3210. * testSaveAllNotDeepAssociated method
  3211. * test that only directly associated data gets saved
  3212. *
  3213. * @return void
  3214. */
  3215. public function testSaveAllNotDeepAssociated() {
  3216. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  3217. $TestModel = new Article();
  3218. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3219. $TestModel->hasAndBelongsToMany = array();
  3220. $result = $TestModel->saveAll(array(
  3221. 'Article' => array('id' => 2),
  3222. 'Comment' => array(
  3223. array(
  3224. 'comment' => 'First new comment', 'published' => 'Y', 'user_id' => 2,
  3225. 'User' => array('user' => 'newuser', 'password' => 'newuserpass')
  3226. ),
  3227. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3228. )
  3229. ), array('deep' => false));
  3230. $this->assertTrue($result);
  3231. $result = $TestModel->Comment->User->field('id', array('user' => 'newuser', 'password' => 'newuserpass'));
  3232. $this->assertFalse($result);
  3233. $result = $TestModel->saveAll(array(
  3234. 'Article' => array('id' => 2),
  3235. 'Comment' => array(
  3236. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 4),
  3237. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
  3238. )
  3239. ), array('deep' => false));
  3240. $this->assertTrue($result);
  3241. $result = $TestModel->Comment->Attachment->field('id', array('attachment' => 'deepsaved'));
  3242. $this->assertFalse($result);
  3243. $data = array(
  3244. 'Attachment' => array(
  3245. 'attachment' => 'deepsave insert',
  3246. ),
  3247. 'Comment' => array(
  3248. 'comment' => 'First comment deepsave insert',
  3249. 'published' => 'Y',
  3250. 'user_id' => 4,
  3251. 'article_id' => 1,
  3252. 'Article' => array(
  3253. 'title' => 'First Article deepsave insert',
  3254. 'body' => 'First Article Body deepsave insert',
  3255. 'User' => array(
  3256. 'user' => 'deepsave',
  3257. 'password' => 'magic'
  3258. ),
  3259. ),
  3260. )
  3261. );
  3262. $expected = $TestModel->User->find('count');
  3263. $TestModel->Comment->Attachment->create();
  3264. $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => false));
  3265. $this->assertTrue($result);
  3266. $result = $TestModel->User->find('count');
  3267. $this->assertEquals($expected, $result);
  3268. $result = $TestModel->Comment->Attachment->findById($TestModel->Comment->Attachment->id);
  3269. $expected = array(
  3270. 'Attachment' => array(
  3271. 'id' => '2',
  3272. 'comment_id' => '11',
  3273. 'attachment' => 'deepsave insert',
  3274. ),
  3275. 'Comment' => array(
  3276. 'id' => '11',
  3277. 'article_id' => 1,
  3278. 'user_id' => '4',
  3279. 'comment' => 'First comment deepsave insert',
  3280. 'published' => 'Y',
  3281. )
  3282. );
  3283. unset($result['Attachment']['created'], $result['Attachment']['updated']);
  3284. $this->assertEquals($expected['Attachment'], $result['Attachment']);
  3285. unset($result['Comment']['created'], $result['Comment']['updated']);
  3286. $this->assertEquals($expected['Comment'], $result['Comment']);
  3287. }
  3288. /**
  3289. * testSaveAllNotDeepMany
  3290. * tests the save methods to not save deeper recursive data
  3291. *
  3292. * @return void
  3293. */
  3294. public function testSaveAllNotDeepMany() {
  3295. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  3296. $TestModel = new Article();
  3297. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3298. $TestModel->hasAndBelongsToMany = array();
  3299. $data = array(
  3300. array(
  3301. 'id' => 1, 'body' => '',
  3302. 'Comment' => array(
  3303. array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
  3304. array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
  3305. )
  3306. ),
  3307. array(
  3308. 'Article' => array('id' => 2),
  3309. 'Comment' => array(
  3310. array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => '')),
  3311. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  3312. )
  3313. )
  3314. );
  3315. $TestModel->Comment->validate['comment'] = 'notEmpty';
  3316. $result = $TestModel->saveAll($data, array('deep' => false));
  3317. $this->assertFalse($result);
  3318. $expected = array(
  3319. 0 => array(
  3320. 'body' => array('This field cannot be left blank')
  3321. )
  3322. );
  3323. $result = $TestModel->validationErrors;
  3324. $this->assertSame($expected, $result);
  3325. $data = array(
  3326. array(
  3327. 'Article' => array('id' => 1, 'body' => 'Ignore invalid comment'),
  3328. 'Comment' => array(
  3329. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  3330. )
  3331. ),
  3332. array(
  3333. 'Article' => array('id' => 2, 'body' => 'Same here'),
  3334. 'Comment' => array(
  3335. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  3336. )
  3337. )
  3338. );
  3339. $result = $TestModel->saveAll($data, array('deep' => false));
  3340. $this->assertTrue($result);
  3341. }
  3342. /**
  3343. * testSaveAllNotDeepValidateOnly
  3344. * tests the validate methods to not validate deeper recursive data
  3345. *
  3346. * @return void
  3347. */
  3348. public function testSaveAllNotDeepValidateOnly() {
  3349. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  3350. $TestModel = new Article();
  3351. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3352. $TestModel->hasAndBelongsToMany = array();
  3353. $TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
  3354. $TestModel->Comment->validate['comment'] = 'notEmpty';
  3355. $result = $TestModel->saveAll(
  3356. array(
  3357. 'Article' => array('id' => 2, 'body' => ''),
  3358. 'Comment' => array(
  3359. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3360. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3361. )
  3362. ),
  3363. array('validate' => 'only', 'deep' => false)
  3364. );
  3365. $this->assertFalse($result);
  3366. $expected = array('body' => array('This field cannot be left blank'));
  3367. $result = $TestModel->validationErrors;
  3368. $this->assertSame($expected, $result);
  3369. $result = $TestModel->saveAll(
  3370. array(
  3371. 'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
  3372. 'Comment' => array(
  3373. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3374. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3375. )
  3376. ),
  3377. array('validate' => 'only', 'deep' => false)
  3378. );
  3379. $this->assertTrue($result);
  3380. $result = $TestModel->saveAll(
  3381. array(
  3382. 'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
  3383. 'Comment' => array(
  3384. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3385. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3386. )
  3387. ),
  3388. array('validate' => 'only', 'atomic' => false, 'deep' => false)
  3389. );
  3390. $expected = array(
  3391. 'Article' => true,
  3392. 'Comment' => array(
  3393. true,
  3394. true
  3395. )
  3396. );
  3397. $this->assertSame($expected, $result);
  3398. $result = $TestModel->saveAll(array(
  3399. 'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
  3400. 'Comment' => array(
  3401. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3402. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  3403. )
  3404. ),
  3405. array('validate' => 'only', 'deep' => false)
  3406. );
  3407. $this->assertTrue($result);
  3408. $result = $TestModel->saveAll(array(
  3409. 'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
  3410. 'Comment' => array(
  3411. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3412. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  3413. )
  3414. ),
  3415. array('validate' => 'only', 'atomic' => false, 'deep' => false)
  3416. );
  3417. $expected = array(
  3418. 'Article' => true,
  3419. 'Comment' => array(
  3420. true,
  3421. true
  3422. )
  3423. );
  3424. $this->assertSame($expected, $result);
  3425. $expected = array();
  3426. $result = $TestModel->validationErrors;
  3427. $this->assertSame($expected, $result);
  3428. $data = array(
  3429. 'Attachment' => array(
  3430. 'attachment' => 'deepsave insert',
  3431. ),
  3432. 'Comment' => array(
  3433. 'comment' => 'First comment deepsave insert',
  3434. 'published' => 'Y',
  3435. 'user_id' => 5,
  3436. 'Article' => array(
  3437. 'title' => 'First Article deepsave insert ignored',
  3438. 'body' => 'First Article Body deepsave insert',
  3439. 'User' => array(
  3440. 'user' => '',
  3441. 'password' => 'magic'
  3442. ),
  3443. ),
  3444. )
  3445. );
  3446. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
  3447. $this->assertTrue($result);
  3448. $result = $TestModel->Comment->Attachment->validationErrors;
  3449. $expected = array();
  3450. $this->assertSame($expected, $result);
  3451. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
  3452. $expected = array(
  3453. 'Attachment' => true,
  3454. 'Comment' => true
  3455. );
  3456. $this->assertEquals($expected, $result);
  3457. $data['Comment']['Article']['body'] = '';
  3458. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
  3459. $this->assertTrue($result);
  3460. $result = $TestModel->Comment->Attachment->validationErrors;
  3461. $expected = array();
  3462. $this->assertSame($expected, $result);
  3463. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
  3464. $expected = array(
  3465. 'Attachment' => true,
  3466. 'Comment' => true
  3467. );
  3468. $this->assertEquals($expected, $result);
  3469. }
  3470. /**
  3471. * testSaveAllHasMany method
  3472. *
  3473. * @return void
  3474. */
  3475. public function testSaveAllHasMany() {
  3476. $this->loadFixtures('Article', 'Comment');
  3477. $TestModel = new Article();
  3478. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3479. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  3480. $result = $TestModel->saveAll(array(
  3481. 'Article' => array('id' => 2),
  3482. 'Comment' => array(
  3483. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  3484. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3485. )
  3486. ));
  3487. $this->assertFalse(empty($result));
  3488. $result = $TestModel->findById(2);
  3489. $expected = array(
  3490. 'First Comment for Second Article',
  3491. 'Second Comment for Second Article',
  3492. 'First new comment',
  3493. 'Second new comment'
  3494. );
  3495. $result = Set::extract(Set::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  3496. $this->assertEquals($expected, $result);
  3497. $result = $TestModel->saveAll(
  3498. array(
  3499. 'Article' => array('id' => 2),
  3500. 'Comment' => array(
  3501. array(
  3502. 'comment' => 'Third new comment',
  3503. 'published' => 'Y',
  3504. 'user_id' => 1
  3505. ))),
  3506. array('atomic' => false)
  3507. );
  3508. $this->assertFalse(empty($result));
  3509. $result = $TestModel->findById(2);
  3510. $expected = array(
  3511. 'First Comment for Second Article',
  3512. 'Second Comment for Second Article',
  3513. 'First new comment',
  3514. 'Second new comment',
  3515. 'Third new comment'
  3516. );
  3517. $result = Set::extract(Set::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  3518. $this->assertEquals($expected, $result);
  3519. $TestModel->beforeSaveReturn = false;
  3520. $result = $TestModel->saveAll(
  3521. array(
  3522. 'Article' => array('id' => 2),
  3523. 'Comment' => array(
  3524. array(
  3525. 'comment' => 'Fourth new comment',
  3526. 'published' => 'Y',
  3527. 'user_id' => 1
  3528. ))),
  3529. array('atomic' => false)
  3530. );
  3531. $this->assertEquals(array('Article' => false), $result);
  3532. $result = $TestModel->findById(2);
  3533. $expected = array(
  3534. 'First Comment for Second Article',
  3535. 'Second Comment for Second Article',
  3536. 'First new comment',
  3537. 'Second new comment',
  3538. 'Third new comment'
  3539. );
  3540. $result = Set::extract(Set::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  3541. $this->assertEquals($expected, $result);
  3542. }
  3543. /**
  3544. * testSaveAllHasManyValidation method
  3545. *
  3546. * @return void
  3547. */
  3548. public function testSaveAllHasManyValidation() {
  3549. $this->loadFixtures('Article', 'Comment');
  3550. $TestModel = new Article();
  3551. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  3552. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  3553. $result = $TestModel->saveAll(array(
  3554. 'Article' => array('id' => 2),
  3555. 'Comment' => array(
  3556. array('comment' => '', 'published' => 'Y', 'user_id' => 1),
  3557. )
  3558. ), array('validate' => true));
  3559. $this->assertFalse($result);
  3560. $expected = array('Comment' => array(
  3561. array('comment' => array('This field cannot be left blank'))
  3562. ));
  3563. $this->assertEquals($expected, $TestModel->validationErrors);
  3564. $expected = array(
  3565. array('comment' => array('This field cannot be left blank'))
  3566. );
  3567. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  3568. $result = $TestModel->saveAll(array(
  3569. 'Article' => array('id' => 2),
  3570. 'Comment' => array(
  3571. array(
  3572. 'comment' => '',
  3573. 'published' => 'Y',
  3574. 'user_id' => 1
  3575. ))
  3576. ), array('validate' => 'first'));
  3577. $this->assertFalse($result);
  3578. }
  3579. /**
  3580. * test saveAll with transactions and ensure there is no missing rollback.
  3581. *
  3582. * @return void
  3583. */
  3584. public function testSaveAllManyRowsTransactionNoRollback() {
  3585. $this->loadFixtures('Post');
  3586. $this->getMock('DboSource', array('connect', 'rollback', 'describe'), array(), 'MockTransactionDboSource');
  3587. $db = ConnectionManager::create('mock_transaction', array(
  3588. 'datasource' => 'MockTransactionDboSource',
  3589. ));
  3590. $db->expects($this->once())
  3591. ->method('describe')
  3592. ->will($this->returnValue(array()));
  3593. $db->expects($this->once())->method('rollback');
  3594. $Post = new Post('mock_transaction');
  3595. $Post->validate = array(
  3596. 'title' => array('rule' => array('notEmpty'))
  3597. );
  3598. $data = array(
  3599. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3600. array('author_id' => 1, 'title' => '')
  3601. );
  3602. $Post->saveAll($data, array('atomic' => true));
  3603. }
  3604. /**
  3605. * test saveAll with transactions and ensure there is no missing rollback.
  3606. *
  3607. * @return void
  3608. */
  3609. public function testSaveAllAssociatedTransactionNoRollback() {
  3610. $testDb = ConnectionManager::getDataSource('test');
  3611. $mock = $this->getMock(
  3612. 'DboSource',
  3613. array('connect', 'rollback', 'describe', 'create', 'update', 'begin'),
  3614. array(),
  3615. 'MockTransactionAssociatedDboSource'
  3616. );
  3617. $db = ConnectionManager::create('mock_transaction_assoc', array(
  3618. 'datasource' => 'MockTransactionAssociatedDboSource',
  3619. ));
  3620. $this->mockObjects[] = $db;
  3621. $db->columns = $testDb->columns;
  3622. $db->expects($this->once())->method('rollback');
  3623. $db->expects($this->any())->method('describe')
  3624. ->will($this->returnValue(array(
  3625. 'id' => array('type' => 'integer', 'length' => 11),
  3626. 'title' => array('type' => 'string'),
  3627. 'body' => array('type' => 'text'),
  3628. 'published' => array('type' => 'string')
  3629. )));
  3630. $Post = new Post();
  3631. $Post->useDbConfig = 'mock_transaction_assoc';
  3632. $Post->Author->useDbConfig = 'mock_transaction_assoc';
  3633. $Post->Author->validate = array(
  3634. 'user' => array('rule' => array('notEmpty'))
  3635. );
  3636. $data = array(
  3637. 'Post' => array(
  3638. 'title' => 'New post',
  3639. 'body' => 'Content',
  3640. 'published' => 'Y'
  3641. ),
  3642. 'Author' => array(
  3643. 'user' => '',
  3644. 'password' => "sekret"
  3645. )
  3646. );
  3647. $Post->saveAll($data, array('validate' => true));
  3648. }
  3649. /**
  3650. * test saveAll with nested saveAll call.
  3651. *
  3652. * @return void
  3653. */
  3654. public function testSaveAllNestedSaveAll() {
  3655. $this->loadFixtures('Sample');
  3656. $TransactionTestModel = new TransactionTestModel();
  3657. $data = array(
  3658. array('apple_id' => 1, 'name' => 'sample5'),
  3659. );
  3660. $this->assertTrue($TransactionTestModel->saveAll($data, array('atomic' => true)));
  3661. }
  3662. /**
  3663. * testSaveAllTransaction method
  3664. *
  3665. * @return void
  3666. */
  3667. public function testSaveAllTransaction() {
  3668. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  3669. $TestModel = new Post();
  3670. $TestModel->validate = array('title' => 'notEmpty');
  3671. $data = array(
  3672. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3673. array('author_id' => 1, 'title' => 'New Fifth Post'),
  3674. array('author_id' => 1, 'title' => '')
  3675. );
  3676. $ts = date('Y-m-d H:i:s');
  3677. $this->assertFalse($TestModel->saveAll($data));
  3678. $result = $TestModel->find('all', array('recursive' => -1));
  3679. $expected = array(
  3680. array('Post' => array(
  3681. 'id' => '1',
  3682. 'author_id' => 1,
  3683. 'title' => 'First Post',
  3684. 'body' => 'First Post Body',
  3685. 'published' => 'Y',
  3686. 'created' => '2007-03-18 10:39:23',
  3687. 'updated' => '2007-03-18 10:41:31'
  3688. )),
  3689. array('Post' => array(
  3690. 'id' => '2',
  3691. 'author_id' => 3,
  3692. 'title' => 'Second Post',
  3693. 'body' => 'Second Post Body',
  3694. 'published' => 'Y',
  3695. 'created' => '2007-03-18 10:41:23',
  3696. 'updated' => '2007-03-18 10:43:31'
  3697. )),
  3698. array('Post' => array(
  3699. 'id' => '3',
  3700. 'author_id' => 1,
  3701. 'title' => 'Third Post',
  3702. 'body' => 'Third Post Body',
  3703. 'published' => 'Y',
  3704. 'created' => '2007-03-18 10:43:23',
  3705. 'updated' => '2007-03-18 10:45:31'
  3706. )));
  3707. if (count($result) != 3) {
  3708. // Database doesn't support transactions
  3709. $expected[] = array(
  3710. 'Post' => array(
  3711. 'id' => '4',
  3712. 'author_id' => 1,
  3713. 'title' => 'New Fourth Post',
  3714. 'body' => null,
  3715. 'published' => 'N',
  3716. 'created' => $ts,
  3717. 'updated' => $ts
  3718. ));
  3719. $expected[] = array(
  3720. 'Post' => array(
  3721. 'id' => '5',
  3722. 'author_id' => 1,
  3723. 'title' => 'New Fifth Post',
  3724. 'body' => null,
  3725. 'published' => 'N',
  3726. 'created' => $ts,
  3727. 'updated' => $ts
  3728. ));
  3729. $this->assertEquals($expected, $result);
  3730. // Skip the rest of the transactional tests
  3731. return;
  3732. }
  3733. $this->assertEquals($expected, $result);
  3734. $data = array(
  3735. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3736. array('author_id' => 1, 'title' => ''),
  3737. array('author_id' => 1, 'title' => 'New Sixth Post')
  3738. );
  3739. $ts = date('Y-m-d H:i:s');
  3740. $this->assertFalse($TestModel->saveAll($data));
  3741. $result = $TestModel->find('all', array('recursive' => -1));
  3742. $expected = array(
  3743. array('Post' => array(
  3744. 'id' => '1',
  3745. 'author_id' => 1,
  3746. 'title' => 'First Post',
  3747. 'body' => 'First Post Body',
  3748. 'published' => 'Y',
  3749. 'created' => '2007-03-18 10:39:23',
  3750. 'updated' => '2007-03-18 10:41:31'
  3751. )),
  3752. array('Post' => array(
  3753. 'id' => '2',
  3754. 'author_id' => 3,
  3755. 'title' => 'Second Post',
  3756. 'body' => 'Second Post Body',
  3757. 'published' => 'Y',
  3758. 'created' => '2007-03-18 10:41:23',
  3759. 'updated' => '2007-03-18 10:43:31'
  3760. )),
  3761. array('Post' => array(
  3762. 'id' => '3',
  3763. 'author_id' => 1,
  3764. 'title' => 'Third Post',
  3765. 'body' => 'Third Post Body',
  3766. 'published' => 'Y',
  3767. 'created' => '2007-03-18 10:43:23',
  3768. 'updated' => '2007-03-18 10:45:31'
  3769. )));
  3770. if (count($result) != 3) {
  3771. // Database doesn't support transactions
  3772. $expected[] = array(
  3773. 'Post' => array(
  3774. 'id' => '4',
  3775. 'author_id' => 1,
  3776. 'title' => 'New Fourth Post',
  3777. 'body' => 'Third Post Body',
  3778. 'published' => 'N',
  3779. 'created' => $ts,
  3780. 'updated' => $ts
  3781. ));
  3782. $expected[] = array(
  3783. 'Post' => array(
  3784. 'id' => '5',
  3785. 'author_id' => 1,
  3786. 'title' => 'Third Post',
  3787. 'body' => 'Third Post Body',
  3788. 'published' => 'N',
  3789. 'created' => $ts,
  3790. 'updated' => $ts
  3791. ));
  3792. }
  3793. $this->assertEquals($expected, $result);
  3794. $TestModel->validate = array('title' => 'notEmpty');
  3795. $data = array(
  3796. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3797. array('author_id' => 1, 'title' => 'New Fifth Post'),
  3798. array('author_id' => 1, 'title' => 'New Sixth Post')
  3799. );
  3800. $this->assertTrue($TestModel->saveAll($data));
  3801. $result = $TestModel->find('all', array(
  3802. 'recursive' => -1,
  3803. 'fields' => array('author_id', 'title','body','published'),
  3804. 'order' => array('Post.created' => 'ASC')
  3805. ));
  3806. $expected = array(
  3807. array('Post' => array(
  3808. 'author_id' => 1,
  3809. 'title' => 'First Post',
  3810. 'body' => 'First Post Body',
  3811. 'published' => 'Y'
  3812. )),
  3813. array('Post' => array(
  3814. 'author_id' => 3,
  3815. 'title' => 'Second Post',
  3816. 'body' => 'Second Post Body',
  3817. 'published' => 'Y'
  3818. )),
  3819. array('Post' => array(
  3820. 'author_id' => 1,
  3821. 'title' => 'Third Post',
  3822. 'body' => 'Third Post Body',
  3823. 'published' => 'Y'
  3824. )),
  3825. array('Post' => array(
  3826. 'author_id' => 1,
  3827. 'title' => 'New Fourth Post',
  3828. 'body' => '',
  3829. 'published' => 'N'
  3830. )),
  3831. array('Post' => array(
  3832. 'author_id' => 1,
  3833. 'title' => 'New Fifth Post',
  3834. 'body' => '',
  3835. 'published' => 'N'
  3836. )),
  3837. array('Post' => array(
  3838. 'author_id' => 1,
  3839. 'title' => 'New Sixth Post',
  3840. 'body' => '',
  3841. 'published' => 'N'
  3842. )));
  3843. $this->assertEquals($expected, $result);
  3844. }
  3845. /**
  3846. * testSaveAllValidation method
  3847. *
  3848. * @return void
  3849. */
  3850. public function testSaveAllValidation() {
  3851. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  3852. $TestModel = new Post();
  3853. $data = array(
  3854. array(
  3855. 'id' => '1',
  3856. 'title' => 'Baleeted First Post',
  3857. 'body' => 'Baleeted!',
  3858. 'published' => 'N'
  3859. ),
  3860. array(
  3861. 'id' => '2',
  3862. 'title' => 'Just update the title'
  3863. ),
  3864. array(
  3865. 'title' => 'Creating a fourth post',
  3866. 'body' => 'Fourth post body',
  3867. 'author_id' => 2
  3868. ));
  3869. $ts = date('Y-m-d H:i:s');
  3870. $this->assertTrue($TestModel->saveAll($data));
  3871. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3872. $expected = array(
  3873. array(
  3874. 'Post' => array(
  3875. 'id' => '1',
  3876. 'author_id' => '1',
  3877. 'title' => 'Baleeted First Post',
  3878. 'body' => 'Baleeted!',
  3879. 'published' => 'N',
  3880. 'created' => '2007-03-18 10:39:23'
  3881. )),
  3882. array(
  3883. 'Post' => array(
  3884. 'id' => '2',
  3885. 'author_id' => '3',
  3886. 'title' => 'Just update the title',
  3887. 'body' => 'Second Post Body',
  3888. 'published' => 'Y',
  3889. 'created' => '2007-03-18 10:41:23'
  3890. )),
  3891. array(
  3892. 'Post' => array(
  3893. 'id' => '3',
  3894. 'author_id' => '1',
  3895. 'title' => 'Third Post',
  3896. 'body' => 'Third Post Body',
  3897. 'published' => 'Y',
  3898. 'created' => '2007-03-18 10:43:23',
  3899. 'updated' => '2007-03-18 10:45:31'
  3900. )),
  3901. array(
  3902. 'Post' => array(
  3903. 'id' => '4',
  3904. 'author_id' => '2',
  3905. 'title' => 'Creating a fourth post',
  3906. 'body' => 'Fourth post body',
  3907. 'published' => 'N'
  3908. )));
  3909. $this->assertTrue($result[0]['Post']['updated'] >= $ts);
  3910. $this->assertTrue($result[1]['Post']['updated'] >= $ts);
  3911. $this->assertTrue($result[3]['Post']['created'] >= $ts);
  3912. $this->assertTrue($result[3]['Post']['updated'] >= $ts);
  3913. unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
  3914. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  3915. $this->assertEquals($expected, $result);
  3916. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  3917. $data = array(
  3918. array(
  3919. 'id' => '1',
  3920. 'title' => 'Un-Baleeted First Post',
  3921. 'body' => 'Not Baleeted!',
  3922. 'published' => 'Y'
  3923. ),
  3924. array(
  3925. 'id' => '2',
  3926. 'title' => '',
  3927. 'body' => 'Trying to get away with an empty title'
  3928. ));
  3929. $result = $TestModel->saveAll($data);
  3930. $this->assertFalse($result);
  3931. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3932. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  3933. $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
  3934. if (!$transactionWorked) {
  3935. $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
  3936. $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
  3937. }
  3938. $this->assertEquals($TestModel->validationErrors, $errors);
  3939. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  3940. $data = array(
  3941. array(
  3942. 'id' => '1',
  3943. 'title' => 'Un-Baleeted First Post',
  3944. 'body' => 'Not Baleeted!',
  3945. 'published' => 'Y'
  3946. ),
  3947. array(
  3948. 'id' => '2',
  3949. 'title' => '',
  3950. 'body' => 'Trying to get away with an empty title'
  3951. ));
  3952. $result = $TestModel->saveAll($data, array('validate' => true, 'atomic' => false));
  3953. $this->assertEquals(array(true, false), $result);
  3954. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3955. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  3956. $expected = array(
  3957. array(
  3958. 'Post' => array(
  3959. 'id' => '1',
  3960. 'author_id' => '1',
  3961. 'title' => 'Un-Baleeted First Post',
  3962. 'body' => 'Not Baleeted!',
  3963. 'published' => 'Y',
  3964. 'created' => '2007-03-18 10:39:23'
  3965. )
  3966. ),
  3967. array(
  3968. 'Post' => array(
  3969. 'id' => '2',
  3970. 'author_id' => '3',
  3971. 'title' => 'Just update the title',
  3972. 'body' => 'Second Post Body',
  3973. 'published' => 'Y',
  3974. 'created' => '2007-03-18 10:41:23'
  3975. )
  3976. ),
  3977. array(
  3978. 'Post' => array(
  3979. 'id' => '3',
  3980. 'author_id' => '1',
  3981. 'title' => 'Third Post',
  3982. 'body' => 'Third Post Body',
  3983. 'published' => 'Y',
  3984. 'created' => '2007-03-18 10:43:23',
  3985. 'updated' => '2007-03-18 10:45:31'
  3986. )
  3987. ),
  3988. array(
  3989. 'Post' => array(
  3990. 'id' => '4',
  3991. 'author_id' => '2',
  3992. 'title' => 'Creating a fourth post',
  3993. 'body' => 'Fourth post body',
  3994. 'published' => 'N'
  3995. )
  3996. )
  3997. );
  3998. $this->assertTrue($result[0]['Post']['updated'] >= $ts);
  3999. $this->assertTrue($result[1]['Post']['updated'] >= $ts);
  4000. $this->assertTrue($result[3]['Post']['updated'] >= $ts);
  4001. $this->assertTrue($result[3]['Post']['created'] >= $ts);
  4002. unset(
  4003. $result[0]['Post']['updated'], $result[1]['Post']['updated'],
  4004. $result[3]['Post']['updated'], $result[3]['Post']['created']
  4005. );
  4006. $this->assertEquals($expected, $result);
  4007. $this->assertEquals($TestModel->validationErrors, $errors);
  4008. $data = array(
  4009. array(
  4010. 'id' => '1',
  4011. 'title' => 'Re-Baleeted First Post',
  4012. 'body' => 'Baleeted!',
  4013. 'published' => 'N'
  4014. ),
  4015. array(
  4016. 'id' => '2',
  4017. 'title' => '',
  4018. 'body' => 'Trying to get away with an empty title'
  4019. ));
  4020. $this->assertFalse($TestModel->saveAll($data, array('validate' => 'first')));
  4021. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  4022. unset(
  4023. $result[0]['Post']['updated'], $result[1]['Post']['updated'],
  4024. $result[3]['Post']['updated'], $result[3]['Post']['created']
  4025. );
  4026. $this->assertEquals($expected, $result);
  4027. $this->assertEquals($TestModel->validationErrors, $errors);
  4028. }
  4029. /**
  4030. * testSaveAllValidationOnly method
  4031. *
  4032. * @return void
  4033. */
  4034. public function testSaveAllValidationOnly() {
  4035. $this->loadFixtures('Comment', 'Attachment');
  4036. $TestModel = new Comment();
  4037. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  4038. $data = array(
  4039. 'Comment' => array(
  4040. 'comment' => 'This is the comment'
  4041. ),
  4042. 'Attachment' => array(
  4043. 'attachment' => ''
  4044. )
  4045. );
  4046. $result = $TestModel->saveAll($data, array('validate' => 'only'));
  4047. $this->assertFalse($result);
  4048. $TestModel = new Article();
  4049. $TestModel->validate = array('title' => 'notEmpty');
  4050. $result = $TestModel->saveAll(
  4051. array(
  4052. 0 => array('title' => ''),
  4053. 1 => array('title' => 'title 1'),
  4054. 2 => array('title' => 'title 2'),
  4055. ),
  4056. array('validate' => 'only')
  4057. );
  4058. $this->assertFalse($result);
  4059. $expected = array(
  4060. 0 => array('title' => array('This field cannot be left blank')),
  4061. );
  4062. $this->assertEquals($expected, $TestModel->validationErrors);
  4063. $result = $TestModel->saveAll(
  4064. array(
  4065. 0 => array('title' => 'title 0'),
  4066. 1 => array('title' => ''),
  4067. 2 => array('title' => 'title 2'),
  4068. ),
  4069. array('validate' => 'only')
  4070. );
  4071. $this->assertFalse($result);
  4072. $expected = array(
  4073. 1 => array('title' => array('This field cannot be left blank')),
  4074. );
  4075. $this->assertEquals($expected, $TestModel->validationErrors);
  4076. }
  4077. /**
  4078. * testSaveAllValidateFirst method
  4079. *
  4080. * @return void
  4081. */
  4082. public function testSaveAllValidateFirst() {
  4083. $this->loadFixtures('Article', 'Comment', 'Attachment');
  4084. $model = new Article();
  4085. $model->deleteAll(true);
  4086. $model->Comment->validate = array('comment' => 'notEmpty');
  4087. $result = $model->saveAll(array(
  4088. 'Article' => array(
  4089. 'title' => 'Post with Author',
  4090. 'body' => 'This post will be saved author'
  4091. ),
  4092. 'Comment' => array(
  4093. array('comment' => 'First new comment'),
  4094. array('comment' => '')
  4095. )
  4096. ), array('validate' => 'first'));
  4097. $this->assertFalse($result);
  4098. $result = $model->find('all');
  4099. $this->assertEquals(array(), $result);
  4100. $expected = array('Comment' => array(
  4101. 1 => array('comment' => array('This field cannot be left blank'))
  4102. ));
  4103. $this->assertEquals($expected['Comment'], $model->Comment->validationErrors);
  4104. $this->assertSame($model->Comment->find('count'), 0);
  4105. $result = $model->saveAll(
  4106. array(
  4107. 'Article' => array(
  4108. 'title' => 'Post with Author',
  4109. 'body' => 'This post will be saved with an author',
  4110. 'user_id' => 2
  4111. ),
  4112. 'Comment' => array(
  4113. array(
  4114. 'comment' => 'Only new comment',
  4115. 'user_id' => 2
  4116. ))),
  4117. array('validate' => 'first')
  4118. );
  4119. $this->assertSame($result, true);
  4120. $result = $model->Comment->find('all');
  4121. $this->assertSame(count($result), 1);
  4122. $result = Set::extract('/Comment/article_id', $result);
  4123. $this->assertEquals(4, $result[0]);
  4124. $model->deleteAll(true);
  4125. $data = array(
  4126. 'Article' => array(
  4127. 'title' => 'Post with Author saveAlled from comment',
  4128. 'body' => 'This post will be saved with an author',
  4129. 'user_id' => 2
  4130. ),
  4131. 'Comment' => array(
  4132. 'comment' => 'Only new comment', 'user_id' => 2
  4133. ));
  4134. $result = $model->Comment->saveAll($data, array('validate' => 'first'));
  4135. $this->assertFalse(empty($result));
  4136. $result = $model->find('all');
  4137. $this->assertEquals(
  4138. $result[0]['Article']['title'],
  4139. 'Post with Author saveAlled from comment'
  4140. );
  4141. $this->assertEquals('Only new comment', $result[0]['Comment'][0]['comment']);
  4142. }
  4143. /**
  4144. * test saveAll()'s return is correct when using atomic = false and validate = first.
  4145. *
  4146. * @return void
  4147. */
  4148. public function testSaveAllValidateFirstAtomicFalse() {
  4149. $Something = new Something();
  4150. $invalidData = array(
  4151. array(
  4152. 'title' => 'foo',
  4153. 'body' => 'bar',
  4154. 'published' => 'baz',
  4155. ),
  4156. array(
  4157. 'body' => 3,
  4158. 'published' => 'sd',
  4159. ),
  4160. );
  4161. $Something->create();
  4162. $Something->validate = array(
  4163. 'title' => array(
  4164. 'rule' => 'alphaNumeric',
  4165. 'required' => true,
  4166. ),
  4167. 'body' => array(
  4168. 'rule' => 'alphaNumeric',
  4169. 'required' => true,
  4170. 'allowEmpty' => true,
  4171. ),
  4172. );
  4173. $result = $Something->saveAll($invalidData, array(
  4174. 'atomic' => false,
  4175. 'validate' => 'first',
  4176. ));
  4177. $expected = array(true, false);
  4178. $this->assertEquals($expected, $result);
  4179. $Something = new Something();
  4180. $validData = array(
  4181. array(
  4182. 'title' => 'title value',
  4183. 'body' => 'body value',
  4184. 'published' => 'baz',
  4185. ),
  4186. array(
  4187. 'title' => 'valid',
  4188. 'body' => 'this body',
  4189. 'published' => 'sd',
  4190. ),
  4191. );
  4192. $Something->create();
  4193. $result = $Something->saveAll($validData, array(
  4194. 'atomic' => false,
  4195. 'validate' => 'first',
  4196. ));
  4197. $expected = array(true, true);
  4198. $this->assertEquals($expected, $result);
  4199. }
  4200. /**
  4201. * testSaveAllHasManyValidationOnly method
  4202. *
  4203. * @return void
  4204. */
  4205. public function testSaveAllHasManyValidationOnly() {
  4206. $this->loadFixtures('Article', 'Comment', 'Attachment');
  4207. $TestModel = new Article();
  4208. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4209. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  4210. $result = $TestModel->saveAll(
  4211. array(
  4212. 'Article' => array('id' => 2),
  4213. 'Comment' => array(
  4214. array(
  4215. 'id' => 1,
  4216. 'comment' => '',
  4217. 'published' => 'Y',
  4218. 'user_id' => 1),
  4219. array(
  4220. 'id' => 2,
  4221. 'comment' =>
  4222. 'comment',
  4223. 'published' => 'Y',
  4224. 'user_id' => 1
  4225. ))),
  4226. array('validate' => 'only')
  4227. );
  4228. $this->assertFalse($result);
  4229. $result = $TestModel->saveAll(
  4230. array(
  4231. 'Article' => array('id' => 2),
  4232. 'Comment' => array(
  4233. array(
  4234. 'id' => 1,
  4235. 'comment' => '',
  4236. 'published' => 'Y',
  4237. 'user_id' => 1
  4238. ),
  4239. array(
  4240. 'id' => 2,
  4241. 'comment' => 'comment',
  4242. 'published' => 'Y',
  4243. 'user_id' => 1
  4244. ),
  4245. array(
  4246. 'id' => 3,
  4247. 'comment' => '',
  4248. 'published' => 'Y',
  4249. 'user_id' => 1
  4250. ))),
  4251. array(
  4252. 'validate' => 'only',
  4253. 'atomic' => false
  4254. ));
  4255. $expected = array(
  4256. 'Article' => true,
  4257. 'Comment' => array(false, true, false)
  4258. );
  4259. $this->assertSame($expected, $result);
  4260. $expected = array('Comment' => array(
  4261. 0 => array('comment' => array('This field cannot be left blank')),
  4262. 2 => array('comment' => array('This field cannot be left blank'))
  4263. ));
  4264. $this->assertEquals($expected, $TestModel->validationErrors);
  4265. $expected = array(
  4266. 0 => array('comment' => array('This field cannot be left blank')),
  4267. 2 => array('comment' => array('This field cannot be left blank'))
  4268. );
  4269. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  4270. }
  4271. /**
  4272. * test that saveAll behaves like plain save() when supplied empty data
  4273. *
  4274. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  4275. * @return void
  4276. */
  4277. public function testSaveAllEmptyData() {
  4278. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  4279. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  4280. $model = new Article();
  4281. $result = $model->saveAll(array(), array('validate' => 'first'));
  4282. $this->assertFalse(empty($result));
  4283. $model = new ProductUpdateAll();
  4284. $result = $model->saveAll(array());
  4285. $this->assertFalse($result);
  4286. }
  4287. /**
  4288. * testSaveAssociated method
  4289. *
  4290. * @return void
  4291. */
  4292. public function testSaveAssociated() {
  4293. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  4294. $TestModel = new Post();
  4295. $result = $TestModel->find('all');
  4296. $this->assertEquals(3, count($result));
  4297. $this->assertFalse(isset($result[3]));
  4298. $ts = date('Y-m-d H:i:s');
  4299. $TestModel->saveAssociated(array(
  4300. 'Post' => array(
  4301. 'title' => 'Post with Author',
  4302. 'body' => 'This post will be saved with an author'
  4303. ),
  4304. 'Author' => array(
  4305. 'user' => 'bob',
  4306. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
  4307. )));
  4308. $result = $TestModel->find('all');
  4309. $expected = array(
  4310. 'Post' => array(
  4311. 'id' => '4',
  4312. 'author_id' => '5',
  4313. 'title' => 'Post with Author',
  4314. 'body' => 'This post will be saved with an author',
  4315. 'published' => 'N'
  4316. ),
  4317. 'Author' => array(
  4318. 'id' => '5',
  4319. 'user' => 'bob',
  4320. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
  4321. 'test' => 'working'
  4322. ));
  4323. $this->assertTrue($result[3]['Post']['updated'] >= $ts);
  4324. $this->assertTrue($result[3]['Post']['created'] >= $ts);
  4325. $this->assertTrue($result[3]['Author']['created'] >= $ts);
  4326. $this->assertTrue($result[3]['Author']['updated'] >= $ts);
  4327. unset(
  4328. $result[3]['Post']['updated'], $result[3]['Post']['created'],
  4329. $result[3]['Author']['updated'], $result[3]['Author']['created']
  4330. );
  4331. $this->assertEquals($expected, $result[3]);
  4332. $this->assertEquals(4, count($result));
  4333. $ts = date('Y-m-d H:i:s');
  4334. $TestModel = new Comment();
  4335. $ts = date('Y-m-d H:i:s');
  4336. $result = $TestModel->saveAssociated(array(
  4337. 'Comment' => array(
  4338. 'article_id' => 2,
  4339. 'user_id' => 2,
  4340. 'comment' => 'New comment with attachment',
  4341. 'published' => 'Y'
  4342. ),
  4343. 'Attachment' => array(
  4344. 'attachment' => 'some_file.tgz'
  4345. )));
  4346. $this->assertFalse(empty($result));
  4347. $result = $TestModel->find('all');
  4348. $expected = array(
  4349. 'id' => '7',
  4350. 'article_id' => '2',
  4351. 'user_id' => '2',
  4352. 'comment' => 'New comment with attachment',
  4353. 'published' => 'Y'
  4354. );
  4355. $this->assertTrue($result[6]['Comment']['updated'] >= $ts);
  4356. $this->assertTrue($result[6]['Comment']['created'] >= $ts);
  4357. unset($result[6]['Comment']['updated'], $result[6]['Comment']['created']);
  4358. $this->assertEquals($expected, $result[6]['Comment']);
  4359. $expected = array(
  4360. 'id' => '2',
  4361. 'comment_id' => '7',
  4362. 'attachment' => 'some_file.tgz'
  4363. );
  4364. $this->assertTrue($result[6]['Attachment']['updated'] >= $ts);
  4365. $this->assertTrue($result[6]['Attachment']['created'] >= $ts);
  4366. unset($result[6]['Attachment']['updated'], $result[6]['Attachment']['created']);
  4367. $this->assertEquals($expected, $result[6]['Attachment']);
  4368. }
  4369. /**
  4370. * testSaveMany method
  4371. *
  4372. * @return void
  4373. */
  4374. public function testSaveMany() {
  4375. $this->loadFixtures('Post');
  4376. $TestModel = new Post();
  4377. $TestModel->deleteAll(true);
  4378. $this->assertEquals(array(), $TestModel->find('all'));
  4379. // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
  4380. $this->db->truncate($TestModel);
  4381. $ts = date('Y-m-d H:i:s');
  4382. $TestModel->saveMany(array(
  4383. array(
  4384. 'title' => 'Multi-record post 1',
  4385. 'body' => 'First multi-record post',
  4386. 'author_id' => 2
  4387. ),
  4388. array(
  4389. 'title' => 'Multi-record post 2',
  4390. 'body' => 'Second multi-record post',
  4391. 'author_id' => 2
  4392. )));
  4393. $result = $TestModel->find('all', array(
  4394. 'recursive' => -1,
  4395. 'order' => 'Post.id ASC'
  4396. ));
  4397. $expected = array(
  4398. array(
  4399. 'Post' => array(
  4400. 'id' => '1',
  4401. 'author_id' => '2',
  4402. 'title' => 'Multi-record post 1',
  4403. 'body' => 'First multi-record post',
  4404. 'published' => 'N'
  4405. )
  4406. ),
  4407. array(
  4408. 'Post' => array(
  4409. 'id' => '2',
  4410. 'author_id' => '2',
  4411. 'title' => 'Multi-record post 2',
  4412. 'body' => 'Second multi-record post',
  4413. 'published' => 'N'
  4414. )
  4415. )
  4416. );
  4417. $this->assertTrue($result[0]['Post']['updated'] >= $ts);
  4418. $this->assertTrue($result[0]['Post']['created'] >= $ts);
  4419. $this->assertTrue($result[1]['Post']['updated'] >= $ts);
  4420. $this->assertTrue($result[1]['Post']['created'] >= $ts);
  4421. unset($result[0]['Post']['updated'], $result[0]['Post']['created']);
  4422. unset($result[1]['Post']['updated'], $result[1]['Post']['created']);
  4423. $this->assertEquals($expected, $result);
  4424. }
  4425. /**
  4426. * Test SaveAssociated with Habtm relations
  4427. *
  4428. * @return void
  4429. */
  4430. public function testSaveAssociatedHabtm() {
  4431. $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  4432. $data = array(
  4433. 'Article' => array(
  4434. 'user_id' => 1,
  4435. 'title' => 'Article Has and belongs to Many Tags'
  4436. ),
  4437. 'Tag' => array(
  4438. 'Tag' => array(1, 2)
  4439. ),
  4440. 'Comment' => array(
  4441. array(
  4442. 'comment' => 'Article comment',
  4443. 'user_id' => 1
  4444. )));
  4445. $Article = new Article();
  4446. $result = $Article->saveAssociated($data);
  4447. $this->assertFalse(empty($result));
  4448. $result = $Article->read();
  4449. $this->assertEquals(2, count($result['Tag']));
  4450. $this->assertEquals('tag1', $result['Tag'][0]['tag']);
  4451. $this->assertEquals(1, count($result['Comment']));
  4452. $this->assertEquals(1, count($result['Comment'][0]['comment']));
  4453. }
  4454. /**
  4455. * Test SaveAssociated with Habtm relations and extra join table fields
  4456. *
  4457. * @return void
  4458. */
  4459. public function testSaveAssociatedHabtmWithExtraJoinTableFields() {
  4460. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  4461. $data = array(
  4462. 'Something' => array(
  4463. 'id' => 4,
  4464. 'title' => 'Extra Fields',
  4465. 'body' => 'Extra Fields Body',
  4466. 'published' => '1'
  4467. ),
  4468. 'SomethingElse' => array(
  4469. array('something_else_id' => 1, 'doomed' => '1'),
  4470. array('something_else_id' => 2, 'doomed' => '0'),
  4471. array('something_else_id' => 3, 'doomed' => '1')
  4472. )
  4473. );
  4474. $Something = new Something();
  4475. $result = $Something->saveAssociated($data);
  4476. $this->assertFalse(empty($result));
  4477. $result = $Something->read();
  4478. $this->assertEquals(3, count($result['SomethingElse']));
  4479. $this->assertTrue(Set::matches('/Something[id=4]', $result));
  4480. $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
  4481. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
  4482. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
  4483. $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
  4484. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
  4485. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
  4486. $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
  4487. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
  4488. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
  4489. }
  4490. /**
  4491. * testSaveAssociatedHasOne method
  4492. *
  4493. * @return void
  4494. */
  4495. public function testSaveAssociatedHasOne() {
  4496. $model = new Comment();
  4497. $model->deleteAll(true);
  4498. $this->assertEquals(array(), $model->find('all'));
  4499. $model->Attachment->deleteAll(true);
  4500. $this->assertEquals(array(), $model->Attachment->find('all'));
  4501. $this->assertTrue($model->saveAssociated(array(
  4502. 'Comment' => array(
  4503. 'comment' => 'Comment with attachment',
  4504. 'article_id' => 1,
  4505. 'user_id' => 1
  4506. ),
  4507. 'Attachment' => array(
  4508. 'attachment' => 'some_file.zip'
  4509. ))));
  4510. $result = $model->find('all', array('fields' => array(
  4511. 'Comment.id', 'Comment.comment', 'Attachment.id',
  4512. 'Attachment.comment_id', 'Attachment.attachment'
  4513. )));
  4514. $expected = array(array(
  4515. 'Comment' => array(
  4516. 'id' => '1',
  4517. 'comment' => 'Comment with attachment'
  4518. ),
  4519. 'Attachment' => array(
  4520. 'id' => '1',
  4521. 'comment_id' => '1',
  4522. 'attachment' => 'some_file.zip'
  4523. )));
  4524. $this->assertEquals($expected, $result);
  4525. $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
  4526. $data = array(
  4527. 'Comment' => array(
  4528. 'comment' => 'Comment with attachment',
  4529. 'article_id' => 1,
  4530. 'user_id' => 1
  4531. ),
  4532. 'Attachment' => array(
  4533. 'attachment' => 'some_file.zip'
  4534. ));
  4535. $this->assertTrue($model->saveAssociated($data, array('validate' => 'first')));
  4536. }
  4537. /**
  4538. * testSaveAssociatedBelongsTo method
  4539. *
  4540. * @return void
  4541. */
  4542. public function testSaveAssociatedBelongsTo() {
  4543. $model = new Comment();
  4544. $model->deleteAll(true);
  4545. $this->assertEquals(array(), $model->find('all'));
  4546. $model->Article->deleteAll(true);
  4547. $this->assertEquals(array(), $model->Article->find('all'));
  4548. $this->assertTrue($model->saveAssociated(array(
  4549. 'Comment' => array(
  4550. 'comment' => 'Article comment',
  4551. 'article_id' => 1,
  4552. 'user_id' => 1
  4553. ),
  4554. 'Article' => array(
  4555. 'title' => 'Model Associations 101',
  4556. 'user_id' => 1
  4557. ))));
  4558. $result = $model->find('all', array('fields' => array(
  4559. 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
  4560. )));
  4561. $expected = array(array(
  4562. 'Comment' => array(
  4563. 'id' => '1',
  4564. 'article_id' => '1',
  4565. 'comment' => 'Article comment'
  4566. ),
  4567. 'Article' => array(
  4568. 'id' => '1',
  4569. 'title' => 'Model Associations 101'
  4570. )));
  4571. $this->assertEquals($expected, $result);
  4572. }
  4573. /**
  4574. * testSaveAssociatedHasOneValidation method
  4575. *
  4576. * @return void
  4577. */
  4578. public function testSaveAssociatedHasOneValidation() {
  4579. $model = new Comment();
  4580. $model->deleteAll(true);
  4581. $this->assertEquals(array(), $model->find('all'));
  4582. $model->Attachment->deleteAll(true);
  4583. $this->assertEquals(array(), $model->Attachment->find('all'));
  4584. $model->validate = array('comment' => 'notEmpty');
  4585. $model->Attachment->validate = array('attachment' => 'notEmpty');
  4586. $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
  4587. $this->assertEquals($model->saveAssociated(
  4588. array(
  4589. 'Comment' => array(
  4590. 'comment' => '',
  4591. 'article_id' => 1,
  4592. 'user_id' => 1
  4593. ),
  4594. 'Attachment' => array('attachment' => '')
  4595. )
  4596. ), false);
  4597. $expected = array(
  4598. 'Comment' => array('comment' => array('This field cannot be left blank')),
  4599. 'Attachment' => array('attachment' => array('This field cannot be left blank'))
  4600. );
  4601. $this->assertEquals($expected['Comment'], $model->validationErrors);
  4602. $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
  4603. }
  4604. /**
  4605. * testSaveAssociatedAtomic method
  4606. *
  4607. * @return void
  4608. */
  4609. public function testSaveAssociatedAtomic() {
  4610. $this->loadFixtures('Article', 'User');
  4611. $TestModel = new Article();
  4612. $result = $TestModel->saveAssociated(array(
  4613. 'Article' => array(
  4614. 'title' => 'Post with Author',
  4615. 'body' => 'This post will be saved with an author',
  4616. 'user_id' => 2
  4617. ),
  4618. 'Comment' => array(
  4619. array('comment' => 'First new comment', 'user_id' => 2))
  4620. ), array('atomic' => false));
  4621. $this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
  4622. $result = $TestModel->saveAssociated(array(
  4623. 'Article' => array('id' => 2),
  4624. 'Comment' => array(
  4625. array(
  4626. 'comment' => 'First new comment',
  4627. 'published' => 'Y',
  4628. 'user_id' => 1
  4629. ),
  4630. array(
  4631. 'comment' => 'Second new comment',
  4632. 'published' => 'Y',
  4633. 'user_id' => 2
  4634. ))
  4635. ), array('validate' => true, 'atomic' => false));
  4636. $this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
  4637. }
  4638. /**
  4639. * testSaveManyAtomic method
  4640. *
  4641. * @return void
  4642. */
  4643. public function testSaveManyAtomic() {
  4644. $this->loadFixtures('Article', 'User');
  4645. $TestModel = new Article();
  4646. $result = $TestModel->saveMany(array(
  4647. array(
  4648. 'id' => '1',
  4649. 'title' => 'Baleeted First Post',
  4650. 'body' => 'Baleeted!',
  4651. 'published' => 'N'
  4652. ),
  4653. array(
  4654. 'id' => '2',
  4655. 'title' => 'Just update the title'
  4656. ),
  4657. array(
  4658. 'title' => 'Creating a fourth post',
  4659. 'body' => 'Fourth post body',
  4660. 'user_id' => 2
  4661. )
  4662. ), array('atomic' => false));
  4663. $this->assertSame($result, array(true, true, true));
  4664. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  4665. $result = $TestModel->saveMany(array(
  4666. array(
  4667. 'id' => '1',
  4668. 'title' => 'Un-Baleeted First Post',
  4669. 'body' => 'Not Baleeted!',
  4670. 'published' => 'Y'
  4671. ),
  4672. array(
  4673. 'id' => '2',
  4674. 'title' => '',
  4675. 'body' => 'Trying to get away with an empty title'
  4676. )
  4677. ), array('validate' => true, 'atomic' => false));
  4678. $this->assertSame(array(true, false), $result);
  4679. }
  4680. /**
  4681. * testSaveAssociatedHasMany method
  4682. *
  4683. * @return void
  4684. */
  4685. public function testSaveAssociatedHasMany() {
  4686. $this->loadFixtures('Article', 'Comment');
  4687. $TestModel = new Article();
  4688. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4689. $result = $TestModel->saveAssociated(array(
  4690. 'Article' => array('id' => 2),
  4691. 'Comment' => array(
  4692. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  4693. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  4694. )
  4695. ));
  4696. $this->assertFalse(empty($result));
  4697. $result = $TestModel->findById(2);
  4698. $expected = array(
  4699. 'First Comment for Second Article',
  4700. 'Second Comment for Second Article',
  4701. 'First new comment',
  4702. 'Second new comment'
  4703. );
  4704. $this->assertEquals($expected, Set::extract($result['Comment'], '{n}.comment'));
  4705. $result = $TestModel->saveAssociated(
  4706. array(
  4707. 'Article' => array('id' => 2),
  4708. 'Comment' => array(
  4709. array(
  4710. 'comment' => 'Third new comment',
  4711. 'published' => 'Y',
  4712. 'user_id' => 1
  4713. ))),
  4714. array('atomic' => false)
  4715. );
  4716. $this->assertFalse(empty($result));
  4717. $result = $TestModel->findById(2);
  4718. $expected = array(
  4719. 'First Comment for Second Article',
  4720. 'Second Comment for Second Article',
  4721. 'First new comment',
  4722. 'Second new comment',
  4723. 'Third new comment'
  4724. );
  4725. $this->assertEquals($expected, Set::extract($result['Comment'], '{n}.comment'));
  4726. $TestModel->beforeSaveReturn = false;
  4727. $result = $TestModel->saveAssociated(
  4728. array(
  4729. 'Article' => array('id' => 2),
  4730. 'Comment' => array(
  4731. array(
  4732. 'comment' => 'Fourth new comment',
  4733. 'published' => 'Y',
  4734. 'user_id' => 1
  4735. ))),
  4736. array('atomic' => false)
  4737. );
  4738. $this->assertEquals(array('Article' => false), $result);
  4739. $result = $TestModel->findById(2);
  4740. $expected = array(
  4741. 'First Comment for Second Article',
  4742. 'Second Comment for Second Article',
  4743. 'First new comment',
  4744. 'Second new comment',
  4745. 'Third new comment'
  4746. );
  4747. $this->assertEquals($expected, Set::extract($result['Comment'], '{n}.comment'));
  4748. }
  4749. /**
  4750. * testSaveAssociatedHasManyValidation method
  4751. *
  4752. * @return void
  4753. */
  4754. public function testSaveAssociatedHasManyValidation() {
  4755. $this->loadFixtures('Article', 'Comment');
  4756. $TestModel = new Article();
  4757. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4758. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  4759. $result = $TestModel->saveAssociated(array(
  4760. 'Article' => array('id' => 2),
  4761. 'Comment' => array(
  4762. array('comment' => '', 'published' => 'Y', 'user_id' => 1),
  4763. )
  4764. ), array('validate' => true));
  4765. $this->assertFalse($result);
  4766. $expected = array('Comment' => array(
  4767. array('comment' => array('This field cannot be left blank'))
  4768. ));
  4769. $this->assertEquals($expected, $TestModel->validationErrors);
  4770. $expected = array(
  4771. array('comment' => array('This field cannot be left blank'))
  4772. );
  4773. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  4774. $result = $TestModel->saveAssociated(array(
  4775. 'Article' => array('id' => 2),
  4776. 'Comment' => array(
  4777. array(
  4778. 'comment' => '',
  4779. 'published' => 'Y',
  4780. 'user_id' => 1
  4781. ))
  4782. ), array('validate' => 'first'));
  4783. $this->assertFalse($result);
  4784. }
  4785. /**
  4786. * test saveMany with transactions and ensure there is no missing rollback.
  4787. *
  4788. * @return void
  4789. */
  4790. public function testSaveManyTransactionNoRollback() {
  4791. $this->loadFixtures('Post');
  4792. $this->getMock('DboSource', array('connect', 'rollback', 'describe'), array(), 'MockManyTransactionDboSource');
  4793. $db = ConnectionManager::create('mock_many_transaction', array(
  4794. 'datasource' => 'MockManyTransactionDboSource',
  4795. ));
  4796. $db->expects($this->once())
  4797. ->method('describe')
  4798. ->will($this->returnValue(array()));
  4799. $db->expects($this->once())->method('rollback');
  4800. $Post = new Post('mock_many_transaction');
  4801. $Post->validate = array(
  4802. 'title' => array('rule' => array('notEmpty'))
  4803. );
  4804. $data = array(
  4805. array('author_id' => 1, 'title' => 'New Fourth Post'),
  4806. array('author_id' => 1, 'title' => '')
  4807. );
  4808. $Post->saveMany($data);
  4809. }
  4810. /**
  4811. * test saveAssociated with transactions and ensure there is no missing rollback.
  4812. *
  4813. * @return void
  4814. */
  4815. public function testSaveAssociatedTransactionNoRollback() {
  4816. $testDb = ConnectionManager::getDataSource('test');
  4817. $mock = $this->getMock(
  4818. 'DboSource',
  4819. array('connect', 'rollback', 'describe', 'create', 'begin'),
  4820. array(),
  4821. 'MockAssociatedTransactionDboSource',
  4822. false
  4823. );
  4824. $db = ConnectionManager::create('mock_assoc_transaction', array(
  4825. 'datasource' => 'MockAssociatedTransactionDboSource',
  4826. ));
  4827. $this->mockObjects[] = $db;
  4828. $db->columns = $testDb->columns;
  4829. $db->expects($this->once())->method('rollback');
  4830. $db->expects($this->any())->method('describe')
  4831. ->will($this->returnValue(array(
  4832. 'id' => array('type' => 'integer', 'length' => 11),
  4833. 'title' => array('type' => 'string'),
  4834. 'body' => array('type' => 'text'),
  4835. 'published' => array('type' => 'string')
  4836. )));
  4837. $Post = new Post();
  4838. $Post->useDbConfig = 'mock_assoc_transaction';
  4839. $Post->Author->useDbConfig = 'mock_assoc_transaction';
  4840. $Post->Author->validate = array(
  4841. 'user' => array('rule' => array('notEmpty'))
  4842. );
  4843. $data = array(
  4844. 'Post' => array(
  4845. 'title' => 'New post',
  4846. 'body' => 'Content',
  4847. 'published' => 'Y'
  4848. ),
  4849. 'Author' => array(
  4850. 'user' => '',
  4851. 'password' => "sekret"
  4852. )
  4853. );
  4854. $Post->saveAssociated($data, array('validate' => true, 'atomic' => true));
  4855. }
  4856. /**
  4857. * test saveMany with nested saveMany call.
  4858. *
  4859. * @return void
  4860. */
  4861. public function testSaveManyNestedSaveMany() {
  4862. $this->loadFixtures('Sample');
  4863. $TransactionManyTestModel = new TransactionManyTestModel();
  4864. $data = array(
  4865. array('apple_id' => 1, 'name' => 'sample5'),
  4866. );
  4867. $this->assertTrue($TransactionManyTestModel->saveMany($data, array('atomic' => true)));
  4868. }
  4869. /**
  4870. * testSaveManyTransaction method
  4871. *
  4872. * @return void
  4873. */
  4874. public function testSaveManyTransaction() {
  4875. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  4876. $TestModel = new Post();
  4877. $TestModel->validate = array('title' => 'notEmpty');
  4878. $data = array(
  4879. array('author_id' => 1, 'title' => 'New Fourth Post'),
  4880. array('author_id' => 1, 'title' => 'New Fifth Post'),
  4881. array('author_id' => 1, 'title' => '')
  4882. );
  4883. $ts = date('Y-m-d H:i:s');
  4884. $this->assertFalse($TestModel->saveMany($data));
  4885. $result = $TestModel->find('all', array('recursive' => -1));
  4886. $expected = array(
  4887. array('Post' => array(
  4888. 'id' => '1',
  4889. 'author_id' => 1,
  4890. 'title' => 'First Post',
  4891. 'body' => 'First Post Body',
  4892. 'published' => 'Y',
  4893. 'created' => '2007-03-18 10:39:23',
  4894. 'updated' => '2007-03-18 10:41:31'
  4895. )),
  4896. array('Post' => array(
  4897. 'id' => '2',
  4898. 'author_id' => 3,
  4899. 'title' => 'Second Post',
  4900. 'body' => 'Second Post Body',
  4901. 'published' => 'Y',
  4902. 'created' => '2007-03-18 10:41:23',
  4903. 'updated' => '2007-03-18 10:43:31'
  4904. )),
  4905. array('Post' => array(
  4906. 'id' => '3',
  4907. 'author_id' => 1,
  4908. 'title' => 'Third Post',
  4909. 'body' => 'Third Post Body',
  4910. 'published' => 'Y',
  4911. 'created' => '2007-03-18 10:43:23',
  4912. 'updated' => '2007-03-18 10:45:31'
  4913. )));
  4914. if (count($result) != 3) {
  4915. // Database doesn't support transactions
  4916. $expected[] = array(
  4917. 'Post' => array(
  4918. 'id' => '4',
  4919. 'author_id' => 1,
  4920. 'title' => 'New Fourth Post',
  4921. 'body' => null,
  4922. 'published' => 'N'
  4923. ));
  4924. $expected[] = array(
  4925. 'Post' => array(
  4926. 'id' => '5',
  4927. 'author_id' => 1,
  4928. 'title' => 'New Fifth Post',
  4929. 'body' => null,
  4930. 'published' => 'N',
  4931. ));
  4932. $this->assertTrue($result[3]['Post']['created'] >= $ts);
  4933. $this->assertTrue($result[3]['Post']['updated'] >= $ts);
  4934. $this->assertTrue($result[4]['Post']['created'] >= $ts);
  4935. $this->assertTrue($result[4]['Post']['updated'] >= $ts);
  4936. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  4937. unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
  4938. $this->assertEquals($expected, $result);
  4939. // Skip the rest of the transactional tests
  4940. return;
  4941. }
  4942. $this->assertEquals($expected, $result);
  4943. $data = array(
  4944. array('author_id' => 1, 'title' => 'New Fourth Post'),
  4945. array('author_id' => 1, 'title' => ''),
  4946. array('author_id' => 1, 'title' => 'New Sixth Post')
  4947. );
  4948. $ts = date('Y-m-d H:i:s');
  4949. $this->assertFalse($TestModel->saveMany($data));
  4950. $result = $TestModel->find('all', array('recursive' => -1));
  4951. $expected = array(
  4952. array('Post' => array(
  4953. 'id' => '1',
  4954. 'author_id' => 1,
  4955. 'title' => 'First Post',
  4956. 'body' => 'First Post Body',
  4957. 'published' => 'Y',
  4958. 'created' => '2007-03-18 10:39:23',
  4959. 'updated' => '2007-03-18 10:41:31'
  4960. )),
  4961. array('Post' => array(
  4962. 'id' => '2',
  4963. 'author_id' => 3,
  4964. 'title' => 'Second Post',
  4965. 'body' => 'Second Post Body',
  4966. 'published' => 'Y',
  4967. 'created' => '2007-03-18 10:41:23',
  4968. 'updated' => '2007-03-18 10:43:31'
  4969. )),
  4970. array('Post' => array(
  4971. 'id' => '3',
  4972. 'author_id' => 1,
  4973. 'title' => 'Third Post',
  4974. 'body' => 'Third Post Body',
  4975. 'published' => 'Y',
  4976. 'created' => '2007-03-18 10:43:23',
  4977. 'updated' => '2007-03-18 10:45:31'
  4978. )));
  4979. if (count($result) != 3) {
  4980. // Database doesn't support transactions
  4981. $expected[] = array(
  4982. 'Post' => array(
  4983. 'id' => '4',
  4984. 'author_id' => 1,
  4985. 'title' => 'New Fourth Post',
  4986. 'body' => 'Third Post Body',
  4987. 'published' => 'N'
  4988. ));
  4989. $expected[] = array(
  4990. 'Post' => array(
  4991. 'id' => '5',
  4992. 'author_id' => 1,
  4993. 'title' => 'Third Post',
  4994. 'body' => 'Third Post Body',
  4995. 'published' => 'N'
  4996. ));
  4997. $this->assertTrue($result[3]['Post']['created'] >= $ts);
  4998. $this->assertTrue($result[3]['Post']['updated'] >= $ts);
  4999. $this->assertTrue($result[4]['Post']['created'] >= $ts);
  5000. $this->assertTrue($result[4]['Post']['updated'] >= $ts);
  5001. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  5002. unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
  5003. }
  5004. $this->assertEquals($expected, $result);
  5005. $TestModel->validate = array('title' => 'notEmpty');
  5006. $data = array(
  5007. array('author_id' => 1, 'title' => 'New Fourth Post'),
  5008. array('author_id' => 1, 'title' => 'New Fifth Post'),
  5009. array('author_id' => 1, 'title' => 'New Sixth Post')
  5010. );
  5011. $this->assertTrue($TestModel->saveMany($data));
  5012. $result = $TestModel->find('all', array(
  5013. 'recursive' => -1,
  5014. 'fields' => array('author_id', 'title','body','published'),
  5015. 'order' => array('Post.created' => 'ASC')
  5016. ));
  5017. $expected = array(
  5018. array('Post' => array(
  5019. 'author_id' => 1,
  5020. 'title' => 'First Post',
  5021. 'body' => 'First Post Body',
  5022. 'published' => 'Y'
  5023. )),
  5024. array('Post' => array(
  5025. 'author_id' => 3,
  5026. 'title' => 'Second Post',
  5027. 'body' => 'Second Post Body',
  5028. 'published' => 'Y'
  5029. )),
  5030. array('Post' => array(
  5031. 'author_id' => 1,
  5032. 'title' => 'Third Post',
  5033. 'body' => 'Third Post Body',
  5034. 'published' => 'Y'
  5035. )),
  5036. array('Post' => array(
  5037. 'author_id' => 1,
  5038. 'title' => 'New Fourth Post',
  5039. 'body' => '',
  5040. 'published' => 'N'
  5041. )),
  5042. array('Post' => array(
  5043. 'author_id' => 1,
  5044. 'title' => 'New Fifth Post',
  5045. 'body' => '',
  5046. 'published' => 'N'
  5047. )),
  5048. array('Post' => array(
  5049. 'author_id' => 1,
  5050. 'title' => 'New Sixth Post',
  5051. 'body' => '',
  5052. 'published' => 'N'
  5053. )));
  5054. $this->assertEquals($expected, $result);
  5055. }
  5056. /**
  5057. * testSaveManyValidation method
  5058. *
  5059. * @return void
  5060. */
  5061. public function testSaveManyValidation() {
  5062. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  5063. $TestModel = new Post();
  5064. $ts = date('Y-m-d H:i:s');
  5065. $data = array(
  5066. array(
  5067. 'id' => '1',
  5068. 'title' => 'Baleeted First Post',
  5069. 'body' => 'Baleeted!',
  5070. 'published' => 'N'
  5071. ),
  5072. array(
  5073. 'id' => '2',
  5074. 'title' => 'Just update the title'
  5075. ),
  5076. array(
  5077. 'title' => 'Creating a fourth post',
  5078. 'body' => 'Fourth post body',
  5079. 'author_id' => 2
  5080. ));
  5081. $this->assertTrue($TestModel->saveMany($data));
  5082. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  5083. $expected = array(
  5084. array(
  5085. 'Post' => array(
  5086. 'id' => '1',
  5087. 'author_id' => '1',
  5088. 'title' => 'Baleeted First Post',
  5089. 'body' => 'Baleeted!',
  5090. 'published' => 'N',
  5091. 'created' => '2007-03-18 10:39:23'
  5092. )
  5093. ),
  5094. array(
  5095. 'Post' => array(
  5096. 'id' => '2',
  5097. 'author_id' => '3',
  5098. 'title' => 'Just update the title',
  5099. 'body' => 'Second Post Body',
  5100. 'published' => 'Y',
  5101. 'created' => '2007-03-18 10:41:23'
  5102. )
  5103. ),
  5104. array(
  5105. 'Post' => array(
  5106. 'id' => '3',
  5107. 'author_id' => '1',
  5108. 'title' => 'Third Post',
  5109. 'body' => 'Third Post Body',
  5110. 'published' => 'Y',
  5111. 'created' => '2007-03-18 10:43:23',
  5112. 'updated' => '2007-03-18 10:45:31'
  5113. )),
  5114. array(
  5115. 'Post' => array(
  5116. 'id' => '4',
  5117. 'author_id' => '2',
  5118. 'title' => 'Creating a fourth post',
  5119. 'body' => 'Fourth post body',
  5120. 'published' => 'N'
  5121. )
  5122. )
  5123. );
  5124. $this->assertTrue($result[0]['Post']['updated'] >= $ts);
  5125. $this->assertTrue($result[1]['Post']['updated'] >= $ts);
  5126. $this->assertTrue($result[3]['Post']['created'] >= $ts);
  5127. $this->assertTrue($result[3]['Post']['updated'] >= $ts);
  5128. unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
  5129. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  5130. $this->assertEquals($expected, $result);
  5131. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  5132. $data = array(
  5133. array(
  5134. 'id' => '1',
  5135. 'title' => 'Un-Baleeted First Post',
  5136. 'body' => 'Not Baleeted!',
  5137. 'published' => 'Y'
  5138. ),
  5139. array(
  5140. 'id' => '2',
  5141. 'title' => '',
  5142. 'body' => 'Trying to get away with an empty title'
  5143. ));
  5144. $result = $TestModel->saveMany($data);
  5145. $this->assertFalse($result);
  5146. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  5147. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  5148. $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
  5149. if (!$transactionWorked) {
  5150. $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
  5151. $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
  5152. }
  5153. $this->assertEquals($TestModel->validationErrors, $errors);
  5154. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  5155. $data = array(
  5156. array(
  5157. 'id' => '1',
  5158. 'title' => 'Un-Baleeted First Post',
  5159. 'body' => 'Not Baleeted!',
  5160. 'published' => 'Y'
  5161. ),
  5162. array(
  5163. 'id' => '2',
  5164. 'title' => '',
  5165. 'body' => 'Trying to get away with an empty title'
  5166. ));
  5167. $result = $TestModel->saveMany($data, array('validate' => true, 'atomic' => false));
  5168. $this->assertEquals(array(true, false), $result);
  5169. $result = $TestModel->find('all', array(
  5170. 'fields' => array('id', 'author_id', 'title', 'body', 'published'),
  5171. 'recursive' => -1,
  5172. 'order' => 'Post.id ASC'
  5173. ));
  5174. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  5175. $expected = array(
  5176. array(
  5177. 'Post' => array(
  5178. 'id' => '1',
  5179. 'author_id' => '1',
  5180. 'title' => 'Un-Baleeted First Post',
  5181. 'body' => 'Not Baleeted!',
  5182. 'published' => 'Y',
  5183. )),
  5184. array(
  5185. 'Post' => array(
  5186. 'id' => '2',
  5187. 'author_id' => '3',
  5188. 'title' => 'Just update the title',
  5189. 'body' => 'Second Post Body',
  5190. 'published' => 'Y',
  5191. )),
  5192. array(
  5193. 'Post' => array(
  5194. 'id' => '3',
  5195. 'author_id' => '1',
  5196. 'title' => 'Third Post',
  5197. 'body' => 'Third Post Body',
  5198. 'published' => 'Y',
  5199. )),
  5200. array(
  5201. 'Post' => array(
  5202. 'id' => '4',
  5203. 'author_id' => '2',
  5204. 'title' => 'Creating a fourth post',
  5205. 'body' => 'Fourth post body',
  5206. 'published' => 'N',
  5207. )));
  5208. $this->assertEquals($expected, $result);
  5209. $this->assertEquals($TestModel->validationErrors, $errors);
  5210. $data = array(
  5211. array(
  5212. 'id' => '1',
  5213. 'title' => 'Re-Baleeted First Post',
  5214. 'body' => 'Baleeted!',
  5215. 'published' => 'N'
  5216. ),
  5217. array(
  5218. 'id' => '2',
  5219. 'title' => '',
  5220. 'body' => 'Trying to get away with an empty title'
  5221. ));
  5222. $this->assertFalse($TestModel->saveMany($data, array('validate' => 'first')));
  5223. $result = $TestModel->find('all', array(
  5224. 'fields' => array('id', 'author_id', 'title', 'body', 'published'),
  5225. 'recursive' => -1,
  5226. 'order' => 'Post.id ASC'
  5227. ));
  5228. $this->assertEquals($expected, $result);
  5229. $this->assertEquals($TestModel->validationErrors, $errors);
  5230. }
  5231. /**
  5232. * testValidateMany method
  5233. *
  5234. * @return void
  5235. */
  5236. public function testValidateMany() {
  5237. $TestModel = new Article();
  5238. $TestModel->validate = array('title' => 'notEmpty');
  5239. $result = $TestModel->validateMany(
  5240. array(
  5241. 0 => array('title' => ''),
  5242. 1 => array('title' => 'title 1'),
  5243. 2 => array('title' => 'title 2'),
  5244. ));
  5245. $this->assertFalse($result);
  5246. $expected = array(
  5247. 0 => array('title' => array('This field cannot be left blank')),
  5248. );
  5249. $this->assertEquals($expected, $TestModel->validationErrors);
  5250. $result = $TestModel->validateMany(
  5251. array(
  5252. 0 => array('title' => 'title 0'),
  5253. 1 => array('title' => ''),
  5254. 2 => array('title' => 'title 2'),
  5255. ));
  5256. $this->assertFalse($result);
  5257. $expected = array(
  5258. 1 => array('title' => array('This field cannot be left blank')),
  5259. );
  5260. $this->assertEquals($expected, $TestModel->validationErrors);
  5261. }
  5262. /**
  5263. * testSaveAssociatedValidateFirst method
  5264. *
  5265. * @return void
  5266. */
  5267. public function testSaveAssociatedValidateFirst() {
  5268. $this->loadFixtures('Article', 'Comment', 'Attachment');
  5269. $model = new Article();
  5270. $model->deleteAll(true);
  5271. $model->Comment->validate = array('comment' => 'notEmpty');
  5272. $result = $model->saveAssociated(array(
  5273. 'Article' => array(
  5274. 'title' => 'Post with Author',
  5275. 'body' => 'This post will be saved author'
  5276. ),
  5277. 'Comment' => array(
  5278. array('comment' => 'First new comment'),
  5279. array('comment' => '')
  5280. )
  5281. ), array('validate' => 'first'));
  5282. $this->assertFalse($result);
  5283. $result = $model->find('all');
  5284. $this->assertEquals(array(), $result);
  5285. $expected = array('Comment' => array(
  5286. 1 => array('comment' => array('This field cannot be left blank'))
  5287. ));
  5288. $this->assertEquals($expected['Comment'], $model->Comment->validationErrors);
  5289. $this->assertSame($model->Comment->find('count'), 0);
  5290. $result = $model->saveAssociated(
  5291. array(
  5292. 'Article' => array(
  5293. 'title' => 'Post with Author',
  5294. 'body' => 'This post will be saved with an author',
  5295. 'user_id' => 2
  5296. ),
  5297. 'Comment' => array(
  5298. array(
  5299. 'comment' => 'Only new comment',
  5300. 'user_id' => 2
  5301. ))),
  5302. array('validate' => 'first')
  5303. );
  5304. $this->assertSame($result, true);
  5305. $result = $model->Comment->find('all');
  5306. $this->assertSame(count($result), 1);
  5307. $result = Set::extract('/Comment/article_id', $result);
  5308. $this->assertEquals(4, $result[0]);
  5309. $model->deleteAll(true);
  5310. $data = array(
  5311. 'Article' => array(
  5312. 'title' => 'Post with Author saveAlled from comment',
  5313. 'body' => 'This post will be saved with an author',
  5314. 'user_id' => 2
  5315. ),
  5316. 'Comment' => array(
  5317. 'comment' => 'Only new comment', 'user_id' => 2
  5318. ));
  5319. $result = $model->Comment->saveAssociated($data, array('validate' => 'first'));
  5320. $this->assertFalse(empty($result));
  5321. $result = $model->find('all');
  5322. $this->assertEquals(
  5323. $result[0]['Article']['title'],
  5324. 'Post with Author saveAlled from comment'
  5325. );
  5326. $this->assertEquals('Only new comment', $result[0]['Comment'][0]['comment']);
  5327. }
  5328. /**
  5329. * test saveMany()'s return is correct when using atomic = false and validate = first.
  5330. *
  5331. * @return void
  5332. */
  5333. public function testSaveManyValidateFirstAtomicFalse() {
  5334. $Something = new Something();
  5335. $invalidData = array(
  5336. array(
  5337. 'title' => 'foo',
  5338. 'body' => 'bar',
  5339. 'published' => 'baz',
  5340. ),
  5341. array(
  5342. 'body' => 3,
  5343. 'published' => 'sd',
  5344. ),
  5345. );
  5346. $Something->create();
  5347. $Something->validate = array(
  5348. 'title' => array(
  5349. 'rule' => 'alphaNumeric',
  5350. 'required' => true,
  5351. ),
  5352. 'body' => array(
  5353. 'rule' => 'alphaNumeric',
  5354. 'required' => true,
  5355. 'allowEmpty' => true,
  5356. ),
  5357. );
  5358. $result = $Something->saveMany($invalidData, array(
  5359. 'atomic' => false,
  5360. 'validate' => 'first',
  5361. ));
  5362. $expected = array(true, false);
  5363. $this->assertEquals($expected, $result);
  5364. $Something = new Something();
  5365. $validData = array(
  5366. array(
  5367. 'title' => 'title value',
  5368. 'body' => 'body value',
  5369. 'published' => 'baz',
  5370. ),
  5371. array(
  5372. 'title' => 'valid',
  5373. 'body' => 'this body',
  5374. 'published' => 'sd',
  5375. ),
  5376. );
  5377. $Something->create();
  5378. $result = $Something->saveMany($validData, array(
  5379. 'atomic' => false,
  5380. 'validate' => 'first',
  5381. ));
  5382. $expected = array(true, true);
  5383. $this->assertEquals($expected, $result);
  5384. }
  5385. /**
  5386. * testValidateAssociated method
  5387. *
  5388. * @return void
  5389. */
  5390. public function testValidateAssociated() {
  5391. $TestModel = new Comment();
  5392. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  5393. $data = array(
  5394. 'Comment' => array(
  5395. 'comment' => 'This is the comment'
  5396. ),
  5397. 'Attachment' => array(
  5398. 'attachment' => ''
  5399. )
  5400. );
  5401. $result = $TestModel->validateAssociated($data);
  5402. $this->assertFalse($result);
  5403. $TestModel = new Article();
  5404. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  5405. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  5406. $result = $TestModel->validateAssociated(
  5407. array(
  5408. 'Article' => array('id' => 2),
  5409. 'Comment' => array(
  5410. array(
  5411. 'id' => 1,
  5412. 'comment' => '',
  5413. 'published' => 'Y',
  5414. 'user_id' => 1),
  5415. array(
  5416. 'id' => 2,
  5417. 'comment' =>
  5418. 'comment',
  5419. 'published' => 'Y',
  5420. 'user_id' => 1
  5421. ))));
  5422. $this->assertFalse($result);
  5423. $result = $TestModel->validateAssociated(
  5424. array(
  5425. 'Article' => array('id' => 2),
  5426. 'Comment' => array(
  5427. array(
  5428. 'id' => 1,
  5429. 'comment' => '',
  5430. 'published' => 'Y',
  5431. 'user_id' => 1
  5432. ),
  5433. array(
  5434. 'id' => 2,
  5435. 'comment' => 'comment',
  5436. 'published' => 'Y',
  5437. 'user_id' => 1
  5438. ),
  5439. array(
  5440. 'id' => 3,
  5441. 'comment' => '',
  5442. 'published' => 'Y',
  5443. 'user_id' => 1
  5444. ))),
  5445. array(
  5446. 'atomic' => false
  5447. ));
  5448. $expected = array(
  5449. 'Article' => true,
  5450. 'Comment' => array(false, true, false)
  5451. );
  5452. $this->assertSame($expected, $result);
  5453. $expected = array('Comment' => array(
  5454. 0 => array('comment' => array('This field cannot be left blank')),
  5455. 2 => array('comment' => array('This field cannot be left blank'))
  5456. ));
  5457. $this->assertEquals($expected, $TestModel->validationErrors);
  5458. $expected = array(
  5459. 0 => array('comment' => array('This field cannot be left blank')),
  5460. 2 => array('comment' => array('This field cannot be left blank'))
  5461. );
  5462. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  5463. }
  5464. /**
  5465. * test that saveMany behaves like plain save() when suplied empty data
  5466. *
  5467. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  5468. * @return void
  5469. */
  5470. public function testSaveManyEmptyData() {
  5471. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  5472. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  5473. $model = new Article();
  5474. $result = $model->saveMany(array(), array('validate' => true));
  5475. $this->assertFalse(empty($result));
  5476. $model = new ProductUpdateAll();
  5477. $result = $model->saveMany(array());
  5478. $this->assertFalse($result);
  5479. }
  5480. /**
  5481. * test that saveAssociated behaves like plain save() when supplied empty data
  5482. *
  5483. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  5484. * @return void
  5485. */
  5486. public function testSaveAssociatedEmptyData() {
  5487. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  5488. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  5489. $model = new Article();
  5490. $result = $model->saveAssociated(array(), array('validate' => true));
  5491. $this->assertFalse(empty($result));
  5492. $model = new ProductUpdateAll();
  5493. $result = $model->saveAssociated(array());
  5494. $this->assertFalse($result);
  5495. }
  5496. /**
  5497. * testUpdateWithCalculation method
  5498. *
  5499. * @return void
  5500. */
  5501. public function testUpdateWithCalculation() {
  5502. $this->loadFixtures('DataTest');
  5503. $model = new DataTest();
  5504. $model->deleteAll(true);
  5505. $result = $model->saveMany(array(
  5506. array('count' => 5, 'float' => 1.1),
  5507. array('count' => 3, 'float' => 1.2),
  5508. array('count' => 4, 'float' => 1.3),
  5509. array('count' => 1, 'float' => 2.0),
  5510. ));
  5511. $this->assertFalse(empty($result));
  5512. $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count')));
  5513. $this->assertEquals(array(5, 3, 4, 1), $result);
  5514. $this->assertTrue($model->updateAll(array('count' => 'count + 2')));
  5515. $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count')));
  5516. $this->assertEquals(array(7, 5, 6, 3), $result);
  5517. $this->assertTrue($model->updateAll(array('DataTest.count' => 'DataTest.count - 1')));
  5518. $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count')));
  5519. $this->assertEquals(array(6, 4, 5, 2), $result);
  5520. }
  5521. public function testToggleBoolFields() {
  5522. $this->loadFixtures('CounterCachePost');
  5523. $Post = new CounterCachePost();
  5524. $Post->unbindModel(array('belongsTo' => array('User')), true);
  5525. $true = array('Post' => array('published' => true, 'id' => 2));
  5526. $false = array('Post' => array('published' => false, 'id' => 2));
  5527. $fields = array('Post.published', 'Post.id');
  5528. $updateConditions = array('Post.id' => 2);
  5529. // check its true
  5530. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5531. $this->assertEqual($result, $true);
  5532. // Testing without the alias
  5533. $this->assertTrue($Post->updateAll(array('published' => '1 - published'), $updateConditions));
  5534. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5535. $this->assertEqual($result, $false);
  5536. $this->assertTrue($Post->updateAll(array('published' => '1 - published'), $updateConditions));
  5537. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5538. $this->assertEqual($result, $true);
  5539. // Testing with the alias
  5540. $this->assertTrue($Post->updateAll(array('Post.published' => '1 - Post.published'), $updateConditions));
  5541. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5542. $this->assertEqual($result, $false);
  5543. $this->assertTrue($Post->updateAll(array('Post.published' => '1 - Post.published'), $updateConditions));
  5544. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5545. $this->assertEqual($result, $true);
  5546. $this->assertTrue($Post->updateAll(array('Post.published = 1 - Post.published'), $updateConditions));
  5547. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5548. $this->assertEqual($result, $false);
  5549. $this->assertTrue($Post->updateAll(array('Post.published = 1 - Post.published'), $updateConditions));
  5550. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5551. $this->assertEqual($result, $true);
  5552. $this->assertTrue($Post->updateAll(array('Post.published' => '! Post.published'), $updateConditions));
  5553. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5554. $this->assertEqual($result, $false);
  5555. $this->assertTrue($Post->updateAll(array('Post.published' => 'NOT Post.published'), $updateConditions));
  5556. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5557. $this->assertEqual($result, $true);
  5558. }
  5559. /**
  5560. * TestFindAllWithoutForeignKey
  5561. *
  5562. * @return void
  5563. */
  5564. public function testFindAllForeignKey() {
  5565. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  5566. $ProductUpdateAll = new ProductUpdateAll();
  5567. $conditions = array('Group.name' => 'group one');
  5568. $ProductUpdateAll->bindModel(array(
  5569. 'belongsTo' => array(
  5570. 'Group' => array('className' => 'GroupUpdateAll')
  5571. )
  5572. ));
  5573. $ProductUpdateAll->belongsTo = array(
  5574. 'Group' => array('className' => 'GroupUpdateAll', 'foreignKey' => 'group_id')
  5575. );
  5576. $results = $ProductUpdateAll->find('all', compact('conditions'));
  5577. $this->assertTrue(!empty($results));
  5578. $ProductUpdateAll->bindModel(array('belongsTo' => array('Group')));
  5579. $ProductUpdateAll->belongsTo = array(
  5580. 'Group' => array(
  5581. 'className' => 'GroupUpdateAll',
  5582. 'foreignKey' => false,
  5583. 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
  5584. ));
  5585. $resultsFkFalse = $ProductUpdateAll->find('all', compact('conditions'));
  5586. $this->assertTrue(!empty($resultsFkFalse));
  5587. $expected = array(
  5588. '0' => array(
  5589. 'ProductUpdateAll' => array(
  5590. 'id' => 1,
  5591. 'name' => 'product one',
  5592. 'groupcode' => 120,
  5593. 'group_id' => 1),
  5594. 'Group' => array(
  5595. 'id' => 1,
  5596. 'name' => 'group one',
  5597. 'code' => 120)
  5598. ),
  5599. '1' => array(
  5600. 'ProductUpdateAll' => array(
  5601. 'id' => 2,
  5602. 'name' => 'product two',
  5603. 'groupcode' => 120,
  5604. 'group_id' => 1),
  5605. 'Group' => array(
  5606. 'id' => 1,
  5607. 'name' => 'group one',
  5608. 'code' => 120)
  5609. )
  5610. );
  5611. $this->assertEquals($expected, $results);
  5612. $this->assertEquals($expected, $resultsFkFalse);
  5613. }
  5614. /**
  5615. * test updateAll with empty values.
  5616. *
  5617. * @return void
  5618. */
  5619. public function testUpdateAllEmptyValues() {
  5620. $this->skipIf($this->db instanceof Sqlserver || $this->db instanceof Postgres, 'This test is not compatible with Postgres or SQL Server.');
  5621. $this->loadFixtures('Author', 'Post');
  5622. $model = new Author();
  5623. $result = $model->updateAll(array('user' => '""'));
  5624. $this->assertTrue($result);
  5625. }
  5626. /**
  5627. * testUpdateAllWithJoins
  5628. *
  5629. * @return void
  5630. */
  5631. public function testUpdateAllWithJoins() {
  5632. $this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql or sqlite');
  5633. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  5634. $ProductUpdateAll = new ProductUpdateAll();
  5635. $conditions = array('Group.name' => 'group one');
  5636. $ProductUpdateAll->bindModel(array('belongsTo' => array(
  5637. 'Group' => array('className' => 'GroupUpdateAll')))
  5638. );
  5639. $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
  5640. $results = $ProductUpdateAll->find('all', array(
  5641. 'conditions' => array('ProductUpdateAll.name' => 'new product')
  5642. ));
  5643. $expected = array(
  5644. '0' => array(
  5645. 'ProductUpdateAll' => array(
  5646. 'id' => 1,
  5647. 'name' => 'new product',
  5648. 'groupcode' => 120,
  5649. 'group_id' => 1),
  5650. 'Group' => array(
  5651. 'id' => 1,
  5652. 'name' => 'group one',
  5653. 'code' => 120)
  5654. ),
  5655. '1' => array(
  5656. 'ProductUpdateAll' => array(
  5657. 'id' => 2,
  5658. 'name' => 'new product',
  5659. 'groupcode' => 120,
  5660. 'group_id' => 1),
  5661. 'Group' => array(
  5662. 'id' => 1,
  5663. 'name' => 'group one',
  5664. 'code' => 120)));
  5665. $this->assertEquals($expected, $results);
  5666. }
  5667. /**
  5668. * testUpdateAllWithoutForeignKey
  5669. *
  5670. * @return void
  5671. */
  5672. public function testUpdateAllWithoutForeignKey() {
  5673. $this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql');
  5674. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  5675. $ProductUpdateAll = new ProductUpdateAll();
  5676. $conditions = array('Group.name' => 'group one');
  5677. $ProductUpdateAll->bindModel(array('belongsTo' => array(
  5678. 'Group' => array('className' => 'GroupUpdateAll')
  5679. )));
  5680. $ProductUpdateAll->belongsTo = array(
  5681. 'Group' => array(
  5682. 'className' => 'GroupUpdateAll',
  5683. 'foreignKey' => false,
  5684. 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
  5685. )
  5686. );
  5687. $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
  5688. $resultsFkFalse = $ProductUpdateAll->find('all', array('conditions' => array('ProductUpdateAll.name' => 'new product')));
  5689. $expected = array(
  5690. '0' => array(
  5691. 'ProductUpdateAll' => array(
  5692. 'id' => 1,
  5693. 'name' => 'new product',
  5694. 'groupcode' => 120,
  5695. 'group_id' => 1),
  5696. 'Group' => array(
  5697. 'id' => 1,
  5698. 'name' => 'group one',
  5699. 'code' => 120)
  5700. ),
  5701. '1' => array(
  5702. 'ProductUpdateAll' => array(
  5703. 'id' => 2,
  5704. 'name' => 'new product',
  5705. 'groupcode' => 120,
  5706. 'group_id' => 1),
  5707. 'Group' => array(
  5708. 'id' => 1,
  5709. 'name' => 'group one',
  5710. 'code' => 120)));
  5711. $this->assertEquals($expected, $resultsFkFalse);
  5712. }
  5713. /**
  5714. * test writing floats in german locale.
  5715. *
  5716. * @return void
  5717. */
  5718. public function testWriteFloatAsGerman() {
  5719. $restore = setlocale(LC_ALL, 0);
  5720. setlocale(LC_ALL, 'de_DE');
  5721. $model = new DataTest();
  5722. $result = $model->save(array(
  5723. 'count' => 1,
  5724. 'float' => 3.14593
  5725. ));
  5726. $this->assertTrue((bool)$result);
  5727. setlocale(LC_ALL, $restore);
  5728. }
  5729. /**
  5730. * Test returned array contains primary key when save creates a new record
  5731. *
  5732. * @return void
  5733. */
  5734. public function testPkInReturnArrayForCreate() {
  5735. $this->loadFixtures('Article');
  5736. $TestModel = new Article();
  5737. $data = array('Article' => array(
  5738. 'user_id' => '1',
  5739. 'title' => 'Fourth Article',
  5740. 'body' => 'Fourth Article Body',
  5741. 'published' => 'Y'
  5742. ));
  5743. $result = $TestModel->save($data);
  5744. $this->assertSame($result['Article']['id'], $TestModel->id);
  5745. }
  5746. /**
  5747. * testSaveAllFieldListValidateBelongsTo
  5748. *
  5749. * @return void
  5750. */
  5751. public function testSaveAllFieldListValidateBelongsTo() {
  5752. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  5753. $TestModel = new Post();
  5754. $result = $TestModel->find('all');
  5755. $this->assertCount(3, $result);
  5756. $this->assertFalse(isset($result[3]));
  5757. $ts = date('Y-m-d H:i:s');
  5758. // test belongsTo
  5759. $fieldList = array(
  5760. 'Post' => array('title', 'author_id'),
  5761. 'Author' => array('user')
  5762. );
  5763. $TestModel->saveAll(array(
  5764. 'Post' => array(
  5765. 'title' => 'Post without body',
  5766. 'body' => 'This will not be saved',
  5767. ),
  5768. 'Author' => array(
  5769. 'user' => 'bob',
  5770. 'test' => 'This will not be saved',
  5771. )), array('fieldList' => $fieldList));
  5772. $result = $TestModel->find('all');
  5773. $expected = array(
  5774. 'Post' => array (
  5775. 'id' => '4',
  5776. 'author_id' => '5',
  5777. 'title' => 'Post without body',
  5778. 'body' => null,
  5779. 'published' => 'N',
  5780. 'created' => $ts,
  5781. 'updated' => $ts,
  5782. ),
  5783. 'Author' => array (
  5784. 'id' => '5',
  5785. 'user' => 'bob',
  5786. 'password' => null,
  5787. 'created' => $ts,
  5788. 'updated' => $ts,
  5789. 'test' => 'working',
  5790. ),
  5791. );
  5792. $this->assertEquals($expected, $result[3]);
  5793. $this->assertCount(4, $result);
  5794. $this->assertEquals('', $result[3]['Post']['body']);
  5795. $this->assertEquals('working', $result[3]['Author']['test']);
  5796. // test multirecord
  5797. $this->db->truncate($TestModel);
  5798. $ts = date('Y-m-d H:i:s');
  5799. $fieldList = array('title', 'author_id');
  5800. $TestModel->saveAll(array(
  5801. array(
  5802. 'title' => 'Multi-record post 1',
  5803. 'body' => 'First multi-record post',
  5804. 'author_id' => 2
  5805. ),
  5806. array(
  5807. 'title' => 'Multi-record post 2',
  5808. 'body' => 'Second multi-record post',
  5809. 'author_id' => 2
  5810. )), array('fieldList' => $fieldList));
  5811. $result = $TestModel->find('all', array(
  5812. 'recursive' => -1,
  5813. 'order' => 'Post.id ASC'
  5814. ));
  5815. $expected = array(
  5816. array(
  5817. 'Post' => array(
  5818. 'id' => '1',
  5819. 'author_id' => '2',
  5820. 'title' => 'Multi-record post 1',
  5821. 'body' => '',
  5822. 'published' => 'N',
  5823. 'created' => $ts,
  5824. 'updated' => $ts
  5825. )
  5826. ),
  5827. array(
  5828. 'Post' => array(
  5829. 'id' => '2',
  5830. 'author_id' => '2',
  5831. 'title' => 'Multi-record post 2',
  5832. 'body' => '',
  5833. 'published' => 'N',
  5834. 'created' => $ts,
  5835. 'updated' => $ts
  5836. )
  5837. )
  5838. );
  5839. $this->assertEquals($expected, $result);
  5840. }
  5841. /**
  5842. * testSaveAllFieldListHasMany method
  5843. *
  5844. * return @void
  5845. */
  5846. public function testSaveAllFieldListHasMany() {
  5847. $this->loadFixtures('Article', 'Comment');
  5848. $TestModel = new Article();
  5849. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  5850. $this->db->truncate($TestModel);
  5851. $this->db->truncate(new Comment());
  5852. $fieldList = array(
  5853. 'Article' => array('id'),
  5854. 'Comment' => array('article_id', 'user_id')
  5855. );
  5856. $result = $TestModel->saveAll(array(
  5857. 'Article' => array('id' => 2, 'title' => 'I will not save'),
  5858. 'Comment' => array(
  5859. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  5860. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  5861. )
  5862. ), array('fieldList' => $fieldList));
  5863. $result = $TestModel->find('all');
  5864. $this->assertEquals('', $result[0]['Article']['title']);
  5865. $this->assertEquals('', $result[0]['Comment'][0]['comment']);
  5866. $this->assertEquals('', $result[0]['Comment'][1]['comment']);
  5867. }
  5868. /**
  5869. * testSaveAllFieldListHasOne method
  5870. *
  5871. * @return void
  5872. */
  5873. public function testSaveAllFieldListHasOne() {
  5874. $this->loadFixtures('Attachment', 'Comment', 'Article', 'User');
  5875. $TestModel = new Comment();
  5876. $TestModel->validate = array('comment' => 'notEmpty');
  5877. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  5878. $record = array(
  5879. 'Comment' => array(
  5880. 'user_id' => 1,
  5881. 'article_id' => 1,
  5882. 'comment' => '',
  5883. ),
  5884. 'Attachment' => array(
  5885. 'attachment' => ''
  5886. )
  5887. );
  5888. $result = $TestModel->saveAll($record, array('validate' => 'only'));
  5889. $this->assertFalse($result);
  5890. $fieldList = array(
  5891. 'Comment' => array('id', 'article_id', 'user_id'),
  5892. 'Attachment' => array('comment_id')
  5893. );
  5894. $result = $TestModel->saveAll($record, array(
  5895. 'fieldList' => $fieldList, 'validate' => 'only'
  5896. ));
  5897. $this->assertTrue($result);
  5898. $this->assertEmpty($TestModel->validationErrors);
  5899. }
  5900. /**
  5901. * testSaveAllDeepFieldListValidateBelongsTo
  5902. *
  5903. * @return void
  5904. */
  5905. public function testSaveAllDeepFieldListValidateBelongsTo() {
  5906. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  5907. $TestModel = new Post();
  5908. $TestModel->Author->bindModel(array('hasMany' => array('Comment' => array('foreignKey' => 'user_id'))), false);
  5909. $TestModel->recursive = 2;
  5910. $result = $TestModel->find('all');
  5911. $this->assertCount(3, $result);
  5912. $this->assertFalse(isset($result[3]));
  5913. $ts = date('Y-m-d H:i:s');
  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. }