ModelWriteTest.php 215 KB

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