ModelIntegrationTest.php 69 KB

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