ModelWriteTest.php 193 KB

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