ModelWriteTest.php 194 KB

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