ModelWriteTest.php 176 KB

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