1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033 |
- <?php
- /**
- * ModelWriteTest file
- *
- * PHP 5
- *
- * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
- * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
- * @package Cake.Test.Case.Model
- * @since CakePHP(tm) v 1.2.0.4206
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
- /**
- * ModelWriteTest
- *
- * @package Cake.Test.Case.Model
- */
- class ModelWriteTest extends BaseModelTest {
- /**
- * Test save() failing when there is no data.
- *
- * @return void
- */
- public function testInsertNoData() {
- $this->loadFixtures('Bid');
- $Bid = ClassRegistry::init('Bid');
- $this->assertFalse($Bid->save());
- $result = $Bid->save(array('Bid' => array()));
- $this->assertFalse($result);
- $result = $Bid->save(array('Bid' => array('not in schema' => 1)));
- $this->assertFalse($result);
- }
- /**
- * testInsertAnotherHabtmRecordWithSameForeignKey method
- *
- * @access public
- * @return void
- */
- public function testInsertAnotherHabtmRecordWithSameForeignKey() {
- $this->loadFixtures('JoinA', 'JoinB', 'JoinAB', 'JoinC', 'JoinAC');
- $TestModel = new JoinA();
- $result = $TestModel->JoinAsJoinB->findById(1);
- $expected = array(
- 'JoinAsJoinB' => array(
- 'id' => 1,
- 'join_a_id' => 1,
- 'join_b_id' => 2,
- 'other' => 'Data for Join A 1 Join B 2',
- 'created' => '2008-01-03 10:56:33',
- 'updated' => '2008-01-03 10:56:33'
- ));
- $this->assertEquals($expected, $result);
- $TestModel->JoinAsJoinB->create();
- $data = array(
- 'join_a_id' => 1,
- 'join_b_id' => 1,
- 'other' => 'Data for Join A 1 Join B 1',
- 'created' => '2008-01-03 10:56:44',
- 'updated' => '2008-01-03 10:56:44'
- );
- $result = $TestModel->JoinAsJoinB->save($data);
- $lastInsertId = $TestModel->JoinAsJoinB->getLastInsertID();
- $data['id'] = $lastInsertId;
- $this->assertEquals(array('JoinAsJoinB' => $data), $result);
- $this->assertTrue($lastInsertId > 0);
- $result = $TestModel->JoinAsJoinB->findById(1);
- $expected = array(
- 'JoinAsJoinB' => array(
- 'id' => 1,
- 'join_a_id' => 1,
- 'join_b_id' => 2,
- 'other' => 'Data for Join A 1 Join B 2',
- 'created' => '2008-01-03 10:56:33',
- 'updated' => '2008-01-03 10:56:33'
- ));
- $this->assertEquals($expected, $result);
- $updatedValue = 'UPDATED Data for Join A 1 Join B 2';
- $TestModel->JoinAsJoinB->id = 1;
- $result = $TestModel->JoinAsJoinB->saveField('other', $updatedValue, false);
- $this->assertFalse(empty($result));
- $result = $TestModel->JoinAsJoinB->findById(1);
- $this->assertEquals($updatedValue, $result['JoinAsJoinB']['other']);
- }
- /**
- * testSaveDateAsFirstEntry method
- *
- * @return void
- */
- public function testSaveDateAsFirstEntry() {
- $this->loadFixtures('Article', 'User', 'Comment', 'Attachment', 'Tag', 'ArticlesTag');
- $Article = new Article();
- $data = array(
- 'Article' => array(
- 'created' => array(
- 'day' => '1',
- 'month' => '1',
- 'year' => '2008'
- ),
- 'title' => 'Test Title',
- 'user_id' => 1
- ));
- $Article->create();
- $result = $Article->save($data);
- $this->assertFalse(empty($result));
- $testResult = $Article->find('first', array('conditions' => array('Article.title' => 'Test Title')));
- $this->assertEquals($data['Article']['title'], $testResult['Article']['title']);
- $this->assertEquals('2008-01-01 00:00:00', $testResult['Article']['created']);
- }
- /**
- * testUnderscoreFieldSave method
- *
- * @return void
- */
- public function testUnderscoreFieldSave() {
- $this->loadFixtures('UnderscoreField');
- $UnderscoreField = new UnderscoreField();
- $currentCount = $UnderscoreField->find('count');
- $this->assertEquals(3, $currentCount);
- $data = array('UnderscoreField' => array(
- 'user_id' => '1',
- 'my_model_has_a_field' => 'Content here',
- 'body' => 'Body',
- 'published' => 'Y',
- 'another_field' => 4
- ));
- $ret = $UnderscoreField->save($data);
- $this->assertFalse(empty($ret));
- $currentCount = $UnderscoreField->find('count');
- $this->assertEquals(4, $currentCount);
- }
- /**
- * testAutoSaveUuid method
- *
- * @return void
- */
- public function testAutoSaveUuid() {
- // SQLite does not support non-integer primary keys
- $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with SQLite.');
- $this->loadFixtures('Uuid');
- $TestModel = new Uuid();
- $TestModel->save(array('title' => 'Test record'));
- $result = $TestModel->findByTitle('Test record');
- $this->assertEquals(
- array('id', 'title', 'count', 'created', 'updated'),
- array_keys($result['Uuid'])
- );
- $this->assertEquals(36, strlen($result['Uuid']['id']));
- }
- /**
- * Ensure that if the id key is null but present the save doesn't fail (with an
- * x sql error: "Column id specified twice")
- *
- * @return void
- */
- public function testSaveUuidNull() {
- // SQLite does not support non-integer primary keys
- $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with SQLite.');
- $this->loadFixtures('Uuid');
- $TestModel = new Uuid();
- $TestModel->save(array('title' => 'Test record', 'id' => null));
- $result = $TestModel->findByTitle('Test record');
- $this->assertEquals(
- array('id', 'title', 'count', 'created', 'updated'),
- array_keys($result['Uuid'])
- );
- $this->assertEquals(36, strlen($result['Uuid']['id']));
- }
- /**
- * testZeroDefaultFieldValue method
- *
- * @return void
- */
- public function testZeroDefaultFieldValue() {
- $this->skipIf($this->db instanceof Sqlite, 'SQLite uses loose typing, this operation is unsupported.');
- $this->loadFixtures('DataTest');
- $TestModel = new DataTest();
- $TestModel->create(array());
- $TestModel->save();
- $result = $TestModel->findById($TestModel->id);
- $this->assertEquals(0, $result['DataTest']['count']);
- $this->assertEquals(0, $result['DataTest']['float']);
- }
- /**
- * Tests validation parameter order in custom validation methods
- *
- * @return void
- */
- public function testAllowSimulatedFields() {
- $TestModel = new ValidationTest1();
- $TestModel->create(array(
- 'title' => 'foo',
- 'bar' => 'baz'
- ));
- $expected = array(
- 'ValidationTest1' => array(
- 'title' => 'foo',
- 'bar' => 'baz'
- ));
- $this->assertEquals($expected, $TestModel->data);
- }
- /**
- * test that Caches are getting cleared on save().
- * ensure that both inflections of controller names are getting cleared
- * as url for controller could be either overallFavorites/index or overall_favorites/index
- *
- * @return void
- */
- public function testCacheClearOnSave() {
- $_back = array(
- 'check' => Configure::read('Cache.check'),
- 'disable' => Configure::read('Cache.disable'),
- );
- Configure::write('Cache.check', true);
- Configure::write('Cache.disable', false);
- $this->loadFixtures('OverallFavorite');
- $OverallFavorite = new OverallFavorite();
- touch(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php');
- touch(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php');
- $data = array(
- 'OverallFavorite' => array(
- 'id' => 22,
- 'model_type' => '8-track',
- 'model_id' => '3',
- 'priority' => '1'
- )
- );
- $OverallFavorite->create($data);
- $OverallFavorite->save();
- $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php'));
- $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php'));
- Configure::write('Cache.check', $_back['check']);
- Configure::write('Cache.disable', $_back['disable']);
- }
- /**
- * testSaveWithCounterCache method
- *
- * @return void
- */
- public function testSaveWithCounterCache() {
- $this->loadFixtures('Syfile', 'Item', 'Image', 'Portfolio', 'ItemsPortfolio');
- $TestModel = new Syfile();
- $TestModel2 = new Item();
- $result = $TestModel->findById(1);
- $this->assertSame($result['Syfile']['item_count'], null);
- $TestModel2->save(array(
- 'name' => 'Item 7',
- 'syfile_id' => 1,
- 'published' => false
- ));
- $result = $TestModel->findById(1);
- $this->assertEquals(2, $result['Syfile']['item_count']);
- $TestModel2->delete(1);
- $result = $TestModel->findById(1);
- $this->assertEquals(1, $result['Syfile']['item_count']);
- $TestModel2->id = 2;
- $TestModel2->saveField('syfile_id', 1);
- $result = $TestModel->findById(1);
- $this->assertEquals(2, $result['Syfile']['item_count']);
- $result = $TestModel->findById(2);
- $this->assertEquals(0, $result['Syfile']['item_count']);
- }
- /**
- * Tests that counter caches are updated when records are added
- *
- * @return void
- */
- public function testCounterCacheIncrease() {
- $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
- $User = new CounterCacheUser();
- $Post = new CounterCachePost();
- $data = array('Post' => array(
- 'id' => 22,
- 'title' => 'New Post',
- 'user_id' => 66
- ));
- $Post->save($data);
- $user = $User->find('first', array(
- 'conditions' => array('id' => 66),
- 'recursive' => -1
- ));
- $result = $user[$User->alias]['post_count'];
- $expected = 3;
- $this->assertEquals($expected, $result);
- }
- /**
- * Tests that counter caches are updated when records are deleted
- *
- * @return void
- */
- public function testCounterCacheDecrease() {
- $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
- $User = new CounterCacheUser();
- $Post = new CounterCachePost();
- $Post->delete(2);
- $user = $User->find('first', array(
- 'conditions' => array('id' => 66),
- 'recursive' => -1
- ));
- $result = $user[$User->alias]['post_count'];
- $expected = 1;
- $this->assertEquals($expected, $result);
- }
- /**
- * Tests that counter caches are updated when foreign keys of counted records change
- *
- * @return void
- */
- public function testCounterCacheUpdated() {
- $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
- $User = new CounterCacheUser();
- $Post = new CounterCachePost();
- $data = $Post->find('first', array(
- 'conditions' => array('id' => 1),
- 'recursive' => -1
- ));
- $data[$Post->alias]['user_id'] = 301;
- $Post->save($data);
- $users = $User->find('all',array('order' => 'User.id'));
- $this->assertEquals(1, $users[0]['User']['post_count']);
- $this->assertEquals(2, $users[1]['User']['post_count']);
- }
- /**
- * Test counter cache with models that use a non-standard (i.e. not using 'id')
- * as their primary key.
- *
- * @return void
- */
- public function testCounterCacheWithNonstandardPrimaryKey() {
- $this->loadFixtures(
- 'CounterCacheUserNonstandardPrimaryKey',
- 'CounterCachePostNonstandardPrimaryKey'
- );
- $User = new CounterCacheUserNonstandardPrimaryKey();
- $Post = new CounterCachePostNonstandardPrimaryKey();
- $data = $Post->find('first', array(
- 'conditions' => array('pid' => 1),
- 'recursive' => -1
- ));
- $data[$Post->alias]['uid'] = 301;
- $Post->save($data);
- $users = $User->find('all',array('order' => 'User.uid'));
- $this->assertEquals(1, $users[0]['User']['post_count']);
- $this->assertEquals(2, $users[1]['User']['post_count']);
- }
- /**
- * test Counter Cache With Self Joining table
- *
- * @return void
- */
- public function testCounterCacheWithSelfJoin() {
- $this->skipIf($this->db instanceof Sqlite, 'SQLite 2.x does not support ALTER TABLE ADD COLUMN');
- $this->loadFixtures('CategoryThread');
- $column = 'COLUMN ';
- if ($this->db instanceof Sqlserver) {
- $column = '';
- }
- $column .= $this->db->buildColumn(array('name' => 'child_count', 'type' => 'integer'));
- $this->db->query('ALTER TABLE ' . $this->db->fullTableName('category_threads') . ' ADD ' . $column);
- $this->db->flushMethodCache();
- $Category = new CategoryThread();
- $result = $Category->updateAll(array('CategoryThread.name' => "'updated'"), array('CategoryThread.parent_id' => 5));
- $this->assertFalse(empty($result));
- $Category = new CategoryThread();
- $Category->belongsTo['ParentCategory']['counterCache'] = 'child_count';
- $Category->updateCounterCache(array('parent_id' => 5));
- $result = Hash::extract($Category->find('all', array('conditions' => array('CategoryThread.id' => 5))), '{n}.CategoryThread.child_count');
- $expected = array(1);
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveWithCounterCacheScope method
- *
- * @return void
- */
- public function testSaveWithCounterCacheScope() {
- $this->loadFixtures('Syfile', 'Item', 'Image', 'ItemsPortfolio', 'Portfolio');
- $TestModel = new Syfile();
- $TestModel2 = new Item();
- $TestModel2->belongsTo['Syfile']['counterCache'] = true;
- $TestModel2->belongsTo['Syfile']['counterScope'] = array('published' => true);
- $result = $TestModel->findById(1);
- $this->assertSame($result['Syfile']['item_count'], null);
- $TestModel2->save(array(
- 'name' => 'Item 7',
- 'syfile_id' => 1,
- 'published' => true
- ));
- $result = $TestModel->findById(1);
- $this->assertEquals(1, $result['Syfile']['item_count']);
- $TestModel2->id = 1;
- $TestModel2->saveField('published', true);
- $result = $TestModel->findById(1);
- $this->assertEquals(2, $result['Syfile']['item_count']);
- $TestModel2->save(array(
- 'id' => 1,
- 'syfile_id' => 1,
- 'published' => false
- ));
- $result = $TestModel->findById(1);
- $this->assertEquals(1, $result['Syfile']['item_count']);
- }
- /**
- * Tests having multiple counter caches for an associated model
- *
- * @access public
- * @return void
- */
- public function testCounterCacheMultipleCaches() {
- $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
- $User = new CounterCacheUser();
- $Post = new CounterCachePost();
- $Post->unbindModel(array('belongsTo' => array('User')), false);
- $Post->bindModel(array(
- 'belongsTo' => array(
- 'User' => array(
- 'className' => 'CounterCacheUser',
- 'foreignKey' => 'user_id',
- 'counterCache' => array(
- true,
- 'posts_published' => array('Post.published' => true)
- )
- )
- )
- ), false);
- // Count Increase
- $data = array('Post' => array(
- 'id' => 22,
- 'title' => 'New Post',
- 'user_id' => 66,
- 'published' => true
- ));
- $Post->save($data);
- $result = $User->find('first', array(
- 'conditions' => array('id' => 66),
- 'recursive' => -1
- ));
- $this->assertEquals(3, $result[$User->alias]['post_count']);
- $this->assertEquals(2, $result[$User->alias]['posts_published']);
- // Count decrease
- $Post->delete(1);
- $result = $User->find('first', array(
- 'conditions' => array('id' => 66),
- 'recursive' => -1
- ));
- $this->assertEquals(2, $result[$User->alias]['post_count']);
- $this->assertEquals(2, $result[$User->alias]['posts_published']);
- // Count update
- $data = $Post->find('first', array(
- 'conditions' => array('id' => 1),
- 'recursive' => -1
- ));
- $data[$Post->alias]['user_id'] = 301;
- $Post->save($data);
- $result = $User->find('all',array('order' => 'User.id'));
- $this->assertEquals(2, $result[0]['User']['post_count']);
- $this->assertEquals(1, $result[1]['User']['posts_published']);
- }
- /**
- * test that beforeValidate returning false can abort saves.
- *
- * @return void
- */
- public function testBeforeValidateSaveAbortion() {
- $this->loadFixtures('Post');
- $Model = new CallbackPostTestModel();
- $Model->beforeValidateReturn = false;
- $data = array(
- 'title' => 'new article',
- 'body' => 'this is some text.'
- );
- $Model->create();
- $result = $Model->save($data);
- $this->assertFalse($result);
- }
- /**
- * test that beforeSave returning false can abort saves.
- *
- * @return void
- */
- public function testBeforeSaveSaveAbortion() {
- $this->loadFixtures('Post');
- $Model = new CallbackPostTestModel();
- $Model->beforeSaveReturn = false;
- $data = array(
- 'title' => 'new article',
- 'body' => 'this is some text.'
- );
- $Model->create();
- $result = $Model->save($data);
- $this->assertFalse($result);
- }
- /**
- * testSaveField method
- *
- * @return void
- */
- public function testSaveField() {
- $this->loadFixtures('Article');
- $TestModel = new Article();
- $TestModel->id = 1;
- $result = $TestModel->saveField('title', 'New First Article');
- $this->assertFalse(empty($result));
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
- $expected = array('Article' => array(
- 'id' => '1',
- 'user_id' => '1',
- 'title' => 'New First Article',
- 'body' => 'First Article Body'
- ));
- $this->assertEquals($expected, $result);
- $TestModel->id = 1;
- $result = $TestModel->saveField('title', '');
- $this->assertFalse(empty($result));
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
- $expected = array('Article' => array(
- 'id' => '1',
- 'user_id' => '1',
- 'title' => '',
- 'body' => 'First Article Body'
- ));
- $result['Article']['title'] = trim($result['Article']['title']);
- $this->assertEquals($expected, $result);
- $TestModel->id = 1;
- $TestModel->set('body', 'Messed up data');
- $result = $TestModel->saveField('title', 'First Article');
- $this->assertFalse(empty($result));
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
- $expected = array('Article' => array(
- 'id' => '1',
- 'user_id' => '1',
- 'title' => 'First Article',
- 'body' => 'First Article Body'
- ));
- $this->assertEquals($expected, $result);
- $TestModel->recursive = -1;
- $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
- $TestModel->id = 1;
- $result = $TestModel->saveField('title', '', true);
- $this->assertFalse($result);
- $TestModel->recursive = -1;
- $TestModel->id = 1;
- $result = $TestModel->saveField('user_id', 9999);
- $this->assertTrue((bool)$result);
- $result = $TestModel->read(array('id', 'user_id'), 1);
- $expected = array('Article' => array(
- 'id' => '1',
- 'user_id' => '9999',
- ));
- $this->assertEquals($expected, $result);
- $this->loadFixtures('Node', 'Dependency');
- $Node = new Node();
- $Node->set('id', 1);
- $result = $Node->read();
- $this->assertEquals(array('Second'), Hash::extract($result, 'ParentNode.{n}.name'));
- $Node->saveField('state', 10);
- $result = $Node->read();
- $this->assertEquals(array('Second'), Hash::extract($result, 'ParentNode.{n}.name'));
- }
- /**
- * testSaveWithCreate method
- *
- * @return void
- */
- public function testSaveWithCreate() {
- $this->loadFixtures(
- 'User',
- 'Article',
- 'User',
- 'Comment',
- 'Tag',
- 'ArticlesTag',
- 'Attachment'
- );
- $TestModel = new User();
- $data = array('User' => array(
- 'user' => 'user',
- 'password' => ''
- ));
- $result = $TestModel->save($data);
- $this->assertFalse($result);
- $this->assertTrue(!empty($TestModel->validationErrors));
- $TestModel = new Article();
- $data = array('Article' => array(
- 'user_id' => '',
- 'title' => '',
- 'body' => ''
- ));
- $result = $TestModel->create($data) && $TestModel->save();
- $this->assertFalse($result);
- $this->assertTrue(!empty($TestModel->validationErrors));
- $data = array('Article' => array(
- 'id' => 1,
- 'user_id' => '1',
- 'title' => 'New First Article',
- 'body' => ''
- ));
- $result = $TestModel->create($data) && $TestModel->save();
- $this->assertFalse($result);
- $data = array('Article' => array(
- 'id' => 1,
- 'title' => 'New First Article'
- ));
- $result = $TestModel->create() && $TestModel->save($data, false);
- $this->assertFalse(empty($result));
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
- $expected = array('Article' => array(
- 'id' => '1',
- 'user_id' => '1',
- 'title' => 'New First Article',
- 'body' => 'First Article Body',
- 'published' => 'N'
- ));
- $this->assertEquals($expected, $result);
- $data = array('Article' => array(
- 'id' => 1,
- 'user_id' => '2',
- 'title' => 'First Article',
- 'body' => 'New First Article Body',
- 'published' => 'Y'
- ));
- $result = $TestModel->create() && $TestModel->save($data, true, array('id', 'title', 'published'));
- $this->assertFalse(empty($result));
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
- $expected = array('Article' => array(
- 'id' => '1',
- 'user_id' => '1',
- 'title' => 'First Article',
- 'body' => 'First Article Body',
- 'published' => 'Y'
- ));
- $this->assertEquals($expected, $result);
- $data = array(
- 'Article' => array(
- 'user_id' => '2',
- 'title' => 'New Article',
- 'body' => 'New Article Body',
- 'created' => '2007-03-18 14:55:23',
- 'updated' => '2007-03-18 14:57:31'
- ),
- 'Tag' => array('Tag' => array(1, 3))
- );
- $TestModel->create();
- $result = $TestModel->create() && $TestModel->save($data);
- $this->assertFalse(empty($result));
- $TestModel->recursive = 2;
- $result = $TestModel->read(null, 4);
- $expected = array(
- 'Article' => array(
- 'id' => '4',
- 'user_id' => '2',
- 'title' => 'New Article',
- 'body' => 'New Article Body',
- 'published' => 'N',
- 'created' => '2007-03-18 14:55:23',
- 'updated' => '2007-03-18 14:57:31'
- ),
- 'User' => array(
- 'id' => '2',
- 'user' => 'nate',
- 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
- 'created' => '2007-03-17 01:18:23',
- 'updated' => '2007-03-17 01:20:31'
- ),
- 'Comment' => array(),
- 'Tag' => array(
- array(
- 'id' => '1',
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- array(
- 'id' => '3',
- 'tag' => 'tag3',
- 'created' => '2007-03-18 12:26:23',
- 'updated' => '2007-03-18 12:28:31'
- )));
- $this->assertEquals($expected, $result);
- $data = array('Comment' => array(
- 'article_id' => '4',
- 'user_id' => '1',
- 'comment' => 'Comment New Article',
- 'published' => 'Y',
- 'created' => '2007-03-18 14:57:23',
- 'updated' => '2007-03-18 14:59:31'
- ));
- $result = $TestModel->Comment->create() && $TestModel->Comment->save($data);
- $this->assertFalse(empty($result));
- $data = array('Attachment' => array(
- 'comment_id' => '7',
- 'attachment' => 'newattachment.zip',
- 'created' => '2007-03-18 15:02:23',
- 'updated' => '2007-03-18 15:04:31'
- ));
- $result = $TestModel->Comment->Attachment->save($data);
- $this->assertFalse(empty($result));
- $TestModel->recursive = 2;
- $result = $TestModel->read(null, 4);
- $expected = array(
- 'Article' => array(
- 'id' => '4',
- 'user_id' => '2',
- 'title' => 'New Article',
- 'body' => 'New Article Body',
- 'published' => 'N',
- 'created' => '2007-03-18 14:55:23',
- 'updated' => '2007-03-18 14:57:31'
- ),
- 'User' => array(
- 'id' => '2',
- 'user' => 'nate',
- 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
- 'created' => '2007-03-17 01:18:23',
- 'updated' => '2007-03-17 01:20:31'
- ),
- 'Comment' => array(
- array(
- 'id' => '7',
- 'article_id' => '4',
- 'user_id' => '1',
- 'comment' => 'Comment New Article',
- 'published' => 'Y',
- 'created' => '2007-03-18 14:57:23',
- 'updated' => '2007-03-18 14:59:31',
- 'Article' => array(
- 'id' => '4',
- 'user_id' => '2',
- 'title' => 'New Article',
- 'body' => 'New Article Body',
- 'published' => 'N',
- 'created' => '2007-03-18 14:55:23',
- 'updated' => '2007-03-18 14:57:31'
- ),
- 'User' => array(
- 'id' => '1',
- 'user' => 'mariano',
- 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
- 'created' => '2007-03-17 01:16:23',
- 'updated' => '2007-03-17 01:18:31'
- ),
- 'Attachment' => array(
- 'id' => '2',
- 'comment_id' => '7',
- 'attachment' => 'newattachment.zip',
- 'created' => '2007-03-18 15:02:23',
- 'updated' => '2007-03-18 15:04:31'
- ))),
- 'Tag' => array(
- array(
- 'id' => '1',
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- array(
- 'id' => '3',
- 'tag' => 'tag3',
- 'created' => '2007-03-18 12:26:23',
- 'updated' => '2007-03-18 12:28:31'
- )));
- $this->assertEquals($expected, $result);
- }
- /**
- * test that a null Id doesn't cause errors
- *
- * @return void
- */
- public function testSaveWithNullId() {
- $this->loadFixtures('User');
- $User = new User();
- $User->read(null, 1);
- $User->data['User']['id'] = null;
- $result = $User->save(array('password' => 'test'));
- $this->assertFalse(empty($result));
- $this->assertTrue($User->id > 0);
- $User->read(null, 2);
- $User->data['User']['id'] = null;
- $result = $User->save(array('password' => 'test'));
- $this->assertFalse(empty($result));
- $this->assertTrue($User->id > 0);
- $User->data['User'] = array('password' => 'something');
- $result = $User->save();
- $this->assertFalse(empty($result));
- $result = $User->read();
- $this->assertEquals('something', $User->data['User']['password']);
- }
- /**
- * testSaveWithSet method
- *
- * @return void
- */
- public function testSaveWithSet() {
- $this->loadFixtures('Article');
- $TestModel = new Article();
- // Create record we will be updating later
- $data = array('Article' => array(
- 'user_id' => '1',
- 'title' => 'Fourth Article',
- 'body' => 'Fourth Article Body',
- 'published' => 'Y'
- ));
- $result = $TestModel->create() && $TestModel->save($data);
- $this->assertFalse(empty($result));
- // Check record we created
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
- $expected = array('Article' => array(
- 'id' => '4',
- 'user_id' => '1',
- 'title' => 'Fourth Article',
- 'body' => 'Fourth Article Body',
- 'published' => 'Y'
- ));
- $this->assertEquals($expected, $result);
- // Create new record just to overlap Model->id on previously created record
- $data = array('Article' => array(
- 'user_id' => '4',
- 'title' => 'Fifth Article',
- 'body' => 'Fifth Article Body',
- 'published' => 'Y'
- ));
- $result = $TestModel->create() && $TestModel->save($data);
- $this->assertFalse(empty($result));
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
- $expected = array('Article' => array(
- 'id' => '5',
- 'user_id' => '4',
- 'title' => 'Fifth Article',
- 'body' => 'Fifth Article Body',
- 'published' => 'Y'
- ));
- $this->assertEquals($expected, $result);
- // Go back and edit the first article we created, starting by checking it's still there
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
- $expected = array('Article' => array(
- 'id' => '4',
- 'user_id' => '1',
- 'title' => 'Fourth Article',
- 'body' => 'Fourth Article Body',
- 'published' => 'Y'
- ));
- $this->assertEquals($expected, $result);
- // And now do the update with set()
- $data = array('Article' => array(
- 'id' => '4',
- 'title' => 'Fourth Article - New Title',
- 'published' => 'N'
- ));
- $result = $TestModel->set($data) && $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
- $expected = array('Article' => array(
- 'id' => '4',
- 'user_id' => '1',
- 'title' => 'Fourth Article - New Title',
- 'body' => 'Fourth Article Body',
- 'published' => 'N'
- ));
- $this->assertEquals($expected, $result);
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
- $expected = array('Article' => array(
- 'id' => '5',
- 'user_id' => '4',
- 'title' => 'Fifth Article',
- 'body' => 'Fifth Article Body',
- 'published' => 'Y'
- ));
- $this->assertEquals($expected, $result);
- $data = array('Article' => array('id' => '5', 'title' => 'Fifth Article - New Title 5'));
- $result = ($TestModel->set($data) && $TestModel->save());
- $this->assertFalse(empty($result));
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
- $expected = array('Article' => array(
- 'id' => '5',
- 'user_id' => '4',
- 'title' => 'Fifth Article - New Title 5',
- 'body' => 'Fifth Article Body',
- 'published' => 'Y'
- ));
- $this->assertEquals($expected, $result);
- $TestModel->recursive = -1;
- $result = $TestModel->find('all', array(
- 'fields' => array('id', 'title'),
- 'order' => array('Article.id' => 'ASC')
- ));
- $expected = array(
- array('Article' => array('id' => 1, 'title' => 'First Article')),
- array('Article' => array('id' => 2, 'title' => 'Second Article')),
- array('Article' => array('id' => 3, 'title' => 'Third Article')),
- array('Article' => array('id' => 4, 'title' => 'Fourth Article - New Title')),
- array('Article' => array('id' => 5, 'title' => 'Fifth Article - New Title 5'))
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveWithNonExistentFields method
- *
- * @return void
- */
- public function testSaveWithNonExistentFields() {
- $this->loadFixtures('Article');
- $TestModel = new Article();
- $TestModel->recursive = -1;
- $data = array(
- 'non_existent' => 'This field does not exist',
- 'user_id' => '1',
- 'title' => 'Fourth Article - New Title',
- 'body' => 'Fourth Article Body',
- 'published' => 'N'
- );
- $result = $TestModel->create() && $TestModel->save($data);
- $this->assertFalse(empty($result));
- $expected = array('Article' => array(
- 'id' => '4',
- 'user_id' => '1',
- 'title' => 'Fourth Article - New Title',
- 'body' => 'Fourth Article Body',
- 'published' => 'N'
- ));
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
- $this->assertEquals($expected, $result);
- $data = array(
- 'user_id' => '1',
- 'non_existent' => 'This field does not exist',
- 'title' => 'Fifth Article - New Title',
- 'body' => 'Fifth Article Body',
- 'published' => 'N'
- );
- $result = $TestModel->create() && $TestModel->save($data);
- $this->assertFalse(empty($result));
- $expected = array('Article' => array(
- 'id' => '5',
- 'user_id' => '1',
- 'title' => 'Fifth Article - New Title',
- 'body' => 'Fifth Article Body',
- 'published' => 'N'
- ));
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveFromXml method
- *
- * @return void
- */
- public function testSaveFromXml() {
- $this->markTestSkipped('This feature needs to be fixed or dropped');
- $this->loadFixtures('Article');
- App::uses('Xml', 'Utility');
- $Article = new Article();
- $result = $Article->save(Xml::build('<article title="test xml" user_id="5" />'));
- $this->assertFalse(empty($result));
- $results = $Article->find('first', array('conditions' => array('Article.title' => 'test xml')));
- $this->assertFalse(empty($results));
- $result = $Article->save(Xml::build('<article><title>testing</title><user_id>6</user_id></article>'));
- $this->assertFalse(empty($result));
- $results = $Article->find('first', array('conditions' => array('Article.title' => 'testing')));
- $this->assertFalse(empty($results));
- $result = $Article->save(Xml::build('<article><title>testing with DOMDocument</title><user_id>7</user_id></article>', array('return' => 'domdocument')));
- $this->assertFalse(empty($result));
- $results = $Article->find('first', array('conditions' => array('Article.title' => 'testing with DOMDocument')));
- $this->assertFalse(empty($results));
- }
- /**
- * testSaveHabtm method
- *
- * @return void
- */
- public function testSaveHabtm() {
- $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
- $TestModel = new Article();
- $result = $TestModel->findById(2);
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'Second Article',
- 'body' => 'Second Article Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:41:23',
- 'updated' => '2007-03-18 10:43:31'
- ),
- 'User' => array(
- 'id' => '3',
- 'user' => 'larry',
- 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
- 'created' => '2007-03-17 01:20:23',
- 'updated' => '2007-03-17 01:22:31'
- ),
- 'Comment' => array(
- array(
- 'id' => '5',
- 'article_id' => '2',
- 'user_id' => '1',
- 'comment' => 'First Comment for Second Article',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:53:23',
- 'updated' => '2007-03-18 10:55:31'
- ),
- array(
- 'id' => '6',
- 'article_id' => '2',
- 'user_id' => '2',
- 'comment' => 'Second Comment for Second Article',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:55:23',
- 'updated' => '2007-03-18 10:57:31'
- )),
- 'Tag' => array(
- array(
- 'id' => '1',
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- array(
- 'id' => '3',
- 'tag' => 'tag3',
- 'created' => '2007-03-18 12:26:23',
- 'updated' => '2007-03-18 12:28:31'
- )
- )
- );
- $this->assertEquals($expected, $result);
- $data = array(
- 'Article' => array(
- 'id' => '2',
- 'title' => 'New Second Article'
- ),
- 'Tag' => array('Tag' => array(1, 2))
- );
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $this->assertEquals($data['Tag'], $result['Tag']);
- $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'New Second Article',
- 'body' => 'Second Article Body'
- ),
- 'Tag' => array(
- array(
- 'id' => '1',
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- array(
- 'id' => '2',
- 'tag' => 'tag2',
- 'created' => '2007-03-18 12:24:23',
- 'updated' => '2007-03-18 12:26:31'
- )));
- $this->assertEquals($expected, $result);
- $data = array('Article' => array('id' => '2'), 'Tag' => array('Tag' => array(2, 3)));
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'New Second Article',
- 'body' => 'Second Article Body'
- ),
- 'Tag' => array(
- array(
- 'id' => '2',
- 'tag' => 'tag2',
- 'created' => '2007-03-18 12:24:23',
- 'updated' => '2007-03-18 12:26:31'
- ),
- array(
- 'id' => '3',
- 'tag' => 'tag3',
- 'created' => '2007-03-18 12:26:23',
- 'updated' => '2007-03-18 12:28:31'
- )));
- $this->assertEquals($expected, $result);
- $data = array('Tag' => array('Tag' => array(1, 2, 3)));
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'New Second Article',
- 'body' => 'Second Article Body'
- ),
- 'Tag' => array(
- array(
- 'id' => '1',
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- array(
- 'id' => '2',
- 'tag' => 'tag2',
- 'created' => '2007-03-18 12:24:23',
- 'updated' => '2007-03-18 12:26:31'
- ),
- array(
- 'id' => '3',
- 'tag' => 'tag3',
- 'created' => '2007-03-18 12:26:23',
- 'updated' => '2007-03-18 12:28:31'
- )));
- $this->assertEquals($expected, $result);
- $data = array('Tag' => array('Tag' => array()));
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $data = array('Tag' => array('Tag' => ''));
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'New Second Article',
- 'body' => 'Second Article Body'
- ),
- 'Tag' => array()
- );
- $this->assertEquals($expected, $result);
- $data = array('Tag' => array('Tag' => array(2, 3)));
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'New Second Article',
- 'body' => 'Second Article Body'
- ),
- 'Tag' => array(
- array(
- 'id' => '2',
- 'tag' => 'tag2',
- 'created' => '2007-03-18 12:24:23',
- 'updated' => '2007-03-18 12:26:31'
- ),
- array(
- 'id' => '3',
- 'tag' => 'tag3',
- 'created' => '2007-03-18 12:26:23',
- 'updated' => '2007-03-18 12:28:31'
- )));
- $this->assertEquals($expected, $result);
- $data = array(
- 'Tag' => array(
- 'Tag' => array(1, 2)
- ),
- 'Article' => array(
- 'id' => '2',
- 'title' => 'New Second Article'
- ));
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'New Second Article',
- 'body' => 'Second Article Body'
- ),
- 'Tag' => array(
- array(
- 'id' => '1',
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- array(
- 'id' => '2',
- 'tag' => 'tag2',
- 'created' => '2007-03-18 12:24:23',
- 'updated' => '2007-03-18 12:26:31'
- )));
- $this->assertEquals($expected, $result);
- $data = array(
- 'Tag' => array(
- 'Tag' => array(1, 2)
- ),
- 'Article' => array(
- 'id' => '2',
- 'title' => 'New Second Article Title'
- ));
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'New Second Article Title',
- 'body' => 'Second Article Body'
- ),
- 'Tag' => array(
- array(
- 'id' => '1',
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- array(
- 'id' => '2',
- 'tag' => 'tag2',
- 'created' => '2007-03-18 12:24:23',
- 'updated' => '2007-03-18 12:26:31'
- )
- )
- );
- $this->assertEquals($expected, $result);
- $data = array(
- 'Tag' => array(
- 'Tag' => array(2, 3)
- ),
- 'Article' => array(
- 'id' => '2',
- 'title' => 'Changed Second Article'
- ));
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'Changed Second Article',
- 'body' => 'Second Article Body'
- ),
- 'Tag' => array(
- array(
- 'id' => '2',
- 'tag' => 'tag2',
- 'created' => '2007-03-18 12:24:23',
- 'updated' => '2007-03-18 12:26:31'
- ),
- array(
- 'id' => '3',
- 'tag' => 'tag3',
- 'created' => '2007-03-18 12:26:23',
- 'updated' => '2007-03-18 12:28:31'
- )
- )
- );
- $this->assertEquals($expected, $result);
- $data = array(
- 'Tag' => array(
- 'Tag' => array(1, 3)
- ),
- 'Article' => array('id' => '2'),
- );
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'Changed Second Article',
- 'body' => 'Second Article Body'
- ),
- 'Tag' => array(
- array(
- 'id' => '1',
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- array(
- 'id' => '3',
- 'tag' => 'tag3',
- 'created' => '2007-03-18 12:26:23',
- 'updated' => '2007-03-18 12:28:31'
- )));
- $this->assertEquals($expected, $result);
- $data = array(
- 'Article' => array(
- 'id' => 10,
- 'user_id' => '2',
- 'title' => 'New Article With Tags and fieldList',
- 'body' => 'New Article Body with Tags and fieldList',
- 'created' => '2007-03-18 14:55:23',
- 'updated' => '2007-03-18 14:57:31'
- ),
- 'Tag' => array(
- 'Tag' => array(1, 2, 3)
- )
- );
- $result = $TestModel->create()
- && $TestModel->save($data, true, array('user_id', 'title', 'published'));
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->read();
- $expected = array(
- 'Article' => array(
- 'id' => 4,
- 'user_id' => 2,
- 'title' => 'New Article With Tags and fieldList',
- 'body' => '',
- 'published' => 'N',
- 'created' => '',
- 'updated' => ''
- ),
- 'Tag' => array(
- 0 => array(
- 'id' => 1,
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- 1 => array(
- 'id' => 2,
- 'tag' => 'tag2',
- 'created' => '2007-03-18 12:24:23',
- 'updated' => '2007-03-18 12:26:31'
- ),
- 2 => array(
- 'id' => 3,
- 'tag' => 'tag3',
- 'created' => '2007-03-18 12:26:23',
- 'updated' => '2007-03-18 12:28:31'
- )));
- $this->assertEquals($expected, $result);
- $this->loadFixtures('JoinA', 'JoinC', 'JoinAC', 'JoinB', 'JoinAB');
- $TestModel = new JoinA();
- $TestModel->hasBelongsToMany = array('JoinC' => array('unique' => true));
- $data = array(
- 'JoinA' => array(
- 'id' => 1,
- 'name' => 'Join A 1',
- 'body' => 'Join A 1 Body',
- ),
- 'JoinC' => array(
- 'JoinC' => array(
- array('join_c_id' => 2, 'other' => 'new record'),
- array('join_c_id' => 3, 'other' => 'new record')
- )
- )
- );
- $TestModel->save($data);
- $result = $TestModel->read(null, 1);
- $expected = array(4, 5);
- $this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.id'));
- $expected = array('new record', 'new record');
- $this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.other'));
- }
- /**
- * testSaveHabtmNoPrimaryData method
- *
- * @return void
- */
- public function testSaveHabtmNoPrimaryData() {
- $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
- $TestModel = new Article();
- $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')), false);
- $result = $TestModel->findById(2);
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'Second Article',
- 'body' => 'Second Article Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:41:23',
- 'updated' => '2007-03-18 10:43:31'
- ),
- 'Tag' => array(
- array(
- 'id' => '1',
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- array(
- 'id' => '3',
- 'tag' => 'tag3',
- 'created' => '2007-03-18 12:26:23',
- 'updated' => '2007-03-18 12:28:31'
- )
- )
- );
- $this->assertEquals($expected, $result);
- $TestModel->id = 2;
- $data = array('Tag' => array('Tag' => array(2)));
- $TestModel->save($data);
- $result = $TestModel->findById(2);
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'Second Article',
- 'body' => 'Second Article Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:41:23',
- 'updated' => self::date()
- ),
- 'Tag' => array(
- array(
- 'id' => '2',
- 'tag' => 'tag2',
- 'created' => '2007-03-18 12:24:23',
- 'updated' => '2007-03-18 12:26:31'
- )
- )
- );
- $this->assertEquals($expected, $result);
- $this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio');
- $TestModel = new Portfolio();
- $result = $TestModel->findById(2);
- $expected = array(
- 'Portfolio' => array(
- 'id' => 2,
- 'seller_id' => 1,
- 'name' => 'Portfolio 2'
- ),
- 'Item' => array(
- array(
- 'id' => 2,
- 'syfile_id' => 2,
- 'published' => '',
- 'name' => 'Item 2',
- 'ItemsPortfolio' => array(
- 'id' => 2,
- 'item_id' => 2,
- 'portfolio_id' => 2
- )
- ),
- array(
- 'id' => 6,
- 'syfile_id' => 6,
- 'published' => '',
- 'name' => 'Item 6',
- 'ItemsPortfolio' => array(
- 'id' => 6,
- 'item_id' => 6,
- 'portfolio_id' => 2
- )
- )
- )
- );
- $this->assertEquals($expected, $result);
- $data = array('Item' => array('Item' => array(1, 2)));
- $TestModel->id = 2;
- $TestModel->save($data);
- $result = $TestModel->findById(2);
- $result['Item'] = Hash::sort($result['Item'], '{n}.id', 'asc');
- $expected = array(
- 'Portfolio' => array(
- 'id' => 2,
- 'seller_id' => 1,
- 'name' => 'Portfolio 2'
- ),
- 'Item' => array(
- array(
- 'id' => 1,
- 'syfile_id' => 1,
- 'published' => '',
- 'name' => 'Item 1',
- 'ItemsPortfolio' => array(
- 'id' => 7,
- 'item_id' => 1,
- 'portfolio_id' => 2
- )
- ),
- array(
- 'id' => 2,
- 'syfile_id' => 2,
- 'published' => '',
- 'name' => 'Item 2',
- 'ItemsPortfolio' => array(
- 'id' => 8,
- 'item_id' => 2,
- 'portfolio_id' => 2
- )
- )
- )
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveHabtmCustomKeys method
- *
- * @return void
- */
- public function testSaveHabtmCustomKeys() {
- $this->loadFixtures('Story', 'StoriesTag', 'Tag');
- $Story = new Story();
- $data = array(
- 'Story' => array('story' => '1'),
- 'Tag' => array(
- 'Tag' => array(2, 3)
- ));
- $result = $Story->set($data);
- $this->assertFalse(empty($result));
- $result = $Story->save();
- $this->assertFalse(empty($result));
- $result = $Story->find('all', array('order' => array('Story.story')));
- $expected = array(
- array(
- 'Story' => array(
- 'story' => 1,
- 'title' => 'First Story'
- ),
- 'Tag' => array(
- array(
- 'id' => 2,
- 'tag' => 'tag2',
- 'created' => '2007-03-18 12:24:23',
- 'updated' => '2007-03-18 12:26:31'
- ),
- array(
- 'id' => 3,
- 'tag' => 'tag3',
- 'created' => '2007-03-18 12:26:23',
- 'updated' => '2007-03-18 12:28:31'
- ))),
- array(
- 'Story' => array(
- 'story' => 2,
- 'title' => 'Second Story'
- ),
- 'Tag' => array()
- ));
- $this->assertEquals($expected, $result);
- }
- /**
- * test that saving habtm records respects conditions set in the 'conditions' key
- * for the association.
- *
- * @return void
- */
- public function testHabtmSaveWithConditionsInAssociation() {
- $this->loadFixtures('JoinThing', 'Something', 'SomethingElse');
- $Something = new Something();
- $Something->unbindModel(array('hasAndBelongsToMany' => array('SomethingElse')), false);
- $Something->bindModel(array(
- 'hasAndBelongsToMany' => array(
- 'DoomedSomethingElse' => array(
- 'className' => 'SomethingElse',
- 'joinTable' => 'join_things',
- 'conditions' => array('JoinThing.doomed' => true),
- 'unique' => true
- ),
- 'NotDoomedSomethingElse' => array(
- 'className' => 'SomethingElse',
- 'joinTable' => 'join_things',
- 'conditions' => array('JoinThing.doomed' => 0),
- 'unique' => true
- )
- )
- ), false);
- $result = $Something->read(null, 1);
- $this->assertTrue(empty($result['NotDoomedSomethingElse']));
- $this->assertEquals(1, count($result['DoomedSomethingElse']));
- $data = array(
- 'Something' => array('id' => 1),
- 'NotDoomedSomethingElse' => array(
- 'NotDoomedSomethingElse' => array(
- array('something_else_id' => 2, 'doomed' => 0),
- array('something_else_id' => 3, 'doomed' => 0)
- )
- )
- );
- $Something->create($data);
- $result = $Something->save();
- $this->assertFalse(empty($result));
- $result = $Something->read(null, 1);
- $this->assertEquals(2, count($result['NotDoomedSomethingElse']));
- $this->assertEquals(1, count($result['DoomedSomethingElse']));
- }
- /**
- * testHabtmSaveKeyResolution method
- *
- * @return void
- */
- public function testHabtmSaveKeyResolution() {
- $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies');
- $ThePaper = new ThePaper();
- $ThePaper->id = 1;
- $ThePaper->save(array('Monkey' => array(2, 3)));
- $result = $ThePaper->findById(1);
- $expected = array(
- array(
- 'id' => '2',
- 'device_type_id' => '1',
- 'name' => 'Device 2',
- 'typ' => '1'
- ),
- array(
- 'id' => '3',
- 'device_type_id' => '1',
- 'name' => 'Device 3',
- 'typ' => '2'
- ));
- $this->assertEquals($expected, $result['Monkey']);
- $ThePaper->id = 2;
- $ThePaper->save(array('Monkey' => array(1, 2, 3)));
- $result = $ThePaper->findById(2);
- $expected = array(
- array(
- 'id' => '1',
- 'device_type_id' => '1',
- 'name' => 'Device 1',
- 'typ' => '1'
- ),
- array(
- 'id' => '2',
- 'device_type_id' => '1',
- 'name' => 'Device 2',
- 'typ' => '1'
- ),
- array(
- 'id' => '3',
- 'device_type_id' => '1',
- 'name' => 'Device 3',
- 'typ' => '2'
- ));
- $this->assertEquals($expected, $result['Monkey']);
- $ThePaper->id = 2;
- $ThePaper->save(array('Monkey' => array(1, 3)));
- $result = $ThePaper->findById(2);
- $expected = array(
- array(
- 'id' => '1',
- 'device_type_id' => '1',
- 'name' => 'Device 1',
- 'typ' => '1'
- ),
- array(
- 'id' => '3',
- 'device_type_id' => '1',
- 'name' => 'Device 3',
- 'typ' => '2'
- ));
- $this->assertEquals($expected, $result['Monkey']);
- $result = $ThePaper->findById(1);
- $expected = array(
- array(
- 'id' => '2',
- 'device_type_id' => '1',
- 'name' => 'Device 2',
- 'typ' => '1'
- ),
- array(
- 'id' => '3',
- 'device_type_id' => '1',
- 'name' => 'Device 3',
- 'typ' => '2'
- ));
- $this->assertEquals($expected, $result['Monkey']);
- }
- /**
- * testCreationOfEmptyRecord method
- *
- * @return void
- */
- public function testCreationOfEmptyRecord() {
- $this->loadFixtures('Author');
- $TestModel = new Author();
- $this->assertEquals(4, $TestModel->find('count'));
- $TestModel->deleteAll(true, false, false);
- $this->assertEquals(0, $TestModel->find('count'));
- $result = $TestModel->save();
- $this->assertTrue(isset($result['Author']['created']));
- $this->assertTrue(isset($result['Author']['updated']));
- $this->assertEquals(1, $TestModel->find('count'));
- }
- /**
- * testCreateWithPKFiltering method
- *
- * @return void
- */
- public function testCreateWithPKFiltering() {
- $TestModel = new Article();
- $data = array(
- 'id' => 5,
- 'user_id' => 2,
- 'title' => 'My article',
- 'body' => 'Some text'
- );
- $result = $TestModel->create($data);
- $expected = array(
- 'Article' => array(
- 'published' => 'N',
- 'id' => 5,
- 'user_id' => 2,
- 'title' => 'My article',
- 'body' => 'Some text'
- ));
- $this->assertEquals($expected, $result);
- $this->assertEquals(5, $TestModel->id);
- $result = $TestModel->create($data, true);
- $expected = array(
- 'Article' => array(
- 'published' => 'N',
- 'id' => false,
- 'user_id' => 2,
- 'title' => 'My article',
- 'body' => 'Some text'
- ));
- $this->assertEquals($expected, $result);
- $this->assertFalse($TestModel->id);
- $result = $TestModel->create(array('Article' => $data), true);
- $expected = array(
- 'Article' => array(
- 'published' => 'N',
- 'id' => false,
- 'user_id' => 2,
- 'title' => 'My article',
- 'body' => 'Some text'
- ));
- $this->assertEquals($expected, $result);
- $this->assertFalse($TestModel->id);
- $data = array(
- 'id' => 6,
- 'user_id' => 2,
- 'title' => 'My article',
- 'body' => 'Some text',
- 'created' => '1970-01-01 00:00:00',
- 'updated' => '1970-01-01 12:00:00',
- 'modified' => '1970-01-01 12:00:00'
- );
- $result = $TestModel->create($data);
- $expected = array(
- 'Article' => array(
- 'published' => 'N',
- 'id' => 6,
- 'user_id' => 2,
- 'title' => 'My article',
- 'body' => 'Some text',
- 'created' => '1970-01-01 00:00:00',
- 'updated' => '1970-01-01 12:00:00',
- 'modified' => '1970-01-01 12:00:00'
- ));
- $this->assertEquals($expected, $result);
- $this->assertEquals(6, $TestModel->id);
- $result = $TestModel->create(array(
- 'Article' => array_diff_key($data, array(
- 'created' => true,
- 'updated' => true,
- 'modified' => true
- ))), true);
- $expected = array(
- 'Article' => array(
- 'published' => 'N',
- 'id' => false,
- 'user_id' => 2,
- 'title' => 'My article',
- 'body' => 'Some text'
- ));
- $this->assertEquals($expected, $result);
- $this->assertFalse($TestModel->id);
- }
- /**
- * testCreationWithMultipleData method
- *
- * @return void
- */
- public function testCreationWithMultipleData() {
- $this->loadFixtures('Article', 'Comment');
- $Article = new Article();
- $Comment = new Comment();
- $articles = $Article->find('all', array(
- 'fields' => array('id','title'),
- 'recursive' => -1,
- 'order' => array('Article.id' => 'ASC')
- ));
- $expected = array(
- array('Article' => array(
- 'id' => 1,
- 'title' => 'First Article'
- )),
- array('Article' => array(
- 'id' => 2,
- 'title' => 'Second Article'
- )),
- array('Article' => array(
- 'id' => 3,
- 'title' => 'Third Article'
- )));
- $this->assertEquals($expected, $articles);
- $comments = $Comment->find('all', array(
- 'fields' => array('id','article_id','user_id','comment','published'),
- 'recursive' => -1,
- 'order' => array('Comment.id' => 'ASC')
- ));
- $expected = array(
- array('Comment' => array(
- 'id' => 1,
- 'article_id' => 1,
- 'user_id' => 2,
- 'comment' => 'First Comment for First Article',
- 'published' => 'Y'
- )),
- array('Comment' => array(
- 'id' => 2,
- 'article_id' => 1,
- 'user_id' => 4,
- 'comment' => 'Second Comment for First Article',
- 'published' => 'Y'
- )),
- array('Comment' => array(
- 'id' => 3,
- 'article_id' => 1,
- 'user_id' => 1,
- 'comment' => 'Third Comment for First Article',
- 'published' => 'Y'
- )),
- array('Comment' => array(
- 'id' => 4,
- 'article_id' => 1,
- 'user_id' => 1,
- 'comment' => 'Fourth Comment for First Article',
- 'published' => 'N'
- )),
- array('Comment' => array(
- 'id' => 5,
- 'article_id' => 2,
- 'user_id' => 1,
- 'comment' => 'First Comment for Second Article',
- 'published' => 'Y'
- )),
- array('Comment' => array(
- 'id' => 6,
- 'article_id' => 2,
- 'user_id' => 2,
- 'comment' => 'Second Comment for Second Article',
- 'published' => 'Y'
- )));
- $this->assertEquals($expected, $comments);
- $data = array(
- 'Comment' => array(
- 'article_id' => 2,
- 'user_id' => 4,
- 'comment' => 'Brand New Comment',
- 'published' => 'N'
- ),
- 'Article' => array(
- 'id' => 2,
- 'title' => 'Second Article Modified'
- ));
- $result = $Comment->create($data);
- $this->assertFalse(empty($result));
- $result = $Comment->save();
- $this->assertFalse(empty($result));
- $articles = $Article->find('all', array(
- 'fields' => array('id','title'),
- 'recursive' => -1,
- 'order' => array('Article.id' => 'ASC')
- ));
- $expected = array(
- array('Article' => array(
- 'id' => 1,
- 'title' => 'First Article'
- )),
- array('Article' => array(
- 'id' => 2,
- 'title' => 'Second Article'
- )),
- array('Article' => array(
- 'id' => 3,
- 'title' => 'Third Article'
- )));
- $this->assertEquals($expected, $articles);
- $comments = $Comment->find('all', array(
- 'fields' => array('id','article_id','user_id','comment','published'),
- 'recursive' => -1,
- 'order' => array('Comment.id' => 'ASC')
- ));
- $expected = array(
- array('Comment' => array(
- 'id' => 1,
- 'article_id' => 1,
- 'user_id' => 2,
- 'comment' => 'First Comment for First Article',
- 'published' => 'Y'
- )),
- array('Comment' => array(
- 'id' => 2,
- 'article_id' => 1,
- 'user_id' => 4,
- 'comment' => 'Second Comment for First Article',
- 'published' => 'Y'
- )),
- array('Comment' => array(
- 'id' => 3,
- 'article_id' => 1,
- 'user_id' => 1,
- 'comment' => 'Third Comment for First Article',
- 'published' => 'Y'
- )),
- array('Comment' => array(
- 'id' => 4,
- 'article_id' => 1,
- 'user_id' => 1,
- 'comment' => 'Fourth Comment for First Article',
- 'published' => 'N'
- )),
- array('Comment' => array(
- 'id' => 5,
- 'article_id' => 2,
- 'user_id' => 1,
- 'comment' => 'First Comment for Second Article',
- 'published' => 'Y'
- )),
- array('Comment' => array(
- 'id' => 6,
- 'article_id' => 2,
- 'user_id' => 2, 'comment' =>
- 'Second Comment for Second Article',
- 'published' => 'Y'
- )),
- array('Comment' => array(
- 'id' => 7,
- 'article_id' => 2,
- 'user_id' => 4,
- 'comment' => 'Brand New Comment',
- 'published' => 'N'
- )));
- $this->assertEquals($expected, $comments);
- }
- /**
- * testCreationWithMultipleDataSameModel method
- *
- * @return void
- */
- public function testCreationWithMultipleDataSameModel() {
- $this->loadFixtures('Article');
- $Article = new Article();
- $result = $Article->field('title', array('id' => 1));
- $this->assertEquals('First Article', $result);
- $data = array(
- 'Article' => array(
- 'user_id' => 2,
- 'title' => 'Brand New Article',
- 'body' => 'Brand New Article Body',
- 'published' => 'Y'
- ),
- 'SecondaryArticle' => array(
- 'id' => 1
- ));
- $Article->create();
- $result = $Article->save($data);
- $this->assertFalse(empty($result));
- $result = $Article->getInsertID();
- $this->assertTrue(!empty($result));
- $result = $Article->field('title', array('id' => 1));
- $this->assertEquals('First Article', $result);
- $articles = $Article->find('all', array(
- 'fields' => array('id','title'),
- 'recursive' => -1,
- 'order' => array('Article.id' => 'ASC')
- ));
- $expected = array(
- array('Article' => array(
- 'id' => 1,
- 'title' => 'First Article'
- )),
- array('Article' => array(
- 'id' => 2,
- 'title' => 'Second Article'
- )),
- array('Article' => array(
- 'id' => 3,
- 'title' => 'Third Article'
- )),
- array('Article' => array(
- 'id' => 4,
- 'title' => 'Brand New Article'
- )));
- $this->assertEquals($expected, $articles);
- }
- /**
- * testCreationWithMultipleDataSameModelManualInstances method
- *
- * @return void
- */
- public function testCreationWithMultipleDataSameModelManualInstances() {
- $this->loadFixtures('PrimaryModel');
- $Primary = new PrimaryModel();
- $result = $Primary->field('primary_name', array('id' => 1));
- $this->assertEquals('Primary Name Existing', $result);
- $data = array(
- 'PrimaryModel' => array(
- 'primary_name' => 'Primary Name New'
- ),
- 'SecondaryModel' => array(
- 'id' => array(1)
- ));
- $Primary->create();
- $result = $Primary->save($data);
- $this->assertFalse(empty($result));
- $result = $Primary->field('primary_name', array('id' => 1));
- $this->assertEquals('Primary Name Existing', $result);
- $result = $Primary->getInsertID();
- $this->assertTrue(!empty($result));
- $result = $Primary->field('primary_name', array('id' => $result));
- $this->assertEquals('Primary Name New', $result);
- $result = $Primary->find('count');
- $this->assertEquals(2, $result);
- }
- /**
- * testRecordExists method
- *
- * @return void
- */
- public function testRecordExists() {
- $this->loadFixtures('User');
- $TestModel = new User();
- $this->assertFalse($TestModel->exists());
- $TestModel->read(null, 1);
- $this->assertTrue($TestModel->exists());
- $TestModel->create();
- $this->assertFalse($TestModel->exists());
- $TestModel->id = 4;
- $this->assertTrue($TestModel->exists());
- $TestModel = new TheVoid();
- $this->assertFalse($TestModel->exists());
- }
- /**
- * testRecordExistsMissingTable method
- *
- * @expectedException PDOException
- * @return void
- */
- public function testRecordExistsMissingTable() {
- $TestModel = new TheVoid();
- $TestModel->id = 5;
- $TestModel->exists();
- }
- /**
- * testUpdateExisting method
- *
- * @return void
- */
- public function testUpdateExisting() {
- $this->loadFixtures('User', 'Article', 'Comment');
- $TestModel = new User();
- $TestModel->create();
- $TestModel->save(array(
- 'User' => array(
- 'user' => 'some user',
- 'password' => 'some password'
- )));
- $this->assertTrue(is_int($TestModel->id) || (intval($TestModel->id) === 5));
- $id = $TestModel->id;
- $TestModel->save(array(
- 'User' => array(
- 'user' => 'updated user'
- )));
- $this->assertEquals($id, $TestModel->id);
- $result = $TestModel->findById($id);
- $this->assertEquals('updated user', $result['User']['user']);
- $this->assertEquals('some password', $result['User']['password']);
- $Article = new Article();
- $Comment = new Comment();
- $data = array(
- 'Comment' => array(
- 'id' => 1,
- 'comment' => 'First Comment for First Article'
- ),
- 'Article' => array(
- 'id' => 2,
- 'title' => 'Second Article'
- ));
- $result = $Article->save($data);
- $this->assertFalse(empty($result));
- $result = $Comment->save($data);
- $this->assertFalse(empty($result));
- }
- /**
- * test updating records and saving blank values.
- *
- * @return void
- */
- public function testUpdateSavingBlankValues() {
- $this->loadFixtures('Article');
- $Article = new Article();
- $Article->validate = array();
- $Article->create();
- $result = $Article->save(array(
- 'id' => 1,
- 'title' => '',
- 'body' => ''
- ));
- $this->assertTrue((bool)$result);
- $result = $Article->find('first', array('conditions' => array('Article.id' => 1)));
- $this->assertEquals('', $result['Article']['title'], 'Title is not blank');
- $this->assertEquals('', $result['Article']['body'], 'Body is not blank');
- }
- /**
- * testUpdateMultiple method
- *
- * @return void
- */
- public function testUpdateMultiple() {
- $this->loadFixtures('Comment', 'Article', 'User', 'CategoryThread');
- $TestModel = new Comment();
- $result = Hash::extract($TestModel->find('all'), '{n}.Comment.user_id');
- $expected = array('2', '4', '1', '1', '1', '2');
- $this->assertEquals($expected, $result);
- $TestModel->updateAll(array('Comment.user_id' => 5), array('Comment.user_id' => 2));
- $result = Hash::combine($TestModel->find('all'), '{n}.Comment.id', '{n}.Comment.user_id');
- $expected = array(1 => 5, 2 => 4, 3 => 1, 4 => 1, 5 => 1, 6 => 5);
- $this->assertEquals($expected, $result);
- $result = $TestModel->updateAll(
- array('Comment.comment' => "'Updated today'"),
- array('Comment.user_id' => 5)
- );
- $this->assertFalse(empty($result));
- $result = Hash::extract(
- $TestModel->find('all', array(
- 'conditions' => array(
- 'Comment.user_id' => 5
- ))),
- '{n}.Comment.comment'
- );
- $expected = array_fill(0, 2, 'Updated today');
- $this->assertEquals($expected, $result);
- }
- /**
- * testHabtmUuidWithUuidId method
- *
- * @return void
- */
- public function testHabtmUuidWithUuidId() {
- $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolio', 'UuiditemsUuidportfolioNumericid');
- $TestModel = new Uuidportfolio();
- $data = array('Uuidportfolio' => array('name' => 'Portfolio 3'));
- $data['Uuiditem']['Uuiditem'] = array('483798c8-c7cc-430e-8cf9-4fcc40cf8569');
- $TestModel->create($data);
- $TestModel->save();
- $id = $TestModel->id;
- $result = $TestModel->read(null, $id);
- $this->assertEquals(1, count($result['Uuiditem']));
- $this->assertEquals(36, strlen($result['Uuiditem'][0]['UuiditemsUuidportfolio']['id']));
- }
- /**
- * test HABTM saving when join table has no primary key and only 2 columns.
- *
- * @return void
- */
- public function testHabtmSavingWithNoPrimaryKeyUuidJoinTable() {
- $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
- $Fruit = new Fruit();
- $data = array(
- 'Fruit' => array(
- 'color' => 'Red',
- 'shape' => 'Heart-shaped',
- 'taste' => 'sweet',
- 'name' => 'Strawberry',
- ),
- 'UuidTag' => array(
- 'UuidTag' => array(
- '481fc6d0-b920-43e0-e50f-6d1740cf8569'
- )
- )
- );
- $result = $Fruit->save($data);
- $this->assertFalse(empty($result));
- }
- /**
- * test HABTM saving when join table has no primary key and only 2 columns, no with model is used.
- *
- * @return void
- */
- public function testHabtmSavingWithNoPrimaryKeyUuidJoinTableNoWith() {
- $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
- $Fruit = new FruitNoWith();
- $data = array(
- 'Fruit' => array(
- 'color' => 'Red',
- 'shape' => 'Heart-shaped',
- 'taste' => 'sweet',
- 'name' => 'Strawberry',
- ),
- 'UuidTag' => array(
- 'UuidTag' => array(
- '481fc6d0-b920-43e0-e50f-6d1740cf8569'
- )
- )
- );
- $result = $Fruit->save($data);
- $this->assertFalse(empty($result));
- }
- /**
- * testHabtmUuidWithNumericId method
- *
- * @return void
- */
- public function testHabtmUuidWithNumericId() {
- $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolioNumericid');
- $TestModel = new Uuiditem();
- $data = array('Uuiditem' => array('name' => 'Item 7', 'published' => 0));
- $data['Uuidportfolio']['Uuidportfolio'] = array('480af662-eb8c-47d3-886b-230540cf8569');
- $TestModel->create($data);
- $TestModel->save();
- $id = $TestModel->id;
- $result = $TestModel->read(null, $id);
- $this->assertEquals(1, count($result['Uuidportfolio']));
- }
- /**
- * testSaveMultipleHabtm method
- *
- * @return void
- */
- public function testSaveMultipleHabtm() {
- $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC');
- $TestModel = new JoinA();
- $result = $TestModel->findById(1);
- $expected = array(
- 'JoinA' => array(
- 'id' => 1,
- 'name' => 'Join A 1',
- 'body' => 'Join A 1 Body',
- 'created' => '2008-01-03 10:54:23',
- 'updated' => '2008-01-03 10:54:23'
- ),
- 'JoinB' => array(
- 0 => array(
- 'id' => 2,
- 'name' => 'Join B 2',
- 'created' => '2008-01-03 10:55:02',
- 'updated' => '2008-01-03 10:55:02',
- 'JoinAsJoinB' => array(
- 'id' => 1,
- 'join_a_id' => 1,
- 'join_b_id' => 2,
- 'other' => 'Data for Join A 1 Join B 2',
- 'created' => '2008-01-03 10:56:33',
- 'updated' => '2008-01-03 10:56:33'
- ))),
- 'JoinC' => array(
- 0 => array(
- 'id' => 2,
- 'name' => 'Join C 2',
- 'created' => '2008-01-03 10:56:12',
- 'updated' => '2008-01-03 10:56:12',
- 'JoinAsJoinC' => array(
- 'id' => 1,
- 'join_a_id' => 1,
- 'join_c_id' => 2,
- 'other' => 'Data for Join A 1 Join C 2',
- 'created' => '2008-01-03 10:57:22',
- 'updated' => '2008-01-03 10:57:22'
- ))));
- $this->assertEquals($expected, $result);
- $TestModel->id = 1;
- $data = array(
- 'JoinA' => array(
- 'id' => '1',
- 'name' => 'New name for Join A 1',
- 'updated' => self::date()
- ),
- 'JoinB' => array(
- array(
- 'id' => 1,
- 'join_b_id' => 2,
- 'other' => 'New data for Join A 1 Join B 2',
- 'created' => self::date(),
- 'updated' => self::date()
- )),
- 'JoinC' => array(
- array(
- 'id' => 1,
- 'join_c_id' => 2,
- 'other' => 'New data for Join A 1 Join C 2',
- 'created' => self::date(),
- 'updated' => self::date()
- )));
- $TestModel->set($data);
- $TestModel->save();
- $result = $TestModel->findById(1);
- $expected = array(
- 'JoinA' => array(
- 'id' => 1,
- 'name' => 'New name for Join A 1',
- 'body' => 'Join A 1 Body',
- 'created' => '2008-01-03 10:54:23',
- 'updated' => self::date()
- ),
- 'JoinB' => array(
- 0 => array(
- 'id' => 2,
- 'name' => 'Join B 2',
- 'created' => '2008-01-03 10:55:02',
- 'updated' => '2008-01-03 10:55:02',
- 'JoinAsJoinB' => array(
- 'id' => 1,
- 'join_a_id' => 1,
- 'join_b_id' => 2,
- 'other' => 'New data for Join A 1 Join B 2',
- 'created' => self::date(),
- 'updated' => self::date()
- ))),
- 'JoinC' => array(
- 0 => array(
- 'id' => 2,
- 'name' => 'Join C 2',
- 'created' => '2008-01-03 10:56:12',
- 'updated' => '2008-01-03 10:56:12',
- 'JoinAsJoinC' => array(
- 'id' => 1,
- 'join_a_id' => 1,
- 'join_c_id' => 2,
- 'other' => 'New data for Join A 1 Join C 2',
- 'created' => self::date(),
- 'updated' => self::date()
- ))));
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAll method
- *
- * @return void
- */
- public function testSaveAll() {
- $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
- $TestModel = new Post();
- $result = $TestModel->find('all');
- $this->assertEquals(3, count($result));
- $this->assertFalse(isset($result[3]));
- $TestModel->saveAll(array(
- 'Post' => array(
- 'title' => 'Post with Author',
- 'body' => 'This post will be saved with an author'
- ),
- 'Author' => array(
- 'user' => 'bob',
- 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
- )));
- $result = $TestModel->find('all');
- $expected = array(
- 'Post' => array(
- 'id' => '4',
- 'author_id' => '5',
- 'title' => 'Post with Author',
- 'body' => 'This post will be saved with an author',
- 'published' => 'N'
- ),
- 'Author' => array(
- 'id' => '5',
- 'user' => 'bob',
- 'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
- 'test' => 'working'
- ));
- $this->assertEquals(self::date(), $result[3]['Post']['created']);
- $this->assertEquals(self::date(), $result[3]['Post']['updated']);
- $this->assertEquals(self::date(), $result[3]['Author']['created']);
- $this->assertEquals(self::date(), $result[3]['Author']['updated']);
- unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
- unset($result[3]['Author']['created'], $result[3]['Author']['updated']);
- $this->assertEquals($expected, $result[3]);
- $this->assertEquals(4, count($result));
- $TestModel->deleteAll(true);
- $this->assertEquals(array(), $TestModel->find('all'));
- // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
- $this->db->truncate($TestModel);
- $TestModel->saveAll(array(
- array(
- 'title' => 'Multi-record post 1',
- 'body' => 'First multi-record post',
- 'author_id' => 2
- ),
- array(
- 'title' => 'Multi-record post 2',
- 'body' => 'Second multi-record post',
- 'author_id' => 2
- )));
- $result = $TestModel->find('all', array(
- 'recursive' => -1,
- 'order' => 'Post.id ASC'
- ));
- $expected = array(
- array(
- 'Post' => array(
- 'id' => '1',
- 'author_id' => '2',
- 'title' => 'Multi-record post 1',
- 'body' => 'First multi-record post',
- 'published' => 'N'
- )),
- array(
- 'Post' => array(
- 'id' => '2',
- 'author_id' => '2',
- 'title' => 'Multi-record post 2',
- 'body' => 'Second multi-record post',
- 'published' => 'N'
- )));
- $this->assertEquals(self::date(), $result[0]['Post']['created']);
- $this->assertEquals(self::date(), $result[0]['Post']['updated']);
- $this->assertEquals(self::date(), $result[1]['Post']['created']);
- $this->assertEquals(self::date(), $result[1]['Post']['updated']);
- unset($result[0]['Post']['created'], $result[0]['Post']['updated']);
- unset($result[1]['Post']['created'], $result[1]['Post']['updated']);
- $this->assertEquals($expected, $result);
- $TestModel = new Comment();
- $result = $TestModel->saveAll(array(
- 'Comment' => array(
- 'article_id' => 2,
- 'user_id' => 2,
- 'comment' => 'New comment with attachment',
- 'published' => 'Y'
- ),
- 'Attachment' => array(
- 'attachment' => 'some_file.tgz'
- )));
- $this->assertFalse(empty($result));
- $result = $TestModel->find('all');
- $expected = array(
- 'id' => '7',
- 'article_id' => '2',
- 'user_id' => '2',
- 'comment' => 'New comment with attachment',
- 'published' => 'Y'
- );
- $this->assertEquals(self::date(), $result[6]['Comment']['created']);
- $this->assertEquals(self::date(), $result[6]['Comment']['updated']);
- unset($result[6]['Comment']['created'], $result[6]['Comment']['updated']);
- $this->assertEquals($expected, $result[6]['Comment']);
- $expected = array(
- 'id' => '2',
- 'comment_id' => '7',
- 'attachment' => 'some_file.tgz'
- );
- $this->assertEquals(self::date(), $result[6]['Attachment']['created']);
- $this->assertEquals(self::date(), $result[6]['Attachment']['updated']);
- unset($result[6]['Attachment']['created'], $result[6]['Attachment']['updated']);
- $this->assertEquals($expected, $result[6]['Attachment']);
- }
- /**
- * Test SaveAll with Habtm relations
- *
- * @return void
- */
- public function testSaveAllHabtm() {
- $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
- $data = array(
- 'Article' => array(
- 'user_id' => 1,
- 'title' => 'Article Has and belongs to Many Tags'
- ),
- 'Tag' => array(
- 'Tag' => array(1, 2)
- ),
- 'Comment' => array(
- array(
- 'comment' => 'Article comment',
- 'user_id' => 1
- )));
- $Article = new Article();
- $result = $Article->saveAll($data);
- $this->assertFalse(empty($result));
- $result = $Article->read();
- $this->assertEquals(2, count($result['Tag']));
- $this->assertEquals('tag1', $result['Tag'][0]['tag']);
- $this->assertEquals(1, count($result['Comment']));
- $this->assertEquals(1, count($result['Comment'][0]['comment']));
- }
- /**
- * Test SaveAll with Habtm relations and extra join table fields
- *
- * @return void
- */
- public function testSaveAllHabtmWithExtraJoinTableFields() {
- $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
- $data = array(
- 'Something' => array(
- 'id' => 4,
- 'title' => 'Extra Fields',
- 'body' => 'Extra Fields Body',
- 'published' => '1'
- ),
- 'SomethingElse' => array(
- array('something_else_id' => 1, 'doomed' => '1'),
- array('something_else_id' => 2, 'doomed' => '0'),
- array('something_else_id' => 3, 'doomed' => '1')
- )
- );
- $Something = new Something();
- $result = $Something->saveAll($data);
- $this->assertFalse(empty($result));
- $result = $Something->read();
- $this->assertEquals(3, count($result['SomethingElse']));
- $this->assertTrue(Set::matches('/Something[id=4]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
- }
- /**
- * testSaveAllHasOne method
- *
- * @return void
- */
- public function testSaveAllHasOne() {
- $model = new Comment();
- $model->deleteAll(true);
- $this->assertEquals(array(), $model->find('all'));
- $model->Attachment->deleteAll(true);
- $this->assertEquals(array(), $model->Attachment->find('all'));
- $this->assertTrue($model->saveAll(array(
- 'Comment' => array(
- 'comment' => 'Comment with attachment',
- 'article_id' => 1,
- 'user_id' => 1
- ),
- 'Attachment' => array(
- 'attachment' => 'some_file.zip'
- ))));
- $result = $model->find('all', array('fields' => array(
- 'Comment.id', 'Comment.comment', 'Attachment.id',
- 'Attachment.comment_id', 'Attachment.attachment'
- )));
- $expected = array(array(
- 'Comment' => array(
- 'id' => '1',
- 'comment' => 'Comment with attachment'
- ),
- 'Attachment' => array(
- 'id' => '1',
- 'comment_id' => '1',
- 'attachment' => 'some_file.zip'
- )));
- $this->assertEquals($expected, $result);
- $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
- $data = array(
- 'Comment' => array(
- 'comment' => 'Comment with attachment',
- 'article_id' => 1,
- 'user_id' => 1
- ),
- 'Attachment' => array(
- 'attachment' => 'some_file.zip'
- ));
- $this->assertTrue($model->saveAll($data, array('validate' => 'first')));
- }
- /**
- * testSaveAllBelongsTo method
- *
- * @return void
- */
- public function testSaveAllBelongsTo() {
- $model = new Comment();
- $model->deleteAll(true);
- $this->assertEquals(array(), $model->find('all'));
- $model->Article->deleteAll(true);
- $this->assertEquals(array(), $model->Article->find('all'));
- $this->assertTrue($model->saveAll(array(
- 'Comment' => array(
- 'comment' => 'Article comment',
- 'article_id' => 1,
- 'user_id' => 1
- ),
- 'Article' => array(
- 'title' => 'Model Associations 101',
- 'user_id' => 1
- ))));
- $result = $model->find('all', array('fields' => array(
- 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
- )));
- $expected = array(array(
- 'Comment' => array(
- 'id' => '1',
- 'article_id' => '1',
- 'comment' => 'Article comment'
- ),
- 'Article' => array(
- 'id' => '1',
- 'title' => 'Model Associations 101'
- )));
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllHasOneValidation method
- *
- * @return void
- */
- public function testSaveAllHasOneValidation() {
- $model = new Comment();
- $model->deleteAll(true);
- $this->assertEquals(array(), $model->find('all'));
- $model->Attachment->deleteAll(true);
- $this->assertEquals(array(), $model->Attachment->find('all'));
- $model->validate = array('comment' => 'notEmpty');
- $model->Attachment->validate = array('attachment' => 'notEmpty');
- $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
- $result = $model->saveAll(
- array(
- 'Comment' => array(
- 'comment' => '',
- 'article_id' => 1,
- 'user_id' => 1
- ),
- 'Attachment' => array('attachment' => '')
- ),
- array('validate' => 'first')
- );
- $this->assertEquals(false, $result);
- $expected = array(
- 'comment' => array('This field cannot be left blank'),
- 'Attachment' => array(
- 'attachment' => array('This field cannot be left blank')
- )
- );
- $this->assertEquals($expected, $model->validationErrors);
- $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
- }
- /**
- * testSaveAllAtomic method
- *
- * @return void
- */
- public function testSaveAllAtomic() {
- $this->loadFixtures('Article', 'User', 'Comment');
- $TestModel = new Article();
- $result = $TestModel->saveAll(array(
- 'Article' => array(
- 'title' => 'Post with Author',
- 'body' => 'This post will be saved with an author',
- 'user_id' => 2
- ),
- 'Comment' => array(
- array('comment' => 'First new comment', 'user_id' => 2))
- ), array('atomic' => false));
- $this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
- $result = $TestModel->saveAll(array(
- array(
- 'id' => '1',
- 'title' => 'Baleeted First Post',
- 'body' => 'Baleeted!',
- 'published' => 'N'
- ),
- array(
- 'id' => '2',
- 'title' => 'Just update the title'
- ),
- array(
- 'title' => 'Creating a fourth post',
- 'body' => 'Fourth post body',
- 'user_id' => 2
- )
- ), array('atomic' => false));
- $this->assertSame($result, array(true, true, true));
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'comment' => 'First new comment',
- 'published' => 'Y',
- 'user_id' => 1
- ),
- array(
- 'comment' => 'Second new comment',
- 'published' => 'Y',
- 'user_id' => 2
- ))
- ), array('validate' => true, 'atomic' => false));
- $this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
- $TestModel->validate = array(
- 'title' => 'notEmpty',
- 'author_id' => 'numeric'
- );
- $result = $TestModel->saveAll(array(
- array(
- 'id' => '1',
- 'title' => 'Un-Baleeted First Post',
- 'body' => 'Not Baleeted!',
- 'published' => 'Y'
- ),
- array(
- 'id' => '2',
- 'title' => '',
- 'body' => 'Trying to get away with an empty title'
- )
- ), array('validate' => true, 'atomic' => false));
- $this->assertSame(array(true, false), $result);
- }
- /**
- * testSaveAllDeepAssociated method
- *
- * @return void
- */
- public function testSaveAllDeepAssociated() {
- $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
- $TestModel = new Article();
- $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
- $TestModel->hasAndBelongsToMany = array();
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- ), array('deep' => true));
- $this->assertTrue($result);
- $result = $TestModel->findById(2);
- $expected = array(
- 'First Comment for Second Article',
- 'Second Comment for Second Article',
- 'First new comment',
- 'Second new comment'
- );
- $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
- $this->assertEquals($expected, $result);
- $result = $TestModel->Comment->User->field('id', array('user' => 'newuser', 'password' => 'newuserpass'));
- $this->assertEquals(5, $result);
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
- array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
- )
- ), array('deep' => true));
- $this->assertTrue($result);
- $result = $TestModel->findById(2);
- $expected = array(
- 'First Comment for Second Article',
- 'Second Comment for Second Article',
- 'First new comment',
- 'Second new comment',
- 'Third new comment',
- 'Fourth new comment'
- );
- $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
- $this->assertEquals($expected, $result);
- $result = $TestModel->Comment->Attachment->field('id', array('attachment' => 'deepsaved'));
- $this->assertEquals(2, $result);
- $data = array(
- 'Attachment' => array(
- 'attachment' => 'deepsave insert',
- ),
- 'Comment' => array(
- 'comment' => 'First comment deepsave insert',
- 'published' => 'Y',
- 'user_id' => 5,
- 'Article' => array(
- 'title' => 'First Article deepsave insert',
- 'body' => 'First Article Body deepsave insert',
- 'User' => array(
- 'user' => '',
- 'password' => 'magic'
- ),
- ),
- )
- );
- $TestModel->Comment->Attachment->create();
- $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => true));
- $this->assertFalse($result);
- $expected = array('User' => array('user' => array('This field cannot be left blank')));
- $this->assertEquals($expected, $TestModel->validationErrors);
- $data['Comment']['Article']['User']['user'] = 'deepsave';
- $TestModel->Comment->Attachment->create();
- $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => true));
- $this->assertTrue($result);
- $result = $TestModel->Comment->Attachment->findById($TestModel->Comment->Attachment->id);
- $expected = array(
- 'Attachment' => array(
- 'id' => '3',
- 'comment_id' => '11',
- 'attachment' => 'deepsave insert',
- ),
- 'Comment' => array(
- 'id' => '11',
- 'article_id' => '4',
- 'user_id' => '5',
- 'comment' => 'First comment deepsave insert',
- 'published' => 'Y',
- )
- );
- unset($result['Attachment']['created'], $result['Attachment']['updated']);
- $this->assertEquals($expected['Attachment'], $result['Attachment']);
- unset($result['Comment']['created'], $result['Comment']['updated']);
- $this->assertEquals($expected['Comment'], $result['Comment']);
- $result = $TestModel->findById($result['Comment']['article_id']);
- $expected = array(
- 'Article' => array(
- 'id' => '4',
- 'user_id' => '6',
- 'title' => 'First Article deepsave insert',
- 'body' => 'First Article Body deepsave insert',
- 'published' => 'N',
- ),
- 'User' => array(
- 'id' => '6',
- 'user' => 'deepsave',
- 'password' => 'magic',
- ),
- 'Comment' => array(
- array(
- 'id' => '11',
- 'article_id' => '4',
- 'user_id' => '5',
- 'comment' => 'First comment deepsave insert',
- 'published' => 'Y',
- )
- )
- );
- unset(
- $result['Article']['created'], $result['Article']['updated'],
- $result['User']['created'], $result['User']['updated'],
- $result['Comment'][0]['created'], $result['Comment'][0]['updated']
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllDeepMany
- * tests the validate methods with deeper recursive data
- *
- * @return void
- */
- public function testSaveAllDeepMany() {
- $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
- $TestModel = new Article();
- $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
- $TestModel->hasAndBelongsToMany = array();
- $data = array(
- array(
- 'Article' => array('id' => 1),
- 'Comment' => array(
- array('comment' => 'First comment deepsaved article 1', 'published' => 'Y', 'User' => array('user' => 'savemany', 'password' => 'manysaved')),
- array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
- )
- ),
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => 'moresaved')),
- array('comment' => 'Second comment deepsaved article 2', 'published' => 'Y', 'user_id' => 2)
- )
- )
- );
- $result = $TestModel->saveAll($data, array('deep' => true));
- $this->assertTrue($result);
- $data = array(
- array(
- 'id' => 1, 'body' => '',
- 'Comment' => array(
- array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
- array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
- )
- ),
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => '')),
- array('comment' => '', 'published' => 'Y', 'user_id' => 2)
- )
- )
- );
- $TestModel->Comment->validate['comment'] = 'notEmpty';
- $result = $TestModel->saveAll($data, array('deep' => true));
- $this->assertFalse($result);
- $expected = array(
- 0 => array(
- 'body' => array('This field cannot be left blank'),
- 'Comment' => array(
- 0 => array(
- 'comment' => array('This field cannot be left blank'),
- 'User' => array(
- 'user' => array('This field cannot be left blank')
- )
- )
- )
- ),
- 1 => array(
- 'Comment' => array(
- 0 => array(
- 'User' => array(
- 'password' => array('This field cannot be left blank')
- )
- ),
- 1 => array(
- 'comment' => array('This field cannot be left blank')
- )
- )
- )
- );
- $result = $TestModel->validationErrors;
- $this->assertSame($expected, $result);
- }
- /**
- * testSaveAllDeepValidateOnly
- * tests the validate methods with deeper recursive data
- *
- * @return void
- */
- public function testSaveAllDeepValidateOnly() {
- $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
- $TestModel = new Article();
- $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
- $TestModel->hasAndBelongsToMany = array();
- $TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
- $TestModel->Comment->validate['comment'] = 'notEmpty';
- $result = $TestModel->saveAll(
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- ),
- array('validate' => 'only', 'deep' => true)
- );
- $this->assertTrue($result);
- $result = $TestModel->saveAll(
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- ),
- array('validate' => 'only', 'deep' => true)
- );
- $this->assertFalse($result);
- $result = $TestModel->saveAll(
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- ),
- array('validate' => 'only', 'atomic' => false, 'deep' => true)
- );
- $expected = array(
- 'Article' => true,
- 'Comment' => array(
- true,
- true
- )
- );
- $this->assertSame($expected, $result);
- $result = $TestModel->saveAll(
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- ),
- array('validate' => 'only', 'atomic' => false, 'deep' => true)
- );
- $expected = array(
- 'Article' => true,
- 'Comment' => array(
- false,
- true
- )
- );
- $this->assertSame($expected, $result);
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
- array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
- )
- ),
- array('validate' => 'only', 'deep' => true)
- );
- $this->assertTrue($result);
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
- array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
- )
- ),
- array('validate' => 'only', 'deep' => true)
- );
- $this->assertFalse($result);
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
- array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsave'))
- )
- ),
- array('validate' => 'only', 'atomic' => false, 'deep' => true)
- );
- $expected = array(
- 'Article' => true,
- 'Comment' => array(
- true,
- true
- )
- );
- $this->assertSame($expected, $result);
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
- array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
- )
- ),
- array('validate' => 'only', 'atomic' => false, 'deep' => true)
- );
- $expected = array(
- 'Article' => true,
- 'Comment' => array(
- true,
- false
- )
- );
- $this->assertSame($expected, $result);
- $expected = array(
- 'Comment' => array(
- 1 => array(
- 'Attachment' => array(
- 'attachment' => array('This field cannot be left blank')
- )
- )
- )
- );
- $result = $TestModel->validationErrors;
- $this->assertSame($expected, $result);
- $data = array(
- 'Attachment' => array(
- 'attachment' => 'deepsave insert',
- ),
- 'Comment' => array(
- 'comment' => 'First comment deepsave insert',
- 'published' => 'Y',
- 'user_id' => 5,
- 'Article' => array(
- 'title' => 'First Article deepsave insert',
- 'body' => 'First Article Body deepsave insert',
- 'User' => array(
- 'user' => 'deepsave',
- 'password' => 'magic'
- ),
- ),
- )
- );
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
- $this->assertTrue($result);
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
- $expected = array(
- 'Attachment' => true,
- 'Comment' => true
- );
- $this->assertSame($expected, $result);
- $data = array(
- 'Attachment' => array(
- 'attachment' => 'deepsave insert',
- ),
- 'Comment' => array(
- 'comment' => 'First comment deepsave insert',
- 'published' => 'Y',
- 'user_id' => 5,
- 'Article' => array(
- 'title' => 'First Article deepsave insert',
- 'body' => 'First Article Body deepsave insert',
- 'User' => array(
- 'user' => '',
- 'password' => 'magic'
- ),
- ),
- )
- );
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
- $this->assertFalse($result);
- $result = $TestModel->Comment->Attachment->validationErrors;
- $expected = array(
- 'Comment' => array(
- 'Article' => array(
- 'User' => array(
- 'user' => array('This field cannot be left blank')
- )
- )
- )
- );
- $this->assertSame($expected, $result);
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
- $expected = array(
- 'Attachment' => true,
- 'Comment' => false
- );
- $this->assertEquals($expected, $result);
- $data['Comment']['Article']['body'] = '';
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
- $this->assertFalse($result);
- $result = $TestModel->Comment->Attachment->validationErrors;
- $expected = array(
- 'Comment' => array(
- 'Article' => array(
- 'body' => array('This field cannot be left blank'),
- 'User' => array(
- 'user' => array('This field cannot be left blank')
- )
- )
- )
- );
- $this->assertSame($expected, $result);
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
- $expected = array(
- 'Attachment' => true,
- 'Comment' => false
- );
- $this->assertEquals($expected, $result);
- $data['Comment']['comment'] = '';
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
- $this->assertFalse($result);
- $result = $TestModel->Comment->Attachment->validationErrors;
- $expected = array(
- 'Comment' => array(
- 'comment' => array('This field cannot be left blank'),
- 'Article' => array(
- 'body' => array('This field cannot be left blank'),
- 'User' => array(
- 'user' => array('This field cannot be left blank')
- )
- )
- )
- );
- $this->assertSame($expected, $result);
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
- $expected = array(
- 'Attachment' => true,
- 'Comment' => false
- );
- $this->assertEquals($expected, $result);
- $data['Attachment']['attachment'] = '';
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
- $this->assertFalse($result);
- $result = $TestModel->Comment->Attachment->validationErrors;
- $expected = array(
- 'attachment' => array('This field cannot be left blank'),
- 'Comment' => array(
- 'comment' => array('This field cannot be left blank'),
- 'Article' => array(
- 'body' => array('This field cannot be left blank'),
- 'User' => array(
- 'user' => array('This field cannot be left blank')
- )
- )
- )
- );
- $this->assertSame($expected, $result);
- $result = $TestModel->Comment->validationErrors;
- $expected = array(
- 'comment' => array('This field cannot be left blank'),
- 'Article' => array(
- 'body' => array('This field cannot be left blank'),
- 'User' => array(
- 'user' => array('This field cannot be left blank')
- )
- )
- );
- $this->assertSame($expected, $result);
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
- $expected = array(
- 'Attachment' => false,
- 'Comment' => false
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllNotDeepAssociated method
- * test that only directly associated data gets saved
- *
- * @return void
- */
- public function testSaveAllNotDeepAssociated() {
- $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
- $TestModel = new Article();
- $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
- $TestModel->hasAndBelongsToMany = array();
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'comment' => 'First new comment', 'published' => 'Y', 'user_id' => 2,
- 'User' => array('user' => 'newuser', 'password' => 'newuserpass')
- ),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- ), array('deep' => false));
- $this->assertTrue($result);
- $result = $TestModel->Comment->User->field('id', array('user' => 'newuser', 'password' => 'newuserpass'));
- $this->assertFalse($result);
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 4),
- array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
- )
- ), array('deep' => false));
- $this->assertTrue($result);
- $result = $TestModel->Comment->Attachment->field('id', array('attachment' => 'deepsaved'));
- $this->assertFalse($result);
- $data = array(
- 'Attachment' => array(
- 'attachment' => 'deepsave insert',
- ),
- 'Comment' => array(
- 'comment' => 'First comment deepsave insert',
- 'published' => 'Y',
- 'user_id' => 4,
- 'article_id' => 1,
- 'Article' => array(
- 'title' => 'First Article deepsave insert',
- 'body' => 'First Article Body deepsave insert',
- 'User' => array(
- 'user' => 'deepsave',
- 'password' => 'magic'
- ),
- ),
- )
- );
- $expected = $TestModel->User->find('count');
- $TestModel->Comment->Attachment->create();
- $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => false));
- $this->assertTrue($result);
- $result = $TestModel->User->find('count');
- $this->assertEquals($expected, $result);
- $result = $TestModel->Comment->Attachment->findById($TestModel->Comment->Attachment->id);
- $expected = array(
- 'Attachment' => array(
- 'id' => '2',
- 'comment_id' => '11',
- 'attachment' => 'deepsave insert',
- ),
- 'Comment' => array(
- 'id' => '11',
- 'article_id' => 1,
- 'user_id' => '4',
- 'comment' => 'First comment deepsave insert',
- 'published' => 'Y',
- )
- );
- unset($result['Attachment']['created'], $result['Attachment']['updated']);
- $this->assertEquals($expected['Attachment'], $result['Attachment']);
- unset($result['Comment']['created'], $result['Comment']['updated']);
- $this->assertEquals($expected['Comment'], $result['Comment']);
- }
- /**
- * testSaveAllNotDeepMany
- * tests the save methods to not save deeper recursive data
- *
- * @return void
- */
- public function testSaveAllNotDeepMany() {
- $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
- $TestModel = new Article();
- $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
- $TestModel->hasAndBelongsToMany = array();
- $data = array(
- array(
- 'id' => 1,
- 'body' => '',
- 'Comment' => array(
- array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
- array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
- )
- ),
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => '')),
- array('comment' => '', 'published' => 'Y', 'user_id' => 2)
- )
- )
- );
- $TestModel->Comment->validate['comment'] = 'notEmpty';
- $result = $TestModel->saveAll($data, array('deep' => false));
- $this->assertFalse($result);
- $expected = array(
- 0 => array(
- 'body' => array('This field cannot be left blank')
- )
- );
- $result = $TestModel->validationErrors;
- $this->assertSame($expected, $result);
- $data = array(
- array(
- 'Article' => array('id' => 1, 'body' => 'Ignore invalid comment'),
- 'Comment' => array(
- array('comment' => '', 'published' => 'Y', 'user_id' => 2)
- )
- ),
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => '', 'published' => 'Y', 'user_id' => 2)
- )
- )
- );
- $result = $TestModel->saveAll($data, array('deep' => false));
- $this->assertTrue($result);
- }
- /**
- * testSaveAllNotDeepValidateOnly
- * tests the validate methods to not validate deeper recursive data
- *
- * @return void
- */
- public function testSaveAllNotDeepValidateOnly() {
- $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
- $TestModel = new Article();
- $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
- $TestModel->hasAndBelongsToMany = array();
- $TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
- $TestModel->Comment->validate['comment'] = 'notEmpty';
- $result = $TestModel->saveAll(
- array(
- 'Article' => array('id' => 2, 'body' => ''),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- ),
- array('validate' => 'only', 'deep' => false)
- );
- $this->assertFalse($result);
- $expected = array('body' => array('This field cannot be left blank'));
- $result = $TestModel->validationErrors;
- $this->assertSame($expected, $result);
- $result = $TestModel->saveAll(
- array(
- 'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- ),
- array('validate' => 'only', 'deep' => false)
- );
- $this->assertTrue($result);
- $result = $TestModel->saveAll(
- array(
- 'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- ),
- array('validate' => 'only', 'atomic' => false, 'deep' => false)
- );
- $expected = array(
- 'Article' => true,
- 'Comment' => array(
- true,
- true
- )
- );
- $this->assertSame($expected, $result);
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
- 'Comment' => array(
- array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
- array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
- )
- ),
- array('validate' => 'only', 'deep' => false)
- );
- $this->assertTrue($result);
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
- 'Comment' => array(
- array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
- array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
- )
- ),
- array('validate' => 'only', 'atomic' => false, 'deep' => false)
- );
- $expected = array(
- 'Article' => true,
- 'Comment' => array(
- true,
- true
- )
- );
- $this->assertSame($expected, $result);
- $expected = array();
- $result = $TestModel->validationErrors;
- $this->assertSame($expected, $result);
- $data = array(
- 'Attachment' => array(
- 'attachment' => 'deepsave insert',
- ),
- 'Comment' => array(
- 'comment' => 'First comment deepsave insert',
- 'published' => 'Y',
- 'user_id' => 5,
- 'Article' => array(
- 'title' => 'First Article deepsave insert ignored',
- 'body' => 'First Article Body deepsave insert',
- 'User' => array(
- 'user' => '',
- 'password' => 'magic'
- ),
- ),
- )
- );
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
- $this->assertTrue($result);
- $result = $TestModel->Comment->Attachment->validationErrors;
- $expected = array();
- $this->assertSame($expected, $result);
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
- $expected = array(
- 'Attachment' => true,
- 'Comment' => true
- );
- $this->assertEquals($expected, $result);
- $data['Comment']['Article']['body'] = '';
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
- $this->assertTrue($result);
- $result = $TestModel->Comment->Attachment->validationErrors;
- $expected = array();
- $this->assertSame($expected, $result);
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
- $expected = array(
- 'Attachment' => true,
- 'Comment' => true
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllHasMany method
- *
- * @return void
- */
- public function testSaveAllHasMany() {
- $this->loadFixtures('Article', 'Comment');
- $TestModel = new Article();
- $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- ));
- $this->assertFalse(empty($result));
- $result = $TestModel->findById(2);
- $expected = array(
- 'First Comment for Second Article',
- 'Second Comment for Second Article',
- 'First new comment',
- 'Second new comment'
- );
- $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
- $this->assertEquals($expected, $result);
- $result = $TestModel->saveAll(
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'comment' => 'Third new comment',
- 'published' => 'Y',
- 'user_id' => 1
- ))),
- array('atomic' => false)
- );
- $this->assertFalse(empty($result));
- $result = $TestModel->findById(2);
- $expected = array(
- 'First Comment for Second Article',
- 'Second Comment for Second Article',
- 'First new comment',
- 'Second new comment',
- 'Third new comment'
- );
- $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
- $this->assertEquals($expected, $result);
- $TestModel->beforeSaveReturn = false;
- $result = $TestModel->saveAll(
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'comment' => 'Fourth new comment',
- 'published' => 'Y',
- 'user_id' => 1
- ))),
- array('atomic' => false)
- );
- $this->assertEquals(array('Article' => false), $result);
- $result = $TestModel->findById(2);
- $expected = array(
- 'First Comment for Second Article',
- 'Second Comment for Second Article',
- 'First new comment',
- 'Second new comment',
- 'Third new comment'
- );
- $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllHasManyValidation method
- *
- * @return void
- */
- public function testSaveAllHasManyValidation() {
- $this->loadFixtures('Article', 'Comment');
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
- $TestModel->Comment->validate = array('comment' => 'notEmpty');
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => '', 'published' => 'Y', 'user_id' => 1),
- )
- ), array('validate' => true));
- $this->assertFalse($result);
- $expected = array('Comment' => array(
- array('comment' => array('This field cannot be left blank'))
- ));
- $this->assertEquals($expected, $TestModel->validationErrors);
- $expected = array(
- array('comment' => array('This field cannot be left blank'))
- );
- $this->assertEquals($expected, $TestModel->Comment->validationErrors);
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'comment' => '',
- 'published' => 'Y',
- 'user_id' => 1
- ))
- ), array('validate' => 'first'));
- $this->assertFalse($result);
- }
- /**
- * test saveAll with transactions and ensure there is no missing rollback.
- *
- * @return void
- */
- public function testSaveAllManyRowsTransactionNoRollback() {
- $this->loadFixtures('Post');
- $this->getMock('DboSource', array('connect', 'rollback', 'describe'), array(), 'MockTransactionDboSource');
- $db = ConnectionManager::create('mock_transaction', array(
- 'datasource' => 'MockTransactionDboSource',
- ));
- $db->expects($this->once())
- ->method('describe')
- ->will($this->returnValue(array()));
- $db->expects($this->once())->method('rollback');
- $Post = new Post('mock_transaction');
- $Post->validate = array(
- 'title' => array('rule' => array('notEmpty'))
- );
- $data = array(
- array('author_id' => 1, 'title' => 'New Fourth Post'),
- array('author_id' => 1, 'title' => '')
- );
- $Post->saveAll($data, array('atomic' => true));
- }
- /**
- * test saveAll with transactions and ensure there is no missing rollback.
- *
- * @return void
- */
- public function testSaveAllAssociatedTransactionNoRollback() {
- $testDb = ConnectionManager::getDataSource('test');
- $this->getMock(
- 'DboSource',
- array('connect', 'rollback', 'describe', 'create', 'update', 'begin'),
- array(),
- 'MockTransactionAssociatedDboSource'
- );
- $db = ConnectionManager::create('mock_transaction_assoc', array(
- 'datasource' => 'MockTransactionAssociatedDboSource',
- ));
- $this->mockObjects[] = $db;
- $db->columns = $testDb->columns;
- $db->expects($this->once())->method('rollback');
- $db->expects($this->any())->method('describe')
- ->will($this->returnValue(array(
- 'id' => array('type' => 'integer', 'length' => 11),
- 'title' => array('type' => 'string'),
- 'body' => array('type' => 'text'),
- 'published' => array('type' => 'string')
- )));
- $Post = new Post();
- $Post->useDbConfig = 'mock_transaction_assoc';
- $Post->Author->useDbConfig = 'mock_transaction_assoc';
- $Post->Author->validate = array(
- 'user' => array('rule' => array('notEmpty'))
- );
- $data = array(
- 'Post' => array(
- 'title' => 'New post',
- 'body' => 'Content',
- 'published' => 'Y'
- ),
- 'Author' => array(
- 'user' => '',
- 'password' => "sekret"
- )
- );
- $Post->saveAll($data, array('validate' => true));
- }
- /**
- * test saveAll with nested saveAll call.
- *
- * @return void
- */
- public function testSaveAllNestedSaveAll() {
- $this->loadFixtures('Sample');
- $TransactionTestModel = new TransactionTestModel();
- $data = array(
- array('apple_id' => 1, 'name' => 'sample5'),
- );
- $this->assertTrue($TransactionTestModel->saveAll($data, array('atomic' => true)));
- }
- /**
- * testSaveAllTransaction method
- *
- * @return void
- */
- public function testSaveAllTransaction() {
- $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
- $TestModel = new Post();
- $TestModel->validate = array('title' => 'notEmpty');
- $data = array(
- array('author_id' => 1, 'title' => 'New Fourth Post'),
- array('author_id' => 1, 'title' => 'New Fifth Post'),
- array('author_id' => 1, 'title' => '')
- );
- $this->assertFalse($TestModel->saveAll($data));
- $result = $TestModel->find('all', array('recursive' => -1));
- $expected = array(
- array('Post' => array(
- 'id' => '1',
- 'author_id' => 1,
- 'title' => 'First Post',
- 'body' => 'First Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:39:23',
- 'updated' => '2007-03-18 10:41:31'
- )),
- array('Post' => array(
- 'id' => '2',
- 'author_id' => 3,
- 'title' => 'Second Post',
- 'body' => 'Second Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:41:23',
- 'updated' => '2007-03-18 10:43:31'
- )),
- array('Post' => array(
- 'id' => '3',
- 'author_id' => 1,
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:43:23',
- 'updated' => '2007-03-18 10:45:31'
- )));
- if (count($result) != 3) {
- // Database doesn't support transactions
- $expected[] = array(
- 'Post' => array(
- 'id' => '4',
- 'author_id' => 1,
- 'title' => 'New Fourth Post',
- 'body' => null,
- 'published' => 'N',
- 'created' => self::date(),
- 'updated' => self::date()
- ));
- $expected[] = array(
- 'Post' => array(
- 'id' => '5',
- 'author_id' => 1,
- 'title' => 'New Fifth Post',
- 'body' => null,
- 'published' => 'N',
- 'created' => self::date(),
- 'updated' => self::date()
- ));
- $this->assertEquals($expected, $result);
- // Skip the rest of the transactional tests
- return;
- }
- $this->assertEquals($expected, $result);
- $data = array(
- array('author_id' => 1, 'title' => 'New Fourth Post'),
- array('author_id' => 1, 'title' => ''),
- array('author_id' => 1, 'title' => 'New Sixth Post')
- );
- $this->assertFalse($TestModel->saveAll($data));
- $result = $TestModel->find('all', array('recursive' => -1));
- $expected = array(
- array('Post' => array(
- 'id' => '1',
- 'author_id' => 1,
- 'title' => 'First Post',
- 'body' => 'First Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:39:23',
- 'updated' => '2007-03-18 10:41:31'
- )),
- array('Post' => array(
- 'id' => '2',
- 'author_id' => 3,
- 'title' => 'Second Post',
- 'body' => 'Second Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:41:23',
- 'updated' => '2007-03-18 10:43:31'
- )),
- array('Post' => array(
- 'id' => '3',
- 'author_id' => 1,
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:43:23',
- 'updated' => '2007-03-18 10:45:31'
- )));
- if (count($result) != 3) {
- // Database doesn't support transactions
- $expected[] = array(
- 'Post' => array(
- 'id' => '4',
- 'author_id' => 1,
- 'title' => 'New Fourth Post',
- 'body' => 'Third Post Body',
- 'published' => 'N',
- 'created' => self::date(),
- 'updated' => self::date()
- ));
- $expected[] = array(
- 'Post' => array(
- 'id' => '5',
- 'author_id' => 1,
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'N',
- 'created' => self::date(),
- 'updated' => self::date()
- ));
- }
- $this->assertEquals($expected, $result);
- $TestModel->validate = array('title' => 'notEmpty');
- $data = array(
- array('author_id' => 1, 'title' => 'New Fourth Post'),
- array('author_id' => 1, 'title' => 'New Fifth Post'),
- array('author_id' => 1, 'title' => 'New Sixth Post')
- );
- $this->assertTrue($TestModel->saveAll($data));
- $result = $TestModel->find('all', array(
- 'recursive' => -1,
- 'fields' => array('author_id', 'title','body','published'),
- 'order' => array('Post.created' => 'ASC')
- ));
- $expected = array(
- array('Post' => array(
- 'author_id' => 1,
- 'title' => 'First Post',
- 'body' => 'First Post Body',
- 'published' => 'Y'
- )),
- array('Post' => array(
- 'author_id' => 3,
- 'title' => 'Second Post',
- 'body' => 'Second Post Body',
- 'published' => 'Y'
- )),
- array('Post' => array(
- 'author_id' => 1,
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'Y'
- )),
- array('Post' => array(
- 'author_id' => 1,
- 'title' => 'New Fourth Post',
- 'body' => '',
- 'published' => 'N'
- )),
- array('Post' => array(
- 'author_id' => 1,
- 'title' => 'New Fifth Post',
- 'body' => '',
- 'published' => 'N'
- )),
- array('Post' => array(
- 'author_id' => 1,
- 'title' => 'New Sixth Post',
- 'body' => '',
- 'published' => 'N'
- )));
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllValidation method
- *
- * @return void
- */
- public function testSaveAllValidation() {
- $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
- $TestModel = new Post();
- $data = array(
- array(
- 'id' => '1',
- 'title' => 'Baleeted First Post',
- 'body' => 'Baleeted!',
- 'published' => 'N'
- ),
- array(
- 'id' => '2',
- 'title' => 'Just update the title'
- ),
- array(
- 'title' => 'Creating a fourth post',
- 'body' => 'Fourth post body',
- 'author_id' => 2
- ));
- $this->assertTrue($TestModel->saveAll($data));
- $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
- $expected = array(
- array(
- 'Post' => array(
- 'id' => '1',
- 'author_id' => '1',
- 'title' => 'Baleeted First Post',
- 'body' => 'Baleeted!',
- 'published' => 'N',
- 'created' => '2007-03-18 10:39:23'
- )),
- array(
- 'Post' => array(
- 'id' => '2',
- 'author_id' => '3',
- 'title' => 'Just update the title',
- 'body' => 'Second Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:41:23'
- )),
- array(
- 'Post' => array(
- 'id' => '3',
- 'author_id' => '1',
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:43:23',
- 'updated' => '2007-03-18 10:45:31'
- )),
- array(
- 'Post' => array(
- 'id' => '4',
- 'author_id' => '2',
- 'title' => 'Creating a fourth post',
- 'body' => 'Fourth post body',
- 'published' => 'N'
- )));
- $this->assertEquals(self::date(), $result[0]['Post']['updated']);
- $this->assertEquals(self::date(), $result[1]['Post']['updated']);
- $this->assertEquals(self::date(), $result[3]['Post']['created']);
- $this->assertEquals(self::date(), $result[3]['Post']['updated']);
- unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
- unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
- $this->assertEquals($expected, $result);
- $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
- $data = array(
- array(
- 'id' => '1',
- 'title' => 'Un-Baleeted First Post',
- 'body' => 'Not Baleeted!',
- 'published' => 'Y'
- ),
- array(
- 'id' => '2',
- 'title' => '',
- 'body' => 'Trying to get away with an empty title'
- ));
- $result = $TestModel->saveAll($data);
- $this->assertFalse($result);
- $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
- $errors = array(1 => array('title' => array('This field cannot be left blank')));
- $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
- if (!$transactionWorked) {
- $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
- $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
- }
- $this->assertEquals($errors, $TestModel->validationErrors);
- $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
- $data = array(
- array(
- 'id' => '1',
- 'title' => 'Un-Baleeted First Post',
- 'body' => 'Not Baleeted!',
- 'published' => 'Y'
- ),
- array(
- 'id' => '2',
- 'title' => '',
- 'body' => 'Trying to get away with an empty title'
- ));
- $result = $TestModel->saveAll($data, array('validate' => true, 'atomic' => false));
- $this->assertEquals(array(true, false), $result);
- $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
- $errors = array(1 => array('title' => array('This field cannot be left blank')));
- $expected = array(
- array(
- 'Post' => array(
- 'id' => '1',
- 'author_id' => '1',
- 'title' => 'Un-Baleeted First Post',
- 'body' => 'Not Baleeted!',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:39:23'
- )
- ),
- array(
- 'Post' => array(
- 'id' => '2',
- 'author_id' => '3',
- 'title' => 'Just update the title',
- 'body' => 'Second Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:41:23'
- )
- ),
- array(
- 'Post' => array(
- 'id' => '3',
- 'author_id' => '1',
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:43:23',
- 'updated' => '2007-03-18 10:45:31'
- )
- ),
- array(
- 'Post' => array(
- 'id' => '4',
- 'author_id' => '2',
- 'title' => 'Creating a fourth post',
- 'body' => 'Fourth post body',
- 'published' => 'N'
- )
- )
- );
- $this->assertEquals(self::date(), $result[0]['Post']['updated']);
- $this->assertEquals(self::date(), $result[1]['Post']['updated']);
- $this->assertEquals(self::date(), $result[3]['Post']['updated']);
- $this->assertEquals(self::date(), $result[3]['Post']['created']);
- unset(
- $result[0]['Post']['updated'], $result[1]['Post']['updated'],
- $result[3]['Post']['updated'], $result[3]['Post']['created']
- );
- $this->assertEquals($expected, $result);
- $this->assertEquals($errors, $TestModel->validationErrors);
- $data = array(
- array(
- 'id' => '1',
- 'title' => 'Re-Baleeted First Post',
- 'body' => 'Baleeted!',
- 'published' => 'N'
- ),
- array(
- 'id' => '2',
- 'title' => '',
- 'body' => 'Trying to get away with an empty title'
- ));
- $this->assertFalse($TestModel->saveAll($data, array('validate' => 'first')));
- $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
- unset(
- $result[0]['Post']['updated'], $result[1]['Post']['updated'],
- $result[3]['Post']['updated'], $result[3]['Post']['created']
- );
- $this->assertEquals($expected, $result);
- $this->assertEquals($errors, $TestModel->validationErrors);
- }
- /**
- * testSaveAllValidationOnly method
- *
- * @return void
- */
- public function testSaveAllValidationOnly() {
- $this->loadFixtures('Comment', 'Attachment');
- $TestModel = new Comment();
- $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
- $data = array(
- 'Comment' => array(
- 'comment' => 'This is the comment'
- ),
- 'Attachment' => array(
- 'attachment' => ''
- )
- );
- $result = $TestModel->saveAll($data, array('validate' => 'only'));
- $this->assertFalse($result);
- $TestModel = new Article();
- $TestModel->validate = array('title' => 'notEmpty');
- $result = $TestModel->saveAll(
- array(
- 0 => array('title' => ''),
- 1 => array('title' => 'title 1'),
- 2 => array('title' => 'title 2'),
- ),
- array('validate' => 'only')
- );
- $this->assertFalse($result);
- $expected = array(
- 0 => array('title' => array('This field cannot be left blank')),
- );
- $this->assertEquals($expected, $TestModel->validationErrors);
- $result = $TestModel->saveAll(
- array(
- 0 => array('title' => 'title 0'),
- 1 => array('title' => ''),
- 2 => array('title' => 'title 2'),
- ),
- array('validate' => 'only')
- );
- $this->assertFalse($result);
- $expected = array(
- 1 => array('title' => array('This field cannot be left blank')),
- );
- $this->assertEquals($expected, $TestModel->validationErrors);
- }
- /**
- * testSaveAllValidateFirst method
- *
- * @return void
- */
- public function testSaveAllValidateFirst() {
- $this->loadFixtures('Article', 'Comment', 'Attachment', 'User', 'ArticlesTag', 'Tag');
- $model = new Article();
- $model->deleteAll(true);
- $model->Comment->validate = array('comment' => 'notEmpty');
- $result = $model->saveAll(array(
- 'Article' => array(
- 'title' => 'Post with Author',
- 'body' => 'This post will be saved author'
- ),
- 'Comment' => array(
- array('comment' => 'First new comment'),
- array('comment' => '')
- )
- ), array('validate' => 'first'));
- $this->assertFalse($result);
- $result = $model->find('all');
- $this->assertSame(array(), $result);
- $expected = array('Comment' => array(
- 1 => array('comment' => array('This field cannot be left blank'))
- ));
- $this->assertEquals($expected['Comment'], $model->Comment->validationErrors);
- $this->assertSame($model->Comment->find('count'), 0);
- $result = $model->saveAll(
- array(
- 'Article' => array(
- 'title' => 'Post with Author',
- 'body' => 'This post will be saved with an author',
- 'user_id' => 2
- ),
- 'Comment' => array(
- array(
- 'comment' => 'Only new comment',
- 'user_id' => 2
- ))),
- array('validate' => 'first')
- );
- $this->assertSame($result, true);
- $result = $model->Comment->find('all');
- $this->assertSame(count($result), 1);
- $result = Hash::extract($result, '{n}.Comment.article_id');
- $this->assertEquals(4, $result[0]);
- $model->deleteAll(true);
- $data = array(
- 'Article' => array(
- 'title' => 'Post with Author saveAlled from comment',
- 'body' => 'This post will be saved with an author',
- 'user_id' => 2
- ),
- 'Comment' => array(
- 'comment' => 'Only new comment', 'user_id' => 2
- ));
- $result = $model->Comment->saveAll($data, array('validate' => 'first'));
- $this->assertFalse(empty($result));
- $result = $model->find('all');
- $this->assertEquals(
- $result[0]['Article']['title'],
- 'Post with Author saveAlled from comment'
- );
- $this->assertEquals('Only new comment', $result[0]['Comment'][0]['comment']);
- }
- /**
- * test saveAll()'s return is correct when using atomic = false and validate = first.
- *
- * @return void
- */
- public function testSaveAllValidateFirstAtomicFalse() {
- $this->loadFixtures('Something');
- $Something = new Something();
- $invalidData = array(
- array(
- 'title' => 'foo',
- 'body' => 'bar',
- 'published' => 'baz',
- ),
- array(
- 'body' => 3,
- 'published' => 'sd',
- ),
- );
- $Something->create();
- $Something->validate = array(
- 'title' => array(
- 'rule' => 'alphaNumeric',
- 'required' => true,
- ),
- 'body' => array(
- 'rule' => 'alphaNumeric',
- 'required' => true,
- 'allowEmpty' => true,
- ),
- );
- $result = $Something->saveAll($invalidData, array(
- 'atomic' => false,
- 'validate' => 'first',
- ));
- $expected = array(true, false);
- $this->assertEquals($expected, $result);
- $Something = new Something();
- $validData = array(
- array(
- 'title' => 'title value',
- 'body' => 'body value',
- 'published' => 'baz',
- ),
- array(
- 'title' => 'valid',
- 'body' => 'this body',
- 'published' => 'sd',
- ),
- );
- $Something->create();
- $result = $Something->saveAll($validData, array(
- 'atomic' => false,
- 'validate' => 'first',
- ));
- $expected = array(true, true);
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllHasManyValidationOnly method
- *
- * @return void
- */
- public function testSaveAllHasManyValidationOnly() {
- $this->loadFixtures('Article', 'Comment', 'Attachment');
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
- $TestModel->Comment->validate = array('comment' => 'notEmpty');
- $result = $TestModel->saveAll(
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'id' => 1,
- 'comment' => '',
- 'published' => 'Y',
- 'user_id' => 1),
- array(
- 'id' => 2,
- 'comment' =>
- 'comment',
- 'published' => 'Y',
- 'user_id' => 1
- ))),
- array('validate' => 'only')
- );
- $this->assertFalse($result);
- $result = $TestModel->saveAll(
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'id' => 1,
- 'comment' => '',
- 'published' => 'Y',
- 'user_id' => 1
- ),
- array(
- 'id' => 2,
- 'comment' => 'comment',
- 'published' => 'Y',
- 'user_id' => 1
- ),
- array(
- 'id' => 3,
- 'comment' => '',
- 'published' => 'Y',
- 'user_id' => 1
- ))),
- array(
- 'validate' => 'only',
- 'atomic' => false
- ));
- $expected = array(
- 'Article' => true,
- 'Comment' => array(false, true, false)
- );
- $this->assertSame($expected, $result);
- $expected = array('Comment' => array(
- 0 => array('comment' => array('This field cannot be left blank')),
- 2 => array('comment' => array('This field cannot be left blank'))
- ));
- $this->assertEquals($expected, $TestModel->validationErrors);
- $expected = array(
- 0 => array('comment' => array('This field cannot be left blank')),
- 2 => array('comment' => array('This field cannot be left blank'))
- );
- $this->assertEquals($expected, $TestModel->Comment->validationErrors);
- }
- /**
- * test that saveAll still behaves like previous versions (does not necessarily need a first argument)
- *
- * @return void
- */
- public function testSaveAllWithSet() {
- $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
- $data = array(
- 'Article' => array(
- 'user_id' => 1,
- 'title' => 'Article Has and belongs to Many Tags'
- ),
- 'Tag' => array(
- 'Tag' => array(1, 2)
- ),
- 'Comment' => array(
- array(
- 'comment' => 'Article comment',
- 'user_id' => 1
- )));
- $Article = new Article();
- $Article->set($data);
- $result = $Article->saveAll();
- $this->assertFalse(empty($result));
- }
- /**
- * test that saveAll behaves like plain save() when supplied empty data
- *
- * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
- * @return void
- */
- public function testSaveAllEmptyData() {
- $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
- $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
- $model = new Article();
- $result = $model->saveAll(array(), array('validate' => 'first'));
- $this->assertFalse(empty($result));
- $model = new ProductUpdateAll();
- $result = $model->saveAll();
- $this->assertFalse($result);
- }
- /**
- * testSaveAssociated method
- *
- * @return void
- */
- public function testSaveAssociated() {
- $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
- $TestModel = new Post();
- $result = $TestModel->find('all');
- $this->assertEquals(3, count($result));
- $this->assertFalse(isset($result[3]));
- $TestModel->saveAssociated(array(
- 'Post' => array(
- 'title' => 'Post with Author',
- 'body' => 'This post will be saved with an author'
- ),
- 'Author' => array(
- 'user' => 'bob',
- 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
- )));
- $result = $TestModel->find('all', array('order' => array('Post.id ' => 'ASC')));
- $expected = array(
- 'Post' => array(
- 'id' => '4',
- 'author_id' => '5',
- 'title' => 'Post with Author',
- 'body' => 'This post will be saved with an author',
- 'published' => 'N'
- ),
- 'Author' => array(
- 'id' => '5',
- 'user' => 'bob',
- 'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
- 'test' => 'working'
- ));
- $this->assertEquals(self::date(), $result[3]['Post']['updated']);
- $this->assertEquals(self::date(), $result[3]['Post']['created']);
- $this->assertEquals(self::date(), $result[3]['Author']['created']);
- $this->assertEquals(self::date(), $result[3]['Author']['updated']);
- unset(
- $result[3]['Post']['updated'], $result[3]['Post']['created'],
- $result[3]['Author']['updated'], $result[3]['Author']['created']
- );
- $this->assertEquals($expected, $result[3]);
- $this->assertEquals(4, count($result));
- $TestModel = new Comment();
- $result = $TestModel->saveAssociated(array(
- 'Comment' => array(
- 'article_id' => 2,
- 'user_id' => 2,
- 'comment' => 'New comment with attachment',
- 'published' => 'Y'
- ),
- 'Attachment' => array(
- 'attachment' => 'some_file.tgz'
- )));
- $this->assertFalse(empty($result));
- $result = $TestModel->find('all');
- $expected = array(
- 'id' => '7',
- 'article_id' => '2',
- 'user_id' => '2',
- 'comment' => 'New comment with attachment',
- 'published' => 'Y'
- );
- $this->assertEquals(self::date(), $result[6]['Comment']['updated']);
- $this->assertEquals(self::date(), $result[6]['Comment']['created']);
- unset($result[6]['Comment']['updated'], $result[6]['Comment']['created']);
- $this->assertEquals($expected, $result[6]['Comment']);
- $expected = array(
- 'id' => '2',
- 'comment_id' => '7',
- 'attachment' => 'some_file.tgz'
- );
- $this->assertEquals(self::date(), $result[6]['Attachment']['updated']);
- $this->assertEquals(self::date(), $result[6]['Attachment']['created']);
- unset($result[6]['Attachment']['updated'], $result[6]['Attachment']['created']);
- $this->assertEquals($expected, $result[6]['Attachment']);
- }
- /**
- * Test that validate = first, atomic = false works when associated records
- * fail validation.
- *
- * @return void
- */
- public function testSaveAssociatedAtomicFalseValidateFirstWithErrors() {
- $this->loadFixtures('Comment', 'Article', 'User');
- $Article = ClassRegistry::init('Article');
- $Article->Comment->validator()->add('comment', array(
- array('rule' => 'notEmpty')
- ));
- $data = array(
- 'Article' => array(
- 'user_id' => 1,
- 'title' => 'Foo',
- 'body' => 'text',
- 'published' => 'N'
- ),
- 'Comment' => array(
- array(
- 'user_id' => 1,
- 'comment' => '',
- 'published' => 'N',
- )
- ),
- );
- $Article->saveAssociated(
- $data,
- array('validate' => 'first', 'atomic' => false)
- );
- $result = $Article->validationErrors;
- $expected = array(
- 'Comment' => array(
- array(
- 'comment' => array( 'This field cannot be left blank' )
- )
- )
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveMany method
- *
- * @return void
- */
- public function testSaveMany() {
- $this->loadFixtures('Post');
- $TestModel = new Post();
- $TestModel->deleteAll(true);
- $this->assertEquals(array(), $TestModel->find('all'));
- // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
- $this->db->truncate($TestModel);
- $TestModel->saveMany(array(
- array(
- 'title' => 'Multi-record post 1',
- 'body' => 'First multi-record post',
- 'author_id' => 2
- ),
- array(
- 'title' => 'Multi-record post 2',
- 'body' => 'Second multi-record post',
- 'author_id' => 2
- )));
- $result = $TestModel->find('all', array(
- 'recursive' => -1,
- 'order' => 'Post.id ASC'
- ));
- $expected = array(
- array(
- 'Post' => array(
- 'id' => '1',
- 'author_id' => '2',
- 'title' => 'Multi-record post 1',
- 'body' => 'First multi-record post',
- 'published' => 'N'
- )
- ),
- array(
- 'Post' => array(
- 'id' => '2',
- 'author_id' => '2',
- 'title' => 'Multi-record post 2',
- 'body' => 'Second multi-record post',
- 'published' => 'N'
- )
- )
- );
- $this->assertEquals(self::date(), $result[0]['Post']['updated']);
- $this->assertEquals(self::date(), $result[0]['Post']['created']);
- $this->assertEquals(self::date(), $result[1]['Post']['updated']);
- $this->assertEquals(self::date(), $result[1]['Post']['created']);
- unset($result[0]['Post']['updated'], $result[0]['Post']['created']);
- unset($result[1]['Post']['updated'], $result[1]['Post']['created']);
- $this->assertEquals($expected, $result);
- }
- /**
- * Test SaveAssociated with Habtm relations
- *
- * @return void
- */
- public function testSaveAssociatedHabtm() {
- $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
- $data = array(
- 'Article' => array(
- 'user_id' => 1,
- 'title' => 'Article Has and belongs to Many Tags'
- ),
- 'Tag' => array(
- 'Tag' => array(1, 2)
- ),
- 'Comment' => array(
- array(
- 'comment' => 'Article comment',
- 'user_id' => 1
- )));
- $Article = new Article();
- $result = $Article->saveAssociated($data);
- $this->assertFalse(empty($result));
- $result = $Article->read();
- $this->assertEquals(2, count($result['Tag']));
- $this->assertEquals('tag1', $result['Tag'][0]['tag']);
- $this->assertEquals(1, count($result['Comment']));
- $this->assertEquals(1, count($result['Comment'][0]['comment']));
- }
- /**
- * Test SaveAssociated with Habtm relations and extra join table fields
- *
- * @return void
- */
- public function testSaveAssociatedHabtmWithExtraJoinTableFields() {
- $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
- $data = array(
- 'Something' => array(
- 'id' => 4,
- 'title' => 'Extra Fields',
- 'body' => 'Extra Fields Body',
- 'published' => '1'
- ),
- 'SomethingElse' => array(
- array('something_else_id' => 1, 'doomed' => '1'),
- array('something_else_id' => 2, 'doomed' => '0'),
- array('something_else_id' => 3, 'doomed' => '1')
- )
- );
- $Something = new Something();
- $result = $Something->saveAssociated($data);
- $this->assertFalse(empty($result));
- $result = $Something->read();
- $this->assertEquals(3, count($result['SomethingElse']));
- $this->assertTrue(Set::matches('/Something[id=4]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
- $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
- }
- /**
- * testSaveAssociatedHasOne method
- *
- * @return void
- */
- public function testSaveAssociatedHasOne() {
- $model = new Comment();
- $model->deleteAll(true);
- $this->assertEquals(array(), $model->find('all'));
- $model->Attachment->deleteAll(true);
- $this->assertEquals(array(), $model->Attachment->find('all'));
- $this->assertTrue($model->saveAssociated(array(
- 'Comment' => array(
- 'comment' => 'Comment with attachment',
- 'article_id' => 1,
- 'user_id' => 1
- ),
- 'Attachment' => array(
- 'attachment' => 'some_file.zip'
- ))));
- $result = $model->find('all', array('fields' => array(
- 'Comment.id', 'Comment.comment', 'Attachment.id',
- 'Attachment.comment_id', 'Attachment.attachment'
- )));
- $expected = array(array(
- 'Comment' => array(
- 'id' => '1',
- 'comment' => 'Comment with attachment'
- ),
- 'Attachment' => array(
- 'id' => '1',
- 'comment_id' => '1',
- 'attachment' => 'some_file.zip'
- )));
- $this->assertEquals($expected, $result);
- $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
- $data = array(
- 'Comment' => array(
- 'comment' => 'Comment with attachment',
- 'article_id' => 1,
- 'user_id' => 1
- ),
- 'Attachment' => array(
- 'attachment' => 'some_file.zip'
- ));
- $this->assertTrue($model->saveAssociated($data, array('validate' => 'first')));
- }
- /**
- * testSaveAssociatedBelongsTo method
- *
- * @return void
- */
- public function testSaveAssociatedBelongsTo() {
- $model = new Comment();
- $model->deleteAll(true);
- $this->assertEquals(array(), $model->find('all'));
- $model->Article->deleteAll(true);
- $this->assertEquals(array(), $model->Article->find('all'));
- $this->assertTrue($model->saveAssociated(array(
- 'Comment' => array(
- 'comment' => 'Article comment',
- 'article_id' => 1,
- 'user_id' => 1
- ),
- 'Article' => array(
- 'title' => 'Model Associations 101',
- 'user_id' => 1
- ))));
- $result = $model->find('all', array('fields' => array(
- 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
- )));
- $expected = array(array(
- 'Comment' => array(
- 'id' => '1',
- 'article_id' => '1',
- 'comment' => 'Article comment'
- ),
- 'Article' => array(
- 'id' => '1',
- 'title' => 'Model Associations 101'
- )));
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAssociatedHasOneValidation method
- *
- * @return void
- */
- public function testSaveAssociatedHasOneValidation() {
- $model = new Comment();
- $model->deleteAll(true);
- $this->assertEquals(array(), $model->find('all'));
- $model->Attachment->deleteAll(true);
- $this->assertEquals(array(), $model->Attachment->find('all'));
- $model->validate = array('comment' => 'notEmpty');
- $model->Attachment->validate = array('attachment' => 'notEmpty');
- $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
- $result = $model->saveAssociated(
- array(
- 'Comment' => array(
- 'comment' => '',
- 'article_id' => 1,
- 'user_id' => 1
- ),
- 'Attachment' => array('attachment' => '')
- )
- );
- $this->assertFalse($result);
- $expected = array(
- 'comment' => array(
- 'This field cannot be left blank'
- ),
- 'Attachment' => array(
- 'attachment' => array(
- 'This field cannot be left blank'
- )
- )
- );
- $this->assertEquals($expected, $model->validationErrors);
- $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
- }
- /**
- * testSaveAssociatedAtomic method
- *
- * @return void
- */
- public function testSaveAssociatedAtomic() {
- $this->loadFixtures('Article', 'User');
- $TestModel = new Article();
- $result = $TestModel->saveAssociated(array(
- 'Article' => array(
- 'title' => 'Post with Author',
- 'body' => 'This post will be saved with an author',
- 'user_id' => 2
- ),
- 'Comment' => array(
- array('comment' => 'First new comment', 'user_id' => 2))
- ), array('atomic' => false));
- $this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
- $result = $TestModel->saveAssociated(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'comment' => 'First new comment',
- 'published' => 'Y',
- 'user_id' => 1
- ),
- array(
- 'comment' => 'Second new comment',
- 'published' => 'Y',
- 'user_id' => 2
- ))
- ), array('validate' => true, 'atomic' => false));
- $this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
- }
- /**
- * testSaveManyAtomic method
- *
- * @return void
- */
- public function testSaveManyAtomic() {
- $this->loadFixtures('Article', 'User');
- $TestModel = new Article();
- $result = $TestModel->saveMany(array(
- array(
- 'id' => '1',
- 'title' => 'Baleeted First Post',
- 'body' => 'Baleeted!',
- 'published' => 'N'
- ),
- array(
- 'id' => '2',
- 'title' => 'Just update the title'
- ),
- array(
- 'title' => 'Creating a fourth post',
- 'body' => 'Fourth post body',
- 'user_id' => 2
- )
- ), array('atomic' => false));
- $this->assertSame($result, array(true, true, true));
- $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
- $result = $TestModel->saveMany(array(
- array(
- 'id' => '1',
- 'title' => 'Un-Baleeted First Post',
- 'body' => 'Not Baleeted!',
- 'published' => 'Y'
- ),
- array(
- 'id' => '2',
- 'title' => '',
- 'body' => 'Trying to get away with an empty title'
- )
- ), array('validate' => true, 'atomic' => false));
- $this->assertSame(array(true, false), $result);
- }
- /**
- * testSaveAssociatedHasMany method
- *
- * @return void
- */
- public function testSaveAssociatedHasMany() {
- $this->loadFixtures('Article', 'Comment');
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
- $result = $TestModel->saveAssociated(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- ));
- $this->assertFalse(empty($result));
- $result = $TestModel->findById(2);
- $expected = array(
- 'First Comment for Second Article',
- 'Second Comment for Second Article',
- 'First new comment',
- 'Second new comment'
- );
- $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
- $result = $TestModel->saveAssociated(
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'comment' => 'Third new comment',
- 'published' => 'Y',
- 'user_id' => 1
- ))),
- array('atomic' => false)
- );
- $this->assertFalse(empty($result));
- $result = $TestModel->findById(2);
- $expected = array(
- 'First Comment for Second Article',
- 'Second Comment for Second Article',
- 'First new comment',
- 'Second new comment',
- 'Third new comment'
- );
- $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
- $TestModel->beforeSaveReturn = false;
- $result = $TestModel->saveAssociated(
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'comment' => 'Fourth new comment',
- 'published' => 'Y',
- 'user_id' => 1
- ))),
- array('atomic' => false)
- );
- $this->assertEquals(array('Article' => false), $result);
- $result = $TestModel->findById(2);
- $expected = array(
- 'First Comment for Second Article',
- 'Second Comment for Second Article',
- 'First new comment',
- 'Second new comment',
- 'Third new comment'
- );
- $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
- }
- /**
- * testSaveAssociatedHasManyEmpty method
- *
- * @return void
- */
- public function testSaveAssociatedHasManyEmpty() {
- $this->loadFixtures('Article', 'Comment');
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
- $TestModel->validate = $TestModel->Comment->validate = array('user_id' => array('notEmpty' => array('rule' => 'notEmpty', 'required' => true)));
- //empty hasMany data is ignored in save
- $result = $TestModel->saveAssociated(array(
- 'Article' => array('title' => 'title', 'user_id' => 1),
- 'Comment' => array()
- ), array('validate' => true));
- $this->assertTrue($result);
- $result = $TestModel->saveAssociated(array(
- 'Article' => array('title' => 'title', 'user_id' => 1),
- 'Comment' => array()
- ), array('validate' => true, 'atomic' => false));
- $this->assertEquals(array('Article' => true), $result);
- //empty primary data is not ignored
- $result = $TestModel->saveAssociated(array('Article' => array()), array('validate' => true));
- $this->assertFalse($result);
- $result = $TestModel->saveAssociated(array('Article' => array()), array('validate' => true, 'atomic' => false));
- $this->assertEquals(array('Article' => false), $result);
- }
- /**
- * testSaveAssociatedHasManyValidation method
- *
- * @return void
- */
- public function testSaveAssociatedHasManyValidation() {
- $this->loadFixtures('Article', 'Comment');
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
- $TestModel->Comment->validate = array('comment' => 'notEmpty');
- $result = $TestModel->saveAssociated(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => '', 'published' => 'Y', 'user_id' => 1),
- )
- ), array('validate' => true));
- $this->assertFalse($result);
- $expected = array('Comment' => array(
- array('comment' => array('This field cannot be left blank'))
- ));
- $this->assertEquals($expected, $TestModel->validationErrors);
- $expected = array(
- array('comment' => array('This field cannot be left blank'))
- );
- $this->assertEquals($expected, $TestModel->Comment->validationErrors);
- $result = $TestModel->saveAssociated(array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'comment' => '',
- 'published' => 'Y',
- 'user_id' => 1
- ))
- ), array('validate' => 'first'));
- $this->assertFalse($result);
- }
- /**
- * test saveMany with transactions and ensure there is no missing rollback.
- *
- * @return void
- */
- public function testSaveManyTransactionNoRollback() {
- $this->loadFixtures('Post');
- $this->getMock('DboSource', array('connect', 'rollback', 'describe'), array(), 'MockManyTransactionDboSource');
- $db = ConnectionManager::create('mock_many_transaction', array(
- 'datasource' => 'MockManyTransactionDboSource',
- ));
- $db->expects($this->once())
- ->method('describe')
- ->will($this->returnValue(array()));
- $db->expects($this->once())->method('rollback');
- $Post = new Post('mock_many_transaction');
- $Post->validate = array(
- 'title' => array('rule' => array('notEmpty'))
- );
- $data = array(
- array('author_id' => 1, 'title' => 'New Fourth Post'),
- array('author_id' => 1, 'title' => '')
- );
- $Post->saveMany($data);
- }
- /**
- * test saveAssociated with transactions and ensure there is no missing rollback.
- *
- * @return void
- */
- public function testSaveAssociatedTransactionNoRollback() {
- $testDb = ConnectionManager::getDataSource('test');
- $this->getMock(
- 'DboSource',
- array('connect', 'rollback', 'describe', 'create', 'begin'),
- array(),
- 'MockAssociatedTransactionDboSource',
- false
- );
- $db = ConnectionManager::create('mock_assoc_transaction', array(
- 'datasource' => 'MockAssociatedTransactionDboSource',
- ));
- $this->mockObjects[] = $db;
- $db->columns = $testDb->columns;
- $db->expects($this->once())->method('rollback');
- $db->expects($this->any())->method('describe')
- ->will($this->returnValue(array(
- 'id' => array('type' => 'integer', 'length' => 11),
- 'title' => array('type' => 'string'),
- 'body' => array('type' => 'text'),
- 'published' => array('type' => 'string')
- )));
- $Post = new Post();
- $Post->useDbConfig = 'mock_assoc_transaction';
- $Post->Author->useDbConfig = 'mock_assoc_transaction';
- $Post->Author->validate = array(
- 'user' => array('rule' => array('notEmpty'))
- );
- $data = array(
- 'Post' => array(
- 'title' => 'New post',
- 'body' => 'Content',
- 'published' => 'Y'
- ),
- 'Author' => array(
- 'user' => '',
- 'password' => "sekret"
- )
- );
- $Post->saveAssociated($data, array('validate' => true, 'atomic' => true));
- }
- /**
- * test saveMany with nested saveMany call.
- *
- * @return void
- */
- public function testSaveManyNestedSaveMany() {
- $this->loadFixtures('Sample');
- $TransactionManyTestModel = new TransactionManyTestModel();
- $data = array(
- array('apple_id' => 1, 'name' => 'sample5'),
- );
- $this->assertTrue($TransactionManyTestModel->saveMany($data, array('atomic' => true)));
- }
- /**
- * testSaveManyTransaction method
- *
- * @return void
- */
- public function testSaveManyTransaction() {
- $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
- $TestModel = new Post();
- $TestModel->validate = array('title' => 'notEmpty');
- $data = array(
- array('author_id' => 1, 'title' => 'New Fourth Post'),
- array('author_id' => 1, 'title' => 'New Fifth Post'),
- array('author_id' => 1, 'title' => '')
- );
- $this->assertFalse($TestModel->saveMany($data));
- $result = $TestModel->find('all', array('recursive' => -1));
- $expected = array(
- array('Post' => array(
- 'id' => '1',
- 'author_id' => 1,
- 'title' => 'First Post',
- 'body' => 'First Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:39:23',
- 'updated' => '2007-03-18 10:41:31'
- )),
- array('Post' => array(
- 'id' => '2',
- 'author_id' => 3,
- 'title' => 'Second Post',
- 'body' => 'Second Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:41:23',
- 'updated' => '2007-03-18 10:43:31'
- )),
- array('Post' => array(
- 'id' => '3',
- 'author_id' => 1,
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:43:23',
- 'updated' => '2007-03-18 10:45:31'
- )));
- if (count($result) != 3) {
- // Database doesn't support transactions
- $expected[] = array(
- 'Post' => array(
- 'id' => '4',
- 'author_id' => 1,
- 'title' => 'New Fourth Post',
- 'body' => null,
- 'published' => 'N'
- ));
- $expected[] = array(
- 'Post' => array(
- 'id' => '5',
- 'author_id' => 1,
- 'title' => 'New Fifth Post',
- 'body' => null,
- 'published' => 'N',
- ));
- $this->assertEquals(self::date(), $result[3]['Post']['created']);
- $this->assertEquals(self::date(), $result[3]['Post']['updated']);
- $this->assertEquals(self::date(), $result[4]['Post']['created']);
- $this->assertEquals(self::date(), $result[4]['Post']['updated']);
- unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
- unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
- $this->assertEquals($expected, $result);
- // Skip the rest of the transactional tests
- return;
- }
- $this->assertEquals($expected, $result);
- $data = array(
- array('author_id' => 1, 'title' => 'New Fourth Post'),
- array('author_id' => 1, 'title' => ''),
- array('author_id' => 1, 'title' => 'New Sixth Post')
- );
- $this->assertFalse($TestModel->saveMany($data));
- $result = $TestModel->find('all', array('recursive' => -1));
- $expected = array(
- array('Post' => array(
- 'id' => '1',
- 'author_id' => 1,
- 'title' => 'First Post',
- 'body' => 'First Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:39:23',
- 'updated' => '2007-03-18 10:41:31'
- )),
- array('Post' => array(
- 'id' => '2',
- 'author_id' => 3,
- 'title' => 'Second Post',
- 'body' => 'Second Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:41:23',
- 'updated' => '2007-03-18 10:43:31'
- )),
- array('Post' => array(
- 'id' => '3',
- 'author_id' => 1,
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:43:23',
- 'updated' => '2007-03-18 10:45:31'
- )));
- if (count($result) != 3) {
- // Database doesn't support transactions
- $expected[] = array(
- 'Post' => array(
- 'id' => '4',
- 'author_id' => 1,
- 'title' => 'New Fourth Post',
- 'body' => 'Third Post Body',
- 'published' => 'N'
- ));
- $expected[] = array(
- 'Post' => array(
- 'id' => '5',
- 'author_id' => 1,
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'N'
- ));
- $this->assertEquals(self::date(), $result[3]['Post']['created']);
- $this->assertEquals(self::date(), $result[3]['Post']['updated']);
- $this->assertEquals(self::date(), $result[4]['Post']['created']);
- $this->assertEquals(self::date(), $result[4]['Post']['updated']);
- unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
- unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
- }
- $this->assertEquals($expected, $result);
- $TestModel->validate = array('title' => 'notEmpty');
- $data = array(
- array('author_id' => 1, 'title' => 'New Fourth Post'),
- array('author_id' => 1, 'title' => 'New Fifth Post'),
- array('author_id' => 1, 'title' => 'New Sixth Post')
- );
- $this->assertTrue($TestModel->saveMany($data));
- $result = $TestModel->find('all', array(
- 'recursive' => -1,
- 'fields' => array('author_id', 'title','body','published'),
- 'order' => array('Post.created' => 'ASC')
- ));
- $expected = array(
- array('Post' => array(
- 'author_id' => 1,
- 'title' => 'First Post',
- 'body' => 'First Post Body',
- 'published' => 'Y'
- )),
- array('Post' => array(
- 'author_id' => 3,
- 'title' => 'Second Post',
- 'body' => 'Second Post Body',
- 'published' => 'Y'
- )),
- array('Post' => array(
- 'author_id' => 1,
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'Y'
- )),
- array('Post' => array(
- 'author_id' => 1,
- 'title' => 'New Fourth Post',
- 'body' => '',
- 'published' => 'N'
- )),
- array('Post' => array(
- 'author_id' => 1,
- 'title' => 'New Fifth Post',
- 'body' => '',
- 'published' => 'N'
- )),
- array('Post' => array(
- 'author_id' => 1,
- 'title' => 'New Sixth Post',
- 'body' => '',
- 'published' => 'N'
- )));
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveManyValidation method
- *
- * @return void
- */
- public function testSaveManyValidation() {
- $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
- $TestModel = new Post();
- $data = array(
- array(
- 'id' => '1',
- 'title' => 'Baleeted First Post',
- 'body' => 'Baleeted!',
- 'published' => 'N'
- ),
- array(
- 'id' => '2',
- 'title' => 'Just update the title'
- ),
- array(
- 'title' => 'Creating a fourth post',
- 'body' => 'Fourth post body',
- 'author_id' => 2
- ));
- $this->assertTrue($TestModel->saveMany($data));
- $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
- $expected = array(
- array(
- 'Post' => array(
- 'id' => '1',
- 'author_id' => '1',
- 'title' => 'Baleeted First Post',
- 'body' => 'Baleeted!',
- 'published' => 'N',
- 'created' => '2007-03-18 10:39:23'
- )
- ),
- array(
- 'Post' => array(
- 'id' => '2',
- 'author_id' => '3',
- 'title' => 'Just update the title',
- 'body' => 'Second Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:41:23'
- )
- ),
- array(
- 'Post' => array(
- 'id' => '3',
- 'author_id' => '1',
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:43:23',
- 'updated' => '2007-03-18 10:45:31'
- )),
- array(
- 'Post' => array(
- 'id' => '4',
- 'author_id' => '2',
- 'title' => 'Creating a fourth post',
- 'body' => 'Fourth post body',
- 'published' => 'N'
- )
- )
- );
- $this->assertEquals(self::date(), $result[0]['Post']['updated']);
- $this->assertEquals(self::date(), $result[1]['Post']['updated']);
- $this->assertEquals(self::date(), $result[3]['Post']['created']);
- $this->assertEquals(self::date(), $result[3]['Post']['updated']);
- unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
- unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
- $this->assertEquals($expected, $result);
- $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
- $data = array(
- array(
- 'id' => '1',
- 'title' => 'Un-Baleeted First Post',
- 'body' => 'Not Baleeted!',
- 'published' => 'Y'
- ),
- array(
- 'id' => '2',
- 'title' => '',
- 'body' => 'Trying to get away with an empty title'
- ));
- $result = $TestModel->saveMany($data);
- $this->assertFalse($result);
- $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
- $errors = array(1 => array('title' => array('This field cannot be left blank')));
- $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
- if (!$transactionWorked) {
- $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
- $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
- }
- $this->assertEquals($errors, $TestModel->validationErrors);
- $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
- $data = array(
- array(
- 'id' => '1',
- 'title' => 'Un-Baleeted First Post',
- 'body' => 'Not Baleeted!',
- 'published' => 'Y'
- ),
- array(
- 'id' => '2',
- 'title' => '',
- 'body' => 'Trying to get away with an empty title'
- ));
- $result = $TestModel->saveMany($data, array('validate' => true, 'atomic' => false));
- $this->assertEquals(array(true, false), $result);
- $result = $TestModel->find('all', array(
- 'fields' => array('id', 'author_id', 'title', 'body', 'published'),
- 'recursive' => -1,
- 'order' => 'Post.id ASC'
- ));
- $errors = array(1 => array('title' => array('This field cannot be left blank')));
- $expected = array(
- array(
- 'Post' => array(
- 'id' => '1',
- 'author_id' => '1',
- 'title' => 'Un-Baleeted First Post',
- 'body' => 'Not Baleeted!',
- 'published' => 'Y',
- )),
- array(
- 'Post' => array(
- 'id' => '2',
- 'author_id' => '3',
- 'title' => 'Just update the title',
- 'body' => 'Second Post Body',
- 'published' => 'Y',
- )),
- array(
- 'Post' => array(
- 'id' => '3',
- 'author_id' => '1',
- 'title' => 'Third Post',
- 'body' => 'Third Post Body',
- 'published' => 'Y',
- )),
- array(
- 'Post' => array(
- 'id' => '4',
- 'author_id' => '2',
- 'title' => 'Creating a fourth post',
- 'body' => 'Fourth post body',
- 'published' => 'N',
- )));
- $this->assertEquals($expected, $result);
- $this->assertEquals($errors, $TestModel->validationErrors);
- $data = array(
- array(
- 'id' => '1',
- 'title' => 'Re-Baleeted First Post',
- 'body' => 'Baleeted!',
- 'published' => 'N'
- ),
- array(
- 'id' => '2',
- 'title' => '',
- 'body' => 'Trying to get away with an empty title'
- ));
- $this->assertFalse($TestModel->saveMany($data, array('validate' => 'first')));
- $result = $TestModel->find('all', array(
- 'fields' => array('id', 'author_id', 'title', 'body', 'published'),
- 'recursive' => -1,
- 'order' => 'Post.id ASC'
- ));
- $this->assertEquals($expected, $result);
- $this->assertEquals($errors, $TestModel->validationErrors);
- }
- /**
- * testValidateMany method
- *
- * @return void
- */
- public function testValidateMany() {
- $TestModel = new Article();
- $TestModel->validate = array('title' => 'notEmpty');
- $data = array(
- 0 => array('title' => ''),
- 1 => array('title' => 'title 1'),
- 2 => array('title' => 'title 2'),
- );
- $result = $TestModel->validateMany($data);
- $this->assertFalse($result);
- $expected = array(
- 0 => array('title' => array('This field cannot be left blank')),
- );
- $this->assertEquals($expected, $TestModel->validationErrors);
- $data = array(
- 0 => array('title' => 'title 0'),
- 1 => array('title' => ''),
- 2 => array('title' => 'title 2'),
- );
- $result = $TestModel->validateMany($data);
- $this->assertFalse($result);
- $expected = array(
- 1 => array('title' => array('This field cannot be left blank')),
- );
- $this->assertEquals($expected, $TestModel->validationErrors);
- }
- /**
- * testSaveAssociatedValidateFirst method
- *
- * @return void
- */
- public function testSaveAssociatedValidateFirst() {
- $this->loadFixtures('Article', 'Comment', 'Attachment');
- $model = new Article();
- $model->deleteAll(true);
- $model->Comment->validate = array('comment' => 'notEmpty');
- $result = $model->saveAssociated(array(
- 'Article' => array(
- 'title' => 'Post with Author',
- 'body' => 'This post will be saved author'
- ),
- 'Comment' => array(
- array('comment' => 'First new comment'),
- array('comment' => '')
- )
- ), array('validate' => 'first'));
- $this->assertFalse($result);
- $result = $model->find('all');
- $this->assertSame(array(), $result);
- $expected = array('Comment' => array(
- 1 => array('comment' => array('This field cannot be left blank'))
- ));
- $this->assertEquals($expected['Comment'], $model->Comment->validationErrors);
- $this->assertSame($model->Comment->find('count'), 0);
- $result = $model->saveAssociated(
- array(
- 'Article' => array(
- 'title' => 'Post with Author',
- 'body' => 'This post will be saved with an author',
- 'user_id' => 2
- ),
- 'Comment' => array(
- array(
- 'comment' => 'Only new comment',
- 'user_id' => 2
- ))),
- array('validate' => 'first')
- );
- $this->assertSame($result, true);
- $result = $model->Comment->find('all');
- $this->assertSame(count($result), 1);
- $result = Hash::extract($result, '{n}.Comment.article_id');
- $this->assertEquals(4, $result[0]);
- $model->deleteAll(true);
- $data = array(
- 'Article' => array(
- 'title' => 'Post with Author saveAlled from comment',
- 'body' => 'This post will be saved with an author',
- 'user_id' => 2
- ),
- 'Comment' => array(
- 'comment' => 'Only new comment', 'user_id' => 2
- ));
- $result = $model->Comment->saveAssociated($data, array('validate' => 'first'));
- $this->assertFalse(empty($result));
- $result = $model->find('all');
- $this->assertEquals(
- 'Post with Author saveAlled from comment',
- $result[0]['Article']['title']
- );
- $this->assertEquals('Only new comment', $result[0]['Comment'][0]['comment']);
- }
- /**
- * test saveMany()'s return is correct when using atomic = false and validate = first.
- *
- * @return void
- */
- public function testSaveManyValidateFirstAtomicFalse() {
- $Something = new Something();
- $invalidData = array(
- array(
- 'title' => 'foo',
- 'body' => 'bar',
- 'published' => 'baz',
- ),
- array(
- 'body' => 3,
- 'published' => 'sd',
- ),
- );
- $Something->create();
- $Something->validate = array(
- 'title' => array(
- 'rule' => 'alphaNumeric',
- 'required' => true,
- ),
- 'body' => array(
- 'rule' => 'alphaNumeric',
- 'required' => true,
- 'allowEmpty' => true,
- ),
- );
- $result = $Something->saveMany($invalidData, array(
- 'atomic' => false,
- 'validate' => 'first',
- ));
- $expected = array(true, false);
- $this->assertEquals($expected, $result);
- $Something = new Something();
- $validData = array(
- array(
- 'title' => 'title value',
- 'body' => 'body value',
- 'published' => 'baz',
- ),
- array(
- 'title' => 'valid',
- 'body' => 'this body',
- 'published' => 'sd',
- ),
- );
- $Something->create();
- $result = $Something->saveMany($validData, array(
- 'atomic' => false,
- 'validate' => 'first',
- ));
- $expected = array(true, true);
- $this->assertEquals($expected, $result);
- }
- /**
- * testValidateAssociated method
- *
- * @return void
- */
- public function testValidateAssociated() {
- $this->loadFixtures('Attachment', 'Article', 'Comment');
- $TestModel = new Comment();
- $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
- $data = array(
- 'Comment' => array(
- 'comment' => 'This is the comment'
- ),
- 'Attachment' => array(
- 'attachment' => ''
- )
- );
- $result = $TestModel->validateAssociated($data);
- $this->assertFalse($result);
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
- $TestModel->Comment->validate = array('comment' => 'notEmpty');
- $data = array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'id' => 1,
- 'comment' => '',
- 'published' => 'Y',
- 'user_id' => 1),
- array(
- 'id' => 2,
- 'comment' =>
- 'comment',
- 'published' => 'Y',
- 'user_id' => 1
- )));
- $result = $TestModel->validateAssociated($data);
- $this->assertFalse($result);
- $data = array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'id' => 1,
- 'comment' => '',
- 'published' => 'Y',
- 'user_id' => 1
- ),
- array(
- 'id' => 2,
- 'comment' => 'comment',
- 'published' => 'Y',
- 'user_id' => 1
- ),
- array(
- 'id' => 3,
- 'comment' => '',
- 'published' => 'Y',
- 'user_id' => 1
- )));
- $result = $TestModel->validateAssociated($data, array(
- 'atomic' => false
- ));
- $expected = array(
- 'Article' => true,
- 'Comment' => array(false, true, false)
- );
- $this->assertSame($expected, $result);
- $expected = array('Comment' => array(
- 0 => array('comment' => array('This field cannot be left blank')),
- 2 => array('comment' => array('This field cannot be left blank'))
- ));
- $this->assertEquals($expected, $TestModel->validationErrors);
- $expected = array(
- 0 => array('comment' => array('This field cannot be left blank')),
- 2 => array('comment' => array('This field cannot be left blank'))
- );
- $this->assertEquals($expected, $TestModel->Comment->validationErrors);
- }
- /**
- * test that saveMany behaves like plain save() when suplied empty data
- *
- * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
- * @return void
- */
- public function testSaveManyEmptyData() {
- $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
- $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
- $model = new Article();
- $result = $model->saveMany(array(), array('validate' => true));
- $this->assertFalse(empty($result));
- $model = new ProductUpdateAll();
- $result = $model->saveMany(array());
- $this->assertFalse($result);
- }
- /**
- * test that saveAssociated behaves like plain save() when supplied empty data
- *
- * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
- * @return void
- */
- public function testSaveAssociatedEmptyData() {
- $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
- $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
- $model = new Article();
- $result = $model->saveAssociated(array(), array('validate' => true));
- $this->assertFalse(empty($result));
- $model = new ProductUpdateAll();
- $result = $model->saveAssociated(array());
- $this->assertFalse($result);
- }
- /**
- * testUpdateWithCalculation method
- *
- * @return void
- */
- public function testUpdateWithCalculation() {
- $this->loadFixtures('DataTest');
- $model = new DataTest();
- $model->deleteAll(true);
- $result = $model->saveMany(array(
- array('count' => 5, 'float' => 1.1),
- array('count' => 3, 'float' => 1.2),
- array('count' => 4, 'float' => 1.3),
- array('count' => 1, 'float' => 2.0),
- ));
- $this->assertFalse(empty($result));
- $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
- $this->assertEquals(array(5, 3, 4, 1), $result);
- $this->assertTrue($model->updateAll(array('count' => 'count + 2')));
- $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
- $this->assertEquals(array(7, 5, 6, 3), $result);
- $this->assertTrue($model->updateAll(array('DataTest.count' => 'DataTest.count - 1')));
- $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
- $this->assertEquals(array(6, 4, 5, 2), $result);
- }
- public function testToggleBoolFields() {
- $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
- $Post = new CounterCachePost();
- $Post->unbindModel(array('belongsTo' => array('User')), true);
- $true = array('Post' => array('published' => true, 'id' => 2));
- $false = array('Post' => array('published' => false, 'id' => 2));
- $fields = array('Post.published', 'Post.id');
- $updateConditions = array('Post.id' => 2);
- // check its true
- $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
- $this->assertEquals($true, $result);
- // Testing without the alias
- $this->assertTrue($Post->updateAll(array('published' => 'NOT published'), $updateConditions));
- $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
- $this->assertEquals($false, $result);
- $this->assertTrue($Post->updateAll(array('published' => 'NOT published'), $updateConditions));
- $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
- $this->assertEquals($true, $result);
- $db = ConnectionManager::getDataSource('test');
- $alias = $db->name('Post.published');
- // Testing with the alias
- $this->assertTrue($Post->updateAll(array('Post.published' => "NOT $alias"), $updateConditions));
- $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
- $this->assertEquals($false, $result);
- $this->assertTrue($Post->updateAll(array('Post.published' => "NOT $alias"), $updateConditions));
- $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
- $this->assertEquals($true, $result);
- }
- /**
- * TestFindAllWithoutForeignKey
- *
- * @return void
- */
- public function testFindAllForeignKey() {
- $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
- $ProductUpdateAll = new ProductUpdateAll();
- $conditions = array('Group.name' => 'group one');
- $ProductUpdateAll->bindModel(array(
- 'belongsTo' => array(
- 'Group' => array('className' => 'GroupUpdateAll')
- )
- ));
- $ProductUpdateAll->belongsTo = array(
- 'Group' => array('className' => 'GroupUpdateAll', 'foreignKey' => 'group_id')
- );
- $results = $ProductUpdateAll->find('all', compact('conditions'));
- $this->assertTrue(!empty($results));
- $ProductUpdateAll->bindModel(array('belongsTo' => array('Group')));
- $ProductUpdateAll->belongsTo = array(
- 'Group' => array(
- 'className' => 'GroupUpdateAll',
- 'foreignKey' => false,
- 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
- ));
- $resultsFkFalse = $ProductUpdateAll->find('all', compact('conditions'));
- $this->assertTrue(!empty($resultsFkFalse));
- $expected = array(
- '0' => array(
- 'ProductUpdateAll' => array(
- 'id' => 1,
- 'name' => 'product one',
- 'groupcode' => 120,
- 'group_id' => 1),
- 'Group' => array(
- 'id' => 1,
- 'name' => 'group one',
- 'code' => 120)
- ),
- '1' => array(
- 'ProductUpdateAll' => array(
- 'id' => 2,
- 'name' => 'product two',
- 'groupcode' => 120,
- 'group_id' => 1),
- 'Group' => array(
- 'id' => 1,
- 'name' => 'group one',
- 'code' => 120)
- )
- );
- $this->assertEquals($expected, $results);
- $this->assertEquals($expected, $resultsFkFalse);
- }
- /**
- * test updateAll with empty values.
- *
- * @return void
- */
- public function testUpdateAllEmptyValues() {
- $this->skipIf($this->db instanceof Sqlserver || $this->db instanceof Postgres, 'This test is not compatible with Postgres or SQL Server.');
- $this->loadFixtures('Author', 'Post');
- $model = new Author();
- $result = $model->updateAll(array('user' => '""'));
- $this->assertTrue($result);
- }
- /**
- * testUpdateAllWithJoins
- *
- * @return void
- */
- public function testUpdateAllWithJoins() {
- $this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql or sqlite');
- $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
- $ProductUpdateAll = new ProductUpdateAll();
- $conditions = array('Group.name' => 'group one');
- $ProductUpdateAll->bindModel(array('belongsTo' => array(
- 'Group' => array('className' => 'GroupUpdateAll')))
- );
- $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
- $results = $ProductUpdateAll->find('all', array(
- 'conditions' => array('ProductUpdateAll.name' => 'new product')
- ));
- $expected = array(
- '0' => array(
- 'ProductUpdateAll' => array(
- 'id' => 1,
- 'name' => 'new product',
- 'groupcode' => 120,
- 'group_id' => 1),
- 'Group' => array(
- 'id' => 1,
- 'name' => 'group one',
- 'code' => 120)
- ),
- '1' => array(
- 'ProductUpdateAll' => array(
- 'id' => 2,
- 'name' => 'new product',
- 'groupcode' => 120,
- 'group_id' => 1),
- 'Group' => array(
- 'id' => 1,
- 'name' => 'group one',
- 'code' => 120)));
- $this->assertEquals($expected, $results);
- }
- /**
- * testUpdateAllWithoutForeignKey
- *
- * @return void
- */
- public function testUpdateAllWithoutForeignKey() {
- $this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql');
- $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
- $ProductUpdateAll = new ProductUpdateAll();
- $conditions = array('Group.name' => 'group one');
- $ProductUpdateAll->bindModel(array('belongsTo' => array(
- 'Group' => array('className' => 'GroupUpdateAll')
- )));
- $ProductUpdateAll->belongsTo = array(
- 'Group' => array(
- 'className' => 'GroupUpdateAll',
- 'foreignKey' => false,
- 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
- )
- );
- $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
- $resultsFkFalse = $ProductUpdateAll->find('all', array('conditions' => array('ProductUpdateAll.name' => 'new product')));
- $expected = array(
- '0' => array(
- 'ProductUpdateAll' => array(
- 'id' => 1,
- 'name' => 'new product',
- 'groupcode' => 120,
- 'group_id' => 1),
- 'Group' => array(
- 'id' => 1,
- 'name' => 'group one',
- 'code' => 120)
- ),
- '1' => array(
- 'ProductUpdateAll' => array(
- 'id' => 2,
- 'name' => 'new product',
- 'groupcode' => 120,
- 'group_id' => 1),
- 'Group' => array(
- 'id' => 1,
- 'name' => 'group one',
- 'code' => 120)));
- $this->assertEquals($expected, $resultsFkFalse);
- }
- /**
- * test writing floats in german locale.
- *
- * @return void
- */
- public function testWriteFloatAsGerman() {
- $restore = setlocale(LC_NUMERIC, 0);
- setlocale(LC_NUMERIC, 'de_DE');
- $model = new DataTest();
- $result = $model->save(array(
- 'count' => 1,
- 'float' => 3.14593
- ));
- $this->assertTrue((bool)$result);
- setlocale(LC_NUMERIC, $restore);
- }
- /**
- * Test returned array contains primary key when save creates a new record
- *
- * @return void
- */
- public function testPkInReturnArrayForCreate() {
- $this->loadFixtures('Article');
- $TestModel = new Article();
- $data = array('Article' => array(
- 'user_id' => '1',
- 'title' => 'Fourth Article',
- 'body' => 'Fourth Article Body',
- 'published' => 'Y'
- ));
- $result = $TestModel->save($data);
- $this->assertSame($result['Article']['id'], $TestModel->id);
- }
- /**
- * testSaveAllFieldListValidateBelongsTo
- *
- * @return void
- */
- public function testSaveAllFieldListValidateBelongsTo() {
- $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
- $TestModel = new Post();
- $result = $TestModel->find('all');
- $this->assertEquals(3, count($result));
- $this->assertFalse(isset($result[3]));
- // test belongsTo
- $fieldList = array(
- 'Post' => array('title'),
- 'Author' => array('user')
- );
- $data = array(
- 'Post' => array(
- 'title' => 'Post without body',
- 'body' => 'This will not be saved',
- ),
- 'Author' => array(
- 'user' => 'bob',
- 'test' => 'This will not be saved',
- ));
- $TestModel->saveAll($data, array('fieldList' => $fieldList));
- $result = $TestModel->find('all');
- $expected = array(
- 'Post' => array (
- 'id' => '4',
- 'author_id' => '5',
- 'title' => 'Post without body',
- 'body' => null,
- 'published' => 'N',
- 'created' => self::date(),
- 'updated' => self::date(),
- ),
- 'Author' => array (
- 'id' => '5',
- 'user' => 'bob',
- 'password' => null,
- 'created' => self::date(),
- 'updated' => self::date(),
- 'test' => 'working',
- ),
- );
- $this->assertEquals($expected, $result[3]);
- $this->assertEquals(4, count($result));
- $this->assertEquals('', $result[3]['Post']['body']);
- $this->assertEquals('working', $result[3]['Author']['test']);
- // test multirecord
- $this->db->truncate($TestModel);
- $fieldList = array('title', 'author_id');
- $TestModel->saveAll(array(
- array(
- 'title' => 'Multi-record post 1',
- 'body' => 'First multi-record post',
- 'author_id' => 2
- ),
- array(
- 'title' => 'Multi-record post 2',
- 'body' => 'Second multi-record post',
- 'author_id' => 2
- )), array('fieldList' => $fieldList));
- $result = $TestModel->find('all', array(
- 'recursive' => -1,
- 'order' => 'Post.id ASC'
- ));
- $expected = array(
- array(
- 'Post' => array(
- 'id' => '1',
- 'author_id' => '2',
- 'title' => 'Multi-record post 1',
- 'body' => '',
- 'published' => 'N',
- 'created' => self::date(),
- 'updated' => self::date()
- )
- ),
- array(
- 'Post' => array(
- 'id' => '2',
- 'author_id' => '2',
- 'title' => 'Multi-record post 2',
- 'body' => '',
- 'published' => 'N',
- 'created' => self::date(),
- 'updated' => self::date()
- )
- )
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllFieldListHasMany method
- *
- * return @void
- */
- public function testSaveAllFieldListHasMany() {
- $this->loadFixtures('Article', 'Comment');
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
- $this->db->truncate($TestModel);
- $this->db->truncate(new Comment());
- $data = array(
- 'Article' => array('title' => 'I will not save'),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- );
- $fieldList = array(
- 'Article' => array('id'),
- 'Comment' => array('article_id', 'user_id')
- );
- $TestModel->saveAll($data, array('fieldList' => $fieldList));
- $result = $TestModel->find('all');
- $this->assertEquals('', $result[0]['Article']['title']);
- $this->assertEquals('', $result[0]['Comment'][0]['comment']);
- $this->assertEquals('', $result[0]['Comment'][1]['comment']);
- $fieldList = array(
- 'Article' => array('id'),
- 'Comment' => array('user_id')
- );
- $TestModel->saveAll($data, array('fieldList' => $fieldList));
- $result = $TestModel->find('all');
- $this->assertEquals('', $result[1]['Article']['title']);
- $this->assertEquals(2, count($result[1]['Comment']));
- $TestModel->whitelist = array('id');
- $TestModel->Comment->whitelist = array('user_id');
- $TestModel->saveAll($data);
- $result = $TestModel->find('all');
- $this->assertEquals('', $result[2]['Article']['title']);
- $this->assertEquals(2, count($result[2]['Comment']));
- }
- /**
- * testSaveAllFieldListHasOne method
- *
- * @return void
- */
- public function testSaveAllFieldListHasOne() {
- $this->loadFixtures('Attachment', 'Comment', 'Article', 'User');
- $TestModel = new Comment();
- $TestModel->validate = array('comment' => 'notEmpty');
- $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
- $record = array(
- 'Comment' => array(
- 'user_id' => 1,
- 'article_id' => 1,
- 'comment' => '',
- ),
- 'Attachment' => array(
- 'attachment' => ''
- )
- );
- $result = $TestModel->saveAll($record, array('validate' => 'only'));
- $this->assertFalse($result);
- $fieldList = array(
- 'Comment' => array('id', 'article_id', 'user_id'),
- 'Attachment' => array('comment_id')
- );
- $result = $TestModel->saveAll($record, array(
- 'fieldList' => $fieldList, 'validate' => 'only'
- ));
- $this->assertTrue($result);
- $this->assertEmpty($TestModel->validationErrors);
- }
- /**
- * testSaveAllFieldListHasOneAddFkToWhitelist method
- *
- * @return void
- */
- public function testSaveAllFieldListHasOneAddFkToWhitelist() {
- $this->loadFixtures('ArticleFeatured', 'Featured');
- $Article = new ArticleFeatured();
- $Article->belongsTo = $Article->hasMany = array();
- $Article->Featured->validate = array('end_date' => 'notEmpty');
- $record = array(
- 'ArticleFeatured' => array(
- 'user_id' => 1,
- 'title' => 'First Article',
- 'body' => '',
- 'published' => 'Y'
- ),
- 'Featured' => array(
- 'category_id' => 1,
- 'end_date' => ''
- )
- );
- $result = $Article->saveAll($record, array('validate' => 'only'));
- $this->assertFalse($result);
- $expected = array(
- 'body' => array(
- 'This field cannot be left blank'
- ),
- 'Featured' => array(
- 'end_date' => array(
- 'This field cannot be left blank'
- )
- )
- );
- $this->assertEquals($expected, $Article->validationErrors);
- $fieldList = array(
- 'ArticleFeatured' => array('user_id', 'title'),
- 'Featured' => array('category_id')
- );
- $result = $Article->saveAll($record, array(
- 'fieldList' => $fieldList, 'validate' => 'first'
- ));
- $this->assertTrue($result);
- $this->assertEmpty($Article->validationErrors);
- $Article->recursive = 0;
- $result = $Article->find('first', array('order' => array('ArticleFeatured.created' => 'DESC')));
- $this->assertSame($result['ArticleFeatured']['id'], $result['Featured']['article_featured_id']);
- }
- /**
- * testSaveAllDeepFieldListValidateBelongsTo
- *
- * @return void
- */
- public function testSaveAllDeepFieldListValidateBelongsTo() {
- $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
- $TestModel = new Post();
- $TestModel->Author->bindModel(array('hasMany' => array('Comment' => array('foreignKey' => 'user_id'))), false);
- $TestModel->recursive = 2;
- $result = $TestModel->find('all');
- $this->assertEquals(3, count($result));
- $this->assertFalse(isset($result[3]));
- // test belongsTo
- $fieldList = array(
- 'Post' => array('title', 'author_id'),
- 'Author' => array('user'),
- 'Comment' => array('comment')
- );
- $TestModel->saveAll(array(
- 'Post' => array(
- 'title' => 'Post without body',
- 'body' => 'This will not be saved',
- ),
- 'Author' => array(
- 'user' => 'bob',
- 'test' => 'This will not be saved',
- 'Comment' => array(
- array('id' => 5, 'comment' => 'I am still published', 'published' => 'N'))
- )), array('fieldList' => $fieldList, 'deep' => true));
- $result = $TestModel->Author->Comment->find('first', array(
- 'conditions' => array('Comment.id' => 5),
- 'fields' => array('comment', 'published')
- ));
- $expected = array(
- 'Comment' => array(
- 'comment' => 'I am still published',
- 'published' => 'Y'
- )
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllDeepFieldListHasMany method
- *
- * return @void
- */
- public function testSaveAllDeepFieldListHasMany() {
- $this->loadFixtures('Article', 'Comment', 'User');
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
- $this->db->truncate($TestModel);
- $this->db->truncate(new Comment());
- $fieldList = array(
- 'Article' => array('id'),
- 'Comment' => array('article_id', 'user_id'),
- 'User' => array('user')
- );
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2, 'title' => 'I will not save'),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
- array(
- 'comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2,
- 'User' => array('user' => 'nopassword', 'password' => 'not saved')
- )
- )
- ), array('fieldList' => $fieldList, 'deep' => true));
- $result = $TestModel->Comment->User->find('first', array(
- 'conditions' => array('User.user' => 'nopassword'),
- 'fields' => array('user', 'password')
- ));
- $expected = array(
- 'User' => array(
- 'user' => 'nopassword',
- 'password' => ''
- )
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllDeepHasManyBelongsTo method
- *
- * return @void
- */
- public function testSaveAllDeepHasManyBelongsTo() {
- $this->loadFixtures('Article', 'Comment', 'User');
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
- $this->db->truncate($TestModel);
- $this->db->truncate(new Comment());
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2, 'title' => 'The title'),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
- array(
- 'comment' => 'belongsto', 'published' => 'Y',
- 'User' => array('user' => 'findme', 'password' => 'somepass')
- )
- )
- ), array('deep' => true));
- $result = $TestModel->Comment->User->find('first', array(
- 'conditions' => array('User.user' => 'findme'),
- 'fields' => array('id', 'user', 'password')
- ));
- $expected = array(
- 'User' => array(
- 'id' => 5,
- 'user' => 'findme',
- 'password' => 'somepass',
- )
- );
- $this->assertEquals($expected, $result);
- $result = $TestModel->Comment->find('first', array(
- 'conditions' => array('Comment.user_id' => 5),
- 'fields' => array('id', 'comment', 'published', 'user_id')
- ));
- $expected = array(
- 'Comment' => array(
- 'id' => 2,
- 'comment' => 'belongsto',
- 'published' => 'Y',
- 'user_id' => 5
- )
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllDeepHasManyhasMany method
- *
- * return @void
- */
- public function testSaveAllDeepHasManyHasMany() {
- $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
- $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
- $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
- $this->db->truncate($TestModel);
- $this->db->truncate(new Comment());
- $this->db->truncate(new Attachment());
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2, 'title' => 'The title'),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
- array(
- 'comment' => 'hasmany', 'published' => 'Y', 'user_id' => 5,
- 'Attachment' => array(
- array('attachment' => 'first deep attachment'),
- array('attachment' => 'second deep attachment'),
- )
- )
- )
- ), array('deep' => true));
- $result = $TestModel->Comment->find('first', array(
- 'conditions' => array('Comment.comment' => 'hasmany'),
- 'fields' => array('id', 'comment', 'published', 'user_id'),
- 'recursive' => -1
- ));
- $expected = array(
- 'Comment' => array(
- 'id' => 2,
- 'comment' => 'hasmany',
- 'published' => 'Y',
- 'user_id' => 5
- )
- );
- $this->assertEquals($expected, $result);
- $result = $TestModel->Comment->Attachment->find('all', array(
- 'fields' => array('attachment', 'comment_id'),
- 'order' => array('Attachment.id' => 'ASC')
- ));
- $expected = array(
- array('Attachment' => array('attachment' => 'first deep attachment', 'comment_id' => 2)),
- array('Attachment' => array('attachment' => 'second deep attachment', 'comment_id' => 2)),
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllDeepOrderHasManyHasMany method
- *
- * return @void
- */
- public function testSaveAllDeepOrderHasManyHasMany() {
- $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
- $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
- $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
- $this->db->truncate($TestModel);
- $this->db->truncate(new Comment());
- $this->db->truncate(new Attachment());
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 2, 'title' => 'Comment has its data after Attachment'),
- 'Comment' => array(
- array(
- 'Attachment' => array(
- array('attachment' => 'attachment should be created with comment_id'),
- array('attachment' => 'comment should be created with article_id'),
- ),
- 'comment' => 'after associated data',
- 'user_id' => 1
- )
- )
- ), array('deep' => true));
- $result = $TestModel->Comment->find('first', array(
- 'conditions' => array('Comment.article_id' => 2),
- ));
- $this->assertEquals(2, $result['Comment']['article_id']);
- $this->assertEquals(2, count($result['Attachment']));
- }
- /**
- * testSaveAllDeepEmptyHasManyHasMany method
- *
- * return @void
- */
- public function testSaveAllDeepEmptyHasManyHasMany() {
- $this->skipIf(!$this->db instanceof Mysql, 'This test is only compatible with Mysql.');
- $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
- $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
- $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
- $this->db->truncate($TestModel);
- $this->db->truncate(new Comment());
- $this->db->truncate(new Attachment());
- $result = $TestModel->saveAll(array(
- 'Article' => array('id' => 3, 'user_id' => 1, 'title' => 'Comment has no data'),
- 'Comment' => array(
- array(
- 'user_id' => 1,
- 'Attachment' => array(
- array('attachment' => 'attachment should be created with comment_id'),
- array('attachment' => 'comment should be created with article_id'),
- ),
- )
- )
- ), array('deep' => true));
- $result = $TestModel->Comment->find('first', array(
- 'conditions' => array('Comment.article_id' => 3),
- ));
- $this->assertEquals(3, $result['Comment']['article_id']);
- $this->assertEquals(2, count($result['Attachment']));
- }
- /**
- * testUpdateAllBoolean
- *
- * return @void
- */
- public function testUpdateAllBoolean() {
- $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
- $TestModel = new Item();
- $result = $TestModel->updateAll(array('published' => true));
- $this->assertTrue($result);
- $result = $TestModel->find('first', array('fields' => array('id', 'published')));
- $this->assertEquals(true, $result['Item']['published']);
- }
- /**
- * testUpdateAllBooleanConditions
- *
- * return @void
- */
- public function testUpdateAllBooleanConditions() {
- $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
- $TestModel = new Item();
- $result = $TestModel->updateAll(array('published' => true), array('Item.id' => 1));
- $this->assertTrue($result);
- $result = $TestModel->find('first', array(
- 'fields' => array('id', 'published'),
- 'conditions' => array('Item.id' => 1)));
- $this->assertEquals(true, $result['Item']['published']);
- }
- /**
- * testUpdateBoolean
- *
- * return @void
- */
- public function testUpdateBoolean() {
- $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
- $TestModel = new Item();
- $result = $TestModel->save(array('published' => true, 'id' => 1));
- $this->assertTrue((boolean)$result);
- $result = $TestModel->find('first', array(
- 'fields' => array('id', 'published'),
- 'conditions' => array('Item.id' => 1)));
- $this->assertEquals(true, $result['Item']['published']);
- }
- }
|