ModelIntegrationTest.php 69 KB

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