ModelWriteTest.php 197 KB

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