ModelWriteTest.php 187 KB

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