ModelWriteTest.php 187 KB

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