ModelWriteTest.php 188 KB

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