ModelIntegrationTest.php 70 KB

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