ModelWriteTest.php 187 KB

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