ModelWriteTest.php 198 KB

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