ModelWriteTest.php 145 KB

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