ModelWriteTest.php 145 KB

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