ModelWriteTest.php 210 KB

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