ModelWriteTest.php 186 KB

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