ModelWriteTest.php 177 KB

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