| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071 |
- <?php
- /**
- * ModelWriteTest file
- *
- * PHP 5
- *
- * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
- * @package Cake.Test.Case.Model
- * @since CakePHP(tm) v 1.2.0.4206
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- App::uses('MockTransactionDboSource', 'Model/Datasource');
- App::uses('MockTransactionAssociatedDboSource', 'Model/Datasource');
- App::uses('MockManyTransactionDboSource', 'Model/Datasource');
- App::uses('MockAssociatedTransactionDboSource', 'Model/Datasource');
- require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
- /**
- * ModelWriteTest
- *
- * @package Cake.Test.Case.Model
- */
- class ModelWriteTest extends BaseModelTest {
- /**
- * override locale to the default (eng).
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- Configure::write('Config.language', 'eng');
- }
- /**
- * Test save() failing when there is no data.
- *
- * @return void
- */
- public function testInsertNoData() {
- $this->loadFixtures('Bid');
- $Bid = ClassRegistry::init('Bid');
- $this->assertFalse($Bid->save());
- $result = $Bid->save(array('Bid' => array()));
- $this->assertFalse($result);
- $result = $Bid->save(array('Bid' => array('not in schema' => 1)));
- $this->assertFalse($result);
- }
- /**
- * testInsertAnotherHabtmRecordWithSameForeignKey method
- *
- * @access public
- * @return void
- */
- public function testInsertAnotherHabtmRecordWithSameForeignKey() {
- $this->loadFixtures('JoinA', 'JoinB', 'JoinAB', 'JoinC', 'JoinAC');
- $TestModel = new JoinA();
- $result = $TestModel->JoinAsJoinB->findById(1);
- $expected = array(
- 'JoinAsJoinB' => array(
- 'id' => 1,
- 'join_a_id' => 1,
- 'join_b_id' => 2,
- 'other' => 'Data for Join A 1 Join B 2',
- 'created' => '2008-01-03 10:56:33',
- 'updated' => '2008-01-03 10:56:33'
- ));
- $this->assertEquals($expected, $result);
- $TestModel->JoinAsJoinB->create();
- $data = array(
- 'join_a_id' => 1,
- 'join_b_id' => 1,
- 'other' => 'Data for Join A 1 Join B 1',
- 'created' => '2008-01-03 10:56:44',
- 'updated' => '2008-01-03 10:56:44'
- );
- $result = $TestModel->JoinAsJoinB->save($data);
- $lastInsertId = $TestModel->JoinAsJoinB->getLastInsertID();
- $data['id'] = $lastInsertId;
- $this->assertEquals(array('JoinAsJoinB' => $data), $result);
- $this->assertTrue($lastInsertId > 0);
- $result = $TestModel->JoinAsJoinB->findById(1);
- $expected = array(
- 'JoinAsJoinB' => array(
- 'id' => 1,
- 'join_a_id' => 1,
- 'join_b_id' => 2,
- 'other' => 'Data for Join A 1 Join B 2',
- 'created' => '2008-01-03 10:56:33',
- 'updated' => '2008-01-03 10:56:33'
- ));
- $this->assertEquals($expected, $result);
- $updatedValue = 'UPDATED Data for Join A 1 Join B 2';
- $TestModel->JoinAsJoinB->id = 1;
- $result = $TestModel->JoinAsJoinB->saveField('other', $updatedValue, false);
- $this->assertFalse(empty($result));
- $result = $TestModel->JoinAsJoinB->findById(1);
- $this->assertEquals($updatedValue, $result['JoinAsJoinB']['other']);
- }
- /**
- * testSaveDateAsFirstEntry method
- *
- * @return void
- */
- public function testSaveDateAsFirstEntry() {
- $this->loadFixtures('Article', 'User', 'Comment', 'Attachment', 'Tag', 'ArticlesTag');
- $Article = new Article();
- $data = array(
- 'Article' => array(
- 'created' => array(
- 'day' => '1',
- 'month' => '1',
- 'year' => '2008'
- ),
- 'title' => 'Test Title',
- 'user_id' => 1
- ));
- $Article->create();
- $result = $Article->save($data);
- $this->assertFalse(empty($result));
- $testResult = $Article->find('first', array('conditions' => array('Article.title' => 'Test Title')));
- $this->assertEquals($data['Article']['title'], $testResult['Article']['title']);
- $this->assertEquals('2008-01-01 00:00:00', $testResult['Article']['created']);
- }
- /**
- * testUnderscoreFieldSave method
- *
- * @return void
- */
- public function testUnderscoreFieldSave() {
- $this->loadFixtures('UnderscoreField');
- $UnderscoreField = new UnderscoreField();
- $currentCount = $UnderscoreField->find('count');
- $this->assertEquals(3, $currentCount);
- $data = array('UnderscoreField' => array(
- 'user_id' => '1',
- 'my_model_has_a_field' => 'Content here',
- 'body' => 'Body',
- 'published' => 'Y',
- 'another_field' => 4
- ));
- $ret = $UnderscoreField->save($data);
- $this->assertFalse(empty($ret));
- $currentCount = $UnderscoreField->find('count');
- $this->assertEquals(4, $currentCount);
- }
- /**
- * testAutoSaveUuid method
- *
- * @return void
- */
- public function testAutoSaveUuid() {
- // SQLite does not support non-integer primary keys
- $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with SQLite.');
- $this->loadFixtures('Uuid');
- $TestModel = new Uuid();
- $TestModel->save(array('title' => 'Test record'));
- $result = $TestModel->findByTitle('Test record');
- $this->assertEquals(
- array('id', 'title', 'count', 'created', 'updated'),
- array_keys($result['Uuid'])
- );
- $this->assertEquals(36, strlen($result['Uuid']['id']));
- }
- /**
- * Ensure that if the id key is null but present the save doesn't fail (with an
- * x sql error: "Column id specified twice")
- *
- * @return void
- */
- public function testSaveUuidNull() {
- // SQLite does not support non-integer primary keys
- $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with SQLite.');
- $this->loadFixtures('Uuid');
- $TestModel = new Uuid();
- $TestModel->save(array('title' => 'Test record', 'id' => null));
- $result = $TestModel->findByTitle('Test record');
- $this->assertEquals(
- array('id', 'title', 'count', 'created', 'updated'),
- array_keys($result['Uuid'])
- );
- $this->assertEquals(36, strlen($result['Uuid']['id']));
- }
- /**
- * testZeroDefaultFieldValue method
- *
- * @return void
- */
- public function testZeroDefaultFieldValue() {
- $this->skipIf($this->db instanceof Sqlite, 'SQLite uses loose typing, this operation is unsupported.');
- $this->loadFixtures('DataTest');
- $TestModel = new DataTest();
- $TestModel->create(array());
- $TestModel->save();
- $result = $TestModel->findById($TestModel->id);
- $this->assertEquals(0, $result['DataTest']['count']);
- $this->assertEquals(0, $result['DataTest']['float']);
- }
- /**
- * Tests validation parameter order in custom validation methods
- *
- * @return void
- */
- public function testAllowSimulatedFields() {
- $TestModel = new ValidationTest1();
- $TestModel->create(array(
- 'title' => 'foo',
- 'bar' => 'baz'
- ));
- $expected = array(
- 'ValidationTest1' => array(
- 'title' => 'foo',
- 'bar' => 'baz'
- ));
- $this->assertEquals($expected, $TestModel->data);
- }
- /**
- * test that Caches are getting cleared on save().
- * ensure that both inflections of controller names are getting cleared
- * as url for controller could be either overallFavorites/index or overall_favorites/index
- *
- * @return void
- */
- public function testCacheClearOnSave() {
- $_back = array(
- 'check' => Configure::read('Cache.check'),
- 'disable' => Configure::read('Cache.disable'),
- );
- Configure::write('Cache.check', true);
- Configure::write('Cache.disable', false);
- $this->loadFixtures('OverallFavorite');
- $OverallFavorite = new OverallFavorite();
- touch(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php');
- touch(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php');
- $data = array(
- 'OverallFavorite' => array(
- 'id' => 22,
- 'model_type' => '8-track',
- 'model_id' => '3',
- 'priority' => '1'
- )
- );
- $OverallFavorite->create($data);
- $OverallFavorite->save();
- $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php'));
- $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php'));
- Configure::write('Cache.check', $_back['check']);
- Configure::write('Cache.disable', $_back['disable']);
- }
- /**
- * test that save() resets whitelist on failed save
- */
- public function testSaveFieldListResetsWhitelistOnFailedSave() {
- $this->loadFixtures('Bidding');
- $model = new Bidding();
- $whitelist = array('title');
- $model->whitelist = $whitelist;
- $result = $model->save(
- array(),
- array('fieldList' => array('body'))
- );
- $this->assertFalse($result);
- $this->assertEquals($whitelist, $model->whitelist);
- }
- /**
- * testSaveWithCounterCache method
- *
- * @return void
- */
- public function testSaveWithCounterCache() {
- $this->loadFixtures('Syfile', 'Item', 'Image', 'Portfolio', 'ItemsPortfolio');
- $TestModel = new Syfile();
- $TestModel2 = new Item();
- $result = $TestModel->findById(1);
- $this->assertSame($result['Syfile']['item_count'], null);
- $TestModel2->save(array(
- 'name' => 'Item 7',
- 'syfile_id' => 1,
- 'published' => false
- ));
- $result = $TestModel->findById(1);
- $this->assertEquals(2, $result['Syfile']['item_count']);
- $TestModel2->delete(1);
- $result = $TestModel->findById(1);
- $this->assertEquals(1, $result['Syfile']['item_count']);
- $TestModel2->id = 2;
- $TestModel2->saveField('syfile_id', 1);
- $result = $TestModel->findById(1);
- $this->assertEquals(2, $result['Syfile']['item_count']);
- $result = $TestModel->findById(2);
- $this->assertEquals(0, $result['Syfile']['item_count']);
- }
- /**
- * Tests that counter caches are updated when records are added
- *
- * @return void
- */
- public function testCounterCacheIncrease() {
- $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
- $User = new CounterCacheUser();
- $Post = new CounterCachePost();
- $data = array('Post' => array(
- 'id' => 22,
- 'title' => 'New Post',
- 'user_id' => 66
- ));
- $Post->save($data);
- $user = $User->find('first', array(
- 'conditions' => array('id' => 66),
- 'recursive' => -1
- ));
- $result = $user[$User->alias]['post_count'];
- $expected = 3;
- $this->assertEquals($expected, $result);
- }
- /**
- * Tests that counter caches are updated when records are deleted
- *
- * @return void
- */
- public function testCounterCacheDecrease() {
- $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
- $User = new CounterCacheUser();
- $Post = new CounterCachePost();
- $Post->delete(2);
- $user = $User->find('first', array(
- 'conditions' => array('id' => 66),
- 'recursive' => -1
- ));
- $result = $user[$User->alias]['post_count'];
- $expected = 1;
- $this->assertEquals($expected, $result);
- }
- /**
- * Tests that counter caches are updated when foreign keys of counted records change
- *
- * @return void
- */
- public function testCounterCacheUpdated() {
- $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
- $User = new CounterCacheUser();
- $Post = new CounterCachePost();
- $data = $Post->find('first', array(
- 'conditions' => array('id' => 1),
- 'recursive' => -1
- ));
- $data[$Post->alias]['user_id'] = 301;
- $Post->save($data);
- $users = $User->find('all', array('order' => 'User.id'));
- $this->assertEquals(1, $users[0]['User']['post_count']);
- $this->assertEquals(2, $users[1]['User']['post_count']);
- }
- /**
- * Test counter cache with models that use a non-standard (i.e. not using 'id')
- * as their primary key.
- *
- * @return void
- */
- public function testCounterCacheWithNonstandardPrimaryKey() {
- $this->loadFixtures(
- 'CounterCacheUserNonstandardPrimaryKey',
- 'CounterCachePostNonstandardPrimaryKey'
- );
- $User = new CounterCacheUserNonstandardPrimaryKey();
- $Post = new CounterCachePostNonstandardPrimaryKey();
- $data = $Post->find('first', array(
- 'conditions' => array('pid' => 1),
- 'recursive' => -1
- ));
- $data[$Post->alias]['uid'] = 301;
- $Post->save($data);
- $users = $User->find('all', array('order' => 'User.uid'));
- $this->assertEquals(1, $users[0]['User']['post_count']);
- $this->assertEquals(2, $users[1]['User']['post_count']);
- }
- /**
- * test Counter Cache With Self Joining table
- *
- * @return void
- */
- public function testCounterCacheWithSelfJoin() {
- $this->skipIf($this->db instanceof Sqlite, 'SQLite 2.x does not support ALTER TABLE ADD COLUMN');
- $this->loadFixtures('CategoryThread');
- $column = 'COLUMN ';
- if ($this->db instanceof Sqlserver) {
- $column = '';
- }
- $column .= $this->db->buildColumn(array('name' => 'child_count', 'type' => 'integer'));
- $this->db->query('ALTER TABLE ' . $this->db->fullTableName('category_threads') . ' ADD ' . $column);
- $this->db->flushMethodCache();
- $Category = new CategoryThread();
- $result = $Category->updateAll(array('CategoryThread.name' => "'updated'"), array('CategoryThread.parent_id' => 5));
- $this->assertFalse(empty($result));
- $Category = new CategoryThread();
- $Category->belongsTo['ParentCategory']['counterCache'] = 'child_count';
- $Category->updateCounterCache(array('parent_id' => 5));
- $result = Hash::extract($Category->find('all', array('conditions' => array('CategoryThread.id' => 5))), '{n}.CategoryThread.child_count');
- $expected = array(1);
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveWithCounterCacheScope method
- *
- * @return void
- */
- public function testSaveWithCounterCacheScope() {
- $this->loadFixtures('Syfile', 'Item', 'Image', 'ItemsPortfolio', 'Portfolio');
- $TestModel = new Syfile();
- $TestModel2 = new Item();
- $TestModel2->belongsTo['Syfile']['counterCache'] = true;
- $TestModel2->belongsTo['Syfile']['counterScope'] = array('published' => true);
- $result = $TestModel->findById(1);
- $this->assertSame($result['Syfile']['item_count'], null);
- $TestModel2->save(array(
- 'name' => 'Item 7',
- 'syfile_id' => 1,
- 'published' => true
- ));
- $result = $TestModel->findById(1);
- $this->assertEquals(1, $result['Syfile']['item_count']);
- $TestModel2->id = 1;
- $TestModel2->saveField('published', true);
- $result = $TestModel->findById(1);
- $this->assertEquals(2, $result['Syfile']['item_count']);
- $TestModel2->save(array(
- 'id' => 1,
- 'syfile_id' => 1,
- 'published' => false
- ));
- $result = $TestModel->findById(1);
- $this->assertEquals(1, $result['Syfile']['item_count']);
- }
- /**
- * Tests having multiple counter caches for an associated model
- *
- * @access public
- * @return void
- */
- public function testCounterCacheMultipleCaches() {
- $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
- $User = new CounterCacheUser();
- $Post = new CounterCachePost();
- $Post->unbindModel(array('belongsTo' => array('User')), false);
- $Post->bindModel(array(
- 'belongsTo' => array(
- 'User' => array(
- 'className' => 'CounterCacheUser',
- 'foreignKey' => 'user_id',
- 'counterCache' => array(
- true,
- 'posts_published' => array('Post.published' => true)
- )
- )
- )
- ), false);
- // Count Increase
- $data = array('Post' => array(
- 'id' => 22,
- 'title' => 'New Post',
- 'user_id' => 66,
- 'published' => true
- ));
- $Post->save($data);
- $result = $User->find('first', array(
- 'conditions' => array('id' => 66),
- 'recursive' => -1
- ));
- $this->assertEquals(3, $result[$User->alias]['post_count']);
- $this->assertEquals(2, $result[$User->alias]['posts_published']);
- // Count decrease
- $Post->delete(1);
- $result = $User->find('first', array(
- 'conditions' => array('id' => 66),
- 'recursive' => -1
- ));
- $this->assertEquals(2, $result[$User->alias]['post_count']);
- $this->assertEquals(2, $result[$User->alias]['posts_published']);
- // Count update
- $data = $Post->find('first', array(
- 'conditions' => array('id' => 1),
- 'recursive' => -1
- ));
- $data[$Post->alias]['user_id'] = 301;
- $Post->save($data);
- $result = $User->find('all', array('order' => 'User.id'));
- $this->assertEquals(2, $result[0]['User']['post_count']);
- $this->assertEquals(1, $result[1]['User']['posts_published']);
- }
- /**
- * test that beforeValidate returning false can abort saves.
- *
- * @return void
- */
- public function testBeforeValidateSaveAbortion() {
- $this->loadFixtures('Post');
- $Model = new CallbackPostTestModel();
- $Model->beforeValidateReturn = false;
- $data = array(
- 'title' => 'new article',
- 'body' => 'this is some text.'
- );
- $Model->create();
- $result = $Model->save($data);
- $this->assertFalse($result);
- }
- /**
- * test that beforeSave returning false can abort saves.
- *
- * @return void
- */
- public function testBeforeSaveSaveAbortion() {
- $this->loadFixtures('Post');
- $Model = new CallbackPostTestModel();
- $Model->beforeSaveReturn = false;
- $data = array(
- 'title' => 'new article',
- 'body' => 'this is some text.'
- );
- $Model->create();
- $result = $Model->save($data);
- $this->assertFalse($result);
- }
- /**
- * testSaveField method
- *
- * @return void
- */
- public function testSaveField() {
- $this->loadFixtures('Article');
- $TestModel = new Article();
- $TestModel->id = 1;
- $result = $TestModel->saveField('title', 'New First Article');
- $this->assertFalse(empty($result));
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
- $expected = array('Article' => array(
- 'id' => '1',
- 'user_id' => '1',
- 'title' => 'New First Article',
- 'body' => 'First Article Body'
- ));
- $this->assertEquals($expected, $result);
- $TestModel->id = 1;
- $result = $TestModel->saveField('title', '');
- $this->assertFalse(empty($result));
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
- $expected = array('Article' => array(
- 'id' => '1',
- 'user_id' => '1',
- 'title' => '',
- 'body' => 'First Article Body'
- ));
- $result['Article']['title'] = trim($result['Article']['title']);
- $this->assertEquals($expected, $result);
- $TestModel->id = 1;
- $TestModel->set('body', 'Messed up data');
- $result = $TestModel->saveField('title', 'First Article');
- $this->assertFalse(empty($result));
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
- $expected = array('Article' => array(
- 'id' => '1',
- 'user_id' => '1',
- 'title' => 'First Article',
- 'body' => 'First Article Body'
- ));
- $this->assertEquals($expected, $result);
- $TestModel->recursive = -1;
- $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
- $TestModel->id = 1;
- $result = $TestModel->saveField('title', '', true);
- $this->assertFalse($result);
- $TestModel->recursive = -1;
- $TestModel->id = 1;
- $result = $TestModel->saveField('user_id', 9999);
- $this->assertTrue((bool)$result);
- $result = $TestModel->read(array('id', 'user_id'), 1);
- $expected = array('Article' => array(
- 'id' => '1',
- 'user_id' => '9999',
- ));
- $this->assertEquals($expected, $result);
- $this->loadFixtures('Node', 'Dependency');
- $Node = new Node();
- $Node->set('id', 1);
- $result = $Node->read();
- $this->assertEquals(array('Second'), Hash::extract($result, 'ParentNode.{n}.name'));
- $Node->saveField('state', 10);
- $result = $Node->read();
- $this->assertEquals(array('Second'), Hash::extract($result, 'ParentNode.{n}.name'));
- }
- /**
- * testSaveWithCreate method
- *
- * @return void
- */
- public function testSaveWithCreate() {
- $this->loadFixtures(
- 'User',
- 'Article',
- 'User',
- 'Comment',
- 'Tag',
- 'ArticlesTag',
- 'Attachment'
- );
- $TestModel = new User();
- $data = array('User' => array(
- 'user' => 'user',
- 'password' => ''
- ));
- $result = $TestModel->save($data);
- $this->assertFalse($result);
- $this->assertTrue(!empty($TestModel->validationErrors));
- $TestModel = new Article();
- $data = array('Article' => array(
- 'user_id' => '',
- 'title' => '',
- 'body' => ''
- ));
- $result = $TestModel->create($data) && $TestModel->save();
- $this->assertFalse($result);
- $this->assertTrue(!empty($TestModel->validationErrors));
- $data = array('Article' => array(
- 'id' => 1,
- 'user_id' => '1',
- 'title' => 'New First Article',
- 'body' => ''
- ));
- $result = $TestModel->create($data) && $TestModel->save();
- $this->assertFalse($result);
- $data = array('Article' => array(
- 'id' => 1,
- 'title' => 'New First Article'
- ));
- $result = $TestModel->create() && $TestModel->save($data, false);
- $this->assertFalse(empty($result));
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
- $expected = array('Article' => array(
- 'id' => '1',
- 'user_id' => '1',
- 'title' => 'New First Article',
- 'body' => 'First Article Body',
- 'published' => 'N'
- ));
- $this->assertEquals($expected, $result);
- $data = array('Article' => array(
- 'id' => 1,
- 'user_id' => '2',
- 'title' => 'First Article',
- 'body' => 'New First Article Body',
- 'published' => 'Y'
- ));
- $result = $TestModel->create() && $TestModel->save($data, true, array('id', 'title', 'published'));
- $this->assertFalse(empty($result));
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
- $expected = array('Article' => array(
- 'id' => '1',
- 'user_id' => '1',
- 'title' => 'First Article',
- 'body' => 'First Article Body',
- 'published' => 'Y'
- ));
- $this->assertEquals($expected, $result);
- $data = array(
- 'Article' => array(
- 'user_id' => '2',
- 'title' => 'New Article',
- 'body' => 'New Article Body',
- 'created' => '2007-03-18 14:55:23',
- 'updated' => '2007-03-18 14:57:31'
- ),
- 'Tag' => array('Tag' => array(1, 3))
- );
- $TestModel->create();
- $result = $TestModel->create() && $TestModel->save($data);
- $this->assertFalse(empty($result));
- $TestModel->recursive = 2;
- $result = $TestModel->read(null, 4);
- $expected = array(
- 'Article' => array(
- 'id' => '4',
- 'user_id' => '2',
- 'title' => 'New Article',
- 'body' => 'New Article Body',
- 'published' => 'N',
- 'created' => '2007-03-18 14:55:23',
- 'updated' => '2007-03-18 14:57:31'
- ),
- 'User' => array(
- 'id' => '2',
- 'user' => 'nate',
- 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
- 'created' => '2007-03-17 01:18:23',
- 'updated' => '2007-03-17 01:20:31'
- ),
- 'Comment' => array(),
- 'Tag' => array(
- array(
- 'id' => '1',
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- array(
- 'id' => '3',
- 'tag' => 'tag3',
- 'created' => '2007-03-18 12:26:23',
- 'updated' => '2007-03-18 12:28:31'
- )));
- $this->assertEquals($expected, $result);
- $data = array('Comment' => array(
- 'article_id' => '4',
- 'user_id' => '1',
- 'comment' => 'Comment New Article',
- 'published' => 'Y',
- 'created' => '2007-03-18 14:57:23',
- 'updated' => '2007-03-18 14:59:31'
- ));
- $result = $TestModel->Comment->create() && $TestModel->Comment->save($data);
- $this->assertFalse(empty($result));
- $data = array('Attachment' => array(
- 'comment_id' => '7',
- 'attachment' => 'newattachment.zip',
- 'created' => '2007-03-18 15:02:23',
- 'updated' => '2007-03-18 15:04:31'
- ));
- $result = $TestModel->Comment->Attachment->save($data);
- $this->assertFalse(empty($result));
- $TestModel->recursive = 2;
- $result = $TestModel->read(null, 4);
- $expected = array(
- 'Article' => array(
- 'id' => '4',
- 'user_id' => '2',
- 'title' => 'New Article',
- 'body' => 'New Article Body',
- 'published' => 'N',
- 'created' => '2007-03-18 14:55:23',
- 'updated' => '2007-03-18 14:57:31'
- ),
- 'User' => array(
- 'id' => '2',
- 'user' => 'nate',
- 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
- 'created' => '2007-03-17 01:18:23',
- 'updated' => '2007-03-17 01:20:31'
- ),
- 'Comment' => array(
- array(
- 'id' => '7',
- 'article_id' => '4',
- 'user_id' => '1',
- 'comment' => 'Comment New Article',
- 'published' => 'Y',
- 'created' => '2007-03-18 14:57:23',
- 'updated' => '2007-03-18 14:59:31',
- 'Article' => array(
- 'id' => '4',
- 'user_id' => '2',
- 'title' => 'New Article',
- 'body' => 'New Article Body',
- 'published' => 'N',
- 'created' => '2007-03-18 14:55:23',
- 'updated' => '2007-03-18 14:57:31'
- ),
- 'User' => array(
- 'id' => '1',
- 'user' => 'mariano',
- 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
- 'created' => '2007-03-17 01:16:23',
- 'updated' => '2007-03-17 01:18:31'
- ),
- 'Attachment' => array(
- 'id' => '2',
- 'comment_id' => '7',
- 'attachment' => 'newattachment.zip',
- 'created' => '2007-03-18 15:02:23',
- 'updated' => '2007-03-18 15:04:31'
- ))),
- 'Tag' => array(
- array(
- 'id' => '1',
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- array(
- 'id' => '3',
- 'tag' => 'tag3',
- 'created' => '2007-03-18 12:26:23',
- 'updated' => '2007-03-18 12:28:31'
- )));
- $this->assertEquals($expected, $result);
- }
- /**
- * test that a null Id doesn't cause errors
- *
- * @return void
- */
- public function testSaveWithNullId() {
- $this->loadFixtures('User');
- $User = new User();
- $User->read(null, 1);
- $User->data['User']['id'] = null;
- $result = $User->save(array('password' => 'test'));
- $this->assertFalse(empty($result));
- $this->assertTrue($User->id > 0);
- $User->read(null, 2);
- $User->data['User']['id'] = null;
- $result = $User->save(array('password' => 'test'));
- $this->assertFalse(empty($result));
- $this->assertTrue($User->id > 0);
- $User->data['User'] = array('password' => 'something');
- $result = $User->save();
- $this->assertFalse(empty($result));
- $result = $User->read();
- $this->assertEquals('something', $User->data['User']['password']);
- }
- /**
- * testSaveWithSet method
- *
- * @return void
- */
- public function testSaveWithSet() {
- $this->loadFixtures('Article');
- $TestModel = new Article();
- // Create record we will be updating later
- $data = array('Article' => array(
- 'user_id' => '1',
- 'title' => 'Fourth Article',
- 'body' => 'Fourth Article Body',
- 'published' => 'Y'
- ));
- $result = $TestModel->create() && $TestModel->save($data);
- $this->assertFalse(empty($result));
- // Check record we created
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
- $expected = array('Article' => array(
- 'id' => '4',
- 'user_id' => '1',
- 'title' => 'Fourth Article',
- 'body' => 'Fourth Article Body',
- 'published' => 'Y'
- ));
- $this->assertEquals($expected, $result);
- // Create new record just to overlap Model->id on previously created record
- $data = array('Article' => array(
- 'user_id' => '4',
- 'title' => 'Fifth Article',
- 'body' => 'Fifth Article Body',
- 'published' => 'Y'
- ));
- $result = $TestModel->create() && $TestModel->save($data);
- $this->assertFalse(empty($result));
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
- $expected = array('Article' => array(
- 'id' => '5',
- 'user_id' => '4',
- 'title' => 'Fifth Article',
- 'body' => 'Fifth Article Body',
- 'published' => 'Y'
- ));
- $this->assertEquals($expected, $result);
- // Go back and edit the first article we created, starting by checking it's still there
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
- $expected = array('Article' => array(
- 'id' => '4',
- 'user_id' => '1',
- 'title' => 'Fourth Article',
- 'body' => 'Fourth Article Body',
- 'published' => 'Y'
- ));
- $this->assertEquals($expected, $result);
- // And now do the update with set()
- $data = array('Article' => array(
- 'id' => '4',
- 'title' => 'Fourth Article - New Title',
- 'published' => 'N'
- ));
- $result = $TestModel->set($data) && $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
- $expected = array('Article' => array(
- 'id' => '4',
- 'user_id' => '1',
- 'title' => 'Fourth Article - New Title',
- 'body' => 'Fourth Article Body',
- 'published' => 'N'
- ));
- $this->assertEquals($expected, $result);
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
- $expected = array('Article' => array(
- 'id' => '5',
- 'user_id' => '4',
- 'title' => 'Fifth Article',
- 'body' => 'Fifth Article Body',
- 'published' => 'Y'
- ));
- $this->assertEquals($expected, $result);
- $data = array('Article' => array('id' => '5', 'title' => 'Fifth Article - New Title 5'));
- $result = ($TestModel->set($data) && $TestModel->save());
- $this->assertFalse(empty($result));
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
- $expected = array('Article' => array(
- 'id' => '5',
- 'user_id' => '4',
- 'title' => 'Fifth Article - New Title 5',
- 'body' => 'Fifth Article Body',
- 'published' => 'Y'
- ));
- $this->assertEquals($expected, $result);
- $TestModel->recursive = -1;
- $result = $TestModel->find('all', array(
- 'fields' => array('id', 'title'),
- 'order' => array('Article.id' => 'ASC')
- ));
- $expected = array(
- array('Article' => array('id' => 1, 'title' => 'First Article')),
- array('Article' => array('id' => 2, 'title' => 'Second Article')),
- array('Article' => array('id' => 3, 'title' => 'Third Article')),
- array('Article' => array('id' => 4, 'title' => 'Fourth Article - New Title')),
- array('Article' => array('id' => 5, 'title' => 'Fifth Article - New Title 5'))
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveWithNonExistentFields method
- *
- * @return void
- */
- public function testSaveWithNonExistentFields() {
- $this->loadFixtures('Article');
- $TestModel = new Article();
- $TestModel->recursive = -1;
- $data = array(
- 'non_existent' => 'This field does not exist',
- 'user_id' => '1',
- 'title' => 'Fourth Article - New Title',
- 'body' => 'Fourth Article Body',
- 'published' => 'N'
- );
- $result = $TestModel->create() && $TestModel->save($data);
- $this->assertFalse(empty($result));
- $expected = array('Article' => array(
- 'id' => '4',
- 'user_id' => '1',
- 'title' => 'Fourth Article - New Title',
- 'body' => 'Fourth Article Body',
- 'published' => 'N'
- ));
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
- $this->assertEquals($expected, $result);
- $data = array(
- 'user_id' => '1',
- 'non_existent' => 'This field does not exist',
- 'title' => 'Fifth Article - New Title',
- 'body' => 'Fifth Article Body',
- 'published' => 'N'
- );
- $result = $TestModel->create() && $TestModel->save($data);
- $this->assertFalse(empty($result));
- $expected = array('Article' => array(
- 'id' => '5',
- 'user_id' => '1',
- 'title' => 'Fifth Article - New Title',
- 'body' => 'Fifth Article Body',
- 'published' => 'N'
- ));
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveFromXml method
- *
- * @return void
- */
- public function testSaveFromXml() {
- $this->markTestSkipped('This feature needs to be fixed or dropped');
- $this->loadFixtures('Article');
- App::uses('Xml', 'Utility');
- $Article = new Article();
- $result = $Article->save(Xml::build('<article title="test xml" user_id="5" />'));
- $this->assertFalse(empty($result));
- $results = $Article->find('first', array('conditions' => array('Article.title' => 'test xml')));
- $this->assertFalse(empty($results));
- $result = $Article->save(Xml::build('<article><title>testing</title><user_id>6</user_id></article>'));
- $this->assertFalse(empty($result));
- $results = $Article->find('first', array('conditions' => array('Article.title' => 'testing')));
- $this->assertFalse(empty($results));
- $result = $Article->save(Xml::build('<article><title>testing with DOMDocument</title><user_id>7</user_id></article>', array('return' => 'domdocument')));
- $this->assertFalse(empty($result));
- $results = $Article->find('first', array('conditions' => array('Article.title' => 'testing with DOMDocument')));
- $this->assertFalse(empty($results));
- }
- /**
- * testSaveHabtm method
- *
- * @return void
- */
- public function testSaveHabtm() {
- $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
- $TestModel = new Article();
- $result = $TestModel->findById(2);
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'Second Article',
- 'body' => 'Second Article Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:41:23',
- 'updated' => '2007-03-18 10:43:31'
- ),
- 'User' => array(
- 'id' => '3',
- 'user' => 'larry',
- 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
- 'created' => '2007-03-17 01:20:23',
- 'updated' => '2007-03-17 01:22:31'
- ),
- 'Comment' => array(
- array(
- 'id' => '5',
- 'article_id' => '2',
- 'user_id' => '1',
- 'comment' => 'First Comment for Second Article',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:53:23',
- 'updated' => '2007-03-18 10:55:31'
- ),
- array(
- 'id' => '6',
- 'article_id' => '2',
- 'user_id' => '2',
- 'comment' => 'Second Comment for Second Article',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:55:23',
- 'updated' => '2007-03-18 10:57:31'
- )),
- 'Tag' => array(
- array(
- 'id' => '1',
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- array(
- 'id' => '3',
- 'tag' => 'tag3',
- 'created' => '2007-03-18 12:26:23',
- 'updated' => '2007-03-18 12:28:31'
- )
- )
- );
- $this->assertEquals($expected, $result);
- $data = array(
- 'Article' => array(
- 'id' => '2',
- 'title' => 'New Second Article'
- ),
- 'Tag' => array('Tag' => array(1, 2))
- );
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $this->assertEquals($data['Tag'], $result['Tag']);
- $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'New Second Article',
- 'body' => 'Second Article Body'
- ),
- 'Tag' => array(
- array(
- 'id' => '1',
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- array(
- 'id' => '2',
- 'tag' => 'tag2',
- 'created' => '2007-03-18 12:24:23',
- 'updated' => '2007-03-18 12:26:31'
- )));
- $this->assertEquals($expected, $result);
- $data = array('Article' => array('id' => '2'), 'Tag' => array('Tag' => array(2, 3)));
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'New Second Article',
- 'body' => 'Second Article Body'
- ),
- 'Tag' => array(
- array(
- 'id' => '2',
- 'tag' => 'tag2',
- 'created' => '2007-03-18 12:24:23',
- 'updated' => '2007-03-18 12:26:31'
- ),
- array(
- 'id' => '3',
- 'tag' => 'tag3',
- 'created' => '2007-03-18 12:26:23',
- 'updated' => '2007-03-18 12:28:31'
- )));
- $this->assertEquals($expected, $result);
- $data = array('Tag' => array('Tag' => array(1, 2, 3)));
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'New Second Article',
- 'body' => 'Second Article Body'
- ),
- 'Tag' => array(
- array(
- 'id' => '1',
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- array(
- 'id' => '2',
- 'tag' => 'tag2',
- 'created' => '2007-03-18 12:24:23',
- 'updated' => '2007-03-18 12:26:31'
- ),
- array(
- 'id' => '3',
- 'tag' => 'tag3',
- 'created' => '2007-03-18 12:26:23',
- 'updated' => '2007-03-18 12:28:31'
- )));
- $this->assertEquals($expected, $result);
- $data = array('Tag' => array('Tag' => array()));
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $data = array('Tag' => array('Tag' => ''));
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'New Second Article',
- 'body' => 'Second Article Body'
- ),
- 'Tag' => array()
- );
- $this->assertEquals($expected, $result);
- $data = array('Tag' => array('Tag' => array(2, 3)));
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'New Second Article',
- 'body' => 'Second Article Body'
- ),
- 'Tag' => array(
- array(
- 'id' => '2',
- 'tag' => 'tag2',
- 'created' => '2007-03-18 12:24:23',
- 'updated' => '2007-03-18 12:26:31'
- ),
- array(
- 'id' => '3',
- 'tag' => 'tag3',
- 'created' => '2007-03-18 12:26:23',
- 'updated' => '2007-03-18 12:28:31'
- )));
- $this->assertEquals($expected, $result);
- $data = array(
- 'Tag' => array(
- 'Tag' => array(1, 2)
- ),
- 'Article' => array(
- 'id' => '2',
- 'title' => 'New Second Article'
- ));
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'New Second Article',
- 'body' => 'Second Article Body'
- ),
- 'Tag' => array(
- array(
- 'id' => '1',
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- array(
- 'id' => '2',
- 'tag' => 'tag2',
- 'created' => '2007-03-18 12:24:23',
- 'updated' => '2007-03-18 12:26:31'
- )));
- $this->assertEquals($expected, $result);
- $data = array(
- 'Tag' => array(
- 'Tag' => array(1, 2)
- ),
- 'Article' => array(
- 'id' => '2',
- 'title' => 'New Second Article Title'
- ));
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'New Second Article Title',
- 'body' => 'Second Article Body'
- ),
- 'Tag' => array(
- array(
- 'id' => '1',
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- array(
- 'id' => '2',
- 'tag' => 'tag2',
- 'created' => '2007-03-18 12:24:23',
- 'updated' => '2007-03-18 12:26:31'
- )
- )
- );
- $this->assertEquals($expected, $result);
- $data = array(
- 'Tag' => array(
- 'Tag' => array(2, 3)
- ),
- 'Article' => array(
- 'id' => '2',
- 'title' => 'Changed Second Article'
- ));
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'Changed Second Article',
- 'body' => 'Second Article Body'
- ),
- 'Tag' => array(
- array(
- 'id' => '2',
- 'tag' => 'tag2',
- 'created' => '2007-03-18 12:24:23',
- 'updated' => '2007-03-18 12:26:31'
- ),
- array(
- 'id' => '3',
- 'tag' => 'tag3',
- 'created' => '2007-03-18 12:26:23',
- 'updated' => '2007-03-18 12:28:31'
- )
- )
- );
- $this->assertEquals($expected, $result);
- $data = array(
- 'Tag' => array(
- 'Tag' => array(1, 3)
- ),
- 'Article' => array('id' => '2'),
- );
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'Changed Second Article',
- 'body' => 'Second Article Body'
- ),
- 'Tag' => array(
- array(
- 'id' => '1',
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- array(
- 'id' => '3',
- 'tag' => 'tag3',
- 'created' => '2007-03-18 12:26:23',
- 'updated' => '2007-03-18 12:28:31'
- )));
- $this->assertEquals($expected, $result);
- $data = array(
- 'Article' => array(
- 'id' => 10,
- 'user_id' => '2',
- 'title' => 'New Article With Tags and fieldList',
- 'body' => 'New Article Body with Tags and fieldList',
- 'created' => '2007-03-18 14:55:23',
- 'updated' => '2007-03-18 14:57:31'
- ),
- 'Tag' => array(
- 'Tag' => array(1, 2, 3)
- )
- );
- $result = $TestModel->create()
- && $TestModel->save($data, true, array('user_id', 'title', 'published'));
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->read();
- $expected = array(
- 'Article' => array(
- 'id' => 4,
- 'user_id' => 2,
- 'title' => 'New Article With Tags and fieldList',
- 'body' => '',
- 'published' => 'N',
- 'created' => '',
- 'updated' => ''
- ),
- 'Tag' => array(
- 0 => array(
- 'id' => 1,
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- 1 => array(
- 'id' => 2,
- 'tag' => 'tag2',
- 'created' => '2007-03-18 12:24:23',
- 'updated' => '2007-03-18 12:26:31'
- ),
- 2 => array(
- 'id' => 3,
- 'tag' => 'tag3',
- 'created' => '2007-03-18 12:26:23',
- 'updated' => '2007-03-18 12:28:31'
- )));
- $this->assertEquals($expected, $result);
- $this->loadFixtures('JoinA', 'JoinC', 'JoinAC', 'JoinB', 'JoinAB');
- $TestModel = new JoinA();
- $TestModel->hasBelongsToMany = array('JoinC' => array('unique' => true));
- $data = array(
- 'JoinA' => array(
- 'id' => 1,
- 'name' => 'Join A 1',
- 'body' => 'Join A 1 Body',
- ),
- 'JoinC' => array(
- 'JoinC' => array(
- array('join_c_id' => 2, 'other' => 'new record'),
- array('join_c_id' => 3, 'other' => 'new record')
- )
- )
- );
- $TestModel->save($data);
- $result = $TestModel->read(null, 1);
- $expected = array(4, 5);
- $this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.id'));
- $expected = array('new record', 'new record');
- $this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.other'));
- }
- /**
- * testSaveHabtmNoPrimaryData method
- *
- * @return void
- */
- public function testSaveHabtmNoPrimaryData() {
- $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
- $TestModel = new Article();
- $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')), false);
- $result = $TestModel->findById(2);
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'Second Article',
- 'body' => 'Second Article Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:41:23',
- 'updated' => '2007-03-18 10:43:31'
- ),
- 'Tag' => array(
- array(
- 'id' => '1',
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- array(
- 'id' => '3',
- 'tag' => 'tag3',
- 'created' => '2007-03-18 12:26:23',
- 'updated' => '2007-03-18 12:28:31'
- )
- )
- );
- $this->assertEquals($expected, $result);
- $TestModel->id = 2;
- $data = array('Tag' => array('Tag' => array(2)));
- $result = $TestModel->save($data);
- $this->assertEquals($data['Tag'], $result['Tag']);
- $result = $TestModel->findById(2);
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'Second Article',
- 'body' => 'Second Article Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:41:23',
- 'updated' => self::date()
- ),
- 'Tag' => array(
- array(
- 'id' => '2',
- 'tag' => 'tag2',
- 'created' => '2007-03-18 12:24:23',
- 'updated' => '2007-03-18 12:26:31'
- )
- )
- );
- $this->assertEquals($expected, $result);
- $this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio');
- $TestModel = new Portfolio();
- $result = $TestModel->findById(2);
- $expected = array(
- 'Portfolio' => array(
- 'id' => 2,
- 'seller_id' => 1,
- 'name' => 'Portfolio 2'
- ),
- 'Item' => array(
- array(
- 'id' => 2,
- 'syfile_id' => 2,
- 'published' => '',
- 'name' => 'Item 2',
- 'ItemsPortfolio' => array(
- 'id' => 2,
- 'item_id' => 2,
- 'portfolio_id' => 2
- )
- ),
- array(
- 'id' => 6,
- 'syfile_id' => 6,
- 'published' => '',
- 'name' => 'Item 6',
- 'ItemsPortfolio' => array(
- 'id' => 6,
- 'item_id' => 6,
- 'portfolio_id' => 2
- )
- )
- )
- );
- $this->assertEquals($expected, $result);
- $data = array('Item' => array('Item' => array(1, 2)));
- $TestModel->id = 2;
- $TestModel->save($data);
- $result = $TestModel->findById(2);
- $result['Item'] = Hash::sort($result['Item'], '{n}.id', 'asc');
- $expected = array(
- 'Portfolio' => array(
- 'id' => 2,
- 'seller_id' => 1,
- 'name' => 'Portfolio 2'
- ),
- 'Item' => array(
- array(
- 'id' => 1,
- 'syfile_id' => 1,
- 'published' => '',
- 'name' => 'Item 1',
- 'ItemsPortfolio' => array(
- 'id' => 7,
- 'item_id' => 1,
- 'portfolio_id' => 2
- )
- ),
- array(
- 'id' => 2,
- 'syfile_id' => 2,
- 'published' => '',
- 'name' => 'Item 2',
- 'ItemsPortfolio' => array(
- 'id' => 8,
- 'item_id' => 2,
- 'portfolio_id' => 2
- )
- )
- )
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveHabtmCustomKeys method
- *
- * @return void
- */
- public function testSaveHabtmCustomKeys() {
- $this->loadFixtures('Story', 'StoriesTag', 'Tag');
- $Story = new Story();
- $data = array(
- 'Story' => array('story' => '1'),
- 'Tag' => array(
- 'Tag' => array(2, 3)
- ));
- $result = $Story->set($data);
- $this->assertFalse(empty($result));
- $result = $Story->save();
- $this->assertFalse(empty($result));
- $result = $Story->find('all', array('order' => array('Story.story')));
- $expected = array(
- array(
- 'Story' => array(
- 'story' => 1,
- 'title' => 'First Story'
- ),
- 'Tag' => array(
- array(
- 'id' => 2,
- 'tag' => 'tag2',
- 'created' => '2007-03-18 12:24:23',
- 'updated' => '2007-03-18 12:26:31'
- ),
- array(
- 'id' => 3,
- 'tag' => 'tag3',
- 'created' => '2007-03-18 12:26:23',
- 'updated' => '2007-03-18 12:28:31'
- ))),
- array(
- 'Story' => array(
- 'story' => 2,
- 'title' => 'Second Story'
- ),
- 'Tag' => array()
- ));
- $this->assertEquals($expected, $result);
- }
- /**
- * test that saving habtm records respects conditions set in the 'conditions' key
- * for the association.
- *
- * @return void
- */
- public function testHabtmSaveWithConditionsInAssociation() {
- $this->loadFixtures('JoinThing', 'Something', 'SomethingElse');
- $Something = new Something();
- $Something->unbindModel(array('hasAndBelongsToMany' => array('SomethingElse')), false);
- $Something->bindModel(array(
- 'hasAndBelongsToMany' => array(
- 'DoomedSomethingElse' => array(
- 'className' => 'SomethingElse',
- 'joinTable' => 'join_things',
- 'conditions' => array('JoinThing.doomed' => true),
- 'unique' => true
- ),
- 'NotDoomedSomethingElse' => array(
- 'className' => 'SomethingElse',
- 'joinTable' => 'join_things',
- 'conditions' => array('JoinThing.doomed' => 0),
- 'unique' => true
- )
- )
- ), false);
- $result = $Something->read(null, 1);
- $this->assertTrue(empty($result['NotDoomedSomethingElse']));
- $this->assertEquals(1, count($result['DoomedSomethingElse']));
- $data = array(
- 'Something' => array('id' => 1),
- 'NotDoomedSomethingElse' => array(
- 'NotDoomedSomethingElse' => array(
- array('something_else_id' => 2, 'doomed' => 0),
- array('something_else_id' => 3, 'doomed' => 0)
- )
- )
- );
- $Something->create($data);
- $result = $Something->save();
- $this->assertFalse(empty($result));
- $result = $Something->read(null, 1);
- $this->assertEquals(2, count($result['NotDoomedSomethingElse']));
- $this->assertEquals(1, count($result['DoomedSomethingElse']));
- }
- /**
- * testHabtmSaveKeyResolution method
- *
- * @return void
- */
- public function testHabtmSaveKeyResolution() {
- $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies');
- $ThePaper = new ThePaper();
- $ThePaper->id = 1;
- $ThePaper->save(array('Monkey' => array(2, 3)));
- $result = $ThePaper->findById(1);
- $expected = array(
- array(
- 'id' => '2',
- 'device_type_id' => '1',
- 'name' => 'Device 2',
- 'typ' => '1'
- ),
- array(
- 'id' => '3',
- 'device_type_id' => '1',
- 'name' => 'Device 3',
- 'typ' => '2'
- ));
- $this->assertEquals($expected, $result['Monkey']);
- $ThePaper->id = 2;
- $ThePaper->save(array('Monkey' => array(1, 2, 3)));
- $result = $ThePaper->findById(2);
- $expected = array(
- array(
- 'id' => '1',
- 'device_type_id' => '1',
- 'name' => 'Device 1',
- 'typ' => '1'
- ),
- array(
- 'id' => '2',
- 'device_type_id' => '1',
- 'name' => 'Device 2',
- 'typ' => '1'
- ),
- array(
- 'id' => '3',
- 'device_type_id' => '1',
- 'name' => 'Device 3',
- 'typ' => '2'
- ));
- $this->assertEquals($expected, $result['Monkey']);
- $ThePaper->id = 2;
- $ThePaper->save(array('Monkey' => array(1, 3)));
- $result = $ThePaper->findById(2);
- $expected = array(
- array(
- 'id' => '1',
- 'device_type_id' => '1',
- 'name' => 'Device 1',
- 'typ' => '1'
- ),
- array(
- 'id' => '3',
- 'device_type_id' => '1',
- 'name' => 'Device 3',
- 'typ' => '2'
- ));
- $this->assertEquals($expected, $result['Monkey']);
- $result = $ThePaper->findById(1);
- $expected = array(
- array(
- 'id' => '2',
- 'device_type_id' => '1',
- 'name' => 'Device 2',
- 'typ' => '1'
- ),
- array(
- 'id' => '3',
- 'device_type_id' => '1',
- 'name' => 'Device 3',
- 'typ' => '2'
- ));
- $this->assertEquals($expected, $result['Monkey']);
- }
- /**
- * testCreationOfEmptyRecord method
- *
- * @return void
- */
- public function testCreationOfEmptyRecord() {
- $this->loadFixtures('Author');
- $TestModel = new Author();
- $this->assertEquals(4, $TestModel->find('count'));
- $TestModel->deleteAll(true, false, false);
- $this->assertEquals(0, $TestModel->find('count'));
- $result = $TestModel->save();
- $this->assertTrue(isset($result['Author']['created']));
- $this->assertTrue(isset($result['Author']['updated']));
- $this->assertEquals(1, $TestModel->find('count'));
- }
- /**
- * testCreateWithPKFiltering method
- *
- * @return void
- */
- public function testCreateWithPKFiltering() {
- $TestModel = new Article();
- $data = array(
- 'id' => 5,
- 'user_id' => 2,
- 'title' => 'My article',
- 'body' => 'Some text'
- );
- $result = $TestModel->create($data);
- $expected = array(
- 'Article' => array(
- 'published' => 'N',
- 'id' => 5,
- 'user_id' => 2,
- 'title' => 'My article',
- 'body' => 'Some text'
- ));
- $this->assertEquals($expected, $result);
- $this->assertEquals(5, $TestModel->id);
- $result = $TestModel->create($data, true);
- $expected = array(
- 'Article' => array(
- 'published' => 'N',
- 'id' => false,
- 'user_id' => 2,
- 'title' => 'My article',
- 'body' => 'Some text'
- ));
- $this->assertEquals($expected, $result);
- $this->assertFalse($TestModel->id);
- $result = $TestModel->create(array('Article' => $data), true);
- $expected = array(
- 'Article' => array(
- 'published' => 'N',
- 'id' => false,
- 'user_id' => 2,
- 'title' => 'My article',
- 'body' => 'Some text'
- ));
- $this->assertEquals($expected, $result);
- $this->assertFalse($TestModel->id);
- $data = array(
- 'id' => 6,
- 'user_id' => 2,
- 'title' => 'My article',
- 'body' => 'Some text',
- 'created' => '1970-01-01 00:00:00',
- 'updated' => '1970-01-01 12:00:00',
- 'modified' => '1970-01-01 12:00:00'
- );
- $result = $TestModel->create($data);
- $expected = array(
- 'Article' => array(
- 'published' => 'N',
- 'id' => 6,
- 'user_id' => 2,
- 'title' => 'My article',
- 'body' => 'Some text',
- 'created' => '1970-01-01 00:00:00',
- 'updated' => '1970-01-01 12:00:00',
- 'modified' => '1970-01-01 12:00:00'
- ));
- $this->assertEquals($expected, $result);
- $this->assertEquals(6, $TestModel->id);
- $result = $TestModel->create(array(
- 'Article' => array_diff_key($data, array(
- 'created' => true,
- 'updated' => true,
- 'modified' => true
- ))), true);
- $expected = array(
- 'Article' => array(
- 'published' => 'N',
- 'id' => false,
- 'user_id' => 2,
- 'title' => 'My article',
- 'body' => 'Some text'
- ));
- $this->assertEquals($expected, $result);
- $this->assertFalse($TestModel->id);
- }
- /**
- * testCreationWithMultipleData method
- *
- * @return void
- */
- public function testCreationWithMultipleData() {
- $this->loadFixtures('Article', 'Comment');
- $Article = new Article();
- $Comment = new Comment();
- $articles = $Article->find('all', array(
- 'fields' => array('id', 'title'),
- 'recursive' => -1,
- 'order' => array('Article.id' => 'ASC')
- ));
- $expected = array(
- array('Article' => array(
- 'id' => 1,
- 'title' => 'First Article'
- )),
- array('Article' => array(
- 'id' => 2,
- 'title' => 'Second Article'
- )),
- array('Article' => array(
- 'id' => 3,
- 'title' => 'Third Article'
- )));
- $this->assertEquals($expected, $articles);
- $comments = $Comment->find('all', array(
- 'fields' => array('id', 'article_id', 'user_id', 'comment', 'published'),
- 'recursive' => -1,
- 'order' => array('Comment.id' => 'ASC')
- ));
- $expected = array(
- array('Comment' => array(
- 'id' => 1,
- 'article_id' => 1,
- 'user_id' => 2,
- 'comment' => 'First Comment for First Article',
- 'published' => 'Y'
- )),
- array('Comment' => array(
- 'id' => 2,
- 'article_id' => 1,
- 'user_id' => 4,
- 'comment' => 'Second Comment for First Article',
- 'published' => 'Y'
- )),
- array('Comment' => array(
- 'id' => 3,
- 'article_id' => 1,
- 'user_id' => 1,
- 'comment' => 'Third Comment for First Article',
- 'published' => 'Y'
- )),
- array('Comment' => array(
- 'id' => 4,
- 'article_id' => 1,
- 'user_id' => 1,
- 'comment' => 'Fourth Comment for First Article',
- 'published' => 'N'
- )),
- array('Comment' => array(
- 'id' => 5,
- 'article_id' => 2,
- 'user_id' => 1,
- 'comment' => 'First Comment for Second Article',
- 'published' => 'Y'
- )),
- array('Comment' => array(
- 'id' => 6,
- 'article_id' => 2,
- 'user_id' => 2,
- 'comment' => 'Second Comment for Second Article',
- 'published' => 'Y'
- )));
- $this->assertEquals($expected, $comments);
- $data = array(
- 'Comment' => array(
- 'article_id' => 2,
- 'user_id' => 4,
- 'comment' => 'Brand New Comment',
- 'published' => 'N'
- ),
- 'Article' => array(
- 'id' => 2,
- 'title' => 'Second Article Modified'
- ));
- $result = $Comment->create($data);
- $this->assertFalse(empty($result));
- $result = $Comment->save();
- $this->assertFalse(empty($result));
- $articles = $Article->find('all', array(
- 'fields' => array('id', 'title'),
- 'recursive' => -1,
- 'order' => array('Article.id' => 'ASC')
- ));
- $expected = array(
- array('Article' => array(
- 'id' => 1,
- 'title' => 'First Article'
- )),
- array('Article' => array(
- 'id' => 2,
- 'title' => 'Second Article'
- )),
- array('Article' => array(
- 'id' => 3,
- 'title' => 'Third Article'
- )));
- $this->assertEquals($expected, $articles);
- $comments = $Comment->find('all', array(
- 'fields' => array('id', 'article_id', 'user_id', 'comment', 'published'),
- 'recursive' => -1,
- 'order' => array('Comment.id' => 'ASC')
- ));
- $expected = array(
- array('Comment' => array(
- 'id' => 1,
- 'article_id' => 1,
- 'user_id' => 2,
- 'comment' => 'First Comment for First Article',
- 'published' => 'Y'
- )),
- array('Comment' => array(
- 'id' => 2,
- 'article_id' => 1,
- 'user_id' => 4,
- 'comment' => 'Second Comment for First Article',
- 'published' => 'Y'
- )),
- array('Comment' => array(
- 'id' => 3,
- 'article_id' => 1,
- 'user_id' => 1,
- 'comment' => 'Third Comment for First Article',
- 'published' => 'Y'
- )),
- array('Comment' => array(
- 'id' => 4,
- 'article_id' => 1,
- 'user_id' => 1,
- 'comment' => 'Fourth Comment for First Article',
- 'published' => 'N'
- )),
- array('Comment' => array(
- 'id' => 5,
- 'article_id' => 2,
- 'user_id' => 1,
- 'comment' => 'First Comment for Second Article',
- 'published' => 'Y'
- )),
- array('Comment' => array(
- 'id' => 6,
- 'article_id' => 2,
- 'user_id' => 2, 'comment' =>
- 'Second Comment for Second Article',
- 'published' => 'Y'
- )),
- array('Comment' => array(
- 'id' => 7,
- 'article_id' => 2,
- 'user_id' => 4,
- 'comment' => 'Brand New Comment',
- 'published' => 'N'
- )));
- $this->assertEquals($expected, $comments);
- }
- /**
- * testCreationWithMultipleDataSameModel method
- *
- * @return void
- */
- public function testCreationWithMultipleDataSameModel() {
- $this->loadFixtures('Article');
- $Article = new Article();
- $result = $Article->field('title', array('id' => 1));
- $this->assertEquals('First Article', $result);
- $data = array(
- 'Article' => array(
- 'user_id' => 2,
- 'title' => 'Brand New Article',
- 'body' => 'Brand New Article Body',
- 'published' => 'Y'
- ),
- 'SecondaryArticle' => array(
- 'id' => 1
- ));
- $Article->create();
- $result = $Article->save($data);
- $this->assertFalse(empty($result));
- $result = $Article->getInsertID();
- $this->assertTrue(!empty($result));
- $result = $Article->field('title', array('id' => 1));
- $this->assertEquals('First Article', $result);
- $articles = $Article->find('all', array(
- 'fields' => array('id', 'title'),
- 'recursive' => -1,
- 'order' => array('Article.id' => 'ASC')
- ));
- $expected = array(
- array('Article' => array(
- 'id' => 1,
- 'title' => 'First Article'
- )),
- array('Article' => array(
- 'id' => 2,
- 'title' => 'Second Article'
- )),
- array('Article' => array(
- 'id' => 3,
- 'title' => 'Third Article'
- )),
- array('Article' => array(
- 'id' => 4,
- 'title' => 'Brand New Article'
- )));
- $this->assertEquals($expected, $articles);
- }
- /**
- * testCreationWithMultipleDataSameModelManualInstances method
- *
- * @return void
- */
- public function testCreationWithMultipleDataSameModelManualInstances() {
- $this->loadFixtures('PrimaryModel');
- $Primary = new PrimaryModel();
- $result = $Primary->field('primary_name', array('id' => 1));
- $this->assertEquals('Primary Name Existing', $result);
- $data = array(
- 'PrimaryModel' => array(
- 'primary_name' => 'Primary Name New'
- ),
- 'SecondaryModel' => array(
- 'id' => array(1)
- ));
- $Primary->create();
- $result = $Primary->save($data);
- $this->assertFalse(empty($result));
- $result = $Primary->field('primary_name', array('id' => 1));
- $this->assertEquals('Primary Name Existing', $result);
- $result = $Primary->getInsertID();
- $this->assertTrue(!empty($result));
- $result = $Primary->field('primary_name', array('id' => $result));
- $this->assertEquals('Primary Name New', $result);
- $result = $Primary->find('count');
- $this->assertEquals(2, $result);
- }
- /**
- * testRecordExists method
- *
- * @return void
- */
- public function testRecordExists() {
- $this->loadFixtures('User');
- $TestModel = new User();
- $this->assertFalse($TestModel->exists());
- $TestModel->read(null, 1);
- $this->assertTrue($TestModel->exists());
- $TestModel->create();
- $this->assertFalse($TestModel->exists());
- $TestModel->id = 4;
- $this->assertTrue($TestModel->exists());
- $TestModel = new TheVoid();
- $this->assertFalse($TestModel->exists());
- }
- /**
- * testRecordExistsMissingTable method
- *
- * @expectedException PDOException
- * @return void
- */
- public function testRecordExistsMissingTable() {
- $TestModel = new TheVoid();
- $TestModel->id = 5;
- $TestModel->exists();
- }
- /**
- * testUpdateExisting method
- *
- * @return void
- */
- public function testUpdateExisting() {
- $this->loadFixtures('User', 'Article', 'Comment');
- $TestModel = new User();
- $TestModel->create();
- $TestModel->save(array(
- 'User' => array(
- 'user' => 'some user',
- 'password' => 'some password'
- )));
- $this->assertTrue(is_int($TestModel->id) || (intval($TestModel->id) === 5));
- $id = $TestModel->id;
- $TestModel->save(array(
- 'User' => array(
- 'user' => 'updated user'
- )));
- $this->assertEquals($id, $TestModel->id);
- $result = $TestModel->findById($id);
- $this->assertEquals('updated user', $result['User']['user']);
- $this->assertEquals('some password', $result['User']['password']);
- $Article = new Article();
- $Comment = new Comment();
- $data = array(
- 'Comment' => array(
- 'id' => 1,
- 'comment' => 'First Comment for First Article'
- ),
- 'Article' => array(
- 'id' => 2,
- 'title' => 'Second Article'
- ));
- $result = $Article->save($data);
- $this->assertFalse(empty($result));
- $result = $Comment->save($data);
- $this->assertFalse(empty($result));
- }
- /**
- * test updating records and saving blank values.
- *
- * @return void
- */
- public function testUpdateSavingBlankValues() {
- $this->loadFixtures('Article');
- $Article = new Article();
- $Article->validate = array();
- $Article->create();
- $result = $Article->save(array(
- 'id' => 1,
- 'title' => '',
- 'body' => ''
- ));
- $this->assertTrue((bool)$result);
- $result = $Article->find('first', array('conditions' => array('Article.id' => 1)));
- $this->assertEquals('', $result['Article']['title'], 'Title is not blank');
- $this->assertEquals('', $result['Article']['body'], 'Body is not blank');
- }
- /**
- * testUpdateMultiple method
- *
- * @return void
- */
- public function testUpdateMultiple() {
- $this->loadFixtures('Comment', 'Article', 'User', 'CategoryThread');
- $TestModel = new Comment();
- $result = Hash::extract($TestModel->find('all'), '{n}.Comment.user_id');
- $expected = array('2', '4', '1', '1', '1', '2');
- $this->assertEquals($expected, $result);
- $TestModel->updateAll(array('Comment.user_id' => 5), array('Comment.user_id' => 2));
- $result = Hash::combine($TestModel->find('all'), '{n}.Comment.id', '{n}.Comment.user_id');
- $expected = array(1 => 5, 2 => 4, 3 => 1, 4 => 1, 5 => 1, 6 => 5);
- $this->assertEquals($expected, $result);
- $result = $TestModel->updateAll(
- array('Comment.comment' => "'Updated today'"),
- array('Comment.user_id' => 5)
- );
- $this->assertFalse(empty($result));
- $result = Hash::extract(
- $TestModel->find('all', array(
- 'conditions' => array(
- 'Comment.user_id' => 5
- ))),
- '{n}.Comment.comment'
- );
- $expected = array_fill(0, 2, 'Updated today');
- $this->assertEquals($expected, $result);
- }
- /**
- * testHabtmUuidWithUuidId method
- *
- * @return void
- */
- public function testHabtmUuidWithUuidId() {
- $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolio', 'UuiditemsUuidportfolioNumericid');
- $TestModel = new Uuidportfolio();
- $data = array('Uuidportfolio' => array('name' => 'Portfolio 3'));
- $data['Uuiditem']['Uuiditem'] = array('483798c8-c7cc-430e-8cf9-4fcc40cf8569');
- $TestModel->create($data);
- $TestModel->save();
- $id = $TestModel->id;
- $result = $TestModel->read(null, $id);
- $this->assertEquals(1, count($result['Uuiditem']));
- $this->assertEquals(36, strlen($result['Uuiditem'][0]['UuiditemsUuidportfolio']['id']));
- }
- /**
- * test HABTM saving when join table has no primary key and only 2 columns.
- *
- * @return void
- */
- public function testHabtmSavingWithNoPrimaryKeyUuidJoinTable() {
- $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
- $Fruit = new Fruit();
- $data = array(
- 'Fruit' => array(
- 'color' => 'Red',
- 'shape' => 'Heart-shaped',
- 'taste' => 'sweet',
- 'name' => 'Strawberry',
- ),
- 'UuidTag' => array(
- 'UuidTag' => array(
- '481fc6d0-b920-43e0-e50f-6d1740cf8569'
- )
- )
- );
- $result = $Fruit->save($data);
- $this->assertFalse(empty($result));
- }
- /**
- * test HABTM saving when join table has no primary key and only 2 columns, no with model is used.
- *
- * @return void
- */
- public function testHabtmSavingWithNoPrimaryKeyUuidJoinTableNoWith() {
- $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
- $Fruit = new FruitNoWith();
- $data = array(
- 'Fruit' => array(
- 'color' => 'Red',
- 'shape' => 'Heart-shaped',
- 'taste' => 'sweet',
- 'name' => 'Strawberry',
- ),
- 'UuidTag' => array(
- 'UuidTag' => array(
- '481fc6d0-b920-43e0-e50f-6d1740cf8569'
- )
- )
- );
- $result = $Fruit->save($data);
- $this->assertFalse(empty($result));
- }
- /**
- * testHabtmUuidWithNumericId method
- *
- * @return void
- */
- public function testHabtmUuidWithNumericId() {
- $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolioNumericid');
- $TestModel = new Uuiditem();
- $data = array('Uuiditem' => array('name' => 'Item 7', 'published' => 0));
- $data['Uuidportfolio']['Uuidportfolio'] = array('480af662-eb8c-47d3-886b-230540cf8569');
- $TestModel->create($data);
- $TestModel->save();
- $id = $TestModel->id;
- $result = $TestModel->read(null, $id);
- $this->assertEquals(1, count($result['Uuidportfolio']));
- }
- /**
- * testSaveMultipleHabtm method
- *
- * @return void
- */
- public function testSaveMultipleHabtm() {
- $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC');
- $TestModel = new JoinA();
- $result = $TestModel->findById(1);
- $expected = array(
- 'JoinA' => array(
- 'id' => 1,
- 'name' => 'Join A 1',
- 'body' => 'Join A 1 Body',
- 'created' => '2008-01-03 10:54:23',
- 'updated' => '2008-01-03 10:54:23'
- ),
- 'JoinB' => array(
- 0 => array(
- 'id' => 2,
- 'name' => 'Join B 2',
- 'created' => '2008-01-03 10:55:02',
- 'updated' => '2008-01-03 10:55:02',
- 'JoinAsJoinB' => array(
- 'id' => 1,
- 'join_a_id' => 1,
- 'join_b_id' => 2,
- 'other' => 'Data for Join A 1 Join B 2',
- 'created' => '2008-01-03 10:56:33',
- 'updated' => '2008-01-03 10:56:33'
- ))),
- 'JoinC' => array(
- 0 => array(
- 'id' => 2,
- 'name' => 'Join C 2',
- 'created' => '2008-01-03 10:56:12',
- 'updated' => '2008-01-03 10:56:12',
- 'JoinAsJoinC' => array(
- 'id' => 1,
- 'join_a_id' => 1,
- 'join_c_id' => 2,
- 'other' => 'Data for Join A 1 Join C 2',
- 'created' => '2008-01-03 10:57:22',
- 'updated' => '2008-01-03 10:57:22'
- ))));
- $this->assertEquals($expected, $result);
- $TestModel->id = 1;
- $data = array(
- 'JoinA' => array(
- 'id' => '1',
- 'name' => 'New name for Join A 1',
- 'updated' => self::date()
- ),
- 'JoinB' => array(
- array(
- 'id' => 1,
- 'join_b_id' => 2,
- 'other' => 'New data for Join A 1 Join B 2',
- 'created' => self::date(),
- 'updated' => self::date()
- )),
- 'JoinC' => array(
- array(
- 'id' => 1,
- 'join_c_id' => 2,
- 'other' => 'New data for Join A 1 Join C 2',
- 'created' => self::date(),
- 'updated' => self::date()
- )));
- $TestModel->set($data);
- $TestModel->save();
- $result = $TestModel->findById(1);
- $expected = array(
- 'JoinA' => array(
- 'id' => 1,
- 'name' => 'New name for Join A 1',
- 'body' => 'Join A 1 Body',
- 'created' => '2008-01-03 10:54:23',
- 'updated' => self::date()
- ),
- 'JoinB' => array(
- 0 => array(
- 'id' => 2,
- 'name' => 'Join B 2',
- 'created' => '2008-01-03 10:55:02',
- 'updated' => '2008-01-03 10:55:02',
- 'JoinAsJoinB' => array(
- 'id' => 1,
- 'join_a_id' => 1,
- 'join_b_id' => 2,
- 'other' => 'New data for Join A 1 Join B 2',
- 'created' => self::date(),
- 'updated' => self::date()
- ))),
- 'JoinC' => array(
- 0 => array(
- 'id' => 2,
- 'name' => 'Join C 2',
- 'created' => '2008-01-03 10:56:12',
- 'updated' => '2008-01-03 10:56:12',
- 'JoinAsJoinC' => array(
- 'id' => 1,
- 'join_a_id' => 1,
- 'join_c_id' => 2,
- 'other' => 'New data for Join A 1 Join C 2',
- 'created' => self::date(),
- 'updated' => self::date()
- ))));
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAll method
- *
- * @return void
- */
- public function testSaveAll() {
- $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
- $TestModel = new Post();
- $result = $TestModel->find('all');
- $this->assertEquals(3, count($result));
- $this->assertFalse(isset($result[3]));
- $TestModel->saveAll(array(
- 'Post' => array(
- 'title' => 'Post with Author',
- 'body' => 'This post will be saved with an author'
- ),
- 'Author' => array(
- 'user' => 'bob',
- 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
- )));
- $result = $TestModel->find('all');
- $expected = array(
- 'Post' => array(
- 'id' => '4',
- 'author_id' => '5',
- 'title' => 'Post with Author',
- 'body' => 'This post will be saved with an author',
- 'published' => 'N'
- ),
- 'Author' => array(
- 'id' => '5',
- 'user' => 'bob',
- 'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
- 'test' => 'working'
- ));
- $this->assertEquals(self::date(), $result[3]['Post']['created']);
- $this->assertEquals(self::date(), $result[3]['Post']['updated']);
- $this->assertEquals(self::date(), $result[3]['Author']['created']);
- $this->assertEquals(self::date(), $result[3]['Author']['updated']);
- unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
- unset($result[3]['Author']['created'], $result[3]['Author']['updated']);
- $this->assertEquals($expected, $result[3]);
- $this->assertEquals(4, count($result));
- $TestModel->deleteAll(true);
- $this->assertEquals(array(), $TestModel->find('all'));
- // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
- $this->db->truncate($TestModel);
- $TestModel->saveAll(array(
- array(
- 'title' => 'Multi-record post 1',
- 'body' => 'First multi-record post',
- 'author_id' => 2
- ),
- array(
- 'title' => 'Multi-record post 2',
- 'body' => 'Second multi-record post',
- 'author_id' => 2
- )));
- $result = $TestModel->find('all', array(
- 'recursive' => -1,
- 'order' => 'Post.id ASC'
- ));
- $expected = array(
- array(
- 'Post' => array(
- 'id' => '1',
- 'author_id' => '2',
- 'title' => 'Multi-record post 1',
- 'body' => 'First multi-record post',
- 'published' => 'N'
- )),
- array(
- 'Post' => array(
- 'id' => '2',
- 'author_id' => '2',
- 'title' => 'Multi-record post 2',
- 'body' => 'Second multi-record post',
- 'published' => 'N'
- )));
- $this->assertEquals(self::date(), $result[0]['Post']['created']);
- $this->assertEquals(self::date(), $result[0]['Post']['updated']);
- $this->assertEquals(self::date(), $result[1]['Post']['created']);
- $this->assertEquals(self::date(), $result[1]['Post']['updated']);
- unset($result[0]['Post']['created'], $result[0]['Post']['updated']);
- unset($result[1]['Post']['created'], $result[1]['Post']['updated']);
- $this->assertEquals($expected, $result);
- $TestModel = new Comment();
- $result = $TestModel->saveAll(array(
- 'Comment' => array(
- 'article_id' => 2,
- 'user_id' => 2,
- 'comment' => 'New comment with attachment',
- 'published' => 'Y'
- ),
- 'Attachment' => array(
- 'attachment' => 'some_file.tgz'
- )));
- $this->assertFalse(empty($result));
- $result = $TestModel->find('all');
- $expected = array(
- 'id' => '7',
- 'article_id' => '2',
- 'user_id' => '2',
- 'comment' => 'New comment with attachment',
- 'published' => 'Y'
- );
- $this->assertEquals(self::date(), $result[6]['Comment']['created']);
- $this->assertEquals(self::date(), $result[6]['Comment']['updated']);
- unset($result[6]['Comment']['created'], $result[6]['Comment']['updated']);
- $this->assertEquals($expected, $result[6]['Comment']);
- $expected = array(
- 'id' => '2',
- 'comment_id' => '7',
- 'attachment' => 'some_file.tgz'
- );
- $this->assertEquals(self::date(), $result[6]['Attachment']['created']);
- $this->assertEquals(self::date(), $result[6]['Attachment']['updated']);
- unset($result[6]['Attachment']['created'], $result[6]['Attachment']['updated']);
- $this->assertEquals($expected, $result[6]['Attachment']);
- }
- /**
- * Test SaveAll with Habtm relations
- *
- * @return void
- */
- public function testSaveAllHabtm() {
- $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
- $data = array(
- 'Article' => array(
- 'user_id' => 1,
- 'title' => 'Article Has and belongs to Many Tags'
- ),
- 'Tag' => array(
- 'Tag' => array(1, 2)
- ),
- 'Comment' => array(
- array(
- 'comment' => 'Article comment',
- 'user_id' => 1
- )));
- $Article = new Article();
- $result = $Article->saveAll($data);
- $this->assertFalse(empty($result));
- $result = $Article->read();
- $this->assertEquals(2, count($result['Tag']));
- $this->assertEquals('tag1', $result['Tag'][0]['tag']);
- $this->assertEquals(1, count($result['Comment']));
- $this->assertEquals(1, count($result['Comment'][0]['comment']));
- }
- /**
- * Test SaveAll with Habtm relations and extra join table fields
- *
- * @return void
- */
- public function testSaveAllHabtmWithExtraJoinTableFields() {
- $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
- $data = array(
- 'Something' => array(
- 'id' => 4,
- 'title' => 'Extra Fields',
- 'body' => 'Extra Fields Body',
- 'published' => '1'
- ),
- 'SomethingElse' => array(
- array('something_else_id' => 1, 'doomed' => '1'),
- array('something_else_id' => 2, 'doomed' => '0'),
- array('something_else_id' => 3, 'doomed' => '1')
- )
- );
- $Something = new Something();
- $result = $Something->saveAll($data);
- $this->assertFalse(empty($result));
- $result = $Something->read();
- $this->assertEquals(3, count($result['SomethingElse']));
- $this->assertTrue(Set::matches('/Something[id=4]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
- }
- /**
- * testSaveAllHasOne method
- *
- * @return void
- */
- public function testSaveAllHasOne() {
- $model = new Comment();
- $model->deleteAll(true);
- $this->assertEquals(array(), $model->find('all'));
- $model->Attachment->deleteAll(true);
- $this->assertEquals(array(), $model->Attachment->find('all'));
- $this->assertTrue($model->saveAll(array(
- 'Comment' => array(
- 'comment' => 'Comment with attachment',
- 'article_id' => 1,
- 'user_id' => 1
- ),
- 'Attachment' => array(
- 'attachment' => 'some_file.zip'
- ))));
- $result = $model->find('all', array('fields' => array(
- 'Comment.id', 'Comment.comment', 'Attachment.id',
- 'Attachment.comment_id', 'Attachment.attachment'
- )));
- $expected = array(array(
- 'Comment' => array(
- 'id' => '1',
- 'comment' => 'Comment with attachment'
- ),
- 'Attachment' => array(
- 'id' => '1',
- 'comment_id' => '1',
- 'attachment' => 'some_file.zip'
- )));
- $this->assertEquals($expected, $result);
- $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
- $data = array(
- 'Comment' => array(
- 'comment' => 'Comment with attachment',
- 'article_id' => 1,
- 'user_id' => 1
- ),
- 'Attachment' => array(
- 'attachment' => 'some_file.zip'
- ));
- $this->assertTrue($model->saveAll($data, array('validate' => 'first')));
- }
- /**
- * testSaveAllBelongsTo method
- *
- * @return void
- */
- public function testSaveAllBelongsTo() {
- $model = new Comment();
- $model->deleteAll(true);
- $this->assertEquals(array(), $model->find('all'));
- $model->Article->deleteAll(true);
- $this->assertEquals(array(), $model->Article->find('all'));
- $this->assertTrue($model->saveAll(array(
- 'Comment' => array(
- 'comment' => 'Article comment',
- 'article_id' => 1,
- 'user_id' => 1
- ),
- 'Article' => array(
- 'title' => 'Model Associations 101',
- 'user_id' => 1
- ))));
- $result = $model->find('all', array('fields' => array(
- 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
- )));
- $expected = array(array(
- 'Comment' => array(
- 'id' => '1',
- 'article_id' => '1',
- 'comment' => 'Article comment'
- ),
- 'Article' => array(
- 'id' => '1',
- 'title' => 'Model Associations 101'
- )));
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllHasOneValidation method
- *
- * @return void
- */
- public function testSaveAllHasOneValidation() {
- $model = new Comment();
- $model->deleteAll(true);
- $this->assertEquals(array(), $model->find('all'));
- $model->Attachment->deleteAll(true);
- $this->assertEquals(array(), $model->Attachment->find('all'));
- $model->validate = array('comment' => 'notEmpty');
- $model->Attachment->validate = array('attachment' => 'notEmpty');
- $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
- $result = $model->saveAll(
- array(
- 'Comment' => array(
- 'comment' => '',
- 'article_id' => 1,
- 'user_id' => 1
- ),
- 'Attachment' => array('attachment' => '')
- ),
- array('validate' => 'first')
- );
- $this->assertEquals(false, $result);
- $expected = array(
- 'comment' => array('This field cannot be left blank'),
- 'Attachment' => array(
- 'attachment' => array('This field cannot be left blank')
- )
- );
- $this->assertEquals($expected, $model->validationErrors);
- $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
- }
- /**
- * testSaveAllAtomic method
- *
- * @return void
- */
- public function testSaveAllAtomic() {
- $this->loadFixtures('Article', 'User', 'Comment');
- $TestModel = new Article();
- $result = $TestModel->saveAll(array(
- 'Article' => array(
- 'title' => 'Post with Author',
- 'body' => 'This post will be saved with an author',
- 'user_id' => 2
- ),
- 'Comment' => array(
- array('comment' => 'First new comment', 'user_id' => 2))
- ), array('atomic' => false));
- $this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
- $result = $TestModel->saveAll(array(
- array(
- 'id' => '1',
- 'title' => 'Baleeted First Post',
- 'body' => 'Baleeted!',
- 'published' => 'N'
- ),
- array(
- 'id' => '2',
- 'title' => 'Just update the title'
- ),
- array(
- 'title' => 'Creating a fourth post',
- 'body' => 'Fourth post body',
- 'user_id' => 2
- )
- ), array('atomic' => false));
- $this->assertSame($result, array(true, true, true));
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'comment' => 'First new comment',
- 'published' => 'Y',
- 'user_id' => 1
- ),
- array(
- 'comment' => 'Second new comment',
- 'published' => 'Y',
- 'user_id' => 2
- ))
- ), array('validate' => true, 'atomic' => false));
- $this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
- $TestModel->validate = array(
- 'title' => 'notEmpty',
- 'author_id' => 'numeric'
- );
- $result = $TestModel->saveAll(array(
- array(
- 'id' => '1',
- 'title' => 'Un-Baleeted First Post',
- 'body' => 'Not Baleeted!',
- 'published' => 'Y'
- ),
- array(
- 'id' => '2',
- 'title' => '',
- 'body' => 'Trying to get away with an empty title'
- )
- ), array('validate' => true, 'atomic' => false));
- $this->assertSame(array(true, false), $result);
- }
- /**
- * testSaveAllDeepAssociated method
- *
- * @return void
- */
- public function testSaveAllDeepAssociated() {
- $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
- $TestModel = new Article();
- $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
- $TestModel->hasAndBelongsToMany = array();
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- ), array('deep' => true));
- $this->assertTrue($result);
- $result = $TestModel->findById(2);
- $expected = array(
- 'First Comment for Second Article',
- 'Second Comment for Second Article',
- 'First new comment',
- 'Second new comment'
- );
- $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
- $this->assertEquals($expected, $result);
- $result = $TestModel->Comment->User->field('id', array('user' => 'newuser', 'password' => 'newuserpass'));
- $this->assertEquals(5, $result);
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
- array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
- )
- ), array('deep' => true));
- $this->assertTrue($result);
- $result = $TestModel->findById(2);
- $expected = array(
- 'First Comment for Second Article',
- 'Second Comment for Second Article',
- 'First new comment',
- 'Second new comment',
- 'Third new comment',
- 'Fourth new comment'
- );
- $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
- $this->assertEquals($expected, $result);
- $result = $TestModel->Comment->Attachment->field('id', array('attachment' => 'deepsaved'));
- $this->assertEquals(2, $result);
- $data = array(
- 'Attachment' => array(
- 'attachment' => 'deepsave insert',
- ),
- 'Comment' => array(
- 'comment' => 'First comment deepsave insert',
- 'published' => 'Y',
- 'user_id' => 5,
- 'Article' => array(
- 'title' => 'First Article deepsave insert',
- 'body' => 'First Article Body deepsave insert',
- 'User' => array(
- 'user' => '',
- 'password' => 'magic'
- ),
- ),
- )
- );
- $TestModel->Comment->Attachment->create();
- $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => true));
- $this->assertFalse($result);
- $expected = array('User' => array('user' => array('This field cannot be left blank')));
- $this->assertEquals($expected, $TestModel->validationErrors);
- $data['Comment']['Article']['User']['user'] = 'deepsave';
- $TestModel->Comment->Attachment->create();
- $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => true));
- $this->assertTrue($result);
- $result = $TestModel->Comment->Attachment->findById($TestModel->Comment->Attachment->id);
- $expected = array(
- 'Attachment' => array(
- 'id' => '3',
- 'comment_id' => '11',
- 'attachment' => 'deepsave insert',
- ),
- 'Comment' => array(
- 'id' => '11',
- 'article_id' => '4',
- 'user_id' => '5',
- 'comment' => 'First comment deepsave insert',
- 'published' => 'Y',
- )
- );
- unset($result['Attachment']['created'], $result['Attachment']['updated']);
- $this->assertEquals($expected['Attachment'], $result['Attachment']);
- unset($result['Comment']['created'], $result['Comment']['updated']);
- $this->assertEquals($expected['Comment'], $result['Comment']);
- $result = $TestModel->findById($result['Comment']['article_id']);
- $expected = array(
- 'Article' => array(
- 'id' => '4',
- 'user_id' => '6',
- 'title' => 'First Article deepsave insert',
- 'body' => 'First Article Body deepsave insert',
- 'published' => 'N',
- ),
- 'User' => array(
- 'id' => '6',
- 'user' => 'deepsave',
- 'password' => 'magic',
- ),
- 'Comment' => array(
- array(
- 'id' => '11',
- 'article_id' => '4',
- 'user_id' => '5',
- 'comment' => 'First comment deepsave insert',
- 'published' => 'Y',
- )
- )
- );
- unset(
- $result['Article']['created'], $result['Article']['updated'],
- $result['User']['created'], $result['User']['updated'],
- $result['Comment'][0]['created'], $result['Comment'][0]['updated']
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllDeepMany
- * tests the validate methods with deeper recursive data
- *
- * @return void
- */
- public function testSaveAllDeepMany() {
- $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
- $TestModel = new Article();
- $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
- $TestModel->hasAndBelongsToMany = array();
- $data = array(
- array(
- 'Article' => array('id' => 1),
- 'Comment' => array(
- array('comment' => 'First comment deepsaved article 1', 'published' => 'Y', 'User' => array('user' => 'savemany', 'password' => 'manysaved')),
- array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
- )
- ),
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => 'moresaved')),
- array('comment' => 'Second comment deepsaved article 2', 'published' => 'Y', 'user_id' => 2)
- )
- )
- );
- $result = $TestModel->saveAll($data, array('deep' => true));
- $this->assertTrue($result);
- $data = array(
- array(
- 'id' => 1, 'body' => '',
- 'Comment' => array(
- array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
- array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
- )
- ),
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => '')),
- array('comment' => '', 'published' => 'Y', 'user_id' => 2)
- )
- )
- );
- $TestModel->Comment->validate['comment'] = 'notEmpty';
- $result = $TestModel->saveAll($data, array('deep' => true));
- $this->assertFalse($result);
- $expected = array(
- 0 => array(
- 'body' => array('This field cannot be left blank'),
- 'Comment' => array(
- 0 => array(
- 'comment' => array('This field cannot be left blank'),
- 'User' => array(
- 'user' => array('This field cannot be left blank')
- )
- )
- )
- ),
- 1 => array(
- 'Comment' => array(
- 0 => array(
- 'User' => array(
- 'password' => array('This field cannot be left blank')
- )
- ),
- 1 => array(
- 'comment' => array('This field cannot be left blank')
- )
- )
- )
- );
- $result = $TestModel->validationErrors;
- $this->assertSame($expected, $result);
- }
- /**
- * testSaveAllDeepValidateOnly
- * tests the validate methods with deeper recursive data
- *
- * @return void
- */
- public function testSaveAllDeepValidateOnly() {
- $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
- $TestModel = new Article();
- $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
- $TestModel->hasAndBelongsToMany = array();
- $TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
- $TestModel->Comment->validate['comment'] = 'notEmpty';
- $result = $TestModel->saveAll(
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- ),
- array('validate' => 'only', 'deep' => true)
- );
- $this->assertTrue($result);
- $result = $TestModel->saveAll(
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- ),
- array('validate' => 'only', 'deep' => true)
- );
- $this->assertFalse($result);
- $result = $TestModel->saveAll(
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- ),
- array('validate' => 'only', 'atomic' => false, 'deep' => true)
- );
- $expected = array(
- 'Article' => true,
- 'Comment' => array(
- true,
- true
- )
- );
- $this->assertSame($expected, $result);
- $result = $TestModel->saveAll(
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- ),
- array('validate' => 'only', 'atomic' => false, 'deep' => true)
- );
- $expected = array(
- 'Article' => true,
- 'Comment' => array(
- false,
- true
- )
- );
- $this->assertSame($expected, $result);
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
- array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
- )
- ),
- array('validate' => 'only', 'deep' => true)
- );
- $this->assertTrue($result);
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
- array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
- )
- ),
- array('validate' => 'only', 'deep' => true)
- );
- $this->assertFalse($result);
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
- array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsave'))
- )
- ),
- array('validate' => 'only', 'atomic' => false, 'deep' => true)
- );
- $expected = array(
- 'Article' => true,
- 'Comment' => array(
- true,
- true
- )
- );
- $this->assertSame($expected, $result);
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
- array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
- )
- ),
- array('validate' => 'only', 'atomic' => false, 'deep' => true)
- );
- $expected = array(
- 'Article' => true,
- 'Comment' => array(
- true,
- false
- )
- );
- $this->assertSame($expected, $result);
- $expected = array(
- 'Comment' => array(
- 1 => array(
- 'Attachment' => array(
- 'attachment' => array('This field cannot be left blank')
- )
- )
- )
- );
- $result = $TestModel->validationErrors;
- $this->assertSame($expected, $result);
- $data = array(
- 'Attachment' => array(
- 'attachment' => 'deepsave insert',
- ),
- 'Comment' => array(
- 'comment' => 'First comment deepsave insert',
- 'published' => 'Y',
- 'user_id' => 5,
- 'Article' => array(
- 'title' => 'First Article deepsave insert',
- 'body' => 'First Article Body deepsave insert',
- 'User' => array(
- 'user' => 'deepsave',
- 'password' => 'magic'
- ),
- ),
- )
- );
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
- $this->assertTrue($result);
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
- $expected = array(
- 'Attachment' => true,
- 'Comment' => true
- );
- $this->assertSame($expected, $result);
- $data = array(
- 'Attachment' => array(
- 'attachment' => 'deepsave insert',
- ),
- 'Comment' => array(
- 'comment' => 'First comment deepsave insert',
- 'published' => 'Y',
- 'user_id' => 5,
- 'Article' => array(
- 'title' => 'First Article deepsave insert',
- 'body' => 'First Article Body deepsave insert',
- 'User' => array(
- 'user' => '',
- 'password' => 'magic'
- ),
- ),
- )
- );
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
- $this->assertFalse($result);
- $result = $TestModel->Comment->Attachment->validationErrors;
- $expected = array(
- 'Comment' => array(
- 'Article' => array(
- 'User' => array(
- 'user' => array('This field cannot be left blank')
- )
- )
- )
- );
- $this->assertSame($expected, $result);
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
- $expected = array(
- 'Attachment' => true,
- 'Comment' => false
- );
- $this->assertEquals($expected, $result);
- $data['Comment']['Article']['body'] = '';
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
- $this->assertFalse($result);
- $result = $TestModel->Comment->Attachment->validationErrors;
- $expected = array(
- 'Comment' => array(
- 'Article' => array(
- 'body' => array('This field cannot be left blank'),
- 'User' => array(
- 'user' => array('This field cannot be left blank')
- )
- )
- )
- );
- $this->assertSame($expected, $result);
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
- $expected = array(
- 'Attachment' => true,
- 'Comment' => false
- );
- $this->assertEquals($expected, $result);
- $data['Comment']['comment'] = '';
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
- $this->assertFalse($result);
- $result = $TestModel->Comment->Attachment->validationErrors;
- $expected = array(
- 'Comment' => array(
- 'comment' => array('This field cannot be left blank'),
- 'Article' => array(
- 'body' => array('This field cannot be left blank'),
- 'User' => array(
- 'user' => array('This field cannot be left blank')
- )
- )
- )
- );
- $this->assertSame($expected, $result);
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
- $expected = array(
- 'Attachment' => true,
- 'Comment' => false
- );
- $this->assertEquals($expected, $result);
- $data['Attachment']['attachment'] = '';
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
- $this->assertFalse($result);
- $result = $TestModel->Comment->Attachment->validationErrors;
- $expected = array(
- 'attachment' => array('This field cannot be left blank'),
- 'Comment' => array(
- 'comment' => array('This field cannot be left blank'),
- 'Article' => array(
- 'body' => array('This field cannot be left blank'),
- 'User' => array(
- 'user' => array('This field cannot be left blank')
- )
- )
- )
- );
- $this->assertSame($expected, $result);
- $result = $TestModel->Comment->validationErrors;
- $expected = array(
- 'comment' => array('This field cannot be left blank'),
- 'Article' => array(
- 'body' => array('This field cannot be left blank'),
- 'User' => array(
- 'user' => array('This field cannot be left blank')
- )
- )
- );
- $this->assertSame($expected, $result);
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
- $expected = array(
- 'Attachment' => false,
- 'Comment' => false
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllNotDeepAssociated method
- * test that only directly associated data gets saved
- *
- * @return void
- */
- public function testSaveAllNotDeepAssociated() {
- $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
- $TestModel = new Article();
- $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
- $TestModel->hasAndBelongsToMany = array();
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'comment' => 'First new comment', 'published' => 'Y', 'user_id' => 2,
- 'User' => array('user' => 'newuser', 'password' => 'newuserpass')
- ),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- ), array('deep' => false));
- $this->assertTrue($result);
- $result = $TestModel->Comment->User->field('id', array('user' => 'newuser', 'password' => 'newuserpass'));
- $this->assertFalse($result);
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 4),
- array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
- )
- ), array('deep' => false));
- $this->assertTrue($result);
- $result = $TestModel->Comment->Attachment->field('id', array('attachment' => 'deepsaved'));
- $this->assertFalse($result);
- $data = array(
- 'Attachment' => array(
- 'attachment' => 'deepsave insert',
- ),
- 'Comment' => array(
- 'comment' => 'First comment deepsave insert',
- 'published' => 'Y',
- 'user_id' => 4,
- 'article_id' => 1,
- 'Article' => array(
- 'title' => 'First Article deepsave insert',
- 'body' => 'First Article Body deepsave insert',
- 'User' => array(
- 'user' => 'deepsave',
- 'password' => 'magic'
- ),
- ),
- )
- );
- $expected = $TestModel->User->find('count');
- $TestModel->Comment->Attachment->create();
- $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => false));
- $this->assertTrue($result);
- $result = $TestModel->User->find('count');
- $this->assertEquals($expected, $result);
- $result = $TestModel->Comment->Attachment->findById($TestModel->Comment->Attachment->id);
- $expected = array(
- 'Attachment' => array(
- 'id' => '2',
- 'comment_id' => '11',
- 'attachment' => 'deepsave insert',
- ),
- 'Comment' => array(
- 'id' => '11',
- 'article_id' => 1,
- 'user_id' => '4',
- 'comment' => 'First comment deepsave insert',
- 'published' => 'Y',
- )
- );
- unset($result['Attachment']['created'], $result['Attachment']['updated']);
- $this->assertEquals($expected['Attachment'], $result['Attachment']);
- unset($result['Comment']['created'], $result['Comment']['updated']);
- $this->assertEquals($expected['Comment'], $result['Comment']);
- }
- /**
- * testSaveAllNotDeepMany
- * tests the save methods to not save deeper recursive data
- *
- * @return void
- */
- public function testSaveAllNotDeepMany() {
- $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
- $TestModel = new Article();
- $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
- $TestModel->hasAndBelongsToMany = array();
- $data = array(
- array(
- 'id' => 1,
- 'body' => '',
- 'Comment' => array(
- array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
- array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
- )
- ),
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => '')),
- array('comment' => '', 'published' => 'Y', 'user_id' => 2)
- )
- )
- );
- $TestModel->Comment->validate['comment'] = 'notEmpty';
- $result = $TestModel->saveAll($data, array('deep' => false));
- $this->assertFalse($result);
- $expected = array(
- 0 => array(
- 'body' => array('This field cannot be left blank')
- )
- );
- $result = $TestModel->validationErrors;
- $this->assertSame($expected, $result);
- $data = array(
- array(
- 'Article' => array('id' => 1, 'body' => 'Ignore invalid comment'),
- 'Comment' => array(
- array('comment' => '', 'published' => 'Y', 'user_id' => 2)
- )
- ),
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => '', 'published' => 'Y', 'user_id' => 2)
- )
- )
- );
- $result = $TestModel->saveAll($data, array('deep' => false));
- $this->assertTrue($result);
- }
- /**
- * testSaveAllNotDeepValidateOnly
- * tests the validate methods to not validate deeper recursive data
- *
- * @return void
- */
- public function testSaveAllNotDeepValidateOnly() {
- $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
- $TestModel = new Article();
- $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
- $TestModel->hasAndBelongsToMany = array();
- $TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
- $TestModel->Comment->validate['comment'] = 'notEmpty';
- $result = $TestModel->saveAll(
- array(
- 'Article' => array('id' => 2, 'body' => ''),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- ),
- array('validate' => 'only', 'deep' => false)
- );
- $this->assertFalse($result);
- $expected = array('body' => array('This field cannot be left blank'));
- $result = $TestModel->validationErrors;
- $this->assertSame($expected, $result);
- $result = $TestModel->saveAll(
- array(
- 'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- ),
- array('validate' => 'only', 'deep' => false)
- );
- $this->assertTrue($result);
- $result = $TestModel->saveAll(
- array(
- 'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- ),
- array('validate' => 'only', 'atomic' => false, 'deep' => false)
- );
- $expected = array(
- 'Article' => true,
- 'Comment' => array(
- true,
- true
- )
- );
- $this->assertSame($expected, $result);
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
- 'Comment' => array(
- array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
- array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
- )
- ),
- array('validate' => 'only', 'deep' => false)
- );
- $this->assertTrue($result);
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
- 'Comment' => array(
- array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
- array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
- )
- ),
- array('validate' => 'only', 'atomic' => false, 'deep' => false)
- );
- $expected = array(
- 'Article' => true,
- 'Comment' => array(
- true,
- true
- )
- );
- $this->assertSame($expected, $result);
- $expected = array();
- $result = $TestModel->validationErrors;
- $this->assertSame($expected, $result);
- $data = array(
- 'Attachment' => array(
- 'attachment' => 'deepsave insert',
- ),
- 'Comment' => array(
- 'comment' => 'First comment deepsave insert',
- 'published' => 'Y',
- 'user_id' => 5,
- 'Article' => array(
- 'title' => 'First Article deepsave insert ignored',
- 'body' => 'First Article Body deepsave insert',
- 'User' => array(
- 'user' => '',
- 'password' => 'magic'
- ),
- ),
- )
- );
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
- $this->assertTrue($result);
- $result = $TestModel->Comment->Attachment->validationErrors;
- $expected = array();
- $this->assertSame($expected, $result);
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
- $expected = array(
- 'Attachment' => true,
- 'Comment' => true
- );
- $this->assertEquals($expected, $result);
- $data['Comment']['Article']['body'] = '';
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
- $this->assertTrue($result);
- $result = $TestModel->Comment->Attachment->validationErrors;
- $expected = array();
- $this->assertSame($expected, $result);
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
- $expected = array(
- 'Attachment' => true,
- 'Comment' => true
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllHasMany method
- *
- * @return void
- */
- public function testSaveAllHasMany() {
- $this->loadFixtures('Article', 'Comment');
- $TestModel = new Article();
- $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- ));
- $this->assertFalse(empty($result));
- $result = $TestModel->findById(2);
- $expected = array(
- 'First Comment for Second Article',
- 'Second Comment for Second Article',
- 'First new comment',
- 'Second new comment'
- );
- $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
- $this->assertEquals($expected, $result);
- $result = $TestModel->saveAll(
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'comment' => 'Third new comment',
- 'published' => 'Y',
- 'user_id' => 1
- ))),
- array('atomic' => false)
- );
- $this->assertFalse(empty($result));
- $result = $TestModel->findById(2);
- $expected = array(
- 'First Comment for Second Article',
- 'Second Comment for Second Article',
- 'First new comment',
- 'Second new comment',
- 'Third new comment'
- );
- $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
- $this->assertEquals($expected, $result);
- $TestModel->beforeSaveReturn = false;
- $result = $TestModel->saveAll(
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'comment' => 'Fourth new comment',
- 'published' => 'Y',
- 'user_id' => 1
- ))),
- array('atomic' => false)
- );
- $this->assertEquals(array('Article' => false), $result);
- $result = $TestModel->findById(2);
- $expected = array(
- 'First Comment for Second Article',
- 'Second Comment for Second Article',
- 'First new comment',
- 'Second new comment',
- 'Third new comment'
- );
- $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllHasManyValidation method
- *
- * @return void
- */
- public function testSaveAllHasManyValidation() {
- $this->loadFixtures('Article', 'Comment');
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
- $TestModel->Comment->validate = array('comment' => 'notEmpty');
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => '', 'published' => 'Y', 'user_id' => 1),
- )
- ), array('validate' => true));
- $this->assertFalse($result);
- $expected = array('Comment' => array(
- array('comment' => array('This field cannot be left blank'))
- ));
- $this->assertEquals($expected, $TestModel->validationErrors);
- $expected = array(
- array('comment' => array('This field cannot be left blank'))
- );
- $this->assertEquals($expected, $TestModel->Comment->validationErrors);
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'comment' => '',
- 'published' => 'Y',
- 'user_id' => 1
- ))
- ), array('validate' => 'first'));
- $this->assertFalse($result);
- }
- /**
- * test saveAll with transactions and ensure there is no missing rollback.
- *
- * @return void
- */
- public function testSaveAllManyRowsTransactionNoRollback() {
- $this->loadFixtures('Post');
- $this->getMock('DboSource', array('connect', 'rollback', 'describe'), array(), 'MockTransactionDboSource');
- $db = ConnectionManager::create('mock_transaction', array(
- 'datasource' => 'MockTransactionDboSource',
- ));
- $db->expects($this->once())
- ->method('describe')
- ->will($this->returnValue(array()));
- $db->expects($this->once())->method('rollback');
- $Post = new Post('mock_transaction');
- $Post->validate = array(
- 'title' => array('rule' => array('notEmpty'))
- );
- $data = array(
- array('author_id' => 1, 'title' => 'New Fourth Post'),
- array('author_id' => 1, 'title' => '')
- );
- $Post->saveAll($data, array('atomic' => true));
- }
- /**
- * test saveAll with transactions and ensure there is no missing rollback.
- *
- * @return void
- */
- public function testSaveAllAssociatedTransactionNoRollback() {
- $testDb = ConnectionManager::getDataSource('test');
- $this->getMock(
- 'DboSource',
- array('connect', 'rollback', 'describe', 'create', 'update', 'begin'),
- array(),
- 'MockTransactionAssociatedDboSource'
- );
- $db = ConnectionManager::create('mock_transaction_assoc', array(
- 'datasource' => 'MockTransactionAssociatedDboSource',
- ));
- $this->mockObjects[] = $db;
- $db->columns = $testDb->columns;
- $db->expects($this->once())->method('rollback');
- $db->expects($this->any())->method('describe')
- ->will($this->returnValue(array(
- 'id' => array('type' => 'integer', 'length' => 11),
- 'title' => array('type' => 'string'),
- 'body' => array('type' => 'text'),
- 'published' => array('type' => 'string')
- )));
- $Post = new Post();
- $Post->useDbConfig = 'mock_transaction_assoc';
- $Post->Author->useDbConfig = 'mock_transaction_assoc';
- $Post->Author->validate = array(
- 'user' => array('rule' => array('notEmpty'))
- );
- $data = array(
- 'Post' => array(
- 'title' => 'New post',
- 'body' => 'Content',
- 'published' => 'Y'
- ),
- 'Author' => array(
- 'user' => '',
- 'password' => "sekret"
- )
- );
- $Post->saveAll($data, array('validate' => true));
- }
- /**
- * test saveAll with nested saveAll call.
- *
- * @return void
- */
- public function testSaveAllNestedSaveAll() {
- $this->loadFixtures('Sample');
- $TransactionTestModel = new TransactionTestModel();
- $data = array(
- array('apple_id' => 1, 'name' => 'sample5'),
- );
- $this->assertTrue($TransactionTestModel->saveAll($data, array('atomic' => true)));
- }
- /**
- * testSaveAllTransaction method
- *
- * @return void
- */
- public function testSaveAllTransaction() {
- $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
- $TestModel = new Post();
- $TestModel->validate = array('title' => 'notEmpty');
- $data = array(
- array('author_id' => 1, 'title' => 'New Fourth Post'),
- array('author_id' => 1, 'title' => 'New Fifth Post'),
- array('author_id' => 1, 'title' => '')
- );
- $this->assertFalse($TestModel->saveAll($data));
- $result = $TestModel->find('all', array('recursive' => -1));
- $expected = array(
- array('Post' => array(
- 'id' => '1',
- 'author_id' => 1,
- 'title' => 'First Post',
- 'body' => 'First Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:39:23',
- 'updated' => '2007-03-18 10:41:31'
- )),
- array('Post' => array(
- 'id' => '2',
- 'author_id' => 3,
- 'title' => 'Second Post',
- 'body' => 'Second Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:41:23',
- 'updated' => '2007-03-18 10:43:31'
- )),
- array('Post' => array(
- 'id' => '3',
- 'author_id' => 1,
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:43:23',
- 'updated' => '2007-03-18 10:45:31'
- )));
- if (count($result) != 3) {
- // Database doesn't support transactions
- $expected[] = array(
- 'Post' => array(
- 'id' => '4',
- 'author_id' => 1,
- 'title' => 'New Fourth Post',
- 'body' => null,
- 'published' => 'N',
- 'created' => self::date(),
- 'updated' => self::date()
- ));
- $expected[] = array(
- 'Post' => array(
- 'id' => '5',
- 'author_id' => 1,
- 'title' => 'New Fifth Post',
- 'body' => null,
- 'published' => 'N',
- 'created' => self::date(),
- 'updated' => self::date()
- ));
- $this->assertEquals($expected, $result);
- // Skip the rest of the transactional tests
- return;
- }
- $this->assertEquals($expected, $result);
- $data = array(
- array('author_id' => 1, 'title' => 'New Fourth Post'),
- array('author_id' => 1, 'title' => ''),
- array('author_id' => 1, 'title' => 'New Sixth Post')
- );
- $this->assertFalse($TestModel->saveAll($data));
- $result = $TestModel->find('all', array('recursive' => -1));
- $expected = array(
- array('Post' => array(
- 'id' => '1',
- 'author_id' => 1,
- 'title' => 'First Post',
- 'body' => 'First Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:39:23',
- 'updated' => '2007-03-18 10:41:31'
- )),
- array('Post' => array(
- 'id' => '2',
- 'author_id' => 3,
- 'title' => 'Second Post',
- 'body' => 'Second Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:41:23',
- 'updated' => '2007-03-18 10:43:31'
- )),
- array('Post' => array(
- 'id' => '3',
- 'author_id' => 1,
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:43:23',
- 'updated' => '2007-03-18 10:45:31'
- )));
- if (count($result) != 3) {
- // Database doesn't support transactions
- $expected[] = array(
- 'Post' => array(
- 'id' => '4',
- 'author_id' => 1,
- 'title' => 'New Fourth Post',
- 'body' => 'Third Post Body',
- 'published' => 'N',
- 'created' => self::date(),
- 'updated' => self::date()
- ));
- $expected[] = array(
- 'Post' => array(
- 'id' => '5',
- 'author_id' => 1,
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'N',
- 'created' => self::date(),
- 'updated' => self::date()
- ));
- }
- $this->assertEquals($expected, $result);
- $TestModel->validate = array('title' => 'notEmpty');
- $data = array(
- array('author_id' => 1, 'title' => 'New Fourth Post'),
- array('author_id' => 1, 'title' => 'New Fifth Post'),
- array('author_id' => 1, 'title' => 'New Sixth Post')
- );
- $this->assertTrue($TestModel->saveAll($data));
- $result = $TestModel->find('all', array(
- 'recursive' => -1,
- 'fields' => array('author_id', 'title', 'body', 'published'),
- 'order' => array('Post.created' => 'ASC')
- ));
- $expected = array(
- array('Post' => array(
- 'author_id' => 1,
- 'title' => 'First Post',
- 'body' => 'First Post Body',
- 'published' => 'Y'
- )),
- array('Post' => array(
- 'author_id' => 3,
- 'title' => 'Second Post',
- 'body' => 'Second Post Body',
- 'published' => 'Y'
- )),
- array('Post' => array(
- 'author_id' => 1,
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'Y'
- )),
- array('Post' => array(
- 'author_id' => 1,
- 'title' => 'New Fourth Post',
- 'body' => '',
- 'published' => 'N'
- )),
- array('Post' => array(
- 'author_id' => 1,
- 'title' => 'New Fifth Post',
- 'body' => '',
- 'published' => 'N'
- )),
- array('Post' => array(
- 'author_id' => 1,
- 'title' => 'New Sixth Post',
- 'body' => '',
- 'published' => 'N'
- )));
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllValidation method
- *
- * @return void
- */
- public function testSaveAllValidation() {
- $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
- $TestModel = new Post();
- $data = array(
- array(
- 'id' => '1',
- 'title' => 'Baleeted First Post',
- 'body' => 'Baleeted!',
- 'published' => 'N'
- ),
- array(
- 'id' => '2',
- 'title' => 'Just update the title'
- ),
- array(
- 'title' => 'Creating a fourth post',
- 'body' => 'Fourth post body',
- 'author_id' => 2
- ));
- $this->assertTrue($TestModel->saveAll($data));
- $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
- $expected = array(
- array(
- 'Post' => array(
- 'id' => '1',
- 'author_id' => '1',
- 'title' => 'Baleeted First Post',
- 'body' => 'Baleeted!',
- 'published' => 'N',
- 'created' => '2007-03-18 10:39:23'
- )),
- array(
- 'Post' => array(
- 'id' => '2',
- 'author_id' => '3',
- 'title' => 'Just update the title',
- 'body' => 'Second Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:41:23'
- )),
- array(
- 'Post' => array(
- 'id' => '3',
- 'author_id' => '1',
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:43:23',
- 'updated' => '2007-03-18 10:45:31'
- )),
- array(
- 'Post' => array(
- 'id' => '4',
- 'author_id' => '2',
- 'title' => 'Creating a fourth post',
- 'body' => 'Fourth post body',
- 'published' => 'N'
- )));
- $this->assertEquals(self::date(), $result[0]['Post']['updated']);
- $this->assertEquals(self::date(), $result[1]['Post']['updated']);
- $this->assertEquals(self::date(), $result[3]['Post']['created']);
- $this->assertEquals(self::date(), $result[3]['Post']['updated']);
- unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
- unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
- $this->assertEquals($expected, $result);
- $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
- $data = array(
- array(
- 'id' => '1',
- 'title' => 'Un-Baleeted First Post',
- 'body' => 'Not Baleeted!',
- 'published' => 'Y'
- ),
- array(
- 'id' => '2',
- 'title' => '',
- 'body' => 'Trying to get away with an empty title'
- ));
- $result = $TestModel->saveAll($data);
- $this->assertFalse($result);
- $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
- $errors = array(1 => array('title' => array('This field cannot be left blank')));
- $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
- if (!$transactionWorked) {
- $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
- $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
- }
- $this->assertEquals($errors, $TestModel->validationErrors);
- $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
- $data = array(
- array(
- 'id' => '1',
- 'title' => 'Un-Baleeted First Post',
- 'body' => 'Not Baleeted!',
- 'published' => 'Y'
- ),
- array(
- 'id' => '2',
- 'title' => '',
- 'body' => 'Trying to get away with an empty title'
- ));
- $result = $TestModel->saveAll($data, array('validate' => true, 'atomic' => false));
- $this->assertEquals(array(true, false), $result);
- $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
- $errors = array(1 => array('title' => array('This field cannot be left blank')));
- $expected = array(
- array(
- 'Post' => array(
- 'id' => '1',
- 'author_id' => '1',
- 'title' => 'Un-Baleeted First Post',
- 'body' => 'Not Baleeted!',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:39:23'
- )
- ),
- array(
- 'Post' => array(
- 'id' => '2',
- 'author_id' => '3',
- 'title' => 'Just update the title',
- 'body' => 'Second Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:41:23'
- )
- ),
- array(
- 'Post' => array(
- 'id' => '3',
- 'author_id' => '1',
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:43:23',
- 'updated' => '2007-03-18 10:45:31'
- )
- ),
- array(
- 'Post' => array(
- 'id' => '4',
- 'author_id' => '2',
- 'title' => 'Creating a fourth post',
- 'body' => 'Fourth post body',
- 'published' => 'N'
- )
- )
- );
- $this->assertEquals(self::date(), $result[0]['Post']['updated']);
- $this->assertEquals(self::date(), $result[1]['Post']['updated']);
- $this->assertEquals(self::date(), $result[3]['Post']['updated']);
- $this->assertEquals(self::date(), $result[3]['Post']['created']);
- unset(
- $result[0]['Post']['updated'], $result[1]['Post']['updated'],
- $result[3]['Post']['updated'], $result[3]['Post']['created']
- );
- $this->assertEquals($expected, $result);
- $this->assertEquals($errors, $TestModel->validationErrors);
- $data = array(
- array(
- 'id' => '1',
- 'title' => 'Re-Baleeted First Post',
- 'body' => 'Baleeted!',
- 'published' => 'N'
- ),
- array(
- 'id' => '2',
- 'title' => '',
- 'body' => 'Trying to get away with an empty title'
- ));
- $this->assertFalse($TestModel->saveAll($data, array('validate' => 'first')));
- $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
- unset(
- $result[0]['Post']['updated'], $result[1]['Post']['updated'],
- $result[3]['Post']['updated'], $result[3]['Post']['created']
- );
- $this->assertEquals($expected, $result);
- $this->assertEquals($errors, $TestModel->validationErrors);
- }
- /**
- * testSaveAllValidationOnly method
- *
- * @return void
- */
- public function testSaveAllValidationOnly() {
- $this->loadFixtures('Comment', 'Attachment');
- $TestModel = new Comment();
- $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
- $data = array(
- 'Comment' => array(
- 'comment' => 'This is the comment'
- ),
- 'Attachment' => array(
- 'attachment' => ''
- )
- );
- $result = $TestModel->saveAll($data, array('validate' => 'only'));
- $this->assertFalse($result);
- $TestModel = new Article();
- $TestModel->validate = array('title' => 'notEmpty');
- $result = $TestModel->saveAll(
- array(
- 0 => array('title' => ''),
- 1 => array('title' => 'title 1'),
- 2 => array('title' => 'title 2'),
- ),
- array('validate' => 'only')
- );
- $this->assertFalse($result);
- $expected = array(
- 0 => array('title' => array('This field cannot be left blank')),
- );
- $this->assertEquals($expected, $TestModel->validationErrors);
- $result = $TestModel->saveAll(
- array(
- 0 => array('title' => 'title 0'),
- 1 => array('title' => ''),
- 2 => array('title' => 'title 2'),
- ),
- array('validate' => 'only')
- );
- $this->assertFalse($result);
- $expected = array(
- 1 => array('title' => array('This field cannot be left blank')),
- );
- $this->assertEquals($expected, $TestModel->validationErrors);
- }
- /**
- * testSaveAllValidateFirst method
- *
- * @return void
- */
- public function testSaveAllValidateFirst() {
- $this->loadFixtures('Article', 'Comment', 'Attachment', 'User', 'ArticlesTag', 'Tag');
- $model = new Article();
- $model->deleteAll(true);
- $model->Comment->validate = array('comment' => 'notEmpty');
- $result = $model->saveAll(array(
- 'Article' => array(
- 'title' => 'Post with Author',
- 'body' => 'This post will be saved author'
- ),
- 'Comment' => array(
- array('comment' => 'First new comment'),
- array('comment' => '')
- )
- ), array('validate' => 'first'));
- $this->assertFalse($result);
- $result = $model->find('all');
- $this->assertSame(array(), $result);
- $expected = array('Comment' => array(
- 1 => array('comment' => array('This field cannot be left blank'))
- ));
- $this->assertEquals($expected['Comment'], $model->Comment->validationErrors);
- $this->assertSame($model->Comment->find('count'), 0);
- $result = $model->saveAll(
- array(
- 'Article' => array(
- 'title' => 'Post with Author',
- 'body' => 'This post will be saved with an author',
- 'user_id' => 2
- ),
- 'Comment' => array(
- array(
- 'comment' => 'Only new comment',
- 'user_id' => 2
- ))),
- array('validate' => 'first')
- );
- $this->assertSame($result, true);
- $result = $model->Comment->find('all');
- $this->assertSame(count($result), 1);
- $result = Hash::extract($result, '{n}.Comment.article_id');
- $this->assertEquals(4, $result[0]);
- $model->deleteAll(true);
- $data = array(
- 'Article' => array(
- 'title' => 'Post with Author saveAlled from comment',
- 'body' => 'This post will be saved with an author',
- 'user_id' => 2
- ),
- 'Comment' => array(
- 'comment' => 'Only new comment', 'user_id' => 2
- ));
- $result = $model->Comment->saveAll($data, array('validate' => 'first'));
- $this->assertFalse(empty($result));
- $result = $model->find('all');
- $this->assertEquals(
- $result[0]['Article']['title'],
- 'Post with Author saveAlled from comment'
- );
- $this->assertEquals('Only new comment', $result[0]['Comment'][0]['comment']);
- }
- /**
- * test saveAll()'s return is correct when using atomic = false and validate = first.
- *
- * @return void
- */
- public function testSaveAllValidateFirstAtomicFalse() {
- $this->loadFixtures('Something');
- $Something = new Something();
- $invalidData = array(
- array(
- 'title' => 'foo',
- 'body' => 'bar',
- 'published' => 'baz',
- ),
- array(
- 'body' => 3,
- 'published' => 'sd',
- ),
- );
- $Something->create();
- $Something->validate = array(
- 'title' => array(
- 'rule' => 'alphaNumeric',
- 'required' => true,
- ),
- 'body' => array(
- 'rule' => 'alphaNumeric',
- 'required' => true,
- 'allowEmpty' => true,
- ),
- );
- $result = $Something->saveAll($invalidData, array(
- 'atomic' => false,
- 'validate' => 'first',
- ));
- $expected = array(true, false);
- $this->assertEquals($expected, $result);
- $Something = new Something();
- $validData = array(
- array(
- 'title' => 'title value',
- 'body' => 'body value',
- 'published' => 'baz',
- ),
- array(
- 'title' => 'valid',
- 'body' => 'this body',
- 'published' => 'sd',
- ),
- );
- $Something->create();
- $result = $Something->saveAll($validData, array(
- 'atomic' => false,
- 'validate' => 'first',
- ));
- $expected = array(true, true);
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllHasManyValidationOnly method
- *
- * @return void
- */
- public function testSaveAllHasManyValidationOnly() {
- $this->loadFixtures('Article', 'Comment', 'Attachment');
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
- $TestModel->Comment->validate = array('comment' => 'notEmpty');
- $result = $TestModel->saveAll(
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'id' => 1,
- 'comment' => '',
- 'published' => 'Y',
- 'user_id' => 1),
- array(
- 'id' => 2,
- 'comment' =>
- 'comment',
- 'published' => 'Y',
- 'user_id' => 1
- ))),
- array('validate' => 'only')
- );
- $this->assertFalse($result);
- $result = $TestModel->saveAll(
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'id' => 1,
- 'comment' => '',
- 'published' => 'Y',
- 'user_id' => 1
- ),
- array(
- 'id' => 2,
- 'comment' => 'comment',
- 'published' => 'Y',
- 'user_id' => 1
- ),
- array(
- 'id' => 3,
- 'comment' => '',
- 'published' => 'Y',
- 'user_id' => 1
- ))),
- array(
- 'validate' => 'only',
- 'atomic' => false
- ));
- $expected = array(
- 'Article' => true,
- 'Comment' => array(false, true, false)
- );
- $this->assertSame($expected, $result);
- $expected = array('Comment' => array(
- 0 => array('comment' => array('This field cannot be left blank')),
- 2 => array('comment' => array('This field cannot be left blank'))
- ));
- $this->assertEquals($expected, $TestModel->validationErrors);
- $expected = array(
- 0 => array('comment' => array('This field cannot be left blank')),
- 2 => array('comment' => array('This field cannot be left blank'))
- );
- $this->assertEquals($expected, $TestModel->Comment->validationErrors);
- }
- /**
- * test that saveAll still behaves like previous versions (does not necessarily need a first argument)
- *
- * @return void
- */
- public function testSaveAllWithSet() {
- $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
- $data = array(
- 'Article' => array(
- 'user_id' => 1,
- 'title' => 'Article Has and belongs to Many Tags'
- ),
- 'Tag' => array(
- 'Tag' => array(1, 2)
- ),
- 'Comment' => array(
- array(
- 'comment' => 'Article comment',
- 'user_id' => 1
- )));
- $Article = new Article();
- $Article->set($data);
- $result = $Article->saveAll();
- $this->assertFalse(empty($result));
- }
- /**
- * test that saveAll behaves like plain save() when supplied empty data
- *
- * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
- * @return void
- */
- public function testSaveAllEmptyData() {
- $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
- $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
- $model = new Article();
- $result = $model->saveAll(array(), array('validate' => 'first'));
- $this->assertFalse(empty($result));
- $model = new ProductUpdateAll();
- $result = $model->saveAll();
- $this->assertFalse($result);
- }
- /**
- * testSaveAssociated method
- *
- * @return void
- */
- public function testSaveAssociated() {
- $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
- $TestModel = new Post();
- $result = $TestModel->find('all');
- $this->assertEquals(3, count($result));
- $this->assertFalse(isset($result[3]));
- $TestModel->saveAssociated(array(
- 'Post' => array(
- 'title' => 'Post with Author',
- 'body' => 'This post will be saved with an author'
- ),
- 'Author' => array(
- 'user' => 'bob',
- 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
- )));
- $result = $TestModel->find('all', array('order' => array('Post.id ' => 'ASC')));
- $expected = array(
- 'Post' => array(
- 'id' => '4',
- 'author_id' => '5',
- 'title' => 'Post with Author',
- 'body' => 'This post will be saved with an author',
- 'published' => 'N'
- ),
- 'Author' => array(
- 'id' => '5',
- 'user' => 'bob',
- 'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
- 'test' => 'working'
- ));
- $this->assertEquals(self::date(), $result[3]['Post']['updated']);
- $this->assertEquals(self::date(), $result[3]['Post']['created']);
- $this->assertEquals(self::date(), $result[3]['Author']['created']);
- $this->assertEquals(self::date(), $result[3]['Author']['updated']);
- unset(
- $result[3]['Post']['updated'], $result[3]['Post']['created'],
- $result[3]['Author']['updated'], $result[3]['Author']['created']
- );
- $this->assertEquals($expected, $result[3]);
- $this->assertEquals(4, count($result));
- $TestModel = new Comment();
- $result = $TestModel->saveAssociated(array(
- 'Comment' => array(
- 'article_id' => 2,
- 'user_id' => 2,
- 'comment' => 'New comment with attachment',
- 'published' => 'Y'
- ),
- 'Attachment' => array(
- 'attachment' => 'some_file.tgz'
- )));
- $this->assertFalse(empty($result));
- $result = $TestModel->find('all');
- $expected = array(
- 'id' => '7',
- 'article_id' => '2',
- 'user_id' => '2',
- 'comment' => 'New comment with attachment',
- 'published' => 'Y'
- );
- $this->assertEquals(self::date(), $result[6]['Comment']['updated']);
- $this->assertEquals(self::date(), $result[6]['Comment']['created']);
- unset($result[6]['Comment']['updated'], $result[6]['Comment']['created']);
- $this->assertEquals($expected, $result[6]['Comment']);
- $expected = array(
- 'id' => '2',
- 'comment_id' => '7',
- 'attachment' => 'some_file.tgz'
- );
- $this->assertEquals(self::date(), $result[6]['Attachment']['updated']);
- $this->assertEquals(self::date(), $result[6]['Attachment']['created']);
- unset($result[6]['Attachment']['updated'], $result[6]['Attachment']['created']);
- $this->assertEquals($expected, $result[6]['Attachment']);
- }
- /**
- * Test that validate = first, atomic = false works when associated records
- * fail validation.
- *
- * @return void
- */
- public function testSaveAssociatedAtomicFalseValidateFirstWithErrors() {
- $this->loadFixtures('Comment', 'Article', 'User');
- $Article = ClassRegistry::init('Article');
- $Article->Comment->validator()->add('comment', array(
- array('rule' => 'notEmpty')
- ));
- $data = array(
- 'Article' => array(
- 'user_id' => 1,
- 'title' => 'Foo',
- 'body' => 'text',
- 'published' => 'N'
- ),
- 'Comment' => array(
- array(
- 'user_id' => 1,
- 'comment' => '',
- 'published' => 'N',
- )
- ),
- );
- $Article->saveAssociated(
- $data,
- array('validate' => 'first', 'atomic' => false)
- );
- $result = $Article->validationErrors;
- $expected = array(
- 'Comment' => array(
- array(
- 'comment' => array( 'This field cannot be left blank' )
- )
- )
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveMany method
- *
- * @return void
- */
- public function testSaveMany() {
- $this->loadFixtures('Post');
- $TestModel = new Post();
- $TestModel->deleteAll(true);
- $this->assertEquals(array(), $TestModel->find('all'));
- // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
- $this->db->truncate($TestModel);
- $TestModel->saveMany(array(
- array(
- 'title' => 'Multi-record post 1',
- 'body' => 'First multi-record post',
- 'author_id' => 2
- ),
- array(
- 'title' => 'Multi-record post 2',
- 'body' => 'Second multi-record post',
- 'author_id' => 2
- )));
- $result = $TestModel->find('all', array(
- 'recursive' => -1,
- 'order' => 'Post.id ASC'
- ));
- $expected = array(
- array(
- 'Post' => array(
- 'id' => '1',
- 'author_id' => '2',
- 'title' => 'Multi-record post 1',
- 'body' => 'First multi-record post',
- 'published' => 'N'
- )
- ),
- array(
- 'Post' => array(
- 'id' => '2',
- 'author_id' => '2',
- 'title' => 'Multi-record post 2',
- 'body' => 'Second multi-record post',
- 'published' => 'N'
- )
- )
- );
- $this->assertEquals(self::date(), $result[0]['Post']['updated']);
- $this->assertEquals(self::date(), $result[0]['Post']['created']);
- $this->assertEquals(self::date(), $result[1]['Post']['updated']);
- $this->assertEquals(self::date(), $result[1]['Post']['created']);
- unset($result[0]['Post']['updated'], $result[0]['Post']['created']);
- unset($result[1]['Post']['updated'], $result[1]['Post']['created']);
- $this->assertEquals($expected, $result);
- }
- /**
- * Test SaveAssociated with Habtm relations
- *
- * @return void
- */
- public function testSaveAssociatedHabtm() {
- $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
- $data = array(
- 'Article' => array(
- 'user_id' => 1,
- 'title' => 'Article Has and belongs to Many Tags'
- ),
- 'Tag' => array(
- 'Tag' => array(1, 2)
- ),
- 'Comment' => array(
- array(
- 'comment' => 'Article comment',
- 'user_id' => 1
- )));
- $Article = new Article();
- $result = $Article->saveAssociated($data);
- $this->assertFalse(empty($result));
- $result = $Article->read();
- $this->assertEquals(2, count($result['Tag']));
- $this->assertEquals('tag1', $result['Tag'][0]['tag']);
- $this->assertEquals(1, count($result['Comment']));
- $this->assertEquals(1, count($result['Comment'][0]['comment']));
- }
- /**
- * Test SaveAssociated with Habtm relations and extra join table fields
- *
- * @return void
- */
- public function testSaveAssociatedHabtmWithExtraJoinTableFields() {
- $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
- $data = array(
- 'Something' => array(
- 'id' => 4,
- 'title' => 'Extra Fields',
- 'body' => 'Extra Fields Body',
- 'published' => '1'
- ),
- 'SomethingElse' => array(
- array('something_else_id' => 1, 'doomed' => '1'),
- array('something_else_id' => 2, 'doomed' => '0'),
- array('something_else_id' => 3, 'doomed' => '1')
- )
- );
- $Something = new Something();
- $result = $Something->saveAssociated($data);
- $this->assertFalse(empty($result));
- $result = $Something->read();
- $this->assertEquals(3, count($result['SomethingElse']));
- $this->assertTrue(Set::matches('/Something[id=4]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
- }
- /**
- * testSaveAssociatedHasOne method
- *
- * @return void
- */
- public function testSaveAssociatedHasOne() {
- $model = new Comment();
- $model->deleteAll(true);
- $this->assertEquals(array(), $model->find('all'));
- $model->Attachment->deleteAll(true);
- $this->assertEquals(array(), $model->Attachment->find('all'));
- $this->assertTrue($model->saveAssociated(array(
- 'Comment' => array(
- 'comment' => 'Comment with attachment',
- 'article_id' => 1,
- 'user_id' => 1
- ),
- 'Attachment' => array(
- 'attachment' => 'some_file.zip'
- ))));
- $result = $model->find('all', array('fields' => array(
- 'Comment.id', 'Comment.comment', 'Attachment.id',
- 'Attachment.comment_id', 'Attachment.attachment'
- )));
- $expected = array(array(
- 'Comment' => array(
- 'id' => '1',
- 'comment' => 'Comment with attachment'
- ),
- 'Attachment' => array(
- 'id' => '1',
- 'comment_id' => '1',
- 'attachment' => 'some_file.zip'
- )));
- $this->assertEquals($expected, $result);
- $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
- $data = array(
- 'Comment' => array(
- 'comment' => 'Comment with attachment',
- 'article_id' => 1,
- 'user_id' => 1
- ),
- 'Attachment' => array(
- 'attachment' => 'some_file.zip'
- ));
- $this->assertTrue($model->saveAssociated($data, array('validate' => 'first')));
- }
- /**
- * testSaveAssociatedBelongsTo method
- *
- * @return void
- */
- public function testSaveAssociatedBelongsTo() {
- $model = new Comment();
- $model->deleteAll(true);
- $this->assertEquals(array(), $model->find('all'));
- $model->Article->deleteAll(true);
- $this->assertEquals(array(), $model->Article->find('all'));
- $this->assertTrue($model->saveAssociated(array(
- 'Comment' => array(
- 'comment' => 'Article comment',
- 'article_id' => 1,
- 'user_id' => 1
- ),
- 'Article' => array(
- 'title' => 'Model Associations 101',
- 'user_id' => 1
- ))));
- $result = $model->find('all', array('fields' => array(
- 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
- )));
- $expected = array(array(
- 'Comment' => array(
- 'id' => '1',
- 'article_id' => '1',
- 'comment' => 'Article comment'
- ),
- 'Article' => array(
- 'id' => '1',
- 'title' => 'Model Associations 101'
- )));
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAssociatedHasOneValidation method
- *
- * @return void
- */
- public function testSaveAssociatedHasOneValidation() {
- $model = new Comment();
- $model->deleteAll(true);
- $this->assertEquals(array(), $model->find('all'));
- $model->Attachment->deleteAll(true);
- $this->assertEquals(array(), $model->Attachment->find('all'));
- $model->validate = array('comment' => 'notEmpty');
- $model->Attachment->validate = array('attachment' => 'notEmpty');
- $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
- $result = $model->saveAssociated(
- array(
- 'Comment' => array(
- 'comment' => '',
- 'article_id' => 1,
- 'user_id' => 1
- ),
- 'Attachment' => array('attachment' => '')
- )
- );
- $this->assertFalse($result);
- $expected = array(
- 'comment' => array(
- 'This field cannot be left blank'
- ),
- 'Attachment' => array(
- 'attachment' => array(
- 'This field cannot be left blank'
- )
- )
- );
- $this->assertEquals($expected, $model->validationErrors);
- $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
- }
- /**
- * testSaveAssociatedAtomic method
- *
- * @return void
- */
- public function testSaveAssociatedAtomic() {
- $this->loadFixtures('Article', 'User');
- $TestModel = new Article();
- $result = $TestModel->saveAssociated(array(
- 'Article' => array(
- 'title' => 'Post with Author',
- 'body' => 'This post will be saved with an author',
- 'user_id' => 2
- ),
- 'Comment' => array(
- array('comment' => 'First new comment', 'user_id' => 2))
- ), array('atomic' => false));
- $this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
- $result = $TestModel->saveAssociated(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'comment' => 'First new comment',
- 'published' => 'Y',
- 'user_id' => 1
- ),
- array(
- 'comment' => 'Second new comment',
- 'published' => 'Y',
- 'user_id' => 2
- ))
- ), array('validate' => true, 'atomic' => false));
- $this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
- }
- /**
- * testSaveManyAtomic method
- *
- * @return void
- */
- public function testSaveManyAtomic() {
- $this->loadFixtures('Article', 'User');
- $TestModel = new Article();
- $result = $TestModel->saveMany(array(
- array(
- 'id' => '1',
- 'title' => 'Baleeted First Post',
- 'body' => 'Baleeted!',
- 'published' => 'N'
- ),
- array(
- 'id' => '2',
- 'title' => 'Just update the title'
- ),
- array(
- 'title' => 'Creating a fourth post',
- 'body' => 'Fourth post body',
- 'user_id' => 2
- )
- ), array('atomic' => false));
- $this->assertSame($result, array(true, true, true));
- $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
- $result = $TestModel->saveMany(array(
- array(
- 'id' => '1',
- 'title' => 'Un-Baleeted First Post',
- 'body' => 'Not Baleeted!',
- 'published' => 'Y'
- ),
- array(
- 'id' => '2',
- 'title' => '',
- 'body' => 'Trying to get away with an empty title'
- )
- ), array('validate' => true, 'atomic' => false));
- $this->assertSame(array(true, false), $result);
- }
- /**
- * testSaveAssociatedHasMany method
- *
- * @return void
- */
- public function testSaveAssociatedHasMany() {
- $this->loadFixtures('Article', 'Comment');
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
- $result = $TestModel->saveAssociated(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- ));
- $this->assertFalse(empty($result));
- $result = $TestModel->findById(2);
- $expected = array(
- 'First Comment for Second Article',
- 'Second Comment for Second Article',
- 'First new comment',
- 'Second new comment'
- );
- $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
- $result = $TestModel->saveAssociated(
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'comment' => 'Third new comment',
- 'published' => 'Y',
- 'user_id' => 1
- ))),
- array('atomic' => false)
- );
- $this->assertFalse(empty($result));
- $result = $TestModel->findById(2);
- $expected = array(
- 'First Comment for Second Article',
- 'Second Comment for Second Article',
- 'First new comment',
- 'Second new comment',
- 'Third new comment'
- );
- $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
- $TestModel->beforeSaveReturn = false;
- $result = $TestModel->saveAssociated(
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'comment' => 'Fourth new comment',
- 'published' => 'Y',
- 'user_id' => 1
- ))),
- array('atomic' => false)
- );
- $this->assertEquals(array('Article' => false), $result);
- $result = $TestModel->findById(2);
- $expected = array(
- 'First Comment for Second Article',
- 'Second Comment for Second Article',
- 'First new comment',
- 'Second new comment',
- 'Third new comment'
- );
- $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
- }
- /**
- * testSaveAssociatedHasManyEmpty method
- *
- * @return void
- */
- public function testSaveAssociatedHasManyEmpty() {
- $this->loadFixtures('Article', 'Comment');
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
- $TestModel->validate = $TestModel->Comment->validate = array('user_id' => array('notEmpty' => array('rule' => 'notEmpty', 'required' => true)));
- //empty hasMany data is ignored in save
- $result = $TestModel->saveAssociated(array(
- 'Article' => array('title' => 'title', 'user_id' => 1),
- 'Comment' => array()
- ), array('validate' => true));
- $this->assertTrue($result);
- $result = $TestModel->saveAssociated(array(
- 'Article' => array('title' => 'title', 'user_id' => 1),
- 'Comment' => array()
- ), array('validate' => true, 'atomic' => false));
- $this->assertEquals(array('Article' => true), $result);
- //empty primary data is not ignored
- $result = $TestModel->saveAssociated(array('Article' => array()), array('validate' => true));
- $this->assertFalse($result);
- $result = $TestModel->saveAssociated(array('Article' => array()), array('validate' => true, 'atomic' => false));
- $this->assertEquals(array('Article' => false), $result);
- }
- /**
- * testSaveAssociatedHasManyValidation method
- *
- * @return void
- */
- public function testSaveAssociatedHasManyValidation() {
- $this->loadFixtures('Article', 'Comment');
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
- $TestModel->Comment->validate = array('comment' => 'notEmpty');
- $result = $TestModel->saveAssociated(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => '', 'published' => 'Y', 'user_id' => 1),
- )
- ), array('validate' => true));
- $this->assertFalse($result);
- $expected = array('Comment' => array(
- array('comment' => array('This field cannot be left blank'))
- ));
- $this->assertEquals($expected, $TestModel->validationErrors);
- $expected = array(
- array('comment' => array('This field cannot be left blank'))
- );
- $this->assertEquals($expected, $TestModel->Comment->validationErrors);
- $result = $TestModel->saveAssociated(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'comment' => '',
- 'published' => 'Y',
- 'user_id' => 1
- ))
- ), array('validate' => 'first'));
- $this->assertFalse($result);
- }
- /**
- * test saveMany with transactions and ensure there is no missing rollback.
- *
- * @return void
- */
- public function testSaveManyTransactionNoRollback() {
- $this->loadFixtures('Post');
- $this->getMock('DboSource', array('connect', 'rollback', 'describe'), array(), 'MockManyTransactionDboSource');
- $db = ConnectionManager::create('mock_many_transaction', array(
- 'datasource' => 'MockManyTransactionDboSource',
- ));
- $db->expects($this->once())
- ->method('describe')
- ->will($this->returnValue(array()));
- $db->expects($this->once())->method('rollback');
- $Post = new Post('mock_many_transaction');
- $Post->validate = array(
- 'title' => array('rule' => array('notEmpty'))
- );
- $data = array(
- array('author_id' => 1, 'title' => 'New Fourth Post'),
- array('author_id' => 1, 'title' => '')
- );
- $Post->saveMany($data);
- }
- /**
- * test saveAssociated with transactions and ensure there is no missing rollback.
- *
- * @return void
- */
- public function testSaveAssociatedTransactionNoRollback() {
- $testDb = ConnectionManager::getDataSource('test');
- $this->getMock(
- 'DboSource',
- array('connect', 'rollback', 'describe', 'create', 'begin'),
- array(),
- 'MockAssociatedTransactionDboSource',
- false
- );
- $db = ConnectionManager::create('mock_assoc_transaction', array(
- 'datasource' => 'MockAssociatedTransactionDboSource',
- ));
- $this->mockObjects[] = $db;
- $db->columns = $testDb->columns;
- $db->expects($this->once())->method('rollback');
- $db->expects($this->any())->method('describe')
- ->will($this->returnValue(array(
- 'id' => array('type' => 'integer', 'length' => 11),
- 'title' => array('type' => 'string'),
- 'body' => array('type' => 'text'),
- 'published' => array('type' => 'string')
- )));
- $Post = new Post();
- $Post->useDbConfig = 'mock_assoc_transaction';
- $Post->Author->useDbConfig = 'mock_assoc_transaction';
- $Post->Author->validate = array(
- 'user' => array('rule' => array('notEmpty'))
- );
- $data = array(
- 'Post' => array(
- 'title' => 'New post',
- 'body' => 'Content',
- 'published' => 'Y'
- ),
- 'Author' => array(
- 'user' => '',
- 'password' => "sekret"
- )
- );
- $Post->saveAssociated($data, array('validate' => true, 'atomic' => true));
- }
- /**
- * test saveMany with nested saveMany call.
- *
- * @return void
- */
- public function testSaveManyNestedSaveMany() {
- $this->loadFixtures('Sample');
- $TransactionManyTestModel = new TransactionManyTestModel();
- $data = array(
- array('apple_id' => 1, 'name' => 'sample5'),
- );
- $this->assertTrue($TransactionManyTestModel->saveMany($data, array('atomic' => true)));
- }
- /**
- * testSaveManyTransaction method
- *
- * @return void
- */
- public function testSaveManyTransaction() {
- $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
- $TestModel = new Post();
- $TestModel->validate = array('title' => 'notEmpty');
- $data = array(
- array('author_id' => 1, 'title' => 'New Fourth Post'),
- array('author_id' => 1, 'title' => 'New Fifth Post'),
- array('author_id' => 1, 'title' => '')
- );
- $this->assertFalse($TestModel->saveMany($data));
- $result = $TestModel->find('all', array('recursive' => -1));
- $expected = array(
- array('Post' => array(
- 'id' => '1',
- 'author_id' => 1,
- 'title' => 'First Post',
- 'body' => 'First Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:39:23',
- 'updated' => '2007-03-18 10:41:31'
- )),
- array('Post' => array(
- 'id' => '2',
- 'author_id' => 3,
- 'title' => 'Second Post',
- 'body' => 'Second Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:41:23',
- 'updated' => '2007-03-18 10:43:31'
- )),
- array('Post' => array(
- 'id' => '3',
- 'author_id' => 1,
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:43:23',
- 'updated' => '2007-03-18 10:45:31'
- )));
- if (count($result) != 3) {
- // Database doesn't support transactions
- $expected[] = array(
- 'Post' => array(
- 'id' => '4',
- 'author_id' => 1,
- 'title' => 'New Fourth Post',
- 'body' => null,
- 'published' => 'N'
- ));
- $expected[] = array(
- 'Post' => array(
- 'id' => '5',
- 'author_id' => 1,
- 'title' => 'New Fifth Post',
- 'body' => null,
- 'published' => 'N',
- ));
- $this->assertEquals(self::date(), $result[3]['Post']['created']);
- $this->assertEquals(self::date(), $result[3]['Post']['updated']);
- $this->assertEquals(self::date(), $result[4]['Post']['created']);
- $this->assertEquals(self::date(), $result[4]['Post']['updated']);
- unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
- unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
- $this->assertEquals($expected, $result);
- // Skip the rest of the transactional tests
- return;
- }
- $this->assertEquals($expected, $result);
- $data = array(
- array('author_id' => 1, 'title' => 'New Fourth Post'),
- array('author_id' => 1, 'title' => ''),
- array('author_id' => 1, 'title' => 'New Sixth Post')
- );
- $this->assertFalse($TestModel->saveMany($data));
- $result = $TestModel->find('all', array('recursive' => -1));
- $expected = array(
- array('Post' => array(
- 'id' => '1',
- 'author_id' => 1,
- 'title' => 'First Post',
- 'body' => 'First Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:39:23',
- 'updated' => '2007-03-18 10:41:31'
- )),
- array('Post' => array(
- 'id' => '2',
- 'author_id' => 3,
- 'title' => 'Second Post',
- 'body' => 'Second Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:41:23',
- 'updated' => '2007-03-18 10:43:31'
- )),
- array('Post' => array(
- 'id' => '3',
- 'author_id' => 1,
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:43:23',
- 'updated' => '2007-03-18 10:45:31'
- )));
- if (count($result) != 3) {
- // Database doesn't support transactions
- $expected[] = array(
- 'Post' => array(
- 'id' => '4',
- 'author_id' => 1,
- 'title' => 'New Fourth Post',
- 'body' => 'Third Post Body',
- 'published' => 'N'
- ));
- $expected[] = array(
- 'Post' => array(
- 'id' => '5',
- 'author_id' => 1,
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'N'
- ));
- $this->assertEquals(self::date(), $result[3]['Post']['created']);
- $this->assertEquals(self::date(), $result[3]['Post']['updated']);
- $this->assertEquals(self::date(), $result[4]['Post']['created']);
- $this->assertEquals(self::date(), $result[4]['Post']['updated']);
- unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
- unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
- }
- $this->assertEquals($expected, $result);
- $TestModel->validate = array('title' => 'notEmpty');
- $data = array(
- array('author_id' => 1, 'title' => 'New Fourth Post'),
- array('author_id' => 1, 'title' => 'New Fifth Post'),
- array('author_id' => 1, 'title' => 'New Sixth Post')
- );
- $this->assertTrue($TestModel->saveMany($data));
- $result = $TestModel->find('all', array(
- 'recursive' => -1,
- 'fields' => array('author_id', 'title', 'body', 'published'),
- 'order' => array('Post.created' => 'ASC')
- ));
- $expected = array(
- array('Post' => array(
- 'author_id' => 1,
- 'title' => 'First Post',
- 'body' => 'First Post Body',
- 'published' => 'Y'
- )),
- array('Post' => array(
- 'author_id' => 3,
- 'title' => 'Second Post',
- 'body' => 'Second Post Body',
- 'published' => 'Y'
- )),
- array('Post' => array(
- 'author_id' => 1,
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'Y'
- )),
- array('Post' => array(
- 'author_id' => 1,
- 'title' => 'New Fourth Post',
- 'body' => '',
- 'published' => 'N'
- )),
- array('Post' => array(
- 'author_id' => 1,
- 'title' => 'New Fifth Post',
- 'body' => '',
- 'published' => 'N'
- )),
- array('Post' => array(
- 'author_id' => 1,
- 'title' => 'New Sixth Post',
- 'body' => '',
- 'published' => 'N'
- )));
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveManyValidation method
- *
- * @return void
- */
- public function testSaveManyValidation() {
- $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
- $TestModel = new Post();
- $data = array(
- array(
- 'id' => '1',
- 'title' => 'Baleeted First Post',
- 'body' => 'Baleeted!',
- 'published' => 'N'
- ),
- array(
- 'id' => '2',
- 'title' => 'Just update the title'
- ),
- array(
- 'title' => 'Creating a fourth post',
- 'body' => 'Fourth post body',
- 'author_id' => 2
- ));
- $this->assertTrue($TestModel->saveMany($data));
- $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
- $expected = array(
- array(
- 'Post' => array(
- 'id' => '1',
- 'author_id' => '1',
- 'title' => 'Baleeted First Post',
- 'body' => 'Baleeted!',
- 'published' => 'N',
- 'created' => '2007-03-18 10:39:23'
- )
- ),
- array(
- 'Post' => array(
- 'id' => '2',
- 'author_id' => '3',
- 'title' => 'Just update the title',
- 'body' => 'Second Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:41:23'
- )
- ),
- array(
- 'Post' => array(
- 'id' => '3',
- 'author_id' => '1',
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:43:23',
- 'updated' => '2007-03-18 10:45:31'
- )),
- array(
- 'Post' => array(
- 'id' => '4',
- 'author_id' => '2',
- 'title' => 'Creating a fourth post',
- 'body' => 'Fourth post body',
- 'published' => 'N'
- )
- )
- );
- $this->assertEquals(self::date(), $result[0]['Post']['updated']);
- $this->assertEquals(self::date(), $result[1]['Post']['updated']);
- $this->assertEquals(self::date(), $result[3]['Post']['created']);
- $this->assertEquals(self::date(), $result[3]['Post']['updated']);
- unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
- unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
- $this->assertEquals($expected, $result);
- $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
- $data = array(
- array(
- 'id' => '1',
- 'title' => 'Un-Baleeted First Post',
- 'body' => 'Not Baleeted!',
- 'published' => 'Y'
- ),
- array(
- 'id' => '2',
- 'title' => '',
- 'body' => 'Trying to get away with an empty title'
- ));
- $result = $TestModel->saveMany($data);
- $this->assertFalse($result);
- $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
- $errors = array(1 => array('title' => array('This field cannot be left blank')));
- $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
- if (!$transactionWorked) {
- $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
- $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
- }
- $this->assertEquals($errors, $TestModel->validationErrors);
- $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
- $data = array(
- array(
- 'id' => '1',
- 'title' => 'Un-Baleeted First Post',
- 'body' => 'Not Baleeted!',
- 'published' => 'Y'
- ),
- array(
- 'id' => '2',
- 'title' => '',
- 'body' => 'Trying to get away with an empty title'
- ));
- $result = $TestModel->saveMany($data, array('validate' => true, 'atomic' => false));
- $this->assertEquals(array(true, false), $result);
- $result = $TestModel->find('all', array(
- 'fields' => array('id', 'author_id', 'title', 'body', 'published'),
- 'recursive' => -1,
- 'order' => 'Post.id ASC'
- ));
- $errors = array(1 => array('title' => array('This field cannot be left blank')));
- $expected = array(
- array(
- 'Post' => array(
- 'id' => '1',
- 'author_id' => '1',
- 'title' => 'Un-Baleeted First Post',
- 'body' => 'Not Baleeted!',
- 'published' => 'Y',
- )),
- array(
- 'Post' => array(
- 'id' => '2',
- 'author_id' => '3',
- 'title' => 'Just update the title',
- 'body' => 'Second Post Body',
- 'published' => 'Y',
- )),
- array(
- 'Post' => array(
- 'id' => '3',
- 'author_id' => '1',
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'Y',
- )),
- array(
- 'Post' => array(
- 'id' => '4',
- 'author_id' => '2',
- 'title' => 'Creating a fourth post',
- 'body' => 'Fourth post body',
- 'published' => 'N',
- )));
- $this->assertEquals($expected, $result);
- $this->assertEquals($errors, $TestModel->validationErrors);
- $data = array(
- array(
- 'id' => '1',
- 'title' => 'Re-Baleeted First Post',
- 'body' => 'Baleeted!',
- 'published' => 'N'
- ),
- array(
- 'id' => '2',
- 'title' => '',
- 'body' => 'Trying to get away with an empty title'
- ));
- $this->assertFalse($TestModel->saveMany($data, array('validate' => 'first')));
- $result = $TestModel->find('all', array(
- 'fields' => array('id', 'author_id', 'title', 'body', 'published'),
- 'recursive' => -1,
- 'order' => 'Post.id ASC'
- ));
- $this->assertEquals($expected, $result);
- $this->assertEquals($errors, $TestModel->validationErrors);
- }
- /**
- * testValidateMany method
- *
- * @return void
- */
- public function testValidateMany() {
- $TestModel = new Article();
- $TestModel->validate = array('title' => 'notEmpty');
- $data = array(
- 0 => array('title' => ''),
- 1 => array('title' => 'title 1'),
- 2 => array('title' => 'title 2'),
- );
- $result = $TestModel->validateMany($data);
- $this->assertFalse($result);
- $expected = array(
- 0 => array('title' => array('This field cannot be left blank')),
- );
- $this->assertEquals($expected, $TestModel->validationErrors);
- $data = array(
- 0 => array('title' => 'title 0'),
- 1 => array('title' => ''),
- 2 => array('title' => 'title 2'),
- );
- $result = $TestModel->validateMany($data);
- $this->assertFalse($result);
- $expected = array(
- 1 => array('title' => array('This field cannot be left blank')),
- );
- $this->assertEquals($expected, $TestModel->validationErrors);
- }
- /**
- * testSaveAssociatedValidateFirst method
- *
- * @return void
- */
- public function testSaveAssociatedValidateFirst() {
- $this->loadFixtures('Article', 'Comment', 'Attachment');
- $model = new Article();
- $model->deleteAll(true);
- $model->Comment->validate = array('comment' => 'notEmpty');
- $result = $model->saveAssociated(array(
- 'Article' => array(
- 'title' => 'Post with Author',
- 'body' => 'This post will be saved author'
- ),
- 'Comment' => array(
- array('comment' => 'First new comment'),
- array('comment' => '')
- )
- ), array('validate' => 'first'));
- $this->assertFalse($result);
- $result = $model->find('all');
- $this->assertSame(array(), $result);
- $expected = array('Comment' => array(
- 1 => array('comment' => array('This field cannot be left blank'))
- ));
- $this->assertEquals($expected['Comment'], $model->Comment->validationErrors);
- $this->assertSame($model->Comment->find('count'), 0);
- $result = $model->saveAssociated(
- array(
- 'Article' => array(
- 'title' => 'Post with Author',
- 'body' => 'This post will be saved with an author',
- 'user_id' => 2
- ),
- 'Comment' => array(
- array(
- 'comment' => 'Only new comment',
- 'user_id' => 2
- ))),
- array('validate' => 'first')
- );
- $this->assertSame($result, true);
- $result = $model->Comment->find('all');
- $this->assertSame(count($result), 1);
- $result = Hash::extract($result, '{n}.Comment.article_id');
- $this->assertEquals(4, $result[0]);
- $model->deleteAll(true);
- $data = array(
- 'Article' => array(
- 'title' => 'Post with Author saveAlled from comment',
- 'body' => 'This post will be saved with an author',
- 'user_id' => 2
- ),
- 'Comment' => array(
- 'comment' => 'Only new comment', 'user_id' => 2
- ));
- $result = $model->Comment->saveAssociated($data, array('validate' => 'first'));
- $this->assertFalse(empty($result));
- $result = $model->find('all');
- $this->assertEquals(
- 'Post with Author saveAlled from comment',
- $result[0]['Article']['title']
- );
- $this->assertEquals('Only new comment', $result[0]['Comment'][0]['comment']);
- }
- /**
- * test saveMany()'s return is correct when using atomic = false and validate = first.
- *
- * @return void
- */
- public function testSaveManyValidateFirstAtomicFalse() {
- $Something = new Something();
- $invalidData = array(
- array(
- 'title' => 'foo',
- 'body' => 'bar',
- 'published' => 'baz',
- ),
- array(
- 'body' => 3,
- 'published' => 'sd',
- ),
- );
- $Something->create();
- $Something->validate = array(
- 'title' => array(
- 'rule' => 'alphaNumeric',
- 'required' => true,
- ),
- 'body' => array(
- 'rule' => 'alphaNumeric',
- 'required' => true,
- 'allowEmpty' => true,
- ),
- );
- $result = $Something->saveMany($invalidData, array(
- 'atomic' => false,
- 'validate' => 'first',
- ));
- $expected = array(true, false);
- $this->assertEquals($expected, $result);
- $Something = new Something();
- $validData = array(
- array(
- 'title' => 'title value',
- 'body' => 'body value',
- 'published' => 'baz',
- ),
- array(
- 'title' => 'valid',
- 'body' => 'this body',
- 'published' => 'sd',
- ),
- );
- $Something->create();
- $result = $Something->saveMany($validData, array(
- 'atomic' => false,
- 'validate' => 'first',
- ));
- $expected = array(true, true);
- $this->assertEquals($expected, $result);
- }
- /**
- * testValidateAssociated method
- *
- * @return void
- */
- public function testValidateAssociated() {
- $this->loadFixtures('Attachment', 'Article', 'Comment');
- $TestModel = new Comment();
- $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
- $data = array(
- 'Comment' => array(
- 'comment' => 'This is the comment'
- ),
- 'Attachment' => array(
- 'attachment' => ''
- )
- );
- $result = $TestModel->validateAssociated($data);
- $this->assertFalse($result);
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
- $TestModel->Comment->validate = array('comment' => 'notEmpty');
- $data = array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'id' => 1,
- 'comment' => '',
- 'published' => 'Y',
- 'user_id' => 1),
- array(
- 'id' => 2,
- 'comment' =>
- 'comment',
- 'published' => 'Y',
- 'user_id' => 1
- )));
- $result = $TestModel->validateAssociated($data);
- $this->assertFalse($result);
- $data = array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'id' => 1,
- 'comment' => '',
- 'published' => 'Y',
- 'user_id' => 1
- ),
- array(
- 'id' => 2,
- 'comment' => 'comment',
- 'published' => 'Y',
- 'user_id' => 1
- ),
- array(
- 'id' => 3,
- 'comment' => '',
- 'published' => 'Y',
- 'user_id' => 1
- )));
- $result = $TestModel->validateAssociated($data, array(
- 'atomic' => false
- ));
- $expected = array(
- 'Article' => true,
- 'Comment' => array(false, true, false)
- );
- $this->assertSame($expected, $result);
- $expected = array('Comment' => array(
- 0 => array('comment' => array('This field cannot be left blank')),
- 2 => array('comment' => array('This field cannot be left blank'))
- ));
- $this->assertEquals($expected, $TestModel->validationErrors);
- $expected = array(
- 0 => array('comment' => array('This field cannot be left blank')),
- 2 => array('comment' => array('This field cannot be left blank'))
- );
- $this->assertEquals($expected, $TestModel->Comment->validationErrors);
- }
- /**
- * test that saveMany behaves like plain save() when suplied empty data
- *
- * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
- * @return void
- */
- public function testSaveManyEmptyData() {
- $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
- $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
- $model = new Article();
- $result = $model->saveMany(array(), array('validate' => true));
- $this->assertFalse(empty($result));
- $model = new ProductUpdateAll();
- $result = $model->saveMany(array());
- $this->assertFalse($result);
- }
- /**
- * test that saveAssociated behaves like plain save() when supplied empty data
- *
- * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
- * @return void
- */
- public function testSaveAssociatedEmptyData() {
- $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
- $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
- $model = new Article();
- $result = $model->saveAssociated(array(), array('validate' => true));
- $this->assertFalse(empty($result));
- $model = new ProductUpdateAll();
- $result = $model->saveAssociated(array());
- $this->assertFalse($result);
- }
- /**
- * testUpdateWithCalculation method
- *
- * @return void
- */
- public function testUpdateWithCalculation() {
- $this->loadFixtures('DataTest');
- $model = new DataTest();
- $model->deleteAll(true);
- $result = $model->saveMany(array(
- array('count' => 5, 'float' => 1.1),
- array('count' => 3, 'float' => 1.2),
- array('count' => 4, 'float' => 1.3),
- array('count' => 1, 'float' => 2.0),
- ));
- $this->assertFalse(empty($result));
- $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
- $this->assertEquals(array(5, 3, 4, 1), $result);
- $this->assertTrue($model->updateAll(array('count' => 'count + 2')));
- $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
- $this->assertEquals(array(7, 5, 6, 3), $result);
- $this->assertTrue($model->updateAll(array('DataTest.count' => 'DataTest.count - 1')));
- $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
- $this->assertEquals(array(6, 4, 5, 2), $result);
- }
- public function testToggleBoolFields() {
- $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
- $Post = new CounterCachePost();
- $Post->unbindModel(array('belongsTo' => array('User')), true);
- $true = array('Post' => array('published' => true, 'id' => 2));
- $false = array('Post' => array('published' => false, 'id' => 2));
- $fields = array('Post.published', 'Post.id');
- $updateConditions = array('Post.id' => 2);
- // check its true
- $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
- $this->assertEquals($true, $result);
- // Testing without the alias
- $this->assertTrue($Post->updateAll(array('published' => 'NOT published'), $updateConditions));
- $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
- $this->assertEquals($false, $result);
- $this->assertTrue($Post->updateAll(array('published' => 'NOT published'), $updateConditions));
- $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
- $this->assertEquals($true, $result);
- $db = ConnectionManager::getDataSource('test');
- $alias = $db->name('Post.published');
- // Testing with the alias
- $this->assertTrue($Post->updateAll(array('Post.published' => "NOT $alias"), $updateConditions));
- $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
- $this->assertEquals($false, $result);
- $this->assertTrue($Post->updateAll(array('Post.published' => "NOT $alias"), $updateConditions));
- $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
- $this->assertEquals($true, $result);
- }
- /**
- * TestFindAllWithoutForeignKey
- *
- * @return void
- */
- public function testFindAllForeignKey() {
- $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
- $ProductUpdateAll = new ProductUpdateAll();
- $conditions = array('Group.name' => 'group one');
- $ProductUpdateAll->bindModel(array(
- 'belongsTo' => array(
- 'Group' => array('className' => 'GroupUpdateAll')
- )
- ));
- $ProductUpdateAll->belongsTo = array(
- 'Group' => array('className' => 'GroupUpdateAll', 'foreignKey' => 'group_id')
- );
- $results = $ProductUpdateAll->find('all', compact('conditions'));
- $this->assertTrue(!empty($results));
- $ProductUpdateAll->bindModel(array('belongsTo' => array('Group')));
- $ProductUpdateAll->belongsTo = array(
- 'Group' => array(
- 'className' => 'GroupUpdateAll',
- 'foreignKey' => false,
- 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
- ));
- $resultsFkFalse = $ProductUpdateAll->find('all', compact('conditions'));
- $this->assertTrue(!empty($resultsFkFalse));
- $expected = array(
- '0' => array(
- 'ProductUpdateAll' => array(
- 'id' => 1,
- 'name' => 'product one',
- 'groupcode' => 120,
- 'group_id' => 1),
- 'Group' => array(
- 'id' => 1,
- 'name' => 'group one',
- 'code' => 120)
- ),
- '1' => array(
- 'ProductUpdateAll' => array(
- 'id' => 2,
- 'name' => 'product two',
- 'groupcode' => 120,
- 'group_id' => 1),
- 'Group' => array(
- 'id' => 1,
- 'name' => 'group one',
- 'code' => 120)
- )
- );
- $this->assertEquals($expected, $results);
- $this->assertEquals($expected, $resultsFkFalse);
- }
- /**
- * test updateAll with empty values.
- *
- * @return void
- */
- public function testUpdateAllEmptyValues() {
- $this->skipIf($this->db instanceof Sqlserver || $this->db instanceof Postgres, 'This test is not compatible with Postgres or SQL Server.');
- $this->loadFixtures('Author', 'Post');
- $model = new Author();
- $result = $model->updateAll(array('user' => '""'));
- $this->assertTrue($result);
- }
- /**
- * testUpdateAllWithJoins
- *
- * @return void
- */
- public function testUpdateAllWithJoins() {
- $this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql or sqlite');
- $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
- $ProductUpdateAll = new ProductUpdateAll();
- $conditions = array('Group.name' => 'group one');
- $ProductUpdateAll->bindModel(array('belongsTo' => array(
- 'Group' => array('className' => 'GroupUpdateAll')))
- );
- $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
- $results = $ProductUpdateAll->find('all', array(
- 'conditions' => array('ProductUpdateAll.name' => 'new product')
- ));
- $expected = array(
- '0' => array(
- 'ProductUpdateAll' => array(
- 'id' => 1,
- 'name' => 'new product',
- 'groupcode' => 120,
- 'group_id' => 1),
- 'Group' => array(
- 'id' => 1,
- 'name' => 'group one',
- 'code' => 120)
- ),
- '1' => array(
- 'ProductUpdateAll' => array(
- 'id' => 2,
- 'name' => 'new product',
- 'groupcode' => 120,
- 'group_id' => 1),
- 'Group' => array(
- 'id' => 1,
- 'name' => 'group one',
- 'code' => 120)));
- $this->assertEquals($expected, $results);
- }
- /**
- * testUpdateAllWithoutForeignKey
- *
- * @return void
- */
- public function testUpdateAllWithoutForeignKey() {
- $this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql');
- $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
- $ProductUpdateAll = new ProductUpdateAll();
- $conditions = array('Group.name' => 'group one');
- $ProductUpdateAll->bindModel(array('belongsTo' => array(
- 'Group' => array('className' => 'GroupUpdateAll')
- )));
- $ProductUpdateAll->belongsTo = array(
- 'Group' => array(
- 'className' => 'GroupUpdateAll',
- 'foreignKey' => false,
- 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
- )
- );
- $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
- $resultsFkFalse = $ProductUpdateAll->find('all', array('conditions' => array('ProductUpdateAll.name' => 'new product')));
- $expected = array(
- '0' => array(
- 'ProductUpdateAll' => array(
- 'id' => 1,
- 'name' => 'new product',
- 'groupcode' => 120,
- 'group_id' => 1),
- 'Group' => array(
- 'id' => 1,
- 'name' => 'group one',
- 'code' => 120)
- ),
- '1' => array(
- 'ProductUpdateAll' => array(
- 'id' => 2,
- 'name' => 'new product',
- 'groupcode' => 120,
- 'group_id' => 1),
- 'Group' => array(
- 'id' => 1,
- 'name' => 'group one',
- 'code' => 120)));
- $this->assertEquals($expected, $resultsFkFalse);
- }
- /**
- * test writing floats in german locale.
- *
- * @return void
- */
- public function testWriteFloatAsGerman() {
- $restore = setlocale(LC_NUMERIC, 0);
- setlocale(LC_NUMERIC, 'de_DE');
- $model = new DataTest();
- $result = $model->save(array(
- 'count' => 1,
- 'float' => 3.14593
- ));
- $this->assertTrue((bool)$result);
- setlocale(LC_NUMERIC, $restore);
- }
- /**
- * Test returned array contains primary key when save creates a new record
- *
- * @return void
- */
- public function testPkInReturnArrayForCreate() {
- $this->loadFixtures('Article');
- $TestModel = new Article();
- $data = array('Article' => array(
- 'user_id' => '1',
- 'title' => 'Fourth Article',
- 'body' => 'Fourth Article Body',
- 'published' => 'Y'
- ));
- $result = $TestModel->save($data);
- $this->assertSame($result['Article']['id'], $TestModel->id);
- }
- /**
- * testSaveAllFieldListValidateBelongsTo
- *
- * @return void
- */
- public function testSaveAllFieldListValidateBelongsTo() {
- $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
- $TestModel = new Post();
- $result = $TestModel->find('all');
- $this->assertEquals(3, count($result));
- $this->assertFalse(isset($result[3]));
- // test belongsTo
- $fieldList = array(
- 'Post' => array('title'),
- 'Author' => array('user')
- );
- $data = array(
- 'Post' => array(
- 'title' => 'Post without body',
- 'body' => 'This will not be saved',
- ),
- 'Author' => array(
- 'user' => 'bob',
- 'test' => 'This will not be saved',
- ));
- $TestModel->saveAll($data, array('fieldList' => $fieldList));
- $result = $TestModel->find('all', array(
- 'order' => 'Post.id ASC',
- ));
- $expected = array(
- 'Post' => array (
- 'id' => '4',
- 'author_id' => '5',
- 'title' => 'Post without body',
- 'body' => null,
- 'published' => 'N',
- 'created' => self::date(),
- 'updated' => self::date(),
- ),
- 'Author' => array (
- 'id' => '5',
- 'user' => 'bob',
- 'password' => null,
- 'created' => self::date(),
- 'updated' => self::date(),
- 'test' => 'working',
- ),
- );
- $this->assertEquals($expected, $result[3]);
- $this->assertEquals(4, count($result));
- $this->assertEquals('', $result[3]['Post']['body']);
- $this->assertEquals('working', $result[3]['Author']['test']);
- // test multirecord
- $this->db->truncate($TestModel);
- $fieldList = array('title', 'author_id');
- $TestModel->saveAll(array(
- array(
- 'title' => 'Multi-record post 1',
- 'body' => 'First multi-record post',
- 'author_id' => 2
- ),
- array(
- 'title' => 'Multi-record post 2',
- 'body' => 'Second multi-record post',
- 'author_id' => 2
- )), array('fieldList' => $fieldList));
- $result = $TestModel->find('all', array(
- 'recursive' => -1,
- 'order' => 'Post.id ASC'
- ));
- $expected = array(
- array(
- 'Post' => array(
- 'id' => '1',
- 'author_id' => '2',
- 'title' => 'Multi-record post 1',
- 'body' => '',
- 'published' => 'N',
- 'created' => self::date(),
- 'updated' => self::date()
- )
- ),
- array(
- 'Post' => array(
- 'id' => '2',
- 'author_id' => '2',
- 'title' => 'Multi-record post 2',
- 'body' => '',
- 'published' => 'N',
- 'created' => self::date(),
- 'updated' => self::date()
- )
- )
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllFieldListHasMany method
- *
- * return @void
- */
- public function testSaveAllFieldListHasMany() {
- $this->loadFixtures('Article', 'Comment');
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
- $this->db->truncate($TestModel);
- $this->db->truncate(new Comment());
- $data = array(
- 'Article' => array('title' => 'I will not save'),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- );
- $fieldList = array(
- 'Article' => array('id'),
- 'Comment' => array('article_id', 'user_id')
- );
- $TestModel->saveAll($data, array('fieldList' => $fieldList));
- $result = $TestModel->find('all');
- $this->assertEquals('', $result[0]['Article']['title']);
- $this->assertEquals('', $result[0]['Comment'][0]['comment']);
- $this->assertEquals('', $result[0]['Comment'][1]['comment']);
- $fieldList = array(
- 'Article' => array('id'),
- 'Comment' => array('user_id')
- );
- $TestModel->saveAll($data, array('fieldList' => $fieldList));
- $result = $TestModel->find('all');
- $this->assertEquals('', $result[1]['Article']['title']);
- $this->assertEquals(2, count($result[1]['Comment']));
- $TestModel->whitelist = array('id');
- $TestModel->Comment->whitelist = array('user_id');
- $TestModel->saveAll($data);
- $result = $TestModel->find('all');
- $this->assertEquals('', $result[2]['Article']['title']);
- $this->assertEquals(2, count($result[2]['Comment']));
- }
- /**
- * testSaveAllFieldListHasOne method
- *
- * @return void
- */
- public function testSaveAllFieldListHasOne() {
- $this->loadFixtures('Attachment', 'Comment', 'Article', 'User');
- $TestModel = new Comment();
- $TestModel->validate = array('comment' => 'notEmpty');
- $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
- $record = array(
- 'Comment' => array(
- 'user_id' => 1,
- 'article_id' => 1,
- 'comment' => '',
- ),
- 'Attachment' => array(
- 'attachment' => ''
- )
- );
- $result = $TestModel->saveAll($record, array('validate' => 'only'));
- $this->assertFalse($result);
- $fieldList = array(
- 'Comment' => array('id', 'article_id', 'user_id'),
- 'Attachment' => array('comment_id')
- );
- $result = $TestModel->saveAll($record, array(
- 'fieldList' => $fieldList, 'validate' => 'only'
- ));
- $this->assertTrue($result);
- $this->assertEmpty($TestModel->validationErrors);
- }
- /**
- * testSaveAllFieldListHasOneAddFkToWhitelist method
- *
- * @return void
- */
- public function testSaveAllFieldListHasOneAddFkToWhitelist() {
- $this->loadFixtures('ArticleFeatured', 'Featured');
- $Article = new ArticleFeatured();
- $Article->belongsTo = $Article->hasMany = array();
- $Article->Featured->validate = array('end_date' => 'notEmpty');
- $record = array(
- 'ArticleFeatured' => array(
- 'user_id' => 1,
- 'title' => 'First Article',
- 'body' => '',
- 'published' => 'Y'
- ),
- 'Featured' => array(
- 'category_id' => 1,
- 'end_date' => ''
- )
- );
- $result = $Article->saveAll($record, array('validate' => 'only'));
- $this->assertFalse($result);
- $expected = array(
- 'body' => array(
- 'This field cannot be left blank'
- ),
- 'Featured' => array(
- 'end_date' => array(
- 'This field cannot be left blank'
- )
- )
- );
- $this->assertEquals($expected, $Article->validationErrors);
- $fieldList = array(
- 'ArticleFeatured' => array('user_id', 'title'),
- 'Featured' => array('category_id')
- );
- $result = $Article->saveAll($record, array(
- 'fieldList' => $fieldList, 'validate' => 'first'
- ));
- $this->assertTrue($result);
- $this->assertEmpty($Article->validationErrors);
- $Article->recursive = 0;
- $result = $Article->find('first', array('order' => array('ArticleFeatured.created' => 'DESC')));
- $this->assertSame($result['ArticleFeatured']['id'], $result['Featured']['article_featured_id']);
- }
- /**
- * testSaveAllDeepFieldListValidateBelongsTo
- *
- * @return void
- */
- public function testSaveAllDeepFieldListValidateBelongsTo() {
- $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
- $TestModel = new Post();
- $TestModel->Author->bindModel(array('hasMany' => array('Comment' => array('foreignKey' => 'user_id'))), false);
- $TestModel->recursive = 2;
- $result = $TestModel->find('all');
- $this->assertEquals(3, count($result));
- $this->assertFalse(isset($result[3]));
- // test belongsTo
- $fieldList = array(
- 'Post' => array('title', 'author_id'),
- 'Author' => array('user'),
- 'Comment' => array('comment')
- );
- $TestModel->saveAll(array(
- 'Post' => array(
- 'title' => 'Post without body',
- 'body' => 'This will not be saved',
- ),
- 'Author' => array(
- 'user' => 'bob',
- 'test' => 'This will not be saved',
- 'Comment' => array(
- array('id' => 5, 'comment' => 'I am still published', 'published' => 'N'))
- )), array('fieldList' => $fieldList, 'deep' => true));
- $result = $TestModel->Author->Comment->find('first', array(
- 'conditions' => array('Comment.id' => 5),
- 'fields' => array('comment', 'published')
- ));
- $expected = array(
- 'Comment' => array(
- 'comment' => 'I am still published',
- 'published' => 'Y'
- )
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllDeepFieldListHasMany method
- *
- * return @void
- */
- public function testSaveAllDeepFieldListHasMany() {
- $this->loadFixtures('Article', 'Comment', 'User');
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
- $this->db->truncate($TestModel);
- $this->db->truncate(new Comment());
- $fieldList = array(
- 'Article' => array('id'),
- 'Comment' => array('article_id', 'user_id'),
- 'User' => array('user')
- );
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2, 'title' => 'I will not save'),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
- array(
- 'comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2,
- 'User' => array('user' => 'nopassword', 'password' => 'not saved')
- )
- )
- ), array('fieldList' => $fieldList, 'deep' => true));
- $result = $TestModel->Comment->User->find('first', array(
- 'conditions' => array('User.user' => 'nopassword'),
- 'fields' => array('user', 'password')
- ));
- $expected = array(
- 'User' => array(
- 'user' => 'nopassword',
- 'password' => ''
- )
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllDeepHasManyBelongsTo method
- *
- * return @void
- */
- public function testSaveAllDeepHasManyBelongsTo() {
- $this->loadFixtures('Article', 'Comment', 'User');
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
- $this->db->truncate($TestModel);
- $this->db->truncate(new Comment());
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2, 'title' => 'The title'),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
- array(
- 'comment' => 'belongsto', 'published' => 'Y',
- 'User' => array('user' => 'findme', 'password' => 'somepass')
- )
- )
- ), array('deep' => true));
- $result = $TestModel->Comment->User->find('first', array(
- 'conditions' => array('User.user' => 'findme'),
- 'fields' => array('id', 'user', 'password')
- ));
- $expected = array(
- 'User' => array(
- 'id' => 5,
- 'user' => 'findme',
- 'password' => 'somepass',
- )
- );
- $this->assertEquals($expected, $result);
- $result = $TestModel->Comment->find('first', array(
- 'conditions' => array('Comment.user_id' => 5),
- 'fields' => array('id', 'comment', 'published', 'user_id')
- ));
- $expected = array(
- 'Comment' => array(
- 'id' => 2,
- 'comment' => 'belongsto',
- 'published' => 'Y',
- 'user_id' => 5
- )
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllDeepHasManyhasMany method
- *
- * return @void
- */
- public function testSaveAllDeepHasManyHasMany() {
- $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
- $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
- $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
- $this->db->truncate($TestModel);
- $this->db->truncate(new Comment());
- $this->db->truncate(new Attachment());
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2, 'title' => 'The title'),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
- array(
- 'comment' => 'hasmany', 'published' => 'Y', 'user_id' => 5,
- 'Attachment' => array(
- array('attachment' => 'first deep attachment'),
- array('attachment' => 'second deep attachment'),
- )
- )
- )
- ), array('deep' => true));
- $result = $TestModel->Comment->find('first', array(
- 'conditions' => array('Comment.comment' => 'hasmany'),
- 'fields' => array('id', 'comment', 'published', 'user_id'),
- 'recursive' => -1
- ));
- $expected = array(
- 'Comment' => array(
- 'id' => 2,
- 'comment' => 'hasmany',
- 'published' => 'Y',
- 'user_id' => 5
- )
- );
- $this->assertEquals($expected, $result);
- $result = $TestModel->Comment->Attachment->find('all', array(
- 'fields' => array('attachment', 'comment_id'),
- 'order' => array('Attachment.id' => 'ASC')
- ));
- $expected = array(
- array('Attachment' => array('attachment' => 'first deep attachment', 'comment_id' => 2)),
- array('Attachment' => array('attachment' => 'second deep attachment', 'comment_id' => 2)),
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllDeepOrderHasManyHasMany method
- *
- * return @void
- */
- public function testSaveAllDeepOrderHasManyHasMany() {
- $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
- $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
- $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
- $this->db->truncate($TestModel);
- $this->db->truncate(new Comment());
- $this->db->truncate(new Attachment());
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2, 'title' => 'Comment has its data after Attachment'),
- 'Comment' => array(
- array(
- 'Attachment' => array(
- array('attachment' => 'attachment should be created with comment_id'),
- array('attachment' => 'comment should be created with article_id'),
- ),
- 'comment' => 'after associated data',
- 'user_id' => 1
- )
- )
- ), array('deep' => true));
- $result = $TestModel->Comment->find('first', array(
- 'conditions' => array('Comment.article_id' => 2),
- ));
- $this->assertEquals(2, $result['Comment']['article_id']);
- $this->assertEquals(2, count($result['Attachment']));
- }
- /**
- * testSaveAllDeepEmptyHasManyHasMany method
- *
- * return @void
- */
- public function testSaveAllDeepEmptyHasManyHasMany() {
- $this->skipIf(!$this->db instanceof Mysql, 'This test is only compatible with Mysql.');
- $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
- $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
- $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
- $this->db->truncate($TestModel);
- $this->db->truncate(new Comment());
- $this->db->truncate(new Attachment());
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 3, 'user_id' => 1, 'title' => 'Comment has no data'),
- 'Comment' => array(
- array(
- 'user_id' => 1,
- 'Attachment' => array(
- array('attachment' => 'attachment should be created with comment_id'),
- array('attachment' => 'comment should be created with article_id'),
- ),
- )
- )
- ), array('deep' => true));
- $result = $TestModel->Comment->find('first', array(
- 'conditions' => array('Comment.article_id' => 3),
- ));
- $this->assertEquals(3, $result['Comment']['article_id']);
- $this->assertEquals(2, count($result['Attachment']));
- }
- /**
- * testUpdateAllBoolean
- *
- * return @void
- */
- public function testUpdateAllBoolean() {
- $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
- $TestModel = new Item();
- $result = $TestModel->updateAll(array('published' => true));
- $this->assertTrue($result);
- $result = $TestModel->find('first', array('fields' => array('id', 'published')));
- $this->assertEquals(true, $result['Item']['published']);
- }
- /**
- * testUpdateAllBooleanConditions
- *
- * return @void
- */
- public function testUpdateAllBooleanConditions() {
- $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
- $TestModel = new Item();
- $result = $TestModel->updateAll(array('published' => true), array('Item.id' => 1));
- $this->assertTrue($result);
- $result = $TestModel->find('first', array(
- 'fields' => array('id', 'published'),
- 'conditions' => array('Item.id' => 1)));
- $this->assertEquals(true, $result['Item']['published']);
- }
- /**
- * testUpdateBoolean
- *
- * return @void
- */
- public function testUpdateBoolean() {
- $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
- $TestModel = new Item();
- $result = $TestModel->save(array('published' => true, 'id' => 1));
- $this->assertTrue((boolean)$result);
- $result = $TestModel->find('first', array(
- 'fields' => array('id', 'published'),
- 'conditions' => array('Item.id' => 1)));
- $this->assertEquals(true, $result['Item']['published']);
- }
- }
|