ModelWriteTest.php 140 KB

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