ModelIntegrationTest.php 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445
  1. <?php
  2. /**
  3. * ModelIntegrationTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  16. * @package Cake.Test.Case.Model
  17. * @since CakePHP(tm) v 1.2.0.4206
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
  21. App::uses('DboSource', 'Model/Datasource');
  22. App::uses('DboMock', 'Model/Datasource');
  23. /**
  24. * DboMock class
  25. * A Dbo Source driver to mock a connection and a identity name() method
  26. */
  27. class DboMock extends DboSource {
  28. /**
  29. * Returns the $field without modifications
  30. */
  31. public function name($field) {
  32. return $field;
  33. }
  34. /**
  35. * Returns true to fake a database connection
  36. */
  37. public function connect() {
  38. return true;
  39. }
  40. }
  41. /**
  42. * ModelIntegrationTest
  43. *
  44. * @package Cake.Test.Case.Model
  45. */
  46. class ModelIntegrationTest extends BaseModelTest {
  47. /**
  48. * testAssociationLazyLoading
  49. *
  50. * @group lazyloading
  51. * @return void
  52. */
  53. public function testAssociationLazyLoading() {
  54. $this->loadFixtures('ArticleFeaturedsTags');
  55. $Article = new ArticleFeatured();
  56. $this->assertTrue(isset($Article->belongsTo['User']));
  57. $this->assertFalse(property_exists($Article, 'User'));
  58. $this->assertInstanceOf('User', $Article->User);
  59. $this->assertTrue(isset($Article->belongsTo['Category']));
  60. $this->assertFalse(property_exists($Article, 'Category'));
  61. $this->assertTrue(isset($Article->Category));
  62. $this->assertInstanceOf('Category', $Article->Category);
  63. $this->assertTrue(isset($Article->hasMany['Comment']));
  64. $this->assertFalse(property_exists($Article, 'Comment'));
  65. $this->assertTrue(isset($Article->Comment));
  66. $this->assertInstanceOf('Comment', $Article->Comment);
  67. $this->assertTrue(isset($Article->hasAndBelongsToMany['Tag']));
  68. //There was not enough information to setup the association (joinTable and associationForeignKey)
  69. //so the model was not lazy loaded
  70. $this->assertTrue(property_exists($Article, 'Tag'));
  71. $this->assertTrue(isset($Article->Tag));
  72. $this->assertInstanceOf('Tag', $Article->Tag);
  73. $this->assertFalse(property_exists($Article, 'ArticleFeaturedsTag'));
  74. $this->assertInstanceOf('AppModel', $Article->ArticleFeaturedsTag);
  75. $this->assertEquals('article_featureds_tags', $Article->hasAndBelongsToMany['Tag']['joinTable']);
  76. $this->assertEquals('tag_id', $Article->hasAndBelongsToMany['Tag']['associationForeignKey']);
  77. }
  78. /**
  79. * testAssociationLazyLoadWithHABTM
  80. *
  81. * @group lazyloading
  82. * @return void
  83. */
  84. public function testAssociationLazyLoadWithHABTM() {
  85. $this->loadFixtures('FruitsUuidTag', 'ArticlesTag');
  86. $this->db->cacheSources = false;
  87. $Article = new ArticleB();
  88. $this->assertTrue(isset($Article->hasAndBelongsToMany['TagB']));
  89. $this->assertFalse(property_exists($Article, 'TagB'));
  90. $this->assertInstanceOf('TagB', $Article->TagB);
  91. $this->assertFalse(property_exists($Article, 'ArticlesTag'));
  92. $this->assertInstanceOf('AppModel', $Article->ArticlesTag);
  93. $UuidTag = new UuidTag();
  94. $this->assertTrue(isset($UuidTag->hasAndBelongsToMany['Fruit']));
  95. $this->assertFalse(property_exists($UuidTag, 'Fruit'));
  96. $this->assertFalse(property_exists($UuidTag, 'FruitsUuidTag'));
  97. $this->assertTrue(isset($UuidTag->Fruit));
  98. $this->assertFalse(property_exists($UuidTag, 'FruitsUuidTag'));
  99. $this->assertTrue(isset($UuidTag->FruitsUuidTag));
  100. $this->assertInstanceOf('FruitsUuidTag', $UuidTag->FruitsUuidTag);
  101. }
  102. /**
  103. * testAssociationLazyLoadWithBindModel
  104. *
  105. * @group lazyloading
  106. * @return void
  107. */
  108. public function testAssociationLazyLoadWithBindModel() {
  109. $this->loadFixtures('Article', 'User');
  110. $Article = new ArticleB();
  111. $this->assertFalse(isset($Article->belongsTo['User']));
  112. $this->assertFalse(property_exists($Article, 'User'));
  113. $Article->bindModel(array('belongsTo' => array('User')));
  114. $this->assertTrue(isset($Article->belongsTo['User']));
  115. $this->assertFalse(property_exists($Article, 'User'));
  116. $this->assertInstanceOf('User', $Article->User);
  117. }
  118. /**
  119. * Tests that creating a model with no existent database table associated will throw an exception
  120. *
  121. * @expectedException MissingTableException
  122. * @return void
  123. */
  124. public function testMissingTable() {
  125. $Article = new ArticleB(false, uniqid());
  126. $Article->schema();
  127. }
  128. /**
  129. * testPkInHAbtmLinkModelArticleB
  130. *
  131. * @return void
  132. */
  133. public function testPkInHabtmLinkModelArticleB() {
  134. $this->loadFixtures('Article', 'Tag', 'ArticlesTag');
  135. $TestModel = new ArticleB();
  136. $this->assertEquals('article_id', $TestModel->ArticlesTag->primaryKey);
  137. }
  138. /**
  139. * Tests that $cacheSources can only be disabled in the db using model settings, not enabled
  140. *
  141. * @return void
  142. */
  143. public function testCacheSourcesDisabling() {
  144. $this->loadFixtures('JoinA', 'JoinB', 'JoinAB', 'JoinC', 'JoinAC');
  145. $this->db->cacheSources = true;
  146. $TestModel = new JoinA();
  147. $TestModel->cacheSources = false;
  148. $TestModel->setSource('join_as');
  149. $this->assertFalse($this->db->cacheSources);
  150. $this->db->cacheSources = false;
  151. $TestModel = new JoinA();
  152. $TestModel->cacheSources = true;
  153. $TestModel->setSource('join_as');
  154. $this->assertFalse($this->db->cacheSources);
  155. }
  156. /**
  157. * testPkInHabtmLinkModel method
  158. *
  159. * @return void
  160. */
  161. public function testPkInHabtmLinkModel() {
  162. //Test Nonconformant Models
  163. $this->loadFixtures('Content', 'ContentAccount', 'Account', 'JoinC', 'JoinAC', 'ItemsPortfolio');
  164. $TestModel = new Content();
  165. $this->assertEquals('iContentAccountsId', $TestModel->ContentAccount->primaryKey);
  166. //test conformant models with no PK in the join table
  167. $this->loadFixtures('Article', 'Tag');
  168. $TestModel = new Article();
  169. $this->assertEquals('article_id', $TestModel->ArticlesTag->primaryKey);
  170. //test conformant models with PK in join table
  171. $TestModel = new Portfolio();
  172. $this->assertEquals('id', $TestModel->ItemsPortfolio->primaryKey);
  173. //test conformant models with PK in join table - join table contains extra field
  174. $this->loadFixtures('JoinA', 'JoinB', 'JoinAB');
  175. $TestModel = new JoinA();
  176. $this->assertEquals('id', $TestModel->JoinAsJoinB->primaryKey);
  177. }
  178. /**
  179. * testDynamicBehaviorAttachment method
  180. *
  181. * @return void
  182. */
  183. public function testDynamicBehaviorAttachment() {
  184. $this->loadFixtures('Apple', 'Sample', 'Author');
  185. $TestModel = new Apple();
  186. $this->assertEquals(array(), $TestModel->Behaviors->loaded());
  187. $TestModel->Behaviors->load('Tree', array('left' => 'left_field', 'right' => 'right_field'));
  188. $this->assertTrue(is_object($TestModel->Behaviors->Tree));
  189. $this->assertEquals(array('Tree'), $TestModel->Behaviors->loaded());
  190. $expected = array(
  191. 'parent' => 'parent_id',
  192. 'left' => 'left_field',
  193. 'right' => 'right_field',
  194. 'scope' => '1 = 1',
  195. 'type' => 'nested',
  196. '__parentChange' => false,
  197. 'recursive' => -1
  198. );
  199. $this->assertEquals($expected, $TestModel->Behaviors->Tree->settings['Apple']);
  200. $TestModel->Behaviors->load('Tree', array('enabled' => false));
  201. $this->assertEquals($expected, $TestModel->Behaviors->Tree->settings['Apple']);
  202. $this->assertEquals(array('Tree'), $TestModel->Behaviors->loaded());
  203. $TestModel->Behaviors->unload('Tree');
  204. $this->assertEquals(array(), $TestModel->Behaviors->loaded());
  205. $this->assertFalse(isset($TestModel->Behaviors->Tree));
  206. }
  207. /**
  208. * testFindWithJoinsOption method
  209. *
  210. * @return void
  211. */
  212. public function testFindWithJoinsOption() {
  213. $this->loadFixtures('Article', 'User');
  214. $TestUser = new User();
  215. $options = array(
  216. 'fields' => array(
  217. 'user',
  218. 'Article.published',
  219. ),
  220. 'joins' => array(
  221. array(
  222. 'table' => 'articles',
  223. 'alias' => 'Article',
  224. 'type' => 'LEFT',
  225. 'conditions' => array(
  226. 'User.id = Article.user_id',
  227. ),
  228. ),
  229. ),
  230. 'group' => array('User.user', 'Article.published'),
  231. 'recursive' => -1,
  232. 'order' => array('User.user')
  233. );
  234. $result = $TestUser->find('all', $options);
  235. $expected = array(
  236. array('User' => array('user' => 'garrett'), 'Article' => array('published' => '')),
  237. array('User' => array('user' => 'larry'), 'Article' => array('published' => 'Y')),
  238. array('User' => array('user' => 'mariano'), 'Article' => array('published' => 'Y')),
  239. array('User' => array('user' => 'nate'), 'Article' => array('published' => ''))
  240. );
  241. $this->assertEquals($expected, $result);
  242. }
  243. /**
  244. * Tests cross database joins. Requires $test and $test2 to both be set in DATABASE_CONFIG
  245. * NOTE: When testing on MySQL, you must set 'persistent' => false on *both* database connections,
  246. * or one connection will step on the other.
  247. */
  248. public function testCrossDatabaseJoins() {
  249. $config = ConnectionManager::enumConnectionObjects();
  250. $skip = (!isset($config['test']) || !isset($config['test2']));
  251. if ($skip) {
  252. $this->markTestSkipped('Primary and secondary test databases not configured, skipping cross-database
  253. join tests. To run theses tests defined $test and $test2 in your database configuration.'
  254. );
  255. }
  256. $this->loadFixtures('Article', 'Tag', 'ArticlesTag', 'User', 'Comment');
  257. $TestModel = new Article();
  258. $expected = array(
  259. array(
  260. 'Article' => array(
  261. 'id' => '1',
  262. 'user_id' => '1',
  263. 'title' => 'First Article',
  264. 'body' => 'First Article Body',
  265. 'published' => 'Y',
  266. 'created' => '2007-03-18 10:39:23',
  267. 'updated' => '2007-03-18 10:41:31'
  268. ),
  269. 'User' => array(
  270. 'id' => '1',
  271. 'user' => 'mariano',
  272. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  273. 'created' => '2007-03-17 01:16:23',
  274. 'updated' => '2007-03-17 01:18:31'
  275. ),
  276. 'Comment' => array(
  277. array(
  278. 'id' => '1',
  279. 'article_id' => '1',
  280. 'user_id' => '2',
  281. 'comment' => 'First Comment for First Article',
  282. 'published' => 'Y',
  283. 'created' => '2007-03-18 10:45:23',
  284. 'updated' => '2007-03-18 10:47:31'
  285. ),
  286. array(
  287. 'id' => '2',
  288. 'article_id' => '1',
  289. 'user_id' => '4',
  290. 'comment' => 'Second Comment for First Article',
  291. 'published' => 'Y',
  292. 'created' => '2007-03-18 10:47:23',
  293. 'updated' => '2007-03-18 10:49:31'
  294. ),
  295. array(
  296. 'id' => '3',
  297. 'article_id' => '1',
  298. 'user_id' => '1',
  299. 'comment' => 'Third Comment for First Article',
  300. 'published' => 'Y',
  301. 'created' => '2007-03-18 10:49:23',
  302. 'updated' => '2007-03-18 10:51:31'
  303. ),
  304. array(
  305. 'id' => '4',
  306. 'article_id' => '1',
  307. 'user_id' => '1',
  308. 'comment' => 'Fourth Comment for First Article',
  309. 'published' => 'N',
  310. 'created' => '2007-03-18 10:51:23',
  311. 'updated' => '2007-03-18 10:53:31'
  312. )),
  313. 'Tag' => array(
  314. array(
  315. 'id' => '1',
  316. 'tag' => 'tag1',
  317. 'created' => '2007-03-18 12:22:23',
  318. 'updated' => '2007-03-18 12:24:31'
  319. ),
  320. array(
  321. 'id' => '2',
  322. 'tag' => 'tag2',
  323. 'created' => '2007-03-18 12:24:23',
  324. 'updated' => '2007-03-18 12:26:31'
  325. ))),
  326. array(
  327. 'Article' => array(
  328. 'id' => '2',
  329. 'user_id' => '3',
  330. 'title' => 'Second Article',
  331. 'body' => 'Second Article Body',
  332. 'published' => 'Y',
  333. 'created' => '2007-03-18 10:41:23',
  334. 'updated' => '2007-03-18 10:43:31'
  335. ),
  336. 'User' => array(
  337. 'id' => '3',
  338. 'user' => 'larry',
  339. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  340. 'created' => '2007-03-17 01:20:23',
  341. 'updated' => '2007-03-17 01:22:31'
  342. ),
  343. 'Comment' => array(
  344. array(
  345. 'id' => '5',
  346. 'article_id' => '2',
  347. 'user_id' => '1',
  348. 'comment' => 'First Comment for Second Article',
  349. 'published' => 'Y',
  350. 'created' => '2007-03-18 10:53:23',
  351. 'updated' => '2007-03-18 10:55:31'
  352. ),
  353. array(
  354. 'id' => '6',
  355. 'article_id' => '2',
  356. 'user_id' => '2',
  357. 'comment' => 'Second Comment for Second Article',
  358. 'published' => 'Y',
  359. 'created' => '2007-03-18 10:55:23',
  360. 'updated' => '2007-03-18 10:57:31'
  361. )),
  362. 'Tag' => array(
  363. array(
  364. 'id' => '1',
  365. 'tag' => 'tag1',
  366. 'created' => '2007-03-18 12:22:23',
  367. 'updated' => '2007-03-18 12:24:31'
  368. ),
  369. array(
  370. 'id' => '3',
  371. 'tag' => 'tag3',
  372. 'created' => '2007-03-18 12:26:23',
  373. 'updated' => '2007-03-18 12:28:31'
  374. ))),
  375. array(
  376. 'Article' => array(
  377. 'id' => '3',
  378. 'user_id' => '1',
  379. 'title' => 'Third Article',
  380. 'body' => 'Third Article Body',
  381. 'published' => 'Y',
  382. 'created' => '2007-03-18 10:43:23',
  383. 'updated' => '2007-03-18 10:45:31'
  384. ),
  385. 'User' => array(
  386. 'id' => '1',
  387. 'user' => 'mariano',
  388. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  389. 'created' => '2007-03-17 01:16:23',
  390. 'updated' => '2007-03-17 01:18:31'
  391. ),
  392. 'Comment' => array(),
  393. 'Tag' => array()
  394. ));
  395. $this->assertEquals($expected, $TestModel->find('all'));
  396. $db2 = ConnectionManager::getDataSource('test2');
  397. $this->fixtureManager->loadSingle('User', $db2);
  398. $this->fixtureManager->loadSingle('Comment', $db2);
  399. $this->assertEquals(3, $TestModel->find('count'));
  400. $TestModel->User->setDataSource('test2');
  401. $TestModel->Comment->setDataSource('test2');
  402. foreach ($expected as $key => $value) {
  403. unset($value['Comment'], $value['Tag']);
  404. $expected[$key] = $value;
  405. }
  406. $TestModel->recursive = 0;
  407. $result = $TestModel->find('all');
  408. $this->assertEquals($expected, $result);
  409. foreach ($expected as $key => $value) {
  410. unset($value['Comment'], $value['Tag']);
  411. $expected[$key] = $value;
  412. }
  413. $TestModel->recursive = 0;
  414. $result = $TestModel->find('all');
  415. $this->assertEquals($expected, $result);
  416. $result = Hash::extract($TestModel->User->find('all'), '{n}.User.id');
  417. $this->assertEquals(array('1', '2', '3', '4'), $result);
  418. $this->assertEquals($expected, $TestModel->find('all'));
  419. $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')));
  420. $expected = array(
  421. array(
  422. 'Comment' => array(
  423. 'id' => '1',
  424. 'article_id' => '1',
  425. 'user_id' => '2',
  426. 'comment' => 'First Comment for First Article',
  427. 'published' => 'Y',
  428. 'created' => '2007-03-18 10:45:23',
  429. 'updated' => '2007-03-18 10:47:31'
  430. ),
  431. 'User' => array(
  432. 'id' => '2',
  433. 'user' => 'nate',
  434. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  435. 'created' => '2007-03-17 01:18:23',
  436. 'updated' => '2007-03-17 01:20:31'
  437. ),
  438. 'Article' => array(
  439. 'id' => '1',
  440. 'user_id' => '1',
  441. 'title' => 'First Article',
  442. 'body' => 'First Article Body',
  443. 'published' => 'Y',
  444. 'created' => '2007-03-18 10:39:23',
  445. 'updated' => '2007-03-18 10:41:31'
  446. )),
  447. array(
  448. 'Comment' => array(
  449. 'id' => '2',
  450. 'article_id' => '1',
  451. 'user_id' => '4',
  452. 'comment' => 'Second Comment for First Article',
  453. 'published' => 'Y',
  454. 'created' => '2007-03-18 10:47:23',
  455. 'updated' => '2007-03-18 10:49:31'
  456. ),
  457. 'User' => array(
  458. 'id' => '4',
  459. 'user' => 'garrett',
  460. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  461. 'created' => '2007-03-17 01:22:23',
  462. 'updated' => '2007-03-17 01:24:31'
  463. ),
  464. 'Article' => array(
  465. 'id' => '1',
  466. 'user_id' => '1',
  467. 'title' => 'First Article',
  468. 'body' => 'First Article Body',
  469. 'published' => 'Y',
  470. 'created' => '2007-03-18 10:39:23',
  471. 'updated' => '2007-03-18 10:41:31'
  472. )),
  473. array(
  474. 'Comment' => array(
  475. 'id' => '3',
  476. 'article_id' => '1',
  477. 'user_id' => '1',
  478. 'comment' => 'Third Comment for First Article',
  479. 'published' => 'Y',
  480. 'created' => '2007-03-18 10:49:23',
  481. 'updated' => '2007-03-18 10:51:31'
  482. ),
  483. 'User' => array(
  484. 'id' => '1',
  485. 'user' => 'mariano',
  486. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  487. 'created' => '2007-03-17 01:16:23',
  488. 'updated' => '2007-03-17 01:18:31'
  489. ),
  490. 'Article' => array(
  491. 'id' => '1',
  492. 'user_id' => '1',
  493. 'title' => 'First Article',
  494. 'body' => 'First Article Body',
  495. 'published' => 'Y',
  496. 'created' => '2007-03-18 10:39:23',
  497. 'updated' => '2007-03-18 10:41:31'
  498. )),
  499. array(
  500. 'Comment' => array(
  501. 'id' => '4',
  502. 'article_id' => '1',
  503. 'user_id' => '1',
  504. 'comment' => 'Fourth Comment for First Article',
  505. 'published' => 'N',
  506. 'created' => '2007-03-18 10:51:23',
  507. 'updated' => '2007-03-18 10:53:31'
  508. ),
  509. 'User' => array(
  510. 'id' => '1',
  511. 'user' => 'mariano',
  512. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  513. 'created' => '2007-03-17 01:16:23',
  514. 'updated' => '2007-03-17 01:18:31'
  515. ),
  516. 'Article' => array(
  517. 'id' => '1',
  518. 'user_id' => '1',
  519. 'title' => 'First Article',
  520. 'body' => 'First Article Body',
  521. 'published' => 'Y',
  522. 'created' => '2007-03-18 10:39:23',
  523. 'updated' => '2007-03-18 10:41:31'
  524. )),
  525. array(
  526. 'Comment' => array(
  527. 'id' => '5',
  528. 'article_id' => '2',
  529. 'user_id' => '1',
  530. 'comment' => 'First Comment for Second Article',
  531. 'published' => 'Y',
  532. 'created' => '2007-03-18 10:53:23',
  533. 'updated' => '2007-03-18 10:55:31'
  534. ),
  535. 'User' => array(
  536. 'id' => '1',
  537. 'user' => 'mariano',
  538. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  539. 'created' => '2007-03-17 01:16:23',
  540. 'updated' => '2007-03-17 01:18:31'
  541. ),
  542. 'Article' => array(
  543. 'id' => '2',
  544. 'user_id' => '3',
  545. 'title' => 'Second Article',
  546. 'body' => 'Second Article Body',
  547. 'published' => 'Y',
  548. 'created' => '2007-03-18 10:41:23',
  549. 'updated' => '2007-03-18 10:43:31'
  550. )),
  551. array(
  552. 'Comment' => array(
  553. 'id' => '6',
  554. 'article_id' => '2',
  555. 'user_id' => '2',
  556. 'comment' => 'Second Comment for Second Article',
  557. 'published' => 'Y',
  558. 'created' => '2007-03-18 10:55:23',
  559. 'updated' => '2007-03-18 10:57:31'
  560. ),
  561. 'User' => array(
  562. 'id' => '2',
  563. 'user' => 'nate',
  564. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  565. 'created' => '2007-03-17 01:18:23',
  566. 'updated' => '2007-03-17 01:20:31'
  567. ),
  568. 'Article' => array(
  569. 'id' => '2',
  570. 'user_id' => '3',
  571. 'title' => 'Second Article',
  572. 'body' => 'Second Article Body',
  573. 'published' => 'Y',
  574. 'created' => '2007-03-18 10:41:23',
  575. 'updated' => '2007-03-18 10:43:31'
  576. )));
  577. $this->assertEquals($expected, $TestModel->Comment->find('all'));
  578. }
  579. /**
  580. * test HABM operations without clobbering existing records #275
  581. *
  582. * @return void
  583. */
  584. public function testHABTMKeepExisting() {
  585. $this->loadFixtures('Site', 'Domain', 'DomainsSite');
  586. $Site = new Site();
  587. $results = $Site->find('count');
  588. $expected = 3;
  589. $this->assertEquals($expected, $results);
  590. $data = $Site->findById(1);
  591. // include api.cakephp.org
  592. $data['Domain'] = array('Domain' => array(1, 2, 3));
  593. $Site->save($data);
  594. $Site->id = 1;
  595. $results = $Site->read();
  596. $expected = 3; // 3 domains belonging to cakephp
  597. $this->assertEquals($expected, count($results['Domain']));
  598. $Site->id = 2;
  599. $results = $Site->read();
  600. $expected = 2; // 2 domains belonging to markstory
  601. $this->assertEquals($expected, count($results['Domain']));
  602. $Site->id = 3;
  603. $results = $Site->read();
  604. $expected = 2;
  605. $this->assertEquals($expected, count($results['Domain']));
  606. $results['Domain'] = array('Domain' => array(7));
  607. $Site->save($results); // remove association from domain 6
  608. $results = $Site->read();
  609. $expected = 1; // only 1 domain left belonging to rchavik
  610. $this->assertEquals($expected, count($results['Domain']));
  611. // add deleted domain back
  612. $results['Domain'] = array('Domain' => array(6, 7));
  613. $Site->save($results);
  614. $results = $Site->read();
  615. $expected = 2; // 2 domains belonging to rchavik
  616. $this->assertEquals($expected, count($results['Domain']));
  617. $Site->DomainsSite->id = $results['Domain'][0]['DomainsSite']['id'];
  618. $Site->DomainsSite->saveField('active', true);
  619. $results = $Site->Domain->DomainsSite->find('count', array(
  620. 'conditions' => array(
  621. 'DomainsSite.active' => true,
  622. ),
  623. ));
  624. $expected = 5;
  625. $this->assertEquals($expected, $results);
  626. // activate api.cakephp.org
  627. $activated = $Site->DomainsSite->findByDomainId(3);
  628. $activated['DomainsSite']['active'] = true;
  629. $Site->DomainsSite->save($activated);
  630. $results = $Site->DomainsSite->find('count', array(
  631. 'conditions' => array(
  632. 'DomainsSite.active' => true,
  633. ),
  634. ));
  635. $expected = 6;
  636. $this->assertEquals($expected, $results);
  637. // remove 2 previously active domains, and leave $activated alone
  638. $data = array(
  639. 'Site' => array('id' => 1, 'name' => 'cakephp (modified)'),
  640. 'Domain' => array(
  641. 'Domain' => array(3),
  642. )
  643. );
  644. $Site->create($data);
  645. $Site->save($data);
  646. // tests that record is still identical prior to removal
  647. $Site->id = 1;
  648. $results = $Site->read();
  649. unset($results['Domain'][0]['DomainsSite']['updated']);
  650. unset($activated['DomainsSite']['updated']);
  651. $this->assertEquals($activated['DomainsSite'], $results['Domain'][0]['DomainsSite']);
  652. }
  653. /**
  654. * testHABTMKeepExistingAlternateDataFormat
  655. *
  656. * @return void
  657. */
  658. public function testHABTMKeepExistingAlternateDataFormat() {
  659. $this->loadFixtures('Site', 'Domain', 'DomainsSite');
  660. $Site = new Site();
  661. $expected = array(
  662. array(
  663. 'DomainsSite' => array(
  664. 'id' => 1,
  665. 'site_id' => 1,
  666. 'domain_id' => 1,
  667. 'active' => true,
  668. 'created' => '2007-03-17 01:16:23'
  669. )
  670. ),
  671. array(
  672. 'DomainsSite' => array(
  673. 'id' => 2,
  674. 'site_id' => 1,
  675. 'domain_id' => 2,
  676. 'active' => true,
  677. 'created' => '2007-03-17 01:16:23'
  678. )
  679. )
  680. );
  681. $result = $Site->DomainsSite->find('all', array(
  682. 'conditions' => array('DomainsSite.site_id' => 1),
  683. 'fields' => array(
  684. 'DomainsSite.id',
  685. 'DomainsSite.site_id',
  686. 'DomainsSite.domain_id',
  687. 'DomainsSite.active',
  688. 'DomainsSite.created'
  689. ),
  690. 'order' => 'DomainsSite.id'
  691. ));
  692. $this->assertEquals($expected, $result);
  693. $time = date('Y-m-d H:i:s');
  694. $data = array(
  695. 'Site' => array(
  696. 'id' => 1
  697. ),
  698. 'Domain' => array(
  699. array(
  700. 'site_id' => 1,
  701. 'domain_id' => 3,
  702. 'created' => $time,
  703. ),
  704. array(
  705. 'id' => 2,
  706. 'site_id' => 1,
  707. 'domain_id' => 2
  708. ),
  709. )
  710. );
  711. $Site->save($data);
  712. $expected = array(
  713. array(
  714. 'DomainsSite' => array(
  715. 'id' => 2,
  716. 'site_id' => 1,
  717. 'domain_id' => 2,
  718. 'active' => true,
  719. 'created' => '2007-03-17 01:16:23'
  720. )
  721. ),
  722. array(
  723. 'DomainsSite' => array(
  724. 'id' => 7,
  725. 'site_id' => 1,
  726. 'domain_id' => 3,
  727. 'active' => false,
  728. 'created' => $time
  729. )
  730. )
  731. );
  732. $result = $Site->DomainsSite->find('all', array(
  733. 'conditions' => array('DomainsSite.site_id' => 1),
  734. 'fields' => array(
  735. 'DomainsSite.id',
  736. 'DomainsSite.site_id',
  737. 'DomainsSite.domain_id',
  738. 'DomainsSite.active',
  739. 'DomainsSite.created'
  740. ),
  741. 'order' => 'DomainsSite.id'
  742. ));
  743. $this->assertEquals($expected, $result);
  744. }
  745. /**
  746. * test HABM operations without clobbering existing records #275
  747. *
  748. * @return void
  749. */
  750. public function testHABTMKeepExistingWithThreeDbs() {
  751. $config = ConnectionManager::enumConnectionObjects();
  752. $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with Sqlite.');
  753. $this->skipIf(
  754. !isset($config['test']) || !isset($config['test2']) || !isset($config['test_database_three']),
  755. 'Primary, secondary, and tertiary test databases not configured, skipping test. To run this test define $test, $test2, and $test_database_three in your database configuration.'
  756. );
  757. $this->loadFixtures('Player', 'Guild', 'GuildsPlayer', 'Armor', 'ArmorsPlayer');
  758. $Player = ClassRegistry::init('Player');
  759. $Player->bindModel(array(
  760. 'hasAndBelongsToMany' => array(
  761. 'Armor' => array(
  762. 'with' => 'ArmorsPlayer',
  763. 'unique' => 'keepExisting',
  764. ),
  765. ),
  766. ), false);
  767. $this->assertEquals('test', $Player->useDbConfig);
  768. $this->assertEquals('test', $Player->Guild->useDbConfig);
  769. $this->assertEquals('test2', $Player->Guild->GuildsPlayer->useDbConfig);
  770. $this->assertEquals('test2', $Player->Armor->useDbConfig);
  771. $this->assertEquals('test_database_three', $Player->ArmorsPlayer->useDbConfig);
  772. $players = $Player->find('all');
  773. $this->assertEquals(4, count($players));
  774. $playersGuilds = Hash::extract($players, '{n}.Guild.{n}.GuildsPlayer');
  775. $this->assertEquals(3, count($playersGuilds));
  776. $playersArmors = Hash::extract($players, '{n}.Armor.{n}.ArmorsPlayer');
  777. $this->assertEquals(3, count($playersArmors));
  778. unset($players);
  779. $larry = $Player->findByName('larry');
  780. $larrysArmor = Hash::extract($larry, 'Armor.{n}.ArmorsPlayer');
  781. $this->assertEquals(1, count($larrysArmor));
  782. $larry['Guild']['Guild'] = array(1, 3); // larry joins another guild
  783. $larry['Armor']['Armor'] = array(2, 3); // purchases chainmail
  784. $Player->save($larry);
  785. unset($larry);
  786. $larry = $Player->findByName('larry');
  787. $larrysGuild = Hash::extract($larry, 'Guild.{n}.GuildsPlayer');
  788. $this->assertEquals(2, count($larrysGuild));
  789. $larrysArmor = Hash::extract($larry, 'Armor.{n}.ArmorsPlayer');
  790. $this->assertEquals(2, count($larrysArmor));
  791. $Player->ArmorsPlayer->id = 3;
  792. $Player->ArmorsPlayer->saveField('broken', true); // larry's cloak broke
  793. $larry = $Player->findByName('larry');
  794. $larrysCloak = Hash::extract($larry, 'Armor.{n}.ArmorsPlayer[armor_id=3]', $larry);
  795. $this->assertNotEmpty($larrysCloak);
  796. $this->assertTrue($larrysCloak[0]['broken']); // still broken
  797. }
  798. /**
  799. * testDisplayField method
  800. *
  801. * @return void
  802. */
  803. public function testDisplayField() {
  804. $this->loadFixtures('Post', 'Comment', 'Person', 'User');
  805. $Post = new Post();
  806. $Comment = new Comment();
  807. $Person = new Person();
  808. $this->assertEquals('title', $Post->displayField);
  809. $this->assertEquals('name', $Person->displayField);
  810. $this->assertEquals('id', $Comment->displayField);
  811. }
  812. /**
  813. * testSchema method
  814. *
  815. * @return void
  816. */
  817. public function testSchema() {
  818. $Post = new Post();
  819. $result = $Post->schema();
  820. $columns = array('id', 'author_id', 'title', 'body', 'published', 'created', 'updated');
  821. $this->assertEquals($columns, array_keys($result));
  822. $types = array('integer', 'integer', 'string', 'text', 'string', 'datetime', 'datetime');
  823. $this->assertEquals(Hash::extract(array_values($result), '{n}.type'), $types);
  824. $result = $Post->schema('body');
  825. $this->assertEquals('text', $result['type']);
  826. $this->assertNull($Post->schema('foo'));
  827. $this->assertEquals($Post->getColumnTypes(), array_combine($columns, $types));
  828. }
  829. /**
  830. * Check schema() on a model with useTable = false;
  831. *
  832. * @return void
  833. */
  834. public function testSchemaUseTableFalse() {
  835. $model = new TheVoid();
  836. $result = $model->schema();
  837. $this->assertNull($result);
  838. $result = $model->create();
  839. $this->assertEmpty($result);
  840. }
  841. /**
  842. * data provider for time tests.
  843. *
  844. * @return array
  845. */
  846. public static function timeProvider() {
  847. $db = ConnectionManager::getDataSource('test');
  848. $now = $db->expression('NOW()');
  849. return array(
  850. // blank
  851. array(
  852. array('hour' => '', 'min' => '', 'meridian' => ''),
  853. ''
  854. ),
  855. // missing hour
  856. array(
  857. array('hour' => '', 'min' => '00', 'meridian' => 'pm'),
  858. ''
  859. ),
  860. // all blank
  861. array(
  862. array('hour' => '', 'min' => '', 'sec' => ''),
  863. ''
  864. ),
  865. // set and empty merdian
  866. array(
  867. array('hour' => '1', 'min' => '00', 'meridian' => ''),
  868. ''
  869. ),
  870. // midnight
  871. array(
  872. array('hour' => '12', 'min' => '0', 'meridian' => 'am'),
  873. '00:00:00'
  874. ),
  875. array(
  876. array('hour' => '00', 'min' => '00'),
  877. '00:00:00'
  878. ),
  879. // 3am
  880. array(
  881. array('hour' => '03', 'min' => '04', 'sec' => '04'),
  882. '03:04:04'
  883. ),
  884. array(
  885. array('hour' => '3', 'min' => '4', 'sec' => '4'),
  886. '03:04:04'
  887. ),
  888. array(
  889. array('hour' => '03', 'min' => '4', 'sec' => '4'),
  890. '03:04:04'
  891. ),
  892. array(
  893. $now,
  894. $now
  895. )
  896. );
  897. }
  898. /**
  899. * test deconstruct with time fields.
  900. *
  901. * @dataProvider timeProvider
  902. * @return void
  903. */
  904. public function testDeconstructFieldsTime($input, $result) {
  905. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  906. $this->loadFixtures('Apple');
  907. $TestModel = new Apple();
  908. $data = array(
  909. 'Apple' => array(
  910. 'mytime' => $input
  911. )
  912. );
  913. $TestModel->data = null;
  914. $TestModel->set($data);
  915. $expected = array('Apple' => array('mytime' => $result));
  916. $this->assertEquals($expected, $TestModel->data);
  917. }
  918. /**
  919. * testDeconstructFields with datetime, timestamp, and date fields
  920. *
  921. * @return void
  922. */
  923. public function testDeconstructFieldsDateTime() {
  924. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  925. $this->loadFixtures('Apple');
  926. $TestModel = new Apple();
  927. //test null/empty values first
  928. $data['Apple']['created']['year'] = '';
  929. $data['Apple']['created']['month'] = '';
  930. $data['Apple']['created']['day'] = '';
  931. $data['Apple']['created']['hour'] = '';
  932. $data['Apple']['created']['min'] = '';
  933. $data['Apple']['created']['sec'] = '';
  934. $TestModel->data = null;
  935. $TestModel->set($data);
  936. $expected = array('Apple' => array('created' => ''));
  937. $this->assertEquals($expected, $TestModel->data);
  938. $data = array();
  939. $data['Apple']['date']['year'] = '';
  940. $data['Apple']['date']['month'] = '';
  941. $data['Apple']['date']['day'] = '';
  942. $TestModel->data = null;
  943. $TestModel->set($data);
  944. $expected = array('Apple' => array('date' => ''));
  945. $this->assertEquals($expected, $TestModel->data);
  946. $data = array();
  947. $data['Apple']['created']['year'] = '2007';
  948. $data['Apple']['created']['month'] = '08';
  949. $data['Apple']['created']['day'] = '20';
  950. $data['Apple']['created']['hour'] = '';
  951. $data['Apple']['created']['min'] = '';
  952. $data['Apple']['created']['sec'] = '';
  953. $TestModel->data = null;
  954. $TestModel->set($data);
  955. $expected = array('Apple' => array('created' => '2007-08-20 00:00:00'));
  956. $this->assertEquals($expected, $TestModel->data);
  957. $data = array();
  958. $data['Apple']['created']['year'] = '2007';
  959. $data['Apple']['created']['month'] = '08';
  960. $data['Apple']['created']['day'] = '20';
  961. $data['Apple']['created']['hour'] = '10';
  962. $data['Apple']['created']['min'] = '12';
  963. $data['Apple']['created']['sec'] = '';
  964. $TestModel->data = null;
  965. $TestModel->set($data);
  966. $expected = array('Apple' => array('created' => '2007-08-20 10:12:00'));
  967. $this->assertEquals($expected, $TestModel->data);
  968. $data = array();
  969. $data['Apple']['created']['year'] = '2007';
  970. $data['Apple']['created']['month'] = '';
  971. $data['Apple']['created']['day'] = '12';
  972. $data['Apple']['created']['hour'] = '20';
  973. $data['Apple']['created']['min'] = '';
  974. $data['Apple']['created']['sec'] = '';
  975. $TestModel->data = null;
  976. $TestModel->set($data);
  977. $expected = array('Apple' => array('created' => ''));
  978. $this->assertEquals($expected, $TestModel->data);
  979. $data = array();
  980. $data['Apple']['created']['hour'] = '20';
  981. $data['Apple']['created']['min'] = '33';
  982. $TestModel->data = null;
  983. $TestModel->set($data);
  984. $expected = array('Apple' => array('created' => ''));
  985. $this->assertEquals($expected, $TestModel->data);
  986. $data = array();
  987. $data['Apple']['created']['hour'] = '20';
  988. $data['Apple']['created']['min'] = '33';
  989. $data['Apple']['created']['sec'] = '33';
  990. $TestModel->data = null;
  991. $TestModel->set($data);
  992. $expected = array('Apple' => array('created' => ''));
  993. $this->assertEquals($expected, $TestModel->data);
  994. $data = array();
  995. $data['Apple']['created']['hour'] = '13';
  996. $data['Apple']['created']['min'] = '00';
  997. $data['Apple']['date']['year'] = '2006';
  998. $data['Apple']['date']['month'] = '12';
  999. $data['Apple']['date']['day'] = '25';
  1000. $TestModel->data = null;
  1001. $TestModel->set($data);
  1002. $expected = array(
  1003. 'Apple' => array(
  1004. 'created' => '',
  1005. 'date' => '2006-12-25'
  1006. ));
  1007. $this->assertEquals($expected, $TestModel->data);
  1008. $data = array();
  1009. $data['Apple']['created']['year'] = '2007';
  1010. $data['Apple']['created']['month'] = '08';
  1011. $data['Apple']['created']['day'] = '20';
  1012. $data['Apple']['created']['hour'] = '10';
  1013. $data['Apple']['created']['min'] = '12';
  1014. $data['Apple']['created']['sec'] = '09';
  1015. $data['Apple']['date']['year'] = '2006';
  1016. $data['Apple']['date']['month'] = '12';
  1017. $data['Apple']['date']['day'] = '25';
  1018. $TestModel->data = null;
  1019. $TestModel->set($data);
  1020. $expected = array(
  1021. 'Apple' => array(
  1022. 'created' => '2007-08-20 10:12:09',
  1023. 'date' => '2006-12-25'
  1024. ));
  1025. $this->assertEquals($expected, $TestModel->data);
  1026. $data = array();
  1027. $data['Apple']['created']['year'] = '--';
  1028. $data['Apple']['created']['month'] = '--';
  1029. $data['Apple']['created']['day'] = '--';
  1030. $data['Apple']['created']['hour'] = '--';
  1031. $data['Apple']['created']['min'] = '--';
  1032. $data['Apple']['created']['sec'] = '--';
  1033. $data['Apple']['date']['year'] = '--';
  1034. $data['Apple']['date']['month'] = '--';
  1035. $data['Apple']['date']['day'] = '--';
  1036. $TestModel->data = null;
  1037. $TestModel->set($data);
  1038. $expected = array('Apple' => array('created' => '', 'date' => ''));
  1039. $this->assertEquals($expected, $TestModel->data);
  1040. $data = array();
  1041. $data['Apple']['created']['year'] = '2007';
  1042. $data['Apple']['created']['month'] = '--';
  1043. $data['Apple']['created']['day'] = '20';
  1044. $data['Apple']['created']['hour'] = '10';
  1045. $data['Apple']['created']['min'] = '12';
  1046. $data['Apple']['created']['sec'] = '09';
  1047. $data['Apple']['date']['year'] = '2006';
  1048. $data['Apple']['date']['month'] = '12';
  1049. $data['Apple']['date']['day'] = '25';
  1050. $TestModel->data = null;
  1051. $TestModel->set($data);
  1052. $expected = array('Apple' => array('created' => '', 'date' => '2006-12-25'));
  1053. $this->assertEquals($expected, $TestModel->data);
  1054. $data = array();
  1055. $data['Apple']['date']['year'] = '2006';
  1056. $data['Apple']['date']['month'] = '12';
  1057. $data['Apple']['date']['day'] = '25';
  1058. $TestModel->data = null;
  1059. $TestModel->set($data);
  1060. $expected = array('Apple' => array('date' => '2006-12-25'));
  1061. $this->assertEquals($expected, $TestModel->data);
  1062. $db = ConnectionManager::getDataSource('test');
  1063. $data = array();
  1064. $data['Apple']['modified'] = $db->expression('NOW()');
  1065. $TestModel->data = null;
  1066. $TestModel->set($data);
  1067. $this->assertEquals($TestModel->data, $data);
  1068. }
  1069. /**
  1070. * testTablePrefixSwitching method
  1071. *
  1072. * @return void
  1073. */
  1074. public function testTablePrefixSwitching() {
  1075. ConnectionManager::create('database1',
  1076. array_merge($this->db->config, array('prefix' => 'aaa_')
  1077. ));
  1078. ConnectionManager::create('database2',
  1079. array_merge($this->db->config, array('prefix' => 'bbb_')
  1080. ));
  1081. $db1 = ConnectionManager::getDataSource('database1');
  1082. $db2 = ConnectionManager::getDataSource('database2');
  1083. $TestModel = new Apple();
  1084. $TestModel->setDataSource('database1');
  1085. $this->assertContains('aaa_apples', $this->db->fullTableName($TestModel));
  1086. $this->assertContains('aaa_apples', $db1->fullTableName($TestModel));
  1087. $this->assertContains('aaa_apples', $db2->fullTableName($TestModel));
  1088. $TestModel->setDataSource('database2');
  1089. $this->assertContains('bbb_apples', $this->db->fullTableName($TestModel));
  1090. $this->assertContains('bbb_apples', $db1->fullTableName($TestModel));
  1091. $this->assertContains('bbb_apples', $db2->fullTableName($TestModel));
  1092. $TestModel = new Apple();
  1093. $TestModel->tablePrefix = 'custom_';
  1094. $this->assertContains('custom_apples', $this->db->fullTableName($TestModel));
  1095. $TestModel->setDataSource('database1');
  1096. $this->assertContains('custom_apples', $this->db->fullTableName($TestModel));
  1097. $this->assertContains('custom_apples', $db1->fullTableName($TestModel));
  1098. $TestModel = new Apple();
  1099. $TestModel->setDataSource('database1');
  1100. $this->assertContains('aaa_apples', $this->db->fullTableName($TestModel));
  1101. $TestModel->tablePrefix = '';
  1102. $TestModel->setDataSource('database2');
  1103. $this->assertContains('apples', $db2->fullTableName($TestModel));
  1104. $this->assertContains('apples', $db1->fullTableName($TestModel));
  1105. $TestModel->tablePrefix = null;
  1106. $TestModel->setDataSource('database1');
  1107. $this->assertContains('aaa_apples', $db2->fullTableName($TestModel));
  1108. $this->assertContains('aaa_apples', $db1->fullTableName($TestModel));
  1109. $TestModel->tablePrefix = false;
  1110. $TestModel->setDataSource('database2');
  1111. $this->assertContains('apples', $db2->fullTableName($TestModel));
  1112. $this->assertContains('apples', $db1->fullTableName($TestModel));
  1113. }
  1114. /**
  1115. * Tests validation parameter order in custom validation methods
  1116. *
  1117. * @return void
  1118. */
  1119. public function testInvalidAssociation() {
  1120. $TestModel = new ValidationTest1();
  1121. $this->assertNull($TestModel->getAssociated('Foo'));
  1122. }
  1123. /**
  1124. * testLoadModelSecondIteration method
  1125. *
  1126. * @return void
  1127. */
  1128. public function testLoadModelSecondIteration() {
  1129. $this->loadFixtures('Apple', 'Message', 'Thread', 'Bid');
  1130. $model = new ModelA();
  1131. $this->assertInstanceOf('ModelA', $model);
  1132. $this->assertInstanceOf('ModelB', $model->ModelB);
  1133. $this->assertInstanceOf('ModelD', $model->ModelB->ModelD);
  1134. $this->assertInstanceOf('ModelC', $model->ModelC);
  1135. $this->assertInstanceOf('ModelD', $model->ModelC->ModelD);
  1136. }
  1137. /**
  1138. * ensure that exists() does not persist between method calls reset on create
  1139. *
  1140. * @return void
  1141. */
  1142. public function testResetOfExistsOnCreate() {
  1143. $this->loadFixtures('Article');
  1144. $Article = new Article();
  1145. $Article->id = 1;
  1146. $Article->saveField('title', 'Reset me');
  1147. $Article->delete();
  1148. $Article->id = 1;
  1149. $this->assertFalse($Article->exists());
  1150. $Article->create();
  1151. $this->assertFalse($Article->exists());
  1152. $Article->id = 2;
  1153. $Article->saveField('title', 'Staying alive');
  1154. $result = $Article->read(null, 2);
  1155. $this->assertEquals('Staying alive', $result['Article']['title']);
  1156. }
  1157. /**
  1158. * testUseTableFalseExistsCheck method
  1159. *
  1160. * @return void
  1161. */
  1162. public function testUseTableFalseExistsCheck() {
  1163. $this->loadFixtures('Article');
  1164. $Article = new Article();
  1165. $Article->id = 1337;
  1166. $result = $Article->exists();
  1167. $this->assertFalse($result);
  1168. $Article->useTable = false;
  1169. $Article->id = null;
  1170. $result = $Article->exists();
  1171. $this->assertFalse($result);
  1172. // An article with primary key of '1' has been loaded by the fixtures.
  1173. $Article->useTable = false;
  1174. $Article->id = 1;
  1175. $result = $Article->exists();
  1176. $this->assertTrue($result);
  1177. }
  1178. /**
  1179. * testPluginAssociations method
  1180. *
  1181. * @return void
  1182. */
  1183. public function testPluginAssociations() {
  1184. $this->loadFixtures('TestPluginArticle', 'User', 'TestPluginComment');
  1185. $TestModel = new TestPluginArticle();
  1186. $result = $TestModel->find('all');
  1187. $expected = array(
  1188. array(
  1189. 'TestPluginArticle' => array(
  1190. 'id' => 1,
  1191. 'user_id' => 1,
  1192. 'title' => 'First Plugin Article',
  1193. 'body' => 'First Plugin Article Body',
  1194. 'published' => 'Y',
  1195. 'created' => '2008-09-24 10:39:23',
  1196. 'updated' => '2008-09-24 10:41:31'
  1197. ),
  1198. 'User' => array(
  1199. 'id' => 1,
  1200. 'user' => 'mariano',
  1201. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  1202. 'created' => '2007-03-17 01:16:23',
  1203. 'updated' => '2007-03-17 01:18:31'
  1204. ),
  1205. 'TestPluginComment' => array(
  1206. array(
  1207. 'id' => 1,
  1208. 'article_id' => 1,
  1209. 'user_id' => 2,
  1210. 'comment' => 'First Comment for First Plugin Article',
  1211. 'published' => 'Y',
  1212. 'created' => '2008-09-24 10:45:23',
  1213. 'updated' => '2008-09-24 10:47:31'
  1214. ),
  1215. array(
  1216. 'id' => 2,
  1217. 'article_id' => 1,
  1218. 'user_id' => 4,
  1219. 'comment' => 'Second Comment for First Plugin Article',
  1220. 'published' => 'Y',
  1221. 'created' => '2008-09-24 10:47:23',
  1222. 'updated' => '2008-09-24 10:49:31'
  1223. ),
  1224. array(
  1225. 'id' => 3,
  1226. 'article_id' => 1,
  1227. 'user_id' => 1,
  1228. 'comment' => 'Third Comment for First Plugin Article',
  1229. 'published' => 'Y',
  1230. 'created' => '2008-09-24 10:49:23',
  1231. 'updated' => '2008-09-24 10:51:31'
  1232. ),
  1233. array(
  1234. 'id' => 4,
  1235. 'article_id' => 1,
  1236. 'user_id' => 1,
  1237. 'comment' => 'Fourth Comment for First Plugin Article',
  1238. 'published' => 'N',
  1239. 'created' => '2008-09-24 10:51:23',
  1240. 'updated' => '2008-09-24 10:53:31'
  1241. ))),
  1242. array(
  1243. 'TestPluginArticle' => array(
  1244. 'id' => 2,
  1245. 'user_id' => 3,
  1246. 'title' => 'Second Plugin Article',
  1247. 'body' => 'Second Plugin Article Body',
  1248. 'published' => 'Y',
  1249. 'created' => '2008-09-24 10:41:23',
  1250. 'updated' => '2008-09-24 10:43:31'
  1251. ),
  1252. 'User' => array(
  1253. 'id' => 3,
  1254. 'user' => 'larry',
  1255. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  1256. 'created' => '2007-03-17 01:20:23',
  1257. 'updated' => '2007-03-17 01:22:31'
  1258. ),
  1259. 'TestPluginComment' => array(
  1260. array(
  1261. 'id' => 5,
  1262. 'article_id' => 2,
  1263. 'user_id' => 1,
  1264. 'comment' => 'First Comment for Second Plugin Article',
  1265. 'published' => 'Y',
  1266. 'created' => '2008-09-24 10:53:23',
  1267. 'updated' => '2008-09-24 10:55:31'
  1268. ),
  1269. array(
  1270. 'id' => 6,
  1271. 'article_id' => 2,
  1272. 'user_id' => 2,
  1273. 'comment' => 'Second Comment for Second Plugin Article',
  1274. 'published' => 'Y',
  1275. 'created' => '2008-09-24 10:55:23',
  1276. 'updated' => '2008-09-24 10:57:31'
  1277. ))),
  1278. array(
  1279. 'TestPluginArticle' => array(
  1280. 'id' => 3,
  1281. 'user_id' => 1,
  1282. 'title' => 'Third Plugin Article',
  1283. 'body' => 'Third Plugin Article Body',
  1284. 'published' => 'Y',
  1285. 'created' => '2008-09-24 10:43:23',
  1286. 'updated' => '2008-09-24 10:45:31'
  1287. ),
  1288. 'User' => array(
  1289. 'id' => 1,
  1290. 'user' => 'mariano',
  1291. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  1292. 'created' => '2007-03-17 01:16:23',
  1293. 'updated' => '2007-03-17 01:18:31'
  1294. ),
  1295. 'TestPluginComment' => array()
  1296. ));
  1297. $this->assertEquals($expected, $result);
  1298. }
  1299. /**
  1300. * Tests getAssociated method
  1301. *
  1302. * @return void
  1303. */
  1304. public function testGetAssociated() {
  1305. $this->loadFixtures('Article', 'Tag');
  1306. $Article = ClassRegistry::init('Article');
  1307. $assocTypes = array('hasMany', 'hasOne', 'belongsTo', 'hasAndBelongsToMany');
  1308. foreach ($assocTypes as $type) {
  1309. $this->assertEquals($Article->getAssociated($type), array_keys($Article->{$type}));
  1310. }
  1311. $Article->bindModel(array('hasMany' => array('Category')));
  1312. $this->assertEquals(array('Comment', 'Category'), $Article->getAssociated('hasMany'));
  1313. $results = $Article->getAssociated();
  1314. $results = array_keys($results);
  1315. sort($results);
  1316. $this->assertEquals(array('Category', 'Comment', 'Tag', 'User'), $results);
  1317. $Article->unbindModel(array('hasAndBelongsToMany' => array('Tag')));
  1318. $this->assertEquals(array(), $Article->getAssociated('hasAndBelongsToMany'));
  1319. $result = $Article->getAssociated('Category');
  1320. $expected = array(
  1321. 'className' => 'Category',
  1322. 'foreignKey' => 'article_id',
  1323. 'conditions' => '',
  1324. 'fields' => '',
  1325. 'order' => '',
  1326. 'limit' => '',
  1327. 'offset' => '',
  1328. 'dependent' => '',
  1329. 'exclusive' => '',
  1330. 'finderQuery' => '',
  1331. 'counterQuery' => '',
  1332. 'association' => 'hasMany',
  1333. );
  1334. $this->assertEquals($expected, $result);
  1335. }
  1336. /**
  1337. * testAutoConstructAssociations method
  1338. *
  1339. * @return void
  1340. */
  1341. public function testAutoConstructAssociations() {
  1342. $this->loadFixtures('User', 'ArticleFeatured', 'Featured', 'ArticleFeaturedsTags');
  1343. $TestModel = new AssociationTest1();
  1344. $result = $TestModel->hasAndBelongsToMany;
  1345. $expected = array('AssociationTest2' => array(
  1346. 'unique' => false,
  1347. 'joinTable' => 'join_as_join_bs',
  1348. 'foreignKey' => false,
  1349. 'className' => 'AssociationTest2',
  1350. 'with' => 'JoinAsJoinB',
  1351. 'dynamicWith' => true,
  1352. 'associationForeignKey' => 'join_b_id',
  1353. 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '',
  1354. 'finderQuery' => ''
  1355. ));
  1356. $this->assertEquals($expected, $result);
  1357. $TestModel = new ArticleFeatured();
  1358. $TestFakeModel = new ArticleFeatured(array('table' => false));
  1359. $expected = array(
  1360. 'User' => array(
  1361. 'className' => 'User', 'foreignKey' => 'user_id',
  1362. 'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => ''
  1363. ),
  1364. 'Category' => array(
  1365. 'className' => 'Category', 'foreignKey' => 'category_id',
  1366. 'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => ''
  1367. )
  1368. );
  1369. $this->assertSame($TestModel->belongsTo, $expected);
  1370. $this->assertSame($TestFakeModel->belongsTo, $expected);
  1371. $this->assertEquals('User', $TestModel->User->name);
  1372. $this->assertEquals('User', $TestFakeModel->User->name);
  1373. $this->assertEquals('Category', $TestModel->Category->name);
  1374. $this->assertEquals('Category', $TestFakeModel->Category->name);
  1375. $expected = array(
  1376. 'Featured' => array(
  1377. 'className' => 'Featured',
  1378. 'foreignKey' => 'article_featured_id',
  1379. 'conditions' => '',
  1380. 'fields' => '',
  1381. 'order' => '',
  1382. 'dependent' => ''
  1383. ));
  1384. $this->assertSame($TestModel->hasOne, $expected);
  1385. $this->assertSame($TestFakeModel->hasOne, $expected);
  1386. $this->assertEquals('Featured', $TestModel->Featured->name);
  1387. $this->assertEquals('Featured', $TestFakeModel->Featured->name);
  1388. $expected = array(
  1389. 'Comment' => array(
  1390. 'className' => 'Comment',
  1391. 'dependent' => true,
  1392. 'foreignKey' => 'article_featured_id',
  1393. 'conditions' => '',
  1394. 'fields' => '',
  1395. 'order' => '',
  1396. 'limit' => '',
  1397. 'offset' => '',
  1398. 'exclusive' => '',
  1399. 'finderQuery' => '',
  1400. 'counterQuery' => ''
  1401. ));
  1402. $this->assertSame($TestModel->hasMany, $expected);
  1403. $this->assertSame($TestFakeModel->hasMany, $expected);
  1404. $this->assertEquals('Comment', $TestModel->Comment->name);
  1405. $this->assertEquals('Comment', $TestFakeModel->Comment->name);
  1406. $expected = array(
  1407. 'Tag' => array(
  1408. 'className' => 'Tag',
  1409. 'joinTable' => 'article_featureds_tags',
  1410. 'with' => 'ArticleFeaturedsTag',
  1411. 'dynamicWith' => true,
  1412. 'foreignKey' => 'article_featured_id',
  1413. 'associationForeignKey' => 'tag_id',
  1414. 'conditions' => '',
  1415. 'fields' => '',
  1416. 'order' => '',
  1417. 'limit' => '',
  1418. 'offset' => '',
  1419. 'unique' => true,
  1420. 'finderQuery' => '',
  1421. ));
  1422. $this->assertSame($TestModel->hasAndBelongsToMany, $expected);
  1423. $this->assertSame($TestFakeModel->hasAndBelongsToMany, $expected);
  1424. $this->assertEquals('Tag', $TestModel->Tag->name);
  1425. $this->assertEquals('Tag', $TestFakeModel->Tag->name);
  1426. }
  1427. /**
  1428. * test creating associations with plugins. Ensure a double alias isn't created
  1429. *
  1430. * @return void
  1431. */
  1432. public function testAutoConstructPluginAssociations() {
  1433. $Comment = ClassRegistry::init('TestPluginComment');
  1434. $this->assertEquals(2, count($Comment->belongsTo), 'Too many associations');
  1435. $this->assertFalse(isset($Comment->belongsTo['TestPlugin.User']));
  1436. $this->assertTrue(isset($Comment->belongsTo['User']), 'Missing association');
  1437. $this->assertTrue(isset($Comment->belongsTo['TestPluginArticle']), 'Missing association');
  1438. }
  1439. /**
  1440. * test Model::__construct
  1441. *
  1442. * ensure that $actsAS and $findMethods are merged.
  1443. *
  1444. * @return void
  1445. */
  1446. public function testConstruct() {
  1447. $this->loadFixtures('Post');
  1448. $TestModel = ClassRegistry::init('MergeVarPluginPost');
  1449. $this->assertEquals(array('Containable' => null, 'Tree' => null), $TestModel->actsAs);
  1450. $this->assertTrue(isset($TestModel->Behaviors->Containable));
  1451. $this->assertTrue(isset($TestModel->Behaviors->Tree));
  1452. $TestModel = ClassRegistry::init('MergeVarPluginComment');
  1453. $expected = array('Containable' => array('some_settings'));
  1454. $this->assertEquals($expected, $TestModel->actsAs);
  1455. $this->assertTrue(isset($TestModel->Behaviors->Containable));
  1456. }
  1457. /**
  1458. * test Model::__construct
  1459. *
  1460. * ensure that $actsAS and $findMethods are merged.
  1461. *
  1462. * @return void
  1463. */
  1464. public function testConstructWithAlternateDataSource() {
  1465. $TestModel = ClassRegistry::init(array(
  1466. 'class' => 'DoesntMatter', 'ds' => 'test', 'table' => false
  1467. ));
  1468. $this->assertEquals('test', $TestModel->useDbConfig);
  1469. //deprecated but test it anyway
  1470. $NewVoid = new TheVoid(null, false, 'other');
  1471. $this->assertEquals('other', $NewVoid->useDbConfig);
  1472. }
  1473. /**
  1474. * testColumnTypeFetching method
  1475. *
  1476. * @return void
  1477. */
  1478. public function testColumnTypeFetching() {
  1479. $model = new Test();
  1480. $this->assertEquals('integer', $model->getColumnType('id'));
  1481. $this->assertEquals('text', $model->getColumnType('notes'));
  1482. $this->assertEquals('datetime', $model->getColumnType('updated'));
  1483. $this->assertEquals(null, $model->getColumnType('unknown'));
  1484. $model = new Article();
  1485. $this->assertEquals('datetime', $model->getColumnType('User.created'));
  1486. $this->assertEquals('integer', $model->getColumnType('Tag.id'));
  1487. $this->assertEquals('integer', $model->getColumnType('Article.id'));
  1488. }
  1489. /**
  1490. * testHabtmUniqueKey method
  1491. *
  1492. * @return void
  1493. */
  1494. public function testHabtmUniqueKey() {
  1495. $model = new Item();
  1496. $this->assertFalse($model->hasAndBelongsToMany['Portfolio']['unique']);
  1497. }
  1498. /**
  1499. * testIdentity method
  1500. *
  1501. * @return void
  1502. */
  1503. public function testIdentity() {
  1504. $TestModel = new Test();
  1505. $result = $TestModel->alias;
  1506. $expected = 'Test';
  1507. $this->assertEquals($expected, $result);
  1508. $TestModel = new TestAlias();
  1509. $result = $TestModel->alias;
  1510. $expected = 'TestAlias';
  1511. $this->assertEquals($expected, $result);
  1512. $TestModel = new Test(array('alias' => 'AnotherTest'));
  1513. $result = $TestModel->alias;
  1514. $expected = 'AnotherTest';
  1515. $this->assertEquals($expected, $result);
  1516. $TestModel = ClassRegistry::init('Test');
  1517. $expected = null;
  1518. $this->assertEquals($expected, $TestModel->plugin);
  1519. $TestModel = ClassRegistry::init('TestPlugin.TestPluginComment');
  1520. $expected = 'TestPlugin';
  1521. $this->assertEquals($expected, $TestModel->plugin);
  1522. }
  1523. /**
  1524. * testWithAssociation method
  1525. *
  1526. * @return void
  1527. */
  1528. public function testWithAssociation() {
  1529. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  1530. $TestModel = new Something();
  1531. $result = $TestModel->SomethingElse->find('all');
  1532. $expected = array(
  1533. array(
  1534. 'SomethingElse' => array(
  1535. 'id' => '1',
  1536. 'title' => 'First Post',
  1537. 'body' => 'First Post Body',
  1538. 'published' => 'Y',
  1539. 'created' => '2007-03-18 10:39:23',
  1540. 'updated' => '2007-03-18 10:41:31'
  1541. ),
  1542. 'Something' => array(
  1543. array(
  1544. 'id' => '3',
  1545. 'title' => 'Third Post',
  1546. 'body' => 'Third Post Body',
  1547. 'published' => 'Y',
  1548. 'created' => '2007-03-18 10:43:23',
  1549. 'updated' => '2007-03-18 10:45:31',
  1550. 'JoinThing' => array(
  1551. 'id' => '3',
  1552. 'something_id' => '3',
  1553. 'something_else_id' => '1',
  1554. 'doomed' => true,
  1555. 'created' => '2007-03-18 10:43:23',
  1556. 'updated' => '2007-03-18 10:45:31'
  1557. )))),
  1558. array(
  1559. 'SomethingElse' => array(
  1560. 'id' => '2',
  1561. 'title' => 'Second Post',
  1562. 'body' => 'Second Post Body',
  1563. 'published' => 'Y',
  1564. 'created' => '2007-03-18 10:41:23',
  1565. 'updated' => '2007-03-18 10:43:31'
  1566. ),
  1567. 'Something' => array(
  1568. array(
  1569. 'id' => '1',
  1570. 'title' => 'First Post',
  1571. 'body' => 'First Post Body',
  1572. 'published' => 'Y',
  1573. 'created' => '2007-03-18 10:39:23',
  1574. 'updated' => '2007-03-18 10:41:31',
  1575. 'JoinThing' => array(
  1576. 'id' => '1',
  1577. 'something_id' => '1',
  1578. 'something_else_id' => '2',
  1579. 'doomed' => true,
  1580. 'created' => '2007-03-18 10:39:23',
  1581. 'updated' => '2007-03-18 10:41:31'
  1582. )))),
  1583. array(
  1584. 'SomethingElse' => array(
  1585. 'id' => '3',
  1586. 'title' => 'Third Post',
  1587. 'body' => 'Third Post Body',
  1588. 'published' => 'Y',
  1589. 'created' => '2007-03-18 10:43:23',
  1590. 'updated' => '2007-03-18 10:45:31'
  1591. ),
  1592. 'Something' => array(
  1593. array(
  1594. 'id' => '2',
  1595. 'title' => 'Second Post',
  1596. 'body' => 'Second Post Body',
  1597. 'published' => 'Y',
  1598. 'created' => '2007-03-18 10:41:23',
  1599. 'updated' => '2007-03-18 10:43:31',
  1600. 'JoinThing' => array(
  1601. 'id' => '2',
  1602. 'something_id' => '2',
  1603. 'something_else_id' => '3',
  1604. 'doomed' => false,
  1605. 'created' => '2007-03-18 10:41:23',
  1606. 'updated' => '2007-03-18 10:43:31'
  1607. )))));
  1608. $this->assertEquals($expected, $result);
  1609. $result = $TestModel->find('all');
  1610. $expected = array(
  1611. array(
  1612. 'Something' => array(
  1613. 'id' => '1',
  1614. 'title' => 'First Post',
  1615. 'body' => 'First Post Body',
  1616. 'published' => 'Y',
  1617. 'created' => '2007-03-18 10:39:23',
  1618. 'updated' => '2007-03-18 10:41:31'
  1619. ),
  1620. 'SomethingElse' => array(
  1621. array(
  1622. 'id' => '2',
  1623. 'title' => 'Second Post',
  1624. 'body' => 'Second Post Body',
  1625. 'published' => 'Y',
  1626. 'created' => '2007-03-18 10:41:23',
  1627. 'updated' => '2007-03-18 10:43:31',
  1628. 'JoinThing' => array(
  1629. 'doomed' => true,
  1630. 'something_id' => '1',
  1631. 'something_else_id' => '2'
  1632. )))),
  1633. array(
  1634. 'Something' => array(
  1635. 'id' => '2',
  1636. 'title' => 'Second Post',
  1637. 'body' => 'Second Post Body',
  1638. 'published' => 'Y',
  1639. 'created' => '2007-03-18 10:41:23',
  1640. 'updated' => '2007-03-18 10:43:31'
  1641. ),
  1642. 'SomethingElse' => array(
  1643. array(
  1644. 'id' => '3',
  1645. 'title' => 'Third Post',
  1646. 'body' => 'Third Post Body',
  1647. 'published' => 'Y',
  1648. 'created' => '2007-03-18 10:43:23',
  1649. 'updated' => '2007-03-18 10:45:31',
  1650. 'JoinThing' => array(
  1651. 'doomed' => false,
  1652. 'something_id' => '2',
  1653. 'something_else_id' => '3'
  1654. )))),
  1655. array(
  1656. 'Something' => array(
  1657. 'id' => '3',
  1658. 'title' => 'Third Post',
  1659. 'body' => 'Third Post Body',
  1660. 'published' => 'Y',
  1661. 'created' => '2007-03-18 10:43:23',
  1662. 'updated' => '2007-03-18 10:45:31'
  1663. ),
  1664. 'SomethingElse' => array(
  1665. array(
  1666. 'id' => '1',
  1667. 'title' => 'First Post',
  1668. 'body' => 'First Post Body',
  1669. 'published' => 'Y',
  1670. 'created' => '2007-03-18 10:39:23',
  1671. 'updated' => '2007-03-18 10:41:31',
  1672. 'JoinThing' => array(
  1673. 'doomed' => true,
  1674. 'something_id' => '3',
  1675. 'something_else_id' => '1'
  1676. )))));
  1677. $this->assertEquals($expected, $result);
  1678. $result = $TestModel->findById(1);
  1679. $expected = array(
  1680. 'Something' => array(
  1681. 'id' => '1',
  1682. 'title' => 'First Post',
  1683. 'body' => 'First Post Body',
  1684. 'published' => 'Y',
  1685. 'created' => '2007-03-18 10:39:23',
  1686. 'updated' => '2007-03-18 10:41:31'
  1687. ),
  1688. 'SomethingElse' => array(
  1689. array(
  1690. 'id' => '2',
  1691. 'title' => 'Second Post',
  1692. 'body' => 'Second Post Body',
  1693. 'published' => 'Y',
  1694. 'created' => '2007-03-18 10:41:23',
  1695. 'updated' => '2007-03-18 10:43:31',
  1696. 'JoinThing' => array(
  1697. 'doomed' => true,
  1698. 'something_id' => '1',
  1699. 'something_else_id' => '2'
  1700. ))));
  1701. $this->assertEquals($expected, $result);
  1702. $expected = $TestModel->findById(1);
  1703. $TestModel->set($expected);
  1704. $TestModel->save();
  1705. $result = $TestModel->findById(1);
  1706. $this->assertEquals($expected, $result);
  1707. $TestModel->hasAndBelongsToMany['SomethingElse']['unique'] = false;
  1708. $TestModel->create(array(
  1709. 'Something' => array('id' => 1),
  1710. 'SomethingElse' => array(3, array(
  1711. 'something_else_id' => 1,
  1712. 'doomed' => true
  1713. ))));
  1714. $TestModel->save();
  1715. $TestModel->hasAndBelongsToMany['SomethingElse']['order'] = 'SomethingElse.id ASC';
  1716. $result = $TestModel->findById(1);
  1717. $expected = array(
  1718. 'Something' => array(
  1719. 'id' => '1',
  1720. 'title' => 'First Post',
  1721. 'body' => 'First Post Body',
  1722. 'published' => 'Y',
  1723. 'created' => '2007-03-18 10:39:23'
  1724. ),
  1725. 'SomethingElse' => array(
  1726. array(
  1727. 'id' => '1',
  1728. 'title' => 'First Post',
  1729. 'body' => 'First Post Body',
  1730. 'published' => 'Y',
  1731. 'created' => '2007-03-18 10:39:23',
  1732. 'updated' => '2007-03-18 10:41:31',
  1733. 'JoinThing' => array(
  1734. 'doomed' => true,
  1735. 'something_id' => '1',
  1736. 'something_else_id' => '1'
  1737. )
  1738. ),
  1739. array(
  1740. 'id' => '2',
  1741. 'title' => 'Second Post',
  1742. 'body' => 'Second Post Body',
  1743. 'published' => 'Y',
  1744. 'created' => '2007-03-18 10:41:23',
  1745. 'updated' => '2007-03-18 10:43:31',
  1746. 'JoinThing' => array(
  1747. 'doomed' => true,
  1748. 'something_id' => '1',
  1749. 'something_else_id' => '2'
  1750. )
  1751. ),
  1752. array(
  1753. 'id' => '3',
  1754. 'title' => 'Third Post',
  1755. 'body' => 'Third Post Body',
  1756. 'published' => 'Y',
  1757. 'created' => '2007-03-18 10:43:23',
  1758. 'updated' => '2007-03-18 10:45:31',
  1759. 'JoinThing' => array(
  1760. 'doomed' => false,
  1761. 'something_id' => '1',
  1762. 'something_else_id' => '3')
  1763. )
  1764. )
  1765. );
  1766. $this->assertEquals(self::date(), $result['Something']['updated']);
  1767. unset($result['Something']['updated']);
  1768. $this->assertEquals($expected, $result);
  1769. }
  1770. /**
  1771. * testFindSelfAssociations method
  1772. *
  1773. * @return void
  1774. */
  1775. public function testFindSelfAssociations() {
  1776. $this->loadFixtures('Person');
  1777. $TestModel = new Person();
  1778. $TestModel->recursive = 2;
  1779. $result = $TestModel->read(null, 1);
  1780. $expected = array(
  1781. 'Person' => array(
  1782. 'id' => 1,
  1783. 'name' => 'person',
  1784. 'mother_id' => 2,
  1785. 'father_id' => 3
  1786. ),
  1787. 'Mother' => array(
  1788. 'id' => 2,
  1789. 'name' => 'mother',
  1790. 'mother_id' => 4,
  1791. 'father_id' => 5,
  1792. 'Mother' => array(
  1793. 'id' => 4,
  1794. 'name' => 'mother - grand mother',
  1795. 'mother_id' => 0,
  1796. 'father_id' => 0
  1797. ),
  1798. 'Father' => array(
  1799. 'id' => 5,
  1800. 'name' => 'mother - grand father',
  1801. 'mother_id' => 0,
  1802. 'father_id' => 0
  1803. )),
  1804. 'Father' => array(
  1805. 'id' => 3,
  1806. 'name' => 'father',
  1807. 'mother_id' => 6,
  1808. 'father_id' => 7,
  1809. 'Father' => array(
  1810. 'id' => 7,
  1811. 'name' => 'father - grand father',
  1812. 'mother_id' => 0,
  1813. 'father_id' => 0
  1814. ),
  1815. 'Mother' => array(
  1816. 'id' => 6,
  1817. 'name' => 'father - grand mother',
  1818. 'mother_id' => 0,
  1819. 'father_id' => 0
  1820. )));
  1821. $this->assertEquals($expected, $result);
  1822. $TestModel->recursive = 3;
  1823. $result = $TestModel->read(null, 1);
  1824. $expected = array(
  1825. 'Person' => array(
  1826. 'id' => 1,
  1827. 'name' => 'person',
  1828. 'mother_id' => 2,
  1829. 'father_id' => 3
  1830. ),
  1831. 'Mother' => array(
  1832. 'id' => 2,
  1833. 'name' => 'mother',
  1834. 'mother_id' => 4,
  1835. 'father_id' => 5,
  1836. 'Mother' => array(
  1837. 'id' => 4,
  1838. 'name' => 'mother - grand mother',
  1839. 'mother_id' => 0,
  1840. 'father_id' => 0,
  1841. 'Mother' => array(),
  1842. 'Father' => array()),
  1843. 'Father' => array(
  1844. 'id' => 5,
  1845. 'name' => 'mother - grand father',
  1846. 'mother_id' => 0,
  1847. 'father_id' => 0,
  1848. 'Father' => array(),
  1849. 'Mother' => array()
  1850. )),
  1851. 'Father' => array(
  1852. 'id' => 3,
  1853. 'name' => 'father',
  1854. 'mother_id' => 6,
  1855. 'father_id' => 7,
  1856. 'Father' => array(
  1857. 'id' => 7,
  1858. 'name' => 'father - grand father',
  1859. 'mother_id' => 0,
  1860. 'father_id' => 0,
  1861. 'Father' => array(),
  1862. 'Mother' => array()
  1863. ),
  1864. 'Mother' => array(
  1865. 'id' => 6,
  1866. 'name' => 'father - grand mother',
  1867. 'mother_id' => 0,
  1868. 'father_id' => 0,
  1869. 'Mother' => array(),
  1870. 'Father' => array()
  1871. )));
  1872. $this->assertEquals($expected, $result);
  1873. }
  1874. /**
  1875. * testDynamicAssociations method
  1876. *
  1877. * @return void
  1878. */
  1879. public function testDynamicAssociations() {
  1880. $this->loadFixtures('Article', 'Comment');
  1881. $TestModel = new Article();
  1882. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->hasOne = array();
  1883. $TestModel->hasMany['Comment'] = array_merge($TestModel->hasMany['Comment'], array(
  1884. 'foreignKey' => false,
  1885. 'conditions' => array('Comment.user_id =' => '2')
  1886. ));
  1887. $result = $TestModel->find('all');
  1888. $expected = array(
  1889. array(
  1890. 'Article' => array(
  1891. 'id' => '1',
  1892. 'user_id' => '1',
  1893. 'title' => 'First Article',
  1894. 'body' => 'First Article Body',
  1895. 'published' => 'Y',
  1896. 'created' => '2007-03-18 10:39:23',
  1897. 'updated' => '2007-03-18 10:41:31'
  1898. ),
  1899. 'Comment' => array(
  1900. array(
  1901. 'id' => '1',
  1902. 'article_id' => '1',
  1903. 'user_id' => '2',
  1904. 'comment' => 'First Comment for First Article',
  1905. 'published' => 'Y',
  1906. 'created' => '2007-03-18 10:45:23',
  1907. 'updated' => '2007-03-18 10:47:31'
  1908. ),
  1909. array(
  1910. 'id' => '6',
  1911. 'article_id' => '2',
  1912. 'user_id' => '2',
  1913. 'comment' => 'Second Comment for Second Article',
  1914. 'published' => 'Y',
  1915. 'created' => '2007-03-18 10:55:23',
  1916. 'updated' => '2007-03-18 10:57:31'
  1917. ))),
  1918. array(
  1919. 'Article' => array(
  1920. 'id' => '2',
  1921. 'user_id' => '3',
  1922. 'title' => 'Second Article',
  1923. 'body' => 'Second Article Body',
  1924. 'published' => 'Y',
  1925. 'created' => '2007-03-18 10:41:23',
  1926. 'updated' => '2007-03-18 10:43:31'
  1927. ),
  1928. 'Comment' => array(
  1929. array(
  1930. 'id' => '1',
  1931. 'article_id' => '1',
  1932. 'user_id' => '2',
  1933. 'comment' => 'First Comment for First Article',
  1934. 'published' => 'Y',
  1935. 'created' => '2007-03-18 10:45:23',
  1936. 'updated' => '2007-03-18 10:47:31'
  1937. ),
  1938. array(
  1939. 'id' => '6',
  1940. 'article_id' => '2',
  1941. 'user_id' => '2',
  1942. 'comment' => 'Second Comment for Second Article',
  1943. 'published' => 'Y',
  1944. 'created' => '2007-03-18 10:55:23',
  1945. 'updated' => '2007-03-18 10:57:31'
  1946. ))),
  1947. array(
  1948. 'Article' => array(
  1949. 'id' => '3',
  1950. 'user_id' => '1',
  1951. 'title' => 'Third Article',
  1952. 'body' => 'Third Article Body',
  1953. 'published' => 'Y',
  1954. 'created' => '2007-03-18 10:43:23',
  1955. 'updated' => '2007-03-18 10:45:31'
  1956. ),
  1957. 'Comment' => array(
  1958. array(
  1959. 'id' => '1',
  1960. 'article_id' => '1',
  1961. 'user_id' => '2',
  1962. 'comment' => 'First Comment for First Article',
  1963. 'published' => 'Y',
  1964. 'created' => '2007-03-18 10:45:23',
  1965. 'updated' => '2007-03-18 10:47:31'
  1966. ),
  1967. array(
  1968. 'id' => '6',
  1969. 'article_id' => '2',
  1970. 'user_id' => '2',
  1971. 'comment' => 'Second Comment for Second Article',
  1972. 'published' => 'Y',
  1973. 'created' => '2007-03-18 10:55:23',
  1974. 'updated' => '2007-03-18 10:57:31'
  1975. ))));
  1976. $this->assertEquals($expected, $result);
  1977. }
  1978. /**
  1979. * testCreation method
  1980. *
  1981. * @return void
  1982. */
  1983. public function testCreation() {
  1984. $this->loadFixtures('Article', 'ArticleFeaturedsTags', 'User', 'Featured');
  1985. $TestModel = new Test();
  1986. $result = $TestModel->create();
  1987. $expected = array('Test' => array('notes' => 'write some notes here'));
  1988. $this->assertEquals($expected, $result);
  1989. $TestModel = new User();
  1990. $result = $TestModel->schema();
  1991. if (isset($this->db->columns['primary_key']['length'])) {
  1992. $intLength = $this->db->columns['primary_key']['length'];
  1993. } elseif (isset($this->db->columns['integer']['length'])) {
  1994. $intLength = $this->db->columns['integer']['length'];
  1995. } else {
  1996. $intLength = 11;
  1997. }
  1998. foreach (array('collate', 'charset', 'comment') as $type) {
  1999. foreach ($result as $i => $r) {
  2000. unset($result[$i][$type]);
  2001. }
  2002. }
  2003. $expected = array(
  2004. 'id' => array(
  2005. 'type' => 'integer',
  2006. 'null' => false,
  2007. 'default' => null,
  2008. 'length' => $intLength,
  2009. 'key' => 'primary'
  2010. ),
  2011. 'user' => array(
  2012. 'type' => 'string',
  2013. 'null' => true,
  2014. 'default' => '',
  2015. 'length' => 255
  2016. ),
  2017. 'password' => array(
  2018. 'type' => 'string',
  2019. 'null' => true,
  2020. 'default' => '',
  2021. 'length' => 255
  2022. ),
  2023. 'created' => array(
  2024. 'type' => 'datetime',
  2025. 'null' => true,
  2026. 'default' => null,
  2027. 'length' => null
  2028. ),
  2029. 'updated' => array(
  2030. 'type' => 'datetime',
  2031. 'null' => true,
  2032. 'default' => null,
  2033. 'length' => null
  2034. ));
  2035. $this->assertEquals($expected, $result);
  2036. $TestModel = new Article();
  2037. $result = $TestModel->create();
  2038. $expected = array('Article' => array('published' => 'N'));
  2039. $this->assertEquals($expected, $result);
  2040. $FeaturedModel = new Featured();
  2041. $data = array(
  2042. 'article_featured_id' => 1,
  2043. 'category_id' => 1,
  2044. 'published_date' => array(
  2045. 'year' => 2008,
  2046. 'month' => 06,
  2047. 'day' => 11
  2048. ),
  2049. 'end_date' => array(
  2050. 'year' => 2008,
  2051. 'month' => 06,
  2052. 'day' => 20
  2053. ));
  2054. $expected = array(
  2055. 'Featured' => array(
  2056. 'article_featured_id' => 1,
  2057. 'category_id' => 1,
  2058. 'published_date' => '2008-06-11 00:00:00',
  2059. 'end_date' => '2008-06-20 00:00:00'
  2060. ));
  2061. $this->assertEquals($expected, $FeaturedModel->create($data));
  2062. $data = array(
  2063. 'published_date' => array(
  2064. 'year' => 2008,
  2065. 'month' => 06,
  2066. 'day' => 11
  2067. ),
  2068. 'end_date' => array(
  2069. 'year' => 2008,
  2070. 'month' => 06,
  2071. 'day' => 20
  2072. ),
  2073. 'article_featured_id' => 1,
  2074. 'category_id' => 1
  2075. );
  2076. $expected = array(
  2077. 'Featured' => array(
  2078. 'published_date' => '2008-06-11 00:00:00',
  2079. 'end_date' => '2008-06-20 00:00:00',
  2080. 'article_featured_id' => 1,
  2081. 'category_id' => 1
  2082. ));
  2083. $this->assertEquals($expected, $FeaturedModel->create($data));
  2084. }
  2085. /**
  2086. * testEscapeField to prove it escapes the field well even when it has part of the alias on it
  2087. *
  2088. * @return void
  2089. */
  2090. public function testEscapeField() {
  2091. $TestModel = new Test();
  2092. $db = $TestModel->getDataSource();
  2093. $result = $TestModel->escapeField('test_field');
  2094. $expected = $db->name('Test.test_field');
  2095. $this->assertEquals($expected, $result);
  2096. $result = $TestModel->escapeField('TestField');
  2097. $expected = $db->name('Test.TestField');
  2098. $this->assertEquals($expected, $result);
  2099. $result = $TestModel->escapeField('DomainHandle', 'Domain');
  2100. $expected = $db->name('Domain.DomainHandle');
  2101. $this->assertEquals($expected, $result);
  2102. ConnectionManager::create('mock', array('datasource' => 'DboMock'));
  2103. $TestModel->setDataSource('mock');
  2104. $db = $TestModel->getDataSource();
  2105. $result = $TestModel->escapeField('DomainHandle', 'Domain');
  2106. $expected = $db->name('Domain.DomainHandle');
  2107. $this->assertEquals($expected, $result);
  2108. ConnectionManager::drop('mock');
  2109. }
  2110. /**
  2111. * testGetID
  2112. *
  2113. * @return void
  2114. */
  2115. public function testGetID() {
  2116. $TestModel = new Test();
  2117. $result = $TestModel->getID();
  2118. $this->assertFalse($result);
  2119. $TestModel->id = 9;
  2120. $result = $TestModel->getID();
  2121. $this->assertEquals(9, $result);
  2122. $TestModel->id = array(10, 9, 8, 7);
  2123. $result = $TestModel->getID(2);
  2124. $this->assertEquals(8, $result);
  2125. $TestModel->id = array(array(), 1, 2, 3);
  2126. $result = $TestModel->getID();
  2127. $this->assertFalse($result);
  2128. }
  2129. /**
  2130. * test that model->hasMethod checks self and behaviors.
  2131. *
  2132. * @return void
  2133. */
  2134. public function testHasMethod() {
  2135. $Article = new Article();
  2136. $Article->Behaviors = $this->getMock('BehaviorCollection');
  2137. $Article->Behaviors->expects($this->at(0))
  2138. ->method('hasMethod')
  2139. ->will($this->returnValue(true));
  2140. $Article->Behaviors->expects($this->at(1))
  2141. ->method('hasMethod')
  2142. ->will($this->returnValue(false));
  2143. $this->assertTrue($Article->hasMethod('find'));
  2144. $this->assertTrue($Article->hasMethod('pass'));
  2145. $this->assertFalse($Article->hasMethod('fail'));
  2146. }
  2147. /**
  2148. * testMultischemaFixture
  2149. *
  2150. * @return void
  2151. */
  2152. public function testMultischemaFixture() {
  2153. $config = ConnectionManager::enumConnectionObjects();
  2154. $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with Sqlite.');
  2155. $this->skipIf(!isset($config['test']) || !isset($config['test2']),
  2156. 'Primary and secondary test databases not configured, skipping cross-database join tests. To run these tests define $test and $test2 in your database configuration.'
  2157. );
  2158. $this->loadFixtures('Player', 'Guild', 'GuildsPlayer');
  2159. $Player = ClassRegistry::init('Player');
  2160. $this->assertEquals('test', $Player->useDbConfig);
  2161. $this->assertEquals('test', $Player->Guild->useDbConfig);
  2162. $this->assertEquals('test2', $Player->Guild->GuildsPlayer->useDbConfig);
  2163. $this->assertEquals('test2', $Player->GuildsPlayer->useDbConfig);
  2164. $players = $Player->find('all', array('recursive' => -1));
  2165. $guilds = $Player->Guild->find('all', array('recursive' => -1));
  2166. $guildsPlayers = $Player->GuildsPlayer->find('all', array('recursive' => -1));
  2167. $this->assertEquals(true, count($players) > 1);
  2168. $this->assertEquals(true, count($guilds) > 1);
  2169. $this->assertEquals(true, count($guildsPlayers) > 1);
  2170. }
  2171. /**
  2172. * testMultischemaFixtureWithThreeDatabases, three databases
  2173. *
  2174. * @return void
  2175. */
  2176. public function testMultischemaFixtureWithThreeDatabases() {
  2177. $config = ConnectionManager::enumConnectionObjects();
  2178. $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with Sqlite.');
  2179. $this->skipIf(
  2180. !isset($config['test']) || !isset($config['test2']) || !isset($config['test_database_three']),
  2181. 'Primary, secondary, and tertiary test databases not configured, skipping test. To run this test define $test, $test2, and $test_database_three in your database configuration.'
  2182. );
  2183. $this->loadFixtures('Player', 'Guild', 'GuildsPlayer', 'Armor', 'ArmorsPlayer');
  2184. $Player = ClassRegistry::init('Player');
  2185. $Player->bindModel(array(
  2186. 'hasAndBelongsToMany' => array(
  2187. 'Armor' => array(
  2188. 'with' => 'ArmorsPlayer',
  2189. ),
  2190. ),
  2191. ), false);
  2192. $this->assertEquals('test', $Player->useDbConfig);
  2193. $this->assertEquals('test', $Player->Guild->useDbConfig);
  2194. $this->assertEquals('test2', $Player->Guild->GuildsPlayer->useDbConfig);
  2195. $this->assertEquals('test2', $Player->GuildsPlayer->useDbConfig);
  2196. $this->assertEquals('test2', $Player->Armor->useDbConfig);
  2197. $this->assertEquals('test_database_three', $Player->Armor->ArmorsPlayer->useDbConfig);
  2198. $this->assertEquals('test', $Player->getDataSource()->configKeyName);
  2199. $this->assertEquals('test', $Player->Guild->getDataSource()->configKeyName);
  2200. $this->assertEquals('test2', $Player->GuildsPlayer->getDataSource()->configKeyName);
  2201. $this->assertEquals('test2', $Player->Armor->getDataSource()->configKeyName);
  2202. $this->assertEquals('test_database_three', $Player->Armor->ArmorsPlayer->getDataSource()->configKeyName);
  2203. $players = $Player->find('all', array('recursive' => -1));
  2204. $guilds = $Player->Guild->find('all', array('recursive' => -1));
  2205. $guildsPlayers = $Player->GuildsPlayer->find('all', array('recursive' => -1));
  2206. $armorsPlayers = $Player->ArmorsPlayer->find('all', array('recursive' => -1));
  2207. $this->assertEquals(true, count($players) > 1);
  2208. $this->assertEquals(true, count($guilds) > 1);
  2209. $this->assertEquals(true, count($guildsPlayers) > 1);
  2210. $this->assertEquals(true, count($armorsPlayers) > 1);
  2211. }
  2212. /**
  2213. * Tests that calling schema() on a model that is not supposed to use a table
  2214. * does not trigger any calls on any datasource
  2215. *
  2216. * @return void
  2217. */
  2218. public function testSchemaNoDB() {
  2219. $model = $this->getMock('Article', array('getDataSource'));
  2220. $model->useTable = false;
  2221. $model->expects($this->never())->method('getDataSource');
  2222. $this->assertEmpty($model->schema());
  2223. }
  2224. }