ModelIntegrationTest.php 69 KB

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