ModelWriteTest.php 193 KB

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