ModelIntegrationTest.php 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446
  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. $Player->ArmorsPlayer->id = 3;
  791. $Player->ArmorsPlayer->saveField('broken', true); // larry's cloak broke
  792. $larry = $Player->findByName('larry');
  793. $larrysCloak = Hash::extract($larry, 'Armor.{n}.ArmorsPlayer[armor_id=3]', $larry);
  794. $this->assertNotEmpty($larrysCloak);
  795. $this->assertTrue($larrysCloak[0]['broken']); // still broken
  796. }
  797. /**
  798. * testDisplayField method
  799. *
  800. * @return void
  801. */
  802. public function testDisplayField() {
  803. $this->loadFixtures('Post', 'Comment', 'Person', 'User');
  804. $Post = new Post();
  805. $Comment = new Comment();
  806. $Person = new Person();
  807. $this->assertEquals('title', $Post->displayField);
  808. $this->assertEquals('name', $Person->displayField);
  809. $this->assertEquals('id', $Comment->displayField);
  810. }
  811. /**
  812. * testSchema method
  813. *
  814. * @return void
  815. */
  816. public function testSchema() {
  817. $Post = new Post();
  818. $result = $Post->schema();
  819. $columns = array('id', 'author_id', 'title', 'body', 'published', 'created', 'updated');
  820. $this->assertEquals($columns, array_keys($result));
  821. $types = array('integer', 'integer', 'string', 'text', 'string', 'datetime', 'datetime');
  822. $this->assertEquals(Hash::extract(array_values($result), '{n}.type'), $types);
  823. $result = $Post->schema('body');
  824. $this->assertEquals('text', $result['type']);
  825. $this->assertNull($Post->schema('foo'));
  826. $this->assertEquals($Post->getColumnTypes(), array_combine($columns, $types));
  827. }
  828. /**
  829. * Check schema() on a model with useTable = false;
  830. *
  831. * @return void
  832. */
  833. public function testSchemaUseTableFalse() {
  834. $model = new TheVoid();
  835. $result = $model->schema();
  836. $this->assertNull($result);
  837. $result = $model->create();
  838. $this->assertEmpty($result);
  839. }
  840. /**
  841. * data provider for time tests.
  842. *
  843. * @return array
  844. */
  845. public static function timeProvider() {
  846. $db = ConnectionManager::getDataSource('test');
  847. $now = $db->expression('NOW()');
  848. return array(
  849. // blank
  850. array(
  851. array('hour' => '', 'min' => '', 'meridian' => ''),
  852. ''
  853. ),
  854. // missing hour
  855. array(
  856. array('hour' => '', 'min' => '00', 'meridian' => 'pm'),
  857. ''
  858. ),
  859. // all blank
  860. array(
  861. array('hour' => '', 'min' => '', 'sec' => ''),
  862. ''
  863. ),
  864. // set and empty merdian
  865. array(
  866. array('hour' => '1', 'min' => '00', 'meridian' => ''),
  867. ''
  868. ),
  869. // midnight
  870. array(
  871. array('hour' => '12', 'min' => '0', 'meridian' => 'am'),
  872. '00:00:00'
  873. ),
  874. array(
  875. array('hour' => '00', 'min' => '00'),
  876. '00:00:00'
  877. ),
  878. // 3am
  879. array(
  880. array('hour' => '03', 'min' => '04', 'sec' => '04'),
  881. '03:04:04'
  882. ),
  883. array(
  884. array('hour' => '3', 'min' => '4', 'sec' => '4'),
  885. '03:04:04'
  886. ),
  887. array(
  888. array('hour' => '03', 'min' => '4', 'sec' => '4'),
  889. '03:04:04'
  890. ),
  891. array(
  892. $now,
  893. $now
  894. )
  895. );
  896. }
  897. /**
  898. * test deconstruct with time fields.
  899. *
  900. * @dataProvider timeProvider
  901. * @return void
  902. */
  903. public function testDeconstructFieldsTime($input, $result) {
  904. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  905. $this->loadFixtures('Apple');
  906. $TestModel = new Apple();
  907. $data = array(
  908. 'Apple' => array(
  909. 'mytime' => $input
  910. )
  911. );
  912. $TestModel->data = null;
  913. $TestModel->set($data);
  914. $expected = array('Apple' => array('mytime' => $result));
  915. $this->assertEquals($expected, $TestModel->data);
  916. }
  917. /**
  918. * testDeconstructFields with datetime, timestamp, and date fields
  919. *
  920. * @return void
  921. */
  922. public function testDeconstructFieldsDateTime() {
  923. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  924. $this->loadFixtures('Apple');
  925. $TestModel = new Apple();
  926. //test null/empty values first
  927. $data['Apple']['created']['year'] = '';
  928. $data['Apple']['created']['month'] = '';
  929. $data['Apple']['created']['day'] = '';
  930. $data['Apple']['created']['hour'] = '';
  931. $data['Apple']['created']['min'] = '';
  932. $data['Apple']['created']['sec'] = '';
  933. $TestModel->data = null;
  934. $TestModel->set($data);
  935. $expected = array('Apple' => array('created' => ''));
  936. $this->assertEquals($expected, $TestModel->data);
  937. $data = array();
  938. $data['Apple']['date']['year'] = '';
  939. $data['Apple']['date']['month'] = '';
  940. $data['Apple']['date']['day'] = '';
  941. $TestModel->data = null;
  942. $TestModel->set($data);
  943. $expected = array('Apple' => array('date' => ''));
  944. $this->assertEquals($expected, $TestModel->data);
  945. $data = array();
  946. $data['Apple']['created']['year'] = '2007';
  947. $data['Apple']['created']['month'] = '08';
  948. $data['Apple']['created']['day'] = '20';
  949. $data['Apple']['created']['hour'] = '';
  950. $data['Apple']['created']['min'] = '';
  951. $data['Apple']['created']['sec'] = '';
  952. $TestModel->data = null;
  953. $TestModel->set($data);
  954. $expected = array('Apple' => array('created' => '2007-08-20 00:00:00'));
  955. $this->assertEquals($expected, $TestModel->data);
  956. $data = array();
  957. $data['Apple']['created']['year'] = '2007';
  958. $data['Apple']['created']['month'] = '08';
  959. $data['Apple']['created']['day'] = '20';
  960. $data['Apple']['created']['hour'] = '10';
  961. $data['Apple']['created']['min'] = '12';
  962. $data['Apple']['created']['sec'] = '';
  963. $TestModel->data = null;
  964. $TestModel->set($data);
  965. $expected = array('Apple' => array('created' => '2007-08-20 10:12:00'));
  966. $this->assertEquals($expected, $TestModel->data);
  967. $data = array();
  968. $data['Apple']['created']['year'] = '2007';
  969. $data['Apple']['created']['month'] = '';
  970. $data['Apple']['created']['day'] = '12';
  971. $data['Apple']['created']['hour'] = '20';
  972. $data['Apple']['created']['min'] = '';
  973. $data['Apple']['created']['sec'] = '';
  974. $TestModel->data = null;
  975. $TestModel->set($data);
  976. $expected = array('Apple' => array('created' => ''));
  977. $this->assertEquals($expected, $TestModel->data);
  978. $data = array();
  979. $data['Apple']['created']['hour'] = '20';
  980. $data['Apple']['created']['min'] = '33';
  981. $TestModel->data = null;
  982. $TestModel->set($data);
  983. $expected = array('Apple' => array('created' => ''));
  984. $this->assertEquals($expected, $TestModel->data);
  985. $data = array();
  986. $data['Apple']['created']['hour'] = '20';
  987. $data['Apple']['created']['min'] = '33';
  988. $data['Apple']['created']['sec'] = '33';
  989. $TestModel->data = null;
  990. $TestModel->set($data);
  991. $expected = array('Apple' => array('created' => ''));
  992. $this->assertEquals($expected, $TestModel->data);
  993. $data = array();
  994. $data['Apple']['created']['hour'] = '13';
  995. $data['Apple']['created']['min'] = '00';
  996. $data['Apple']['date']['year'] = '2006';
  997. $data['Apple']['date']['month'] = '12';
  998. $data['Apple']['date']['day'] = '25';
  999. $TestModel->data = null;
  1000. $TestModel->set($data);
  1001. $expected = array(
  1002. 'Apple' => array(
  1003. 'created' => '',
  1004. 'date' => '2006-12-25'
  1005. ));
  1006. $this->assertEquals($expected, $TestModel->data);
  1007. $data = array();
  1008. $data['Apple']['created']['year'] = '2007';
  1009. $data['Apple']['created']['month'] = '08';
  1010. $data['Apple']['created']['day'] = '20';
  1011. $data['Apple']['created']['hour'] = '10';
  1012. $data['Apple']['created']['min'] = '12';
  1013. $data['Apple']['created']['sec'] = '09';
  1014. $data['Apple']['date']['year'] = '2006';
  1015. $data['Apple']['date']['month'] = '12';
  1016. $data['Apple']['date']['day'] = '25';
  1017. $TestModel->data = null;
  1018. $TestModel->set($data);
  1019. $expected = array(
  1020. 'Apple' => array(
  1021. 'created' => '2007-08-20 10:12:09',
  1022. 'date' => '2006-12-25'
  1023. ));
  1024. $this->assertEquals($expected, $TestModel->data);
  1025. $data = array();
  1026. $data['Apple']['created']['year'] = '--';
  1027. $data['Apple']['created']['month'] = '--';
  1028. $data['Apple']['created']['day'] = '--';
  1029. $data['Apple']['created']['hour'] = '--';
  1030. $data['Apple']['created']['min'] = '--';
  1031. $data['Apple']['created']['sec'] = '--';
  1032. $data['Apple']['date']['year'] = '--';
  1033. $data['Apple']['date']['month'] = '--';
  1034. $data['Apple']['date']['day'] = '--';
  1035. $TestModel->data = null;
  1036. $TestModel->set($data);
  1037. $expected = array('Apple' => array('created' => '', 'date' => ''));
  1038. $this->assertEquals($expected, $TestModel->data);
  1039. $data = array();
  1040. $data['Apple']['created']['year'] = '2007';
  1041. $data['Apple']['created']['month'] = '--';
  1042. $data['Apple']['created']['day'] = '20';
  1043. $data['Apple']['created']['hour'] = '10';
  1044. $data['Apple']['created']['min'] = '12';
  1045. $data['Apple']['created']['sec'] = '09';
  1046. $data['Apple']['date']['year'] = '2006';
  1047. $data['Apple']['date']['month'] = '12';
  1048. $data['Apple']['date']['day'] = '25';
  1049. $TestModel->data = null;
  1050. $TestModel->set($data);
  1051. $expected = array('Apple' => array('created' => '', 'date' => '2006-12-25'));
  1052. $this->assertEquals($expected, $TestModel->data);
  1053. $data = array();
  1054. $data['Apple']['date']['year'] = '2006';
  1055. $data['Apple']['date']['month'] = '12';
  1056. $data['Apple']['date']['day'] = '25';
  1057. $TestModel->data = null;
  1058. $TestModel->set($data);
  1059. $expected = array('Apple' => array('date' => '2006-12-25'));
  1060. $this->assertEquals($expected, $TestModel->data);
  1061. $db = ConnectionManager::getDataSource('test');
  1062. $data = array();
  1063. $data['Apple']['modified'] = $db->expression('NOW()');
  1064. $TestModel->data = null;
  1065. $TestModel->set($data);
  1066. $this->assertEquals($TestModel->data, $data);
  1067. }
  1068. /**
  1069. * testTablePrefixSwitching method
  1070. *
  1071. * @return void
  1072. */
  1073. public function testTablePrefixSwitching() {
  1074. ConnectionManager::create('database1',
  1075. array_merge($this->db->config, array('prefix' => 'aaa_')
  1076. ));
  1077. ConnectionManager::create('database2',
  1078. array_merge($this->db->config, array('prefix' => 'bbb_')
  1079. ));
  1080. $db1 = ConnectionManager::getDataSource('database1');
  1081. $db2 = ConnectionManager::getDataSource('database2');
  1082. $TestModel = new Apple();
  1083. $TestModel->setDataSource('database1');
  1084. $this->assertContains('aaa_apples', $this->db->fullTableName($TestModel));
  1085. $this->assertContains('aaa_apples', $db1->fullTableName($TestModel));
  1086. $this->assertContains('aaa_apples', $db2->fullTableName($TestModel));
  1087. $TestModel->setDataSource('database2');
  1088. $this->assertContains('bbb_apples', $this->db->fullTableName($TestModel));
  1089. $this->assertContains('bbb_apples', $db1->fullTableName($TestModel));
  1090. $this->assertContains('bbb_apples', $db2->fullTableName($TestModel));
  1091. $TestModel = new Apple();
  1092. $TestModel->tablePrefix = 'custom_';
  1093. $this->assertContains('custom_apples', $this->db->fullTableName($TestModel));
  1094. $TestModel->setDataSource('database1');
  1095. $this->assertContains('custom_apples', $this->db->fullTableName($TestModel));
  1096. $this->assertContains('custom_apples', $db1->fullTableName($TestModel));
  1097. $TestModel = new Apple();
  1098. $TestModel->setDataSource('database1');
  1099. $this->assertContains('aaa_apples', $this->db->fullTableName($TestModel));
  1100. $TestModel->tablePrefix = '';
  1101. $TestModel->setDataSource('database2');
  1102. $this->assertContains('apples', $db2->fullTableName($TestModel));
  1103. $this->assertContains('apples', $db1->fullTableName($TestModel));
  1104. $TestModel->tablePrefix = null;
  1105. $TestModel->setDataSource('database1');
  1106. $this->assertContains('aaa_apples', $db2->fullTableName($TestModel));
  1107. $this->assertContains('aaa_apples', $db1->fullTableName($TestModel));
  1108. $TestModel->tablePrefix = false;
  1109. $TestModel->setDataSource('database2');
  1110. $this->assertContains('apples', $db2->fullTableName($TestModel));
  1111. $this->assertContains('apples', $db1->fullTableName($TestModel));
  1112. }
  1113. /**
  1114. * Tests validation parameter order in custom validation methods
  1115. *
  1116. * @return void
  1117. */
  1118. public function testInvalidAssociation() {
  1119. $TestModel = new ValidationTest1();
  1120. $this->assertNull($TestModel->getAssociated('Foo'));
  1121. }
  1122. /**
  1123. * testLoadModelSecondIteration method
  1124. *
  1125. * @return void
  1126. */
  1127. public function testLoadModelSecondIteration() {
  1128. $this->loadFixtures('Apple', 'Message', 'Thread', 'Bid');
  1129. $model = new ModelA();
  1130. $this->assertInstanceOf('ModelA', $model);
  1131. $this->assertInstanceOf('ModelB', $model->ModelB);
  1132. $this->assertInstanceOf('ModelD', $model->ModelB->ModelD);
  1133. $this->assertInstanceOf('ModelC', $model->ModelC);
  1134. $this->assertInstanceOf('ModelD', $model->ModelC->ModelD);
  1135. }
  1136. /**
  1137. * ensure that exists() does not persist between method calls reset on create
  1138. *
  1139. * @return void
  1140. */
  1141. public function testResetOfExistsOnCreate() {
  1142. $this->loadFixtures('Article');
  1143. $Article = new Article();
  1144. $Article->id = 1;
  1145. $Article->saveField('title', 'Reset me');
  1146. $Article->delete();
  1147. $Article->id = 1;
  1148. $this->assertFalse($Article->exists());
  1149. $Article->create();
  1150. $this->assertFalse($Article->exists());
  1151. $Article->id = 2;
  1152. $Article->saveField('title', 'Staying alive');
  1153. $result = $Article->read(null, 2);
  1154. $this->assertEquals('Staying alive', $result['Article']['title']);
  1155. }
  1156. /**
  1157. * testUseTableFalseExistsCheck method
  1158. *
  1159. * @return void
  1160. */
  1161. public function testUseTableFalseExistsCheck() {
  1162. $this->loadFixtures('Article');
  1163. $Article = new Article();
  1164. $Article->id = 1337;
  1165. $result = $Article->exists();
  1166. $this->assertFalse($result);
  1167. $Article->useTable = false;
  1168. $Article->id = null;
  1169. $result = $Article->exists();
  1170. $this->assertFalse($result);
  1171. // An article with primary key of '1' has been loaded by the fixtures.
  1172. $Article->useTable = false;
  1173. $Article->id = 1;
  1174. $result = $Article->exists();
  1175. $this->assertTrue($result);
  1176. }
  1177. /**
  1178. * testPluginAssociations method
  1179. *
  1180. * @return void
  1181. */
  1182. public function testPluginAssociations() {
  1183. $this->loadFixtures('TestPluginArticle', 'User', 'TestPluginComment');
  1184. $TestModel = new TestPluginArticle();
  1185. $result = $TestModel->find('all');
  1186. $expected = array(
  1187. array(
  1188. 'TestPluginArticle' => array(
  1189. 'id' => 1,
  1190. 'user_id' => 1,
  1191. 'title' => 'First Plugin Article',
  1192. 'body' => 'First Plugin Article Body',
  1193. 'published' => 'Y',
  1194. 'created' => '2008-09-24 10:39:23',
  1195. 'updated' => '2008-09-24 10:41:31'
  1196. ),
  1197. 'User' => array(
  1198. 'id' => 1,
  1199. 'user' => 'mariano',
  1200. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  1201. 'created' => '2007-03-17 01:16:23',
  1202. 'updated' => '2007-03-17 01:18:31'
  1203. ),
  1204. 'TestPluginComment' => array(
  1205. array(
  1206. 'id' => 1,
  1207. 'article_id' => 1,
  1208. 'user_id' => 2,
  1209. 'comment' => 'First Comment for First Plugin Article',
  1210. 'published' => 'Y',
  1211. 'created' => '2008-09-24 10:45:23',
  1212. 'updated' => '2008-09-24 10:47:31'
  1213. ),
  1214. array(
  1215. 'id' => 2,
  1216. 'article_id' => 1,
  1217. 'user_id' => 4,
  1218. 'comment' => 'Second Comment for First Plugin Article',
  1219. 'published' => 'Y',
  1220. 'created' => '2008-09-24 10:47:23',
  1221. 'updated' => '2008-09-24 10:49:31'
  1222. ),
  1223. array(
  1224. 'id' => 3,
  1225. 'article_id' => 1,
  1226. 'user_id' => 1,
  1227. 'comment' => 'Third Comment for First Plugin Article',
  1228. 'published' => 'Y',
  1229. 'created' => '2008-09-24 10:49:23',
  1230. 'updated' => '2008-09-24 10:51:31'
  1231. ),
  1232. array(
  1233. 'id' => 4,
  1234. 'article_id' => 1,
  1235. 'user_id' => 1,
  1236. 'comment' => 'Fourth Comment for First Plugin Article',
  1237. 'published' => 'N',
  1238. 'created' => '2008-09-24 10:51:23',
  1239. 'updated' => '2008-09-24 10:53:31'
  1240. ))),
  1241. array(
  1242. 'TestPluginArticle' => array(
  1243. 'id' => 2,
  1244. 'user_id' => 3,
  1245. 'title' => 'Second Plugin Article',
  1246. 'body' => 'Second Plugin Article Body',
  1247. 'published' => 'Y',
  1248. 'created' => '2008-09-24 10:41:23',
  1249. 'updated' => '2008-09-24 10:43:31'
  1250. ),
  1251. 'User' => array(
  1252. 'id' => 3,
  1253. 'user' => 'larry',
  1254. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  1255. 'created' => '2007-03-17 01:20:23',
  1256. 'updated' => '2007-03-17 01:22:31'
  1257. ),
  1258. 'TestPluginComment' => array(
  1259. array(
  1260. 'id' => 5,
  1261. 'article_id' => 2,
  1262. 'user_id' => 1,
  1263. 'comment' => 'First Comment for Second Plugin Article',
  1264. 'published' => 'Y',
  1265. 'created' => '2008-09-24 10:53:23',
  1266. 'updated' => '2008-09-24 10:55:31'
  1267. ),
  1268. array(
  1269. 'id' => 6,
  1270. 'article_id' => 2,
  1271. 'user_id' => 2,
  1272. 'comment' => 'Second Comment for Second Plugin Article',
  1273. 'published' => 'Y',
  1274. 'created' => '2008-09-24 10:55:23',
  1275. 'updated' => '2008-09-24 10:57:31'
  1276. ))),
  1277. array(
  1278. 'TestPluginArticle' => array(
  1279. 'id' => 3,
  1280. 'user_id' => 1,
  1281. 'title' => 'Third Plugin Article',
  1282. 'body' => 'Third Plugin Article Body',
  1283. 'published' => 'Y',
  1284. 'created' => '2008-09-24 10:43:23',
  1285. 'updated' => '2008-09-24 10:45:31'
  1286. ),
  1287. 'User' => array(
  1288. 'id' => 1,
  1289. 'user' => 'mariano',
  1290. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  1291. 'created' => '2007-03-17 01:16:23',
  1292. 'updated' => '2007-03-17 01:18:31'
  1293. ),
  1294. 'TestPluginComment' => array()
  1295. ));
  1296. $this->assertEquals($expected, $result);
  1297. }
  1298. /**
  1299. * Tests getAssociated method
  1300. *
  1301. * @return void
  1302. */
  1303. public function testGetAssociated() {
  1304. $this->loadFixtures('Article', 'Tag');
  1305. $Article = ClassRegistry::init('Article');
  1306. $assocTypes = array('hasMany', 'hasOne', 'belongsTo', 'hasAndBelongsToMany');
  1307. foreach ($assocTypes as $type) {
  1308. $this->assertEquals($Article->getAssociated($type), array_keys($Article->{$type}));
  1309. }
  1310. $Article->bindModel(array('hasMany' => array('Category')));
  1311. $this->assertEquals(array('Comment', 'Category'), $Article->getAssociated('hasMany'));
  1312. $results = $Article->getAssociated();
  1313. $results = array_keys($results);
  1314. sort($results);
  1315. $this->assertEquals(array('Category', 'Comment', 'Tag', 'User'), $results);
  1316. $Article->unbindModel(array('hasAndBelongsToMany' => array('Tag')));
  1317. $this->assertEquals(array(), $Article->getAssociated('hasAndBelongsToMany'));
  1318. $result = $Article->getAssociated('Category');
  1319. $expected = array(
  1320. 'className' => 'Category',
  1321. 'foreignKey' => 'article_id',
  1322. 'conditions' => '',
  1323. 'fields' => '',
  1324. 'order' => '',
  1325. 'limit' => '',
  1326. 'offset' => '',
  1327. 'dependent' => '',
  1328. 'exclusive' => '',
  1329. 'finderQuery' => '',
  1330. 'counterQuery' => '',
  1331. 'association' => 'hasMany',
  1332. );
  1333. $this->assertEquals($expected, $result);
  1334. }
  1335. /**
  1336. * testAutoConstructAssociations method
  1337. *
  1338. * @return void
  1339. */
  1340. public function testAutoConstructAssociations() {
  1341. $this->loadFixtures('User', 'ArticleFeatured', 'Featured', 'ArticleFeaturedsTags');
  1342. $TestModel = new AssociationTest1();
  1343. $result = $TestModel->hasAndBelongsToMany;
  1344. $expected = array('AssociationTest2' => array(
  1345. 'unique' => false,
  1346. 'joinTable' => 'join_as_join_bs',
  1347. 'foreignKey' => false,
  1348. 'className' => 'AssociationTest2',
  1349. 'with' => 'JoinAsJoinB',
  1350. 'dynamicWith' => true,
  1351. 'associationForeignKey' => 'join_b_id',
  1352. 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '',
  1353. 'finderQuery' => '', 'deleteQuery' => '', 'insertQuery' => ''
  1354. ));
  1355. $this->assertEquals($expected, $result);
  1356. $TestModel = new ArticleFeatured();
  1357. $TestFakeModel = new ArticleFeatured(array('table' => false));
  1358. $expected = array(
  1359. 'User' => array(
  1360. 'className' => 'User', 'foreignKey' => 'user_id',
  1361. 'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => ''
  1362. ),
  1363. 'Category' => array(
  1364. 'className' => 'Category', 'foreignKey' => 'category_id',
  1365. 'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => ''
  1366. )
  1367. );
  1368. $this->assertSame($TestModel->belongsTo, $expected);
  1369. $this->assertSame($TestFakeModel->belongsTo, $expected);
  1370. $this->assertEquals('User', $TestModel->User->name);
  1371. $this->assertEquals('User', $TestFakeModel->User->name);
  1372. $this->assertEquals('Category', $TestModel->Category->name);
  1373. $this->assertEquals('Category', $TestFakeModel->Category->name);
  1374. $expected = array(
  1375. 'Featured' => array(
  1376. 'className' => 'Featured',
  1377. 'foreignKey' => 'article_featured_id',
  1378. 'conditions' => '',
  1379. 'fields' => '',
  1380. 'order' => '',
  1381. 'dependent' => ''
  1382. ));
  1383. $this->assertSame($TestModel->hasOne, $expected);
  1384. $this->assertSame($TestFakeModel->hasOne, $expected);
  1385. $this->assertEquals('Featured', $TestModel->Featured->name);
  1386. $this->assertEquals('Featured', $TestFakeModel->Featured->name);
  1387. $expected = array(
  1388. 'Comment' => array(
  1389. 'className' => 'Comment',
  1390. 'dependent' => true,
  1391. 'foreignKey' => 'article_featured_id',
  1392. 'conditions' => '',
  1393. 'fields' => '',
  1394. 'order' => '',
  1395. 'limit' => '',
  1396. 'offset' => '',
  1397. 'exclusive' => '',
  1398. 'finderQuery' => '',
  1399. 'counterQuery' => ''
  1400. ));
  1401. $this->assertSame($TestModel->hasMany, $expected);
  1402. $this->assertSame($TestFakeModel->hasMany, $expected);
  1403. $this->assertEquals('Comment', $TestModel->Comment->name);
  1404. $this->assertEquals('Comment', $TestFakeModel->Comment->name);
  1405. $expected = array(
  1406. 'Tag' => array(
  1407. 'className' => 'Tag',
  1408. 'joinTable' => 'article_featureds_tags',
  1409. 'with' => 'ArticleFeaturedsTag',
  1410. 'dynamicWith' => true,
  1411. 'foreignKey' => 'article_featured_id',
  1412. 'associationForeignKey' => 'tag_id',
  1413. 'conditions' => '',
  1414. 'fields' => '',
  1415. 'order' => '',
  1416. 'limit' => '',
  1417. 'offset' => '',
  1418. 'unique' => true,
  1419. 'finderQuery' => '',
  1420. 'deleteQuery' => '',
  1421. 'insertQuery' => ''
  1422. ));
  1423. $this->assertSame($TestModel->hasAndBelongsToMany, $expected);
  1424. $this->assertSame($TestFakeModel->hasAndBelongsToMany, $expected);
  1425. $this->assertEquals('Tag', $TestModel->Tag->name);
  1426. $this->assertEquals('Tag', $TestFakeModel->Tag->name);
  1427. }
  1428. /**
  1429. * test creating associations with plugins. Ensure a double alias isn't created
  1430. *
  1431. * @return void
  1432. */
  1433. public function testAutoConstructPluginAssociations() {
  1434. $Comment = ClassRegistry::init('TestPluginComment');
  1435. $this->assertEquals(2, count($Comment->belongsTo), 'Too many associations');
  1436. $this->assertFalse(isset($Comment->belongsTo['TestPlugin.User']));
  1437. $this->assertTrue(isset($Comment->belongsTo['User']), 'Missing association');
  1438. $this->assertTrue(isset($Comment->belongsTo['TestPluginArticle']), 'Missing association');
  1439. }
  1440. /**
  1441. * test Model::__construct
  1442. *
  1443. * ensure that $actsAS and $findMethods are merged.
  1444. *
  1445. * @return void
  1446. */
  1447. public function testConstruct() {
  1448. $this->loadFixtures('Post');
  1449. $TestModel = ClassRegistry::init('MergeVarPluginPost');
  1450. $this->assertEquals(array('Containable' => null, 'Tree' => null), $TestModel->actsAs);
  1451. $this->assertTrue(isset($TestModel->Behaviors->Containable));
  1452. $this->assertTrue(isset($TestModel->Behaviors->Tree));
  1453. $TestModel = ClassRegistry::init('MergeVarPluginComment');
  1454. $expected = array('Containable' => array('some_settings'));
  1455. $this->assertEquals($expected, $TestModel->actsAs);
  1456. $this->assertTrue(isset($TestModel->Behaviors->Containable));
  1457. }
  1458. /**
  1459. * test Model::__construct
  1460. *
  1461. * ensure that $actsAS and $findMethods are merged.
  1462. *
  1463. * @return void
  1464. */
  1465. public function testConstructWithAlternateDataSource() {
  1466. $TestModel = ClassRegistry::init(array(
  1467. 'class' => 'DoesntMatter', 'ds' => 'test', 'table' => false
  1468. ));
  1469. $this->assertEquals('test', $TestModel->useDbConfig);
  1470. //deprecated but test it anyway
  1471. $NewVoid = new TheVoid(null, false, 'other');
  1472. $this->assertEquals('other', $NewVoid->useDbConfig);
  1473. }
  1474. /**
  1475. * testColumnTypeFetching method
  1476. *
  1477. * @return void
  1478. */
  1479. public function testColumnTypeFetching() {
  1480. $model = new Test();
  1481. $this->assertEquals('integer', $model->getColumnType('id'));
  1482. $this->assertEquals('text', $model->getColumnType('notes'));
  1483. $this->assertEquals('datetime', $model->getColumnType('updated'));
  1484. $this->assertEquals(null, $model->getColumnType('unknown'));
  1485. $model = new Article();
  1486. $this->assertEquals('datetime', $model->getColumnType('User.created'));
  1487. $this->assertEquals('integer', $model->getColumnType('Tag.id'));
  1488. $this->assertEquals('integer', $model->getColumnType('Article.id'));
  1489. }
  1490. /**
  1491. * testHabtmUniqueKey method
  1492. *
  1493. * @return void
  1494. */
  1495. public function testHabtmUniqueKey() {
  1496. $model = new Item();
  1497. $this->assertFalse($model->hasAndBelongsToMany['Portfolio']['unique']);
  1498. }
  1499. /**
  1500. * testIdentity method
  1501. *
  1502. * @return void
  1503. */
  1504. public function testIdentity() {
  1505. $TestModel = new Test();
  1506. $result = $TestModel->alias;
  1507. $expected = 'Test';
  1508. $this->assertEquals($expected, $result);
  1509. $TestModel = new TestAlias();
  1510. $result = $TestModel->alias;
  1511. $expected = 'TestAlias';
  1512. $this->assertEquals($expected, $result);
  1513. $TestModel = new Test(array('alias' => 'AnotherTest'));
  1514. $result = $TestModel->alias;
  1515. $expected = 'AnotherTest';
  1516. $this->assertEquals($expected, $result);
  1517. $TestModel = ClassRegistry::init('Test');
  1518. $expected = null;
  1519. $this->assertEquals($expected, $TestModel->plugin);
  1520. $TestModel = ClassRegistry::init('TestPlugin.TestPluginComment');
  1521. $expected = 'TestPlugin';
  1522. $this->assertEquals($expected, $TestModel->plugin);
  1523. }
  1524. /**
  1525. * testWithAssociation method
  1526. *
  1527. * @return void
  1528. */
  1529. public function testWithAssociation() {
  1530. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  1531. $TestModel = new Something();
  1532. $result = $TestModel->SomethingElse->find('all');
  1533. $expected = array(
  1534. array(
  1535. 'SomethingElse' => array(
  1536. 'id' => '1',
  1537. 'title' => 'First Post',
  1538. 'body' => 'First Post Body',
  1539. 'published' => 'Y',
  1540. 'created' => '2007-03-18 10:39:23',
  1541. 'updated' => '2007-03-18 10:41:31'
  1542. ),
  1543. 'Something' => array(
  1544. array(
  1545. 'id' => '3',
  1546. 'title' => 'Third Post',
  1547. 'body' => 'Third Post Body',
  1548. 'published' => 'Y',
  1549. 'created' => '2007-03-18 10:43:23',
  1550. 'updated' => '2007-03-18 10:45:31',
  1551. 'JoinThing' => array(
  1552. 'id' => '3',
  1553. 'something_id' => '3',
  1554. 'something_else_id' => '1',
  1555. 'doomed' => true,
  1556. 'created' => '2007-03-18 10:43:23',
  1557. 'updated' => '2007-03-18 10:45:31'
  1558. )))),
  1559. array(
  1560. 'SomethingElse' => array(
  1561. 'id' => '2',
  1562. 'title' => 'Second Post',
  1563. 'body' => 'Second Post Body',
  1564. 'published' => 'Y',
  1565. 'created' => '2007-03-18 10:41:23',
  1566. 'updated' => '2007-03-18 10:43:31'
  1567. ),
  1568. 'Something' => array(
  1569. array(
  1570. 'id' => '1',
  1571. 'title' => 'First Post',
  1572. 'body' => 'First Post Body',
  1573. 'published' => 'Y',
  1574. 'created' => '2007-03-18 10:39:23',
  1575. 'updated' => '2007-03-18 10:41:31',
  1576. 'JoinThing' => array(
  1577. 'id' => '1',
  1578. 'something_id' => '1',
  1579. 'something_else_id' => '2',
  1580. 'doomed' => true,
  1581. 'created' => '2007-03-18 10:39:23',
  1582. 'updated' => '2007-03-18 10:41:31'
  1583. )))),
  1584. array(
  1585. 'SomethingElse' => array(
  1586. 'id' => '3',
  1587. 'title' => 'Third Post',
  1588. 'body' => 'Third Post Body',
  1589. 'published' => 'Y',
  1590. 'created' => '2007-03-18 10:43:23',
  1591. 'updated' => '2007-03-18 10:45:31'
  1592. ),
  1593. 'Something' => array(
  1594. array(
  1595. 'id' => '2',
  1596. 'title' => 'Second Post',
  1597. 'body' => 'Second Post Body',
  1598. 'published' => 'Y',
  1599. 'created' => '2007-03-18 10:41:23',
  1600. 'updated' => '2007-03-18 10:43:31',
  1601. 'JoinThing' => array(
  1602. 'id' => '2',
  1603. 'something_id' => '2',
  1604. 'something_else_id' => '3',
  1605. 'doomed' => false,
  1606. 'created' => '2007-03-18 10:41:23',
  1607. 'updated' => '2007-03-18 10:43:31'
  1608. )))));
  1609. $this->assertEquals($expected, $result);
  1610. $result = $TestModel->find('all');
  1611. $expected = array(
  1612. array(
  1613. 'Something' => array(
  1614. 'id' => '1',
  1615. 'title' => 'First Post',
  1616. 'body' => 'First Post Body',
  1617. 'published' => 'Y',
  1618. 'created' => '2007-03-18 10:39:23',
  1619. 'updated' => '2007-03-18 10:41:31'
  1620. ),
  1621. 'SomethingElse' => array(
  1622. array(
  1623. 'id' => '2',
  1624. 'title' => 'Second Post',
  1625. 'body' => 'Second Post Body',
  1626. 'published' => 'Y',
  1627. 'created' => '2007-03-18 10:41:23',
  1628. 'updated' => '2007-03-18 10:43:31',
  1629. 'JoinThing' => array(
  1630. 'doomed' => true,
  1631. 'something_id' => '1',
  1632. 'something_else_id' => '2'
  1633. )))),
  1634. array(
  1635. 'Something' => array(
  1636. 'id' => '2',
  1637. 'title' => 'Second Post',
  1638. 'body' => 'Second Post Body',
  1639. 'published' => 'Y',
  1640. 'created' => '2007-03-18 10:41:23',
  1641. 'updated' => '2007-03-18 10:43:31'
  1642. ),
  1643. 'SomethingElse' => array(
  1644. array(
  1645. 'id' => '3',
  1646. 'title' => 'Third Post',
  1647. 'body' => 'Third Post Body',
  1648. 'published' => 'Y',
  1649. 'created' => '2007-03-18 10:43:23',
  1650. 'updated' => '2007-03-18 10:45:31',
  1651. 'JoinThing' => array(
  1652. 'doomed' => false,
  1653. 'something_id' => '2',
  1654. 'something_else_id' => '3'
  1655. )))),
  1656. array(
  1657. 'Something' => array(
  1658. 'id' => '3',
  1659. 'title' => 'Third Post',
  1660. 'body' => 'Third Post Body',
  1661. 'published' => 'Y',
  1662. 'created' => '2007-03-18 10:43:23',
  1663. 'updated' => '2007-03-18 10:45:31'
  1664. ),
  1665. 'SomethingElse' => array(
  1666. array(
  1667. 'id' => '1',
  1668. 'title' => 'First Post',
  1669. 'body' => 'First Post Body',
  1670. 'published' => 'Y',
  1671. 'created' => '2007-03-18 10:39:23',
  1672. 'updated' => '2007-03-18 10:41:31',
  1673. 'JoinThing' => array(
  1674. 'doomed' => true,
  1675. 'something_id' => '3',
  1676. 'something_else_id' => '1'
  1677. )))));
  1678. $this->assertEquals($expected, $result);
  1679. $result = $TestModel->findById(1);
  1680. $expected = array(
  1681. 'Something' => array(
  1682. 'id' => '1',
  1683. 'title' => 'First Post',
  1684. 'body' => 'First Post Body',
  1685. 'published' => 'Y',
  1686. 'created' => '2007-03-18 10:39:23',
  1687. 'updated' => '2007-03-18 10:41:31'
  1688. ),
  1689. 'SomethingElse' => array(
  1690. array(
  1691. 'id' => '2',
  1692. 'title' => 'Second Post',
  1693. 'body' => 'Second Post Body',
  1694. 'published' => 'Y',
  1695. 'created' => '2007-03-18 10:41:23',
  1696. 'updated' => '2007-03-18 10:43:31',
  1697. 'JoinThing' => array(
  1698. 'doomed' => true,
  1699. 'something_id' => '1',
  1700. 'something_else_id' => '2'
  1701. ))));
  1702. $this->assertEquals($expected, $result);
  1703. $expected = $TestModel->findById(1);
  1704. $TestModel->set($expected);
  1705. $TestModel->save();
  1706. $result = $TestModel->findById(1);
  1707. $this->assertEquals($expected, $result);
  1708. $TestModel->hasAndBelongsToMany['SomethingElse']['unique'] = false;
  1709. $TestModel->create(array(
  1710. 'Something' => array('id' => 1),
  1711. 'SomethingElse' => array(3, array(
  1712. 'something_else_id' => 1,
  1713. 'doomed' => true
  1714. ))));
  1715. $TestModel->save();
  1716. $TestModel->hasAndBelongsToMany['SomethingElse']['order'] = 'SomethingElse.id ASC';
  1717. $result = $TestModel->findById(1);
  1718. $expected = array(
  1719. 'Something' => array(
  1720. 'id' => '1',
  1721. 'title' => 'First Post',
  1722. 'body' => 'First Post Body',
  1723. 'published' => 'Y',
  1724. 'created' => '2007-03-18 10:39:23'
  1725. ),
  1726. 'SomethingElse' => array(
  1727. array(
  1728. 'id' => '1',
  1729. 'title' => 'First Post',
  1730. 'body' => 'First Post Body',
  1731. 'published' => 'Y',
  1732. 'created' => '2007-03-18 10:39:23',
  1733. 'updated' => '2007-03-18 10:41:31',
  1734. 'JoinThing' => array(
  1735. 'doomed' => true,
  1736. 'something_id' => '1',
  1737. 'something_else_id' => '1'
  1738. )
  1739. ),
  1740. array(
  1741. 'id' => '2',
  1742. 'title' => 'Second Post',
  1743. 'body' => 'Second Post Body',
  1744. 'published' => 'Y',
  1745. 'created' => '2007-03-18 10:41:23',
  1746. 'updated' => '2007-03-18 10:43:31',
  1747. 'JoinThing' => array(
  1748. 'doomed' => true,
  1749. 'something_id' => '1',
  1750. 'something_else_id' => '2'
  1751. )
  1752. ),
  1753. array(
  1754. 'id' => '3',
  1755. 'title' => 'Third Post',
  1756. 'body' => 'Third Post Body',
  1757. 'published' => 'Y',
  1758. 'created' => '2007-03-18 10:43:23',
  1759. 'updated' => '2007-03-18 10:45:31',
  1760. 'JoinThing' => array(
  1761. 'doomed' => false,
  1762. 'something_id' => '1',
  1763. 'something_else_id' => '3')
  1764. )
  1765. )
  1766. );
  1767. $this->assertEquals(self::date(), $result['Something']['updated']);
  1768. unset($result['Something']['updated']);
  1769. $this->assertEquals($expected, $result);
  1770. }
  1771. /**
  1772. * testFindSelfAssociations method
  1773. *
  1774. * @return void
  1775. */
  1776. public function testFindSelfAssociations() {
  1777. $this->loadFixtures('Person');
  1778. $TestModel = new Person();
  1779. $TestModel->recursive = 2;
  1780. $result = $TestModel->read(null, 1);
  1781. $expected = array(
  1782. 'Person' => array(
  1783. 'id' => 1,
  1784. 'name' => 'person',
  1785. 'mother_id' => 2,
  1786. 'father_id' => 3
  1787. ),
  1788. 'Mother' => array(
  1789. 'id' => 2,
  1790. 'name' => 'mother',
  1791. 'mother_id' => 4,
  1792. 'father_id' => 5,
  1793. 'Mother' => array(
  1794. 'id' => 4,
  1795. 'name' => 'mother - grand mother',
  1796. 'mother_id' => 0,
  1797. 'father_id' => 0
  1798. ),
  1799. 'Father' => array(
  1800. 'id' => 5,
  1801. 'name' => 'mother - grand father',
  1802. 'mother_id' => 0,
  1803. 'father_id' => 0
  1804. )),
  1805. 'Father' => array(
  1806. 'id' => 3,
  1807. 'name' => 'father',
  1808. 'mother_id' => 6,
  1809. 'father_id' => 7,
  1810. 'Father' => array(
  1811. 'id' => 7,
  1812. 'name' => 'father - grand father',
  1813. 'mother_id' => 0,
  1814. 'father_id' => 0
  1815. ),
  1816. 'Mother' => array(
  1817. 'id' => 6,
  1818. 'name' => 'father - grand mother',
  1819. 'mother_id' => 0,
  1820. 'father_id' => 0
  1821. )));
  1822. $this->assertEquals($expected, $result);
  1823. $TestModel->recursive = 3;
  1824. $result = $TestModel->read(null, 1);
  1825. $expected = array(
  1826. 'Person' => array(
  1827. 'id' => 1,
  1828. 'name' => 'person',
  1829. 'mother_id' => 2,
  1830. 'father_id' => 3
  1831. ),
  1832. 'Mother' => array(
  1833. 'id' => 2,
  1834. 'name' => 'mother',
  1835. 'mother_id' => 4,
  1836. 'father_id' => 5,
  1837. 'Mother' => array(
  1838. 'id' => 4,
  1839. 'name' => 'mother - grand mother',
  1840. 'mother_id' => 0,
  1841. 'father_id' => 0,
  1842. 'Mother' => array(),
  1843. 'Father' => array()),
  1844. 'Father' => array(
  1845. 'id' => 5,
  1846. 'name' => 'mother - grand father',
  1847. 'mother_id' => 0,
  1848. 'father_id' => 0,
  1849. 'Father' => array(),
  1850. 'Mother' => array()
  1851. )),
  1852. 'Father' => array(
  1853. 'id' => 3,
  1854. 'name' => 'father',
  1855. 'mother_id' => 6,
  1856. 'father_id' => 7,
  1857. 'Father' => array(
  1858. 'id' => 7,
  1859. 'name' => 'father - grand father',
  1860. 'mother_id' => 0,
  1861. 'father_id' => 0,
  1862. 'Father' => array(),
  1863. 'Mother' => array()
  1864. ),
  1865. 'Mother' => array(
  1866. 'id' => 6,
  1867. 'name' => 'father - grand mother',
  1868. 'mother_id' => 0,
  1869. 'father_id' => 0,
  1870. 'Mother' => array(),
  1871. 'Father' => array()
  1872. )));
  1873. $this->assertEquals($expected, $result);
  1874. }
  1875. /**
  1876. * testDynamicAssociations method
  1877. *
  1878. * @return void
  1879. */
  1880. public function testDynamicAssociations() {
  1881. $this->loadFixtures('Article', 'Comment');
  1882. $TestModel = new Article();
  1883. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->hasOne = array();
  1884. $TestModel->hasMany['Comment'] = array_merge($TestModel->hasMany['Comment'], array(
  1885. 'foreignKey' => false,
  1886. 'conditions' => array('Comment.user_id =' => '2')
  1887. ));
  1888. $result = $TestModel->find('all');
  1889. $expected = array(
  1890. array(
  1891. 'Article' => array(
  1892. 'id' => '1',
  1893. 'user_id' => '1',
  1894. 'title' => 'First Article',
  1895. 'body' => 'First Article Body',
  1896. 'published' => 'Y',
  1897. 'created' => '2007-03-18 10:39:23',
  1898. 'updated' => '2007-03-18 10:41:31'
  1899. ),
  1900. 'Comment' => array(
  1901. array(
  1902. 'id' => '1',
  1903. 'article_id' => '1',
  1904. 'user_id' => '2',
  1905. 'comment' => 'First Comment for First Article',
  1906. 'published' => 'Y',
  1907. 'created' => '2007-03-18 10:45:23',
  1908. 'updated' => '2007-03-18 10:47:31'
  1909. ),
  1910. array(
  1911. 'id' => '6',
  1912. 'article_id' => '2',
  1913. 'user_id' => '2',
  1914. 'comment' => 'Second Comment for Second Article',
  1915. 'published' => 'Y',
  1916. 'created' => '2007-03-18 10:55:23',
  1917. 'updated' => '2007-03-18 10:57:31'
  1918. ))),
  1919. array(
  1920. 'Article' => array(
  1921. 'id' => '2',
  1922. 'user_id' => '3',
  1923. 'title' => 'Second Article',
  1924. 'body' => 'Second Article Body',
  1925. 'published' => 'Y',
  1926. 'created' => '2007-03-18 10:41:23',
  1927. 'updated' => '2007-03-18 10:43:31'
  1928. ),
  1929. 'Comment' => array(
  1930. array(
  1931. 'id' => '1',
  1932. 'article_id' => '1',
  1933. 'user_id' => '2',
  1934. 'comment' => 'First Comment for First Article',
  1935. 'published' => 'Y',
  1936. 'created' => '2007-03-18 10:45:23',
  1937. 'updated' => '2007-03-18 10:47:31'
  1938. ),
  1939. array(
  1940. 'id' => '6',
  1941. 'article_id' => '2',
  1942. 'user_id' => '2',
  1943. 'comment' => 'Second Comment for Second Article',
  1944. 'published' => 'Y',
  1945. 'created' => '2007-03-18 10:55:23',
  1946. 'updated' => '2007-03-18 10:57:31'
  1947. ))),
  1948. array(
  1949. 'Article' => array(
  1950. 'id' => '3',
  1951. 'user_id' => '1',
  1952. 'title' => 'Third Article',
  1953. 'body' => 'Third Article Body',
  1954. 'published' => 'Y',
  1955. 'created' => '2007-03-18 10:43:23',
  1956. 'updated' => '2007-03-18 10:45:31'
  1957. ),
  1958. 'Comment' => array(
  1959. array(
  1960. 'id' => '1',
  1961. 'article_id' => '1',
  1962. 'user_id' => '2',
  1963. 'comment' => 'First Comment for First Article',
  1964. 'published' => 'Y',
  1965. 'created' => '2007-03-18 10:45:23',
  1966. 'updated' => '2007-03-18 10:47:31'
  1967. ),
  1968. array(
  1969. 'id' => '6',
  1970. 'article_id' => '2',
  1971. 'user_id' => '2',
  1972. 'comment' => 'Second Comment for Second Article',
  1973. 'published' => 'Y',
  1974. 'created' => '2007-03-18 10:55:23',
  1975. 'updated' => '2007-03-18 10:57:31'
  1976. ))));
  1977. $this->assertEquals($expected, $result);
  1978. }
  1979. /**
  1980. * testCreation method
  1981. *
  1982. * @return void
  1983. */
  1984. public function testCreation() {
  1985. $this->loadFixtures('Article', 'ArticleFeaturedsTags', 'User', 'Featured');
  1986. $TestModel = new Test();
  1987. $result = $TestModel->create();
  1988. $expected = array('Test' => array('notes' => 'write some notes here'));
  1989. $this->assertEquals($expected, $result);
  1990. $TestModel = new User();
  1991. $result = $TestModel->schema();
  1992. if (isset($this->db->columns['primary_key']['length'])) {
  1993. $intLength = $this->db->columns['primary_key']['length'];
  1994. } elseif (isset($this->db->columns['integer']['length'])) {
  1995. $intLength = $this->db->columns['integer']['length'];
  1996. } else {
  1997. $intLength = 11;
  1998. }
  1999. foreach (array('collate', 'charset', 'comment') as $type) {
  2000. foreach ($result as $i => $r) {
  2001. unset($result[$i][$type]);
  2002. }
  2003. }
  2004. $expected = array(
  2005. 'id' => array(
  2006. 'type' => 'integer',
  2007. 'null' => false,
  2008. 'default' => null,
  2009. 'length' => $intLength,
  2010. 'key' => 'primary'
  2011. ),
  2012. 'user' => array(
  2013. 'type' => 'string',
  2014. 'null' => true,
  2015. 'default' => '',
  2016. 'length' => 255
  2017. ),
  2018. 'password' => array(
  2019. 'type' => 'string',
  2020. 'null' => true,
  2021. 'default' => '',
  2022. 'length' => 255
  2023. ),
  2024. 'created' => array(
  2025. 'type' => 'datetime',
  2026. 'null' => true,
  2027. 'default' => null,
  2028. 'length' => null
  2029. ),
  2030. 'updated' => array(
  2031. 'type' => 'datetime',
  2032. 'null' => true,
  2033. 'default' => null,
  2034. 'length' => null
  2035. ));
  2036. $this->assertEquals($expected, $result);
  2037. $TestModel = new Article();
  2038. $result = $TestModel->create();
  2039. $expected = array('Article' => array('published' => 'N'));
  2040. $this->assertEquals($expected, $result);
  2041. $FeaturedModel = new Featured();
  2042. $data = array(
  2043. 'article_featured_id' => 1,
  2044. 'category_id' => 1,
  2045. 'published_date' => array(
  2046. 'year' => 2008,
  2047. 'month' => 06,
  2048. 'day' => 11
  2049. ),
  2050. 'end_date' => array(
  2051. 'year' => 2008,
  2052. 'month' => 06,
  2053. 'day' => 20
  2054. ));
  2055. $expected = array(
  2056. 'Featured' => array(
  2057. 'article_featured_id' => 1,
  2058. 'category_id' => 1,
  2059. 'published_date' => '2008-06-11 00:00:00',
  2060. 'end_date' => '2008-06-20 00:00:00'
  2061. ));
  2062. $this->assertEquals($expected, $FeaturedModel->create($data));
  2063. $data = array(
  2064. 'published_date' => array(
  2065. 'year' => 2008,
  2066. 'month' => 06,
  2067. 'day' => 11
  2068. ),
  2069. 'end_date' => array(
  2070. 'year' => 2008,
  2071. 'month' => 06,
  2072. 'day' => 20
  2073. ),
  2074. 'article_featured_id' => 1,
  2075. 'category_id' => 1
  2076. );
  2077. $expected = array(
  2078. 'Featured' => array(
  2079. 'published_date' => '2008-06-11 00:00:00',
  2080. 'end_date' => '2008-06-20 00:00:00',
  2081. 'article_featured_id' => 1,
  2082. 'category_id' => 1
  2083. ));
  2084. $this->assertEquals($expected, $FeaturedModel->create($data));
  2085. }
  2086. /**
  2087. * testEscapeField to prove it escapes the field well even when it has part of the alias on it
  2088. *
  2089. * @return void
  2090. */
  2091. public function testEscapeField() {
  2092. $TestModel = new Test();
  2093. $db = $TestModel->getDataSource();
  2094. $result = $TestModel->escapeField('test_field');
  2095. $expected = $db->name('Test.test_field');
  2096. $this->assertEquals($expected, $result);
  2097. $result = $TestModel->escapeField('TestField');
  2098. $expected = $db->name('Test.TestField');
  2099. $this->assertEquals($expected, $result);
  2100. $result = $TestModel->escapeField('DomainHandle', 'Domain');
  2101. $expected = $db->name('Domain.DomainHandle');
  2102. $this->assertEquals($expected, $result);
  2103. ConnectionManager::create('mock', array('datasource' => 'DboMock'));
  2104. $TestModel->setDataSource('mock');
  2105. $db = $TestModel->getDataSource();
  2106. $result = $TestModel->escapeField('DomainHandle', 'Domain');
  2107. $expected = $db->name('Domain.DomainHandle');
  2108. $this->assertEquals($expected, $result);
  2109. ConnectionManager::drop('mock');
  2110. }
  2111. /**
  2112. * testGetID
  2113. *
  2114. * @return void
  2115. */
  2116. public function testGetID() {
  2117. $TestModel = new Test();
  2118. $result = $TestModel->getID();
  2119. $this->assertFalse($result);
  2120. $TestModel->id = 9;
  2121. $result = $TestModel->getID();
  2122. $this->assertEquals(9, $result);
  2123. $TestModel->id = array(10, 9, 8, 7);
  2124. $result = $TestModel->getID(2);
  2125. $this->assertEquals(8, $result);
  2126. $TestModel->id = array(array(), 1, 2, 3);
  2127. $result = $TestModel->getID();
  2128. $this->assertFalse($result);
  2129. }
  2130. /**
  2131. * test that model->hasMethod checks self and behaviors.
  2132. *
  2133. * @return void
  2134. */
  2135. public function testHasMethod() {
  2136. $Article = new Article();
  2137. $Article->Behaviors = $this->getMock('BehaviorCollection');
  2138. $Article->Behaviors->expects($this->at(0))
  2139. ->method('hasMethod')
  2140. ->will($this->returnValue(true));
  2141. $Article->Behaviors->expects($this->at(1))
  2142. ->method('hasMethod')
  2143. ->will($this->returnValue(false));
  2144. $this->assertTrue($Article->hasMethod('find'));
  2145. $this->assertTrue($Article->hasMethod('pass'));
  2146. $this->assertFalse($Article->hasMethod('fail'));
  2147. }
  2148. /**
  2149. * testMultischemaFixture
  2150. *
  2151. * @return void
  2152. */
  2153. public function testMultischemaFixture() {
  2154. $config = ConnectionManager::enumConnectionObjects();
  2155. $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with Sqlite.');
  2156. $this->skipIf(!isset($config['test']) || !isset($config['test2']),
  2157. 'Primary and secondary test databases not configured, skipping cross-database join tests. To run these tests define $test and $test2 in your database configuration.'
  2158. );
  2159. $this->loadFixtures('Player', 'Guild', 'GuildsPlayer');
  2160. $Player = ClassRegistry::init('Player');
  2161. $this->assertEquals('test', $Player->useDbConfig);
  2162. $this->assertEquals('test', $Player->Guild->useDbConfig);
  2163. $this->assertEquals('test2', $Player->Guild->GuildsPlayer->useDbConfig);
  2164. $this->assertEquals('test2', $Player->GuildsPlayer->useDbConfig);
  2165. $players = $Player->find('all', array('recursive' => -1));
  2166. $guilds = $Player->Guild->find('all', array('recursive' => -1));
  2167. $guildsPlayers = $Player->GuildsPlayer->find('all', array('recursive' => -1));
  2168. $this->assertEquals(true, count($players) > 1);
  2169. $this->assertEquals(true, count($guilds) > 1);
  2170. $this->assertEquals(true, count($guildsPlayers) > 1);
  2171. }
  2172. /**
  2173. * testMultischemaFixtureWithThreeDatabases, three databases
  2174. *
  2175. * @return void
  2176. */
  2177. public function testMultischemaFixtureWithThreeDatabases() {
  2178. $config = ConnectionManager::enumConnectionObjects();
  2179. $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with Sqlite.');
  2180. $this->skipIf(
  2181. !isset($config['test']) || !isset($config['test2']) || !isset($config['test_database_three']),
  2182. '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.'
  2183. );
  2184. $this->loadFixtures('Player', 'Guild', 'GuildsPlayer', 'Armor', 'ArmorsPlayer');
  2185. $Player = ClassRegistry::init('Player');
  2186. $Player->bindModel(array(
  2187. 'hasAndBelongsToMany' => array(
  2188. 'Armor' => array(
  2189. 'with' => 'ArmorsPlayer',
  2190. ),
  2191. ),
  2192. ), false);
  2193. $this->assertEquals('test', $Player->useDbConfig);
  2194. $this->assertEquals('test', $Player->Guild->useDbConfig);
  2195. $this->assertEquals('test2', $Player->Guild->GuildsPlayer->useDbConfig);
  2196. $this->assertEquals('test2', $Player->GuildsPlayer->useDbConfig);
  2197. $this->assertEquals('test2', $Player->Armor->useDbConfig);
  2198. $this->assertEquals('test_database_three', $Player->Armor->ArmorsPlayer->useDbConfig);
  2199. $this->assertEquals('test', $Player->getDataSource()->configKeyName);
  2200. $this->assertEquals('test', $Player->Guild->getDataSource()->configKeyName);
  2201. $this->assertEquals('test2', $Player->GuildsPlayer->getDataSource()->configKeyName);
  2202. $this->assertEquals('test2', $Player->Armor->getDataSource()->configKeyName);
  2203. $this->assertEquals('test_database_three', $Player->Armor->ArmorsPlayer->getDataSource()->configKeyName);
  2204. $players = $Player->find('all', array('recursive' => -1));
  2205. $guilds = $Player->Guild->find('all', array('recursive' => -1));
  2206. $guildsPlayers = $Player->GuildsPlayer->find('all', array('recursive' => -1));
  2207. $armorsPlayers = $Player->ArmorsPlayer->find('all', array('recursive' => -1));
  2208. $this->assertEquals(true, count($players) > 1);
  2209. $this->assertEquals(true, count($guilds) > 1);
  2210. $this->assertEquals(true, count($guildsPlayers) > 1);
  2211. $this->assertEquals(true, count($armorsPlayers) > 1);
  2212. }
  2213. /**
  2214. * Tests that calling schema() on a model that is not supposed to use a table
  2215. * does not trigger any calls on any datasource
  2216. *
  2217. * @return void
  2218. */
  2219. public function testSchemaNoDB() {
  2220. $model = $this->getMock('Article', array('getDataSource'));
  2221. $model->useTable = false;
  2222. $model->expects($this->never())->method('getDataSource');
  2223. $this->assertEmpty($model->schema());
  2224. }
  2225. }