ModelWriteTest.php 191 KB

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