ModelWriteTest.php 191 KB

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