ModelWriteTest.php 192 KB

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