ModelIntegrationTest.php 70 KB

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