ModelWriteTest.php 192 KB

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