ModelWriteTest.php 192 KB

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