ModelIntegrationTest.php 68 KB

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