ModelWriteTest.php 192 KB

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