ModelWriteTest.php 195 KB

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