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 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Model
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
  20. App::uses('DboSource', 'Model/Datasource');
  21. /**
  22. * DboMock class
  23. * A Dbo Source driver to mock a connection and a identity name() method
  24. */
  25. class DboMock extends DboSource {
  26. /**
  27. * Returns the $field without modifications
  28. */
  29. public function name($field) {
  30. return $field;
  31. }
  32. /**
  33. * Returns true to fake a database connection
  34. */
  35. public function connect() {
  36. return true;
  37. }
  38. }
  39. /**
  40. * ModelIntegrationTest
  41. *
  42. * @package Cake.Test.Case.Model
  43. */
  44. class ModelIntegrationTest extends BaseModelTest {
  45. /**
  46. * testAssociationLazyLoading
  47. *
  48. * @group lazyloading
  49. * @return void
  50. */
  51. public function testAssociationLazyLoading() {
  52. $this->loadFixtures('ArticleFeaturedsTags');
  53. $Article = new ArticleFeatured();
  54. $this->assertTrue(isset($Article->belongsTo['User']));
  55. $this->assertFalse(property_exists($Article, 'User'));
  56. $this->assertInstanceOf('User', $Article->User);
  57. $this->assertTrue(isset($Article->belongsTo['Category']));
  58. $this->assertFalse(property_exists($Article, 'Category'));
  59. $this->assertTrue(isset($Article->Category));
  60. $this->assertInstanceOf('Category', $Article->Category);
  61. $this->assertTrue(isset($Article->hasMany['Comment']));
  62. $this->assertFalse(property_exists($Article, 'Comment'));
  63. $this->assertTrue(isset($Article->Comment));
  64. $this->assertInstanceOf('Comment', $Article->Comment);
  65. $this->assertTrue(isset($Article->hasAndBelongsToMany['Tag']));
  66. //There was not enough information to setup the association (joinTable and associationForeignKey)
  67. //so the model was not lazy loaded
  68. $this->assertTrue(property_exists($Article, 'Tag'));
  69. $this->assertTrue(isset($Article->Tag));
  70. $this->assertInstanceOf('Tag', $Article->Tag);
  71. $this->assertFalse(property_exists($Article, 'ArticleFeaturedsTag'));
  72. $this->assertInstanceOf('AppModel', $Article->ArticleFeaturedsTag);
  73. $this->assertEquals('article_featureds_tags', $Article->hasAndBelongsToMany['Tag']['joinTable']);
  74. $this->assertEquals('tag_id', $Article->hasAndBelongsToMany['Tag']['associationForeignKey']);
  75. }
  76. /**
  77. * testAssociationLazyLoadWithHABTM
  78. *
  79. * @group lazyloading
  80. * @return void
  81. */
  82. public function testAssociationLazyLoadWithHABTM() {
  83. $this->loadFixtures('FruitsUuidTag', 'ArticlesTag');
  84. $this->db->cacheSources = false;
  85. $Article = new ArticleB();
  86. $this->assertTrue(isset($Article->hasAndBelongsToMany['TagB']));
  87. $this->assertFalse(property_exists($Article, 'TagB'));
  88. $this->assertInstanceOf('TagB', $Article->TagB);
  89. $this->assertFalse(property_exists($Article, 'ArticlesTag'));
  90. $this->assertInstanceOf('AppModel', $Article->ArticlesTag);
  91. $UuidTag = new UuidTag();
  92. $this->assertTrue(isset($UuidTag->hasAndBelongsToMany['Fruit']));
  93. $this->assertFalse(property_exists($UuidTag, 'Fruit'));
  94. $this->assertFalse(property_exists($UuidTag, 'FruitsUuidTag'));
  95. $this->assertTrue(isset($UuidTag->Fruit));
  96. $this->assertFalse(property_exists($UuidTag, 'FruitsUuidTag'));
  97. $this->assertTrue(isset($UuidTag->FruitsUuidTag));
  98. $this->assertInstanceOf('FruitsUuidTag', $UuidTag->FruitsUuidTag);
  99. }
  100. /**
  101. * testAssociationLazyLoadWithBindModel
  102. *
  103. * @group lazyloading
  104. * @return void
  105. */
  106. public function testAssociationLazyLoadWithBindModel() {
  107. $this->loadFixtures('Article', 'User');
  108. $Article = new ArticleB();
  109. $this->assertFalse(isset($Article->belongsTo['User']));
  110. $this->assertFalse(property_exists($Article, 'User'));
  111. $Article->bindModel(array('belongsTo' => array('User')));
  112. $this->assertTrue(isset($Article->belongsTo['User']));
  113. $this->assertFalse(property_exists($Article, 'User'));
  114. $this->assertInstanceOf('User', $Article->User);
  115. }
  116. /**
  117. * Tests that creating a model with no existent database table associated will throw an exception
  118. *
  119. * @expectedException MissingTableException
  120. * @return void
  121. */
  122. public function testMissingTable() {
  123. $Article = new ArticleB(false, uniqid());
  124. $Article->schema();
  125. }
  126. /**
  127. * testPkInHAbtmLinkModelArticleB
  128. *
  129. * @return void
  130. */
  131. public function testPkInHabtmLinkModelArticleB() {
  132. $this->loadFixtures('Article', 'Tag', 'ArticlesTag');
  133. $TestModel = new ArticleB();
  134. $this->assertEquals('article_id', $TestModel->ArticlesTag->primaryKey);
  135. }
  136. /**
  137. * Tests that $cacheSources can only be disabled in the db using model settings, not enabled
  138. *
  139. * @return void
  140. */
  141. public function testCacheSourcesDisabling() {
  142. $this->loadFixtures('JoinA', 'JoinB', 'JoinAB', 'JoinC', 'JoinAC');
  143. $this->db->cacheSources = true;
  144. $TestModel = new JoinA();
  145. $TestModel->cacheSources = false;
  146. $TestModel->setSource('join_as');
  147. $this->assertFalse($this->db->cacheSources);
  148. $this->db->cacheSources = false;
  149. $TestModel = new JoinA();
  150. $TestModel->cacheSources = true;
  151. $TestModel->setSource('join_as');
  152. $this->assertFalse($this->db->cacheSources);
  153. }
  154. /**
  155. * testPkInHabtmLinkModel method
  156. *
  157. * @return void
  158. */
  159. public function testPkInHabtmLinkModel() {
  160. //Test Nonconformant Models
  161. $this->loadFixtures('Content', 'ContentAccount', 'Account', 'JoinC', 'JoinAC', 'ItemsPortfolio');
  162. $TestModel = new Content();
  163. $this->assertEquals('iContentAccountsId', $TestModel->ContentAccount->primaryKey);
  164. //test conformant models with no PK in the join table
  165. $this->loadFixtures('Article', 'Tag');
  166. $TestModel = new Article();
  167. $this->assertEquals('article_id', $TestModel->ArticlesTag->primaryKey);
  168. //test conformant models with PK in join table
  169. $TestModel = new Portfolio();
  170. $this->assertEquals('id', $TestModel->ItemsPortfolio->primaryKey);
  171. //test conformant models with PK in join table - join table contains extra field
  172. $this->loadFixtures('JoinA', 'JoinB', 'JoinAB');
  173. $TestModel = new JoinA();
  174. $this->assertEquals('id', $TestModel->JoinAsJoinB->primaryKey);
  175. }
  176. /**
  177. * testDynamicBehaviorAttachment method
  178. *
  179. * @return void
  180. */
  181. public function testDynamicBehaviorAttachment() {
  182. $this->loadFixtures('Apple', 'Sample', 'Author');
  183. $TestModel = new Apple();
  184. $this->assertEquals(array(), $TestModel->Behaviors->loaded());
  185. $TestModel->Behaviors->attach('Tree', array('left' => 'left_field', 'right' => 'right_field'));
  186. $this->assertTrue(is_object($TestModel->Behaviors->Tree));
  187. $this->assertEquals(array('Tree'), $TestModel->Behaviors->loaded());
  188. $expected = array(
  189. 'parent' => 'parent_id',
  190. 'left' => 'left_field',
  191. 'right' => 'right_field',
  192. 'scope' => '1 = 1',
  193. 'type' => 'nested',
  194. '__parentChange' => false,
  195. 'recursive' => -1
  196. );
  197. $this->assertEquals($expected, $TestModel->Behaviors->Tree->settings['Apple']);
  198. $TestModel->Behaviors->attach('Tree', array('enabled' => false));
  199. $this->assertEquals($expected, $TestModel->Behaviors->Tree->settings['Apple']);
  200. $this->assertEquals(array('Tree'), $TestModel->Behaviors->loaded());
  201. $TestModel->Behaviors->detach('Tree');
  202. $this->assertEquals(array(), $TestModel->Behaviors->loaded());
  203. $this->assertFalse(isset($TestModel->Behaviors->Tree));
  204. }
  205. /**
  206. * testFindWithJoinsOption method
  207. *
  208. * @access public
  209. * @return void
  210. */
  211. public function testFindWithJoinsOption() {
  212. $this->loadFixtures('Article', 'User');
  213. $TestUser = new User();
  214. $options = array(
  215. 'fields' => array(
  216. 'user',
  217. 'Article.published',
  218. ),
  219. 'joins' => array(
  220. array(
  221. 'table' => 'articles',
  222. 'alias' => 'Article',
  223. 'type' => 'LEFT',
  224. 'conditions' => array(
  225. 'User.id = Article.user_id',
  226. ),
  227. ),
  228. ),
  229. 'group' => array('User.user', 'Article.published'),
  230. 'recursive' => -1,
  231. 'order' => array('User.user')
  232. );
  233. $result = $TestUser->find('all', $options);
  234. $expected = array(
  235. array('User' => array('user' => 'garrett'), 'Article' => array('published' => '')),
  236. array('User' => array('user' => 'larry'), 'Article' => array('published' => 'Y')),
  237. array('User' => array('user' => 'mariano'), 'Article' => array('published' => 'Y')),
  238. array('User' => array('user' => 'nate'), 'Article' => array('published' => ''))
  239. );
  240. $this->assertEquals($expected, $result);
  241. }
  242. /**
  243. * Tests cross database joins. Requires $test and $test2 to both be set in DATABASE_CONFIG
  244. * NOTE: When testing on MySQL, you must set 'persistent' => false on *both* database connections,
  245. * or one connection will step on the other.
  246. */
  247. public function testCrossDatabaseJoins() {
  248. $config = ConnectionManager::enumConnectionObjects();
  249. $skip = (!isset($config['test']) || !isset($config['test2']));
  250. if ($skip) {
  251. $this->markTestSkipped('Primary and secondary test databases not configured, skipping cross-database
  252. join tests. To run theses tests defined $test and $test2 in your database configuration.'
  253. );
  254. }
  255. $this->loadFixtures('Article', 'Tag', 'ArticlesTag', 'User', 'Comment');
  256. $TestModel = new Article();
  257. $expected = array(
  258. array(
  259. 'Article' => array(
  260. 'id' => '1',
  261. 'user_id' => '1',
  262. 'title' => 'First Article',
  263. 'body' => 'First Article Body',
  264. 'published' => 'Y',
  265. 'created' => '2007-03-18 10:39:23',
  266. 'updated' => '2007-03-18 10:41:31'
  267. ),
  268. 'User' => array(
  269. 'id' => '1',
  270. 'user' => 'mariano',
  271. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  272. 'created' => '2007-03-17 01:16:23',
  273. 'updated' => '2007-03-17 01:18:31'
  274. ),
  275. 'Comment' => array(
  276. array(
  277. 'id' => '1',
  278. 'article_id' => '1',
  279. 'user_id' => '2',
  280. 'comment' => 'First Comment for First Article',
  281. 'published' => 'Y',
  282. 'created' => '2007-03-18 10:45:23',
  283. 'updated' => '2007-03-18 10:47:31'
  284. ),
  285. array(
  286. 'id' => '2',
  287. 'article_id' => '1',
  288. 'user_id' => '4',
  289. 'comment' => 'Second Comment for First Article',
  290. 'published' => 'Y',
  291. 'created' => '2007-03-18 10:47:23',
  292. 'updated' => '2007-03-18 10:49:31'
  293. ),
  294. array(
  295. 'id' => '3',
  296. 'article_id' => '1',
  297. 'user_id' => '1',
  298. 'comment' => 'Third Comment for First Article',
  299. 'published' => 'Y',
  300. 'created' => '2007-03-18 10:49:23',
  301. 'updated' => '2007-03-18 10:51:31'
  302. ),
  303. array(
  304. 'id' => '4',
  305. 'article_id' => '1',
  306. 'user_id' => '1',
  307. 'comment' => 'Fourth Comment for First Article',
  308. 'published' => 'N',
  309. 'created' => '2007-03-18 10:51:23',
  310. 'updated' => '2007-03-18 10:53:31'
  311. )),
  312. 'Tag' => array(
  313. array(
  314. 'id' => '1',
  315. 'tag' => 'tag1',
  316. 'created' => '2007-03-18 12:22:23',
  317. 'updated' => '2007-03-18 12:24:31'
  318. ),
  319. array(
  320. 'id' => '2',
  321. 'tag' => 'tag2',
  322. 'created' => '2007-03-18 12:24:23',
  323. 'updated' => '2007-03-18 12:26:31'
  324. ))),
  325. array(
  326. 'Article' => array(
  327. 'id' => '2',
  328. 'user_id' => '3',
  329. 'title' => 'Second Article',
  330. 'body' => 'Second Article Body',
  331. 'published' => 'Y',
  332. 'created' => '2007-03-18 10:41:23',
  333. 'updated' => '2007-03-18 10:43:31'
  334. ),
  335. 'User' => array(
  336. 'id' => '3',
  337. 'user' => 'larry',
  338. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  339. 'created' => '2007-03-17 01:20:23',
  340. 'updated' => '2007-03-17 01:22:31'
  341. ),
  342. 'Comment' => array(
  343. array(
  344. 'id' => '5',
  345. 'article_id' => '2',
  346. 'user_id' => '1',
  347. 'comment' => 'First Comment for Second Article',
  348. 'published' => 'Y',
  349. 'created' => '2007-03-18 10:53:23',
  350. 'updated' => '2007-03-18 10:55:31'
  351. ),
  352. array(
  353. 'id' => '6',
  354. 'article_id' => '2',
  355. 'user_id' => '2',
  356. 'comment' => 'Second Comment for Second Article',
  357. 'published' => 'Y',
  358. 'created' => '2007-03-18 10:55:23',
  359. 'updated' => '2007-03-18 10:57:31'
  360. )),
  361. 'Tag' => array(
  362. array(
  363. 'id' => '1',
  364. 'tag' => 'tag1',
  365. 'created' => '2007-03-18 12:22:23',
  366. 'updated' => '2007-03-18 12:24:31'
  367. ),
  368. array(
  369. 'id' => '3',
  370. 'tag' => 'tag3',
  371. 'created' => '2007-03-18 12:26:23',
  372. 'updated' => '2007-03-18 12:28:31'
  373. ))),
  374. array(
  375. 'Article' => array(
  376. 'id' => '3',
  377. 'user_id' => '1',
  378. 'title' => 'Third Article',
  379. 'body' => 'Third Article Body',
  380. 'published' => 'Y',
  381. 'created' => '2007-03-18 10:43:23',
  382. 'updated' => '2007-03-18 10:45:31'
  383. ),
  384. 'User' => array(
  385. 'id' => '1',
  386. 'user' => 'mariano',
  387. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  388. 'created' => '2007-03-17 01:16:23',
  389. 'updated' => '2007-03-17 01:18:31'
  390. ),
  391. 'Comment' => array(),
  392. 'Tag' => array()
  393. ));
  394. $this->assertEquals($expected, $TestModel->find('all'));
  395. $db2 = ConnectionManager::getDataSource('test2');
  396. $this->fixtureManager->loadSingle('User', $db2);
  397. $this->fixtureManager->loadSingle('Comment', $db2);
  398. $this->assertEquals(3, $TestModel->find('count'));
  399. $TestModel->User->setDataSource('test2');
  400. $TestModel->Comment->setDataSource('test2');
  401. foreach ($expected as $key => $value) {
  402. unset($value['Comment'], $value['Tag']);
  403. $expected[$key] = $value;
  404. }
  405. $TestModel->recursive = 0;
  406. $result = $TestModel->find('all');
  407. $this->assertEquals($expected, $result);
  408. foreach ($expected as $key => $value) {
  409. unset($value['Comment'], $value['Tag']);
  410. $expected[$key] = $value;
  411. }
  412. $TestModel->recursive = 0;
  413. $result = $TestModel->find('all');
  414. $this->assertEquals($expected, $result);
  415. $result = Hash::extract($TestModel->User->find('all'), '{n}.User.id');
  416. $this->assertEquals(array('1', '2', '3', '4'), $result);
  417. $this->assertEquals($expected, $TestModel->find('all'));
  418. $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')));
  419. $expected = array(
  420. array(
  421. 'Comment' => array(
  422. 'id' => '1',
  423. 'article_id' => '1',
  424. 'user_id' => '2',
  425. 'comment' => 'First Comment for First Article',
  426. 'published' => 'Y',
  427. 'created' => '2007-03-18 10:45:23',
  428. 'updated' => '2007-03-18 10:47:31'
  429. ),
  430. 'User' => array(
  431. 'id' => '2',
  432. 'user' => 'nate',
  433. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  434. 'created' => '2007-03-17 01:18:23',
  435. 'updated' => '2007-03-17 01:20:31'
  436. ),
  437. 'Article' => array(
  438. 'id' => '1',
  439. 'user_id' => '1',
  440. 'title' => 'First Article',
  441. 'body' => 'First Article Body',
  442. 'published' => 'Y',
  443. 'created' => '2007-03-18 10:39:23',
  444. 'updated' => '2007-03-18 10:41:31'
  445. )),
  446. array(
  447. 'Comment' => array(
  448. 'id' => '2',
  449. 'article_id' => '1',
  450. 'user_id' => '4',
  451. 'comment' => 'Second Comment for First Article',
  452. 'published' => 'Y',
  453. 'created' => '2007-03-18 10:47:23',
  454. 'updated' => '2007-03-18 10:49:31'
  455. ),
  456. 'User' => array(
  457. 'id' => '4',
  458. 'user' => 'garrett',
  459. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  460. 'created' => '2007-03-17 01:22:23',
  461. 'updated' => '2007-03-17 01:24:31'
  462. ),
  463. 'Article' => array(
  464. 'id' => '1',
  465. 'user_id' => '1',
  466. 'title' => 'First Article',
  467. 'body' => 'First Article Body',
  468. 'published' => 'Y',
  469. 'created' => '2007-03-18 10:39:23',
  470. 'updated' => '2007-03-18 10:41:31'
  471. )),
  472. array(
  473. 'Comment' => array(
  474. 'id' => '3',
  475. 'article_id' => '1',
  476. 'user_id' => '1',
  477. 'comment' => 'Third Comment for First Article',
  478. 'published' => 'Y',
  479. 'created' => '2007-03-18 10:49:23',
  480. 'updated' => '2007-03-18 10:51:31'
  481. ),
  482. 'User' => array(
  483. 'id' => '1',
  484. 'user' => 'mariano',
  485. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  486. 'created' => '2007-03-17 01:16:23',
  487. 'updated' => '2007-03-17 01:18:31'
  488. ),
  489. 'Article' => array(
  490. 'id' => '1',
  491. 'user_id' => '1',
  492. 'title' => 'First Article',
  493. 'body' => 'First Article Body',
  494. 'published' => 'Y',
  495. 'created' => '2007-03-18 10:39:23',
  496. 'updated' => '2007-03-18 10:41:31'
  497. )),
  498. array(
  499. 'Comment' => array(
  500. 'id' => '4',
  501. 'article_id' => '1',
  502. 'user_id' => '1',
  503. 'comment' => 'Fourth Comment for First Article',
  504. 'published' => 'N',
  505. 'created' => '2007-03-18 10:51:23',
  506. 'updated' => '2007-03-18 10:53:31'
  507. ),
  508. 'User' => array(
  509. 'id' => '1',
  510. 'user' => 'mariano',
  511. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  512. 'created' => '2007-03-17 01:16:23',
  513. 'updated' => '2007-03-17 01:18:31'
  514. ),
  515. 'Article' => array(
  516. 'id' => '1',
  517. 'user_id' => '1',
  518. 'title' => 'First Article',
  519. 'body' => 'First Article Body',
  520. 'published' => 'Y',
  521. 'created' => '2007-03-18 10:39:23',
  522. 'updated' => '2007-03-18 10:41:31'
  523. )),
  524. array(
  525. 'Comment' => array(
  526. 'id' => '5',
  527. 'article_id' => '2',
  528. 'user_id' => '1',
  529. 'comment' => 'First Comment for Second Article',
  530. 'published' => 'Y',
  531. 'created' => '2007-03-18 10:53:23',
  532. 'updated' => '2007-03-18 10:55:31'
  533. ),
  534. 'User' => array(
  535. 'id' => '1',
  536. 'user' => 'mariano',
  537. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  538. 'created' => '2007-03-17 01:16:23',
  539. 'updated' => '2007-03-17 01:18:31'
  540. ),
  541. 'Article' => array(
  542. 'id' => '2',
  543. 'user_id' => '3',
  544. 'title' => 'Second Article',
  545. 'body' => 'Second Article Body',
  546. 'published' => 'Y',
  547. 'created' => '2007-03-18 10:41:23',
  548. 'updated' => '2007-03-18 10:43:31'
  549. )),
  550. array(
  551. 'Comment' => array(
  552. 'id' => '6',
  553. 'article_id' => '2',
  554. 'user_id' => '2',
  555. 'comment' => 'Second Comment for Second Article',
  556. 'published' => 'Y',
  557. 'created' => '2007-03-18 10:55:23',
  558. 'updated' => '2007-03-18 10:57:31'
  559. ),
  560. 'User' => array(
  561. 'id' => '2',
  562. 'user' => 'nate',
  563. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  564. 'created' => '2007-03-17 01:18:23',
  565. 'updated' => '2007-03-17 01:20:31'
  566. ),
  567. 'Article' => array(
  568. 'id' => '2',
  569. 'user_id' => '3',
  570. 'title' => 'Second Article',
  571. 'body' => 'Second Article Body',
  572. 'published' => 'Y',
  573. 'created' => '2007-03-18 10:41:23',
  574. 'updated' => '2007-03-18 10:43:31'
  575. )));
  576. $this->assertEquals($expected, $TestModel->Comment->find('all'));
  577. }
  578. /**
  579. * test HABM operations without clobbering existing records #275
  580. *
  581. * @return void
  582. */
  583. public function testHABTMKeepExisting() {
  584. $this->loadFixtures('Site', 'Domain', 'DomainsSite');
  585. $Site = new Site();
  586. $results = $Site->find('count');
  587. $expected = 3;
  588. $this->assertEquals($expected, $results);
  589. $data = $Site->findById(1);
  590. // include api.cakephp.org
  591. $data['Domain'] = array('Domain' => array(1, 2, 3));
  592. $Site->save($data);
  593. $Site->id = 1;
  594. $results = $Site->read();
  595. $expected = 3; // 3 domains belonging to cakephp
  596. $this->assertEquals($expected, count($results['Domain']));
  597. $Site->id = 2;
  598. $results = $Site->read();
  599. $expected = 2; // 2 domains belonging to markstory
  600. $this->assertEquals($expected, count($results['Domain']));
  601. $Site->id = 3;
  602. $results = $Site->read();
  603. $expected = 2;
  604. $this->assertEquals($expected, count($results['Domain']));
  605. $results['Domain'] = array('Domain' => array(7));
  606. $Site->save($results); // remove association from domain 6
  607. $results = $Site->read();
  608. $expected = 1; // only 1 domain left belonging to rchavik
  609. $this->assertEquals($expected, count($results['Domain']));
  610. // add deleted domain back
  611. $results['Domain'] = array('Domain' => array(6, 7));
  612. $Site->save($results);
  613. $results = $Site->read();
  614. $expected = 2; // 2 domains belonging to rchavik
  615. $this->assertEquals($expected, count($results['Domain']));
  616. $Site->DomainsSite->id = $results['Domain'][0]['DomainsSite']['id'];
  617. $Site->DomainsSite->saveField('active', true);
  618. $results = $Site->Domain->DomainsSite->find('count', array(
  619. 'conditions' => array(
  620. 'DomainsSite.active' => true,
  621. ),
  622. ));
  623. $expected = 5;
  624. $this->assertEquals($expected, $results);
  625. // activate api.cakephp.org
  626. $activated = $Site->DomainsSite->findByDomainId(3);
  627. $activated['DomainsSite']['active'] = true;
  628. $Site->DomainsSite->save($activated);
  629. $results = $Site->DomainsSite->find('count', array(
  630. 'conditions' => array(
  631. 'DomainsSite.active' => true,
  632. ),
  633. ));
  634. $expected = 6;
  635. $this->assertEquals($expected, $results);
  636. // remove 2 previously active domains, and leave $activated alone
  637. $data = array(
  638. 'Site' => array('id' => 1, 'name' => 'cakephp (modified)'),
  639. 'Domain' => array(
  640. 'Domain' => array(3),
  641. )
  642. );
  643. $Site->create($data);
  644. $Site->save($data);
  645. // tests that record is still identical prior to removal
  646. $Site->id = 1;
  647. $results = $Site->read();
  648. unset($results['Domain'][0]['DomainsSite']['updated']);
  649. unset($activated['DomainsSite']['updated']);
  650. $this->assertEquals($activated['DomainsSite'], $results['Domain'][0]['DomainsSite']);
  651. }
  652. /**
  653. * testHABTMKeepExistingAlternateDataFormat
  654. *
  655. * @return void
  656. */
  657. public function testHABTMKeepExistingAlternateDataFormat() {
  658. $this->loadFixtures('Site', 'Domain', 'DomainsSite');
  659. $Site = new Site();
  660. $expected = array(
  661. array(
  662. 'DomainsSite' => array(
  663. 'id' => 1,
  664. 'site_id' => 1,
  665. 'domain_id' => 1,
  666. 'active' => true,
  667. 'created' => '2007-03-17 01:16:23'
  668. )
  669. ),
  670. array(
  671. 'DomainsSite' => array(
  672. 'id' => 2,
  673. 'site_id' => 1,
  674. 'domain_id' => 2,
  675. 'active' => true,
  676. 'created' => '2007-03-17 01:16:23'
  677. )
  678. )
  679. );
  680. $result = $Site->DomainsSite->find('all', array(
  681. 'conditions' => array('DomainsSite.site_id' => 1),
  682. 'fields' => array(
  683. 'DomainsSite.id',
  684. 'DomainsSite.site_id',
  685. 'DomainsSite.domain_id',
  686. 'DomainsSite.active',
  687. 'DomainsSite.created'
  688. ),
  689. 'order' => 'DomainsSite.id'
  690. ));
  691. $this->assertEquals($expected, $result);
  692. $time = date('Y-m-d H:i:s');
  693. $data = array(
  694. 'Site' => array(
  695. 'id' => 1
  696. ),
  697. 'Domain' => array(
  698. array(
  699. 'site_id' => 1,
  700. 'domain_id' => 3,
  701. 'created' => $time,
  702. ),
  703. array(
  704. 'id' => 2,
  705. 'site_id' => 1,
  706. 'domain_id' => 2
  707. ),
  708. )
  709. );
  710. $Site->save($data);
  711. $expected = array(
  712. array(
  713. 'DomainsSite' => array(
  714. 'id' => 2,
  715. 'site_id' => 1,
  716. 'domain_id' => 2,
  717. 'active' => true,
  718. 'created' => '2007-03-17 01:16:23'
  719. )
  720. ),
  721. array(
  722. 'DomainsSite' => array(
  723. 'id' => 7,
  724. 'site_id' => 1,
  725. 'domain_id' => 3,
  726. 'active' => false,
  727. 'created' => $time
  728. )
  729. )
  730. );
  731. $result = $Site->DomainsSite->find('all', array(
  732. 'conditions' => array('DomainsSite.site_id' => 1),
  733. 'fields' => array(
  734. 'DomainsSite.id',
  735. 'DomainsSite.site_id',
  736. 'DomainsSite.domain_id',
  737. 'DomainsSite.active',
  738. 'DomainsSite.created'
  739. ),
  740. 'order' => 'DomainsSite.id'
  741. ));
  742. $this->assertEquals($expected, $result);
  743. }
  744. /**
  745. * test HABM operations without clobbering existing records #275
  746. *
  747. * @return void
  748. */
  749. public function testHABTMKeepExistingWithThreeDbs() {
  750. $config = ConnectionManager::enumConnectionObjects();
  751. $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with Sqlite.');
  752. $this->skipIf(
  753. !isset($config['test']) || !isset($config['test2']) || !isset($config['test_database_three']),
  754. '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.'
  755. );
  756. $this->loadFixtures('Player', 'Guild', 'GuildsPlayer', 'Armor', 'ArmorsPlayer');
  757. $Player = ClassRegistry::init('Player');
  758. $Player->bindModel(array(
  759. 'hasAndBelongsToMany' => array(
  760. 'Armor' => array(
  761. 'with' => 'ArmorsPlayer',
  762. 'unique' => 'keepExisting',
  763. ),
  764. ),
  765. ), false);
  766. $this->assertEquals('test', $Player->useDbConfig);
  767. $this->assertEquals('test', $Player->Guild->useDbConfig);
  768. $this->assertEquals('test2', $Player->Guild->GuildsPlayer->useDbConfig);
  769. $this->assertEquals('test2', $Player->Armor->useDbConfig);
  770. $this->assertEquals('test_database_three', $Player->ArmorsPlayer->useDbConfig);
  771. $players = $Player->find('all');
  772. $this->assertEquals(4 , count($players));
  773. $playersGuilds = Hash::extract($players, '{n}.Guild.{n}.GuildsPlayer');
  774. $this->assertEquals(3 , count($playersGuilds));
  775. $playersArmors = Hash::extract($players, '{n}.Armor.{n}.ArmorsPlayer');
  776. $this->assertEquals(3 , count($playersArmors));
  777. unset($players);
  778. $larry = $Player->findByName('larry');
  779. $larrysArmor = Hash::extract($larry, 'Armor.{n}.ArmorsPlayer');
  780. $this->assertEquals(1 , count($larrysArmor));
  781. $larry['Guild']['Guild'] = array(1, 3); // larry joins another guild
  782. $larry['Armor']['Armor'] = array(2, 3); // purchases chainmail
  783. $Player->save($larry);
  784. unset($larry);
  785. $larry = $Player->findByName('larry');
  786. $larrysGuild = Hash::extract($larry, 'Guild.{n}.GuildsPlayer');
  787. $this->assertEquals(2 , count($larrysGuild));
  788. $larrysArmor = Hash::extract($larry, 'Armor.{n}.ArmorsPlayer');
  789. $this->assertEquals(2 , count($larrysArmor));
  790. $larrysArmorsPlayersIds = Hash::extract($larry, 'Armor.{n}.ArmorsPlayer.id');
  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' => '', 'deleteQuery' => '', 'insertQuery' => ''
  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. 'deleteQuery' => '',
  1422. 'insertQuery' => ''
  1423. ));
  1424. $this->assertSame($TestModel->hasAndBelongsToMany, $expected);
  1425. $this->assertSame($TestFakeModel->hasAndBelongsToMany, $expected);
  1426. $this->assertEquals('Tag', $TestModel->Tag->name);
  1427. $this->assertEquals('Tag', $TestFakeModel->Tag->name);
  1428. }
  1429. /**
  1430. * test creating associations with plugins. Ensure a double alias isn't created
  1431. *
  1432. * @return void
  1433. */
  1434. public function testAutoConstructPluginAssociations() {
  1435. $Comment = ClassRegistry::init('TestPluginComment');
  1436. $this->assertEquals(2, count($Comment->belongsTo), 'Too many associations');
  1437. $this->assertFalse(isset($Comment->belongsTo['TestPlugin.User']));
  1438. $this->assertTrue(isset($Comment->belongsTo['User']), 'Missing association');
  1439. $this->assertTrue(isset($Comment->belongsTo['TestPluginArticle']), 'Missing association');
  1440. }
  1441. /**
  1442. * test Model::__construct
  1443. *
  1444. * ensure that $actsAS and $findMethods are merged.
  1445. *
  1446. * @return void
  1447. */
  1448. public function testConstruct() {
  1449. $this->loadFixtures('Post');
  1450. $TestModel = ClassRegistry::init('MergeVarPluginPost');
  1451. $this->assertEquals(array('Containable' => null, 'Tree' => null), $TestModel->actsAs);
  1452. $this->assertTrue(isset($TestModel->Behaviors->Containable));
  1453. $this->assertTrue(isset($TestModel->Behaviors->Tree));
  1454. $TestModel = ClassRegistry::init('MergeVarPluginComment');
  1455. $expected = array('Containable' => array('some_settings'));
  1456. $this->assertEquals($expected, $TestModel->actsAs);
  1457. $this->assertTrue(isset($TestModel->Behaviors->Containable));
  1458. }
  1459. /**
  1460. * test Model::__construct
  1461. *
  1462. * ensure that $actsAS and $findMethods are merged.
  1463. *
  1464. * @return void
  1465. */
  1466. public function testConstructWithAlternateDataSource() {
  1467. $TestModel = ClassRegistry::init(array(
  1468. 'class' => 'DoesntMatter', 'ds' => 'test', 'table' => false
  1469. ));
  1470. $this->assertEquals('test', $TestModel->useDbConfig);
  1471. //deprecated but test it anyway
  1472. $NewVoid = new TheVoid(null, false, 'other');
  1473. $this->assertEquals('other', $NewVoid->useDbConfig);
  1474. }
  1475. /**
  1476. * testColumnTypeFetching method
  1477. *
  1478. * @return void
  1479. */
  1480. public function testColumnTypeFetching() {
  1481. $model = new Test();
  1482. $this->assertEquals('integer', $model->getColumnType('id'));
  1483. $this->assertEquals('text', $model->getColumnType('notes'));
  1484. $this->assertEquals('datetime', $model->getColumnType('updated'));
  1485. $this->assertEquals(null, $model->getColumnType('unknown'));
  1486. $model = new Article();
  1487. $this->assertEquals('datetime', $model->getColumnType('User.created'));
  1488. $this->assertEquals('integer', $model->getColumnType('Tag.id'));
  1489. $this->assertEquals('integer', $model->getColumnType('Article.id'));
  1490. }
  1491. /**
  1492. * testHabtmUniqueKey method
  1493. *
  1494. * @return void
  1495. */
  1496. public function testHabtmUniqueKey() {
  1497. $model = new Item();
  1498. $this->assertFalse($model->hasAndBelongsToMany['Portfolio']['unique']);
  1499. }
  1500. /**
  1501. * testIdentity method
  1502. *
  1503. * @return void
  1504. */
  1505. public function testIdentity() {
  1506. $TestModel = new Test();
  1507. $result = $TestModel->alias;
  1508. $expected = 'Test';
  1509. $this->assertEquals($expected, $result);
  1510. $TestModel = new TestAlias();
  1511. $result = $TestModel->alias;
  1512. $expected = 'TestAlias';
  1513. $this->assertEquals($expected, $result);
  1514. $TestModel = new Test(array('alias' => 'AnotherTest'));
  1515. $result = $TestModel->alias;
  1516. $expected = 'AnotherTest';
  1517. $this->assertEquals($expected, $result);
  1518. $TestModel = ClassRegistry::init('Test');
  1519. $expected = null;
  1520. $this->assertEquals($expected, $TestModel->plugin);
  1521. $TestModel = ClassRegistry::init('TestPlugin.TestPluginComment');
  1522. $expected = 'TestPlugin';
  1523. $this->assertEquals($expected, $TestModel->plugin);
  1524. }
  1525. /**
  1526. * testWithAssociation method
  1527. *
  1528. * @return void
  1529. */
  1530. public function testWithAssociation() {
  1531. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  1532. $TestModel = new Something();
  1533. $result = $TestModel->SomethingElse->find('all');
  1534. $expected = array(
  1535. array(
  1536. 'SomethingElse' => array(
  1537. 'id' => '1',
  1538. 'title' => 'First Post',
  1539. 'body' => 'First Post Body',
  1540. 'published' => 'Y',
  1541. 'created' => '2007-03-18 10:39:23',
  1542. 'updated' => '2007-03-18 10:41:31'
  1543. ),
  1544. 'Something' => array(
  1545. array(
  1546. 'id' => '3',
  1547. 'title' => 'Third Post',
  1548. 'body' => 'Third Post Body',
  1549. 'published' => 'Y',
  1550. 'created' => '2007-03-18 10:43:23',
  1551. 'updated' => '2007-03-18 10:45:31',
  1552. 'JoinThing' => array(
  1553. 'id' => '3',
  1554. 'something_id' => '3',
  1555. 'something_else_id' => '1',
  1556. 'doomed' => true,
  1557. 'created' => '2007-03-18 10:43:23',
  1558. 'updated' => '2007-03-18 10:45:31'
  1559. )))),
  1560. array(
  1561. 'SomethingElse' => array(
  1562. 'id' => '2',
  1563. 'title' => 'Second Post',
  1564. 'body' => 'Second Post Body',
  1565. 'published' => 'Y',
  1566. 'created' => '2007-03-18 10:41:23',
  1567. 'updated' => '2007-03-18 10:43:31'
  1568. ),
  1569. 'Something' => array(
  1570. array(
  1571. 'id' => '1',
  1572. 'title' => 'First Post',
  1573. 'body' => 'First Post Body',
  1574. 'published' => 'Y',
  1575. 'created' => '2007-03-18 10:39:23',
  1576. 'updated' => '2007-03-18 10:41:31',
  1577. 'JoinThing' => array(
  1578. 'id' => '1',
  1579. 'something_id' => '1',
  1580. 'something_else_id' => '2',
  1581. 'doomed' => true,
  1582. 'created' => '2007-03-18 10:39:23',
  1583. 'updated' => '2007-03-18 10:41:31'
  1584. )))),
  1585. array(
  1586. 'SomethingElse' => array(
  1587. 'id' => '3',
  1588. 'title' => 'Third Post',
  1589. 'body' => 'Third Post Body',
  1590. 'published' => 'Y',
  1591. 'created' => '2007-03-18 10:43:23',
  1592. 'updated' => '2007-03-18 10:45:31'
  1593. ),
  1594. 'Something' => array(
  1595. array(
  1596. 'id' => '2',
  1597. 'title' => 'Second Post',
  1598. 'body' => 'Second Post Body',
  1599. 'published' => 'Y',
  1600. 'created' => '2007-03-18 10:41:23',
  1601. 'updated' => '2007-03-18 10:43:31',
  1602. 'JoinThing' => array(
  1603. 'id' => '2',
  1604. 'something_id' => '2',
  1605. 'something_else_id' => '3',
  1606. 'doomed' => false,
  1607. 'created' => '2007-03-18 10:41:23',
  1608. 'updated' => '2007-03-18 10:43:31'
  1609. )))));
  1610. $this->assertEquals($expected, $result);
  1611. $result = $TestModel->find('all');
  1612. $expected = array(
  1613. array(
  1614. 'Something' => array(
  1615. 'id' => '1',
  1616. 'title' => 'First Post',
  1617. 'body' => 'First Post Body',
  1618. 'published' => 'Y',
  1619. 'created' => '2007-03-18 10:39:23',
  1620. 'updated' => '2007-03-18 10:41:31'
  1621. ),
  1622. 'SomethingElse' => array(
  1623. array(
  1624. 'id' => '2',
  1625. 'title' => 'Second Post',
  1626. 'body' => 'Second Post Body',
  1627. 'published' => 'Y',
  1628. 'created' => '2007-03-18 10:41:23',
  1629. 'updated' => '2007-03-18 10:43:31',
  1630. 'JoinThing' => array(
  1631. 'doomed' => true,
  1632. 'something_id' => '1',
  1633. 'something_else_id' => '2'
  1634. )))),
  1635. array(
  1636. 'Something' => array(
  1637. 'id' => '2',
  1638. 'title' => 'Second Post',
  1639. 'body' => 'Second Post Body',
  1640. 'published' => 'Y',
  1641. 'created' => '2007-03-18 10:41:23',
  1642. 'updated' => '2007-03-18 10:43:31'
  1643. ),
  1644. 'SomethingElse' => array(
  1645. array(
  1646. 'id' => '3',
  1647. 'title' => 'Third Post',
  1648. 'body' => 'Third Post Body',
  1649. 'published' => 'Y',
  1650. 'created' => '2007-03-18 10:43:23',
  1651. 'updated' => '2007-03-18 10:45:31',
  1652. 'JoinThing' => array(
  1653. 'doomed' => false,
  1654. 'something_id' => '2',
  1655. 'something_else_id' => '3'
  1656. )))),
  1657. array(
  1658. 'Something' => array(
  1659. 'id' => '3',
  1660. 'title' => 'Third Post',
  1661. 'body' => 'Third Post Body',
  1662. 'published' => 'Y',
  1663. 'created' => '2007-03-18 10:43:23',
  1664. 'updated' => '2007-03-18 10:45:31'
  1665. ),
  1666. 'SomethingElse' => array(
  1667. array(
  1668. 'id' => '1',
  1669. 'title' => 'First Post',
  1670. 'body' => 'First Post Body',
  1671. 'published' => 'Y',
  1672. 'created' => '2007-03-18 10:39:23',
  1673. 'updated' => '2007-03-18 10:41:31',
  1674. 'JoinThing' => array(
  1675. 'doomed' => true,
  1676. 'something_id' => '3',
  1677. 'something_else_id' => '1'
  1678. )))));
  1679. $this->assertEquals($expected, $result);
  1680. $result = $TestModel->findById(1);
  1681. $expected = array(
  1682. 'Something' => array(
  1683. 'id' => '1',
  1684. 'title' => 'First Post',
  1685. 'body' => 'First Post Body',
  1686. 'published' => 'Y',
  1687. 'created' => '2007-03-18 10:39:23',
  1688. 'updated' => '2007-03-18 10:41:31'
  1689. ),
  1690. 'SomethingElse' => array(
  1691. array(
  1692. 'id' => '2',
  1693. 'title' => 'Second Post',
  1694. 'body' => 'Second Post Body',
  1695. 'published' => 'Y',
  1696. 'created' => '2007-03-18 10:41:23',
  1697. 'updated' => '2007-03-18 10:43:31',
  1698. 'JoinThing' => array(
  1699. 'doomed' => true,
  1700. 'something_id' => '1',
  1701. 'something_else_id' => '2'
  1702. ))));
  1703. $this->assertEquals($expected, $result);
  1704. $expected = $TestModel->findById(1);
  1705. $TestModel->set($expected);
  1706. $TestModel->save();
  1707. $result = $TestModel->findById(1);
  1708. $this->assertEquals($expected, $result);
  1709. $TestModel->hasAndBelongsToMany['SomethingElse']['unique'] = false;
  1710. $TestModel->create(array(
  1711. 'Something' => array('id' => 1),
  1712. 'SomethingElse' => array(3, array(
  1713. 'something_else_id' => 1,
  1714. 'doomed' => true
  1715. ))));
  1716. $TestModel->save();
  1717. $TestModel->hasAndBelongsToMany['SomethingElse']['order'] = 'SomethingElse.id ASC';
  1718. $result = $TestModel->findById(1);
  1719. $expected = array(
  1720. 'Something' => array(
  1721. 'id' => '1',
  1722. 'title' => 'First Post',
  1723. 'body' => 'First Post Body',
  1724. 'published' => 'Y',
  1725. 'created' => '2007-03-18 10:39:23'
  1726. ),
  1727. 'SomethingElse' => array(
  1728. array(
  1729. 'id' => '1',
  1730. 'title' => 'First Post',
  1731. 'body' => 'First Post Body',
  1732. 'published' => 'Y',
  1733. 'created' => '2007-03-18 10:39:23',
  1734. 'updated' => '2007-03-18 10:41:31',
  1735. 'JoinThing' => array(
  1736. 'doomed' => true,
  1737. 'something_id' => '1',
  1738. 'something_else_id' => '1'
  1739. )
  1740. ),
  1741. array(
  1742. 'id' => '2',
  1743. 'title' => 'Second Post',
  1744. 'body' => 'Second Post Body',
  1745. 'published' => 'Y',
  1746. 'created' => '2007-03-18 10:41:23',
  1747. 'updated' => '2007-03-18 10:43:31',
  1748. 'JoinThing' => array(
  1749. 'doomed' => true,
  1750. 'something_id' => '1',
  1751. 'something_else_id' => '2'
  1752. )
  1753. ),
  1754. array(
  1755. 'id' => '3',
  1756. 'title' => 'Third Post',
  1757. 'body' => 'Third Post Body',
  1758. 'published' => 'Y',
  1759. 'created' => '2007-03-18 10:43:23',
  1760. 'updated' => '2007-03-18 10:45:31',
  1761. 'JoinThing' => array(
  1762. 'doomed' => false,
  1763. 'something_id' => '1',
  1764. 'something_else_id' => '3')
  1765. )
  1766. )
  1767. );
  1768. $this->assertEquals(self::date(), $result['Something']['updated']);
  1769. unset($result['Something']['updated']);
  1770. $this->assertEquals($expected, $result);
  1771. }
  1772. /**
  1773. * testFindSelfAssociations method
  1774. *
  1775. * @return void
  1776. */
  1777. public function testFindSelfAssociations() {
  1778. $this->loadFixtures('Person');
  1779. $TestModel = new Person();
  1780. $TestModel->recursive = 2;
  1781. $result = $TestModel->read(null, 1);
  1782. $expected = array(
  1783. 'Person' => array(
  1784. 'id' => 1,
  1785. 'name' => 'person',
  1786. 'mother_id' => 2,
  1787. 'father_id' => 3
  1788. ),
  1789. 'Mother' => array(
  1790. 'id' => 2,
  1791. 'name' => 'mother',
  1792. 'mother_id' => 4,
  1793. 'father_id' => 5,
  1794. 'Mother' => array(
  1795. 'id' => 4,
  1796. 'name' => 'mother - grand mother',
  1797. 'mother_id' => 0,
  1798. 'father_id' => 0
  1799. ),
  1800. 'Father' => array(
  1801. 'id' => 5,
  1802. 'name' => 'mother - grand father',
  1803. 'mother_id' => 0,
  1804. 'father_id' => 0
  1805. )),
  1806. 'Father' => array(
  1807. 'id' => 3,
  1808. 'name' => 'father',
  1809. 'mother_id' => 6,
  1810. 'father_id' => 7,
  1811. 'Father' => array(
  1812. 'id' => 7,
  1813. 'name' => 'father - grand father',
  1814. 'mother_id' => 0,
  1815. 'father_id' => 0
  1816. ),
  1817. 'Mother' => array(
  1818. 'id' => 6,
  1819. 'name' => 'father - grand mother',
  1820. 'mother_id' => 0,
  1821. 'father_id' => 0
  1822. )));
  1823. $this->assertEquals($expected, $result);
  1824. $TestModel->recursive = 3;
  1825. $result = $TestModel->read(null, 1);
  1826. $expected = array(
  1827. 'Person' => array(
  1828. 'id' => 1,
  1829. 'name' => 'person',
  1830. 'mother_id' => 2,
  1831. 'father_id' => 3
  1832. ),
  1833. 'Mother' => array(
  1834. 'id' => 2,
  1835. 'name' => 'mother',
  1836. 'mother_id' => 4,
  1837. 'father_id' => 5,
  1838. 'Mother' => array(
  1839. 'id' => 4,
  1840. 'name' => 'mother - grand mother',
  1841. 'mother_id' => 0,
  1842. 'father_id' => 0,
  1843. 'Mother' => array(),
  1844. 'Father' => array()),
  1845. 'Father' => array(
  1846. 'id' => 5,
  1847. 'name' => 'mother - grand father',
  1848. 'mother_id' => 0,
  1849. 'father_id' => 0,
  1850. 'Father' => array(),
  1851. 'Mother' => array()
  1852. )),
  1853. 'Father' => array(
  1854. 'id' => 3,
  1855. 'name' => 'father',
  1856. 'mother_id' => 6,
  1857. 'father_id' => 7,
  1858. 'Father' => array(
  1859. 'id' => 7,
  1860. 'name' => 'father - grand father',
  1861. 'mother_id' => 0,
  1862. 'father_id' => 0,
  1863. 'Father' => array(),
  1864. 'Mother' => array()
  1865. ),
  1866. 'Mother' => array(
  1867. 'id' => 6,
  1868. 'name' => 'father - grand mother',
  1869. 'mother_id' => 0,
  1870. 'father_id' => 0,
  1871. 'Mother' => array(),
  1872. 'Father' => array()
  1873. )));
  1874. $this->assertEquals($expected, $result);
  1875. }
  1876. /**
  1877. * testDynamicAssociations method
  1878. *
  1879. * @return void
  1880. */
  1881. public function testDynamicAssociations() {
  1882. $this->loadFixtures('Article', 'Comment');
  1883. $TestModel = new Article();
  1884. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->hasOne = array();
  1885. $TestModel->hasMany['Comment'] = array_merge($TestModel->hasMany['Comment'], array(
  1886. 'foreignKey' => false,
  1887. 'conditions' => array('Comment.user_id =' => '2')
  1888. ));
  1889. $result = $TestModel->find('all');
  1890. $expected = array(
  1891. array(
  1892. 'Article' => array(
  1893. 'id' => '1',
  1894. 'user_id' => '1',
  1895. 'title' => 'First Article',
  1896. 'body' => 'First Article Body',
  1897. 'published' => 'Y',
  1898. 'created' => '2007-03-18 10:39:23',
  1899. 'updated' => '2007-03-18 10:41:31'
  1900. ),
  1901. 'Comment' => array(
  1902. array(
  1903. 'id' => '1',
  1904. 'article_id' => '1',
  1905. 'user_id' => '2',
  1906. 'comment' => 'First Comment for First Article',
  1907. 'published' => 'Y',
  1908. 'created' => '2007-03-18 10:45:23',
  1909. 'updated' => '2007-03-18 10:47:31'
  1910. ),
  1911. array(
  1912. 'id' => '6',
  1913. 'article_id' => '2',
  1914. 'user_id' => '2',
  1915. 'comment' => 'Second Comment for Second Article',
  1916. 'published' => 'Y',
  1917. 'created' => '2007-03-18 10:55:23',
  1918. 'updated' => '2007-03-18 10:57:31'
  1919. ))),
  1920. array(
  1921. 'Article' => array(
  1922. 'id' => '2',
  1923. 'user_id' => '3',
  1924. 'title' => 'Second Article',
  1925. 'body' => 'Second Article Body',
  1926. 'published' => 'Y',
  1927. 'created' => '2007-03-18 10:41:23',
  1928. 'updated' => '2007-03-18 10:43:31'
  1929. ),
  1930. 'Comment' => array(
  1931. array(
  1932. 'id' => '1',
  1933. 'article_id' => '1',
  1934. 'user_id' => '2',
  1935. 'comment' => 'First Comment for First Article',
  1936. 'published' => 'Y',
  1937. 'created' => '2007-03-18 10:45:23',
  1938. 'updated' => '2007-03-18 10:47:31'
  1939. ),
  1940. array(
  1941. 'id' => '6',
  1942. 'article_id' => '2',
  1943. 'user_id' => '2',
  1944. 'comment' => 'Second Comment for Second Article',
  1945. 'published' => 'Y',
  1946. 'created' => '2007-03-18 10:55:23',
  1947. 'updated' => '2007-03-18 10:57:31'
  1948. ))),
  1949. array(
  1950. 'Article' => array(
  1951. 'id' => '3',
  1952. 'user_id' => '1',
  1953. 'title' => 'Third Article',
  1954. 'body' => 'Third Article Body',
  1955. 'published' => 'Y',
  1956. 'created' => '2007-03-18 10:43:23',
  1957. 'updated' => '2007-03-18 10:45:31'
  1958. ),
  1959. 'Comment' => array(
  1960. array(
  1961. 'id' => '1',
  1962. 'article_id' => '1',
  1963. 'user_id' => '2',
  1964. 'comment' => 'First Comment for First Article',
  1965. 'published' => 'Y',
  1966. 'created' => '2007-03-18 10:45:23',
  1967. 'updated' => '2007-03-18 10:47:31'
  1968. ),
  1969. array(
  1970. 'id' => '6',
  1971. 'article_id' => '2',
  1972. 'user_id' => '2',
  1973. 'comment' => 'Second Comment for Second Article',
  1974. 'published' => 'Y',
  1975. 'created' => '2007-03-18 10:55:23',
  1976. 'updated' => '2007-03-18 10:57:31'
  1977. ))));
  1978. $this->assertEquals($expected, $result);
  1979. }
  1980. /**
  1981. * testCreation method
  1982. *
  1983. * @return void
  1984. */
  1985. public function testCreation() {
  1986. $this->loadFixtures('Article', 'ArticleFeaturedsTags', 'User', 'Featured');
  1987. $TestModel = new Test();
  1988. $result = $TestModel->create();
  1989. $expected = array('Test' => array('notes' => 'write some notes here'));
  1990. $this->assertEquals($expected, $result);
  1991. $TestModel = new User();
  1992. $result = $TestModel->schema();
  1993. if (isset($this->db->columns['primary_key']['length'])) {
  1994. $intLength = $this->db->columns['primary_key']['length'];
  1995. } elseif (isset($this->db->columns['integer']['length'])) {
  1996. $intLength = $this->db->columns['integer']['length'];
  1997. } else {
  1998. $intLength = 11;
  1999. }
  2000. foreach (array('collate', 'charset', 'comment') as $type) {
  2001. foreach ($result as $i => $r) {
  2002. unset($result[$i][$type]);
  2003. }
  2004. }
  2005. $expected = array(
  2006. 'id' => array(
  2007. 'type' => 'integer',
  2008. 'null' => false,
  2009. 'default' => null,
  2010. 'length' => $intLength,
  2011. 'key' => 'primary'
  2012. ),
  2013. 'user' => array(
  2014. 'type' => 'string',
  2015. 'null' => true,
  2016. 'default' => '',
  2017. 'length' => 255
  2018. ),
  2019. 'password' => array(
  2020. 'type' => 'string',
  2021. 'null' => true,
  2022. 'default' => '',
  2023. 'length' => 255
  2024. ),
  2025. 'created' => array(
  2026. 'type' => 'datetime',
  2027. 'null' => true,
  2028. 'default' => null,
  2029. 'length' => null
  2030. ),
  2031. 'updated' => array(
  2032. 'type' => 'datetime',
  2033. 'null' => true,
  2034. 'default' => null,
  2035. 'length' => null
  2036. ));
  2037. $this->assertEquals($expected, $result);
  2038. $TestModel = new Article();
  2039. $result = $TestModel->create();
  2040. $expected = array('Article' => array('published' => 'N'));
  2041. $this->assertEquals($expected, $result);
  2042. $FeaturedModel = new Featured();
  2043. $data = array(
  2044. 'article_featured_id' => 1,
  2045. 'category_id' => 1,
  2046. 'published_date' => array(
  2047. 'year' => 2008,
  2048. 'month' => 06,
  2049. 'day' => 11
  2050. ),
  2051. 'end_date' => array(
  2052. 'year' => 2008,
  2053. 'month' => 06,
  2054. 'day' => 20
  2055. ));
  2056. $expected = array(
  2057. 'Featured' => array(
  2058. 'article_featured_id' => 1,
  2059. 'category_id' => 1,
  2060. 'published_date' => '2008-06-11 00:00:00',
  2061. 'end_date' => '2008-06-20 00:00:00'
  2062. ));
  2063. $this->assertEquals($expected, $FeaturedModel->create($data));
  2064. $data = array(
  2065. 'published_date' => array(
  2066. 'year' => 2008,
  2067. 'month' => 06,
  2068. 'day' => 11
  2069. ),
  2070. 'end_date' => array(
  2071. 'year' => 2008,
  2072. 'month' => 06,
  2073. 'day' => 20
  2074. ),
  2075. 'article_featured_id' => 1,
  2076. 'category_id' => 1
  2077. );
  2078. $expected = array(
  2079. 'Featured' => array(
  2080. 'published_date' => '2008-06-11 00:00:00',
  2081. 'end_date' => '2008-06-20 00:00:00',
  2082. 'article_featured_id' => 1,
  2083. 'category_id' => 1
  2084. ));
  2085. $this->assertEquals($expected, $FeaturedModel->create($data));
  2086. }
  2087. /**
  2088. * testEscapeField to prove it escapes the field well even when it has part of the alias on it
  2089. *
  2090. * @return void
  2091. */
  2092. public function testEscapeField() {
  2093. $TestModel = new Test();
  2094. $db = $TestModel->getDataSource();
  2095. $result = $TestModel->escapeField('test_field');
  2096. $expected = $db->name('Test.test_field');
  2097. $this->assertEquals($expected, $result);
  2098. $result = $TestModel->escapeField('TestField');
  2099. $expected = $db->name('Test.TestField');
  2100. $this->assertEquals($expected, $result);
  2101. $result = $TestModel->escapeField('DomainHandle', 'Domain');
  2102. $expected = $db->name('Domain.DomainHandle');
  2103. $this->assertEquals($expected, $result);
  2104. ConnectionManager::create('mock', array('datasource' => 'DboMock'));
  2105. $TestModel->setDataSource('mock');
  2106. $db = $TestModel->getDataSource();
  2107. $result = $TestModel->escapeField('DomainHandle', 'Domain');
  2108. $expected = $db->name('Domain.DomainHandle');
  2109. $this->assertEquals($expected, $result);
  2110. ConnectionManager::drop('mock');
  2111. }
  2112. /**
  2113. * testGetID
  2114. *
  2115. * @return void
  2116. */
  2117. public function testGetID() {
  2118. $TestModel = new Test();
  2119. $result = $TestModel->getID();
  2120. $this->assertFalse($result);
  2121. $TestModel->id = 9;
  2122. $result = $TestModel->getID();
  2123. $this->assertEquals(9, $result);
  2124. $TestModel->id = array(10, 9, 8, 7);
  2125. $result = $TestModel->getID(2);
  2126. $this->assertEquals(8, $result);
  2127. $TestModel->id = array(array(), 1, 2, 3);
  2128. $result = $TestModel->getID();
  2129. $this->assertFalse($result);
  2130. }
  2131. /**
  2132. * test that model->hasMethod checks self and behaviors.
  2133. *
  2134. * @return void
  2135. */
  2136. public function testHasMethod() {
  2137. $Article = new Article();
  2138. $Article->Behaviors = $this->getMock('BehaviorCollection');
  2139. $Article->Behaviors->expects($this->at(0))
  2140. ->method('hasMethod')
  2141. ->will($this->returnValue(true));
  2142. $Article->Behaviors->expects($this->at(1))
  2143. ->method('hasMethod')
  2144. ->will($this->returnValue(false));
  2145. $this->assertTrue($Article->hasMethod('find'));
  2146. $this->assertTrue($Article->hasMethod('pass'));
  2147. $this->assertFalse($Article->hasMethod('fail'));
  2148. }
  2149. /**
  2150. * testMultischemaFixture
  2151. *
  2152. * @return void
  2153. */
  2154. public function testMultischemaFixture() {
  2155. $config = ConnectionManager::enumConnectionObjects();
  2156. $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with Sqlite.');
  2157. $this->skipIf(!isset($config['test']) || !isset($config['test2']),
  2158. 'Primary and secondary test databases not configured, skipping cross-database join tests. To run these tests define $test and $test2 in your database configuration.'
  2159. );
  2160. $this->loadFixtures('Player', 'Guild', 'GuildsPlayer');
  2161. $Player = ClassRegistry::init('Player');
  2162. $this->assertEquals('test', $Player->useDbConfig);
  2163. $this->assertEquals('test', $Player->Guild->useDbConfig);
  2164. $this->assertEquals('test2', $Player->Guild->GuildsPlayer->useDbConfig);
  2165. $this->assertEquals('test2', $Player->GuildsPlayer->useDbConfig);
  2166. $players = $Player->find('all', array('recursive' => -1));
  2167. $guilds = $Player->Guild->find('all', array('recursive' => -1));
  2168. $guildsPlayers = $Player->GuildsPlayer->find('all', array('recursive' => -1));
  2169. $this->assertEquals(true, count($players) > 1);
  2170. $this->assertEquals(true, count($guilds) > 1);
  2171. $this->assertEquals(true, count($guildsPlayers) > 1);
  2172. }
  2173. /**
  2174. * testMultischemaFixtureWithThreeDatabases, three databases
  2175. *
  2176. * @return void
  2177. */
  2178. public function testMultischemaFixtureWithThreeDatabases() {
  2179. $config = ConnectionManager::enumConnectionObjects();
  2180. $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with Sqlite.');
  2181. $this->skipIf(
  2182. !isset($config['test']) || !isset($config['test2']) || !isset($config['test_database_three']),
  2183. '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.'
  2184. );
  2185. $this->loadFixtures('Player', 'Guild', 'GuildsPlayer', 'Armor', 'ArmorsPlayer');
  2186. $Player = ClassRegistry::init('Player');
  2187. $Player->bindModel(array(
  2188. 'hasAndBelongsToMany' => array(
  2189. 'Armor' => array(
  2190. 'with' => 'ArmorsPlayer',
  2191. ),
  2192. ),
  2193. ), false);
  2194. $this->assertEquals('test', $Player->useDbConfig);
  2195. $this->assertEquals('test', $Player->Guild->useDbConfig);
  2196. $this->assertEquals('test2', $Player->Guild->GuildsPlayer->useDbConfig);
  2197. $this->assertEquals('test2', $Player->GuildsPlayer->useDbConfig);
  2198. $this->assertEquals('test2', $Player->Armor->useDbConfig);
  2199. $this->assertEquals('test_database_three', $Player->Armor->ArmorsPlayer->useDbConfig);
  2200. $this->assertEquals('test', $Player->getDataSource()->configKeyName);
  2201. $this->assertEquals('test', $Player->Guild->getDataSource()->configKeyName);
  2202. $this->assertEquals('test2', $Player->GuildsPlayer->getDataSource()->configKeyName);
  2203. $this->assertEquals('test2', $Player->Armor->getDataSource()->configKeyName);
  2204. $this->assertEquals('test_database_three', $Player->Armor->ArmorsPlayer->getDataSource()->configKeyName);
  2205. $players = $Player->find('all', array('recursive' => -1));
  2206. $guilds = $Player->Guild->find('all', array('recursive' => -1));
  2207. $guildsPlayers = $Player->GuildsPlayer->find('all', array('recursive' => -1));
  2208. $armorsPlayers = $Player->ArmorsPlayer->find('all', array('recursive' => -1));
  2209. $this->assertEquals(true, count($players) > 1);
  2210. $this->assertEquals(true, count($guilds) > 1);
  2211. $this->assertEquals(true, count($guildsPlayers) > 1);
  2212. $this->assertEquals(true, count($armorsPlayers) > 1);
  2213. }
  2214. /**
  2215. * Tests that calling schema() on a model that is not supposed to use a table
  2216. * does not trigger any calls on any datasource
  2217. *
  2218. * @return void
  2219. **/
  2220. public function testSchemaNoDB() {
  2221. $model = $this->getMock('Article', array('getDataSource'));
  2222. $model->useTable = false;
  2223. $model->expects($this->never())->method('getDataSource');
  2224. $this->assertEmpty($model->schema());
  2225. }
  2226. }