ModelIntegrationTest.php 70 KB

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