MysqlTest.php 139 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576
  1. <?php
  2. /**
  3. * DboMysqlTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  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://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Test.Case.Model.Datasource.Database
  16. * @since CakePHP(tm) v 1.2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Model', 'Model');
  20. App::uses('AppModel', 'Model');
  21. App::uses('Mysql', 'Model/Datasource/Database');
  22. App::uses('CakeSchema', 'Model');
  23. require_once dirname(dirname(dirname(__FILE__))) . DS . 'models.php';
  24. /**
  25. * DboMysqlTest class
  26. *
  27. * @package Cake.Test.Case.Model.Datasource.Database
  28. */
  29. class MysqlTest extends CakeTestCase {
  30. /**
  31. * autoFixtures property
  32. *
  33. * @var bool false
  34. */
  35. public $autoFixtures = false;
  36. /**
  37. * fixtures property
  38. *
  39. * @var array
  40. */
  41. public $fixtures = array(
  42. 'core.apple', 'core.article', 'core.articles_tag', 'core.attachment', 'core.comment',
  43. 'core.sample', 'core.tag', 'core.user', 'core.post', 'core.author', 'core.data_test',
  44. 'core.binary_test'
  45. );
  46. /**
  47. * The Dbo instance to be tested
  48. *
  49. * @var DboSource
  50. */
  51. public $Dbo = null;
  52. /**
  53. * Sets up a Dbo class instance for testing
  54. *
  55. */
  56. public function setUp() {
  57. $this->Dbo = ConnectionManager::getDataSource('test');
  58. if (!($this->Dbo instanceof Mysql)) {
  59. $this->markTestSkipped('The MySQL extension is not available.');
  60. }
  61. $this->_debug = Configure::read('debug');
  62. Configure::write('debug', 1);
  63. $this->model = ClassRegistry::init('MysqlTestModel');
  64. }
  65. /**
  66. * Sets up a Dbo class instance for testing
  67. *
  68. */
  69. public function tearDown() {
  70. unset($this->model);
  71. ClassRegistry::flush();
  72. Configure::write('debug', $this->_debug);
  73. }
  74. /**
  75. * Test Dbo value method
  76. *
  77. * @group quoting
  78. */
  79. public function testQuoting() {
  80. $result = $this->Dbo->fields($this->model);
  81. $expected = array(
  82. '`MysqlTestModel`.`id`',
  83. '`MysqlTestModel`.`client_id`',
  84. '`MysqlTestModel`.`name`',
  85. '`MysqlTestModel`.`login`',
  86. '`MysqlTestModel`.`passwd`',
  87. '`MysqlTestModel`.`addr_1`',
  88. '`MysqlTestModel`.`addr_2`',
  89. '`MysqlTestModel`.`zip_code`',
  90. '`MysqlTestModel`.`city`',
  91. '`MysqlTestModel`.`country`',
  92. '`MysqlTestModel`.`phone`',
  93. '`MysqlTestModel`.`fax`',
  94. '`MysqlTestModel`.`url`',
  95. '`MysqlTestModel`.`email`',
  96. '`MysqlTestModel`.`comments`',
  97. '`MysqlTestModel`.`last_login`',
  98. '`MysqlTestModel`.`created`',
  99. '`MysqlTestModel`.`updated`'
  100. );
  101. $this->assertEquals($expected, $result);
  102. $expected = 1.2;
  103. $result = $this->Dbo->value(1.2, 'float');
  104. $this->assertEquals($expected, $result);
  105. $expected = "'1,2'";
  106. $result = $this->Dbo->value('1,2', 'float');
  107. $this->assertEquals($expected, $result);
  108. $expected = "'4713e29446'";
  109. $result = $this->Dbo->value('4713e29446');
  110. $this->assertEquals($expected, $result);
  111. $expected = 'NULL';
  112. $result = $this->Dbo->value('', 'integer');
  113. $this->assertEquals($expected, $result);
  114. $expected = "'0'";
  115. $result = $this->Dbo->value('', 'boolean');
  116. $this->assertEquals($expected, $result);
  117. $expected = 10010001;
  118. $result = $this->Dbo->value(10010001);
  119. $this->assertEquals($expected, $result);
  120. $expected = "'00010010001'";
  121. $result = $this->Dbo->value('00010010001');
  122. $this->assertEquals($expected, $result);
  123. }
  124. /**
  125. * test that localized floats don't cause trouble.
  126. *
  127. * @group quoting
  128. * @return void
  129. */
  130. public function testLocalizedFloats() {
  131. $this->skipIf(DS === '\\', 'The locale is not supported in Windows and affect the others tests.');
  132. $restore = setlocale(LC_ALL, null);
  133. setlocale(LC_ALL, 'de_DE');
  134. $result = $this->Dbo->value(3.141593);
  135. $this->assertEquals('3.141593', $result);
  136. $result = $this->db->value(3.141593, 'float');
  137. $this->assertEquals('3.141593', $result);
  138. $result = $this->db->value(1234567.11, 'float');
  139. $this->assertEquals('1234567.11', $result);
  140. $result = $this->db->value(123456.45464748, 'float');
  141. $this->assertContains('123456.454647', $result);
  142. $result = $this->db->value(0.987654321, 'float');
  143. $this->assertEquals('0.987654321', (string)$result);
  144. $result = $this->db->value(2.2E-54, 'float');
  145. $this->assertEquals('2.2E-54', (string)$result);
  146. $result = $this->db->value(2.2E-54);
  147. $this->assertEquals('2.2E-54', (string)$result);
  148. setlocale(LC_ALL, $restore);
  149. }
  150. /**
  151. * test that scientific notations are working correctly
  152. *
  153. * @return void
  154. */
  155. function testScientificNotation() {
  156. $result = $this->db->value(2.2E-54, 'float');
  157. $this->assertEquals('2.2E-54', (string)$result);
  158. $result = $this->db->value(2.2E-54);
  159. $this->assertEquals('2.2E-54', (string)$result);
  160. }
  161. /**
  162. * testTinyintCasting method
  163. *
  164. *
  165. * @return void
  166. */
  167. public function testTinyintCasting() {
  168. $this->Dbo->cacheSources = false;
  169. $tableName = 'tinyint_' . uniqid();
  170. $this->Dbo->rawQuery('CREATE TABLE ' . $this->Dbo->fullTableName($tableName) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));');
  171. $this->model = new CakeTestModel(array(
  172. 'name' => 'Tinyint', 'table' => $tableName, 'ds' => 'test'
  173. ));
  174. $result = $this->model->schema();
  175. $this->assertEquals($result['bool']['type'], 'boolean');
  176. $this->assertEquals($result['small_int']['type'], 'integer');
  177. $this->assertTrue((bool)$this->model->save(array('bool' => 5, 'small_int' => 5)));
  178. $result = $this->model->find('first');
  179. $this->assertSame($result['Tinyint']['bool'], true);
  180. $this->assertSame($result['Tinyint']['small_int'], '5');
  181. $this->model->deleteAll(true);
  182. $this->assertTrue((bool)$this->model->save(array('bool' => 0, 'small_int' => 100)));
  183. $result = $this->model->find('first');
  184. $this->assertSame($result['Tinyint']['bool'], false);
  185. $this->assertSame($result['Tinyint']['small_int'], '100');
  186. $this->model->deleteAll(true);
  187. $this->assertTrue((bool)$this->model->save(array('bool' => true, 'small_int' => 0)));
  188. $result = $this->model->find('first');
  189. $this->assertSame($result['Tinyint']['bool'], true);
  190. $this->assertSame($result['Tinyint']['small_int'], '0');
  191. $this->model->deleteAll(true);
  192. $this->Dbo->rawQuery('DROP TABLE ' . $this->Dbo->fullTableName($tableName));
  193. }
  194. /**
  195. * testLastAffected method
  196. *
  197. *
  198. * @return void
  199. */
  200. public function testLastAffected() {
  201. $this->Dbo->cacheSources = false;
  202. $tableName = 'tinyint_' . uniqid();
  203. $this->Dbo->rawQuery('CREATE TABLE ' . $this->Dbo->fullTableName($tableName) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));');
  204. $this->model = new CakeTestModel(array(
  205. 'name' => 'Tinyint', 'table' => $tableName, 'ds' => 'test'
  206. ));
  207. $this->assertTrue((bool)$this->model->save(array('bool' => 5, 'small_int' => 5)));
  208. $this->assertEquals(1, $this->model->find('count'));
  209. $this->model->deleteAll(true);
  210. $result = $this->Dbo->lastAffected();
  211. $this->assertEquals(1, $result);
  212. $this->assertEquals(0, $this->model->find('count'));
  213. $this->Dbo->rawQuery('DROP TABLE ' . $this->Dbo->fullTableName($tableName));
  214. }
  215. /**
  216. * testIndexDetection method
  217. *
  218. * @group indices
  219. * @return void
  220. */
  221. public function testIndexDetection() {
  222. $this->Dbo->cacheSources = false;
  223. $name = $this->Dbo->fullTableName('simple');
  224. $this->Dbo->rawQuery('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));');
  225. $expected = array('PRIMARY' => array('column' => 'id', 'unique' => 1));
  226. $result = $this->Dbo->index('simple', false);
  227. $this->Dbo->rawQuery('DROP TABLE ' . $name);
  228. $this->assertEquals($expected, $result);
  229. $name = $this->Dbo->fullTableName('with_a_key');
  230. $this->Dbo->rawQuery('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ));');
  231. $expected = array(
  232. 'PRIMARY' => array('column' => 'id', 'unique' => 1),
  233. 'pointless_bool' => array('column' => 'bool', 'unique' => 0),
  234. );
  235. $result = $this->Dbo->index('with_a_key', false);
  236. $this->Dbo->rawQuery('DROP TABLE ' . $name);
  237. $this->assertEquals($expected, $result);
  238. $name = $this->Dbo->fullTableName('with_two_keys');
  239. $this->Dbo->rawQuery('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ));');
  240. $expected = array(
  241. 'PRIMARY' => array('column' => 'id', 'unique' => 1),
  242. 'pointless_bool' => array('column' => 'bool', 'unique' => 0),
  243. 'pointless_small_int' => array('column' => 'small_int', 'unique' => 0),
  244. );
  245. $result = $this->Dbo->index('with_two_keys', false);
  246. $this->Dbo->rawQuery('DROP TABLE ' . $name);
  247. $this->assertEquals($expected, $result);
  248. $name = $this->Dbo->fullTableName('with_compound_keys');
  249. $this->Dbo->rawQuery('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ));');
  250. $expected = array(
  251. 'PRIMARY' => array('column' => 'id', 'unique' => 1),
  252. 'pointless_bool' => array('column' => 'bool', 'unique' => 0),
  253. 'pointless_small_int' => array('column' => 'small_int', 'unique' => 0),
  254. 'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0),
  255. );
  256. $result = $this->Dbo->index('with_compound_keys', false);
  257. $this->Dbo->rawQuery('DROP TABLE ' . $name);
  258. $this->assertEquals($expected, $result);
  259. $name = $this->Dbo->fullTableName('with_multiple_compound_keys');
  260. $this->Dbo->rawQuery('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ), KEY `other_way` ( `small_int`, `bool` ));');
  261. $expected = array(
  262. 'PRIMARY' => array('column' => 'id', 'unique' => 1),
  263. 'pointless_bool' => array('column' => 'bool', 'unique' => 0),
  264. 'pointless_small_int' => array('column' => 'small_int', 'unique' => 0),
  265. 'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0),
  266. 'other_way' => array('column' => array('small_int', 'bool'), 'unique' => 0),
  267. );
  268. $result = $this->Dbo->index('with_multiple_compound_keys', false);
  269. $this->Dbo->rawQuery('DROP TABLE ' . $name);
  270. $this->assertEquals($expected, $result);
  271. }
  272. /**
  273. * testBuildColumn method
  274. *
  275. * @return void
  276. */
  277. public function testBuildColumn() {
  278. $restore = $this->Dbo->columns;
  279. $this->Dbo->columns = array('varchar(255)' => 1);
  280. $data = array(
  281. 'name' => 'testName',
  282. 'type' => 'varchar(255)',
  283. 'default',
  284. 'null' => true,
  285. 'key',
  286. 'comment' => 'test'
  287. );
  288. $result = $this->Dbo->buildColumn($data);
  289. $expected = '`testName` DEFAULT NULL COMMENT \'test\'';
  290. $this->assertEquals($expected, $result);
  291. $data = array(
  292. 'name' => 'testName',
  293. 'type' => 'varchar(255)',
  294. 'default',
  295. 'null' => true,
  296. 'key',
  297. 'charset' => 'utf8',
  298. 'collate' => 'utf8_unicode_ci'
  299. );
  300. $result = $this->Dbo->buildColumn($data);
  301. $expected = '`testName` CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL';
  302. $this->assertEquals($expected, $result);
  303. $this->Dbo->columns = $restore;
  304. }
  305. /**
  306. * MySQL 4.x returns index data in a different format,
  307. * Using a mock ensure that MySQL 4.x output is properly parsed.
  308. *
  309. * @group indices
  310. * @return void
  311. */
  312. public function testIndexOnMySQL4Output() {
  313. $name = $this->Dbo->fullTableName('simple');
  314. $mockDbo = $this->getMock('Mysql', array('connect', '_execute', 'getVersion'));
  315. $columnData = array(
  316. array('0' => array(
  317. 'Table' => 'with_compound_keys',
  318. 'Non_unique' => '0',
  319. 'Key_name' => 'PRIMARY',
  320. 'Seq_in_index' => '1',
  321. 'Column_name' => 'id',
  322. 'Collation' => 'A',
  323. 'Cardinality' => '0',
  324. 'Sub_part' => NULL,
  325. 'Packed' => NULL,
  326. 'Null' => '',
  327. 'Index_type' => 'BTREE',
  328. 'Comment' => ''
  329. )),
  330. array('0' => array(
  331. 'Table' => 'with_compound_keys',
  332. 'Non_unique' => '1',
  333. 'Key_name' => 'pointless_bool',
  334. 'Seq_in_index' => '1',
  335. 'Column_name' => 'bool',
  336. 'Collation' => 'A',
  337. 'Cardinality' => NULL,
  338. 'Sub_part' => NULL,
  339. 'Packed' => NULL,
  340. 'Null' => 'YES',
  341. 'Index_type' => 'BTREE',
  342. 'Comment' => ''
  343. )),
  344. array('0' => array(
  345. 'Table' => 'with_compound_keys',
  346. 'Non_unique' => '1',
  347. 'Key_name' => 'pointless_small_int',
  348. 'Seq_in_index' => '1',
  349. 'Column_name' => 'small_int',
  350. 'Collation' => 'A',
  351. 'Cardinality' => NULL,
  352. 'Sub_part' => NULL,
  353. 'Packed' => NULL,
  354. 'Null' => 'YES',
  355. 'Index_type' => 'BTREE',
  356. 'Comment' => ''
  357. )),
  358. array('0' => array(
  359. 'Table' => 'with_compound_keys',
  360. 'Non_unique' => '1',
  361. 'Key_name' => 'one_way',
  362. 'Seq_in_index' => '1',
  363. 'Column_name' => 'bool',
  364. 'Collation' => 'A',
  365. 'Cardinality' => NULL,
  366. 'Sub_part' => NULL,
  367. 'Packed' => NULL,
  368. 'Null' => 'YES',
  369. 'Index_type' => 'BTREE',
  370. 'Comment' => ''
  371. )),
  372. array('0' => array(
  373. 'Table' => 'with_compound_keys',
  374. 'Non_unique' => '1',
  375. 'Key_name' => 'one_way',
  376. 'Seq_in_index' => '2',
  377. 'Column_name' => 'small_int',
  378. 'Collation' => 'A',
  379. 'Cardinality' => NULL,
  380. 'Sub_part' => NULL,
  381. 'Packed' => NULL,
  382. 'Null' => 'YES',
  383. 'Index_type' => 'BTREE',
  384. 'Comment' => ''
  385. ))
  386. );
  387. $mockDbo->expects($this->once())->method('getVersion')->will($this->returnValue('4.1'));
  388. $resultMock = $this->getMock('PDOStatement', array('fetch'));
  389. $mockDbo->expects($this->once())
  390. ->method('_execute')
  391. ->with('SHOW INDEX FROM ' . $name)
  392. ->will($this->returnValue($resultMock));
  393. foreach ($columnData as $i => $data) {
  394. $resultMock->expects($this->at($i))->method('fetch')->will($this->returnValue((object) $data));
  395. }
  396. $result = $mockDbo->index($name, false);
  397. $expected = array(
  398. 'PRIMARY' => array('column' => 'id', 'unique' => 1),
  399. 'pointless_bool' => array('column' => 'bool', 'unique' => 0),
  400. 'pointless_small_int' => array('column' => 'small_int', 'unique' => 0),
  401. 'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0),
  402. );
  403. $this->assertEquals($expected, $result);
  404. }
  405. /**
  406. * testColumn method
  407. *
  408. * @return void
  409. */
  410. public function testColumn() {
  411. $result = $this->Dbo->column('varchar(50)');
  412. $expected = 'string';
  413. $this->assertEquals($expected, $result);
  414. $result = $this->Dbo->column('text');
  415. $expected = 'text';
  416. $this->assertEquals($expected, $result);
  417. $result = $this->Dbo->column('int(11)');
  418. $expected = 'integer';
  419. $this->assertEquals($expected, $result);
  420. $result = $this->Dbo->column('int(11) unsigned');
  421. $expected = 'integer';
  422. $this->assertEquals($expected, $result);
  423. $result = $this->Dbo->column('tinyint(1)');
  424. $expected = 'boolean';
  425. $this->assertEquals($expected, $result);
  426. $result = $this->Dbo->column('boolean');
  427. $expected = 'boolean';
  428. $this->assertEquals($expected, $result);
  429. $result = $this->Dbo->column('float');
  430. $expected = 'float';
  431. $this->assertEquals($expected, $result);
  432. $result = $this->Dbo->column('float unsigned');
  433. $expected = 'float';
  434. $this->assertEquals($expected, $result);
  435. $result = $this->Dbo->column('double unsigned');
  436. $expected = 'float';
  437. $this->assertEquals($expected, $result);
  438. $result = $this->Dbo->column('decimal(14,7) unsigned');
  439. $expected = 'float';
  440. $this->assertEquals($expected, $result);
  441. }
  442. /**
  443. * testAlterSchemaIndexes method
  444. *
  445. * @group indices
  446. * @return void
  447. */
  448. public function testAlterSchemaIndexes() {
  449. $this->Dbo->cacheSources = $this->Dbo->testing = false;
  450. $table = $this->Dbo->fullTableName('altertest');
  451. $schema1 = new CakeSchema(array(
  452. 'name' => 'AlterTest1',
  453. 'connection' => 'test',
  454. 'altertest' => array(
  455. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
  456. 'name' => array('type' => 'string', 'null' => false, 'length' => 50),
  457. 'group1' => array('type' => 'integer', 'null' => true),
  458. 'group2' => array('type' => 'integer', 'null' => true)
  459. )));
  460. $result = $this->Dbo->createSchema($schema1);
  461. $this->assertContains('`id` int(11) DEFAULT 0 NOT NULL,', $result);
  462. $this->assertContains('`name` varchar(50) NOT NULL,', $result);
  463. $this->assertContains('`group1` int(11) DEFAULT NULL', $result);
  464. $this->assertContains('`group2` int(11) DEFAULT NULL', $result);
  465. //Test that the string is syntactically correct
  466. $query = $this->Dbo->getConnection()->prepare($result);
  467. $this->assertEquals($result, $query->queryString);
  468. $schema2 = new CakeSchema(array(
  469. 'name' => 'AlterTest2',
  470. 'connection' => 'test',
  471. 'altertest' => array(
  472. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
  473. 'name' => array('type' => 'string', 'null' => false, 'length' => 50),
  474. 'group1' => array('type' => 'integer', 'null' => true),
  475. 'group2' => array('type' => 'integer', 'null' => true),
  476. 'indexes' => array(
  477. 'name_idx' => array('column' => 'name', 'unique' => 0),
  478. 'group_idx' => array('column' => 'group1', 'unique' => 0),
  479. 'compound_idx' => array('column' => array('group1', 'group2'), 'unique' => 0),
  480. 'PRIMARY' => array('column' => 'id', 'unique' => 1))
  481. )));
  482. $result = $this->Dbo->alterSchema($schema2->compare($schema1));
  483. $this->assertContains("ALTER TABLE $table", $result);
  484. $this->assertContains('ADD KEY name_idx (`name`),', $result);
  485. $this->assertContains('ADD KEY group_idx (`group1`),', $result);
  486. $this->assertContains('ADD KEY compound_idx (`group1`, `group2`),', $result);
  487. $this->assertContains('ADD PRIMARY KEY (`id`);', $result);
  488. //Test that the string is syntactically correct
  489. $query = $this->Dbo->getConnection()->prepare($result);
  490. $this->assertEquals($result, $query->queryString);
  491. // Change three indexes, delete one and add another one
  492. $schema3 = new CakeSchema(array(
  493. 'name' => 'AlterTest3',
  494. 'connection' => 'test',
  495. 'altertest' => array(
  496. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
  497. 'name' => array('type' => 'string', 'null' => false, 'length' => 50),
  498. 'group1' => array('type' => 'integer', 'null' => true),
  499. 'group2' => array('type' => 'integer', 'null' => true),
  500. 'indexes' => array(
  501. 'name_idx' => array('column' => 'name', 'unique' => 1),
  502. 'group_idx' => array('column' => 'group2', 'unique' => 0),
  503. 'compound_idx' => array('column' => array('group2', 'group1'), 'unique' => 0),
  504. 'id_name_idx' => array('column' => array('id', 'name'), 'unique' => 0))
  505. )));
  506. $result = $this->Dbo->alterSchema($schema3->compare($schema2));
  507. $this->assertContains("ALTER TABLE $table", $result);
  508. $this->assertContains('DROP PRIMARY KEY,', $result);
  509. $this->assertContains('DROP KEY name_idx,', $result);
  510. $this->assertContains('DROP KEY group_idx,', $result);
  511. $this->assertContains('DROP KEY compound_idx,', $result);
  512. $this->assertContains('ADD KEY id_name_idx (`id`, `name`),', $result);
  513. $this->assertContains('ADD UNIQUE KEY name_idx (`name`),', $result);
  514. $this->assertContains('ADD KEY group_idx (`group2`),', $result);
  515. $this->assertContains('ADD KEY compound_idx (`group2`, `group1`);', $result);
  516. $query = $this->Dbo->getConnection()->prepare($result);
  517. $this->assertEquals($result, $query->queryString);
  518. // Compare us to ourself.
  519. $this->assertEquals($schema3->compare($schema3), array());
  520. // Drop the indexes
  521. $result = $this->Dbo->alterSchema($schema1->compare($schema3));
  522. $this->assertContains("ALTER TABLE $table", $result);
  523. $this->assertContains('DROP KEY name_idx,', $result);
  524. $this->assertContains('DROP KEY group_idx,', $result);
  525. $this->assertContains('DROP KEY compound_idx,', $result);
  526. $this->assertContains('DROP KEY id_name_idx;', $result);
  527. $query = $this->Dbo->getConnection()->prepare($result);
  528. $this->assertEquals($result, $query->queryString);
  529. }
  530. /**
  531. * test saving and retrieval of blobs
  532. *
  533. * @return void
  534. */
  535. public function testBlobSaving() {
  536. $this->loadFixtures('BinaryTest');
  537. $this->Dbo->cacheSources = false;
  538. $data = file_get_contents(CAKE . 'Test' . DS . 'test_app' . DS . 'webroot' . DS . 'img' . DS . 'cake.power.gif');
  539. $model = new CakeTestModel(array('name' => 'BinaryTest', 'ds' => 'test'));
  540. $model->save(compact('data'));
  541. $result = $model->find('first');
  542. $this->assertEquals($result['BinaryTest']['data'], $data);
  543. }
  544. /**
  545. * test altering the table settings with schema.
  546. *
  547. * @return void
  548. */
  549. public function testAlteringTableParameters() {
  550. $this->Dbo->cacheSources = $this->Dbo->testing = false;
  551. $schema1 = new CakeSchema(array(
  552. 'name' => 'AlterTest1',
  553. 'connection' => 'test',
  554. 'altertest' => array(
  555. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
  556. 'name' => array('type' => 'string', 'null' => false, 'length' => 50),
  557. 'tableParameters' => array(
  558. 'charset' => 'latin1',
  559. 'collate' => 'latin1_general_ci',
  560. 'engine' => 'MyISAM'
  561. )
  562. )
  563. ));
  564. $this->Dbo->rawQuery($this->Dbo->createSchema($schema1));
  565. $schema2 = new CakeSchema(array(
  566. 'name' => 'AlterTest1',
  567. 'connection' => 'test',
  568. 'altertest' => array(
  569. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
  570. 'name' => array('type' => 'string', 'null' => false, 'length' => 50),
  571. 'tableParameters' => array(
  572. 'charset' => 'utf8',
  573. 'collate' => 'utf8_general_ci',
  574. 'engine' => 'InnoDB'
  575. )
  576. )
  577. ));
  578. $result = $this->Dbo->alterSchema($schema2->compare($schema1));
  579. $this->assertContains('DEFAULT CHARSET=utf8', $result);
  580. $this->assertContains('ENGINE=InnoDB', $result);
  581. $this->assertContains('COLLATE=utf8_general_ci', $result);
  582. $this->Dbo->rawQuery($result);
  583. $result = $this->Dbo->listDetailedSources($this->Dbo->fullTableName('altertest', false, false));
  584. $this->assertEquals($result['Collation'], 'utf8_general_ci');
  585. $this->assertEquals($result['Engine'], 'InnoDB');
  586. $this->assertEquals($result['charset'], 'utf8');
  587. $this->Dbo->rawQuery($this->Dbo->dropSchema($schema1));
  588. }
  589. /**
  590. * test alterSchema on two tables.
  591. *
  592. * @return void
  593. */
  594. public function testAlteringTwoTables() {
  595. $schema1 = new CakeSchema(array(
  596. 'name' => 'AlterTest1',
  597. 'connection' => 'test',
  598. 'altertest' => array(
  599. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
  600. 'name' => array('type' => 'string', 'null' => false, 'length' => 50),
  601. ),
  602. 'other_table' => array(
  603. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
  604. 'name' => array('type' => 'string', 'null' => false, 'length' => 50),
  605. )
  606. ));
  607. $schema2 = new CakeSchema(array(
  608. 'name' => 'AlterTest1',
  609. 'connection' => 'test',
  610. 'altertest' => array(
  611. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
  612. 'field_two' => array('type' => 'string', 'null' => false, 'length' => 50),
  613. ),
  614. 'other_table' => array(
  615. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
  616. 'field_two' => array('type' => 'string', 'null' => false, 'length' => 50),
  617. )
  618. ));
  619. $result = $this->Dbo->alterSchema($schema2->compare($schema1));
  620. $this->assertEquals(2, substr_count($result, 'field_two'), 'Too many fields');
  621. }
  622. /**
  623. * testReadTableParameters method
  624. *
  625. * @return void
  626. */
  627. public function testReadTableParameters() {
  628. $this->Dbo->cacheSources = $this->Dbo->testing = false;
  629. $tableName = 'tinyint_' . uniqid();
  630. $table = $this->Dbo->fullTableName($tableName);
  631. $this->Dbo->rawQuery('CREATE TABLE ' . $table . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;');
  632. $result = $this->Dbo->readTableParameters($this->Dbo->fullTableName($tableName, false, false));
  633. $this->Dbo->rawQuery('DROP TABLE ' . $table);
  634. $expected = array(
  635. 'charset' => 'utf8',
  636. 'collate' => 'utf8_unicode_ci',
  637. 'engine' => 'InnoDB');
  638. $this->assertEquals($expected, $result);
  639. $table = $this->Dbo->fullTableName($tableName);
  640. $this->Dbo->rawQuery('CREATE TABLE ' . $table . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id)) ENGINE=MyISAM DEFAULT CHARSET=cp1250 COLLATE=cp1250_general_ci;');
  641. $result = $this->Dbo->readTableParameters($this->Dbo->fullTableName($tableName, false, false));
  642. $this->Dbo->rawQuery('DROP TABLE ' . $table);
  643. $expected = array(
  644. 'charset' => 'cp1250',
  645. 'collate' => 'cp1250_general_ci',
  646. 'engine' => 'MyISAM');
  647. $this->assertEquals($expected, $result);
  648. }
  649. /**
  650. * testBuildTableParameters method
  651. *
  652. * @return void
  653. */
  654. public function testBuildTableParameters() {
  655. $this->Dbo->cacheSources = $this->Dbo->testing = false;
  656. $data = array(
  657. 'charset' => 'utf8',
  658. 'collate' => 'utf8_unicode_ci',
  659. 'engine' => 'InnoDB');
  660. $result = $this->Dbo->buildTableParameters($data);
  661. $expected = array(
  662. 'DEFAULT CHARSET=utf8',
  663. 'COLLATE=utf8_unicode_ci',
  664. 'ENGINE=InnoDB');
  665. $this->assertEquals($expected, $result);
  666. }
  667. /**
  668. * testBuildTableParameters method
  669. *
  670. * @return void
  671. */
  672. public function testGetCharsetName() {
  673. $this->Dbo->cacheSources = $this->Dbo->testing = false;
  674. $result = $this->Dbo->getCharsetName('utf8_unicode_ci');
  675. $this->assertEquals($result, 'utf8');
  676. $result = $this->Dbo->getCharsetName('cp1250_general_ci');
  677. $this->assertEquals($result, 'cp1250');
  678. }
  679. /**
  680. * test that changing the virtualFieldSeparator allows for __ fields.
  681. *
  682. * @return void
  683. */
  684. public function testVirtualFieldSeparators() {
  685. $this->loadFixtures('BinaryTest');
  686. $model = new CakeTestModel(array('table' => 'binary_tests', 'ds' => 'test', 'name' => 'BinaryTest'));
  687. $model->virtualFields = array(
  688. 'other__field' => 'SUM(id)'
  689. );
  690. $this->Dbo->virtualFieldSeparator = '_$_';
  691. $result = $this->Dbo->fields($model, null, array('data', 'other__field'));
  692. $expected = array('`BinaryTest`.`data`', '(SUM(id)) AS `BinaryTest_$_other__field`');
  693. $this->assertEquals($expected, $result);
  694. }
  695. /**
  696. * Test describe() on a fixture.
  697. *
  698. * @return void
  699. */
  700. public function testDescribe() {
  701. $this->loadFixtures('Apple');
  702. $model = new Apple();
  703. $result = $this->Dbo->describe($model);
  704. $this->assertTrue(isset($result['id']));
  705. $this->assertTrue(isset($result['color']));
  706. $result = $this->Dbo->describe($model->useTable);
  707. $this->assertTrue(isset($result['id']));
  708. $this->assertTrue(isset($result['color']));
  709. }
  710. /**
  711. * test that a describe() gets additional fieldParameters
  712. *
  713. * @return void
  714. */
  715. public function testDescribeGettingFieldParameters() {
  716. $schema = new CakeSchema(array(
  717. 'connection' => 'test',
  718. 'testdescribes' => array(
  719. 'id' => array('type' => 'integer', 'key' => 'primary'),
  720. 'stringy' => array(
  721. 'type' => 'string',
  722. 'null' => true,
  723. 'charset' => 'cp1250',
  724. 'collate' => 'cp1250_general_ci',
  725. ),
  726. 'other_col' => array(
  727. 'type' => 'string',
  728. 'null' => false,
  729. 'charset' => 'latin1',
  730. 'comment' => 'Test Comment'
  731. )
  732. )
  733. ));
  734. $this->Dbo->execute($this->Dbo->createSchema($schema));
  735. $model = new CakeTestModel(array('table' => 'testdescribes', 'name' => 'Testdescribes'));
  736. $result = $model->getDataSource()->describe($model);
  737. $this->Dbo->execute($this->Dbo->dropSchema($schema));
  738. $this->assertEquals($result['stringy']['collate'], 'cp1250_general_ci');
  739. $this->assertEquals($result['stringy']['charset'], 'cp1250');
  740. $this->assertEquals($result['other_col']['comment'], 'Test Comment');
  741. }
  742. /**
  743. * Tests that listSources method sends the correct query and parses the result accordingly
  744. * @return void
  745. */
  746. public function testListSources() {
  747. $db = $this->getMock('Mysql', array('connect', '_execute'));
  748. $queryResult = $this->getMock('PDOStatement');
  749. $db->expects($this->once())
  750. ->method('_execute')
  751. ->with('SHOW TABLES FROM `cake`')
  752. ->will($this->returnValue($queryResult));
  753. $queryResult->expects($this->at(0))
  754. ->method('fetch')
  755. ->will($this->returnValue(array('cake_table')));
  756. $queryResult->expects($this->at(1))
  757. ->method('fetch')
  758. ->will($this->returnValue(array('another_table')));
  759. $queryResult->expects($this->at(2))
  760. ->method('fetch')
  761. ->will($this->returnValue(null));
  762. $tables = $db->listSources();
  763. $this->assertEquals($tables, array('cake_table', 'another_table'));
  764. }
  765. /**
  766. * test that listDetailedSources with a named table that doesn't exist.
  767. *
  768. * @return void
  769. */
  770. public function testListDetailedSourcesNamed() {
  771. $this->loadFixtures('Apple');
  772. $result = $this->Dbo->listDetailedSources('imaginary');
  773. $this->assertEquals(array(), $result, 'Should be empty when table does not exist.');
  774. $result = $this->Dbo->listDetailedSources();
  775. $tableName = $this->Dbo->fullTableName('apples', false, false);
  776. $this->assertTrue(isset($result[$tableName]), 'Key should exist');
  777. }
  778. /**
  779. * Tests that getVersion method sends the correct query for getting the mysql version
  780. * @return void
  781. */
  782. public function testGetVersion() {
  783. $version = $this->Dbo->getVersion();
  784. $this->assertTrue(is_string($version));
  785. }
  786. /**
  787. * Tests that getVersion method sends the correct query for getting the client encoding
  788. * @return void
  789. */
  790. public function testGetEncoding() {
  791. $db = $this->getMock('Mysql', array('connect', '_execute'));
  792. $queryResult = $this->getMock('PDOStatement');
  793. $db->expects($this->once())
  794. ->method('_execute')
  795. ->with('SHOW VARIABLES LIKE ?', array('character_set_client'))
  796. ->will($this->returnValue($queryResult));
  797. $result = new StdClass;
  798. $result->Value = 'utf-8';
  799. $queryResult->expects($this->once())
  800. ->method('fetchObject')
  801. ->will($this->returnValue($result));
  802. $encoding = $db->getEncoding();
  803. $this->assertEquals('utf-8', $encoding);
  804. }
  805. /**
  806. * testFieldDoubleEscaping method
  807. *
  808. * @return void
  809. */
  810. public function testFieldDoubleEscaping() {
  811. $db = $this->Dbo->config['database'];
  812. $test = $this->getMock('Mysql', array('connect', '_execute', 'execute'));
  813. $test->config['database'] = $db;
  814. $this->Model = $this->getMock('Article2', array('getDataSource'));
  815. $this->Model->alias = 'Article';
  816. $this->Model->expects($this->any())
  817. ->method('getDataSource')
  818. ->will($this->returnValue($test));
  819. $this->assertEquals($this->Model->escapeField(), '`Article`.`id`');
  820. $result = $test->fields($this->Model, null, $this->Model->escapeField());
  821. $this->assertEquals($result, array('`Article`.`id`'));
  822. $test->expects($this->at(0))->method('execute')
  823. ->with('SELECT `Article`.`id` FROM ' . $test->fullTableName('articles'). ' AS `Article` WHERE 1 = 1');
  824. $result = $test->read($this->Model, array(
  825. 'fields' => $this->Model->escapeField(),
  826. 'conditions' => null,
  827. 'recursive' => -1
  828. ));
  829. $test->startQuote = '[';
  830. $test->endQuote = ']';
  831. $this->assertEquals($this->Model->escapeField(), '[Article].[id]');
  832. $result = $test->fields($this->Model, null, $this->Model->escapeField());
  833. $this->assertEquals($result, array('[Article].[id]'));
  834. $test->expects($this->at(0))->method('execute')
  835. ->with('SELECT [Article].[id] FROM ' . $test->fullTableName('articles') . ' AS [Article] WHERE 1 = 1');
  836. $result = $test->read($this->Model, array(
  837. 'fields' => $this->Model->escapeField(),
  838. 'conditions' => null,
  839. 'recursive' => -1
  840. ));
  841. }
  842. /**
  843. * testGenerateAssociationQuerySelfJoin method
  844. *
  845. * @return void
  846. */
  847. public function testGenerateAssociationQuerySelfJoin() {
  848. $this->Dbo = $this->getMock('Mysql', array('connect', '_execute', 'execute'));
  849. $this->startTime = microtime(true);
  850. $this->Model = new Article2();
  851. $this->_buildRelatedModels($this->Model);
  852. $this->_buildRelatedModels($this->Model->Category2);
  853. $this->Model->Category2->ChildCat = new Category2();
  854. $this->Model->Category2->ParentCat = new Category2();
  855. $queryData = array();
  856. foreach ($this->Model->Category2->associations() as $type) {
  857. foreach ($this->Model->Category2->{$type} as $assoc => $assocData) {
  858. $linkModel = $this->Model->Category2->{$assoc};
  859. $external = isset($assocData['external']);
  860. if ($this->Model->Category2->alias == $linkModel->alias && $type != 'hasAndBelongsToMany' && $type != 'hasMany') {
  861. $result = $this->Dbo->generateAssociationQuery($this->Model->Category2, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null);
  862. $this->assertFalse(empty($result));
  863. } else {
  864. if ($this->Model->Category2->useDbConfig == $linkModel->useDbConfig) {
  865. $result = $this->Dbo->generateAssociationQuery($this->Model->Category2, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null);
  866. $this->assertFalse(empty($result));
  867. }
  868. }
  869. }
  870. }
  871. $query = $this->Dbo->generateAssociationQuery($this->Model->Category2, $null, null, null, null, $queryData, false, $null);
  872. $this->assertRegExp('/^SELECT\s+(.+)FROM(.+)`Category2`\.`group_id`\s+=\s+`Group`\.`id`\)\s+LEFT JOIN(.+)WHERE\s+1 = 1\s*$/', $query);
  873. $this->Model = new TestModel4();
  874. $this->Model->schema();
  875. $this->_buildRelatedModels($this->Model);
  876. $binding = array('type' => 'belongsTo', 'model' => 'TestModel4Parent');
  877. $queryData = array();
  878. $resultSet = null;
  879. $null = null;
  880. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  881. $_queryData = $queryData;
  882. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  883. $this->assertTrue($result);
  884. $expected = array(
  885. 'conditions' => array(),
  886. 'fields' => array(
  887. '`TestModel4`.`id`',
  888. '`TestModel4`.`name`',
  889. '`TestModel4`.`created`',
  890. '`TestModel4`.`updated`',
  891. '`TestModel4Parent`.`id`',
  892. '`TestModel4Parent`.`name`',
  893. '`TestModel4Parent`.`created`',
  894. '`TestModel4Parent`.`updated`'
  895. ),
  896. 'joins' => array(
  897. array(
  898. 'table' => $this->Dbo->fullTableName($this->Model),
  899. 'alias' => 'TestModel4Parent',
  900. 'type' => 'LEFT',
  901. 'conditions' => '`TestModel4`.`parent_id` = `TestModel4Parent`.`id`'
  902. )
  903. ),
  904. 'order' => array(),
  905. 'limit' => array(),
  906. 'offset' => array(),
  907. 'group' => array(),
  908. 'callbacks' => null
  909. );
  910. $queryData['joins'][0]['table'] = $this->Dbo->fullTableName($queryData['joins'][0]['table']);
  911. $this->assertEquals($queryData, $expected);
  912. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  913. $this->assertRegExp('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel4Parent`\.`id`, `TestModel4Parent`\.`name`, `TestModel4Parent`\.`created`, `TestModel4Parent`\.`updated`\s+/', $result);
  914. $this->assertRegExp('/FROM\s+\S+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+\S+`test_model4` AS `TestModel4Parent`/', $result);
  915. $this->assertRegExp('/\s+ON\s+\(`TestModel4`.`parent_id` = `TestModel4Parent`.`id`\)\s+WHERE/', $result);
  916. $this->assertRegExp('/\s+WHERE\s+1 = 1\s+$/', $result);
  917. $params['assocData']['type'] = 'INNER';
  918. $this->Model->belongsTo['TestModel4Parent']['type'] = 'INNER';
  919. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $_queryData, $params['external'], $resultSet);
  920. $this->assertTrue($result);
  921. $this->assertEquals($_queryData['joins'][0]['type'], 'INNER');
  922. }
  923. /**
  924. * buildRelatedModels method
  925. *
  926. * @param mixed $model
  927. * @return void
  928. */
  929. function _buildRelatedModels($model) {
  930. foreach ($model->associations() as $type) {
  931. foreach ($model->{$type} as $assoc => $assocData) {
  932. if (is_string($assocData)) {
  933. $className = $assocData;
  934. } elseif (isset($assocData['className'])) {
  935. $className = $assocData['className'];
  936. }
  937. $model->$className = new $className();
  938. $model->$className->schema();
  939. }
  940. }
  941. }
  942. /**
  943. * &_prepareAssociationQuery method
  944. *
  945. * @param mixed $model
  946. * @param mixed $queryData
  947. * @param mixed $binding
  948. * @return void
  949. */
  950. function &_prepareAssociationQuery($model, &$queryData, $binding) {
  951. $type = $binding['type'];
  952. $assoc = $binding['model'];
  953. $assocData = $model->{$type}[$assoc];
  954. $className = $assocData['className'];
  955. $linkModel = $model->{$className};
  956. $external = isset($assocData['external']);
  957. $reflection = new ReflectionMethod($this->Dbo, '_scrubQueryData');
  958. $reflection->setAccessible(true);
  959. $queryData = $reflection->invokeArgs($this->Dbo, array($queryData));
  960. $result = array_merge(array('linkModel' => &$linkModel), compact('type', 'assoc', 'assocData', 'external'));
  961. return $result;
  962. }
  963. /**
  964. * testGenerateInnerJoinAssociationQuery method
  965. *
  966. * @return void
  967. */
  968. public function testGenerateInnerJoinAssociationQuery() {
  969. $db = $this->Dbo->config['database'];
  970. $test = $this->getMock('Mysql', array('connect', '_execute', 'execute'));
  971. $test->config['database'] = $db;
  972. $this->Model = $this->getMock('TestModel9', array('getDataSource'));
  973. $this->Model->expects($this->any())
  974. ->method('getDataSource')
  975. ->will($this->returnValue($test));
  976. $this->Model->TestModel8 = $this->getMock('TestModel8', array('getDataSource'));
  977. $this->Model->TestModel8->expects($this->any())
  978. ->method('getDataSource')
  979. ->will($this->returnValue($test));
  980. $testModel8Table = $this->Model->TestModel8->getDataSource()->fullTableName($this->Model->TestModel8);
  981. $test->expects($this->at(0))->method('execute')
  982. ->with($this->stringContains('`TestModel9` LEFT JOIN ' . $testModel8Table));
  983. $test->expects($this->at(1))->method('execute')
  984. ->with($this->stringContains('TestModel9` INNER JOIN ' . $testModel8Table));
  985. $test->read($this->Model, array('recursive' => 1));
  986. $this->Model->belongsTo['TestModel8']['type'] = 'INNER';
  987. $test->read($this->Model, array('recursive' => 1));
  988. }
  989. /**
  990. * testGenerateAssociationQuerySelfJoinWithConditionsInHasOneBinding method
  991. *
  992. * @return void
  993. */
  994. public function testGenerateAssociationQuerySelfJoinWithConditionsInHasOneBinding() {
  995. $this->Model = new TestModel8();
  996. $this->Model->schema();
  997. $this->_buildRelatedModels($this->Model);
  998. $binding = array('type' => 'hasOne', 'model' => 'TestModel9');
  999. $queryData = array();
  1000. $resultSet = null;
  1001. $null = null;
  1002. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1003. $_queryData = $queryData;
  1004. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1005. $this->assertTrue($result);
  1006. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1007. $this->assertRegExp('/^SELECT\s+`TestModel8`\.`id`, `TestModel8`\.`test_model9_id`, `TestModel8`\.`name`, `TestModel8`\.`created`, `TestModel8`\.`updated`, `TestModel9`\.`id`, `TestModel9`\.`test_model8_id`, `TestModel9`\.`name`, `TestModel9`\.`created`, `TestModel9`\.`updated`\s+/', $result);
  1008. $this->assertRegExp('/FROM\s+\S+`test_model8` AS `TestModel8`\s+LEFT JOIN\s+\S+`test_model9` AS `TestModel9`/', $result);
  1009. $this->assertRegExp('/\s+ON\s+\(`TestModel9`\.`name` != \'mariano\'\s+AND\s+`TestModel9`.`test_model8_id` = `TestModel8`.`id`\)\s+WHERE/', $result);
  1010. $this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1011. }
  1012. /**
  1013. * testGenerateAssociationQuerySelfJoinWithConditionsInBelongsToBinding method
  1014. *
  1015. * @return void
  1016. */
  1017. public function testGenerateAssociationQuerySelfJoinWithConditionsInBelongsToBinding() {
  1018. $this->Model = new TestModel9();
  1019. $this->Model->schema();
  1020. $this->_buildRelatedModels($this->Model);
  1021. $binding = array('type' => 'belongsTo', 'model' => 'TestModel8');
  1022. $queryData = array();
  1023. $resultSet = null;
  1024. $null = null;
  1025. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1026. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1027. $this->assertTrue($result);
  1028. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1029. $this->assertRegExp('/^SELECT\s+`TestModel9`\.`id`, `TestModel9`\.`test_model8_id`, `TestModel9`\.`name`, `TestModel9`\.`created`, `TestModel9`\.`updated`, `TestModel8`\.`id`, `TestModel8`\.`test_model9_id`, `TestModel8`\.`name`, `TestModel8`\.`created`, `TestModel8`\.`updated`\s+/', $result);
  1030. $this->assertRegExp('/FROM\s+\S+`test_model9` AS `TestModel9`\s+LEFT JOIN\s+\S+`test_model8` AS `TestModel8`/', $result);
  1031. $this->assertRegExp('/\s+ON\s+\(`TestModel8`\.`name` != \'larry\'\s+AND\s+`TestModel9`.`test_model8_id` = `TestModel8`.`id`\)\s+WHERE/', $result);
  1032. $this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1033. }
  1034. /**
  1035. * testGenerateAssociationQuerySelfJoinWithConditions method
  1036. *
  1037. * @return void
  1038. */
  1039. public function testGenerateAssociationQuerySelfJoinWithConditions() {
  1040. $this->Model = new TestModel4();
  1041. $this->Model->schema();
  1042. $this->_buildRelatedModels($this->Model);
  1043. $binding = array('type' => 'belongsTo', 'model' => 'TestModel4Parent');
  1044. $queryData = array('conditions' => array('TestModel4Parent.name !=' => 'mariano'));
  1045. $resultSet = null;
  1046. $null = null;
  1047. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1048. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1049. $this->assertTrue($result);
  1050. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1051. $this->assertRegExp('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel4Parent`\.`id`, `TestModel4Parent`\.`name`, `TestModel4Parent`\.`created`, `TestModel4Parent`\.`updated`\s+/', $result);
  1052. $this->assertRegExp('/FROM\s+\S+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+\S+`test_model4` AS `TestModel4Parent`/', $result);
  1053. $this->assertRegExp('/\s+ON\s+\(`TestModel4`.`parent_id` = `TestModel4Parent`.`id`\)\s+WHERE/', $result);
  1054. $this->assertRegExp('/\s+WHERE\s+(?:\()?`TestModel4Parent`.`name`\s+!=\s+\'mariano\'(?:\))?\s*$/', $result);
  1055. $this->Featured2 = new Featured2();
  1056. $this->Featured2->schema();
  1057. $this->Featured2->bindModel(array(
  1058. 'belongsTo' => array(
  1059. 'ArticleFeatured2' => array(
  1060. 'conditions' => 'ArticleFeatured2.published = \'Y\'',
  1061. 'fields' => 'id, title, user_id, published'
  1062. )
  1063. )
  1064. ));
  1065. $this->_buildRelatedModels($this->Featured2);
  1066. $binding = array('type' => 'belongsTo', 'model' => 'ArticleFeatured2');
  1067. $queryData = array('conditions' => array());
  1068. $resultSet = null;
  1069. $null = null;
  1070. $params = &$this->_prepareAssociationQuery($this->Featured2, $queryData, $binding);
  1071. $result = $this->Dbo->generateAssociationQuery($this->Featured2, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1072. $this->assertTrue($result);
  1073. $result = $this->Dbo->generateAssociationQuery($this->Featured2, $null, null, null, null, $queryData, false, $null);
  1074. $this->assertRegExp(
  1075. '/^SELECT\s+`Featured2`\.`id`, `Featured2`\.`article_id`, `Featured2`\.`category_id`, `Featured2`\.`name`,\s+'.
  1076. '`ArticleFeatured2`\.`id`, `ArticleFeatured2`\.`title`, `ArticleFeatured2`\.`user_id`, `ArticleFeatured2`\.`published`\s+' .
  1077. 'FROM\s+\S+`featured2` AS `Featured2`\s+LEFT JOIN\s+\S+`article_featured` AS `ArticleFeatured2`' .
  1078. '\s+ON\s+\(`ArticleFeatured2`.`published` = \'Y\'\s+AND\s+`Featured2`\.`article_featured2_id` = `ArticleFeatured2`\.`id`\)' .
  1079. '\s+WHERE\s+1\s+=\s+1\s*$/',
  1080. $result
  1081. );
  1082. }
  1083. /**
  1084. * testGenerateAssociationQueryHasOne method
  1085. *
  1086. * @return void
  1087. */
  1088. public function testGenerateAssociationQueryHasOne() {
  1089. $this->Model = new TestModel4();
  1090. $this->Model->schema();
  1091. $this->_buildRelatedModels($this->Model);
  1092. $binding = array('type' => 'hasOne', 'model' => 'TestModel5');
  1093. $queryData = array();
  1094. $resultSet = null;
  1095. $null = null;
  1096. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1097. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1098. $this->assertTrue($result);
  1099. $testModel5Table = $this->Dbo->fullTableName($this->Model->TestModel5);
  1100. $result = $this->Dbo->buildJoinStatement($queryData['joins'][0]);
  1101. $expected = ' LEFT JOIN ' . $testModel5Table . ' AS `TestModel5` ON (`TestModel5`.`test_model4_id` = `TestModel4`.`id`)';
  1102. $this->assertEquals(trim($result), trim($expected));
  1103. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1104. $this->assertRegExp('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result);
  1105. $this->assertRegExp('/\s+FROM\s+\S+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+/', $result);
  1106. $this->assertRegExp('/`test_model5` AS `TestModel5`\s+ON\s+\(`TestModel5`.`test_model4_id` = `TestModel4`.`id`\)\s+WHERE/', $result);
  1107. $this->assertRegExp('/\s+WHERE\s+(?:\()?\s*1 = 1\s*(?:\))?\s*$/', $result);
  1108. }
  1109. /**
  1110. * testGenerateAssociationQueryHasOneWithConditions method
  1111. *
  1112. * @return void
  1113. */
  1114. public function testGenerateAssociationQueryHasOneWithConditions() {
  1115. $this->Model = new TestModel4();
  1116. $this->Model->schema();
  1117. $this->_buildRelatedModels($this->Model);
  1118. $binding = array('type' => 'hasOne', 'model' => 'TestModel5');
  1119. $queryData = array('conditions' => array('TestModel5.name !=' => 'mariano'));
  1120. $resultSet = null;
  1121. $null = null;
  1122. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1123. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1124. $this->assertTrue($result);
  1125. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1126. $this->assertRegExp('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result);
  1127. $this->assertRegExp('/\s+FROM\s+\S+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+\S+`test_model5` AS `TestModel5`/', $result);
  1128. $this->assertRegExp('/\s+ON\s+\(`TestModel5`.`test_model4_id`\s+=\s+`TestModel4`.`id`\)\s+WHERE/', $result);
  1129. $this->assertRegExp('/\s+WHERE\s+(?:\()?\s*`TestModel5`.`name`\s+!=\s+\'mariano\'\s*(?:\))?\s*$/', $result);
  1130. }
  1131. /**
  1132. * testGenerateAssociationQueryBelongsTo method
  1133. *
  1134. * @return void
  1135. */
  1136. public function testGenerateAssociationQueryBelongsTo() {
  1137. $this->Model = new TestModel5();
  1138. $this->Model->schema();
  1139. $this->_buildRelatedModels($this->Model);
  1140. $binding = array('type' => 'belongsTo', 'model' => 'TestModel4');
  1141. $queryData = array();
  1142. $resultSet = null;
  1143. $null = null;
  1144. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1145. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1146. $this->assertTrue($result);
  1147. $testModel4Table = $this->Dbo->fullTableName($this->Model->TestModel4, true, true);
  1148. $result = $this->Dbo->buildJoinStatement($queryData['joins'][0]);
  1149. $expected = ' LEFT JOIN ' .$testModel4Table. ' AS `TestModel4` ON (`TestModel5`.`test_model4_id` = `TestModel4`.`id`)';
  1150. $this->assertEquals(trim($result), trim($expected));
  1151. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1152. $this->assertRegExp('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`, `TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result);
  1153. $this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+LEFT JOIN\s+\S+`test_model4` AS `TestModel4`/', $result);
  1154. $this->assertRegExp('/\s+ON\s+\(`TestModel5`.`test_model4_id` = `TestModel4`.`id`\)\s+WHERE\s+/', $result);
  1155. $this->assertRegExp('/\s+WHERE\s+(?:\()?\s*1 = 1\s*(?:\))?\s*$/', $result);
  1156. }
  1157. /**
  1158. * testGenerateAssociationQueryBelongsToWithConditions method
  1159. *
  1160. * @return void
  1161. */
  1162. public function testGenerateAssociationQueryBelongsToWithConditions() {
  1163. $this->Model = new TestModel5();
  1164. $this->Model->schema();
  1165. $this->_buildRelatedModels($this->Model);
  1166. $binding = array('type' => 'belongsTo', 'model' => 'TestModel4');
  1167. $queryData = array('conditions' => array('TestModel5.name !=' => 'mariano'));
  1168. $resultSet = null;
  1169. $null = null;
  1170. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1171. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1172. $this->assertTrue($result);
  1173. $testModel4Table = $this->Dbo->fullTableName($this->Model->TestModel4, true, true);
  1174. $result = $this->Dbo->buildJoinStatement($queryData['joins'][0]);
  1175. $expected = ' LEFT JOIN ' .$testModel4Table. ' AS `TestModel4` ON (`TestModel5`.`test_model4_id` = `TestModel4`.`id`)';
  1176. $this->assertEquals(trim($result), trim($expected));
  1177. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1178. $this->assertRegExp('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`, `TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result);
  1179. $this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+LEFT JOIN\s+\S+`test_model4` AS `TestModel4`/', $result);
  1180. $this->assertRegExp('/\s+ON\s+\(`TestModel5`.`test_model4_id` = `TestModel4`.`id`\)\s+WHERE\s+/', $result);
  1181. $this->assertRegExp('/\s+WHERE\s+`TestModel5`.`name` != \'mariano\'\s*$/', $result);
  1182. }
  1183. /**
  1184. * testGenerateAssociationQueryHasMany method
  1185. *
  1186. * @return void
  1187. */
  1188. public function testGenerateAssociationQueryHasMany() {
  1189. $this->Model = new TestModel5();
  1190. $this->Model->schema();
  1191. $this->_buildRelatedModels($this->Model);
  1192. $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
  1193. $queryData = array();
  1194. $resultSet = null;
  1195. $null = null;
  1196. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1197. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1198. $this->assertRegExp('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result);
  1199. $this->assertRegExp('/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE/', $result);
  1200. $this->assertRegExp('/\s+WHERE\s+`TestModel6`.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)/', $result);
  1201. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1202. $this->assertRegExp('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result);
  1203. $this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
  1204. $this->assertRegExp('/\s+WHERE\s+(?:\()?\s*1 = 1\s*(?:\))?\s*$/', $result);
  1205. }
  1206. /**
  1207. * testGenerateAssociationQueryHasManyWithLimit method
  1208. *
  1209. * @return void
  1210. */
  1211. public function testGenerateAssociationQueryHasManyWithLimit() {
  1212. $this->Model = new TestModel5();
  1213. $this->Model->schema();
  1214. $this->_buildRelatedModels($this->Model);
  1215. $this->Model->hasMany['TestModel6']['limit'] = 2;
  1216. $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
  1217. $queryData = array();
  1218. $resultSet = null;
  1219. $null = null;
  1220. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1221. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1222. $this->assertRegExp(
  1223. '/^SELECT\s+' .
  1224. '`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+'.
  1225. 'FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+' .
  1226. '`TestModel6`.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)\s*'.
  1227. 'LIMIT \d*'.
  1228. '\s*$/', $result
  1229. );
  1230. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1231. $this->assertRegExp(
  1232. '/^SELECT\s+'.
  1233. '`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+'.
  1234. 'FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+'.
  1235. '(?:\()?\s*1 = 1\s*(?:\))?'.
  1236. '\s*$/', $result
  1237. );
  1238. }
  1239. /**
  1240. * testGenerateAssociationQueryHasManyWithConditions method
  1241. *
  1242. * @return void
  1243. */
  1244. public function testGenerateAssociationQueryHasManyWithConditions() {
  1245. $this->Model = new TestModel5();
  1246. $this->Model->schema();
  1247. $this->_buildRelatedModels($this->Model);
  1248. $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
  1249. $queryData = array('conditions' => array('TestModel5.name !=' => 'mariano'));
  1250. $resultSet = null;
  1251. $null = null;
  1252. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1253. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1254. $this->assertRegExp('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result);
  1255. $this->assertRegExp('/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
  1256. $this->assertRegExp('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
  1257. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1258. $this->assertRegExp('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result);
  1259. $this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
  1260. $this->assertRegExp('/\s+WHERE\s+(?:\()?`TestModel5`.`name`\s+!=\s+\'mariano\'(?:\))?\s*$/', $result);
  1261. }
  1262. /**
  1263. * testGenerateAssociationQueryHasManyWithOffsetAndLimit method
  1264. *
  1265. * @return void
  1266. */
  1267. public function testGenerateAssociationQueryHasManyWithOffsetAndLimit() {
  1268. $this->Model = new TestModel5();
  1269. $this->Model->schema();
  1270. $this->_buildRelatedModels($this->Model);
  1271. $__backup = $this->Model->hasMany['TestModel6'];
  1272. $this->Model->hasMany['TestModel6']['offset'] = 2;
  1273. $this->Model->hasMany['TestModel6']['limit'] = 5;
  1274. $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
  1275. $queryData = array();
  1276. $resultSet = null;
  1277. $null = null;
  1278. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1279. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1280. $this->assertRegExp('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result);
  1281. $this->assertRegExp('/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
  1282. $this->assertRegExp('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
  1283. $this->assertRegExp('/\s+LIMIT 2,\s*5\s*$/', $result);
  1284. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1285. $this->assertRegExp('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result);
  1286. $this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
  1287. $this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1288. $this->Model->hasMany['TestModel6'] = $__backup;
  1289. }
  1290. /**
  1291. * testGenerateAssociationQueryHasManyWithPageAndLimit method
  1292. *
  1293. * @return void
  1294. */
  1295. public function testGenerateAssociationQueryHasManyWithPageAndLimit() {
  1296. $this->Model = new TestModel5();
  1297. $this->Model->schema();
  1298. $this->_buildRelatedModels($this->Model);
  1299. $__backup = $this->Model->hasMany['TestModel6'];
  1300. $this->Model->hasMany['TestModel6']['page'] = 2;
  1301. $this->Model->hasMany['TestModel6']['limit'] = 5;
  1302. $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
  1303. $queryData = array();
  1304. $resultSet = null;
  1305. $null = null;
  1306. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1307. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1308. $this->assertRegExp('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result);
  1309. $this->assertRegExp('/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
  1310. $this->assertRegExp('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
  1311. $this->assertRegExp('/\s+LIMIT 5,\s*5\s*$/', $result);
  1312. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1313. $this->assertRegExp('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result);
  1314. $this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
  1315. $this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1316. $this->Model->hasMany['TestModel6'] = $__backup;
  1317. }
  1318. /**
  1319. * testGenerateAssociationQueryHasManyWithFields method
  1320. *
  1321. * @return void
  1322. */
  1323. public function testGenerateAssociationQueryHasManyWithFields() {
  1324. $this->Model = new TestModel5();
  1325. $this->Model->schema();
  1326. $this->_buildRelatedModels($this->Model);
  1327. $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
  1328. $queryData = array('fields' => array('`TestModel5`.`name`'));
  1329. $resultSet = null;
  1330. $null = null;
  1331. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1332. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1333. $this->assertRegExp('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result);
  1334. $this->assertRegExp('/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
  1335. $this->assertRegExp('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
  1336. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1337. $this->assertRegExp('/^SELECT\s+`TestModel5`\.`name`, `TestModel5`\.`id`\s+/', $result);
  1338. $this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
  1339. $this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1340. $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
  1341. $queryData = array('fields' => array('`TestModel5`.`id`, `TestModel5`.`name`'));
  1342. $resultSet = null;
  1343. $null = null;
  1344. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1345. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1346. $this->assertRegExp('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result);
  1347. $this->assertRegExp('/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
  1348. $this->assertRegExp('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
  1349. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1350. $this->assertRegExp('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/', $result);
  1351. $this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
  1352. $this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1353. $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
  1354. $queryData = array('fields' => array('`TestModel5`.`name`', '`TestModel5`.`created`'));
  1355. $resultSet = null;
  1356. $null = null;
  1357. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1358. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1359. $this->assertRegExp('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result);
  1360. $this->assertRegExp('/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
  1361. $this->assertRegExp('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
  1362. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1363. $this->assertRegExp('/^SELECT\s+`TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`id`\s+/', $result);
  1364. $this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
  1365. $this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1366. $this->Model->hasMany['TestModel6']['fields'] = array('name');
  1367. $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
  1368. $queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`'));
  1369. $resultSet = null;
  1370. $null = null;
  1371. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1372. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1373. $this->assertRegExp('/^SELECT\s+`TestModel6`\.`name`, `TestModel6`\.`test_model5_id`\s+/', $result);
  1374. $this->assertRegExp('/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
  1375. $this->assertRegExp('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
  1376. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1377. $this->assertRegExp('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/', $result);
  1378. $this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
  1379. $this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1380. unset($this->Model->hasMany['TestModel6']['fields']);
  1381. $this->Model->hasMany['TestModel6']['fields'] = array('id', 'name');
  1382. $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
  1383. $queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`'));
  1384. $resultSet = null;
  1385. $null = null;
  1386. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1387. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1388. $this->assertRegExp('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`name`, `TestModel6`\.`test_model5_id`\s+/', $result);
  1389. $this->assertRegExp('/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
  1390. $this->assertRegExp('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
  1391. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1392. $this->assertRegExp('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/', $result);
  1393. $this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
  1394. $this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1395. unset($this->Model->hasMany['TestModel6']['fields']);
  1396. $this->Model->hasMany['TestModel6']['fields'] = array('test_model5_id', 'name');
  1397. $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
  1398. $queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`'));
  1399. $resultSet = null;
  1400. $null = null;
  1401. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1402. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1403. $this->assertRegExp('/^SELECT\s+`TestModel6`\.`test_model5_id`, `TestModel6`\.`name`\s+/', $result);
  1404. $this->assertRegExp('/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
  1405. $this->assertRegExp('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
  1406. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1407. $this->assertRegExp('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/', $result);
  1408. $this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
  1409. $this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1410. unset($this->Model->hasMany['TestModel6']['fields']);
  1411. }
  1412. /**
  1413. * test generateAssociationQuery with a hasMany and an aggregate function.
  1414. *
  1415. * @return void
  1416. */
  1417. public function testGenerateAssociationQueryHasManyAndAggregateFunction() {
  1418. $this->Model = new TestModel5();
  1419. $this->Model->schema();
  1420. $this->_buildRelatedModels($this->Model);
  1421. $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
  1422. $queryData = array('fields' => array('MIN(`TestModel5`.`test_model4_id`)'));
  1423. $resultSet = null;
  1424. $null = null;
  1425. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1426. $this->Model->recursive = 0;
  1427. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, $params['type'], $params['assoc'], $params['assocData'], $queryData, false, $resultSet);
  1428. $this->assertRegExp('/^SELECT\s+MIN\(`TestModel5`\.`test_model4_id`\)\s+FROM/', $result);
  1429. }
  1430. /**
  1431. * testGenerateAssociationQueryHasAndBelongsToMany method
  1432. *
  1433. * @return void
  1434. */
  1435. public function testGenerateAssociationQueryHasAndBelongsToMany() {
  1436. $this->Model = new TestModel4();
  1437. $this->Model->schema();
  1438. $this->_buildRelatedModels($this->Model);
  1439. $binding = array('type' => 'hasAndBelongsToMany', 'model' => 'TestModel7');
  1440. $queryData = array();
  1441. $resultSet = null;
  1442. $null = null;
  1443. $params = $this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1444. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1445. $assocTable = $this->Dbo->fullTableName($this->Model->TestModel4TestModel7, true, true);
  1446. $this->assertRegExp('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/', $result);
  1447. $this->assertRegExp('/\s+FROM\s+\S+`test_model7` AS `TestModel7`\s+JOIN\s+' . $assocTable . '/', $result);
  1448. $this->assertRegExp('/\s+ON\s+\(`TestModel4TestModel7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}\s+AND/', $result);
  1449. $this->assertRegExp('/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)/', $result);
  1450. $this->assertRegExp('/WHERE\s+(?:\()?1 = 1(?:\))?\s*$/', $result);
  1451. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1452. $this->assertRegExp('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result);
  1453. $this->assertRegExp('/\s+FROM\s+\S+`test_model4` AS `TestModel4`\s+WHERE/', $result);
  1454. $this->assertRegExp('/\s+WHERE\s+(?:\()?1 = 1(?:\))?\s*$/', $result);
  1455. }
  1456. /**
  1457. * testGenerateAssociationQueryHasAndBelongsToManyWithConditions method
  1458. *
  1459. * @return void
  1460. */
  1461. public function testGenerateAssociationQueryHasAndBelongsToManyWithConditions() {
  1462. $this->Model = new TestModel4();
  1463. $this->Model->schema();
  1464. $this->_buildRelatedModels($this->Model);
  1465. $binding = array('type' => 'hasAndBelongsToMany', 'model' => 'TestModel7');
  1466. $queryData = array('conditions' => array('TestModel4.name !=' => 'mariano'));
  1467. $resultSet = null;
  1468. $null = null;
  1469. $params = $this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1470. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1471. $this->assertRegExp('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/', $result);
  1472. $this->assertRegExp('/\s+FROM\s+\S+`test_model7`\s+AS\s+`TestModel7`\s+JOIN\s+\S+`test_model4_test_model7`\s+AS\s+`TestModel4TestModel7`/', $result);
  1473. $this->assertRegExp('/\s+ON\s+\(`TestModel4TestModel7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}/', $result);
  1474. $this->assertRegExp('/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)\s+WHERE\s+/', $result);
  1475. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1476. $this->assertRegExp('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result);
  1477. $this->assertRegExp('/\s+FROM\s+\S+`test_model4` AS `TestModel4`\s+WHERE\s+(?:\()?`TestModel4`.`name`\s+!=\s+\'mariano\'(?:\))?\s*$/', $result);
  1478. }
  1479. /**
  1480. * testGenerateAssociationQueryHasAndBelongsToManyWithOffsetAndLimit method
  1481. *
  1482. * @return void
  1483. */
  1484. public function testGenerateAssociationQueryHasAndBelongsToManyWithOffsetAndLimit() {
  1485. $this->Model = new TestModel4();
  1486. $this->Model->schema();
  1487. $this->_buildRelatedModels($this->Model);
  1488. $__backup = $this->Model->hasAndBelongsToMany['TestModel7'];
  1489. $this->Model->hasAndBelongsToMany['TestModel7']['offset'] = 2;
  1490. $this->Model->hasAndBelongsToMany['TestModel7']['limit'] = 5;
  1491. $binding = array('type' => 'hasAndBelongsToMany', 'model' => 'TestModel7');
  1492. $queryData = array();
  1493. $resultSet = null;
  1494. $null = null;
  1495. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1496. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1497. $this->assertRegExp('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/', $result);
  1498. $this->assertRegExp('/\s+FROM\s+\S+`test_model7`\s+AS\s+`TestModel7`\s+JOIN\s+\S+`test_model4_test_model7`\s+AS\s+`TestModel4TestModel7`/', $result);
  1499. $this->assertRegExp('/\s+ON\s+\(`TestModel4TestModel7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}\s+/', $result);
  1500. $this->assertRegExp('/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)\s+WHERE\s+/', $result);
  1501. $this->assertRegExp('/\s+(?:\()?1\s+=\s+1(?:\))?\s*\s+LIMIT 2,\s*5\s*$/', $result);
  1502. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1503. $this->assertRegExp('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result);
  1504. $this->assertRegExp('/\s+FROM\s+\S+`test_model4` AS `TestModel4`\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1505. $this->Model->hasAndBelongsToMany['TestModel7'] = $__backup;
  1506. }
  1507. /**
  1508. * testGenerateAssociationQueryHasAndBelongsToManyWithPageAndLimit method
  1509. *
  1510. * @return void
  1511. */
  1512. public function testGenerateAssociationQueryHasAndBelongsToManyWithPageAndLimit() {
  1513. $this->Model = new TestModel4();
  1514. $this->Model->schema();
  1515. $this->_buildRelatedModels($this->Model);
  1516. $__backup = $this->Model->hasAndBelongsToMany['TestModel7'];
  1517. $this->Model->hasAndBelongsToMany['TestModel7']['page'] = 2;
  1518. $this->Model->hasAndBelongsToMany['TestModel7']['limit'] = 5;
  1519. $binding = array('type' => 'hasAndBelongsToMany', 'model' => 'TestModel7');
  1520. $queryData = array();
  1521. $resultSet = null;
  1522. $null = null;
  1523. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1524. $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1525. $this->assertRegExp('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/', $result);
  1526. $this->assertRegExp('/\s+FROM\s+\S+`test_model7`\s+AS\s+`TestModel7`\s+JOIN\s+\S+`test_model4_test_model7`\s+AS\s+`TestModel4TestModel7`/', $result);
  1527. $this->assertRegExp('/\s+ON\s+\(`TestModel4TestModel7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}/', $result);
  1528. $this->assertRegExp('/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)\s+WHERE\s+/', $result);
  1529. $this->assertRegExp('/\s+(?:\()?1\s+=\s+1(?:\))?\s*\s+LIMIT 5,\s*5\s*$/', $result);
  1530. $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1531. $this->assertRegExp('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result);
  1532. $this->assertRegExp('/\s+FROM\s+\S+`test_model4` AS `TestModel4`\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1533. $this->Model->hasAndBelongsToMany['TestModel7'] = $__backup;
  1534. }
  1535. /**
  1536. * testSelectDistict method
  1537. *
  1538. * @return void
  1539. */
  1540. public function testSelectDistict() {
  1541. $this->Model = new TestModel4();
  1542. $result = $this->Dbo->fields($this->Model, 'Vendor', "DISTINCT Vendor.id, Vendor.name");
  1543. $expected = array('DISTINCT `Vendor`.`id`', '`Vendor`.`name`');
  1544. $this->assertEquals($expected, $result);
  1545. }
  1546. /**
  1547. * testStringConditionsParsing method
  1548. *
  1549. * @return void
  1550. */
  1551. public function testStringConditionsParsing() {
  1552. $result = $this->Dbo->conditions("ProjectBid.project_id = Project.id");
  1553. $expected = " WHERE `ProjectBid`.`project_id` = `Project`.`id`";
  1554. $this->assertEquals($expected, $result);
  1555. $result = $this->Dbo->conditions("Candy.name LIKE 'a' AND HardCandy.name LIKE 'c'");
  1556. $expected = " WHERE `Candy`.`name` LIKE 'a' AND `HardCandy`.`name` LIKE 'c'";
  1557. $this->assertEquals($expected, $result);
  1558. $result = $this->Dbo->conditions("HardCandy.name LIKE 'a' AND Candy.name LIKE 'c'");
  1559. $expected = " WHERE `HardCandy`.`name` LIKE 'a' AND `Candy`.`name` LIKE 'c'";
  1560. $this->assertEquals($expected, $result);
  1561. $result = $this->Dbo->conditions("Post.title = '1.1'");
  1562. $expected = " WHERE `Post`.`title` = '1.1'";
  1563. $this->assertEquals($expected, $result);
  1564. $result = $this->Dbo->conditions("User.id != 0 AND User.user LIKE '%arr%'");
  1565. $expected = " WHERE `User`.`id` != 0 AND `User`.`user` LIKE '%arr%'";
  1566. $this->assertEquals($expected, $result);
  1567. $result = $this->Dbo->conditions("SUM(Post.comments_count) > 500");
  1568. $expected = " WHERE SUM(`Post`.`comments_count`) > 500";
  1569. $this->assertEquals($expected, $result);
  1570. $result = $this->Dbo->conditions("(Post.created < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(Post.created), MONTH(Post.created)");
  1571. $expected = " WHERE (`Post`.`created` < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(`Post`.`created`), MONTH(`Post`.`created`)";
  1572. $this->assertEquals($expected, $result);
  1573. $result = $this->Dbo->conditions("score BETWEEN 90.1 AND 95.7");
  1574. $expected = " WHERE score BETWEEN 90.1 AND 95.7";
  1575. $this->assertEquals($expected, $result);
  1576. $result = $this->Dbo->conditions(array('score' => array(2 => 1, 2, 10)));
  1577. $expected = " WHERE score IN (1, 2, 10)";
  1578. $this->assertEquals($expected, $result);
  1579. $result = $this->Dbo->conditions("Aro.rght = Aro.lft + 1.1");
  1580. $expected = " WHERE `Aro`.`rght` = `Aro`.`lft` + 1.1";
  1581. $this->assertEquals($expected, $result);
  1582. $result = $this->Dbo->conditions("(Post.created < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(Post.created), MONTH(Post.created)");
  1583. $expected = " WHERE (`Post`.`created` < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(`Post`.`created`), MONTH(`Post`.`created`)";
  1584. $this->assertEquals($expected, $result);
  1585. $result = $this->Dbo->conditions('Sportstaette.sportstaette LIKE "%ru%" AND Sportstaette.sportstaettenart_id = 2');
  1586. $expected = ' WHERE `Sportstaette`.`sportstaette` LIKE "%ru%" AND `Sportstaette`.`sportstaettenart_id` = 2';
  1587. $this->assertRegExp('/\s*WHERE\s+`Sportstaette`\.`sportstaette`\s+LIKE\s+"%ru%"\s+AND\s+`Sports/', $result);
  1588. $this->assertEquals($expected, $result);
  1589. $result = $this->Dbo->conditions('Sportstaette.sportstaettenart_id = 2 AND Sportstaette.sportstaette LIKE "%ru%"');
  1590. $expected = ' WHERE `Sportstaette`.`sportstaettenart_id` = 2 AND `Sportstaette`.`sportstaette` LIKE "%ru%"';
  1591. $this->assertEquals($expected, $result);
  1592. $result = $this->Dbo->conditions('SUM(Post.comments_count) > 500 AND NOT Post.title IS NULL AND NOT Post.extended_title IS NULL');
  1593. $expected = ' WHERE SUM(`Post`.`comments_count`) > 500 AND NOT `Post`.`title` IS NULL AND NOT `Post`.`extended_title` IS NULL';
  1594. $this->assertEquals($expected, $result);
  1595. $result = $this->Dbo->conditions('NOT Post.title IS NULL AND NOT Post.extended_title IS NULL AND SUM(Post.comments_count) > 500');
  1596. $expected = ' WHERE NOT `Post`.`title` IS NULL AND NOT `Post`.`extended_title` IS NULL AND SUM(`Post`.`comments_count`) > 500';
  1597. $this->assertEquals($expected, $result);
  1598. $result = $this->Dbo->conditions('NOT Post.extended_title IS NULL AND NOT Post.title IS NULL AND Post.title != "" AND SPOON(SUM(Post.comments_count) + 1.1) > 500');
  1599. $expected = ' WHERE NOT `Post`.`extended_title` IS NULL AND NOT `Post`.`title` IS NULL AND `Post`.`title` != "" AND SPOON(SUM(`Post`.`comments_count`) + 1.1) > 500';
  1600. $this->assertEquals($expected, $result);
  1601. $result = $this->Dbo->conditions('NOT Post.title_extended IS NULL AND NOT Post.title IS NULL AND Post.title_extended != Post.title');
  1602. $expected = ' WHERE NOT `Post`.`title_extended` IS NULL AND NOT `Post`.`title` IS NULL AND `Post`.`title_extended` != `Post`.`title`';
  1603. $this->assertEquals($expected, $result);
  1604. $result = $this->Dbo->conditions("Comment.id = 'a'");
  1605. $expected = " WHERE `Comment`.`id` = 'a'";
  1606. $this->assertEquals($expected, $result);
  1607. $result = $this->Dbo->conditions("lower(Article.title) LIKE 'a%'");
  1608. $expected = " WHERE lower(`Article`.`title`) LIKE 'a%'";
  1609. $this->assertEquals($expected, $result);
  1610. $result = $this->Dbo->conditions('((MATCH(Video.title) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 2) + (MATCH(Video.description) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 0.4) + (MATCH(Video.tags) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 1.5))');
  1611. $expected = ' WHERE ((MATCH(`Video`.`title`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 2) + (MATCH(`Video`.`description`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 0.4) + (MATCH(`Video`.`tags`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 1.5))';
  1612. $this->assertEquals($expected, $result);
  1613. $result = $this->Dbo->conditions('DATEDIFF(NOW(),Article.published) < 1 && Article.live=1');
  1614. $expected = " WHERE DATEDIFF(NOW(),`Article`.`published`) < 1 && `Article`.`live`=1";
  1615. $this->assertEquals($expected, $result);
  1616. $result = $this->Dbo->conditions('file = "index.html"');
  1617. $expected = ' WHERE file = "index.html"';
  1618. $this->assertEquals($expected, $result);
  1619. $result = $this->Dbo->conditions("file = 'index.html'");
  1620. $expected = " WHERE file = 'index.html'";
  1621. $this->assertEquals($expected, $result);
  1622. $letter = $letter = 'd.a';
  1623. $conditions = array('Company.name like ' => $letter . '%');
  1624. $result = $this->Dbo->conditions($conditions);
  1625. $expected = " WHERE `Company`.`name` like 'd.a%'";
  1626. $this->assertEquals($expected, $result);
  1627. $conditions = array('Artist.name' => 'JUDY and MARY');
  1628. $result = $this->Dbo->conditions($conditions);
  1629. $expected = " WHERE `Artist`.`name` = 'JUDY and MARY'";
  1630. $this->assertEquals($expected, $result);
  1631. $conditions = array('Artist.name' => 'JUDY AND MARY');
  1632. $result = $this->Dbo->conditions($conditions);
  1633. $expected = " WHERE `Artist`.`name` = 'JUDY AND MARY'";
  1634. $this->assertEquals($expected, $result);
  1635. $conditions = array('Company.name similar to ' => 'a word');
  1636. $result = $this->Dbo->conditions($conditions);
  1637. $expected = " WHERE `Company`.`name` similar to 'a word'";
  1638. $this->assertEquals($expected, $result);
  1639. }
  1640. /**
  1641. * testQuotesInStringConditions method
  1642. *
  1643. * @return void
  1644. */
  1645. public function testQuotesInStringConditions() {
  1646. $result = $this->Dbo->conditions('Member.email = \'mariano@cricava.com\'');
  1647. $expected = ' WHERE `Member`.`email` = \'mariano@cricava.com\'';
  1648. $this->assertEquals($expected, $result);
  1649. $result = $this->Dbo->conditions('Member.email = "mariano@cricava.com"');
  1650. $expected = ' WHERE `Member`.`email` = "mariano@cricava.com"';
  1651. $this->assertEquals($expected, $result);
  1652. $result = $this->Dbo->conditions('Member.email = \'mariano@cricava.com\' AND Member.user LIKE \'mariano.iglesias%\'');
  1653. $expected = ' WHERE `Member`.`email` = \'mariano@cricava.com\' AND `Member`.`user` LIKE \'mariano.iglesias%\'';
  1654. $this->assertEquals($expected, $result);
  1655. $result = $this->Dbo->conditions('Member.email = "mariano@cricava.com" AND Member.user LIKE "mariano.iglesias%"');
  1656. $expected = ' WHERE `Member`.`email` = "mariano@cricava.com" AND `Member`.`user` LIKE "mariano.iglesias%"';
  1657. $this->assertEquals($expected, $result);
  1658. }
  1659. /**
  1660. * testParenthesisInStringConditions method
  1661. *
  1662. * @return void
  1663. */
  1664. public function testParenthesisInStringConditions() {
  1665. $result = $this->Dbo->conditions('Member.name = \'(lu\'');
  1666. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(lu\'$/', $result);
  1667. $result = $this->Dbo->conditions('Member.name = \')lu\'');
  1668. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\)lu\'$/', $result);
  1669. $result = $this->Dbo->conditions('Member.name = \'va(lu\'');
  1670. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\'$/', $result);
  1671. $result = $this->Dbo->conditions('Member.name = \'va)lu\'');
  1672. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\)lu\'$/', $result);
  1673. $result = $this->Dbo->conditions('Member.name = \'va(lu)\'');
  1674. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)\'$/', $result);
  1675. $result = $this->Dbo->conditions('Member.name = \'va(lu)e\'');
  1676. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)e\'$/', $result);
  1677. $result = $this->Dbo->conditions('Member.name = \'(mariano)\'');
  1678. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)\'$/', $result);
  1679. $result = $this->Dbo->conditions('Member.name = \'(mariano)iglesias\'');
  1680. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)iglesias\'$/', $result);
  1681. $result = $this->Dbo->conditions('Member.name = \'(mariano) iglesias\'');
  1682. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\) iglesias\'$/', $result);
  1683. $result = $this->Dbo->conditions('Member.name = \'(mariano word) iglesias\'');
  1684. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano word\) iglesias\'$/', $result);
  1685. $result = $this->Dbo->conditions('Member.name = \'(mariano.iglesias)\'');
  1686. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\)\'$/', $result);
  1687. $result = $this->Dbo->conditions('Member.name = \'Mariano Iglesias (mariano.iglesias)\'');
  1688. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\)\'$/', $result);
  1689. $result = $this->Dbo->conditions('Member.name = \'Mariano Iglesias (mariano.iglesias) CakePHP\'');
  1690. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\) CakePHP\'$/', $result);
  1691. $result = $this->Dbo->conditions('Member.name = \'(mariano.iglesias) CakePHP\'');
  1692. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\) CakePHP\'$/', $result);
  1693. }
  1694. /**
  1695. * testParenthesisInArrayConditions method
  1696. *
  1697. * @return void
  1698. */
  1699. public function testParenthesisInArrayConditions() {
  1700. $result = $this->Dbo->conditions(array('Member.name' => '(lu'));
  1701. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(lu\'$/', $result);
  1702. $result = $this->Dbo->conditions(array('Member.name' => ')lu'));
  1703. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\)lu\'$/', $result);
  1704. $result = $this->Dbo->conditions(array('Member.name' => 'va(lu'));
  1705. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\'$/', $result);
  1706. $result = $this->Dbo->conditions(array('Member.name' => 'va)lu'));
  1707. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\)lu\'$/', $result);
  1708. $result = $this->Dbo->conditions(array('Member.name' => 'va(lu)'));
  1709. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)\'$/', $result);
  1710. $result = $this->Dbo->conditions(array('Member.name' => 'va(lu)e'));
  1711. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)e\'$/', $result);
  1712. $result = $this->Dbo->conditions(array('Member.name' => '(mariano)'));
  1713. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)\'$/', $result);
  1714. $result = $this->Dbo->conditions(array('Member.name' => '(mariano)iglesias'));
  1715. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)iglesias\'$/', $result);
  1716. $result = $this->Dbo->conditions(array('Member.name' => '(mariano) iglesias'));
  1717. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\) iglesias\'$/', $result);
  1718. $result = $this->Dbo->conditions(array('Member.name' => '(mariano word) iglesias'));
  1719. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano word\) iglesias\'$/', $result);
  1720. $result = $this->Dbo->conditions(array('Member.name' => '(mariano.iglesias)'));
  1721. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\)\'$/', $result);
  1722. $result = $this->Dbo->conditions(array('Member.name' => 'Mariano Iglesias (mariano.iglesias)'));
  1723. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\)\'$/', $result);
  1724. $result = $this->Dbo->conditions(array('Member.name' => 'Mariano Iglesias (mariano.iglesias) CakePHP'));
  1725. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\) CakePHP\'$/', $result);
  1726. $result = $this->Dbo->conditions(array('Member.name' => '(mariano.iglesias) CakePHP'));
  1727. $this->assertRegExp('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\) CakePHP\'$/', $result);
  1728. }
  1729. /**
  1730. * testArrayConditionsParsing method
  1731. *
  1732. * @return void
  1733. */
  1734. public function testArrayConditionsParsing() {
  1735. $this->loadFixtures('Post', 'Author');
  1736. $result = $this->Dbo->conditions(array('Stereo.type' => 'in dash speakers'));
  1737. $this->assertRegExp("/^\s+WHERE\s+`Stereo`.`type`\s+=\s+'in dash speakers'/", $result);
  1738. $result = $this->Dbo->conditions(array('Candy.name LIKE' => 'a', 'HardCandy.name LIKE' => 'c'));
  1739. $this->assertRegExp("/^\s+WHERE\s+`Candy`.`name` LIKE\s+'a'\s+AND\s+`HardCandy`.`name`\s+LIKE\s+'c'/", $result);
  1740. $result = $this->Dbo->conditions(array('HardCandy.name LIKE' => 'a', 'Candy.name LIKE' => 'c'));
  1741. $expected = " WHERE `HardCandy`.`name` LIKE 'a' AND `Candy`.`name` LIKE 'c'";
  1742. $this->assertEquals($expected, $result);
  1743. $result = $this->Dbo->conditions(array('HardCandy.name LIKE' => 'a%', 'Candy.name LIKE' => '%c%'));
  1744. $expected = " WHERE `HardCandy`.`name` LIKE 'a%' AND `Candy`.`name` LIKE '%c%'";
  1745. $this->assertEquals($expected, $result);
  1746. $result = $this->Dbo->conditions(array('HardCandy.name LIKE' => 'to be or%', 'Candy.name LIKE' => '%not to be%'));
  1747. $expected = " WHERE `HardCandy`.`name` LIKE 'to be or%' AND `Candy`.`name` LIKE '%not to be%'";
  1748. $this->assertEquals($expected, $result);
  1749. $result = $this->Dbo->conditions(array('score BETWEEN ? AND ?' => array(90.1, 95.7)));
  1750. $expected = " WHERE `score` BETWEEN 90.1 AND 95.7";
  1751. $this->assertEquals($expected, $result);
  1752. $result = $this->Dbo->conditions(array('Post.title' => 1.1));
  1753. $expected = " WHERE `Post`.`title` = 1.1";
  1754. $this->assertEquals($expected, $result);
  1755. $result = $this->Dbo->conditions(array('Post.title' => 1.1), true, true, new Post());
  1756. $expected = " WHERE `Post`.`title` = '1.1'";
  1757. $this->assertEquals($expected, $result);
  1758. $result = $this->Dbo->conditions(array('SUM(Post.comments_count) >' => '500'));
  1759. $expected = " WHERE SUM(`Post`.`comments_count`) > '500'";
  1760. $this->assertEquals($expected, $result);
  1761. $result = $this->Dbo->conditions(array('MAX(Post.rating) >' => '50'));
  1762. $expected = " WHERE MAX(`Post`.`rating`) > '50'";
  1763. $this->assertEquals($expected, $result);
  1764. $result = $this->Dbo->conditions(array('lower(Article.title)' => 'secrets'));
  1765. $expected = " WHERE lower(`Article`.`title`) = 'secrets'";
  1766. $this->assertEquals($expected, $result);
  1767. $result = $this->Dbo->conditions(array('title LIKE' => '%hello'));
  1768. $expected = " WHERE `title` LIKE '%hello'";
  1769. $this->assertEquals($expected, $result);
  1770. $result = $this->Dbo->conditions(array('Post.name' => 'mad(g)ik'));
  1771. $expected = " WHERE `Post`.`name` = 'mad(g)ik'";
  1772. $this->assertEquals($expected, $result);
  1773. $result = $this->Dbo->conditions(array('score' => array(1, 2, 10)));
  1774. $expected = " WHERE score IN (1, 2, 10)";
  1775. $this->assertEquals($expected, $result);
  1776. $result = $this->Dbo->conditions(array('score' => array()));
  1777. $expected = " WHERE `score` IS NULL";
  1778. $this->assertEquals($expected, $result);
  1779. $result = $this->Dbo->conditions(array('score !=' => array()));
  1780. $expected = " WHERE `score` IS NOT NULL";
  1781. $this->assertEquals($expected, $result);
  1782. $result = $this->Dbo->conditions(array('score !=' => '20'));
  1783. $expected = " WHERE `score` != '20'";
  1784. $this->assertEquals($expected, $result);
  1785. $result = $this->Dbo->conditions(array('score >' => '20'));
  1786. $expected = " WHERE `score` > '20'";
  1787. $this->assertEquals($expected, $result);
  1788. $result = $this->Dbo->conditions(array('client_id >' => '20'), true, true, new TestModel());
  1789. $expected = " WHERE `client_id` > 20";
  1790. $this->assertEquals($expected, $result);
  1791. $result = $this->Dbo->conditions(array('OR' => array(
  1792. array('User.user' => 'mariano'),
  1793. array('User.user' => 'nate')
  1794. )));
  1795. $expected = " WHERE ((`User`.`user` = 'mariano') OR (`User`.`user` = 'nate'))";
  1796. $this->assertEquals($expected, $result);
  1797. $result = $this->Dbo->conditions(array('or' => array(
  1798. 'score BETWEEN ? AND ?' => array('4', '5'), 'rating >' => '20'
  1799. )));
  1800. $expected = " WHERE ((`score` BETWEEN '4' AND '5') OR (`rating` > '20'))";
  1801. $this->assertEquals($expected, $result);
  1802. $result = $this->Dbo->conditions(array('or' => array(
  1803. 'score BETWEEN ? AND ?' => array('4', '5'), array('score >' => '20')
  1804. )));
  1805. $expected = " WHERE ((`score` BETWEEN '4' AND '5') OR (`score` > '20'))";
  1806. $this->assertEquals($expected, $result);
  1807. $result = $this->Dbo->conditions(array('and' => array(
  1808. 'score BETWEEN ? AND ?' => array('4', '5'), array('score >' => '20')
  1809. )));
  1810. $expected = " WHERE ((`score` BETWEEN '4' AND '5') AND (`score` > '20'))";
  1811. $this->assertEquals($expected, $result);
  1812. $result = $this->Dbo->conditions(array(
  1813. 'published' => 1, 'or' => array('score >' => '2', array('score >' => '20'))
  1814. ));
  1815. $expected = " WHERE `published` = 1 AND ((`score` > '2') OR (`score` > '20'))";
  1816. $this->assertEquals($expected, $result);
  1817. $result = $this->Dbo->conditions(array(array('Project.removed' => false)));
  1818. $expected = " WHERE `Project`.`removed` = '0'";
  1819. $this->assertEquals($expected, $result);
  1820. $result = $this->Dbo->conditions(array(array('Project.removed' => true)));
  1821. $expected = " WHERE `Project`.`removed` = '1'";
  1822. $this->assertEquals($expected, $result);
  1823. $result = $this->Dbo->conditions(array(array('Project.removed' => null)));
  1824. $expected = " WHERE `Project`.`removed` IS NULL";
  1825. $this->assertEquals($expected, $result);
  1826. $result = $this->Dbo->conditions(array(array('Project.removed !=' => null)));
  1827. $expected = " WHERE `Project`.`removed` IS NOT NULL";
  1828. $this->assertEquals($expected, $result);
  1829. $result = $this->Dbo->conditions(array('(Usergroup.permissions) & 4' => 4));
  1830. $expected = " WHERE (`Usergroup`.`permissions`) & 4 = 4";
  1831. $this->assertEquals($expected, $result);
  1832. $result = $this->Dbo->conditions(array('((Usergroup.permissions) & 4)' => 4));
  1833. $expected = " WHERE ((`Usergroup`.`permissions`) & 4) = 4";
  1834. $this->assertEquals($expected, $result);
  1835. $result = $this->Dbo->conditions(array('Post.modified >=' => 'DATE_SUB(NOW(), INTERVAL 7 DAY)'));
  1836. $expected = " WHERE `Post`.`modified` >= 'DATE_SUB(NOW(), INTERVAL 7 DAY)'";
  1837. $this->assertEquals($expected, $result);
  1838. $result = $this->Dbo->conditions(array('Post.modified >= DATE_SUB(NOW(), INTERVAL 7 DAY)'));
  1839. $expected = " WHERE `Post`.`modified` >= DATE_SUB(NOW(), INTERVAL 7 DAY)";
  1840. $this->assertEquals($expected, $result);
  1841. $result = $this->Dbo->conditions(array(
  1842. 'NOT' => array('Course.id' => null, 'Course.vet' => 'N', 'level_of_education_id' => array(912,999)),
  1843. 'Enrollment.yearcompleted >' => '0')
  1844. );
  1845. $this->assertRegExp('/^\s*WHERE\s+\(NOT\s+\(`Course`\.`id` IS NULL\)\s+AND NOT\s+\(`Course`\.`vet`\s+=\s+\'N\'\)\s+AND NOT\s+\(level_of_education_id IN \(912, 999\)\)\)\s+AND\s+`Enrollment`\.`yearcompleted`\s+>\s+\'0\'\s*$/', $result);
  1846. $result = $this->Dbo->conditions(array('id <>' => '8'));
  1847. $this->assertRegExp('/^\s*WHERE\s+`id`\s+<>\s+\'8\'\s*$/', $result);
  1848. $result = $this->Dbo->conditions(array('TestModel.field =' => 'gribe$@()lu'));
  1849. $expected = " WHERE `TestModel`.`field` = 'gribe$@()lu'";
  1850. $this->assertEquals($expected, $result);
  1851. $conditions['NOT'] = array('Listing.expiration BETWEEN ? AND ?' => array("1", "100"));
  1852. $conditions[0]['OR'] = array(
  1853. "Listing.title LIKE" => "%term%",
  1854. "Listing.description LIKE" => "%term%"
  1855. );
  1856. $conditions[1]['OR'] = array(
  1857. "Listing.title LIKE" => "%term_2%",
  1858. "Listing.description LIKE" => "%term_2%"
  1859. );
  1860. $result = $this->Dbo->conditions($conditions);
  1861. $expected = " WHERE NOT (`Listing`.`expiration` BETWEEN '1' AND '100') AND" .
  1862. " ((`Listing`.`title` LIKE '%term%') OR (`Listing`.`description` LIKE '%term%')) AND" .
  1863. " ((`Listing`.`title` LIKE '%term_2%') OR (`Listing`.`description` LIKE '%term_2%'))";
  1864. $this->assertEquals($expected, $result);
  1865. $result = $this->Dbo->conditions(array('MD5(CONCAT(Reg.email,Reg.id))' => 'blah'));
  1866. $expected = " WHERE MD5(CONCAT(`Reg`.`email`,`Reg`.`id`)) = 'blah'";
  1867. $this->assertEquals($expected, $result);
  1868. $result = $this->Dbo->conditions(array(
  1869. 'MD5(CONCAT(Reg.email,Reg.id))' => array('blah', 'blahblah')
  1870. ));
  1871. $expected = " WHERE MD5(CONCAT(`Reg`.`email`,`Reg`.`id`)) IN ('blah', 'blahblah')";
  1872. $this->assertEquals($expected, $result);
  1873. $conditions = array('id' => array(2, 5, 6, 9, 12, 45, 78, 43, 76));
  1874. $result = $this->Dbo->conditions($conditions);
  1875. $expected = " WHERE id IN (2, 5, 6, 9, 12, 45, 78, 43, 76)";
  1876. $this->assertEquals($expected, $result);
  1877. $conditions = array('title' => 'user(s)');
  1878. $result = $this->Dbo->conditions($conditions);
  1879. $expected = " WHERE `title` = 'user(s)'";
  1880. $this->assertEquals($expected, $result);
  1881. $conditions = array('title' => 'user(s) data');
  1882. $result = $this->Dbo->conditions($conditions);
  1883. $expected = " WHERE `title` = 'user(s) data'";
  1884. $this->assertEquals($expected, $result);
  1885. $conditions = array('title' => 'user(s,arg) data');
  1886. $result = $this->Dbo->conditions($conditions);
  1887. $expected = " WHERE `title` = 'user(s,arg) data'";
  1888. $this->assertEquals($expected, $result);
  1889. $result = $this->Dbo->conditions(array("Book.book_name" => 'Java(TM)'));
  1890. $expected = " WHERE `Book`.`book_name` = 'Java(TM)'";
  1891. $this->assertEquals($expected, $result);
  1892. $result = $this->Dbo->conditions(array("Book.book_name" => 'Java(TM) '));
  1893. $expected = " WHERE `Book`.`book_name` = 'Java(TM) '";
  1894. $this->assertEquals($expected, $result);
  1895. $result = $this->Dbo->conditions(array("Book.id" => 0));
  1896. $expected = " WHERE `Book`.`id` = 0";
  1897. $this->assertEquals($expected, $result);
  1898. $result = $this->Dbo->conditions(array("Book.id" => null));
  1899. $expected = " WHERE `Book`.`id` IS NULL";
  1900. $this->assertEquals($expected, $result);
  1901. $conditions = array('MysqlModel.id' => '');
  1902. $result = $this->Dbo->conditions($conditions, true, true, $this->model);
  1903. $expected = " WHERE `MysqlModel`.`id` IS NULL";
  1904. $this->assertEquals($expected, $result);
  1905. $result = $this->Dbo->conditions(array('Listing.beds >=' => 0));
  1906. $expected = " WHERE `Listing`.`beds` >= 0";
  1907. $this->assertEquals($expected, $result);
  1908. $result = $this->Dbo->conditions(array(
  1909. 'ASCII(SUBSTRING(keyword, 1, 1)) BETWEEN ? AND ?' => array(65, 90)
  1910. ));
  1911. $expected = ' WHERE ASCII(SUBSTRING(keyword, 1, 1)) BETWEEN 65 AND 90';
  1912. $this->assertEquals($expected, $result);
  1913. $result = $this->Dbo->conditions(array('or' => array(
  1914. '? BETWEEN Model.field1 AND Model.field2' => '2009-03-04'
  1915. )));
  1916. $expected = " WHERE '2009-03-04' BETWEEN Model.field1 AND Model.field2";
  1917. $this->assertEquals($expected, $result);
  1918. }
  1919. /**
  1920. * testArrayConditionsParsingComplexKeys method
  1921. *
  1922. * @return void
  1923. */
  1924. public function testArrayConditionsParsingComplexKeys() {
  1925. $result = $this->Dbo->conditions(array(
  1926. 'CAST(Book.created AS DATE)' => '2008-08-02'
  1927. ));
  1928. $expected = " WHERE CAST(`Book`.`created` AS DATE) = '2008-08-02'";
  1929. $this->assertEquals($expected, $result);
  1930. $result = $this->Dbo->conditions(array(
  1931. 'CAST(Book.created AS DATE) <=' => '2008-08-02'
  1932. ));
  1933. $expected = " WHERE CAST(`Book`.`created` AS DATE) <= '2008-08-02'";
  1934. $this->assertEquals($expected, $result);
  1935. $result = $this->Dbo->conditions(array(
  1936. '(Stats.clicks * 100) / Stats.views >' => 50
  1937. ));
  1938. $expected = " WHERE (`Stats`.`clicks` * 100) / `Stats`.`views` > 50";
  1939. $this->assertEquals($expected, $result);
  1940. }
  1941. /**
  1942. * testMixedConditionsParsing method
  1943. *
  1944. * @return void
  1945. */
  1946. public function testMixedConditionsParsing() {
  1947. $conditions[] = 'User.first_name = \'Firstname\'';
  1948. $conditions[] = array('User.last_name' => 'Lastname');
  1949. $result = $this->Dbo->conditions($conditions);
  1950. $expected = " WHERE `User`.`first_name` = 'Firstname' AND `User`.`last_name` = 'Lastname'";
  1951. $this->assertEquals($expected, $result);
  1952. $conditions = array(
  1953. 'Thread.project_id' => 5,
  1954. 'Thread.buyer_id' => 14,
  1955. '1=1 GROUP BY Thread.project_id'
  1956. );
  1957. $result = $this->Dbo->conditions($conditions);
  1958. $this->assertRegExp('/^\s*WHERE\s+`Thread`.`project_id`\s*=\s*5\s+AND\s+`Thread`.`buyer_id`\s*=\s*14\s+AND\s+1\s*=\s*1\s+GROUP BY `Thread`.`project_id`$/', $result);
  1959. }
  1960. /**
  1961. * testConditionsOptionalArguments method
  1962. *
  1963. * @return void
  1964. */
  1965. public function testConditionsOptionalArguments() {
  1966. $result = $this->Dbo->conditions( array('Member.name' => 'Mariano'), true, false);
  1967. $this->assertRegExp('/^\s*`Member`.`name`\s*=\s*\'Mariano\'\s*$/', $result);
  1968. $result = $this->Dbo->conditions( array(), true, false);
  1969. $this->assertRegExp('/^\s*1\s*=\s*1\s*$/', $result);
  1970. }
  1971. /**
  1972. * testConditionsWithModel
  1973. *
  1974. * @return void
  1975. */
  1976. public function testConditionsWithModel() {
  1977. $this->Model = new Article2();
  1978. $result = $this->Dbo->conditions(array('Article2.viewed >=' => 0), true, true, $this->Model);
  1979. $expected = " WHERE `Article2`.`viewed` >= 0";
  1980. $this->assertEquals($expected, $result);
  1981. $result = $this->Dbo->conditions(array('Article2.viewed >=' => '0'), true, true, $this->Model);
  1982. $expected = " WHERE `Article2`.`viewed` >= 0";
  1983. $this->assertEquals($expected, $result);
  1984. $result = $this->Dbo->conditions(array('Article2.viewed >=' => '1'), true, true, $this->Model);
  1985. $expected = " WHERE `Article2`.`viewed` >= 1";
  1986. $this->assertEquals($expected, $result);
  1987. $result = $this->Dbo->conditions(array('Article2.rate_sum BETWEEN ? AND ?' => array(0, 10)), true, true, $this->Model);
  1988. $expected = " WHERE `Article2`.`rate_sum` BETWEEN 0 AND 10";
  1989. $this->assertEquals($expected, $result);
  1990. $result = $this->Dbo->conditions(array('Article2.rate_sum BETWEEN ? AND ?' => array('0', '10')), true, true, $this->Model);
  1991. $expected = " WHERE `Article2`.`rate_sum` BETWEEN 0 AND 10";
  1992. $this->assertEquals($expected, $result);
  1993. $result = $this->Dbo->conditions(array('Article2.rate_sum BETWEEN ? AND ?' => array('1', '10')), true, true, $this->Model);
  1994. $expected = " WHERE `Article2`.`rate_sum` BETWEEN 1 AND 10";
  1995. $this->assertEquals($expected, $result);
  1996. }
  1997. /**
  1998. * testFieldParsing method
  1999. *
  2000. * @return void
  2001. */
  2002. public function testFieldParsing() {
  2003. $this->Model = new TestModel();
  2004. $result = $this->Dbo->fields($this->Model, 'Vendor', "Vendor.id, COUNT(Model.vendor_id) AS `Vendor`.`count`");
  2005. $expected = array('`Vendor`.`id`', 'COUNT(`Model`.`vendor_id`) AS `Vendor`.`count`');
  2006. $this->assertEquals($expected, $result);
  2007. $result = $this->Dbo->fields($this->Model, 'Vendor', "`Vendor`.`id`, COUNT(`Model`.`vendor_id`) AS `Vendor`.`count`");
  2008. $expected = array('`Vendor`.`id`', 'COUNT(`Model`.`vendor_id`) AS `Vendor`.`count`');
  2009. $this->assertEquals($expected, $result);
  2010. $result = $this->Dbo->fields($this->Model, 'Post', "CONCAT(REPEAT(' ', COUNT(Parent.name) - 1), Node.name) AS name, Node.created");
  2011. $expected = array("CONCAT(REPEAT(' ', COUNT(`Parent`.`name`) - 1), Node.name) AS name", "`Node`.`created`");
  2012. $this->assertEquals($expected, $result);
  2013. $result = $this->Dbo->fields($this->Model, null, 'round( (3.55441 * fooField), 3 ) AS test');
  2014. $this->assertEquals($result, array('round( (3.55441 * fooField), 3 ) AS test'));
  2015. $result = $this->Dbo->fields($this->Model, null, 'ROUND(`Rating`.`rate_total` / `Rating`.`rate_count`,2) AS rating');
  2016. $this->assertEquals($result, array('ROUND(`Rating`.`rate_total` / `Rating`.`rate_count`,2) AS rating'));
  2017. $result = $this->Dbo->fields($this->Model, null, 'ROUND(Rating.rate_total / Rating.rate_count,2) AS rating');
  2018. $this->assertEquals($result, array('ROUND(Rating.rate_total / Rating.rate_count,2) AS rating'));
  2019. $result = $this->Dbo->fields($this->Model, 'Post', "Node.created, CONCAT(REPEAT(' ', COUNT(Parent.name) - 1), Node.name) AS name");
  2020. $expected = array("`Node`.`created`", "CONCAT(REPEAT(' ', COUNT(`Parent`.`name`) - 1), Node.name) AS name");
  2021. $this->assertEquals($expected, $result);
  2022. $result = $this->Dbo->fields($this->Model, 'Post', "2.2,COUNT(*), SUM(Something.else) as sum, Node.created, CONCAT(REPEAT(' ', COUNT(Parent.name) - 1), Node.name) AS name,Post.title,Post.1,1.1");
  2023. $expected = array(
  2024. '2.2', 'COUNT(*)', 'SUM(`Something`.`else`) as sum', '`Node`.`created`',
  2025. "CONCAT(REPEAT(' ', COUNT(`Parent`.`name`) - 1), Node.name) AS name", '`Post`.`title`', '`Post`.`1`', '1.1'
  2026. );
  2027. $this->assertEquals($expected, $result);
  2028. $result = $this->Dbo->fields($this->Model, null, "(`Provider`.`star_total` / `Provider`.`total_ratings`) as `rating`");
  2029. $expected = array("(`Provider`.`star_total` / `Provider`.`total_ratings`) as `rating`");
  2030. $this->assertEquals($expected, $result);
  2031. $result = $this->Dbo->fields($this->Model, 'Post');
  2032. $expected = array(
  2033. '`Post`.`id`', '`Post`.`client_id`', '`Post`.`name`', '`Post`.`login`',
  2034. '`Post`.`passwd`', '`Post`.`addr_1`', '`Post`.`addr_2`', '`Post`.`zip_code`',
  2035. '`Post`.`city`', '`Post`.`country`', '`Post`.`phone`', '`Post`.`fax`',
  2036. '`Post`.`url`', '`Post`.`email`', '`Post`.`comments`', '`Post`.`last_login`',
  2037. '`Post`.`created`', '`Post`.`updated`'
  2038. );
  2039. $this->assertEquals($expected, $result);
  2040. $result = $this->Dbo->fields($this->Model, 'Other');
  2041. $expected = array(
  2042. '`Other`.`id`', '`Other`.`client_id`', '`Other`.`name`', '`Other`.`login`',
  2043. '`Other`.`passwd`', '`Other`.`addr_1`', '`Other`.`addr_2`', '`Other`.`zip_code`',
  2044. '`Other`.`city`', '`Other`.`country`', '`Other`.`phone`', '`Other`.`fax`',
  2045. '`Other`.`url`', '`Other`.`email`', '`Other`.`comments`', '`Other`.`last_login`',
  2046. '`Other`.`created`', '`Other`.`updated`'
  2047. );
  2048. $this->assertEquals($expected, $result);
  2049. $result = $this->Dbo->fields($this->Model, null, array(), false);
  2050. $expected = array('id', 'client_id', 'name', 'login', 'passwd', 'addr_1', 'addr_2', 'zip_code', 'city', 'country', 'phone', 'fax', 'url', 'email', 'comments', 'last_login', 'created', 'updated');
  2051. $this->assertEquals($expected, $result);
  2052. $result = $this->Dbo->fields($this->Model, null, 'COUNT(*)');
  2053. $expected = array('COUNT(*)');
  2054. $this->assertEquals($expected, $result);
  2055. $result = $this->Dbo->fields($this->Model, null, 'SUM(Thread.unread_buyer) AS ' . $this->Dbo->name('sum_unread_buyer'));
  2056. $expected = array('SUM(`Thread`.`unread_buyer`) AS `sum_unread_buyer`');
  2057. $this->assertEquals($expected, $result);
  2058. $result = $this->Dbo->fields($this->Model, null, 'name, count(*)');
  2059. $expected = array('`TestModel`.`name`', 'count(*)');
  2060. $this->assertEquals($expected, $result);
  2061. $result = $this->Dbo->fields($this->Model, null, 'count(*), name');
  2062. $expected = array('count(*)', '`TestModel`.`name`');
  2063. $this->assertEquals($expected, $result);
  2064. $result = $this->Dbo->fields(
  2065. $this->Model, null, 'field1, field2, field3, count(*), name'
  2066. );
  2067. $expected = array(
  2068. '`TestModel`.`field1`', '`TestModel`.`field2`',
  2069. '`TestModel`.`field3`', 'count(*)', '`TestModel`.`name`'
  2070. );
  2071. $this->assertEquals($expected, $result);
  2072. $result = $this->Dbo->fields($this->Model, null, array('dayofyear(now())'));
  2073. $expected = array('dayofyear(now())');
  2074. $this->assertEquals($expected, $result);
  2075. $result = $this->Dbo->fields($this->Model, null, array('MAX(Model.field) As Max'));
  2076. $expected = array('MAX(`Model`.`field`) As Max');
  2077. $this->assertEquals($expected, $result);
  2078. $result = $this->Dbo->fields($this->Model, null, array('Model.field AS AnotherName'));
  2079. $expected = array('`Model`.`field` AS `AnotherName`');
  2080. $this->assertEquals($expected, $result);
  2081. $result = $this->Dbo->fields($this->Model, null, array('field AS AnotherName'));
  2082. $expected = array('`field` AS `AnotherName`');
  2083. $this->assertEquals($expected, $result);
  2084. $result = $this->Dbo->fields($this->Model, null, array(
  2085. 'TestModel.field AS AnotherName'
  2086. ));
  2087. $expected = array('`TestModel`.`field` AS `AnotherName`');
  2088. $this->assertEquals($expected, $result);
  2089. $result = $this->Dbo->fields($this->Model, 'Foo', array(
  2090. 'id', 'title', '(user_count + discussion_count + post_count) AS score'
  2091. ));
  2092. $expected = array(
  2093. '`Foo`.`id`',
  2094. '`Foo`.`title`',
  2095. '(user_count + discussion_count + post_count) AS score'
  2096. );
  2097. $this->assertEquals($expected, $result);
  2098. }
  2099. /**
  2100. * test that fields() will accept objects made from DboSource::expression
  2101. *
  2102. * @return void
  2103. */
  2104. public function testFieldsWithExpression() {
  2105. $this->Model = new TestModel;
  2106. $expression = $this->Dbo->expression("CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col");
  2107. $result = $this->Dbo->fields($this->Model, null, array("id", $expression));
  2108. $expected = array(
  2109. '`TestModel`.`id`',
  2110. "CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col"
  2111. );
  2112. $this->assertEquals($expected, $result);
  2113. }
  2114. /**
  2115. * testRenderStatement method
  2116. *
  2117. * @return void
  2118. */
  2119. public function testRenderStatement() {
  2120. $result = $this->Dbo->renderStatement('select', array(
  2121. 'fields' => 'id', 'table' => 'table', 'conditions' => 'WHERE 1=1',
  2122. 'alias' => '', 'joins' => '', 'order' => '', 'limit' => '', 'group' => ''
  2123. ));
  2124. $this->assertRegExp('/^\s*SELECT\s+id\s+FROM\s+table\s+WHERE\s+1=1\s*$/', $result);
  2125. $result = $this->Dbo->renderStatement('update', array('fields' => 'value=2', 'table' => 'table', 'conditions' => 'WHERE 1=1', 'alias' => ''));
  2126. $this->assertRegExp('/^\s*UPDATE\s+table\s+SET\s+value=2\s+WHERE\s+1=1\s*$/', $result);
  2127. $result = $this->Dbo->renderStatement('update', array('fields' => 'value=2', 'table' => 'table', 'conditions' => 'WHERE 1=1', 'alias' => 'alias', 'joins' => ''));
  2128. $this->assertRegExp('/^\s*UPDATE\s+table\s+AS\s+alias\s+SET\s+value=2\s+WHERE\s+1=1\s*$/', $result);
  2129. $result = $this->Dbo->renderStatement('delete', array('fields' => 'value=2', 'table' => 'table', 'conditions' => 'WHERE 1=1', 'alias' => ''));
  2130. $this->assertRegExp('/^\s*DELETE\s+FROM\s+table\s+WHERE\s+1=1\s*$/', $result);
  2131. $result = $this->Dbo->renderStatement('delete', array('fields' => 'value=2', 'table' => 'table', 'conditions' => 'WHERE 1=1', 'alias' => 'alias', 'joins' => ''));
  2132. $this->assertRegExp('/^\s*DELETE\s+alias\s+FROM\s+table\s+AS\s+alias\s+WHERE\s+1=1\s*$/', $result);
  2133. }
  2134. /**
  2135. * testSchema method
  2136. *
  2137. * @return void
  2138. */
  2139. public function testSchema() {
  2140. $Schema = new CakeSchema();
  2141. $Schema->tables = array('table' => array(), 'anotherTable' => array());
  2142. $result = $this->Dbo->dropSchema($Schema, 'non_existing');
  2143. $this->assertTrue(empty($result));
  2144. $result = $this->Dbo->dropSchema($Schema, 'table');
  2145. $this->assertRegExp('/^\s*DROP TABLE IF EXISTS\s+' . $this->Dbo->fullTableName('table') . ';\s*$/s', $result);
  2146. }
  2147. /**
  2148. * testDropSchemaNoSchema method
  2149. *
  2150. * @expectedException PHPUnit_Framework_Error
  2151. * @return void
  2152. */
  2153. public function testDropSchemaNoSchema() {
  2154. $result = $this->Dbo->dropSchema(null);
  2155. }
  2156. /**
  2157. * testOrderParsing method
  2158. *
  2159. * @return void
  2160. */
  2161. public function testOrderParsing() {
  2162. $result = $this->Dbo->order("ADDTIME(Event.time_begin, '-06:00:00') ASC");
  2163. $expected = " ORDER BY ADDTIME(`Event`.`time_begin`, '-06:00:00') ASC";
  2164. $this->assertEquals($expected, $result);
  2165. $result = $this->Dbo->order("title, id");
  2166. $this->assertRegExp('/^\s*ORDER BY\s+`title`\s+ASC,\s+`id`\s+ASC\s*$/', $result);
  2167. $result = $this->Dbo->order("title desc, id desc");
  2168. $this->assertRegExp('/^\s*ORDER BY\s+`title`\s+desc,\s+`id`\s+desc\s*$/', $result);
  2169. $result = $this->Dbo->order(array("title desc, id desc"));
  2170. $this->assertRegExp('/^\s*ORDER BY\s+`title`\s+desc,\s+`id`\s+desc\s*$/', $result);
  2171. $result = $this->Dbo->order(array("title", "id"));
  2172. $this->assertRegExp('/^\s*ORDER BY\s+`title`\s+ASC,\s+`id`\s+ASC\s*$/', $result);
  2173. $result = $this->Dbo->order(array(array('title'), array('id')));
  2174. $this->assertRegExp('/^\s*ORDER BY\s+`title`\s+ASC,\s+`id`\s+ASC\s*$/', $result);
  2175. $result = $this->Dbo->order(array("Post.title" => 'asc', "Post.id" => 'desc'));
  2176. $this->assertRegExp('/^\s*ORDER BY\s+`Post`.`title`\s+asc,\s+`Post`.`id`\s+desc\s*$/', $result);
  2177. $result = $this->Dbo->order(array(array("Post.title" => 'asc', "Post.id" => 'desc')));
  2178. $this->assertRegExp('/^\s*ORDER BY\s+`Post`.`title`\s+asc,\s+`Post`.`id`\s+desc\s*$/', $result);
  2179. $result = $this->Dbo->order(array("title"));
  2180. $this->assertRegExp('/^\s*ORDER BY\s+`title`\s+ASC\s*$/', $result);
  2181. $result = $this->Dbo->order(array(array("title")));
  2182. $this->assertRegExp('/^\s*ORDER BY\s+`title`\s+ASC\s*$/', $result);
  2183. $result = $this->Dbo->order("Dealer.id = 7 desc, Dealer.id = 3 desc, Dealer.title asc");
  2184. $expected = " ORDER BY `Dealer`.`id` = 7 desc, `Dealer`.`id` = 3 desc, `Dealer`.`title` asc";
  2185. $this->assertEquals($expected, $result);
  2186. $result = $this->Dbo->order(array("Page.name" => "='test' DESC"));
  2187. $this->assertRegExp("/^\s*ORDER BY\s+`Page`\.`name`\s*='test'\s+DESC\s*$/", $result);
  2188. $result = $this->Dbo->order("Page.name = 'view' DESC");
  2189. $this->assertRegExp("/^\s*ORDER BY\s+`Page`\.`name`\s*=\s*'view'\s+DESC\s*$/", $result);
  2190. $result = $this->Dbo->order("(Post.views)");
  2191. $this->assertRegExp("/^\s*ORDER BY\s+\(`Post`\.`views`\)\s+ASC\s*$/", $result);
  2192. $result = $this->Dbo->order("(Post.views)*Post.views");
  2193. $this->assertRegExp("/^\s*ORDER BY\s+\(`Post`\.`views`\)\*`Post`\.`views`\s+ASC\s*$/", $result);
  2194. $result = $this->Dbo->order("(Post.views) * Post.views");
  2195. $this->assertRegExp("/^\s*ORDER BY\s+\(`Post`\.`views`\) \* `Post`\.`views`\s+ASC\s*$/", $result);
  2196. $result = $this->Dbo->order("(Model.field1 + Model.field2) * Model.field3");
  2197. $this->assertRegExp("/^\s*ORDER BY\s+\(`Model`\.`field1` \+ `Model`\.`field2`\) \* `Model`\.`field3`\s+ASC\s*$/", $result);
  2198. $result = $this->Dbo->order("Model.name+0 ASC");
  2199. $this->assertRegExp("/^\s*ORDER BY\s+`Model`\.`name`\+0\s+ASC\s*$/", $result);
  2200. $result = $this->Dbo->order("Anuncio.destaque & 2 DESC");
  2201. $expected = ' ORDER BY `Anuncio`.`destaque` & 2 DESC';
  2202. $this->assertEquals($expected, $result);
  2203. $result = $this->Dbo->order("3963.191 * id");
  2204. $expected = ' ORDER BY 3963.191 * id ASC';
  2205. $this->assertEquals($expected, $result);
  2206. $result = $this->Dbo->order(array('Property.sale_price IS NULL'));
  2207. $expected = ' ORDER BY `Property`.`sale_price` IS NULL ASC';
  2208. $this->assertEquals($expected, $result);
  2209. }
  2210. /**
  2211. * testComplexSortExpression method
  2212. *
  2213. * @return void
  2214. */
  2215. public function testComplexSortExpression() {
  2216. $result = $this->Dbo->order(array('(Model.field > 100) DESC', 'Model.field ASC'));
  2217. $this->assertRegExp("/^\s*ORDER BY\s+\(`Model`\.`field`\s+>\s+100\)\s+DESC,\s+`Model`\.`field`\s+ASC\s*$/", $result);
  2218. }
  2219. /**
  2220. * testCalculations method
  2221. *
  2222. * @return void
  2223. */
  2224. public function testCalculations() {
  2225. $this->Model = new TestModel();
  2226. $result = $this->Dbo->calculate($this->Model, 'count');
  2227. $this->assertEquals($result, 'COUNT(*) AS `count`');
  2228. $result = $this->Dbo->calculate($this->Model, 'count', array('id'));
  2229. $this->assertEquals($result, 'COUNT(`id`) AS `count`');
  2230. $result = $this->Dbo->calculate(
  2231. $this->Model,
  2232. 'count',
  2233. array($this->Dbo->expression('DISTINCT id'))
  2234. );
  2235. $this->assertEquals($result, 'COUNT(DISTINCT id) AS `count`');
  2236. $result = $this->Dbo->calculate($this->Model, 'count', array('id', 'id_count'));
  2237. $this->assertEquals($result, 'COUNT(`id`) AS `id_count`');
  2238. $result = $this->Dbo->calculate($this->Model, 'count', array('Model.id', 'id_count'));
  2239. $this->assertEquals($result, 'COUNT(`Model`.`id`) AS `id_count`');
  2240. $result = $this->Dbo->calculate($this->Model, 'max', array('id'));
  2241. $this->assertEquals($result, 'MAX(`id`) AS `id`');
  2242. $result = $this->Dbo->calculate($this->Model, 'max', array('Model.id', 'id'));
  2243. $this->assertEquals($result, 'MAX(`Model`.`id`) AS `id`');
  2244. $result = $this->Dbo->calculate($this->Model, 'max', array('`Model`.`id`', 'id'));
  2245. $this->assertEquals($result, 'MAX(`Model`.`id`) AS `id`');
  2246. $result = $this->Dbo->calculate($this->Model, 'min', array('`Model`.`id`', 'id'));
  2247. $this->assertEquals($result, 'MIN(`Model`.`id`) AS `id`');
  2248. $result = $this->Dbo->calculate($this->Model, 'min', 'left');
  2249. $this->assertEquals($result, 'MIN(`left`) AS `left`');
  2250. }
  2251. /**
  2252. * testLength method
  2253. *
  2254. * @return void
  2255. */
  2256. public function testLength() {
  2257. $result = $this->Dbo->length('varchar(255)');
  2258. $expected = 255;
  2259. $this->assertSame($expected, $result);
  2260. $result = $this->Dbo->length('int(11)');
  2261. $expected = 11;
  2262. $this->assertSame($expected, $result);
  2263. $result = $this->Dbo->length('float(5,3)');
  2264. $expected = '5,3';
  2265. $this->assertSame($expected, $result);
  2266. $result = $this->Dbo->length('decimal(5,2)');
  2267. $expected = '5,2';
  2268. $this->assertSame($expected, $result);
  2269. $result = $this->Dbo->length("enum('test','me','now')");
  2270. $expected = 4;
  2271. $this->assertSame($expected, $result);
  2272. $result = $this->Dbo->length("set('a','b','cd')");
  2273. $expected = 2;
  2274. $this->assertSame($expected, $result);
  2275. $result = $this->Dbo->length(false);
  2276. $this->assertTrue($result === null);
  2277. $result = $this->Dbo->length('datetime');
  2278. $expected = null;
  2279. $this->assertSame($expected, $result);
  2280. $result = $this->Dbo->length('text');
  2281. $expected = null;
  2282. $this->assertSame($expected, $result);
  2283. }
  2284. /**
  2285. * testBuildIndex method
  2286. *
  2287. * @return void
  2288. */
  2289. public function testBuildIndex() {
  2290. $data = array(
  2291. 'PRIMARY' => array('column' => 'id')
  2292. );
  2293. $result = $this->Dbo->buildIndex($data);
  2294. $expected = array('PRIMARY KEY (`id`)');
  2295. $this->assertSame($expected, $result);
  2296. $data = array(
  2297. 'MyIndex' => array('column' => 'id', 'unique' => true)
  2298. );
  2299. $result = $this->Dbo->buildIndex($data);
  2300. $expected = array('UNIQUE KEY `MyIndex` (`id`)');
  2301. $this->assertEquals($expected, $result);
  2302. $data = array(
  2303. 'MyIndex' => array('column' => array('id', 'name'), 'unique' => true)
  2304. );
  2305. $result = $this->Dbo->buildIndex($data);
  2306. $expected = array('UNIQUE KEY `MyIndex` (`id`, `name`)');
  2307. $this->assertEquals($expected, $result);
  2308. }
  2309. /**
  2310. * testBuildColumn method
  2311. *
  2312. * @return void
  2313. */
  2314. public function testBuildColumn2() {
  2315. $data = array(
  2316. 'name' => 'testName',
  2317. 'type' => 'string',
  2318. 'length' => 255,
  2319. 'default',
  2320. 'null' => true,
  2321. 'key'
  2322. );
  2323. $result = $this->Dbo->buildColumn($data);
  2324. $expected = '`testName` varchar(255) DEFAULT NULL';
  2325. $this->assertEquals($expected, $result);
  2326. $data = array(
  2327. 'name' => 'int_field',
  2328. 'type' => 'integer',
  2329. 'default' => '',
  2330. 'null' => false,
  2331. );
  2332. $restore = $this->Dbo->columns;
  2333. $this->Dbo->columns = array('integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'), );
  2334. $result = $this->Dbo->buildColumn($data);
  2335. $expected = '`int_field` int(11) NOT NULL';
  2336. $this->assertEquals($expected, $result);
  2337. $this->Dbo->fieldParameters['param'] = array(
  2338. 'value' => 'COLLATE',
  2339. 'quote' => false,
  2340. 'join' => ' ',
  2341. 'column' => 'Collate',
  2342. 'position' => 'beforeDefault',
  2343. 'options' => array('GOOD', 'OK')
  2344. );
  2345. $data = array(
  2346. 'name' => 'int_field',
  2347. 'type' => 'integer',
  2348. 'default' => '',
  2349. 'null' => false,
  2350. 'param' => 'BAD'
  2351. );
  2352. $result = $this->Dbo->buildColumn($data);
  2353. $expected = '`int_field` int(11) NOT NULL';
  2354. $this->assertEquals($expected, $result);
  2355. $data = array(
  2356. 'name' => 'int_field',
  2357. 'type' => 'integer',
  2358. 'default' => '',
  2359. 'null' => false,
  2360. 'param' => 'GOOD'
  2361. );
  2362. $result = $this->Dbo->buildColumn($data);
  2363. $expected = '`int_field` int(11) COLLATE GOOD NOT NULL';
  2364. $this->assertEquals($expected, $result);
  2365. $this->Dbo->columns = $restore;
  2366. $data = array(
  2367. 'name' => 'created',
  2368. 'type' => 'timestamp',
  2369. 'default' => 'current_timestamp',
  2370. 'null' => false,
  2371. );
  2372. $result = $this->Dbo->buildColumn($data);
  2373. $expected = '`created` timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL';
  2374. $this->assertEquals($expected, $result);
  2375. $data = array(
  2376. 'name' => 'created',
  2377. 'type' => 'timestamp',
  2378. 'default' => 'CURRENT_TIMESTAMP',
  2379. 'null' => true,
  2380. );
  2381. $result = $this->Dbo->buildColumn($data);
  2382. $expected = '`created` timestamp DEFAULT CURRENT_TIMESTAMP';
  2383. $this->assertEquals($expected, $result);
  2384. $data = array(
  2385. 'name' => 'modified',
  2386. 'type' => 'timestamp',
  2387. 'null' => true,
  2388. );
  2389. $result = $this->Dbo->buildColumn($data);
  2390. $expected = '`modified` timestamp NULL';
  2391. $this->assertEquals($expected, $result);
  2392. $data = array(
  2393. 'name' => 'modified',
  2394. 'type' => 'timestamp',
  2395. 'default' => null,
  2396. 'null' => true,
  2397. );
  2398. $result = $this->Dbo->buildColumn($data);
  2399. $expected = '`modified` timestamp NULL';
  2400. $this->assertEquals($expected, $result);
  2401. }
  2402. /**
  2403. * testBuildColumnBadType method
  2404. *
  2405. * @expectedException PHPUnit_Framework_Error
  2406. * @return void
  2407. */
  2408. public function testBuildColumnBadType() {
  2409. $data = array(
  2410. 'name' => 'testName',
  2411. 'type' => 'varchar(255)',
  2412. 'default',
  2413. 'null' => true,
  2414. 'key'
  2415. );
  2416. $this->Dbo->buildColumn($data);
  2417. }
  2418. /**
  2419. * test hasAny()
  2420. *
  2421. * @return void
  2422. */
  2423. public function testHasAny() {
  2424. $db = $this->Dbo->config['database'];
  2425. $this->Dbo = $this->getMock('Mysql', array('connect', '_execute', 'execute', 'value'));
  2426. $this->Dbo->config['database'] = $db;
  2427. $this->Model = $this->getMock('TestModel', array('getDataSource'));
  2428. $this->Model->expects($this->any())
  2429. ->method('getDataSource')
  2430. ->will($this->returnValue($this->Dbo));
  2431. $this->Dbo->expects($this->at(0))->method('value')
  2432. ->with('harry')
  2433. ->will($this->returnValue("'harry'"));
  2434. $modelTable = $this->Dbo->fullTableName($this->Model);
  2435. $this->Dbo->expects($this->at(1))->method('execute')
  2436. ->with('SELECT COUNT(`TestModel`.`id`) AS count FROM ' .$modelTable. ' AS `TestModel` WHERE `TestModel`.`name` = \'harry\'');
  2437. $this->Dbo->expects($this->at(2))->method('execute')
  2438. ->with('SELECT COUNT(`TestModel`.`id`) AS count FROM ' .$modelTable. ' AS `TestModel` WHERE 1 = 1');
  2439. $this->Dbo->hasAny($this->Model, array('TestModel.name' => 'harry'));
  2440. $this->Dbo->hasAny($this->Model, array());
  2441. }
  2442. /**
  2443. * test fields generating usable virtual fields to use in query
  2444. *
  2445. * @return void
  2446. */
  2447. public function testVirtualFields() {
  2448. $this->loadFixtures('Article', 'Comment', 'Tag');
  2449. $this->Dbo->virtualFieldSeparator = '__';
  2450. $Article = ClassRegistry::init('Article');
  2451. $commentsTable = $this->Dbo->fullTableName('comments', false, false);
  2452. $Article->virtualFields = array(
  2453. 'this_moment' => 'NOW()',
  2454. 'two' => '1 + 1',
  2455. 'comment_count' => 'SELECT COUNT(*) FROM ' . $commentsTable .
  2456. ' WHERE Article.id = ' . $commentsTable . '.article_id'
  2457. );
  2458. $result = $this->Dbo->fields($Article);
  2459. $expected = array(
  2460. '`Article`.`id`',
  2461. '`Article`.`user_id`',
  2462. '`Article`.`title`',
  2463. '`Article`.`body`',
  2464. '`Article`.`published`',
  2465. '`Article`.`created`',
  2466. '`Article`.`updated`',
  2467. '(NOW()) AS `Article__this_moment`',
  2468. '(1 + 1) AS `Article__two`',
  2469. "(SELECT COUNT(*) FROM $commentsTable WHERE `Article`.`id` = `$commentsTable`.`article_id`) AS `Article__comment_count`"
  2470. );
  2471. $this->assertEquals($expected, $result);
  2472. $result = $this->Dbo->fields($Article, null, array('this_moment', 'title'));
  2473. $expected = array(
  2474. '`Article`.`title`',
  2475. '(NOW()) AS `Article__this_moment`',
  2476. );
  2477. $this->assertEquals($expected, $result);
  2478. $result = $this->Dbo->fields($Article, null, array('Article.title', 'Article.this_moment'));
  2479. $expected = array(
  2480. '`Article`.`title`',
  2481. '(NOW()) AS `Article__this_moment`',
  2482. );
  2483. $this->assertEquals($expected, $result);
  2484. $result = $this->Dbo->fields($Article, null, array('Article.this_moment', 'Article.title'));
  2485. $expected = array(
  2486. '`Article`.`title`',
  2487. '(NOW()) AS `Article__this_moment`',
  2488. );
  2489. $this->assertEquals($expected, $result);
  2490. $result = $this->Dbo->fields($Article, null, array('Article.*'));
  2491. $expected = array(
  2492. '`Article`.*',
  2493. '(NOW()) AS `Article__this_moment`',
  2494. '(1 + 1) AS `Article__two`',
  2495. "(SELECT COUNT(*) FROM $commentsTable WHERE `Article`.`id` = `$commentsTable`.`article_id`) AS `Article__comment_count`"
  2496. );
  2497. $this->assertEquals($expected, $result);
  2498. $result = $this->Dbo->fields($Article, null, array('*'));
  2499. $expected = array(
  2500. '*',
  2501. '(NOW()) AS `Article__this_moment`',
  2502. '(1 + 1) AS `Article__two`',
  2503. "(SELECT COUNT(*) FROM $commentsTable WHERE `Article`.`id` = `$commentsTable`.`article_id`) AS `Article__comment_count`"
  2504. );
  2505. $this->assertEquals($expected, $result);
  2506. }
  2507. /**
  2508. * test conditions to generate query conditions for virtual fields
  2509. *
  2510. * @return void
  2511. */
  2512. public function testVirtualFieldsInConditions() {
  2513. $Article = ClassRegistry::init('Article');
  2514. $commentsTable = $this->Dbo->fullTableName('comments', false, false);
  2515. $Article->virtualFields = array(
  2516. 'this_moment' => 'NOW()',
  2517. 'two' => '1 + 1',
  2518. 'comment_count' => 'SELECT COUNT(*) FROM ' . $commentsTable .
  2519. ' WHERE Article.id = ' . $commentsTable . '.article_id'
  2520. );
  2521. $conditions = array('two' => 2);
  2522. $result = $this->Dbo->conditions($conditions, true, false, $Article);
  2523. $expected = '(1 + 1) = 2';
  2524. $this->assertEquals($expected, $result);
  2525. $conditions = array('this_moment BETWEEN ? AND ?' => array(1,2));
  2526. $expected = 'NOW() BETWEEN 1 AND 2';
  2527. $result = $this->Dbo->conditions($conditions, true, false, $Article);
  2528. $this->assertEquals($expected, $result);
  2529. $conditions = array('comment_count >' => 5);
  2530. $expected = "(SELECT COUNT(*) FROM $commentsTable WHERE `Article`.`id` = `$commentsTable`.`article_id`) > 5";
  2531. $result = $this->Dbo->conditions($conditions, true, false, $Article);
  2532. $this->assertEquals($expected, $result);
  2533. $conditions = array('NOT' => array('two' => 2));
  2534. $result = $this->Dbo->conditions($conditions, true, false, $Article);
  2535. $expected = 'NOT ((1 + 1) = 2)';
  2536. $this->assertEquals($expected, $result);
  2537. }
  2538. /**
  2539. * test that virtualFields with complex functions and aliases work.
  2540. *
  2541. * @return void
  2542. */
  2543. public function testConditionsWithComplexVirtualFields() {
  2544. $Article = ClassRegistry::init('Article', 'Comment', 'Tag');
  2545. $Article->virtualFields = array(
  2546. 'distance' => 'ACOS(SIN(20 * PI() / 180)
  2547. * SIN(Article.latitude * PI() / 180)
  2548. + COS(20 * PI() / 180)
  2549. * COS(Article.latitude * PI() / 180)
  2550. * COS((50 - Article.longitude) * PI() / 180)
  2551. ) * 180 / PI() * 60 * 1.1515 * 1.609344'
  2552. );
  2553. $conditions = array('distance >=' => 20);
  2554. $result = $this->Dbo->conditions($conditions, true, true, $Article);
  2555. $this->assertRegExp('/\) >= 20/', $result);
  2556. $this->assertRegExp('/[`\'"]Article[`\'"].[`\'"]latitude[`\'"]/', $result);
  2557. $this->assertRegExp('/[`\'"]Article[`\'"].[`\'"]longitude[`\'"]/', $result);
  2558. }
  2559. /**
  2560. * test calculate to generate claculate statements on virtual fields
  2561. *
  2562. * @return void
  2563. */
  2564. public function testVirtualFieldsInCalculate() {
  2565. $Article = ClassRegistry::init('Article');
  2566. $commentsTable = $this->Dbo->fullTableName('comments', false, false);
  2567. $Article->virtualFields = array(
  2568. 'this_moment' => 'NOW()',
  2569. 'two' => '1 + 1',
  2570. 'comment_count' => 'SELECT COUNT(*) FROM ' . $commentsTable .
  2571. ' WHERE Article.id = ' . $commentsTable . '.article_id'
  2572. );
  2573. $result = $this->Dbo->calculate($Article, 'count', array('this_moment'));
  2574. $expected = 'COUNT(NOW()) AS `count`';
  2575. $this->assertEquals($expected, $result);
  2576. $result = $this->Dbo->calculate($Article, 'max', array('comment_count'));
  2577. $expected = "MAX(SELECT COUNT(*) FROM $commentsTable WHERE `Article`.`id` = `$commentsTable`.`article_id`) AS `comment_count`";
  2578. $this->assertEquals($expected, $result);
  2579. }
  2580. /**
  2581. * test reading virtual fields containing newlines when recursive > 0
  2582. *
  2583. * @return void
  2584. */
  2585. public function testReadVirtualFieldsWithNewLines() {
  2586. $Article = new Article();
  2587. $Article->recursive = 1;
  2588. $Article->virtualFields = array(
  2589. 'test' => '
  2590. User.id + User.id
  2591. '
  2592. );
  2593. $result = $this->Dbo->fields($Article, null, array());
  2594. $result = $this->Dbo->fields($Article, $Article->alias, $result);
  2595. $this->assertRegExp('/[`\"]User[`\"]\.[`\"]id[`\"] \+ [`\"]User[`\"]\.[`\"]id[`\"]/', $result[7]);
  2596. }
  2597. /**
  2598. * test group to generate GROUP BY statements on virtual fields
  2599. *
  2600. * @return void
  2601. */
  2602. public function testVirtualFieldsInGroup() {
  2603. $Article = ClassRegistry::init('Article');
  2604. $Article->virtualFields = array(
  2605. 'this_year' => 'YEAR(Article.created)'
  2606. );
  2607. $result = $this->Dbo->group('this_year', $Article);
  2608. $expected = " GROUP BY (YEAR(`Article`.`created`))";
  2609. $this->assertEquals($expected, $result);
  2610. }
  2611. /**
  2612. * test that virtualFields with complex functions and aliases work.
  2613. *
  2614. * @return void
  2615. */
  2616. public function testFieldsWithComplexVirtualFields() {
  2617. $Article = new Article();
  2618. $Article->virtualFields = array(
  2619. 'distance' => 'ACOS(SIN(20 * PI() / 180)
  2620. * SIN(Article.latitude * PI() / 180)
  2621. + COS(20 * PI() / 180)
  2622. * COS(Article.latitude * PI() / 180)
  2623. * COS((50 - Article.longitude) * PI() / 180)
  2624. ) * 180 / PI() * 60 * 1.1515 * 1.609344'
  2625. );
  2626. $fields = array('id', 'distance');
  2627. $result = $this->Dbo->fields($Article, null, $fields);
  2628. $qs = $this->Dbo->startQuote;
  2629. $qe = $this->Dbo->endQuote;
  2630. $this->assertEquals($result[0], "{$qs}Article{$qe}.{$qs}id{$qe}");
  2631. $this->assertRegExp('/Article__distance/', $result[1]);
  2632. $this->assertRegExp('/[`\'"]Article[`\'"].[`\'"]latitude[`\'"]/', $result[1]);
  2633. $this->assertRegExp('/[`\'"]Article[`\'"].[`\'"]longitude[`\'"]/', $result[1]);
  2634. }
  2635. /**
  2636. * test that execute runs queries.
  2637. *
  2638. * @return void
  2639. */
  2640. public function testExecute() {
  2641. $query = 'SELECT * FROM ' . $this->Dbo->fullTableName('articles') . ' WHERE 1 = 1';
  2642. $this->Dbo->took = null;
  2643. $this->Dbo->affected = null;
  2644. $result = $this->Dbo->execute($query, array('log' => false));
  2645. $this->assertNotNull($result, 'No query performed! %s');
  2646. $this->assertNull($this->Dbo->took, 'Stats were set %s');
  2647. $this->assertNull($this->Dbo->affected, 'Stats were set %s');
  2648. $result = $this->Dbo->execute($query);
  2649. $this->assertNotNull($result, 'No query performed! %s');
  2650. $this->assertNotNull($this->Dbo->took, 'Stats were not set %s');
  2651. $this->assertNotNull($this->Dbo->affected, 'Stats were not set %s');
  2652. }
  2653. /**
  2654. * test a full example of using virtual fields
  2655. *
  2656. * @return void
  2657. */
  2658. public function testVirtualFieldsFetch() {
  2659. $this->loadFixtures('Article', 'Comment');
  2660. $Article = ClassRegistry::init('Article');
  2661. $Article->virtualFields = array(
  2662. 'comment_count' => 'SELECT COUNT(*) FROM ' . $this->Dbo->fullTableName('comments') .
  2663. ' WHERE Article.id = ' . $this->Dbo->fullTableName('comments') . '.article_id'
  2664. );
  2665. $conditions = array('comment_count >' => 2);
  2666. $query = 'SELECT ' . join(',',$this->Dbo->fields($Article, null, array('id', 'comment_count'))) .
  2667. ' FROM ' . $this->Dbo->fullTableName($Article) . ' Article ' . $this->Dbo->conditions($conditions, true, true, $Article);
  2668. $result = $this->Dbo->fetchAll($query);
  2669. $expected = array(array(
  2670. 'Article' => array('id' => 1, 'comment_count' => 4)
  2671. ));
  2672. $this->assertEquals($expected, $result);
  2673. }
  2674. /**
  2675. * test reading complex virtualFields with subqueries.
  2676. *
  2677. * @return void
  2678. */
  2679. public function testVirtualFieldsComplexRead() {
  2680. $this->loadFixtures('DataTest', 'Article', 'Comment', 'User', 'Tag', 'ArticlesTag');
  2681. $Article = ClassRegistry::init('Article');
  2682. $commentTable = $this->Dbo->fullTableName('comments');
  2683. $Article = ClassRegistry::init('Article');
  2684. $Article->virtualFields = array(
  2685. 'comment_count' => 'SELECT COUNT(*) FROM ' . $commentTable .
  2686. ' AS Comment WHERE Article.id = Comment.article_id'
  2687. );
  2688. $result = $Article->find('all');
  2689. $this->assertTrue(count($result) > 0);
  2690. $this->assertTrue($result[0]['Article']['comment_count'] > 0);
  2691. $DataTest = ClassRegistry::init('DataTest');
  2692. $DataTest->virtualFields = array(
  2693. 'complicated' => 'ACOS(SIN(20 * PI() / 180)
  2694. * SIN(DataTest.float * PI() / 180)
  2695. + COS(20 * PI() / 180)
  2696. * COS(DataTest.count * PI() / 180)
  2697. * COS((50 - DataTest.float) * PI() / 180)
  2698. ) * 180 / PI() * 60 * 1.1515 * 1.609344'
  2699. );
  2700. $result = $DataTest->find('all');
  2701. $this->assertTrue(count($result) > 0);
  2702. $this->assertTrue($result[0]['DataTest']['complicated'] > 0);
  2703. }
  2704. /**
  2705. * testIntrospectType method
  2706. *
  2707. * @return void
  2708. */
  2709. public function testIntrospectType() {
  2710. $this->assertEquals($this->Dbo->introspectType(0), 'integer');
  2711. $this->assertEquals($this->Dbo->introspectType(2), 'integer');
  2712. $this->assertEquals($this->Dbo->introspectType('2'), 'string');
  2713. $this->assertEquals($this->Dbo->introspectType('2.2'), 'string');
  2714. $this->assertEquals($this->Dbo->introspectType(2.2), 'float');
  2715. $this->assertEquals($this->Dbo->introspectType('stringme'), 'string');
  2716. $this->assertEquals($this->Dbo->introspectType('0stringme'), 'string');
  2717. $data = array(2.2);
  2718. $this->assertEquals($this->Dbo->introspectType($data), 'float');
  2719. $data = array('2.2');
  2720. $this->assertEquals($this->Dbo->introspectType($data), 'float');
  2721. $data = array(2);
  2722. $this->assertEquals($this->Dbo->introspectType($data), 'integer');
  2723. $data = array('2');
  2724. $this->assertEquals($this->Dbo->introspectType($data), 'integer');
  2725. $data = array('string');
  2726. $this->assertEquals($this->Dbo->introspectType($data), 'string');
  2727. $data = array(2.2, '2.2');
  2728. $this->assertEquals($this->Dbo->introspectType($data), 'float');
  2729. $data = array(2, '2');
  2730. $this->assertEquals($this->Dbo->introspectType($data), 'integer');
  2731. $data = array('string one', 'string two');
  2732. $this->assertEquals($this->Dbo->introspectType($data), 'string');
  2733. $data = array('2.2', 3);
  2734. $this->assertEquals($this->Dbo->introspectType($data), 'integer');
  2735. $data = array('2.2', '0stringme');
  2736. $this->assertEquals($this->Dbo->introspectType($data), 'string');
  2737. $data = array(2.2, 3);
  2738. $this->assertEquals($this->Dbo->introspectType($data), 'integer');
  2739. $data = array(2.2, '0stringme');
  2740. $this->assertEquals($this->Dbo->introspectType($data), 'string');
  2741. $data = array(2, 'stringme');
  2742. $this->assertEquals($this->Dbo->introspectType($data), 'string');
  2743. $data = array(2, '2.2', 'stringgme');
  2744. $this->assertEquals($this->Dbo->introspectType($data), 'string');
  2745. $data = array(2, '2.2');
  2746. $this->assertEquals($this->Dbo->introspectType($data), 'integer');
  2747. $data = array(2, 2.2);
  2748. $this->assertEquals($this->Dbo->introspectType($data), 'integer');
  2749. // NULL
  2750. $result = $this->Dbo->value(null, 'boolean');
  2751. $this->assertEquals($result, 'NULL');
  2752. // EMPTY STRING
  2753. $result = $this->Dbo->value('', 'boolean');
  2754. $this->assertEquals($result, "'0'");
  2755. // BOOLEAN
  2756. $result = $this->Dbo->value('true', 'boolean');
  2757. $this->assertEquals($result, "'1'");
  2758. $result = $this->Dbo->value('false', 'boolean');
  2759. $this->assertEquals($result, "'1'");
  2760. $result = $this->Dbo->value(true, 'boolean');
  2761. $this->assertEquals($result, "'1'");
  2762. $result = $this->Dbo->value(false, 'boolean');
  2763. $this->assertEquals($result, "'0'");
  2764. $result = $this->Dbo->value(1, 'boolean');
  2765. $this->assertEquals($result, "'1'");
  2766. $result = $this->Dbo->value(0, 'boolean');
  2767. $this->assertEquals($result, "'0'");
  2768. $result = $this->Dbo->value('abc', 'boolean');
  2769. $this->assertEquals($result, "'1'");
  2770. $result = $this->Dbo->value(1.234, 'boolean');
  2771. $this->assertEquals($result, "'1'");
  2772. $result = $this->Dbo->value('1.234e05', 'boolean');
  2773. $this->assertEquals($result, "'1'");
  2774. // NUMBERS
  2775. $result = $this->Dbo->value(123, 'integer');
  2776. $this->assertEquals($result, 123);
  2777. $result = $this->Dbo->value('123', 'integer');
  2778. $this->assertEquals($result, '123');
  2779. $result = $this->Dbo->value('0123', 'integer');
  2780. $this->assertEquals($result, "'0123'");
  2781. $result = $this->Dbo->value('0x123ABC', 'integer');
  2782. $this->assertEquals($result, "'0x123ABC'");
  2783. $result = $this->Dbo->value('0x123', 'integer');
  2784. $this->assertEquals($result, "'0x123'");
  2785. $result = $this->Dbo->value(1.234, 'float');
  2786. $this->assertEquals($result, 1.234);
  2787. $result = $this->Dbo->value('1.234', 'float');
  2788. $this->assertEquals($result, '1.234');
  2789. $result = $this->Dbo->value(' 1.234 ', 'float');
  2790. $this->assertEquals($result, "' 1.234 '");
  2791. $result = $this->Dbo->value('1.234e05', 'float');
  2792. $this->assertEquals($result, "'1.234e05'");
  2793. $result = $this->Dbo->value('1.234e+5', 'float');
  2794. $this->assertEquals($result, "'1.234e+5'");
  2795. $result = $this->Dbo->value('1,234', 'float');
  2796. $this->assertEquals($result, "'1,234'");
  2797. $result = $this->Dbo->value('FFF', 'integer');
  2798. $this->assertEquals($result, "'FFF'");
  2799. $result = $this->Dbo->value('abc', 'integer');
  2800. $this->assertEquals($result, "'abc'");
  2801. // STRINGS
  2802. $result = $this->Dbo->value('123', 'string');
  2803. $this->assertEquals($result, "'123'");
  2804. $result = $this->Dbo->value(123, 'string');
  2805. $this->assertEquals($result, "'123'");
  2806. $result = $this->Dbo->value(1.234, 'string');
  2807. $this->assertEquals($result, "'1.234'");
  2808. $result = $this->Dbo->value('abc', 'string');
  2809. $this->assertEquals($result, "'abc'");
  2810. $result = $this->Dbo->value(' abc ', 'string');
  2811. $this->assertEquals($result, "' abc '");
  2812. $result = $this->Dbo->value('a bc', 'string');
  2813. $this->assertEquals($result, "'a bc'");
  2814. }
  2815. /**
  2816. * testRealQueries method
  2817. *
  2818. * @return void
  2819. */
  2820. public function testRealQueries() {
  2821. $this->loadFixtures('Apple', 'Article', 'User', 'Comment', 'Tag', 'Sample', 'ArticlesTag');
  2822. $Apple = ClassRegistry::init('Apple');
  2823. $Article = ClassRegistry::init('Article');
  2824. $result = $this->Dbo->rawQuery('SELECT color, name FROM ' . $this->Dbo->fullTableName('apples'));
  2825. $this->assertTrue(!empty($result));
  2826. $result = $this->Dbo->fetchRow($result);
  2827. $expected = array($this->Dbo->fullTableName('apples', false, false) => array(
  2828. 'color' => 'Red 1',
  2829. 'name' => 'Red Apple 1'
  2830. ));
  2831. $this->assertEquals($expected, $result);
  2832. $result = $this->Dbo->fetchAll('SELECT name FROM ' . $this->Dbo->fullTableName('apples') . ' ORDER BY id');
  2833. $expected = array(
  2834. array($this->Dbo->fullTableName('apples', false, false) => array('name' => 'Red Apple 1')),
  2835. array($this->Dbo->fullTableName('apples', false, false) => array('name' => 'Bright Red Apple')),
  2836. array($this->Dbo->fullTableName('apples', false, false) => array('name' => 'green blue')),
  2837. array($this->Dbo->fullTableName('apples', false, false) => array('name' => 'Test Name')),
  2838. array($this->Dbo->fullTableName('apples', false, false) => array('name' => 'Blue Green')),
  2839. array($this->Dbo->fullTableName('apples', false, false) => array('name' => 'My new apple')),
  2840. array($this->Dbo->fullTableName('apples', false, false) => array('name' => 'Some odd color'))
  2841. );
  2842. $this->assertEquals($expected, $result);
  2843. $result = $this->Dbo->field($this->Dbo->fullTableName('apples', false, false), 'SELECT color, name FROM ' . $this->Dbo->fullTableName('apples') . ' ORDER BY id');
  2844. $expected = array(
  2845. 'color' => 'Red 1',
  2846. 'name' => 'Red Apple 1'
  2847. );
  2848. $this->assertEquals($expected, $result);
  2849. $Apple->unbindModel(array(), false);
  2850. $result = $this->Dbo->read($Apple, array(
  2851. 'fields' => array($Apple->escapeField('name')),
  2852. 'conditions' => null,
  2853. 'recursive' => -1
  2854. ));
  2855. $expected = array(
  2856. array('Apple' => array('name' => 'Red Apple 1')),
  2857. array('Apple' => array('name' => 'Bright Red Apple')),
  2858. array('Apple' => array('name' => 'green blue')),
  2859. array('Apple' => array('name' => 'Test Name')),
  2860. array('Apple' => array('name' => 'Blue Green')),
  2861. array('Apple' => array('name' => 'My new apple')),
  2862. array('Apple' => array('name' => 'Some odd color'))
  2863. );
  2864. $this->assertEquals($expected, $result);
  2865. $result = $this->Dbo->read($Article, array(
  2866. 'fields' => array('id', 'user_id', 'title'),
  2867. 'conditions' => null,
  2868. 'recursive' => 1
  2869. ));
  2870. $this->assertTrue(Set::matches('/Article[id=1]', $result));
  2871. $this->assertTrue(Set::matches('/Comment[id=1]', $result));
  2872. $this->assertTrue(Set::matches('/Comment[id=2]', $result));
  2873. $this->assertFalse(Set::matches('/Comment[id=10]', $result));
  2874. }
  2875. /**
  2876. * @expectedException MissingConnectionException
  2877. * @return void
  2878. */
  2879. public function testExceptionOnBrokenConnection() {
  2880. $dbo = new Mysql(array(
  2881. 'driver' => 'mysql',
  2882. 'host' => 'imaginary_host',
  2883. 'login' => 'mark',
  2884. 'password' => 'inyurdatabase',
  2885. 'database' => 'imaginary'
  2886. ));
  2887. }
  2888. /**
  2889. * testStatements method
  2890. *
  2891. * @return void
  2892. */
  2893. public function testUpdateStatements() {
  2894. $this->loadFixtures('Article', 'User');
  2895. $test = ConnectionManager::getDatasource('test');
  2896. $db = $test->config['database'];
  2897. $this->Dbo = $this->getMock('Mysql', array('execute'), array($test->config));
  2898. $this->Dbo->expects($this->at(0))->method('execute')
  2899. ->with("UPDATE `$db`.`articles` SET `field1` = 'value1' WHERE 1 = 1");
  2900. $this->Dbo->expects($this->at(1))->method('execute')
  2901. ->with("UPDATE `$db`.`articles` AS `Article` LEFT JOIN `$db`.`users` AS `User` ON " .
  2902. "(`Article`.`user_id` = `User`.`id`)" .
  2903. " SET `Article`.`field1` = 2 WHERE 2=2");
  2904. $this->Dbo->expects($this->at(2))->method('execute')
  2905. ->with("UPDATE `$db`.`articles` AS `Article` LEFT JOIN `$db`.`users` AS `User` ON " .
  2906. "(`Article`.`user_id` = `User`.`id`)" .
  2907. " SET `Article`.`field1` = 'value' WHERE `index` = 'val'");
  2908. $Article = new Article();
  2909. $this->Dbo->update($Article, array('field1'), array('value1'));
  2910. $this->Dbo->update($Article, array('field1'), array('2'), '2=2');
  2911. $this->Dbo->update($Article, array('field1'), array("'value'"), array('index' => 'val'));
  2912. }
  2913. /**
  2914. * Test deletes with a mock.
  2915. *
  2916. * @return void
  2917. */
  2918. public function testDeleteStatements() {
  2919. $this->loadFixtures('Article', 'User');
  2920. $test = ConnectionManager::getDatasource('test');
  2921. $db = $test->config['database'];
  2922. $this->Dbo = $this->getMock('Mysql', array('execute'), array($test->config));
  2923. $this->Dbo->expects($this->at(0))->method('execute')
  2924. ->with("DELETE FROM `$db`.`articles` WHERE 1 = 1");
  2925. $this->Dbo->expects($this->at(1))->method('execute')
  2926. ->with("DELETE `Article` FROM `$db`.`articles` AS `Article` LEFT JOIN `$db`.`users` AS `User` " .
  2927. "ON (`Article`.`user_id` = `User`.`id`)" .
  2928. " WHERE 1 = 1");
  2929. $this->Dbo->expects($this->at(2))->method('execute')
  2930. ->with("DELETE `Article` FROM `$db`.`articles` AS `Article` LEFT JOIN `$db`.`users` AS `User` " .
  2931. "ON (`Article`.`user_id` = `User`.`id`)" .
  2932. " WHERE 2=2");
  2933. $Article = new Article();
  2934. $this->Dbo->delete($Article);
  2935. $this->Dbo->delete($Article, true);
  2936. $this->Dbo->delete($Article, '2=2');
  2937. }
  2938. /**
  2939. * Test truncate with a mock.
  2940. *
  2941. * @return void
  2942. */
  2943. public function testTruncateStatements() {
  2944. $this->loadFixtures('Article', 'User');
  2945. $db = ConnectionManager::getDatasource('test');
  2946. $schema = $db->config['database'];
  2947. $Article = new Article();
  2948. $this->Dbo = $this->getMock('Mysql', array('execute'), array($db->config));
  2949. $this->Dbo->expects($this->at(0))->method('execute')
  2950. ->with("TRUNCATE TABLE `$schema`.`articles`");
  2951. $this->Dbo->truncate($Article);
  2952. $this->Dbo->expects($this->at(0))->method('execute')
  2953. ->with("TRUNCATE TABLE `$schema`.`articles`");
  2954. $this->Dbo->truncate('articles');
  2955. // #2355: prevent duplicate prefix
  2956. $this->Dbo->config['prefix'] = 'tbl_';
  2957. $Article->tablePrefix = 'tbl_';
  2958. $this->Dbo->expects($this->at(0))->method('execute')
  2959. ->with("TRUNCATE TABLE `$schema`.`tbl_articles`");
  2960. $this->Dbo->truncate($Article);
  2961. $this->Dbo->expects($this->at(0))->method('execute')
  2962. ->with("TRUNCATE TABLE `$schema`.`tbl_articles`");
  2963. $this->Dbo->truncate('articles');
  2964. }
  2965. }