ModelWriteTest.php 167 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124
  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($data['Article']['title'], $testResult['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('id', 'title', 'count', 'created', 'updated'),
  138. array_keys($result['Uuid'])
  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('id', 'title', 'count', 'created', 'updated'),
  157. array_keys($result['Uuid'])
  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. $TestModel->id = 2;
  1454. $data = array('Tag' => array('Tag' => array(2)));
  1455. $TestModel->save($data);
  1456. $result = $TestModel->findById(2);
  1457. $expected = array(
  1458. 'Article' => array(
  1459. 'id' => '2',
  1460. 'user_id' => '3',
  1461. 'title' => 'Second Article',
  1462. 'body' => 'Second Article Body',
  1463. 'published' => 'Y',
  1464. 'created' => '2007-03-18 10:41:23',
  1465. 'updated' => self::date()
  1466. ),
  1467. 'Tag' => array(
  1468. array(
  1469. 'id' => '2',
  1470. 'tag' => 'tag2',
  1471. 'created' => '2007-03-18 12:24:23',
  1472. 'updated' => '2007-03-18 12:26:31'
  1473. )
  1474. )
  1475. );
  1476. $this->assertEquals($expected, $result);
  1477. $this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio');
  1478. $TestModel = new Portfolio();
  1479. $result = $TestModel->findById(2);
  1480. $expected = array(
  1481. 'Portfolio' => array(
  1482. 'id' => 2,
  1483. 'seller_id' => 1,
  1484. 'name' => 'Portfolio 2'
  1485. ),
  1486. 'Item' => array(
  1487. array(
  1488. 'id' => 2,
  1489. 'syfile_id' => 2,
  1490. 'published' => '',
  1491. 'name' => 'Item 2',
  1492. 'ItemsPortfolio' => array(
  1493. 'id' => 2,
  1494. 'item_id' => 2,
  1495. 'portfolio_id' => 2
  1496. )
  1497. ),
  1498. array(
  1499. 'id' => 6,
  1500. 'syfile_id' => 6,
  1501. 'published' => '',
  1502. 'name' => 'Item 6',
  1503. 'ItemsPortfolio' => array(
  1504. 'id' => 6,
  1505. 'item_id' => 6,
  1506. 'portfolio_id' => 2
  1507. )
  1508. )
  1509. )
  1510. );
  1511. $this->assertEquals($expected, $result);
  1512. $data = array('Item' => array('Item' => array(1, 2)));
  1513. $TestModel->id = 2;
  1514. $TestModel->save($data);
  1515. $result = $TestModel->findById(2);
  1516. $result['Item'] = Hash::sort($result['Item'], '{n}.id', 'asc');
  1517. $expected = array(
  1518. 'Portfolio' => array(
  1519. 'id' => 2,
  1520. 'seller_id' => 1,
  1521. 'name' => 'Portfolio 2'
  1522. ),
  1523. 'Item' => array(
  1524. array(
  1525. 'id' => 1,
  1526. 'syfile_id' => 1,
  1527. 'published' => '',
  1528. 'name' => 'Item 1',
  1529. 'ItemsPortfolio' => array(
  1530. 'id' => 7,
  1531. 'item_id' => 1,
  1532. 'portfolio_id' => 2
  1533. )
  1534. ),
  1535. array(
  1536. 'id' => 2,
  1537. 'syfile_id' => 2,
  1538. 'published' => '',
  1539. 'name' => 'Item 2',
  1540. 'ItemsPortfolio' => array(
  1541. 'id' => 8,
  1542. 'item_id' => 2,
  1543. 'portfolio_id' => 2
  1544. )
  1545. )
  1546. )
  1547. );
  1548. $this->assertEquals($expected, $result);
  1549. }
  1550. /**
  1551. * testSaveHabtmCustomKeys method
  1552. *
  1553. * @return void
  1554. */
  1555. public function testSaveHabtmCustomKeys() {
  1556. $this->loadFixtures('Story', 'StoriesTag', 'Tag');
  1557. $Story = new Story();
  1558. $data = array(
  1559. 'Story' => array('story' => '1'),
  1560. 'Tag' => array(
  1561. 'Tag' => array(2, 3)
  1562. ));
  1563. $result = $Story->set($data);
  1564. $this->assertFalse(empty($result));
  1565. $result = $Story->save();
  1566. $this->assertFalse(empty($result));
  1567. $result = $Story->find('all', array('order' => array('Story.story')));
  1568. $expected = array(
  1569. array(
  1570. 'Story' => array(
  1571. 'story' => 1,
  1572. 'title' => 'First Story'
  1573. ),
  1574. 'Tag' => array(
  1575. array(
  1576. 'id' => 2,
  1577. 'tag' => 'tag2',
  1578. 'created' => '2007-03-18 12:24:23',
  1579. 'updated' => '2007-03-18 12:26:31'
  1580. ),
  1581. array(
  1582. 'id' => 3,
  1583. 'tag' => 'tag3',
  1584. 'created' => '2007-03-18 12:26:23',
  1585. 'updated' => '2007-03-18 12:28:31'
  1586. ))),
  1587. array(
  1588. 'Story' => array(
  1589. 'story' => 2,
  1590. 'title' => 'Second Story'
  1591. ),
  1592. 'Tag' => array()
  1593. ));
  1594. $this->assertEquals($expected, $result);
  1595. }
  1596. /**
  1597. * test that saving habtm records respects conditions set in the 'conditions' key
  1598. * for the association.
  1599. *
  1600. * @return void
  1601. */
  1602. public function testHabtmSaveWithConditionsInAssociation() {
  1603. $this->loadFixtures('JoinThing', 'Something', 'SomethingElse');
  1604. $Something = new Something();
  1605. $Something->unbindModel(array('hasAndBelongsToMany' => array('SomethingElse')), false);
  1606. $Something->bindModel(array(
  1607. 'hasAndBelongsToMany' => array(
  1608. 'DoomedSomethingElse' => array(
  1609. 'className' => 'SomethingElse',
  1610. 'joinTable' => 'join_things',
  1611. 'conditions' => array('JoinThing.doomed' => true),
  1612. 'unique' => true
  1613. ),
  1614. 'NotDoomedSomethingElse' => array(
  1615. 'className' => 'SomethingElse',
  1616. 'joinTable' => 'join_things',
  1617. 'conditions' => array('JoinThing.doomed' => 0),
  1618. 'unique' => true
  1619. )
  1620. )
  1621. ), false);
  1622. $result = $Something->read(null, 1);
  1623. $this->assertTrue(empty($result['NotDoomedSomethingElse']));
  1624. $this->assertEquals(1, count($result['DoomedSomethingElse']));
  1625. $data = array(
  1626. 'Something' => array('id' => 1),
  1627. 'NotDoomedSomethingElse' => array(
  1628. 'NotDoomedSomethingElse' => array(
  1629. array('something_else_id' => 2, 'doomed' => 0),
  1630. array('something_else_id' => 3, 'doomed' => 0)
  1631. )
  1632. )
  1633. );
  1634. $Something->create($data);
  1635. $result = $Something->save();
  1636. $this->assertFalse(empty($result));
  1637. $result = $Something->read(null, 1);
  1638. $this->assertEquals(2, count($result['NotDoomedSomethingElse']));
  1639. $this->assertEquals(1, count($result['DoomedSomethingElse']));
  1640. }
  1641. /**
  1642. * testHabtmSaveKeyResolution method
  1643. *
  1644. * @return void
  1645. */
  1646. public function testHabtmSaveKeyResolution() {
  1647. $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies');
  1648. $ThePaper = new ThePaper();
  1649. $ThePaper->id = 1;
  1650. $ThePaper->save(array('Monkey' => array(2, 3)));
  1651. $result = $ThePaper->findById(1);
  1652. $expected = array(
  1653. array(
  1654. 'id' => '2',
  1655. 'device_type_id' => '1',
  1656. 'name' => 'Device 2',
  1657. 'typ' => '1'
  1658. ),
  1659. array(
  1660. 'id' => '3',
  1661. 'device_type_id' => '1',
  1662. 'name' => 'Device 3',
  1663. 'typ' => '2'
  1664. ));
  1665. $this->assertEquals($expected, $result['Monkey']);
  1666. $ThePaper->id = 2;
  1667. $ThePaper->save(array('Monkey' => array(1, 2, 3)));
  1668. $result = $ThePaper->findById(2);
  1669. $expected = array(
  1670. array(
  1671. 'id' => '1',
  1672. 'device_type_id' => '1',
  1673. 'name' => 'Device 1',
  1674. 'typ' => '1'
  1675. ),
  1676. array(
  1677. 'id' => '2',
  1678. 'device_type_id' => '1',
  1679. 'name' => 'Device 2',
  1680. 'typ' => '1'
  1681. ),
  1682. array(
  1683. 'id' => '3',
  1684. 'device_type_id' => '1',
  1685. 'name' => 'Device 3',
  1686. 'typ' => '2'
  1687. ));
  1688. $this->assertEquals($expected, $result['Monkey']);
  1689. $ThePaper->id = 2;
  1690. $ThePaper->save(array('Monkey' => array(1, 3)));
  1691. $result = $ThePaper->findById(2);
  1692. $expected = array(
  1693. array(
  1694. 'id' => '1',
  1695. 'device_type_id' => '1',
  1696. 'name' => 'Device 1',
  1697. 'typ' => '1'
  1698. ),
  1699. array(
  1700. 'id' => '3',
  1701. 'device_type_id' => '1',
  1702. 'name' => 'Device 3',
  1703. 'typ' => '2'
  1704. ));
  1705. $this->assertEquals($expected, $result['Monkey']);
  1706. $result = $ThePaper->findById(1);
  1707. $expected = array(
  1708. array(
  1709. 'id' => '2',
  1710. 'device_type_id' => '1',
  1711. 'name' => 'Device 2',
  1712. 'typ' => '1'
  1713. ),
  1714. array(
  1715. 'id' => '3',
  1716. 'device_type_id' => '1',
  1717. 'name' => 'Device 3',
  1718. 'typ' => '2'
  1719. ));
  1720. $this->assertEquals($expected, $result['Monkey']);
  1721. }
  1722. /**
  1723. * testCreationOfEmptyRecord method
  1724. *
  1725. * @return void
  1726. */
  1727. public function testCreationOfEmptyRecord() {
  1728. $this->loadFixtures('Author');
  1729. $TestModel = new Author();
  1730. $this->assertEquals(4, $TestModel->find('count'));
  1731. $TestModel->deleteAll(true, false, false);
  1732. $this->assertEquals(0, $TestModel->find('count'));
  1733. $result = $TestModel->save();
  1734. $this->assertTrue(isset($result['Author']['created']));
  1735. $this->assertTrue(isset($result['Author']['updated']));
  1736. $this->assertEquals(1, $TestModel->find('count'));
  1737. }
  1738. /**
  1739. * testCreateWithPKFiltering method
  1740. *
  1741. * @return void
  1742. */
  1743. public function testCreateWithPKFiltering() {
  1744. $TestModel = new Article();
  1745. $data = array(
  1746. 'id' => 5,
  1747. 'user_id' => 2,
  1748. 'title' => 'My article',
  1749. 'body' => 'Some text'
  1750. );
  1751. $result = $TestModel->create($data);
  1752. $expected = array(
  1753. 'Article' => array(
  1754. 'published' => 'N',
  1755. 'id' => 5,
  1756. 'user_id' => 2,
  1757. 'title' => 'My article',
  1758. 'body' => 'Some text'
  1759. ));
  1760. $this->assertEquals($expected, $result);
  1761. $this->assertEquals(5, $TestModel->id);
  1762. $result = $TestModel->create($data, true);
  1763. $expected = array(
  1764. 'Article' => array(
  1765. 'published' => 'N',
  1766. 'id' => false,
  1767. 'user_id' => 2,
  1768. 'title' => 'My article',
  1769. 'body' => 'Some text'
  1770. ));
  1771. $this->assertEquals($expected, $result);
  1772. $this->assertFalse($TestModel->id);
  1773. $result = $TestModel->create(array('Article' => $data), true);
  1774. $expected = array(
  1775. 'Article' => array(
  1776. 'published' => 'N',
  1777. 'id' => false,
  1778. 'user_id' => 2,
  1779. 'title' => 'My article',
  1780. 'body' => 'Some text'
  1781. ));
  1782. $this->assertEquals($expected, $result);
  1783. $this->assertFalse($TestModel->id);
  1784. $data = array(
  1785. 'id' => 6,
  1786. 'user_id' => 2,
  1787. 'title' => 'My article',
  1788. 'body' => 'Some text',
  1789. 'created' => '1970-01-01 00:00:00',
  1790. 'updated' => '1970-01-01 12:00:00',
  1791. 'modified' => '1970-01-01 12:00:00'
  1792. );
  1793. $result = $TestModel->create($data);
  1794. $expected = array(
  1795. 'Article' => array(
  1796. 'published' => 'N',
  1797. 'id' => 6,
  1798. 'user_id' => 2,
  1799. 'title' => 'My article',
  1800. 'body' => 'Some text',
  1801. 'created' => '1970-01-01 00:00:00',
  1802. 'updated' => '1970-01-01 12:00:00',
  1803. 'modified' => '1970-01-01 12:00:00'
  1804. ));
  1805. $this->assertEquals($expected, $result);
  1806. $this->assertEquals(6, $TestModel->id);
  1807. $result = $TestModel->create(array(
  1808. 'Article' => array_diff_key($data, array(
  1809. 'created' => true,
  1810. 'updated' => true,
  1811. 'modified' => true
  1812. ))), true);
  1813. $expected = array(
  1814. 'Article' => array(
  1815. 'published' => 'N',
  1816. 'id' => false,
  1817. 'user_id' => 2,
  1818. 'title' => 'My article',
  1819. 'body' => 'Some text'
  1820. ));
  1821. $this->assertEquals($expected, $result);
  1822. $this->assertFalse($TestModel->id);
  1823. }
  1824. /**
  1825. * testCreationWithMultipleData method
  1826. *
  1827. * @return void
  1828. */
  1829. public function testCreationWithMultipleData() {
  1830. $this->loadFixtures('Article', 'Comment');
  1831. $Article = new Article();
  1832. $Comment = new Comment();
  1833. $articles = $Article->find('all', array(
  1834. 'fields' => array('id','title'),
  1835. 'recursive' => -1,
  1836. 'order' => array('Article.id' => 'ASC')
  1837. ));
  1838. $expected = array(
  1839. array('Article' => array(
  1840. 'id' => 1,
  1841. 'title' => 'First Article'
  1842. )),
  1843. array('Article' => array(
  1844. 'id' => 2,
  1845. 'title' => 'Second Article'
  1846. )),
  1847. array('Article' => array(
  1848. 'id' => 3,
  1849. 'title' => 'Third Article'
  1850. )));
  1851. $this->assertEquals($expected, $articles);
  1852. $comments = $Comment->find('all', array(
  1853. 'fields' => array('id','article_id','user_id','comment','published'),
  1854. 'recursive' => -1,
  1855. 'order' => array('Comment.id' => 'ASC')
  1856. ));
  1857. $expected = array(
  1858. array('Comment' => array(
  1859. 'id' => 1,
  1860. 'article_id' => 1,
  1861. 'user_id' => 2,
  1862. 'comment' => 'First Comment for First Article',
  1863. 'published' => 'Y'
  1864. )),
  1865. array('Comment' => array(
  1866. 'id' => 2,
  1867. 'article_id' => 1,
  1868. 'user_id' => 4,
  1869. 'comment' => 'Second Comment for First Article',
  1870. 'published' => 'Y'
  1871. )),
  1872. array('Comment' => array(
  1873. 'id' => 3,
  1874. 'article_id' => 1,
  1875. 'user_id' => 1,
  1876. 'comment' => 'Third Comment for First Article',
  1877. 'published' => 'Y'
  1878. )),
  1879. array('Comment' => array(
  1880. 'id' => 4,
  1881. 'article_id' => 1,
  1882. 'user_id' => 1,
  1883. 'comment' => 'Fourth Comment for First Article',
  1884. 'published' => 'N'
  1885. )),
  1886. array('Comment' => array(
  1887. 'id' => 5,
  1888. 'article_id' => 2,
  1889. 'user_id' => 1,
  1890. 'comment' => 'First Comment for Second Article',
  1891. 'published' => 'Y'
  1892. )),
  1893. array('Comment' => array(
  1894. 'id' => 6,
  1895. 'article_id' => 2,
  1896. 'user_id' => 2,
  1897. 'comment' => 'Second Comment for Second Article',
  1898. 'published' => 'Y'
  1899. )));
  1900. $this->assertEquals($expected, $comments);
  1901. $data = array(
  1902. 'Comment' => array(
  1903. 'article_id' => 2,
  1904. 'user_id' => 4,
  1905. 'comment' => 'Brand New Comment',
  1906. 'published' => 'N'
  1907. ),
  1908. 'Article' => array(
  1909. 'id' => 2,
  1910. 'title' => 'Second Article Modified'
  1911. ));
  1912. $result = $Comment->create($data);
  1913. $this->assertFalse(empty($result));
  1914. $result = $Comment->save();
  1915. $this->assertFalse(empty($result));
  1916. $articles = $Article->find('all', array(
  1917. 'fields' => array('id','title'),
  1918. 'recursive' => -1,
  1919. 'order' => array('Article.id' => 'ASC')
  1920. ));
  1921. $expected = array(
  1922. array('Article' => array(
  1923. 'id' => 1,
  1924. 'title' => 'First Article'
  1925. )),
  1926. array('Article' => array(
  1927. 'id' => 2,
  1928. 'title' => 'Second Article'
  1929. )),
  1930. array('Article' => array(
  1931. 'id' => 3,
  1932. 'title' => 'Third Article'
  1933. )));
  1934. $this->assertEquals($expected, $articles);
  1935. $comments = $Comment->find('all', array(
  1936. 'fields' => array('id','article_id','user_id','comment','published'),
  1937. 'recursive' => -1,
  1938. 'order' => array('Comment.id' => 'ASC')
  1939. ));
  1940. $expected = array(
  1941. array('Comment' => array(
  1942. 'id' => 1,
  1943. 'article_id' => 1,
  1944. 'user_id' => 2,
  1945. 'comment' => 'First Comment for First Article',
  1946. 'published' => 'Y'
  1947. )),
  1948. array('Comment' => array(
  1949. 'id' => 2,
  1950. 'article_id' => 1,
  1951. 'user_id' => 4,
  1952. 'comment' => 'Second Comment for First Article',
  1953. 'published' => 'Y'
  1954. )),
  1955. array('Comment' => array(
  1956. 'id' => 3,
  1957. 'article_id' => 1,
  1958. 'user_id' => 1,
  1959. 'comment' => 'Third Comment for First Article',
  1960. 'published' => 'Y'
  1961. )),
  1962. array('Comment' => array(
  1963. 'id' => 4,
  1964. 'article_id' => 1,
  1965. 'user_id' => 1,
  1966. 'comment' => 'Fourth Comment for First Article',
  1967. 'published' => 'N'
  1968. )),
  1969. array('Comment' => array(
  1970. 'id' => 5,
  1971. 'article_id' => 2,
  1972. 'user_id' => 1,
  1973. 'comment' => 'First Comment for Second Article',
  1974. 'published' => 'Y'
  1975. )),
  1976. array('Comment' => array(
  1977. 'id' => 6,
  1978. 'article_id' => 2,
  1979. 'user_id' => 2, 'comment' =>
  1980. 'Second Comment for Second Article',
  1981. 'published' => 'Y'
  1982. )),
  1983. array('Comment' => array(
  1984. 'id' => 7,
  1985. 'article_id' => 2,
  1986. 'user_id' => 4,
  1987. 'comment' => 'Brand New Comment',
  1988. 'published' => 'N'
  1989. )));
  1990. $this->assertEquals($expected, $comments);
  1991. }
  1992. /**
  1993. * testCreationWithMultipleDataSameModel method
  1994. *
  1995. * @return void
  1996. */
  1997. public function testCreationWithMultipleDataSameModel() {
  1998. $this->loadFixtures('Article');
  1999. $Article = new Article();
  2000. $SecondaryArticle = new Article();
  2001. $result = $Article->field('title', array('id' => 1));
  2002. $this->assertEquals('First Article', $result);
  2003. $data = array(
  2004. 'Article' => array(
  2005. 'user_id' => 2,
  2006. 'title' => 'Brand New Article',
  2007. 'body' => 'Brand New Article Body',
  2008. 'published' => 'Y'
  2009. ),
  2010. 'SecondaryArticle' => array(
  2011. 'id' => 1
  2012. ));
  2013. $Article->create();
  2014. $result = $Article->save($data);
  2015. $this->assertFalse(empty($result));
  2016. $result = $Article->getInsertID();
  2017. $this->assertTrue(!empty($result));
  2018. $result = $Article->field('title', array('id' => 1));
  2019. $this->assertEquals('First Article', $result);
  2020. $articles = $Article->find('all', array(
  2021. 'fields' => array('id','title'),
  2022. 'recursive' => -1,
  2023. 'order' => array('Article.id' => 'ASC')
  2024. ));
  2025. $expected = array(
  2026. array('Article' => array(
  2027. 'id' => 1,
  2028. 'title' => 'First Article'
  2029. )),
  2030. array('Article' => array(
  2031. 'id' => 2,
  2032. 'title' => 'Second Article'
  2033. )),
  2034. array('Article' => array(
  2035. 'id' => 3,
  2036. 'title' => 'Third Article'
  2037. )),
  2038. array('Article' => array(
  2039. 'id' => 4,
  2040. 'title' => 'Brand New Article'
  2041. )));
  2042. $this->assertEquals($expected, $articles);
  2043. }
  2044. /**
  2045. * testCreationWithMultipleDataSameModelManualInstances method
  2046. *
  2047. * @return void
  2048. */
  2049. public function testCreationWithMultipleDataSameModelManualInstances() {
  2050. $this->loadFixtures('PrimaryModel');
  2051. $Primary = new PrimaryModel();
  2052. $Secondary = new PrimaryModel();
  2053. $result = $Primary->field('primary_name', array('id' => 1));
  2054. $this->assertEquals('Primary Name Existing', $result);
  2055. $data = array(
  2056. 'PrimaryModel' => array(
  2057. 'primary_name' => 'Primary Name New'
  2058. ),
  2059. 'SecondaryModel' => array(
  2060. 'id' => array(1)
  2061. ));
  2062. $Primary->create();
  2063. $result = $Primary->save($data);
  2064. $this->assertFalse(empty($result));
  2065. $result = $Primary->field('primary_name', array('id' => 1));
  2066. $this->assertEquals('Primary Name Existing', $result);
  2067. $result = $Primary->getInsertID();
  2068. $this->assertTrue(!empty($result));
  2069. $result = $Primary->field('primary_name', array('id' => $result));
  2070. $this->assertEquals('Primary Name New', $result);
  2071. $result = $Primary->find('count');
  2072. $this->assertEquals(2, $result);
  2073. }
  2074. /**
  2075. * testRecordExists method
  2076. *
  2077. * @return void
  2078. */
  2079. public function testRecordExists() {
  2080. $this->loadFixtures('User');
  2081. $TestModel = new User();
  2082. $this->assertFalse($TestModel->exists());
  2083. $TestModel->read(null, 1);
  2084. $this->assertTrue($TestModel->exists());
  2085. $TestModel->create();
  2086. $this->assertFalse($TestModel->exists());
  2087. $TestModel->id = 4;
  2088. $this->assertTrue($TestModel->exists());
  2089. $TestModel = new TheVoid();
  2090. $this->assertFalse($TestModel->exists());
  2091. }
  2092. /**
  2093. * testRecordExistsMissingTable method
  2094. *
  2095. * @expectedException PDOException
  2096. * @return void
  2097. */
  2098. public function testRecordExistsMissingTable() {
  2099. $TestModel = new TheVoid();
  2100. $TestModel->id = 5;
  2101. $TestModel->exists();
  2102. }
  2103. /**
  2104. * testUpdateExisting method
  2105. *
  2106. * @return void
  2107. */
  2108. public function testUpdateExisting() {
  2109. $this->loadFixtures('User', 'Article', 'Comment');
  2110. $TestModel = new User();
  2111. $TestModel->create();
  2112. $TestModel->save(array(
  2113. 'User' => array(
  2114. 'user' => 'some user',
  2115. 'password' => 'some password'
  2116. )));
  2117. $this->assertTrue(is_int($TestModel->id) || (intval($TestModel->id) === 5));
  2118. $id = $TestModel->id;
  2119. $TestModel->save(array(
  2120. 'User' => array(
  2121. 'user' => 'updated user'
  2122. )));
  2123. $this->assertEquals($id, $TestModel->id);
  2124. $result = $TestModel->findById($id);
  2125. $this->assertEquals('updated user', $result['User']['user']);
  2126. $this->assertEquals('some password', $result['User']['password']);
  2127. $Article = new Article();
  2128. $Comment = new Comment();
  2129. $data = array(
  2130. 'Comment' => array(
  2131. 'id' => 1,
  2132. 'comment' => 'First Comment for First Article'
  2133. ),
  2134. 'Article' => array(
  2135. 'id' => 2,
  2136. 'title' => 'Second Article'
  2137. ));
  2138. $result = $Article->save($data);
  2139. $this->assertFalse(empty($result));
  2140. $result = $Comment->save($data);
  2141. $this->assertFalse(empty($result));
  2142. }
  2143. /**
  2144. * test updating records and saving blank values.
  2145. *
  2146. * @return void
  2147. */
  2148. public function testUpdateSavingBlankValues() {
  2149. $this->loadFixtures('Article');
  2150. $Article = new Article();
  2151. $Article->validate = array();
  2152. $Article->create();
  2153. $result = $Article->save(array(
  2154. 'id' => 1,
  2155. 'title' => '',
  2156. 'body' => ''
  2157. ));
  2158. $this->assertTrue((bool)$result);
  2159. $result = $Article->find('first', array('conditions' => array('Article.id' => 1)));
  2160. $this->assertEquals('', $result['Article']['title'], 'Title is not blank');
  2161. $this->assertEquals('', $result['Article']['body'], 'Body is not blank');
  2162. }
  2163. /**
  2164. * testUpdateMultiple method
  2165. *
  2166. * @return void
  2167. */
  2168. public function testUpdateMultiple() {
  2169. $this->loadFixtures('Comment', 'Article', 'User', 'CategoryThread');
  2170. $TestModel = new Comment();
  2171. $result = Hash::extract($TestModel->find('all'), '{n}.Comment.user_id');
  2172. $expected = array('2', '4', '1', '1', '1', '2');
  2173. $this->assertEquals($expected, $result);
  2174. $TestModel->updateAll(array('Comment.user_id' => 5), array('Comment.user_id' => 2));
  2175. $result = Hash::combine($TestModel->find('all'), '{n}.Comment.id', '{n}.Comment.user_id');
  2176. $expected = array(1 => 5, 2 => 4, 3 => 1, 4 => 1, 5 => 1, 6 => 5);
  2177. $this->assertEquals($expected, $result);
  2178. $result = $TestModel->updateAll(
  2179. array('Comment.comment' => "'Updated today'"),
  2180. array('Comment.user_id' => 5)
  2181. );
  2182. $this->assertFalse(empty($result));
  2183. $result = Hash::extract(
  2184. $TestModel->find('all', array(
  2185. 'conditions' => array(
  2186. 'Comment.user_id' => 5
  2187. ))),
  2188. '{n}.Comment.comment'
  2189. );
  2190. $expected = array_fill(0, 2, 'Updated today');
  2191. $this->assertEquals($expected, $result);
  2192. }
  2193. /**
  2194. * testHabtmUuidWithUuidId method
  2195. *
  2196. * @return void
  2197. */
  2198. public function testHabtmUuidWithUuidId() {
  2199. $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolio', 'UuiditemsUuidportfolioNumericid');
  2200. $TestModel = new Uuidportfolio();
  2201. $data = array('Uuidportfolio' => array('name' => 'Portfolio 3'));
  2202. $data['Uuiditem']['Uuiditem'] = array('483798c8-c7cc-430e-8cf9-4fcc40cf8569');
  2203. $TestModel->create($data);
  2204. $TestModel->save();
  2205. $id = $TestModel->id;
  2206. $result = $TestModel->read(null, $id);
  2207. $this->assertEquals(1, count($result['Uuiditem']));
  2208. $this->assertEquals(36, strlen($result['Uuiditem'][0]['UuiditemsUuidportfolio']['id']));
  2209. }
  2210. /**
  2211. * test HABTM saving when join table has no primary key and only 2 columns.
  2212. *
  2213. * @return void
  2214. */
  2215. public function testHabtmSavingWithNoPrimaryKeyUuidJoinTable() {
  2216. $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
  2217. $Fruit = new Fruit();
  2218. $data = array(
  2219. 'Fruit' => array(
  2220. 'color' => 'Red',
  2221. 'shape' => 'Heart-shaped',
  2222. 'taste' => 'sweet',
  2223. 'name' => 'Strawberry',
  2224. ),
  2225. 'UuidTag' => array(
  2226. 'UuidTag' => array(
  2227. '481fc6d0-b920-43e0-e50f-6d1740cf8569'
  2228. )
  2229. )
  2230. );
  2231. $result = $Fruit->save($data);
  2232. $this->assertFalse(empty($result));
  2233. }
  2234. /**
  2235. * test HABTM saving when join table has no primary key and only 2 columns, no with model is used.
  2236. *
  2237. * @return void
  2238. */
  2239. public function testHabtmSavingWithNoPrimaryKeyUuidJoinTableNoWith() {
  2240. $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
  2241. $Fruit = new FruitNoWith();
  2242. $data = array(
  2243. 'Fruit' => array(
  2244. 'color' => 'Red',
  2245. 'shape' => 'Heart-shaped',
  2246. 'taste' => 'sweet',
  2247. 'name' => 'Strawberry',
  2248. ),
  2249. 'UuidTag' => array(
  2250. 'UuidTag' => array(
  2251. '481fc6d0-b920-43e0-e50f-6d1740cf8569'
  2252. )
  2253. )
  2254. );
  2255. $result = $Fruit->save($data);
  2256. $this->assertFalse(empty($result));
  2257. }
  2258. /**
  2259. * testHabtmUuidWithNumericId method
  2260. *
  2261. * @return void
  2262. */
  2263. public function testHabtmUuidWithNumericId() {
  2264. $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolioNumericid');
  2265. $TestModel = new Uuiditem();
  2266. $data = array('Uuiditem' => array('name' => 'Item 7', 'published' => 0));
  2267. $data['Uuidportfolio']['Uuidportfolio'] = array('480af662-eb8c-47d3-886b-230540cf8569');
  2268. $TestModel->create($data);
  2269. $TestModel->save();
  2270. $id = $TestModel->id;
  2271. $result = $TestModel->read(null, $id);
  2272. $this->assertEquals(1, count($result['Uuidportfolio']));
  2273. }
  2274. /**
  2275. * testSaveMultipleHabtm method
  2276. *
  2277. * @return void
  2278. */
  2279. public function testSaveMultipleHabtm() {
  2280. $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC');
  2281. $TestModel = new JoinA();
  2282. $result = $TestModel->findById(1);
  2283. $expected = array(
  2284. 'JoinA' => array(
  2285. 'id' => 1,
  2286. 'name' => 'Join A 1',
  2287. 'body' => 'Join A 1 Body',
  2288. 'created' => '2008-01-03 10:54:23',
  2289. 'updated' => '2008-01-03 10:54:23'
  2290. ),
  2291. 'JoinB' => array(
  2292. 0 => array(
  2293. 'id' => 2,
  2294. 'name' => 'Join B 2',
  2295. 'created' => '2008-01-03 10:55:02',
  2296. 'updated' => '2008-01-03 10:55:02',
  2297. 'JoinAsJoinB' => array(
  2298. 'id' => 1,
  2299. 'join_a_id' => 1,
  2300. 'join_b_id' => 2,
  2301. 'other' => 'Data for Join A 1 Join B 2',
  2302. 'created' => '2008-01-03 10:56:33',
  2303. 'updated' => '2008-01-03 10:56:33'
  2304. ))),
  2305. 'JoinC' => array(
  2306. 0 => array(
  2307. 'id' => 2,
  2308. 'name' => 'Join C 2',
  2309. 'created' => '2008-01-03 10:56:12',
  2310. 'updated' => '2008-01-03 10:56:12',
  2311. 'JoinAsJoinC' => array(
  2312. 'id' => 1,
  2313. 'join_a_id' => 1,
  2314. 'join_c_id' => 2,
  2315. 'other' => 'Data for Join A 1 Join C 2',
  2316. 'created' => '2008-01-03 10:57:22',
  2317. 'updated' => '2008-01-03 10:57:22'
  2318. ))));
  2319. $this->assertEquals($expected, $result);
  2320. $TestModel->id = 1;
  2321. $data = array(
  2322. 'JoinA' => array(
  2323. 'id' => '1',
  2324. 'name' => 'New name for Join A 1',
  2325. 'updated' => self::date()
  2326. ),
  2327. 'JoinB' => array(
  2328. array(
  2329. 'id' => 1,
  2330. 'join_b_id' => 2,
  2331. 'other' => 'New data for Join A 1 Join B 2',
  2332. 'created' => self::date(),
  2333. 'updated' => self::date()
  2334. )),
  2335. 'JoinC' => array(
  2336. array(
  2337. 'id' => 1,
  2338. 'join_c_id' => 2,
  2339. 'other' => 'New data for Join A 1 Join C 2',
  2340. 'created' => self::date(),
  2341. 'updated' => self::date()
  2342. )));
  2343. $TestModel->set($data);
  2344. $TestModel->save();
  2345. $result = $TestModel->findById(1);
  2346. $expected = array(
  2347. 'JoinA' => array(
  2348. 'id' => 1,
  2349. 'name' => 'New name for Join A 1',
  2350. 'body' => 'Join A 1 Body',
  2351. 'created' => '2008-01-03 10:54:23',
  2352. 'updated' => self::date()
  2353. ),
  2354. 'JoinB' => array(
  2355. 0 => array(
  2356. 'id' => 2,
  2357. 'name' => 'Join B 2',
  2358. 'created' => '2008-01-03 10:55:02',
  2359. 'updated' => '2008-01-03 10:55:02',
  2360. 'JoinAsJoinB' => array(
  2361. 'id' => 1,
  2362. 'join_a_id' => 1,
  2363. 'join_b_id' => 2,
  2364. 'other' => 'New data for Join A 1 Join B 2',
  2365. 'created' => self::date(),
  2366. 'updated' => self::date()
  2367. ))),
  2368. 'JoinC' => array(
  2369. 0 => array(
  2370. 'id' => 2,
  2371. 'name' => 'Join C 2',
  2372. 'created' => '2008-01-03 10:56:12',
  2373. 'updated' => '2008-01-03 10:56:12',
  2374. 'JoinAsJoinC' => array(
  2375. 'id' => 1,
  2376. 'join_a_id' => 1,
  2377. 'join_c_id' => 2,
  2378. 'other' => 'New data for Join A 1 Join C 2',
  2379. 'created' => self::date(),
  2380. 'updated' => self::date()
  2381. ))));
  2382. $this->assertEquals($expected, $result);
  2383. }
  2384. /**
  2385. * testSaveAll method
  2386. *
  2387. * @return void
  2388. */
  2389. public function testSaveAll() {
  2390. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  2391. $TestModel = new Post();
  2392. $result = $TestModel->find('all');
  2393. $this->assertEquals(3, count($result));
  2394. $this->assertFalse(isset($result[3]));
  2395. $TestModel->saveAll(array(
  2396. 'Post' => array(
  2397. 'title' => 'Post with Author',
  2398. 'body' => 'This post will be saved with an author'
  2399. ),
  2400. 'Author' => array(
  2401. 'user' => 'bob',
  2402. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
  2403. )));
  2404. $result = $TestModel->find('all');
  2405. $expected = array(
  2406. 'Post' => array(
  2407. 'id' => '4',
  2408. 'author_id' => '5',
  2409. 'title' => 'Post with Author',
  2410. 'body' => 'This post will be saved with an author',
  2411. 'published' => 'N'
  2412. ),
  2413. 'Author' => array(
  2414. 'id' => '5',
  2415. 'user' => 'bob',
  2416. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
  2417. 'test' => 'working'
  2418. ));
  2419. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  2420. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  2421. $this->assertEquals(self::date(), $result[3]['Author']['created']);
  2422. $this->assertEquals(self::date(), $result[3]['Author']['updated']);
  2423. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  2424. unset($result[3]['Author']['created'], $result[3]['Author']['updated']);
  2425. $this->assertEquals($expected, $result[3]);
  2426. $this->assertEquals(4, count($result));
  2427. $TestModel->deleteAll(true);
  2428. $this->assertEquals(array(), $TestModel->find('all'));
  2429. // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
  2430. $this->db->truncate($TestModel);
  2431. $TestModel->saveAll(array(
  2432. array(
  2433. 'title' => 'Multi-record post 1',
  2434. 'body' => 'First multi-record post',
  2435. 'author_id' => 2
  2436. ),
  2437. array(
  2438. 'title' => 'Multi-record post 2',
  2439. 'body' => 'Second multi-record post',
  2440. 'author_id' => 2
  2441. )));
  2442. $result = $TestModel->find('all', array(
  2443. 'recursive' => -1,
  2444. 'order' => 'Post.id ASC'
  2445. ));
  2446. $expected = array(
  2447. array(
  2448. 'Post' => array(
  2449. 'id' => '1',
  2450. 'author_id' => '2',
  2451. 'title' => 'Multi-record post 1',
  2452. 'body' => 'First multi-record post',
  2453. 'published' => 'N'
  2454. )),
  2455. array(
  2456. 'Post' => array(
  2457. 'id' => '2',
  2458. 'author_id' => '2',
  2459. 'title' => 'Multi-record post 2',
  2460. 'body' => 'Second multi-record post',
  2461. 'published' => 'N'
  2462. )));
  2463. $this->assertEquals(self::date(), $result[0]['Post']['created']);
  2464. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  2465. $this->assertEquals(self::date(), $result[1]['Post']['created']);
  2466. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  2467. unset($result[0]['Post']['created'], $result[0]['Post']['updated']);
  2468. unset($result[1]['Post']['created'], $result[1]['Post']['updated']);
  2469. $this->assertEquals($expected, $result);
  2470. $TestModel = new Comment();
  2471. $result = $TestModel->saveAll(array(
  2472. 'Comment' => array(
  2473. 'article_id' => 2,
  2474. 'user_id' => 2,
  2475. 'comment' => 'New comment with attachment',
  2476. 'published' => 'Y'
  2477. ),
  2478. 'Attachment' => array(
  2479. 'attachment' => 'some_file.tgz'
  2480. )));
  2481. $this->assertFalse(empty($result));
  2482. $result = $TestModel->find('all');
  2483. $expected = array(
  2484. 'id' => '7',
  2485. 'article_id' => '2',
  2486. 'user_id' => '2',
  2487. 'comment' => 'New comment with attachment',
  2488. 'published' => 'Y'
  2489. );
  2490. $this->assertEquals(self::date(), $result[6]['Comment']['created']);
  2491. $this->assertEquals(self::date(), $result[6]['Comment']['updated']);
  2492. unset($result[6]['Comment']['created'], $result[6]['Comment']['updated']);
  2493. $this->assertEquals($expected, $result[6]['Comment']);
  2494. $expected = array(
  2495. 'id' => '2',
  2496. 'comment_id' => '7',
  2497. 'attachment' => 'some_file.tgz'
  2498. );
  2499. $this->assertEquals(self::date(), $result[6]['Attachment']['created']);
  2500. $this->assertEquals(self::date(), $result[6]['Attachment']['updated']);
  2501. unset($result[6]['Attachment']['created'], $result[6]['Attachment']['updated']);
  2502. $this->assertEquals($expected, $result[6]['Attachment']);
  2503. }
  2504. /**
  2505. * Test SaveAll with Habtm relations
  2506. *
  2507. * @return void
  2508. */
  2509. public function testSaveAllHabtm() {
  2510. $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  2511. $data = array(
  2512. 'Article' => array(
  2513. 'user_id' => 1,
  2514. 'title' => 'Article Has and belongs to Many Tags'
  2515. ),
  2516. 'Tag' => array(
  2517. 'Tag' => array(1, 2)
  2518. ),
  2519. 'Comment' => array(
  2520. array(
  2521. 'comment' => 'Article comment',
  2522. 'user_id' => 1
  2523. )));
  2524. $Article = new Article();
  2525. $result = $Article->saveAll($data);
  2526. $this->assertFalse(empty($result));
  2527. $result = $Article->read();
  2528. $this->assertEquals(2, count($result['Tag']));
  2529. $this->assertEquals('tag1', $result['Tag'][0]['tag']);
  2530. $this->assertEquals(1, count($result['Comment']));
  2531. $this->assertEquals(1, count($result['Comment'][0]['comment']));
  2532. }
  2533. /**
  2534. * Test SaveAll with Habtm relations and extra join table fields
  2535. *
  2536. * @return void
  2537. */
  2538. public function testSaveAllHabtmWithExtraJoinTableFields() {
  2539. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  2540. $data = array(
  2541. 'Something' => array(
  2542. 'id' => 4,
  2543. 'title' => 'Extra Fields',
  2544. 'body' => 'Extra Fields Body',
  2545. 'published' => '1'
  2546. ),
  2547. 'SomethingElse' => array(
  2548. array('something_else_id' => 1, 'doomed' => '1'),
  2549. array('something_else_id' => 2, 'doomed' => '0'),
  2550. array('something_else_id' => 3, 'doomed' => '1')
  2551. )
  2552. );
  2553. $Something = new Something();
  2554. $result = $Something->saveAll($data);
  2555. $this->assertFalse(empty($result));
  2556. $result = $Something->read();
  2557. $this->assertEquals(3, count($result['SomethingElse']));
  2558. $this->assertTrue(Set::matches('/Something[id=4]', $result));
  2559. $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
  2560. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
  2561. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
  2562. $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
  2563. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
  2564. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
  2565. $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
  2566. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
  2567. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
  2568. }
  2569. /**
  2570. * testSaveAllHasOne method
  2571. *
  2572. * @return void
  2573. */
  2574. public function testSaveAllHasOne() {
  2575. $model = new Comment();
  2576. $model->deleteAll(true);
  2577. $this->assertEquals(array(), $model->find('all'));
  2578. $model->Attachment->deleteAll(true);
  2579. $this->assertEquals(array(), $model->Attachment->find('all'));
  2580. $this->assertTrue($model->saveAll(array(
  2581. 'Comment' => array(
  2582. 'comment' => 'Comment with attachment',
  2583. 'article_id' => 1,
  2584. 'user_id' => 1
  2585. ),
  2586. 'Attachment' => array(
  2587. 'attachment' => 'some_file.zip'
  2588. ))));
  2589. $result = $model->find('all', array('fields' => array(
  2590. 'Comment.id', 'Comment.comment', 'Attachment.id',
  2591. 'Attachment.comment_id', 'Attachment.attachment'
  2592. )));
  2593. $expected = array(array(
  2594. 'Comment' => array(
  2595. 'id' => '1',
  2596. 'comment' => 'Comment with attachment'
  2597. ),
  2598. 'Attachment' => array(
  2599. 'id' => '1',
  2600. 'comment_id' => '1',
  2601. 'attachment' => 'some_file.zip'
  2602. )));
  2603. $this->assertEquals($expected, $result);
  2604. $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
  2605. $data = array(
  2606. 'Comment' => array(
  2607. 'comment' => 'Comment with attachment',
  2608. 'article_id' => 1,
  2609. 'user_id' => 1
  2610. ),
  2611. 'Attachment' => array(
  2612. 'attachment' => 'some_file.zip'
  2613. ));
  2614. $this->assertTrue($model->saveAll($data, array('validate' => 'first')));
  2615. }
  2616. /**
  2617. * testSaveAllBelongsTo method
  2618. *
  2619. * @return void
  2620. */
  2621. public function testSaveAllBelongsTo() {
  2622. $model = new Comment();
  2623. $model->deleteAll(true);
  2624. $this->assertEquals(array(), $model->find('all'));
  2625. $model->Article->deleteAll(true);
  2626. $this->assertEquals(array(), $model->Article->find('all'));
  2627. $this->assertTrue($model->saveAll(array(
  2628. 'Comment' => array(
  2629. 'comment' => 'Article comment',
  2630. 'article_id' => 1,
  2631. 'user_id' => 1
  2632. ),
  2633. 'Article' => array(
  2634. 'title' => 'Model Associations 101',
  2635. 'user_id' => 1
  2636. ))));
  2637. $result = $model->find('all', array('fields' => array(
  2638. 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
  2639. )));
  2640. $expected = array(array(
  2641. 'Comment' => array(
  2642. 'id' => '1',
  2643. 'article_id' => '1',
  2644. 'comment' => 'Article comment'
  2645. ),
  2646. 'Article' => array(
  2647. 'id' => '1',
  2648. 'title' => 'Model Associations 101'
  2649. )));
  2650. $this->assertEquals($expected, $result);
  2651. }
  2652. /**
  2653. * testSaveAllHasOneValidation method
  2654. *
  2655. * @return void
  2656. */
  2657. public function testSaveAllHasOneValidation() {
  2658. $model = new Comment();
  2659. $model->deleteAll(true);
  2660. $this->assertEquals(array(), $model->find('all'));
  2661. $model->Attachment->deleteAll(true);
  2662. $this->assertEquals(array(), $model->Attachment->find('all'));
  2663. $model->validate = array('comment' => 'notEmpty');
  2664. $model->Attachment->validate = array('attachment' => 'notEmpty');
  2665. $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
  2666. $result = $model->saveAll(
  2667. array(
  2668. 'Comment' => array(
  2669. 'comment' => '',
  2670. 'article_id' => 1,
  2671. 'user_id' => 1
  2672. ),
  2673. 'Attachment' => array('attachment' => '')
  2674. ),
  2675. array('validate' => 'first')
  2676. );
  2677. $this->assertEquals(false, $result);
  2678. $expected = array(
  2679. 'Comment' => array('comment' => array('This field cannot be left blank')),
  2680. 'Attachment' => array('attachment' => array('This field cannot be left blank'))
  2681. );
  2682. $this->assertEquals($expected['Comment'], $model->validationErrors);
  2683. $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
  2684. }
  2685. /**
  2686. * testSaveAllAtomic method
  2687. *
  2688. * @return void
  2689. */
  2690. public function testSaveAllAtomic() {
  2691. $this->loadFixtures('Article', 'User', 'Comment');
  2692. $TestModel = new Article();
  2693. $result = $TestModel->saveAll(array(
  2694. 'Article' => array(
  2695. 'title' => 'Post with Author',
  2696. 'body' => 'This post will be saved with an author',
  2697. 'user_id' => 2
  2698. ),
  2699. 'Comment' => array(
  2700. array('comment' => 'First new comment', 'user_id' => 2))
  2701. ), array('atomic' => false));
  2702. $this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
  2703. $result = $TestModel->saveAll(array(
  2704. array(
  2705. 'id' => '1',
  2706. 'title' => 'Baleeted First Post',
  2707. 'body' => 'Baleeted!',
  2708. 'published' => 'N'
  2709. ),
  2710. array(
  2711. 'id' => '2',
  2712. 'title' => 'Just update the title'
  2713. ),
  2714. array(
  2715. 'title' => 'Creating a fourth post',
  2716. 'body' => 'Fourth post body',
  2717. 'user_id' => 2
  2718. )
  2719. ), array('atomic' => false));
  2720. $this->assertSame($result, array(true, true, true));
  2721. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  2722. $result = $TestModel->saveAll(array(
  2723. array(
  2724. 'id' => '1',
  2725. 'title' => 'Un-Baleeted First Post',
  2726. 'body' => 'Not Baleeted!',
  2727. 'published' => 'Y'
  2728. ),
  2729. array(
  2730. 'id' => '2',
  2731. 'title' => '',
  2732. 'body' => 'Trying to get away with an empty title'
  2733. )
  2734. ), array('validate' => true, 'atomic' => false));
  2735. $this->assertSame(array(true, false), $result);
  2736. $result = $TestModel->saveAll(array(
  2737. 'Article' => array('id' => 2),
  2738. 'Comment' => array(
  2739. array(
  2740. 'comment' => 'First new comment',
  2741. 'published' => 'Y',
  2742. 'user_id' => 1
  2743. ),
  2744. array(
  2745. 'comment' => 'Second new comment',
  2746. 'published' => 'Y',
  2747. 'user_id' => 2
  2748. ))
  2749. ), array('validate' => true, 'atomic' => false));
  2750. $this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
  2751. }
  2752. /**
  2753. * testSaveAllDeepAssociated method
  2754. *
  2755. * @return void
  2756. */
  2757. public function testSaveAllDeepAssociated() {
  2758. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  2759. $TestModel = new Article();
  2760. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  2761. $TestModel->hasAndBelongsToMany = array();
  2762. $result = $TestModel->saveAll(array(
  2763. 'Article' => array('id' => 2),
  2764. 'Comment' => array(
  2765. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
  2766. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  2767. )
  2768. ), array('deep' => true));
  2769. $this->assertTrue($result);
  2770. $result = $TestModel->findById(2);
  2771. $expected = array(
  2772. 'First Comment for Second Article',
  2773. 'Second Comment for Second Article',
  2774. 'First new comment',
  2775. 'Second new comment'
  2776. );
  2777. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  2778. $this->assertEquals($expected, $result);
  2779. $result = $TestModel->Comment->User->field('id', array('user' => 'newuser', 'password' => 'newuserpass'));
  2780. $this->assertEquals(5, $result);
  2781. $result = $TestModel->saveAll(array(
  2782. 'Article' => array('id' => 2),
  2783. 'Comment' => array(
  2784. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  2785. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
  2786. )
  2787. ), array('deep' => true));
  2788. $this->assertTrue($result);
  2789. $result = $TestModel->findById(2);
  2790. $expected = array(
  2791. 'First Comment for Second Article',
  2792. 'Second Comment for Second Article',
  2793. 'First new comment',
  2794. 'Second new comment',
  2795. 'Third new comment',
  2796. 'Fourth new comment'
  2797. );
  2798. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  2799. $this->assertEquals($expected, $result);
  2800. $result = $TestModel->Comment->Attachment->field('id', array('attachment' => 'deepsaved'));
  2801. $this->assertEquals(2, $result);
  2802. $data = array(
  2803. 'Attachment' => array(
  2804. 'attachment' => 'deepsave insert',
  2805. ),
  2806. 'Comment' => array(
  2807. 'comment' => 'First comment deepsave insert',
  2808. 'published' => 'Y',
  2809. 'user_id' => 5,
  2810. 'Article' => array(
  2811. 'title' => 'First Article deepsave insert',
  2812. 'body' => 'First Article Body deepsave insert',
  2813. 'User' => array(
  2814. 'user' => '',
  2815. 'password' => 'magic'
  2816. ),
  2817. ),
  2818. )
  2819. );
  2820. $TestModel->Comment->Attachment->create();
  2821. $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => true));
  2822. $this->assertFalse($result);
  2823. $expected = array('User' => array('user' => array('This field cannot be left blank')));
  2824. $this->assertEquals($expected, $TestModel->validationErrors);
  2825. $data['Comment']['Article']['User']['user'] = 'deepsave';
  2826. $TestModel->Comment->Attachment->create();
  2827. $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => true));
  2828. $this->assertTrue($result);
  2829. $result = $TestModel->Comment->Attachment->findById($TestModel->Comment->Attachment->id);
  2830. $expected = array(
  2831. 'Attachment' => array(
  2832. 'id' => '3',
  2833. 'comment_id' => '11',
  2834. 'attachment' => 'deepsave insert',
  2835. ),
  2836. 'Comment' => array(
  2837. 'id' => '11',
  2838. 'article_id' => '4',
  2839. 'user_id' => '5',
  2840. 'comment' => 'First comment deepsave insert',
  2841. 'published' => 'Y',
  2842. )
  2843. );
  2844. unset($result['Attachment']['created'], $result['Attachment']['updated']);
  2845. $this->assertEquals($expected['Attachment'], $result['Attachment']);
  2846. unset($result['Comment']['created'], $result['Comment']['updated']);
  2847. $this->assertEquals($expected['Comment'], $result['Comment']);
  2848. $result = $TestModel->findById($result['Comment']['article_id']);
  2849. $expected = array(
  2850. 'Article' => array(
  2851. 'id' => '4',
  2852. 'user_id' => '6',
  2853. 'title' => 'First Article deepsave insert',
  2854. 'body' => 'First Article Body deepsave insert',
  2855. 'published' => 'N',
  2856. ),
  2857. 'User' => array(
  2858. 'id' => '6',
  2859. 'user' => 'deepsave',
  2860. 'password' => 'magic',
  2861. ),
  2862. 'Comment' => array(
  2863. array(
  2864. 'id' => '11',
  2865. 'article_id' => '4',
  2866. 'user_id' => '5',
  2867. 'comment' => 'First comment deepsave insert',
  2868. 'published' => 'Y',
  2869. )
  2870. )
  2871. );
  2872. unset(
  2873. $result['Article']['created'], $result['Article']['updated'],
  2874. $result['User']['created'], $result['User']['updated'],
  2875. $result['Comment'][0]['created'], $result['Comment'][0]['updated']
  2876. );
  2877. $this->assertEquals($expected, $result);
  2878. }
  2879. /**
  2880. * testSaveAllDeepMany
  2881. * tests the validate methods with deeper recursive data
  2882. *
  2883. * @return void
  2884. */
  2885. public function testSaveAllDeepMany() {
  2886. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  2887. $TestModel = new Article();
  2888. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  2889. $TestModel->hasAndBelongsToMany = array();
  2890. $data = array(
  2891. array(
  2892. 'id' => 1, 'body' => '',
  2893. 'Comment' => array(
  2894. array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
  2895. array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
  2896. )
  2897. ),
  2898. array(
  2899. 'Article' => array('id' => 2),
  2900. 'Comment' => array(
  2901. array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => '')),
  2902. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  2903. )
  2904. )
  2905. );
  2906. $TestModel->Comment->validate['comment'] = 'notEmpty';
  2907. $result = $TestModel->saveAll($data, array('deep' => true));
  2908. $this->assertFalse($result);
  2909. $expected = array(
  2910. 0 => array(
  2911. 'body' => array('This field cannot be left blank')
  2912. ),
  2913. 1 => array(
  2914. 'Comment' => array(
  2915. 0 => array(
  2916. 'User' => array(
  2917. 'password' => array('This field cannot be left blank')
  2918. )
  2919. ),
  2920. 1 => array(
  2921. 'comment' => array('This field cannot be left blank')
  2922. )
  2923. )
  2924. )
  2925. );
  2926. $result = $TestModel->validationErrors;
  2927. $this->assertSame($expected, $result);
  2928. $data = array(
  2929. array(
  2930. 'Article' => array('id' => 1),
  2931. 'Comment' => array(
  2932. array('comment' => 'First comment deepsaved article 1', 'published' => 'Y', 'User' => array('user' => 'savemany', 'password' => 'manysaved')),
  2933. array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
  2934. )
  2935. ),
  2936. array(
  2937. 'Article' => array('id' => 2),
  2938. 'Comment' => array(
  2939. array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => 'moresaved')),
  2940. array('comment' => 'Second comment deepsaved article 2', 'published' => 'Y', 'user_id' => 2)
  2941. )
  2942. )
  2943. );
  2944. $result = $TestModel->saveAll($data, array('deep' => true));
  2945. $this->assertTrue($result);
  2946. }
  2947. /**
  2948. * testSaveAllNotDeepAssociated method
  2949. * test that only directly associated data gets saved
  2950. *
  2951. * @return void
  2952. */
  2953. public function testSaveAllNotDeepAssociated() {
  2954. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  2955. $TestModel = new Article();
  2956. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  2957. $TestModel->hasAndBelongsToMany = array();
  2958. $result = $TestModel->saveAll(array(
  2959. 'Article' => array('id' => 2),
  2960. 'Comment' => array(
  2961. array(
  2962. 'comment' => 'First new comment', 'published' => 'Y', 'user_id' => 2,
  2963. 'User' => array('user' => 'newuser', 'password' => 'newuserpass')
  2964. ),
  2965. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  2966. )
  2967. ), array('deep' => false));
  2968. $this->assertTrue($result);
  2969. $result = $TestModel->Comment->User->field('id', array('user' => 'newuser', 'password' => 'newuserpass'));
  2970. $this->assertFalse($result);
  2971. $result = $TestModel->saveAll(array(
  2972. 'Article' => array('id' => 2),
  2973. 'Comment' => array(
  2974. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 4),
  2975. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
  2976. )
  2977. ), array('deep' => false));
  2978. $this->assertTrue($result);
  2979. $result = $TestModel->Comment->Attachment->field('id', array('attachment' => 'deepsaved'));
  2980. $this->assertFalse($result);
  2981. $data = array(
  2982. 'Attachment' => array(
  2983. 'attachment' => 'deepsave insert',
  2984. ),
  2985. 'Comment' => array(
  2986. 'comment' => 'First comment deepsave insert',
  2987. 'published' => 'Y',
  2988. 'user_id' => 4,
  2989. 'article_id' => 1,
  2990. 'Article' => array(
  2991. 'title' => 'First Article deepsave insert',
  2992. 'body' => 'First Article Body deepsave insert',
  2993. 'User' => array(
  2994. 'user' => 'deepsave',
  2995. 'password' => 'magic'
  2996. ),
  2997. ),
  2998. )
  2999. );
  3000. $expected = $TestModel->User->find('count');
  3001. $TestModel->Comment->Attachment->create();
  3002. $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => false));
  3003. $this->assertTrue($result);
  3004. $result = $TestModel->User->find('count');
  3005. $this->assertEquals($expected, $result);
  3006. $result = $TestModel->Comment->Attachment->findById($TestModel->Comment->Attachment->id);
  3007. $expected = array(
  3008. 'Attachment' => array(
  3009. 'id' => '2',
  3010. 'comment_id' => '11',
  3011. 'attachment' => 'deepsave insert',
  3012. ),
  3013. 'Comment' => array(
  3014. 'id' => '11',
  3015. 'article_id' => 1,
  3016. 'user_id' => '4',
  3017. 'comment' => 'First comment deepsave insert',
  3018. 'published' => 'Y',
  3019. )
  3020. );
  3021. unset($result['Attachment']['created'], $result['Attachment']['updated']);
  3022. $this->assertEquals($expected['Attachment'], $result['Attachment']);
  3023. unset($result['Comment']['created'], $result['Comment']['updated']);
  3024. $this->assertEquals($expected['Comment'], $result['Comment']);
  3025. }
  3026. /**
  3027. * testSaveAllNotDeepMany
  3028. * tests the save methods to not save deeper recursive data
  3029. *
  3030. * @return void
  3031. */
  3032. public function testSaveAllNotDeepMany() {
  3033. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  3034. $TestModel = new Article();
  3035. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3036. $TestModel->hasAndBelongsToMany = array();
  3037. $data = array(
  3038. array(
  3039. 'id' => 1, 'body' => '',
  3040. 'Comment' => array(
  3041. array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
  3042. array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
  3043. )
  3044. ),
  3045. array(
  3046. 'Article' => array('id' => 2),
  3047. 'Comment' => array(
  3048. array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => '')),
  3049. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  3050. )
  3051. )
  3052. );
  3053. $TestModel->Comment->validate['comment'] = 'notEmpty';
  3054. $result = $TestModel->saveAll($data, array('deep' => false));
  3055. $this->assertFalse($result);
  3056. $expected = array(
  3057. 0 => array(
  3058. 'body' => array('This field cannot be left blank')
  3059. )
  3060. );
  3061. $result = $TestModel->validationErrors;
  3062. $this->assertSame($expected, $result);
  3063. $data = array(
  3064. array(
  3065. 'Article' => array('id' => 1, 'body' => 'Ignore invalid comment'),
  3066. 'Comment' => array(
  3067. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  3068. )
  3069. ),
  3070. array(
  3071. 'Article' => array('id' => 2, 'body' => 'Same here'),
  3072. 'Comment' => array(
  3073. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  3074. )
  3075. )
  3076. );
  3077. $result = $TestModel->saveAll($data, array('deep' => false));
  3078. $this->assertTrue($result);
  3079. }
  3080. /**
  3081. * testSaveAllHasMany method
  3082. *
  3083. * @return void
  3084. */
  3085. public function testSaveAllHasMany() {
  3086. $this->loadFixtures('Article', 'Comment');
  3087. $TestModel = new Article();
  3088. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3089. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  3090. $result = $TestModel->saveAll(array(
  3091. 'Article' => array('id' => 2),
  3092. 'Comment' => array(
  3093. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  3094. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3095. )
  3096. ));
  3097. $this->assertFalse(empty($result));
  3098. $result = $TestModel->findById(2);
  3099. $expected = array(
  3100. 'First Comment for Second Article',
  3101. 'Second Comment for Second Article',
  3102. 'First new comment',
  3103. 'Second new comment'
  3104. );
  3105. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  3106. $this->assertEquals($expected, $result);
  3107. $result = $TestModel->saveAll(
  3108. array(
  3109. 'Article' => array('id' => 2),
  3110. 'Comment' => array(
  3111. array(
  3112. 'comment' => 'Third new comment',
  3113. 'published' => 'Y',
  3114. 'user_id' => 1
  3115. ))),
  3116. array('atomic' => false)
  3117. );
  3118. $this->assertFalse(empty($result));
  3119. $result = $TestModel->findById(2);
  3120. $expected = array(
  3121. 'First Comment for Second Article',
  3122. 'Second Comment for Second Article',
  3123. 'First new comment',
  3124. 'Second new comment',
  3125. 'Third new comment'
  3126. );
  3127. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  3128. $this->assertEquals($expected, $result);
  3129. $TestModel->beforeSaveReturn = false;
  3130. $result = $TestModel->saveAll(
  3131. array(
  3132. 'Article' => array('id' => 2),
  3133. 'Comment' => array(
  3134. array(
  3135. 'comment' => 'Fourth new comment',
  3136. 'published' => 'Y',
  3137. 'user_id' => 1
  3138. ))),
  3139. array('atomic' => false)
  3140. );
  3141. $this->assertEquals(array('Article' => false), $result);
  3142. $result = $TestModel->findById(2);
  3143. $expected = array(
  3144. 'First Comment for Second Article',
  3145. 'Second Comment for Second Article',
  3146. 'First new comment',
  3147. 'Second new comment',
  3148. 'Third new comment'
  3149. );
  3150. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  3151. $this->assertEquals($expected, $result);
  3152. }
  3153. /**
  3154. * testSaveAllHasManyValidation method
  3155. *
  3156. * @return void
  3157. */
  3158. public function testSaveAllHasManyValidation() {
  3159. $this->loadFixtures('Article', 'Comment');
  3160. $TestModel = new Article();
  3161. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  3162. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  3163. $result = $TestModel->saveAll(array(
  3164. 'Article' => array('id' => 2),
  3165. 'Comment' => array(
  3166. array('comment' => '', 'published' => 'Y', 'user_id' => 1),
  3167. )
  3168. ), array('validate' => true));
  3169. $this->assertFalse($result);
  3170. $expected = array('Comment' => array(
  3171. array('comment' => array('This field cannot be left blank'))
  3172. ));
  3173. $this->assertEquals($expected, $TestModel->validationErrors);
  3174. $expected = array(
  3175. array('comment' => array('This field cannot be left blank'))
  3176. );
  3177. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  3178. $result = $TestModel->saveAll(array(
  3179. 'Article' => array('id' => 2),
  3180. 'Comment' => array(
  3181. array(
  3182. 'comment' => '',
  3183. 'published' => 'Y',
  3184. 'user_id' => 1
  3185. ))
  3186. ), array('validate' => 'first'));
  3187. $this->assertFalse($result);
  3188. }
  3189. /**
  3190. * test saveAll with transactions and ensure there is no missing rollback.
  3191. *
  3192. * @return void
  3193. */
  3194. public function testSaveAllManyRowsTransactionNoRollback() {
  3195. $this->loadFixtures('Post');
  3196. $this->getMock('DboSource', array('connect', 'rollback', 'describe'), array(), 'MockTransactionDboSource');
  3197. $db = ConnectionManager::create('mock_transaction', array(
  3198. 'datasource' => 'MockTransactionDboSource',
  3199. ));
  3200. $db->expects($this->once())
  3201. ->method('describe')
  3202. ->will($this->returnValue(array()));
  3203. $db->expects($this->once())->method('rollback');
  3204. $Post = new Post('mock_transaction');
  3205. $Post->validate = array(
  3206. 'title' => array('rule' => array('notEmpty'))
  3207. );
  3208. $data = array(
  3209. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3210. array('author_id' => 1, 'title' => '')
  3211. );
  3212. $Post->saveAll($data, array('atomic' => true));
  3213. }
  3214. /**
  3215. * test saveAll with transactions and ensure there is no missing rollback.
  3216. *
  3217. * @return void
  3218. */
  3219. public function testSaveAllAssociatedTransactionNoRollback() {
  3220. $testDb = ConnectionManager::getDataSource('test');
  3221. $mock = $this->getMock(
  3222. 'DboSource',
  3223. array('connect', 'rollback', 'describe', 'create', 'update', 'begin'),
  3224. array(),
  3225. 'MockTransactionAssociatedDboSource'
  3226. );
  3227. $db = ConnectionManager::create('mock_transaction_assoc', array(
  3228. 'datasource' => 'MockTransactionAssociatedDboSource',
  3229. ));
  3230. $this->mockObjects[] = $db;
  3231. $db->columns = $testDb->columns;
  3232. $db->expects($this->once())->method('rollback');
  3233. $db->expects($this->any())->method('describe')
  3234. ->will($this->returnValue(array(
  3235. 'id' => array('type' => 'integer', 'length' => 11),
  3236. 'title' => array('type' => 'string'),
  3237. 'body' => array('type' => 'text'),
  3238. 'published' => array('type' => 'string')
  3239. )));
  3240. $Post = new Post();
  3241. $Post->useDbConfig = 'mock_transaction_assoc';
  3242. $Post->Author->useDbConfig = 'mock_transaction_assoc';
  3243. $Post->Author->validate = array(
  3244. 'user' => array('rule' => array('notEmpty'))
  3245. );
  3246. $data = array(
  3247. 'Post' => array(
  3248. 'title' => 'New post',
  3249. 'body' => 'Content',
  3250. 'published' => 'Y'
  3251. ),
  3252. 'Author' => array(
  3253. 'user' => '',
  3254. 'password' => "sekret"
  3255. )
  3256. );
  3257. $Post->saveAll($data, array('validate' => true));
  3258. }
  3259. /**
  3260. * test saveAll with nested saveAll call.
  3261. *
  3262. * @return void
  3263. */
  3264. public function testSaveAllNestedSaveAll() {
  3265. $this->loadFixtures('Sample');
  3266. $TransactionTestModel = new TransactionTestModel();
  3267. $data = array(
  3268. array('apple_id' => 1, 'name' => 'sample5'),
  3269. );
  3270. $this->assertTrue($TransactionTestModel->saveAll($data, array('atomic' => true)));
  3271. }
  3272. /**
  3273. * testSaveAllTransaction method
  3274. *
  3275. * @return void
  3276. */
  3277. public function testSaveAllTransaction() {
  3278. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  3279. $TestModel = new Post();
  3280. $TestModel->validate = array('title' => 'notEmpty');
  3281. $data = array(
  3282. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3283. array('author_id' => 1, 'title' => 'New Fifth Post'),
  3284. array('author_id' => 1, 'title' => '')
  3285. );
  3286. $this->assertFalse($TestModel->saveAll($data));
  3287. $result = $TestModel->find('all', array('recursive' => -1));
  3288. $expected = array(
  3289. array('Post' => array(
  3290. 'id' => '1',
  3291. 'author_id' => 1,
  3292. 'title' => 'First Post',
  3293. 'body' => 'First Post Body',
  3294. 'published' => 'Y',
  3295. 'created' => '2007-03-18 10:39:23',
  3296. 'updated' => '2007-03-18 10:41:31'
  3297. )),
  3298. array('Post' => array(
  3299. 'id' => '2',
  3300. 'author_id' => 3,
  3301. 'title' => 'Second Post',
  3302. 'body' => 'Second Post Body',
  3303. 'published' => 'Y',
  3304. 'created' => '2007-03-18 10:41:23',
  3305. 'updated' => '2007-03-18 10:43:31'
  3306. )),
  3307. array('Post' => array(
  3308. 'id' => '3',
  3309. 'author_id' => 1,
  3310. 'title' => 'Third Post',
  3311. 'body' => 'Third Post Body',
  3312. 'published' => 'Y',
  3313. 'created' => '2007-03-18 10:43:23',
  3314. 'updated' => '2007-03-18 10:45:31'
  3315. )));
  3316. if (count($result) != 3) {
  3317. // Database doesn't support transactions
  3318. $expected[] = array(
  3319. 'Post' => array(
  3320. 'id' => '4',
  3321. 'author_id' => 1,
  3322. 'title' => 'New Fourth Post',
  3323. 'body' => null,
  3324. 'published' => 'N',
  3325. 'created' => self::date(),
  3326. 'updated' => self::date()
  3327. ));
  3328. $expected[] = array(
  3329. 'Post' => array(
  3330. 'id' => '5',
  3331. 'author_id' => 1,
  3332. 'title' => 'New Fifth Post',
  3333. 'body' => null,
  3334. 'published' => 'N',
  3335. 'created' => self::date(),
  3336. 'updated' => self::date()
  3337. ));
  3338. $this->assertEquals($expected, $result);
  3339. // Skip the rest of the transactional tests
  3340. return;
  3341. }
  3342. $this->assertEquals($expected, $result);
  3343. $data = array(
  3344. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3345. array('author_id' => 1, 'title' => ''),
  3346. array('author_id' => 1, 'title' => 'New Sixth Post')
  3347. );
  3348. $this->assertFalse($TestModel->saveAll($data));
  3349. $result = $TestModel->find('all', array('recursive' => -1));
  3350. $expected = array(
  3351. array('Post' => array(
  3352. 'id' => '1',
  3353. 'author_id' => 1,
  3354. 'title' => 'First Post',
  3355. 'body' => 'First Post Body',
  3356. 'published' => 'Y',
  3357. 'created' => '2007-03-18 10:39:23',
  3358. 'updated' => '2007-03-18 10:41:31'
  3359. )),
  3360. array('Post' => array(
  3361. 'id' => '2',
  3362. 'author_id' => 3,
  3363. 'title' => 'Second Post',
  3364. 'body' => 'Second Post Body',
  3365. 'published' => 'Y',
  3366. 'created' => '2007-03-18 10:41:23',
  3367. 'updated' => '2007-03-18 10:43:31'
  3368. )),
  3369. array('Post' => array(
  3370. 'id' => '3',
  3371. 'author_id' => 1,
  3372. 'title' => 'Third Post',
  3373. 'body' => 'Third Post Body',
  3374. 'published' => 'Y',
  3375. 'created' => '2007-03-18 10:43:23',
  3376. 'updated' => '2007-03-18 10:45:31'
  3377. )));
  3378. if (count($result) != 3) {
  3379. // Database doesn't support transactions
  3380. $expected[] = array(
  3381. 'Post' => array(
  3382. 'id' => '4',
  3383. 'author_id' => 1,
  3384. 'title' => 'New Fourth Post',
  3385. 'body' => 'Third Post Body',
  3386. 'published' => 'N',
  3387. 'created' => self::date(),
  3388. 'updated' => self::date()
  3389. ));
  3390. $expected[] = array(
  3391. 'Post' => array(
  3392. 'id' => '5',
  3393. 'author_id' => 1,
  3394. 'title' => 'Third Post',
  3395. 'body' => 'Third Post Body',
  3396. 'published' => 'N',
  3397. 'created' => self::date(),
  3398. 'updated' => self::date()
  3399. ));
  3400. }
  3401. $this->assertEquals($expected, $result);
  3402. $TestModel->validate = array('title' => 'notEmpty');
  3403. $data = array(
  3404. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3405. array('author_id' => 1, 'title' => 'New Fifth Post'),
  3406. array('author_id' => 1, 'title' => 'New Sixth Post')
  3407. );
  3408. $this->assertTrue($TestModel->saveAll($data));
  3409. $result = $TestModel->find('all', array(
  3410. 'recursive' => -1,
  3411. 'fields' => array('author_id', 'title','body','published'),
  3412. 'order' => array('Post.created' => 'ASC')
  3413. ));
  3414. $expected = array(
  3415. array('Post' => array(
  3416. 'author_id' => 1,
  3417. 'title' => 'First Post',
  3418. 'body' => 'First Post Body',
  3419. 'published' => 'Y'
  3420. )),
  3421. array('Post' => array(
  3422. 'author_id' => 3,
  3423. 'title' => 'Second Post',
  3424. 'body' => 'Second Post Body',
  3425. 'published' => 'Y'
  3426. )),
  3427. array('Post' => array(
  3428. 'author_id' => 1,
  3429. 'title' => 'Third Post',
  3430. 'body' => 'Third Post Body',
  3431. 'published' => 'Y'
  3432. )),
  3433. array('Post' => array(
  3434. 'author_id' => 1,
  3435. 'title' => 'New Fourth Post',
  3436. 'body' => '',
  3437. 'published' => 'N'
  3438. )),
  3439. array('Post' => array(
  3440. 'author_id' => 1,
  3441. 'title' => 'New Fifth Post',
  3442. 'body' => '',
  3443. 'published' => 'N'
  3444. )),
  3445. array('Post' => array(
  3446. 'author_id' => 1,
  3447. 'title' => 'New Sixth Post',
  3448. 'body' => '',
  3449. 'published' => 'N'
  3450. )));
  3451. $this->assertEquals($expected, $result);
  3452. }
  3453. /**
  3454. * testSaveAllValidation method
  3455. *
  3456. * @return void
  3457. */
  3458. public function testSaveAllValidation() {
  3459. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  3460. $TestModel = new Post();
  3461. $data = array(
  3462. array(
  3463. 'id' => '1',
  3464. 'title' => 'Baleeted First Post',
  3465. 'body' => 'Baleeted!',
  3466. 'published' => 'N'
  3467. ),
  3468. array(
  3469. 'id' => '2',
  3470. 'title' => 'Just update the title'
  3471. ),
  3472. array(
  3473. 'title' => 'Creating a fourth post',
  3474. 'body' => 'Fourth post body',
  3475. 'author_id' => 2
  3476. ));
  3477. $this->assertTrue($TestModel->saveAll($data));
  3478. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3479. $expected = array(
  3480. array(
  3481. 'Post' => array(
  3482. 'id' => '1',
  3483. 'author_id' => '1',
  3484. 'title' => 'Baleeted First Post',
  3485. 'body' => 'Baleeted!',
  3486. 'published' => 'N',
  3487. 'created' => '2007-03-18 10:39:23'
  3488. )),
  3489. array(
  3490. 'Post' => array(
  3491. 'id' => '2',
  3492. 'author_id' => '3',
  3493. 'title' => 'Just update the title',
  3494. 'body' => 'Second Post Body',
  3495. 'published' => 'Y',
  3496. 'created' => '2007-03-18 10:41:23'
  3497. )),
  3498. array(
  3499. 'Post' => array(
  3500. 'id' => '3',
  3501. 'author_id' => '1',
  3502. 'title' => 'Third Post',
  3503. 'body' => 'Third Post Body',
  3504. 'published' => 'Y',
  3505. 'created' => '2007-03-18 10:43:23',
  3506. 'updated' => '2007-03-18 10:45:31'
  3507. )),
  3508. array(
  3509. 'Post' => array(
  3510. 'id' => '4',
  3511. 'author_id' => '2',
  3512. 'title' => 'Creating a fourth post',
  3513. 'body' => 'Fourth post body',
  3514. 'published' => 'N'
  3515. )));
  3516. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  3517. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  3518. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  3519. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  3520. unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
  3521. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  3522. $this->assertEquals($expected, $result);
  3523. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  3524. $data = array(
  3525. array(
  3526. 'id' => '1',
  3527. 'title' => 'Un-Baleeted First Post',
  3528. 'body' => 'Not Baleeted!',
  3529. 'published' => 'Y'
  3530. ),
  3531. array(
  3532. 'id' => '2',
  3533. 'title' => '',
  3534. 'body' => 'Trying to get away with an empty title'
  3535. ));
  3536. $result = $TestModel->saveAll($data);
  3537. $this->assertFalse($result);
  3538. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3539. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  3540. $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
  3541. if (!$transactionWorked) {
  3542. $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
  3543. $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
  3544. }
  3545. $this->assertEquals($errors, $TestModel->validationErrors);
  3546. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  3547. $data = array(
  3548. array(
  3549. 'id' => '1',
  3550. 'title' => 'Un-Baleeted First Post',
  3551. 'body' => 'Not Baleeted!',
  3552. 'published' => 'Y'
  3553. ),
  3554. array(
  3555. 'id' => '2',
  3556. 'title' => '',
  3557. 'body' => 'Trying to get away with an empty title'
  3558. ));
  3559. $result = $TestModel->saveAll($data, array('validate' => true, 'atomic' => false));
  3560. $this->assertEquals(array(true, false), $result);
  3561. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3562. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  3563. $expected = array(
  3564. array(
  3565. 'Post' => array(
  3566. 'id' => '1',
  3567. 'author_id' => '1',
  3568. 'title' => 'Un-Baleeted First Post',
  3569. 'body' => 'Not Baleeted!',
  3570. 'published' => 'Y',
  3571. 'created' => '2007-03-18 10:39:23'
  3572. )
  3573. ),
  3574. array(
  3575. 'Post' => array(
  3576. 'id' => '2',
  3577. 'author_id' => '3',
  3578. 'title' => 'Just update the title',
  3579. 'body' => 'Second Post Body',
  3580. 'published' => 'Y',
  3581. 'created' => '2007-03-18 10:41:23'
  3582. )
  3583. ),
  3584. array(
  3585. 'Post' => array(
  3586. 'id' => '3',
  3587. 'author_id' => '1',
  3588. 'title' => 'Third Post',
  3589. 'body' => 'Third Post Body',
  3590. 'published' => 'Y',
  3591. 'created' => '2007-03-18 10:43:23',
  3592. 'updated' => '2007-03-18 10:45:31'
  3593. )
  3594. ),
  3595. array(
  3596. 'Post' => array(
  3597. 'id' => '4',
  3598. 'author_id' => '2',
  3599. 'title' => 'Creating a fourth post',
  3600. 'body' => 'Fourth post body',
  3601. 'published' => 'N'
  3602. )
  3603. )
  3604. );
  3605. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  3606. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  3607. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  3608. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  3609. unset(
  3610. $result[0]['Post']['updated'], $result[1]['Post']['updated'],
  3611. $result[3]['Post']['updated'], $result[3]['Post']['created']
  3612. );
  3613. $this->assertEquals($expected, $result);
  3614. $this->assertEquals($errors, $TestModel->validationErrors);
  3615. $data = array(
  3616. array(
  3617. 'id' => '1',
  3618. 'title' => 'Re-Baleeted First Post',
  3619. 'body' => 'Baleeted!',
  3620. 'published' => 'N'
  3621. ),
  3622. array(
  3623. 'id' => '2',
  3624. 'title' => '',
  3625. 'body' => 'Trying to get away with an empty title'
  3626. ));
  3627. $this->assertFalse($TestModel->saveAll($data, array('validate' => 'first')));
  3628. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3629. unset(
  3630. $result[0]['Post']['updated'], $result[1]['Post']['updated'],
  3631. $result[3]['Post']['updated'], $result[3]['Post']['created']
  3632. );
  3633. $this->assertEquals($expected, $result);
  3634. $this->assertEquals($errors, $TestModel->validationErrors);
  3635. }
  3636. /**
  3637. * testSaveAllValidateFirst method
  3638. *
  3639. * @return void
  3640. */
  3641. public function testSaveAllValidateFirst() {
  3642. $this->loadFixtures('Article', 'Comment', 'Attachment', 'User', 'ArticlesTag', 'Tag');
  3643. $model = new Article();
  3644. $model->deleteAll(true);
  3645. $model->Comment->validate = array('comment' => 'notEmpty');
  3646. $result = $model->saveAll(array(
  3647. 'Article' => array(
  3648. 'title' => 'Post with Author',
  3649. 'body' => 'This post will be saved author'
  3650. ),
  3651. 'Comment' => array(
  3652. array('comment' => 'First new comment'),
  3653. array('comment' => '')
  3654. )
  3655. ), array('validate' => 'first'));
  3656. $this->assertFalse($result);
  3657. $result = $model->find('all');
  3658. $this->assertEquals(array(), $result);
  3659. $expected = array('Comment' => array(
  3660. 1 => array('comment' => array('This field cannot be left blank'))
  3661. ));
  3662. $this->assertEquals($expected['Comment'], $model->Comment->validationErrors);
  3663. $this->assertSame($model->Comment->find('count'), 0);
  3664. $result = $model->saveAll(
  3665. array(
  3666. 'Article' => array(
  3667. 'title' => 'Post with Author',
  3668. 'body' => 'This post will be saved with an author',
  3669. 'user_id' => 2
  3670. ),
  3671. 'Comment' => array(
  3672. array(
  3673. 'comment' => 'Only new comment',
  3674. 'user_id' => 2
  3675. ))),
  3676. array('validate' => 'first')
  3677. );
  3678. $this->assertSame($result, true);
  3679. $result = $model->Comment->find('all');
  3680. $this->assertSame(count($result), 1);
  3681. $result = Hash::extract($result, '{n}.Comment.article_id');
  3682. $this->assertEquals(4, $result[0]);
  3683. $model->deleteAll(true);
  3684. $data = array(
  3685. 'Article' => array(
  3686. 'title' => 'Post with Author saveAlled from comment',
  3687. 'body' => 'This post will be saved with an author',
  3688. 'user_id' => 2
  3689. ),
  3690. 'Comment' => array(
  3691. 'comment' => 'Only new comment', 'user_id' => 2
  3692. ));
  3693. $result = $model->Comment->saveAll($data, array('validate' => 'first'));
  3694. $this->assertFalse(empty($result));
  3695. $result = $model->find('all');
  3696. $this->assertEquals(
  3697. 'Post with Author saveAlled from comment',
  3698. $result[0]['Article']['title']
  3699. );
  3700. $this->assertEquals('Only new comment', $result[0]['Comment'][0]['comment']);
  3701. }
  3702. /**
  3703. * test saveAll()'s return is correct when using atomic = false and validate = first.
  3704. *
  3705. * @return void
  3706. */
  3707. public function testSaveAllValidateFirstAtomicFalse() {
  3708. $this->loadFixtures('Something');
  3709. $Something = new Something();
  3710. $invalidData = array(
  3711. array(
  3712. 'title' => 'foo',
  3713. 'body' => 'bar',
  3714. 'published' => 'baz',
  3715. ),
  3716. array(
  3717. 'body' => 3,
  3718. 'published' => 'sd',
  3719. ),
  3720. );
  3721. $Something->create();
  3722. $Something->validate = array(
  3723. 'title' => array(
  3724. 'rule' => 'alphaNumeric',
  3725. 'required' => true,
  3726. ),
  3727. 'body' => array(
  3728. 'rule' => 'alphaNumeric',
  3729. 'required' => true,
  3730. 'allowEmpty' => true,
  3731. ),
  3732. );
  3733. $result = $Something->saveAll($invalidData, array(
  3734. 'atomic' => false,
  3735. 'validate' => 'first',
  3736. ));
  3737. $expected = array(true, false);
  3738. $this->assertEquals($expected, $result);
  3739. $Something = new Something();
  3740. $validData = array(
  3741. array(
  3742. 'title' => 'title value',
  3743. 'body' => 'body value',
  3744. 'published' => 'baz',
  3745. ),
  3746. array(
  3747. 'title' => 'valid',
  3748. 'body' => 'this body',
  3749. 'published' => 'sd',
  3750. ),
  3751. );
  3752. $Something->create();
  3753. $result = $Something->saveAll($validData, array(
  3754. 'atomic' => false,
  3755. 'validate' => 'first',
  3756. ));
  3757. $expected = array(true, true);
  3758. $this->assertEquals($expected, $result);
  3759. }
  3760. /**
  3761. * test that saveAll behaves like plain save() when supplied empty data
  3762. *
  3763. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  3764. * @return void
  3765. */
  3766. public function testSaveAllEmptyData() {
  3767. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  3768. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  3769. $model = new Article();
  3770. $result = $model->saveAll(array(), array('validate' => 'first'));
  3771. $this->assertFalse(empty($result));
  3772. $model = new ProductUpdateAll();
  3773. $result = $model->saveAll(array());
  3774. $this->assertFalse($result);
  3775. }
  3776. /**
  3777. * testSaveAssociated method
  3778. *
  3779. * @return void
  3780. */
  3781. public function testSaveAssociated() {
  3782. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  3783. $TestModel = new Post();
  3784. $result = $TestModel->find('all');
  3785. $this->assertEquals(3, count($result));
  3786. $this->assertFalse(isset($result[3]));
  3787. $TestModel->saveAssociated(array(
  3788. 'Post' => array(
  3789. 'title' => 'Post with Author',
  3790. 'body' => 'This post will be saved with an author'
  3791. ),
  3792. 'Author' => array(
  3793. 'user' => 'bob',
  3794. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
  3795. )));
  3796. $result = $TestModel->find('all');
  3797. $expected = array(
  3798. 'Post' => array(
  3799. 'id' => '4',
  3800. 'author_id' => '5',
  3801. 'title' => 'Post with Author',
  3802. 'body' => 'This post will be saved with an author',
  3803. 'published' => 'N'
  3804. ),
  3805. 'Author' => array(
  3806. 'id' => '5',
  3807. 'user' => 'bob',
  3808. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
  3809. 'test' => 'working'
  3810. ));
  3811. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  3812. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  3813. $this->assertEquals(self::date(), $result[3]['Author']['created']);
  3814. $this->assertEquals(self::date(), $result[3]['Author']['updated']);
  3815. unset(
  3816. $result[3]['Post']['updated'], $result[3]['Post']['created'],
  3817. $result[3]['Author']['updated'], $result[3]['Author']['created']
  3818. );
  3819. $this->assertEquals($expected, $result[3]);
  3820. $this->assertEquals(4, count($result));
  3821. $TestModel = new Comment();
  3822. $result = $TestModel->saveAssociated(array(
  3823. 'Comment' => array(
  3824. 'article_id' => 2,
  3825. 'user_id' => 2,
  3826. 'comment' => 'New comment with attachment',
  3827. 'published' => 'Y'
  3828. ),
  3829. 'Attachment' => array(
  3830. 'attachment' => 'some_file.tgz'
  3831. )));
  3832. $this->assertFalse(empty($result));
  3833. $result = $TestModel->find('all');
  3834. $expected = array(
  3835. 'id' => '7',
  3836. 'article_id' => '2',
  3837. 'user_id' => '2',
  3838. 'comment' => 'New comment with attachment',
  3839. 'published' => 'Y'
  3840. );
  3841. $this->assertEquals(self::date(), $result[6]['Comment']['updated']);
  3842. $this->assertEquals(self::date(), $result[6]['Comment']['created']);
  3843. unset($result[6]['Comment']['updated'], $result[6]['Comment']['created']);
  3844. $this->assertEquals($expected, $result[6]['Comment']);
  3845. $expected = array(
  3846. 'id' => '2',
  3847. 'comment_id' => '7',
  3848. 'attachment' => 'some_file.tgz'
  3849. );
  3850. $this->assertEquals(self::date(), $result[6]['Attachment']['updated']);
  3851. $this->assertEquals(self::date(), $result[6]['Attachment']['created']);
  3852. unset($result[6]['Attachment']['updated'], $result[6]['Attachment']['created']);
  3853. $this->assertEquals($expected, $result[6]['Attachment']);
  3854. }
  3855. /**
  3856. * testSaveMany method
  3857. *
  3858. * @return void
  3859. */
  3860. public function testSaveMany() {
  3861. $this->loadFixtures('Post');
  3862. $TestModel = new Post();
  3863. $TestModel->deleteAll(true);
  3864. $this->assertEquals(array(), $TestModel->find('all'));
  3865. // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
  3866. $this->db->truncate($TestModel);
  3867. $TestModel->saveMany(array(
  3868. array(
  3869. 'title' => 'Multi-record post 1',
  3870. 'body' => 'First multi-record post',
  3871. 'author_id' => 2
  3872. ),
  3873. array(
  3874. 'title' => 'Multi-record post 2',
  3875. 'body' => 'Second multi-record post',
  3876. 'author_id' => 2
  3877. )));
  3878. $result = $TestModel->find('all', array(
  3879. 'recursive' => -1,
  3880. 'order' => 'Post.id ASC'
  3881. ));
  3882. $expected = array(
  3883. array(
  3884. 'Post' => array(
  3885. 'id' => '1',
  3886. 'author_id' => '2',
  3887. 'title' => 'Multi-record post 1',
  3888. 'body' => 'First multi-record post',
  3889. 'published' => 'N'
  3890. )
  3891. ),
  3892. array(
  3893. 'Post' => array(
  3894. 'id' => '2',
  3895. 'author_id' => '2',
  3896. 'title' => 'Multi-record post 2',
  3897. 'body' => 'Second multi-record post',
  3898. 'published' => 'N'
  3899. )
  3900. )
  3901. );
  3902. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  3903. $this->assertEquals(self::date(), $result[0]['Post']['created']);
  3904. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  3905. $this->assertEquals(self::date(), $result[1]['Post']['created']);
  3906. unset($result[0]['Post']['updated'], $result[0]['Post']['created']);
  3907. unset($result[1]['Post']['updated'], $result[1]['Post']['created']);
  3908. $this->assertEquals($expected, $result);
  3909. }
  3910. /**
  3911. * Test SaveAssociated with Habtm relations
  3912. *
  3913. * @return void
  3914. */
  3915. public function testSaveAssociatedHabtm() {
  3916. $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  3917. $data = array(
  3918. 'Article' => array(
  3919. 'user_id' => 1,
  3920. 'title' => 'Article Has and belongs to Many Tags'
  3921. ),
  3922. 'Tag' => array(
  3923. 'Tag' => array(1, 2)
  3924. ),
  3925. 'Comment' => array(
  3926. array(
  3927. 'comment' => 'Article comment',
  3928. 'user_id' => 1
  3929. )));
  3930. $Article = new Article();
  3931. $result = $Article->saveAssociated($data);
  3932. $this->assertFalse(empty($result));
  3933. $result = $Article->read();
  3934. $this->assertEquals(2, count($result['Tag']));
  3935. $this->assertEquals('tag1', $result['Tag'][0]['tag']);
  3936. $this->assertEquals(1, count($result['Comment']));
  3937. $this->assertEquals(1, count($result['Comment'][0]['comment']));
  3938. }
  3939. /**
  3940. * Test SaveAssociated with Habtm relations and extra join table fields
  3941. *
  3942. * @return void
  3943. */
  3944. public function testSaveAssociatedHabtmWithExtraJoinTableFields() {
  3945. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  3946. $data = array(
  3947. 'Something' => array(
  3948. 'id' => 4,
  3949. 'title' => 'Extra Fields',
  3950. 'body' => 'Extra Fields Body',
  3951. 'published' => '1'
  3952. ),
  3953. 'SomethingElse' => array(
  3954. array('something_else_id' => 1, 'doomed' => '1'),
  3955. array('something_else_id' => 2, 'doomed' => '0'),
  3956. array('something_else_id' => 3, 'doomed' => '1')
  3957. )
  3958. );
  3959. $Something = new Something();
  3960. $result = $Something->saveAssociated($data);
  3961. $this->assertFalse(empty($result));
  3962. $result = $Something->read();
  3963. $this->assertEquals(3, count($result['SomethingElse']));
  3964. $this->assertTrue(Set::matches('/Something[id=4]', $result));
  3965. $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
  3966. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
  3967. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
  3968. $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
  3969. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
  3970. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
  3971. $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
  3972. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
  3973. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
  3974. }
  3975. /**
  3976. * testSaveAssociatedHasOne method
  3977. *
  3978. * @return void
  3979. */
  3980. public function testSaveAssociatedHasOne() {
  3981. $model = new Comment();
  3982. $model->deleteAll(true);
  3983. $this->assertEquals(array(), $model->find('all'));
  3984. $model->Attachment->deleteAll(true);
  3985. $this->assertEquals(array(), $model->Attachment->find('all'));
  3986. $this->assertTrue($model->saveAssociated(array(
  3987. 'Comment' => array(
  3988. 'comment' => 'Comment with attachment',
  3989. 'article_id' => 1,
  3990. 'user_id' => 1
  3991. ),
  3992. 'Attachment' => array(
  3993. 'attachment' => 'some_file.zip'
  3994. ))));
  3995. $result = $model->find('all', array('fields' => array(
  3996. 'Comment.id', 'Comment.comment', 'Attachment.id',
  3997. 'Attachment.comment_id', 'Attachment.attachment'
  3998. )));
  3999. $expected = array(array(
  4000. 'Comment' => array(
  4001. 'id' => '1',
  4002. 'comment' => 'Comment with attachment'
  4003. ),
  4004. 'Attachment' => array(
  4005. 'id' => '1',
  4006. 'comment_id' => '1',
  4007. 'attachment' => 'some_file.zip'
  4008. )));
  4009. $this->assertEquals($expected, $result);
  4010. $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
  4011. $data = array(
  4012. 'Comment' => array(
  4013. 'comment' => 'Comment with attachment',
  4014. 'article_id' => 1,
  4015. 'user_id' => 1
  4016. ),
  4017. 'Attachment' => array(
  4018. 'attachment' => 'some_file.zip'
  4019. ));
  4020. $this->assertTrue($model->saveAssociated($data, array('validate' => 'first')));
  4021. }
  4022. /**
  4023. * testSaveAssociatedBelongsTo method
  4024. *
  4025. * @return void
  4026. */
  4027. public function testSaveAssociatedBelongsTo() {
  4028. $model = new Comment();
  4029. $model->deleteAll(true);
  4030. $this->assertEquals(array(), $model->find('all'));
  4031. $model->Article->deleteAll(true);
  4032. $this->assertEquals(array(), $model->Article->find('all'));
  4033. $this->assertTrue($model->saveAssociated(array(
  4034. 'Comment' => array(
  4035. 'comment' => 'Article comment',
  4036. 'article_id' => 1,
  4037. 'user_id' => 1
  4038. ),
  4039. 'Article' => array(
  4040. 'title' => 'Model Associations 101',
  4041. 'user_id' => 1
  4042. ))));
  4043. $result = $model->find('all', array('fields' => array(
  4044. 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
  4045. )));
  4046. $expected = array(array(
  4047. 'Comment' => array(
  4048. 'id' => '1',
  4049. 'article_id' => '1',
  4050. 'comment' => 'Article comment'
  4051. ),
  4052. 'Article' => array(
  4053. 'id' => '1',
  4054. 'title' => 'Model Associations 101'
  4055. )));
  4056. $this->assertEquals($expected, $result);
  4057. }
  4058. /**
  4059. * testSaveAssociatedHasOneValidation method
  4060. *
  4061. * @return void
  4062. */
  4063. public function testSaveAssociatedHasOneValidation() {
  4064. $model = new Comment();
  4065. $model->deleteAll(true);
  4066. $this->assertEquals(array(), $model->find('all'));
  4067. $model->Attachment->deleteAll(true);
  4068. $this->assertEquals(array(), $model->Attachment->find('all'));
  4069. $model->validate = array('comment' => 'notEmpty');
  4070. $model->Attachment->validate = array('attachment' => 'notEmpty');
  4071. $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
  4072. $result = $model->saveAssociated(
  4073. array(
  4074. 'Comment' => array(
  4075. 'comment' => '',
  4076. 'article_id' => 1,
  4077. 'user_id' => 1
  4078. ),
  4079. 'Attachment' => array('attachment' => '')
  4080. )
  4081. );
  4082. $this->assertFalse($result);
  4083. $expected = array(
  4084. 'Comment' => array('comment' => array('This field cannot be left blank')),
  4085. 'Attachment' => array('attachment' => array('This field cannot be left blank'))
  4086. );
  4087. $this->assertEquals($expected['Comment'], $model->validationErrors);
  4088. $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
  4089. }
  4090. /**
  4091. * testSaveAssociatedAtomic method
  4092. *
  4093. * @return void
  4094. */
  4095. public function testSaveAssociatedAtomic() {
  4096. $this->loadFixtures('Article', 'User');
  4097. $TestModel = new Article();
  4098. $result = $TestModel->saveAssociated(array(
  4099. 'Article' => array(
  4100. 'title' => 'Post with Author',
  4101. 'body' => 'This post will be saved with an author',
  4102. 'user_id' => 2
  4103. ),
  4104. 'Comment' => array(
  4105. array('comment' => 'First new comment', 'user_id' => 2))
  4106. ), array('atomic' => false));
  4107. $this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
  4108. $result = $TestModel->saveAssociated(array(
  4109. 'Article' => array('id' => 2),
  4110. 'Comment' => array(
  4111. array(
  4112. 'comment' => 'First new comment',
  4113. 'published' => 'Y',
  4114. 'user_id' => 1
  4115. ),
  4116. array(
  4117. 'comment' => 'Second new comment',
  4118. 'published' => 'Y',
  4119. 'user_id' => 2
  4120. ))
  4121. ), array('validate' => true, 'atomic' => false));
  4122. $this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
  4123. }
  4124. /**
  4125. * testSaveManyAtomic method
  4126. *
  4127. * @return void
  4128. */
  4129. public function testSaveManyAtomic() {
  4130. $this->loadFixtures('Article', 'User');
  4131. $TestModel = new Article();
  4132. $result = $TestModel->saveMany(array(
  4133. array(
  4134. 'id' => '1',
  4135. 'title' => 'Baleeted First Post',
  4136. 'body' => 'Baleeted!',
  4137. 'published' => 'N'
  4138. ),
  4139. array(
  4140. 'id' => '2',
  4141. 'title' => 'Just update the title'
  4142. ),
  4143. array(
  4144. 'title' => 'Creating a fourth post',
  4145. 'body' => 'Fourth post body',
  4146. 'user_id' => 2
  4147. )
  4148. ), array('atomic' => false));
  4149. $this->assertSame($result, array(true, true, true));
  4150. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  4151. $result = $TestModel->saveMany(array(
  4152. array(
  4153. 'id' => '1',
  4154. 'title' => 'Un-Baleeted First Post',
  4155. 'body' => 'Not Baleeted!',
  4156. 'published' => 'Y'
  4157. ),
  4158. array(
  4159. 'id' => '2',
  4160. 'title' => '',
  4161. 'body' => 'Trying to get away with an empty title'
  4162. )
  4163. ), array('validate' => true, 'atomic' => false));
  4164. $this->assertSame(array(true, false), $result);
  4165. }
  4166. /**
  4167. * testSaveAssociatedHasMany method
  4168. *
  4169. * @return void
  4170. */
  4171. public function testSaveAssociatedHasMany() {
  4172. $this->loadFixtures('Article', 'Comment');
  4173. $TestModel = new Article();
  4174. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4175. $result = $TestModel->saveAssociated(array(
  4176. 'Article' => array('id' => 2),
  4177. 'Comment' => array(
  4178. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  4179. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  4180. )
  4181. ));
  4182. $this->assertFalse(empty($result));
  4183. $result = $TestModel->findById(2);
  4184. $expected = array(
  4185. 'First Comment for Second Article',
  4186. 'Second Comment for Second Article',
  4187. 'First new comment',
  4188. 'Second new comment'
  4189. );
  4190. $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
  4191. $result = $TestModel->saveAssociated(
  4192. array(
  4193. 'Article' => array('id' => 2),
  4194. 'Comment' => array(
  4195. array(
  4196. 'comment' => 'Third new comment',
  4197. 'published' => 'Y',
  4198. 'user_id' => 1
  4199. ))),
  4200. array('atomic' => false)
  4201. );
  4202. $this->assertFalse(empty($result));
  4203. $result = $TestModel->findById(2);
  4204. $expected = array(
  4205. 'First Comment for Second Article',
  4206. 'Second Comment for Second Article',
  4207. 'First new comment',
  4208. 'Second new comment',
  4209. 'Third new comment'
  4210. );
  4211. $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
  4212. $TestModel->beforeSaveReturn = false;
  4213. $result = $TestModel->saveAssociated(
  4214. array(
  4215. 'Article' => array('id' => 2),
  4216. 'Comment' => array(
  4217. array(
  4218. 'comment' => 'Fourth new comment',
  4219. 'published' => 'Y',
  4220. 'user_id' => 1
  4221. ))),
  4222. array('atomic' => false)
  4223. );
  4224. $this->assertEquals(array('Article' => false), $result);
  4225. $result = $TestModel->findById(2);
  4226. $expected = array(
  4227. 'First Comment for Second Article',
  4228. 'Second Comment for Second Article',
  4229. 'First new comment',
  4230. 'Second new comment',
  4231. 'Third new comment'
  4232. );
  4233. $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
  4234. }
  4235. /**
  4236. * testSaveAssociatedHasManyEmpty method
  4237. *
  4238. * @return void
  4239. */
  4240. public function testSaveAssociatedHasManyEmpty() {
  4241. $this->loadFixtures('Article', 'Comment');
  4242. $TestModel = new Article();
  4243. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4244. $TestModel->validate = $TestModel->Comment->validate = array('user_id' => array('notEmpty' => array('rule' => 'notEmpty', 'required' => true)));
  4245. //empty hasMany data is ignored in save
  4246. $result = $TestModel->saveAssociated(array(
  4247. 'Article' => array('title' => 'title', 'user_id' => 1),
  4248. 'Comment' => array()
  4249. ), array('validate' => true));
  4250. $this->assertTrue($result);
  4251. $result = $TestModel->saveAssociated(array(
  4252. 'Article' => array('title' => 'title', 'user_id' => 1),
  4253. 'Comment' => array()
  4254. ), array('validate' => true, 'atomic' => false));
  4255. $this->assertEquals(array('Article' => true), $result);
  4256. //empty primary data is not ignored
  4257. $result = $TestModel->saveAssociated(array('Article' => array()), array('validate' => true));
  4258. $this->assertFalse($result);
  4259. $result = $TestModel->saveAssociated(array('Article' => array()), array('validate' => true, 'atomic' => false));
  4260. $this->assertEquals(array('Article' => false), $result);
  4261. }
  4262. /**
  4263. * testSaveAssociatedHasManyValidation method
  4264. *
  4265. * @return void
  4266. */
  4267. public function testSaveAssociatedHasManyValidation() {
  4268. $this->loadFixtures('Article', 'Comment');
  4269. $TestModel = new Article();
  4270. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4271. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  4272. $result = $TestModel->saveAssociated(array(
  4273. 'Article' => array('id' => 2),
  4274. 'Comment' => array(
  4275. array('comment' => '', 'published' => 'Y', 'user_id' => 1),
  4276. )
  4277. ), array('validate' => true));
  4278. $this->assertFalse($result);
  4279. $expected = array('Comment' => array(
  4280. array('comment' => array('This field cannot be left blank'))
  4281. ));
  4282. $this->assertEquals($expected, $TestModel->validationErrors);
  4283. $expected = array(
  4284. array('comment' => array('This field cannot be left blank'))
  4285. );
  4286. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  4287. $result = $TestModel->saveAssociated(array(
  4288. 'Article' => array('id' => 2),
  4289. 'Comment' => array(
  4290. array(
  4291. 'comment' => '',
  4292. 'published' => 'Y',
  4293. 'user_id' => 1
  4294. ))
  4295. ), array('validate' => 'first'));
  4296. $this->assertFalse($result);
  4297. }
  4298. /**
  4299. * test saveMany with transactions and ensure there is no missing rollback.
  4300. *
  4301. * @return void
  4302. */
  4303. public function testSaveManyTransactionNoRollback() {
  4304. $this->loadFixtures('Post');
  4305. $this->getMock('DboSource', array('connect', 'rollback', 'describe'), array(), 'MockManyTransactionDboSource');
  4306. $db = ConnectionManager::create('mock_many_transaction', array(
  4307. 'datasource' => 'MockManyTransactionDboSource',
  4308. ));
  4309. $db->expects($this->once())
  4310. ->method('describe')
  4311. ->will($this->returnValue(array()));
  4312. $db->expects($this->once())->method('rollback');
  4313. $Post = new Post('mock_many_transaction');
  4314. $Post->validate = array(
  4315. 'title' => array('rule' => array('notEmpty'))
  4316. );
  4317. $data = array(
  4318. array('author_id' => 1, 'title' => 'New Fourth Post'),
  4319. array('author_id' => 1, 'title' => '')
  4320. );
  4321. $Post->saveMany($data);
  4322. }
  4323. /**
  4324. * test saveAssociated with transactions and ensure there is no missing rollback.
  4325. *
  4326. * @return void
  4327. */
  4328. public function testSaveAssociatedTransactionNoRollback() {
  4329. $testDb = ConnectionManager::getDataSource('test');
  4330. $mock = $this->getMock(
  4331. 'DboSource',
  4332. array('connect', 'rollback', 'describe', 'create', 'begin'),
  4333. array(),
  4334. 'MockAssociatedTransactionDboSource',
  4335. false
  4336. );
  4337. $db = ConnectionManager::create('mock_assoc_transaction', array(
  4338. 'datasource' => 'MockAssociatedTransactionDboSource',
  4339. ));
  4340. $this->mockObjects[] = $db;
  4341. $db->columns = $testDb->columns;
  4342. $db->expects($this->once())->method('rollback');
  4343. $db->expects($this->any())->method('describe')
  4344. ->will($this->returnValue(array(
  4345. 'id' => array('type' => 'integer', 'length' => 11),
  4346. 'title' => array('type' => 'string'),
  4347. 'body' => array('type' => 'text'),
  4348. 'published' => array('type' => 'string')
  4349. )));
  4350. $Post = new Post();
  4351. $Post->useDbConfig = 'mock_assoc_transaction';
  4352. $Post->Author->useDbConfig = 'mock_assoc_transaction';
  4353. $Post->Author->validate = array(
  4354. 'user' => array('rule' => array('notEmpty'))
  4355. );
  4356. $data = array(
  4357. 'Post' => array(
  4358. 'title' => 'New post',
  4359. 'body' => 'Content',
  4360. 'published' => 'Y'
  4361. ),
  4362. 'Author' => array(
  4363. 'user' => '',
  4364. 'password' => "sekret"
  4365. )
  4366. );
  4367. $Post->saveAssociated($data, array('validate' => true, 'atomic' => true));
  4368. }
  4369. /**
  4370. * test saveMany with nested saveMany call.
  4371. *
  4372. * @return void
  4373. */
  4374. public function testSaveManyNestedSaveMany() {
  4375. $this->loadFixtures('Sample');
  4376. $TransactionManyTestModel = new TransactionManyTestModel();
  4377. $data = array(
  4378. array('apple_id' => 1, 'name' => 'sample5'),
  4379. );
  4380. $this->assertTrue($TransactionManyTestModel->saveMany($data, array('atomic' => true)));
  4381. }
  4382. /**
  4383. * testSaveManyTransaction method
  4384. *
  4385. * @return void
  4386. */
  4387. public function testSaveManyTransaction() {
  4388. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  4389. $TestModel = new Post();
  4390. $TestModel->validate = array('title' => 'notEmpty');
  4391. $data = array(
  4392. array('author_id' => 1, 'title' => 'New Fourth Post'),
  4393. array('author_id' => 1, 'title' => 'New Fifth Post'),
  4394. array('author_id' => 1, 'title' => '')
  4395. );
  4396. $this->assertFalse($TestModel->saveMany($data));
  4397. $result = $TestModel->find('all', array('recursive' => -1));
  4398. $expected = array(
  4399. array('Post' => array(
  4400. 'id' => '1',
  4401. 'author_id' => 1,
  4402. 'title' => 'First Post',
  4403. 'body' => 'First Post Body',
  4404. 'published' => 'Y',
  4405. 'created' => '2007-03-18 10:39:23',
  4406. 'updated' => '2007-03-18 10:41:31'
  4407. )),
  4408. array('Post' => array(
  4409. 'id' => '2',
  4410. 'author_id' => 3,
  4411. 'title' => 'Second Post',
  4412. 'body' => 'Second Post Body',
  4413. 'published' => 'Y',
  4414. 'created' => '2007-03-18 10:41:23',
  4415. 'updated' => '2007-03-18 10:43:31'
  4416. )),
  4417. array('Post' => array(
  4418. 'id' => '3',
  4419. 'author_id' => 1,
  4420. 'title' => 'Third Post',
  4421. 'body' => 'Third Post Body',
  4422. 'published' => 'Y',
  4423. 'created' => '2007-03-18 10:43:23',
  4424. 'updated' => '2007-03-18 10:45:31'
  4425. )));
  4426. if (count($result) != 3) {
  4427. // Database doesn't support transactions
  4428. $expected[] = array(
  4429. 'Post' => array(
  4430. 'id' => '4',
  4431. 'author_id' => 1,
  4432. 'title' => 'New Fourth Post',
  4433. 'body' => null,
  4434. 'published' => 'N'
  4435. ));
  4436. $expected[] = array(
  4437. 'Post' => array(
  4438. 'id' => '5',
  4439. 'author_id' => 1,
  4440. 'title' => 'New Fifth Post',
  4441. 'body' => null,
  4442. 'published' => 'N',
  4443. ));
  4444. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  4445. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  4446. $this->assertEquals(self::date(), $result[4]['Post']['created']);
  4447. $this->assertEquals(self::date(), $result[4]['Post']['updated']);
  4448. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  4449. unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
  4450. $this->assertEquals($expected, $result);
  4451. // Skip the rest of the transactional tests
  4452. return;
  4453. }
  4454. $this->assertEquals($expected, $result);
  4455. $data = array(
  4456. array('author_id' => 1, 'title' => 'New Fourth Post'),
  4457. array('author_id' => 1, 'title' => ''),
  4458. array('author_id' => 1, 'title' => 'New Sixth Post')
  4459. );
  4460. $this->assertFalse($TestModel->saveMany($data));
  4461. $result = $TestModel->find('all', array('recursive' => -1));
  4462. $expected = array(
  4463. array('Post' => array(
  4464. 'id' => '1',
  4465. 'author_id' => 1,
  4466. 'title' => 'First Post',
  4467. 'body' => 'First Post Body',
  4468. 'published' => 'Y',
  4469. 'created' => '2007-03-18 10:39:23',
  4470. 'updated' => '2007-03-18 10:41:31'
  4471. )),
  4472. array('Post' => array(
  4473. 'id' => '2',
  4474. 'author_id' => 3,
  4475. 'title' => 'Second Post',
  4476. 'body' => 'Second Post Body',
  4477. 'published' => 'Y',
  4478. 'created' => '2007-03-18 10:41:23',
  4479. 'updated' => '2007-03-18 10:43:31'
  4480. )),
  4481. array('Post' => array(
  4482. 'id' => '3',
  4483. 'author_id' => 1,
  4484. 'title' => 'Third Post',
  4485. 'body' => 'Third Post Body',
  4486. 'published' => 'Y',
  4487. 'created' => '2007-03-18 10:43:23',
  4488. 'updated' => '2007-03-18 10:45:31'
  4489. )));
  4490. if (count($result) != 3) {
  4491. // Database doesn't support transactions
  4492. $expected[] = array(
  4493. 'Post' => array(
  4494. 'id' => '4',
  4495. 'author_id' => 1,
  4496. 'title' => 'New Fourth Post',
  4497. 'body' => 'Third Post Body',
  4498. 'published' => 'N'
  4499. ));
  4500. $expected[] = array(
  4501. 'Post' => array(
  4502. 'id' => '5',
  4503. 'author_id' => 1,
  4504. 'title' => 'Third Post',
  4505. 'body' => 'Third Post Body',
  4506. 'published' => 'N'
  4507. ));
  4508. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  4509. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  4510. $this->assertEquals(self::date(), $result[4]['Post']['created']);
  4511. $this->assertEquals(self::date(), $result[4]['Post']['updated']);
  4512. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  4513. unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
  4514. }
  4515. $this->assertEquals($expected, $result);
  4516. $TestModel->validate = array('title' => 'notEmpty');
  4517. $data = array(
  4518. array('author_id' => 1, 'title' => 'New Fourth Post'),
  4519. array('author_id' => 1, 'title' => 'New Fifth Post'),
  4520. array('author_id' => 1, 'title' => 'New Sixth Post')
  4521. );
  4522. $this->assertTrue($TestModel->saveMany($data));
  4523. $result = $TestModel->find('all', array(
  4524. 'recursive' => -1,
  4525. 'fields' => array('author_id', 'title','body','published'),
  4526. 'order' => array('Post.created' => 'ASC')
  4527. ));
  4528. $expected = array(
  4529. array('Post' => array(
  4530. 'author_id' => 1,
  4531. 'title' => 'First Post',
  4532. 'body' => 'First Post Body',
  4533. 'published' => 'Y'
  4534. )),
  4535. array('Post' => array(
  4536. 'author_id' => 3,
  4537. 'title' => 'Second Post',
  4538. 'body' => 'Second Post Body',
  4539. 'published' => 'Y'
  4540. )),
  4541. array('Post' => array(
  4542. 'author_id' => 1,
  4543. 'title' => 'Third Post',
  4544. 'body' => 'Third Post Body',
  4545. 'published' => 'Y'
  4546. )),
  4547. array('Post' => array(
  4548. 'author_id' => 1,
  4549. 'title' => 'New Fourth Post',
  4550. 'body' => '',
  4551. 'published' => 'N'
  4552. )),
  4553. array('Post' => array(
  4554. 'author_id' => 1,
  4555. 'title' => 'New Fifth Post',
  4556. 'body' => '',
  4557. 'published' => 'N'
  4558. )),
  4559. array('Post' => array(
  4560. 'author_id' => 1,
  4561. 'title' => 'New Sixth Post',
  4562. 'body' => '',
  4563. 'published' => 'N'
  4564. )));
  4565. $this->assertEquals($expected, $result);
  4566. }
  4567. /**
  4568. * testSaveManyValidation method
  4569. *
  4570. * @return void
  4571. */
  4572. public function testSaveManyValidation() {
  4573. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  4574. $TestModel = new Post();
  4575. $data = array(
  4576. array(
  4577. 'id' => '1',
  4578. 'title' => 'Baleeted First Post',
  4579. 'body' => 'Baleeted!',
  4580. 'published' => 'N'
  4581. ),
  4582. array(
  4583. 'id' => '2',
  4584. 'title' => 'Just update the title'
  4585. ),
  4586. array(
  4587. 'title' => 'Creating a fourth post',
  4588. 'body' => 'Fourth post body',
  4589. 'author_id' => 2
  4590. ));
  4591. $this->assertTrue($TestModel->saveMany($data));
  4592. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  4593. $expected = array(
  4594. array(
  4595. 'Post' => array(
  4596. 'id' => '1',
  4597. 'author_id' => '1',
  4598. 'title' => 'Baleeted First Post',
  4599. 'body' => 'Baleeted!',
  4600. 'published' => 'N',
  4601. 'created' => '2007-03-18 10:39:23'
  4602. )
  4603. ),
  4604. array(
  4605. 'Post' => array(
  4606. 'id' => '2',
  4607. 'author_id' => '3',
  4608. 'title' => 'Just update the title',
  4609. 'body' => 'Second Post Body',
  4610. 'published' => 'Y',
  4611. 'created' => '2007-03-18 10:41:23'
  4612. )
  4613. ),
  4614. array(
  4615. 'Post' => array(
  4616. 'id' => '3',
  4617. 'author_id' => '1',
  4618. 'title' => 'Third Post',
  4619. 'body' => 'Third Post Body',
  4620. 'published' => 'Y',
  4621. 'created' => '2007-03-18 10:43:23',
  4622. 'updated' => '2007-03-18 10:45:31'
  4623. )),
  4624. array(
  4625. 'Post' => array(
  4626. 'id' => '4',
  4627. 'author_id' => '2',
  4628. 'title' => 'Creating a fourth post',
  4629. 'body' => 'Fourth post body',
  4630. 'published' => 'N'
  4631. )
  4632. )
  4633. );
  4634. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  4635. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  4636. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  4637. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  4638. unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
  4639. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  4640. $this->assertEquals($expected, $result);
  4641. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  4642. $data = array(
  4643. array(
  4644. 'id' => '1',
  4645. 'title' => 'Un-Baleeted First Post',
  4646. 'body' => 'Not Baleeted!',
  4647. 'published' => 'Y'
  4648. ),
  4649. array(
  4650. 'id' => '2',
  4651. 'title' => '',
  4652. 'body' => 'Trying to get away with an empty title'
  4653. ));
  4654. $result = $TestModel->saveMany($data);
  4655. $this->assertFalse($result);
  4656. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  4657. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  4658. $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
  4659. if (!$transactionWorked) {
  4660. $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
  4661. $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
  4662. }
  4663. $this->assertEquals($errors, $TestModel->validationErrors);
  4664. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  4665. $data = array(
  4666. array(
  4667. 'id' => '1',
  4668. 'title' => 'Un-Baleeted First Post',
  4669. 'body' => 'Not Baleeted!',
  4670. 'published' => 'Y'
  4671. ),
  4672. array(
  4673. 'id' => '2',
  4674. 'title' => '',
  4675. 'body' => 'Trying to get away with an empty title'
  4676. ));
  4677. $result = $TestModel->saveMany($data, array('validate' => true, 'atomic' => false));
  4678. $this->assertEquals(array(true, false), $result);
  4679. $result = $TestModel->find('all', array(
  4680. 'fields' => array('id', 'author_id', 'title', 'body', 'published'),
  4681. 'recursive' => -1,
  4682. 'order' => 'Post.id ASC'
  4683. ));
  4684. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  4685. $expected = array(
  4686. array(
  4687. 'Post' => array(
  4688. 'id' => '1',
  4689. 'author_id' => '1',
  4690. 'title' => 'Un-Baleeted First Post',
  4691. 'body' => 'Not Baleeted!',
  4692. 'published' => 'Y',
  4693. )),
  4694. array(
  4695. 'Post' => array(
  4696. 'id' => '2',
  4697. 'author_id' => '3',
  4698. 'title' => 'Just update the title',
  4699. 'body' => 'Second Post Body',
  4700. 'published' => 'Y',
  4701. )),
  4702. array(
  4703. 'Post' => array(
  4704. 'id' => '3',
  4705. 'author_id' => '1',
  4706. 'title' => 'Third Post',
  4707. 'body' => 'Third Post Body',
  4708. 'published' => 'Y',
  4709. )),
  4710. array(
  4711. 'Post' => array(
  4712. 'id' => '4',
  4713. 'author_id' => '2',
  4714. 'title' => 'Creating a fourth post',
  4715. 'body' => 'Fourth post body',
  4716. 'published' => 'N',
  4717. )));
  4718. $this->assertEquals($expected, $result);
  4719. $this->assertEquals($errors, $TestModel->validationErrors);
  4720. $data = array(
  4721. array(
  4722. 'id' => '1',
  4723. 'title' => 'Re-Baleeted First Post',
  4724. 'body' => 'Baleeted!',
  4725. 'published' => 'N'
  4726. ),
  4727. array(
  4728. 'id' => '2',
  4729. 'title' => '',
  4730. 'body' => 'Trying to get away with an empty title'
  4731. ));
  4732. $this->assertFalse($TestModel->saveMany($data, array('validate' => 'first')));
  4733. $result = $TestModel->find('all', array(
  4734. 'fields' => array('id', 'author_id', 'title', 'body', 'published'),
  4735. 'recursive' => -1,
  4736. 'order' => 'Post.id ASC'
  4737. ));
  4738. $this->assertEquals($expected, $result);
  4739. $this->assertEquals($errors, $TestModel->validationErrors);
  4740. }
  4741. /**
  4742. * testSaveAssociatedValidateFirst method
  4743. *
  4744. * @return void
  4745. */
  4746. public function testSaveAssociatedValidateFirst() {
  4747. $this->loadFixtures('Article', 'Comment', 'Attachment');
  4748. $model = new Article();
  4749. $model->deleteAll(true);
  4750. $model->Comment->validate = array('comment' => 'notEmpty');
  4751. $result = $model->saveAssociated(array(
  4752. 'Article' => array(
  4753. 'title' => 'Post with Author',
  4754. 'body' => 'This post will be saved author'
  4755. ),
  4756. 'Comment' => array(
  4757. array('comment' => 'First new comment'),
  4758. array('comment' => '')
  4759. )
  4760. ), array('validate' => 'first'));
  4761. $this->assertFalse($result);
  4762. $result = $model->find('all');
  4763. $this->assertEquals(array(), $result);
  4764. $expected = array('Comment' => array(
  4765. 1 => array('comment' => array('This field cannot be left blank'))
  4766. ));
  4767. $this->assertEquals($expected['Comment'], $model->Comment->validationErrors);
  4768. $this->assertSame($model->Comment->find('count'), 0);
  4769. $result = $model->saveAssociated(
  4770. array(
  4771. 'Article' => array(
  4772. 'title' => 'Post with Author',
  4773. 'body' => 'This post will be saved with an author',
  4774. 'user_id' => 2
  4775. ),
  4776. 'Comment' => array(
  4777. array(
  4778. 'comment' => 'Only new comment',
  4779. 'user_id' => 2
  4780. ))),
  4781. array('validate' => 'first')
  4782. );
  4783. $this->assertSame($result, true);
  4784. $result = $model->Comment->find('all');
  4785. $this->assertSame(count($result), 1);
  4786. $result = Hash::extract($result, '{n}.Comment.article_id');
  4787. $this->assertEquals(4, $result[0]);
  4788. $model->deleteAll(true);
  4789. $data = array(
  4790. 'Article' => array(
  4791. 'title' => 'Post with Author saveAlled from comment',
  4792. 'body' => 'This post will be saved with an author',
  4793. 'user_id' => 2
  4794. ),
  4795. 'Comment' => array(
  4796. 'comment' => 'Only new comment', 'user_id' => 2
  4797. ));
  4798. $result = $model->Comment->saveAssociated($data, array('validate' => 'first'));
  4799. $this->assertFalse(empty($result));
  4800. $result = $model->find('all');
  4801. $this->assertEquals(
  4802. 'Post with Author saveAlled from comment',
  4803. $result[0]['Article']['title']
  4804. );
  4805. $this->assertEquals('Only new comment', $result[0]['Comment'][0]['comment']);
  4806. }
  4807. /**
  4808. * test saveMany()'s return is correct when using atomic = false and validate = first.
  4809. *
  4810. * @return void
  4811. */
  4812. public function testSaveManyValidateFirstAtomicFalse() {
  4813. $Something = new Something();
  4814. $invalidData = array(
  4815. array(
  4816. 'title' => 'foo',
  4817. 'body' => 'bar',
  4818. 'published' => 'baz',
  4819. ),
  4820. array(
  4821. 'body' => 3,
  4822. 'published' => 'sd',
  4823. ),
  4824. );
  4825. $Something->create();
  4826. $Something->validate = array(
  4827. 'title' => array(
  4828. 'rule' => 'alphaNumeric',
  4829. 'required' => true,
  4830. ),
  4831. 'body' => array(
  4832. 'rule' => 'alphaNumeric',
  4833. 'required' => true,
  4834. 'allowEmpty' => true,
  4835. ),
  4836. );
  4837. $result = $Something->saveMany($invalidData, array(
  4838. 'atomic' => false,
  4839. 'validate' => 'first',
  4840. ));
  4841. $expected = array(true, false);
  4842. $this->assertEquals($expected, $result);
  4843. $Something = new Something();
  4844. $validData = array(
  4845. array(
  4846. 'title' => 'title value',
  4847. 'body' => 'body value',
  4848. 'published' => 'baz',
  4849. ),
  4850. array(
  4851. 'title' => 'valid',
  4852. 'body' => 'this body',
  4853. 'published' => 'sd',
  4854. ),
  4855. );
  4856. $Something->create();
  4857. $result = $Something->saveMany($validData, array(
  4858. 'atomic' => false,
  4859. 'validate' => 'first',
  4860. ));
  4861. $expected = array(true, true);
  4862. $this->assertEquals($expected, $result);
  4863. }
  4864. /**
  4865. * test that saveMany behaves like plain save() when suplied empty data
  4866. *
  4867. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  4868. * @return void
  4869. */
  4870. public function testSaveManyEmptyData() {
  4871. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  4872. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  4873. $model = new Article();
  4874. $result = $model->saveMany(array(), array('validate' => true));
  4875. $this->assertFalse(empty($result));
  4876. $model = new ProductUpdateAll();
  4877. $result = $model->saveMany(array());
  4878. $this->assertFalse($result);
  4879. }
  4880. /**
  4881. * test that saveAssociated behaves like plain save() when supplied empty data
  4882. *
  4883. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  4884. * @return void
  4885. */
  4886. public function testSaveAssociatedEmptyData() {
  4887. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  4888. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  4889. $model = new Article();
  4890. $result = $model->saveAssociated(array(), array('validate' => true));
  4891. $this->assertFalse(empty($result));
  4892. $model = new ProductUpdateAll();
  4893. $result = $model->saveAssociated(array());
  4894. $this->assertFalse($result);
  4895. }
  4896. /**
  4897. * testUpdateWithCalculation method
  4898. *
  4899. * @return void
  4900. */
  4901. public function testUpdateWithCalculation() {
  4902. $this->loadFixtures('DataTest');
  4903. $model = new DataTest();
  4904. $model->deleteAll(true);
  4905. $result = $model->saveMany(array(
  4906. array('count' => 5, 'float' => 1.1),
  4907. array('count' => 3, 'float' => 1.2),
  4908. array('count' => 4, 'float' => 1.3),
  4909. array('count' => 1, 'float' => 2.0),
  4910. ));
  4911. $this->assertFalse(empty($result));
  4912. $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
  4913. $this->assertEquals(array(5, 3, 4, 1), $result);
  4914. $this->assertTrue($model->updateAll(array('count' => 'count + 2')));
  4915. $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
  4916. $this->assertEquals(array(7, 5, 6, 3), $result);
  4917. $this->assertTrue($model->updateAll(array('DataTest.count' => 'DataTest.count - 1')));
  4918. $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
  4919. $this->assertEquals(array(6, 4, 5, 2), $result);
  4920. }
  4921. public function testToggleBoolFields() {
  4922. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  4923. $Post = new CounterCachePost();
  4924. $Post->unbindModel(array('belongsTo' => array('User')), true);
  4925. $true = array('Post' => array('published' => true, 'id' => 2));
  4926. $false = array('Post' => array('published' => false, 'id' => 2));
  4927. $fields = array('Post.published', 'Post.id');
  4928. $updateConditions = array('Post.id' => 2);
  4929. // check its true
  4930. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  4931. $this->assertEquals($true, $result);
  4932. // Testing without the alias
  4933. $this->assertTrue($Post->updateAll(array('published' => 'NOT published'), $updateConditions));
  4934. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  4935. $this->assertEquals($false, $result);
  4936. $this->assertTrue($Post->updateAll(array('published' => 'NOT published'), $updateConditions));
  4937. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  4938. $this->assertEquals($true, $result);
  4939. $db = ConnectionManager::getDataSource('test');
  4940. $alias = $db->name('Post.published');
  4941. // Testing with the alias
  4942. $this->assertTrue($Post->updateAll(array('Post.published' => "NOT $alias"), $updateConditions));
  4943. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  4944. $this->assertEquals($false, $result);
  4945. $this->assertTrue($Post->updateAll(array('Post.published' => "NOT $alias"), $updateConditions));
  4946. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  4947. $this->assertEquals($true, $result);
  4948. }
  4949. /**
  4950. * TestFindAllWithoutForeignKey
  4951. *
  4952. * @return void
  4953. */
  4954. public function testFindAllForeignKey() {
  4955. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  4956. $ProductUpdateAll = new ProductUpdateAll();
  4957. $conditions = array('Group.name' => 'group one');
  4958. $ProductUpdateAll->bindModel(array(
  4959. 'belongsTo' => array(
  4960. 'Group' => array('className' => 'GroupUpdateAll')
  4961. )
  4962. ));
  4963. $ProductUpdateAll->belongsTo = array(
  4964. 'Group' => array('className' => 'GroupUpdateAll', 'foreignKey' => 'group_id')
  4965. );
  4966. $results = $ProductUpdateAll->find('all', compact('conditions'));
  4967. $this->assertTrue(!empty($results));
  4968. $ProductUpdateAll->bindModel(array('belongsTo' => array('Group')));
  4969. $ProductUpdateAll->belongsTo = array(
  4970. 'Group' => array(
  4971. 'className' => 'GroupUpdateAll',
  4972. 'foreignKey' => false,
  4973. 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
  4974. ));
  4975. $resultsFkFalse = $ProductUpdateAll->find('all', compact('conditions'));
  4976. $this->assertTrue(!empty($resultsFkFalse));
  4977. $expected = array(
  4978. '0' => array(
  4979. 'ProductUpdateAll' => array(
  4980. 'id' => 1,
  4981. 'name' => 'product one',
  4982. 'groupcode' => 120,
  4983. 'group_id' => 1),
  4984. 'Group' => array(
  4985. 'id' => 1,
  4986. 'name' => 'group one',
  4987. 'code' => 120)
  4988. ),
  4989. '1' => array(
  4990. 'ProductUpdateAll' => array(
  4991. 'id' => 2,
  4992. 'name' => 'product two',
  4993. 'groupcode' => 120,
  4994. 'group_id' => 1),
  4995. 'Group' => array(
  4996. 'id' => 1,
  4997. 'name' => 'group one',
  4998. 'code' => 120)
  4999. )
  5000. );
  5001. $this->assertEquals($expected, $results);
  5002. $this->assertEquals($expected, $resultsFkFalse);
  5003. }
  5004. /**
  5005. * test updateAll with empty values.
  5006. *
  5007. * @return void
  5008. */
  5009. public function testUpdateAllEmptyValues() {
  5010. $this->skipIf($this->db instanceof Sqlserver || $this->db instanceof Postgres, 'This test is not compatible with Postgres or SQL Server.');
  5011. $this->loadFixtures('Author', 'Post');
  5012. $model = new Author();
  5013. $result = $model->updateAll(array('user' => '""'));
  5014. $this->assertTrue($result);
  5015. }
  5016. /**
  5017. * testUpdateAllWithJoins
  5018. *
  5019. * @return void
  5020. */
  5021. public function testUpdateAllWithJoins() {
  5022. $this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql or sqlite');
  5023. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  5024. $ProductUpdateAll = new ProductUpdateAll();
  5025. $conditions = array('Group.name' => 'group one');
  5026. $ProductUpdateAll->bindModel(array('belongsTo' => array(
  5027. 'Group' => array('className' => 'GroupUpdateAll')))
  5028. );
  5029. $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
  5030. $results = $ProductUpdateAll->find('all', array(
  5031. 'conditions' => array('ProductUpdateAll.name' => 'new product')
  5032. ));
  5033. $expected = array(
  5034. '0' => array(
  5035. 'ProductUpdateAll' => array(
  5036. 'id' => 1,
  5037. 'name' => 'new product',
  5038. 'groupcode' => 120,
  5039. 'group_id' => 1),
  5040. 'Group' => array(
  5041. 'id' => 1,
  5042. 'name' => 'group one',
  5043. 'code' => 120)
  5044. ),
  5045. '1' => array(
  5046. 'ProductUpdateAll' => array(
  5047. 'id' => 2,
  5048. 'name' => 'new product',
  5049. 'groupcode' => 120,
  5050. 'group_id' => 1),
  5051. 'Group' => array(
  5052. 'id' => 1,
  5053. 'name' => 'group one',
  5054. 'code' => 120)));
  5055. $this->assertEquals($expected, $results);
  5056. }
  5057. /**
  5058. * testUpdateAllWithoutForeignKey
  5059. *
  5060. * @return void
  5061. */
  5062. public function testUpdateAllWithoutForeignKey() {
  5063. $this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql');
  5064. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  5065. $ProductUpdateAll = new ProductUpdateAll();
  5066. $conditions = array('Group.name' => 'group one');
  5067. $ProductUpdateAll->bindModel(array('belongsTo' => array(
  5068. 'Group' => array('className' => 'GroupUpdateAll')
  5069. )));
  5070. $ProductUpdateAll->belongsTo = array(
  5071. 'Group' => array(
  5072. 'className' => 'GroupUpdateAll',
  5073. 'foreignKey' => false,
  5074. 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
  5075. )
  5076. );
  5077. $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
  5078. $resultsFkFalse = $ProductUpdateAll->find('all', array('conditions' => array('ProductUpdateAll.name' => 'new product')));
  5079. $expected = array(
  5080. '0' => array(
  5081. 'ProductUpdateAll' => array(
  5082. 'id' => 1,
  5083. 'name' => 'new product',
  5084. 'groupcode' => 120,
  5085. 'group_id' => 1),
  5086. 'Group' => array(
  5087. 'id' => 1,
  5088. 'name' => 'group one',
  5089. 'code' => 120)
  5090. ),
  5091. '1' => array(
  5092. 'ProductUpdateAll' => array(
  5093. 'id' => 2,
  5094. 'name' => 'new product',
  5095. 'groupcode' => 120,
  5096. 'group_id' => 1),
  5097. 'Group' => array(
  5098. 'id' => 1,
  5099. 'name' => 'group one',
  5100. 'code' => 120)));
  5101. $this->assertEquals($expected, $resultsFkFalse);
  5102. }
  5103. /**
  5104. * test writing floats in german locale.
  5105. *
  5106. * @return void
  5107. */
  5108. public function testWriteFloatAsGerman() {
  5109. $restore = setlocale(LC_ALL, 0);
  5110. setlocale(LC_ALL, 'de_DE');
  5111. $model = new DataTest();
  5112. $result = $model->save(array(
  5113. 'count' => 1,
  5114. 'float' => 3.14593
  5115. ));
  5116. $this->assertTrue((bool)$result);
  5117. setlocale(LC_ALL, $restore);
  5118. }
  5119. /**
  5120. * Test returned array contains primary key when save creates a new record
  5121. *
  5122. * @return void
  5123. */
  5124. public function testPkInReturnArrayForCreate() {
  5125. $this->loadFixtures('Article');
  5126. $TestModel = new Article();
  5127. $data = array('Article' => array(
  5128. 'user_id' => '1',
  5129. 'title' => 'Fourth Article',
  5130. 'body' => 'Fourth Article Body',
  5131. 'published' => 'Y'
  5132. ));
  5133. $result = $TestModel->save($data);
  5134. $this->assertSame($result['Article']['id'], $TestModel->id);
  5135. }
  5136. /**
  5137. * testSaveAllFieldListValidateBelongsTo
  5138. *
  5139. * @return void
  5140. */
  5141. public function testSaveAllFieldListValidateBelongsTo() {
  5142. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  5143. $TestModel = new Post();
  5144. $result = $TestModel->find('all');
  5145. $this->assertEquals(3, count($result));
  5146. $this->assertFalse(isset($result[3]));
  5147. // test belongsTo
  5148. $fieldList = array(
  5149. 'Post' => array('title', 'author_id'),
  5150. 'Author' => array('user')
  5151. );
  5152. $TestModel->saveAll(array(
  5153. 'Post' => array(
  5154. 'title' => 'Post without body',
  5155. 'body' => 'This will not be saved',
  5156. ),
  5157. 'Author' => array(
  5158. 'user' => 'bob',
  5159. 'test' => 'This will not be saved',
  5160. )), array('fieldList' => $fieldList));
  5161. $result = $TestModel->find('all');
  5162. $expected = array(
  5163. 'Post' => array (
  5164. 'id' => '4',
  5165. 'author_id' => '5',
  5166. 'title' => 'Post without body',
  5167. 'body' => null,
  5168. 'published' => 'N',
  5169. 'created' => self::date(),
  5170. 'updated' => self::date(),
  5171. ),
  5172. 'Author' => array (
  5173. 'id' => '5',
  5174. 'user' => 'bob',
  5175. 'password' => null,
  5176. 'created' => self::date(),
  5177. 'updated' => self::date(),
  5178. 'test' => 'working',
  5179. ),
  5180. );
  5181. $this->assertEquals($expected, $result[3]);
  5182. $this->assertEquals(4, count($result));
  5183. $this->assertEquals('', $result[3]['Post']['body']);
  5184. $this->assertEquals('working', $result[3]['Author']['test']);
  5185. // test multirecord
  5186. $this->db->truncate($TestModel);
  5187. $fieldList = array('title', 'author_id');
  5188. $TestModel->saveAll(array(
  5189. array(
  5190. 'title' => 'Multi-record post 1',
  5191. 'body' => 'First multi-record post',
  5192. 'author_id' => 2
  5193. ),
  5194. array(
  5195. 'title' => 'Multi-record post 2',
  5196. 'body' => 'Second multi-record post',
  5197. 'author_id' => 2
  5198. )), array('fieldList' => $fieldList));
  5199. $result = $TestModel->find('all', array(
  5200. 'recursive' => -1,
  5201. 'order' => 'Post.id ASC'
  5202. ));
  5203. $expected = array(
  5204. array(
  5205. 'Post' => array(
  5206. 'id' => '1',
  5207. 'author_id' => '2',
  5208. 'title' => 'Multi-record post 1',
  5209. 'body' => '',
  5210. 'published' => 'N',
  5211. 'created' => self::date(),
  5212. 'updated' => self::date()
  5213. )
  5214. ),
  5215. array(
  5216. 'Post' => array(
  5217. 'id' => '2',
  5218. 'author_id' => '2',
  5219. 'title' => 'Multi-record post 2',
  5220. 'body' => '',
  5221. 'published' => 'N',
  5222. 'created' => self::date(),
  5223. 'updated' => self::date()
  5224. )
  5225. )
  5226. );
  5227. $this->assertEquals($expected, $result);
  5228. }
  5229. /**
  5230. * testSaveAllFieldListHasMany method
  5231. *
  5232. * return @void
  5233. */
  5234. public function testSaveAllFieldListHasMany() {
  5235. $this->loadFixtures('Article', 'Comment');
  5236. $TestModel = new Article();
  5237. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  5238. $this->db->truncate($TestModel);
  5239. $this->db->truncate(new Comment());
  5240. $fieldList = array(
  5241. 'Article' => array('id'),
  5242. 'Comment' => array('article_id', 'user_id')
  5243. );
  5244. $result = $TestModel->saveAll(array(
  5245. 'Article' => array('id' => 2, 'title' => 'I will not save'),
  5246. 'Comment' => array(
  5247. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  5248. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  5249. )
  5250. ), array('fieldList' => $fieldList));
  5251. $result = $TestModel->find('all');
  5252. $this->assertEquals('', $result[0]['Article']['title']);
  5253. $this->assertEquals('', $result[0]['Comment'][0]['comment']);
  5254. $this->assertEquals('', $result[0]['Comment'][1]['comment']);
  5255. }
  5256. /**
  5257. * testSaveAllDeepFieldListValidateBelongsTo
  5258. *
  5259. * @return void
  5260. */
  5261. public function testSaveAllDeepFieldListValidateBelongsTo() {
  5262. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  5263. $TestModel = new Post();
  5264. $TestModel->Author->bindModel(array('hasMany' => array('Comment' => array('foreignKey' => 'user_id'))), false);
  5265. $TestModel->recursive = 2;
  5266. $result = $TestModel->find('all');
  5267. $this->assertEquals(3, count($result));
  5268. $this->assertFalse(isset($result[3]));
  5269. // test belongsTo
  5270. $fieldList = array(
  5271. 'Post' => array('title', 'author_id'),
  5272. 'Author' => array('user'),
  5273. 'Comment' => array('comment')
  5274. );
  5275. $TestModel->saveAll(array(
  5276. 'Post' => array(
  5277. 'title' => 'Post without body',
  5278. 'body' => 'This will not be saved',
  5279. ),
  5280. 'Author' => array(
  5281. 'user' => 'bob',
  5282. 'test' => 'This will not be saved',
  5283. 'Comment' => array(
  5284. array('id' => 5, 'comment' => 'I am still published', 'published' => 'N'))
  5285. )), array('fieldList' => $fieldList, 'deep' => true));
  5286. $result = $TestModel->Author->Comment->find('first', array(
  5287. 'conditions' => array('Comment.id' => 5),
  5288. 'fields' => array('comment', 'published')
  5289. ));
  5290. $expected = array(
  5291. 'Comment' => array(
  5292. 'comment' => 'I am still published',
  5293. 'published' => 'Y'
  5294. )
  5295. );
  5296. $this->assertEquals($expected, $result);
  5297. }
  5298. /**
  5299. * testSaveAllDeepFieldListHasMany method
  5300. *
  5301. * return @void
  5302. */
  5303. public function testSaveAllDeepFieldListHasMany() {
  5304. $this->loadFixtures('Article', 'Comment', 'User');
  5305. $TestModel = new Article();
  5306. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  5307. $this->db->truncate($TestModel);
  5308. $this->db->truncate(new Comment());
  5309. $fieldList = array(
  5310. 'Article' => array('id'),
  5311. 'Comment' => array('article_id', 'user_id'),
  5312. 'User' => array('user')
  5313. );
  5314. $result = $TestModel->saveAll(array(
  5315. 'Article' => array('id' => 2, 'title' => 'I will not save'),
  5316. 'Comment' => array(
  5317. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  5318. array(
  5319. 'comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2,
  5320. 'User' => array('user' => 'nopassword', 'password' => 'not saved')
  5321. )
  5322. )
  5323. ), array('fieldList' => $fieldList, 'deep' => true));
  5324. $result = $TestModel->Comment->User->find('first', array(
  5325. 'conditions' => array('User.user' => 'nopassword'),
  5326. 'fields' => array('user', 'password')
  5327. ));
  5328. $expected = array(
  5329. 'User' => array(
  5330. 'user' => 'nopassword',
  5331. 'password' => ''
  5332. )
  5333. );
  5334. $this->assertEquals($expected, $result);
  5335. }
  5336. /**
  5337. * testSaveAllDeepHasManyBelongsTo method
  5338. *
  5339. * return @void
  5340. */
  5341. public function testSaveAllDeepHasManyBelongsTo() {
  5342. $this->loadFixtures('Article', 'Comment', 'User');
  5343. $TestModel = new Article();
  5344. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  5345. $this->db->truncate($TestModel);
  5346. $this->db->truncate(new Comment());
  5347. $result = $TestModel->saveAll(array(
  5348. 'Article' => array('id' => 2, 'title' => 'The title'),
  5349. 'Comment' => array(
  5350. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  5351. array(
  5352. 'comment' => 'belongsto', 'published' => 'Y',
  5353. 'User' => array('user' => 'findme', 'password' => 'somepass')
  5354. )
  5355. )
  5356. ), array('deep' => true));
  5357. $result = $TestModel->Comment->User->find('first', array(
  5358. 'conditions' => array('User.user' => 'findme'),
  5359. 'fields' => array('id', 'user', 'password')
  5360. ));
  5361. $expected = array(
  5362. 'User' => array(
  5363. 'id' => 5,
  5364. 'user' => 'findme',
  5365. 'password' => 'somepass',
  5366. )
  5367. );
  5368. $this->assertEquals($expected, $result);
  5369. $result = $TestModel->Comment->find('first', array(
  5370. 'conditions' => array('Comment.user_id' => 5),
  5371. 'fields' => array('id', 'comment', 'published', 'user_id')
  5372. ));
  5373. $expected = array(
  5374. 'Comment' => array(
  5375. 'id' => 2,
  5376. 'comment' => 'belongsto',
  5377. 'published' => 'Y',
  5378. 'user_id' => 5
  5379. )
  5380. );
  5381. $this->assertEquals($expected, $result);
  5382. }
  5383. /**
  5384. * testSaveAllDeepHasManyhasMany method
  5385. *
  5386. * return @void
  5387. */
  5388. public function testSaveAllDeepHasManyHasMany() {
  5389. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  5390. $TestModel = new Article();
  5391. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
  5392. $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
  5393. $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
  5394. $this->db->truncate($TestModel);
  5395. $this->db->truncate(new Comment());
  5396. $this->db->truncate(new Attachment());
  5397. $result = $TestModel->saveAll(array(
  5398. 'Article' => array('id' => 2, 'title' => 'The title'),
  5399. 'Comment' => array(
  5400. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  5401. array(
  5402. 'comment' => 'hasmany', 'published' => 'Y', 'user_id' => 5,
  5403. 'Attachment' => array(
  5404. array('attachment' => 'first deep attachment'),
  5405. array('attachment' => 'second deep attachment'),
  5406. )
  5407. )
  5408. )
  5409. ), array('deep' => true));
  5410. $result = $TestModel->Comment->find('first', array(
  5411. 'conditions' => array('Comment.comment' => 'hasmany'),
  5412. 'fields' => array('id', 'comment', 'published', 'user_id'),
  5413. 'recursive' => -1
  5414. ));
  5415. $expected = array(
  5416. 'Comment' => array(
  5417. 'id' => 2,
  5418. 'comment' => 'hasmany',
  5419. 'published' => 'Y',
  5420. 'user_id' => 5
  5421. )
  5422. );
  5423. $this->assertEquals($expected, $result);
  5424. $result = $TestModel->Comment->Attachment->find('all', array(
  5425. 'fields' => array('attachment', 'comment_id'),
  5426. 'order' => array('Attachment.id' => 'ASC')
  5427. ));
  5428. $expected = array(
  5429. array('Attachment' => array('attachment' => 'first deep attachment', 'comment_id' => 2)),
  5430. array('Attachment' => array('attachment' => 'second deep attachment', 'comment_id' => 2)),
  5431. );
  5432. $this->assertEquals($expected, $result);
  5433. }
  5434. /**
  5435. * testSaveAllDeepOrderHasManyHasMany method
  5436. *
  5437. * return @void
  5438. */
  5439. public function testSaveAllDeepOrderHasManyHasMany() {
  5440. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  5441. $TestModel = new Article();
  5442. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
  5443. $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
  5444. $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
  5445. $this->db->truncate($TestModel);
  5446. $this->db->truncate(new Comment());
  5447. $this->db->truncate(new Attachment());
  5448. $result = $TestModel->saveAll(array(
  5449. 'Article' => array('id' => 2, 'title' => 'Comment has its data after Attachment'),
  5450. 'Comment' => array(
  5451. array(
  5452. 'Attachment' => array(
  5453. array('attachment' => 'attachment should be created with comment_id'),
  5454. array('attachment' => 'comment should be created with article_id'),
  5455. ),
  5456. 'comment' => 'after associated data',
  5457. 'user_id' => 1
  5458. )
  5459. )
  5460. ), array('deep' => true));
  5461. $result = $TestModel->Comment->find('first', array(
  5462. 'conditions' => array('Comment.article_id' => 2),
  5463. ));
  5464. $this->assertEquals(2, $result['Comment']['article_id']);
  5465. $this->assertEquals(2, count($result['Attachment']));
  5466. }
  5467. /**
  5468. * testSaveAllDeepEmptyHasManyHasMany method
  5469. *
  5470. * return @void
  5471. */
  5472. public function testSaveAllDeepEmptyHasManyHasMany() {
  5473. $this->skipIf(!$this->db instanceof Mysql, 'This test is only compatible with Mysql.');
  5474. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  5475. $TestModel = new Article();
  5476. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
  5477. $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
  5478. $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
  5479. $this->db->truncate($TestModel);
  5480. $this->db->truncate(new Comment());
  5481. $this->db->truncate(new Attachment());
  5482. $result = $TestModel->saveAll(array(
  5483. 'Article' => array('id' => 3, 'title' => 'Comment has no data'),
  5484. 'Comment' => array(
  5485. array(
  5486. 'Attachment' => array(
  5487. array('attachment' => 'attachment should be created with comment_id'),
  5488. array('attachment' => 'comment should be created with article_id'),
  5489. ),
  5490. )
  5491. )
  5492. ), array('deep' => true));
  5493. $result = $TestModel->Comment->find('first', array(
  5494. 'conditions' => array('Comment.article_id' => 3),
  5495. ));
  5496. $this->assertEquals(3, $result['Comment']['article_id']);
  5497. $this->assertEquals(2, count($result['Attachment']));
  5498. }
  5499. /**
  5500. * testUpdateAllBoolean
  5501. *
  5502. * return @void
  5503. */
  5504. public function testUpdateAllBoolean() {
  5505. $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
  5506. $TestModel = new Item();
  5507. $result = $TestModel->updateAll(array('published' => true));
  5508. $this->assertTrue($result);
  5509. $result = $TestModel->find('first', array('fields' => array('id', 'published')));
  5510. $this->assertEquals(true, $result['Item']['published']);
  5511. }
  5512. /**
  5513. * testUpdateAllBooleanConditions
  5514. *
  5515. * return @void
  5516. */
  5517. public function testUpdateAllBooleanConditions() {
  5518. $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
  5519. $TestModel = new Item();
  5520. $result = $TestModel->updateAll(array('published' => true), array('Item.id' => 1));
  5521. $this->assertTrue($result);
  5522. $result = $TestModel->find('first', array(
  5523. 'fields' => array('id', 'published'),
  5524. 'conditions' => array('Item.id' => 1)));
  5525. $this->assertEquals(true, $result['Item']['published']);
  5526. }
  5527. /**
  5528. * testUpdateBoolean
  5529. *
  5530. * return @void
  5531. */
  5532. public function testUpdateBoolean() {
  5533. $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
  5534. $TestModel = new Item();
  5535. $result = $TestModel->save(array('published' => true, 'id' => 1));
  5536. $this->assertTrue((boolean)$result);
  5537. $result = $TestModel->find('first', array(
  5538. 'fields' => array('id', 'published'),
  5539. 'conditions' => array('Item.id' => 1)));
  5540. $this->assertEquals(true, $result['Item']['published']);
  5541. }
  5542. }