ModelWriteTest.php 176 KB

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