ModelWriteTest.php 178 KB

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