ModelWriteTest.php 189 KB

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