ModelIntegrationTest.php 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117
  1. <?php
  2. /**
  3. * ModelIntegrationTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Model
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
  20. App::uses('DboSource', 'Model/Datasource');
  21. /**
  22. * DboMock class
  23. * A Dbo Source driver to mock a connection and a identity name() method
  24. */
  25. class DboMock extends DboSource {
  26. /**
  27. * Returns the $field without modifications
  28. */
  29. public function name($field) {
  30. return $field;
  31. }
  32. /**
  33. * Returns true to fake a database connection
  34. */
  35. public function connect() {
  36. return true;
  37. }
  38. }
  39. /**
  40. * ModelIntegrationTest
  41. *
  42. * @package Cake.Test.Case.Model
  43. */
  44. class ModelIntegrationTest extends BaseModelTest {
  45. /**
  46. * testAssociationLazyLoading
  47. *
  48. * @group lazyloading
  49. * @return void
  50. */
  51. public function testAssociationLazyLoading() {
  52. $this->loadFixtures('ArticleFeaturedsTags');
  53. $Article = new ArticleFeatured();
  54. $this->assertTrue(isset($Article->belongsTo['User']));
  55. $this->assertFalse(property_exists($Article, 'User'));
  56. $this->assertInstanceOf('User', $Article->User);
  57. $this->assertTrue(isset($Article->belongsTo['Category']));
  58. $this->assertFalse(property_exists($Article, 'Category'));
  59. $this->assertTrue(isset($Article->Category));
  60. $this->assertInstanceOf('Category', $Article->Category);
  61. $this->assertTrue(isset($Article->hasMany['Comment']));
  62. $this->assertFalse(property_exists($Article, 'Comment'));
  63. $this->assertTrue(isset($Article->Comment));
  64. $this->assertInstanceOf('Comment', $Article->Comment);
  65. $this->assertTrue(isset($Article->hasAndBelongsToMany['Tag']));
  66. //There was not enough information to setup the association (joinTable and associationForeignKey)
  67. //so the model was not lazy loaded
  68. $this->assertTrue(property_exists($Article, 'Tag'));
  69. $this->assertTrue(isset($Article->Tag));
  70. $this->assertInstanceOf('Tag', $Article->Tag);
  71. $this->assertFalse(property_exists($Article, 'ArticleFeaturedsTag'));
  72. $this->assertInstanceOf('AppModel', $Article->ArticleFeaturedsTag);
  73. $this->assertEquals($Article->hasAndBelongsToMany['Tag']['joinTable'], 'article_featureds_tags');
  74. $this->assertEquals($Article->hasAndBelongsToMany['Tag']['associationForeignKey'], 'tag_id');
  75. }
  76. /**
  77. * testAssociationLazyLoadWithHABTM
  78. *
  79. * @group lazyloading
  80. * @return void
  81. */
  82. public function testAssociationLazyLoadWithHABTM() {
  83. $this->loadFixtures('FruitsUuidTag', 'ArticlesTag');
  84. $this->db->cacheSources = false;
  85. $Article = new ArticleB();
  86. $this->assertTrue(isset($Article->hasAndBelongsToMany['TagB']));
  87. $this->assertFalse(property_exists($Article, 'TagB'));
  88. $this->assertInstanceOf('TagB', $Article->TagB);
  89. $this->assertFalse(property_exists($Article, 'ArticlesTag'));
  90. $this->assertInstanceOf('AppModel', $Article->ArticlesTag);
  91. $UuidTag = new UuidTag();
  92. $this->assertTrue(isset($UuidTag->hasAndBelongsToMany['Fruit']));
  93. $this->assertFalse(property_exists($UuidTag, 'Fruit'));
  94. $this->assertFalse(property_exists($UuidTag, 'FruitsUuidTag'));
  95. $this->assertTrue(isset($UuidTag->Fruit));
  96. $this->assertFalse(property_exists($UuidTag, 'FruitsUuidTag'));
  97. $this->assertTrue(isset($UuidTag->FruitsUuidTag));
  98. $this->assertInstanceOf('FruitsUuidTag', $UuidTag->FruitsUuidTag);
  99. }
  100. /**
  101. * testAssociationLazyLoadWithBindModel
  102. *
  103. * @group lazyloading
  104. * @return void
  105. */
  106. public function testAssociationLazyLoadWithBindModel() {
  107. $this->loadFixtures('Article', 'User');
  108. $Article = new ArticleB();
  109. $this->assertFalse(isset($Article->belongsTo['User']));
  110. $this->assertFalse(property_exists($Article, 'User'));
  111. $Article->bindModel(array('belongsTo' => array('User')));
  112. $this->assertTrue(isset($Article->belongsTo['User']));
  113. $this->assertFalse(property_exists($Article, 'User'));
  114. $this->assertInstanceOf('User', $Article->User);
  115. }
  116. /**
  117. * Tests that creating a model with no existent database table associated will throw an exception
  118. *
  119. * @expectedException MissingTableException
  120. * @return void
  121. */
  122. public function testMissingTable() {
  123. $Article = new ArticleB(false, uniqid());
  124. $Article->schema();
  125. }
  126. /**
  127. * testPkInHAbtmLinkModelArticleB
  128. *
  129. * @return void
  130. */
  131. public function testPkInHabtmLinkModelArticleB() {
  132. $this->loadFixtures('Article', 'Tag', 'ArticlesTag');
  133. $TestModel2 = new ArticleB();
  134. $this->assertEquals($TestModel2->ArticlesTag->primaryKey, 'article_id');
  135. }
  136. /**
  137. * Tests that $cacheSources can only be disabled in the db using model settings, not enabled
  138. *
  139. * @return void
  140. */
  141. public function testCacheSourcesDisabling() {
  142. $this->loadFixtures('JoinA', 'JoinB', 'JoinAB', 'JoinC', 'JoinAC');
  143. $this->db->cacheSources = true;
  144. $TestModel = new JoinA();
  145. $TestModel->cacheSources = false;
  146. $TestModel->setSource('join_as');
  147. $this->assertFalse($this->db->cacheSources);
  148. $this->db->cacheSources = false;
  149. $TestModel = new JoinA();
  150. $TestModel->cacheSources = true;
  151. $TestModel->setSource('join_as');
  152. $this->assertFalse($this->db->cacheSources);
  153. }
  154. /**
  155. * testPkInHabtmLinkModel method
  156. *
  157. * @return void
  158. */
  159. public function testPkInHabtmLinkModel() {
  160. //Test Nonconformant Models
  161. $this->loadFixtures('Content', 'ContentAccount', 'Account', 'JoinC', 'JoinAC', 'ItemsPortfolio');
  162. $TestModel = new Content();
  163. $this->assertEquals($TestModel->ContentAccount->primaryKey, 'iContentAccountsId');
  164. //test conformant models with no PK in the join table
  165. $this->loadFixtures('Article', 'Tag');
  166. $TestModel2 = new Article();
  167. $this->assertEquals($TestModel2->ArticlesTag->primaryKey, 'article_id');
  168. //test conformant models with PK in join table
  169. $TestModel3 = new Portfolio();
  170. $this->assertEquals($TestModel3->ItemsPortfolio->primaryKey, 'id');
  171. //test conformant models with PK in join table - join table contains extra field
  172. $this->loadFixtures('JoinA', 'JoinB', 'JoinAB');
  173. $TestModel4 = new JoinA();
  174. $this->assertEquals($TestModel4->JoinAsJoinB->primaryKey, 'id');
  175. }
  176. /**
  177. * testDynamicBehaviorAttachment method
  178. *
  179. * @return void
  180. */
  181. public function testDynamicBehaviorAttachment() {
  182. $this->loadFixtures('Apple', 'Sample', 'Author');
  183. $TestModel = new Apple();
  184. $this->assertEquals($TestModel->Behaviors->attached(), array());
  185. $TestModel->Behaviors->attach('Tree', array('left' => 'left_field', 'right' => 'right_field'));
  186. $this->assertTrue(is_object($TestModel->Behaviors->Tree));
  187. $this->assertEquals($TestModel->Behaviors->attached(), array('Tree'));
  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($TestModel->Behaviors->Tree->settings['Apple'], $expected);
  198. $expected['enabled'] = false;
  199. $TestModel->Behaviors->attach('Tree', array('enabled' => false));
  200. $this->assertEquals($TestModel->Behaviors->Tree->settings['Apple'], $expected);
  201. $this->assertEquals($TestModel->Behaviors->attached(), array('Tree'));
  202. $TestModel->Behaviors->detach('Tree');
  203. $this->assertEquals($TestModel->Behaviors->attached(), array());
  204. $this->assertFalse(isset($TestModel->Behaviors->Tree));
  205. }
  206. /**
  207. * testFindWithJoinsOption method
  208. *
  209. * @access public
  210. * @return void
  211. */
  212. 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 = new DATABASE_CONFIG();
  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($TestModel->find('all'), $expected);
  396. $db2 = ConnectionManager::getDataSource('test2');
  397. $this->fixtureManager->loadSingle('User', $db2);
  398. $this->fixtureManager->loadSingle('Comment', $db2);
  399. $this->assertEquals($TestModel->find('count'), 3);
  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 = Set::extract($TestModel->User->find('all'), '{n}.User.id');
  417. $this->assertEquals($result, array('1', '2', '3', '4'));
  418. $this->assertEquals($TestModel->find('all'), $expected);
  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($TestModel->Comment->find('all'), $expected);
  578. }
  579. /**
  580. * testDisplayField method
  581. *
  582. * @return void
  583. */
  584. public function testDisplayField() {
  585. $this->loadFixtures('Post', 'Comment', 'Person', 'User');
  586. $Post = new Post();
  587. $Comment = new Comment();
  588. $Person = new Person();
  589. $this->assertEquals($Post->displayField, 'title');
  590. $this->assertEquals($Person->displayField, 'name');
  591. $this->assertEquals($Comment->displayField, 'id');
  592. }
  593. /**
  594. * testSchema method
  595. *
  596. * @return void
  597. */
  598. public function testSchema() {
  599. $Post = new Post();
  600. $result = $Post->schema();
  601. $columns = array('id', 'author_id', 'title', 'body', 'published', 'created', 'updated');
  602. $this->assertEquals(array_keys($result), $columns);
  603. $types = array('integer', 'integer', 'string', 'text', 'string', 'datetime', 'datetime');
  604. $this->assertEquals(Set::extract(array_values($result), '{n}.type'), $types);
  605. $result = $Post->schema('body');
  606. $this->assertEquals($result['type'], 'text');
  607. $this->assertNull($Post->schema('foo'));
  608. $this->assertEquals($Post->getColumnTypes(), array_combine($columns, $types));
  609. }
  610. /**
  611. * test deconstruct() with time fields.
  612. *
  613. * @return void
  614. */
  615. public function testDeconstructFieldsTime() {
  616. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  617. $this->loadFixtures('Apple');
  618. $TestModel = new Apple();
  619. $data = array();
  620. $data['Apple']['mytime']['hour'] = '';
  621. $data['Apple']['mytime']['min'] = '';
  622. $data['Apple']['mytime']['sec'] = '';
  623. $TestModel->data = null;
  624. $TestModel->set($data);
  625. $expected = array('Apple' => array('mytime' => ''));
  626. $this->assertEquals($TestModel->data, $expected);
  627. $data = array();
  628. $data['Apple']['mytime']['hour'] = '';
  629. $data['Apple']['mytime']['min'] = '';
  630. $data['Apple']['mytime']['meridan'] = '';
  631. $TestModel->data = null;
  632. $TestModel->set($data);
  633. $expected = array('Apple' => array('mytime' => ''));
  634. $this->assertEquals($TestModel->data, $expected, 'Empty values are not returning properly. %s');
  635. $data = array();
  636. $data['Apple']['mytime']['hour'] = '12';
  637. $data['Apple']['mytime']['min'] = '0';
  638. $data['Apple']['mytime']['meridian'] = 'am';
  639. $TestModel->data = null;
  640. $TestModel->set($data);
  641. $expected = array('Apple' => array('mytime' => '00:00:00'));
  642. $this->assertEquals($TestModel->data, $expected, 'Midnight is not returning proper values. %s');
  643. $data = array();
  644. $data['Apple']['mytime']['hour'] = '00';
  645. $data['Apple']['mytime']['min'] = '00';
  646. $TestModel->data = null;
  647. $TestModel->set($data);
  648. $expected = array('Apple' => array('mytime' => '00:00:00'));
  649. $this->assertEquals($TestModel->data, $expected, 'Midnight is not returning proper values. %s');
  650. $data = array();
  651. $data['Apple']['mytime']['hour'] = '03';
  652. $data['Apple']['mytime']['min'] = '04';
  653. $data['Apple']['mytime']['sec'] = '04';
  654. $TestModel->data = null;
  655. $TestModel->set($data);
  656. $expected = array('Apple' => array('mytime' => '03:04:04'));
  657. $this->assertEquals($TestModel->data, $expected);
  658. $data = array();
  659. $data['Apple']['mytime']['hour'] = '3';
  660. $data['Apple']['mytime']['min'] = '4';
  661. $data['Apple']['mytime']['sec'] = '4';
  662. $TestModel->data = null;
  663. $TestModel->set($data);
  664. $expected = array('Apple' => array('mytime' => '03:04:04'));
  665. $this->assertEquals($TestModel->data, $expected);
  666. $data = array();
  667. $data['Apple']['mytime']['hour'] = '03';
  668. $data['Apple']['mytime']['min'] = '4';
  669. $data['Apple']['mytime']['sec'] = '4';
  670. $TestModel->data = null;
  671. $TestModel->set($data);
  672. $expected = array('Apple' => array('mytime' => '03:04:04'));
  673. $this->assertEquals($TestModel->data, $expected);
  674. $db = ConnectionManager::getDataSource('test');
  675. $data = array();
  676. $data['Apple']['mytime'] = $db->expression('NOW()');
  677. $TestModel->data = null;
  678. $TestModel->set($data);
  679. $this->assertEquals($TestModel->data, $data);
  680. }
  681. /**
  682. * testDeconstructFields with datetime, timestamp, and date fields
  683. *
  684. * @return void
  685. */
  686. public function testDeconstructFieldsDateTime() {
  687. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  688. $this->loadFixtures('Apple');
  689. $TestModel = new Apple();
  690. //test null/empty values first
  691. $data['Apple']['created']['year'] = '';
  692. $data['Apple']['created']['month'] = '';
  693. $data['Apple']['created']['day'] = '';
  694. $data['Apple']['created']['hour'] = '';
  695. $data['Apple']['created']['min'] = '';
  696. $data['Apple']['created']['sec'] = '';
  697. $TestModel->data = null;
  698. $TestModel->set($data);
  699. $expected = array('Apple' => array('created' => ''));
  700. $this->assertEquals($TestModel->data, $expected);
  701. $data = array();
  702. $data['Apple']['date']['year'] = '';
  703. $data['Apple']['date']['month'] = '';
  704. $data['Apple']['date']['day'] = '';
  705. $TestModel->data = null;
  706. $TestModel->set($data);
  707. $expected = array('Apple' => array('date' => ''));
  708. $this->assertEquals($TestModel->data, $expected);
  709. $data = array();
  710. $data['Apple']['created']['year'] = '2007';
  711. $data['Apple']['created']['month'] = '08';
  712. $data['Apple']['created']['day'] = '20';
  713. $data['Apple']['created']['hour'] = '';
  714. $data['Apple']['created']['min'] = '';
  715. $data['Apple']['created']['sec'] = '';
  716. $TestModel->data = null;
  717. $TestModel->set($data);
  718. $expected = array('Apple' => array('created' => '2007-08-20 00:00:00'));
  719. $this->assertEquals($TestModel->data, $expected);
  720. $data = array();
  721. $data['Apple']['created']['year'] = '2007';
  722. $data['Apple']['created']['month'] = '08';
  723. $data['Apple']['created']['day'] = '20';
  724. $data['Apple']['created']['hour'] = '10';
  725. $data['Apple']['created']['min'] = '12';
  726. $data['Apple']['created']['sec'] = '';
  727. $TestModel->data = null;
  728. $TestModel->set($data);
  729. $expected = array('Apple' => array('created' => '2007-08-20 10:12:00'));
  730. $this->assertEquals($TestModel->data, $expected);
  731. $data = array();
  732. $data['Apple']['created']['year'] = '2007';
  733. $data['Apple']['created']['month'] = '';
  734. $data['Apple']['created']['day'] = '12';
  735. $data['Apple']['created']['hour'] = '20';
  736. $data['Apple']['created']['min'] = '';
  737. $data['Apple']['created']['sec'] = '';
  738. $TestModel->data = null;
  739. $TestModel->set($data);
  740. $expected = array('Apple' => array('created' => ''));
  741. $this->assertEquals($TestModel->data, $expected);
  742. $data = array();
  743. $data['Apple']['created']['hour'] = '20';
  744. $data['Apple']['created']['min'] = '33';
  745. $TestModel->data = null;
  746. $TestModel->set($data);
  747. $expected = array('Apple' => array('created' => ''));
  748. $this->assertEquals($TestModel->data, $expected);
  749. $data = array();
  750. $data['Apple']['created']['hour'] = '20';
  751. $data['Apple']['created']['min'] = '33';
  752. $data['Apple']['created']['sec'] = '33';
  753. $TestModel->data = null;
  754. $TestModel->set($data);
  755. $expected = array('Apple' => array('created' => ''));
  756. $this->assertEquals($TestModel->data, $expected);
  757. $data = array();
  758. $data['Apple']['created']['hour'] = '13';
  759. $data['Apple']['created']['min'] = '00';
  760. $data['Apple']['date']['year'] = '2006';
  761. $data['Apple']['date']['month'] = '12';
  762. $data['Apple']['date']['day'] = '25';
  763. $TestModel->data = null;
  764. $TestModel->set($data);
  765. $expected = array(
  766. 'Apple' => array(
  767. 'created' => '',
  768. 'date' => '2006-12-25'
  769. ));
  770. $this->assertEquals($TestModel->data, $expected);
  771. $data = array();
  772. $data['Apple']['created']['year'] = '2007';
  773. $data['Apple']['created']['month'] = '08';
  774. $data['Apple']['created']['day'] = '20';
  775. $data['Apple']['created']['hour'] = '10';
  776. $data['Apple']['created']['min'] = '12';
  777. $data['Apple']['created']['sec'] = '09';
  778. $data['Apple']['date']['year'] = '2006';
  779. $data['Apple']['date']['month'] = '12';
  780. $data['Apple']['date']['day'] = '25';
  781. $TestModel->data = null;
  782. $TestModel->set($data);
  783. $expected = array(
  784. 'Apple' => array(
  785. 'created' => '2007-08-20 10:12:09',
  786. 'date' => '2006-12-25'
  787. ));
  788. $this->assertEquals($TestModel->data, $expected);
  789. $data = array();
  790. $data['Apple']['created']['year'] = '--';
  791. $data['Apple']['created']['month'] = '--';
  792. $data['Apple']['created']['day'] = '--';
  793. $data['Apple']['created']['hour'] = '--';
  794. $data['Apple']['created']['min'] = '--';
  795. $data['Apple']['created']['sec'] = '--';
  796. $data['Apple']['date']['year'] = '--';
  797. $data['Apple']['date']['month'] = '--';
  798. $data['Apple']['date']['day'] = '--';
  799. $TestModel->data = null;
  800. $TestModel->set($data);
  801. $expected = array('Apple' => array('created' => '', 'date' => ''));
  802. $this->assertEquals($TestModel->data, $expected);
  803. $data = array();
  804. $data['Apple']['created']['year'] = '2007';
  805. $data['Apple']['created']['month'] = '--';
  806. $data['Apple']['created']['day'] = '20';
  807. $data['Apple']['created']['hour'] = '10';
  808. $data['Apple']['created']['min'] = '12';
  809. $data['Apple']['created']['sec'] = '09';
  810. $data['Apple']['date']['year'] = '2006';
  811. $data['Apple']['date']['month'] = '12';
  812. $data['Apple']['date']['day'] = '25';
  813. $TestModel->data = null;
  814. $TestModel->set($data);
  815. $expected = array('Apple' => array('created' => '', 'date' => '2006-12-25'));
  816. $this->assertEquals($TestModel->data, $expected);
  817. $data = array();
  818. $data['Apple']['date']['year'] = '2006';
  819. $data['Apple']['date']['month'] = '12';
  820. $data['Apple']['date']['day'] = '25';
  821. $TestModel->data = null;
  822. $TestModel->set($data);
  823. $expected = array('Apple' => array('date' => '2006-12-25'));
  824. $this->assertEquals($TestModel->data, $expected);
  825. $db = ConnectionManager::getDataSource('test');
  826. $data = array();
  827. $data['Apple']['modified'] = $db->expression('NOW()');
  828. $TestModel->data = null;
  829. $TestModel->set($data);
  830. $this->assertEquals($TestModel->data, $data);
  831. }
  832. /**
  833. * testTablePrefixSwitching method
  834. *
  835. * @return void
  836. */
  837. public function testTablePrefixSwitching() {
  838. ConnectionManager::create('database1',
  839. array_merge($this->db->config, array('prefix' => 'aaa_')
  840. ));
  841. ConnectionManager::create('database2',
  842. array_merge($this->db->config, array('prefix' => 'bbb_')
  843. ));
  844. $db1 = ConnectionManager::getDataSource('database1');
  845. $db2 = ConnectionManager::getDataSource('database2');
  846. $TestModel = new Apple();
  847. $TestModel->setDataSource('database1');
  848. $this->assertEquals($this->db->fullTableName($TestModel, false), 'aaa_apples');
  849. $this->assertEquals($db1->fullTableName($TestModel, false), 'aaa_apples');
  850. $this->assertEquals($db2->fullTableName($TestModel, false), 'aaa_apples');
  851. $TestModel->setDataSource('database2');
  852. $this->assertEquals($this->db->fullTableName($TestModel, false), 'bbb_apples');
  853. $this->assertEquals($db1->fullTableName($TestModel, false), 'bbb_apples');
  854. $this->assertEquals($db2->fullTableName($TestModel, false), 'bbb_apples');
  855. $TestModel = new Apple();
  856. $TestModel->tablePrefix = 'custom_';
  857. $this->assertEquals($this->db->fullTableName($TestModel, false), 'custom_apples');
  858. $TestModel->setDataSource('database1');
  859. $this->assertEquals($this->db->fullTableName($TestModel, false), 'custom_apples');
  860. $this->assertEquals($db1->fullTableName($TestModel, false), 'custom_apples');
  861. $TestModel = new Apple();
  862. $TestModel->setDataSource('database1');
  863. $this->assertEquals($this->db->fullTableName($TestModel, false), 'aaa_apples');
  864. $TestModel->tablePrefix = '';
  865. $TestModel->setDataSource('database2');
  866. $this->assertEquals($db2->fullTableName($TestModel, false), 'apples');
  867. $this->assertEquals($db1->fullTableName($TestModel, false), 'apples');
  868. $TestModel->tablePrefix = null;
  869. $TestModel->setDataSource('database1');
  870. $this->assertEquals($db2->fullTableName($TestModel, false), 'aaa_apples');
  871. $this->assertEquals($db1->fullTableName($TestModel, false), 'aaa_apples');
  872. $TestModel->tablePrefix = false;
  873. $TestModel->setDataSource('database2');
  874. $this->assertEquals($db2->fullTableName($TestModel, false), 'apples');
  875. $this->assertEquals($db1->fullTableName($TestModel, false), 'apples');
  876. }
  877. /**
  878. * Tests validation parameter order in custom validation methods
  879. *
  880. * @return void
  881. */
  882. public function testInvalidAssociation() {
  883. $TestModel = new ValidationTest1();
  884. $this->assertNull($TestModel->getAssociated('Foo'));
  885. }
  886. /**
  887. * testLoadModelSecondIteration method
  888. *
  889. * @return void
  890. */
  891. public function testLoadModelSecondIteration() {
  892. $this->loadFixtures('Apple', 'Message', 'Thread', 'Bid');
  893. $model = new ModelA();
  894. $this->assertInstanceOf('ModelA', $model);
  895. $this->assertInstanceOf('ModelB', $model->ModelB);
  896. $this->assertInstanceOf('ModelD', $model->ModelB->ModelD);
  897. $this->assertInstanceOf('ModelC', $model->ModelC);
  898. $this->assertInstanceOf('ModelD', $model->ModelC->ModelD);
  899. }
  900. /**
  901. * ensure that exists() does not persist between method calls reset on create
  902. *
  903. * @return void
  904. */
  905. public function testResetOfExistsOnCreate() {
  906. $this->loadFixtures('Article');
  907. $Article = new Article();
  908. $Article->id = 1;
  909. $Article->saveField('title', 'Reset me');
  910. $Article->delete();
  911. $Article->id = 1;
  912. $this->assertFalse($Article->exists());
  913. $Article->create();
  914. $this->assertFalse($Article->exists());
  915. $Article->id = 2;
  916. $Article->saveField('title', 'Staying alive');
  917. $result = $Article->read(null, 2);
  918. $this->assertEquals($result['Article']['title'], 'Staying alive');
  919. }
  920. /**
  921. * testUseTableFalseExistsCheck method
  922. *
  923. * @return void
  924. */
  925. public function testUseTableFalseExistsCheck() {
  926. $this->loadFixtures('Article');
  927. $Article = new Article();
  928. $Article->id = 1337;
  929. $result = $Article->exists();
  930. $this->assertFalse($result);
  931. $Article->useTable = false;
  932. $Article->id = null;
  933. $result = $Article->exists();
  934. $this->assertFalse($result);
  935. // An article with primary key of '1' has been loaded by the fixtures.
  936. $Article->useTable = false;
  937. $Article->id = 1;
  938. $result = $Article->exists();
  939. $this->assertTrue($result);
  940. }
  941. /**
  942. * testPluginAssociations method
  943. *
  944. * @return void
  945. */
  946. public function testPluginAssociations() {
  947. $this->loadFixtures('TestPluginArticle', 'User', 'TestPluginComment');
  948. $TestModel = new TestPluginArticle();
  949. $result = $TestModel->find('all');
  950. $expected = array(
  951. array(
  952. 'TestPluginArticle' => array(
  953. 'id' => 1,
  954. 'user_id' => 1,
  955. 'title' => 'First Plugin Article',
  956. 'body' => 'First Plugin Article Body',
  957. 'published' => 'Y',
  958. 'created' => '2008-09-24 10:39:23',
  959. 'updated' => '2008-09-24 10:41:31'
  960. ),
  961. 'User' => array(
  962. 'id' => 1,
  963. 'user' => 'mariano',
  964. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  965. 'created' => '2007-03-17 01:16:23',
  966. 'updated' => '2007-03-17 01:18:31'
  967. ),
  968. 'TestPluginComment' => array(
  969. array(
  970. 'id' => 1,
  971. 'article_id' => 1,
  972. 'user_id' => 2,
  973. 'comment' => 'First Comment for First Plugin Article',
  974. 'published' => 'Y',
  975. 'created' => '2008-09-24 10:45:23',
  976. 'updated' => '2008-09-24 10:47:31'
  977. ),
  978. array(
  979. 'id' => 2,
  980. 'article_id' => 1,
  981. 'user_id' => 4,
  982. 'comment' => 'Second Comment for First Plugin Article',
  983. 'published' => 'Y',
  984. 'created' => '2008-09-24 10:47:23',
  985. 'updated' => '2008-09-24 10:49:31'
  986. ),
  987. array(
  988. 'id' => 3,
  989. 'article_id' => 1,
  990. 'user_id' => 1,
  991. 'comment' => 'Third Comment for First Plugin Article',
  992. 'published' => 'Y',
  993. 'created' => '2008-09-24 10:49:23',
  994. 'updated' => '2008-09-24 10:51:31'
  995. ),
  996. array(
  997. 'id' => 4,
  998. 'article_id' => 1,
  999. 'user_id' => 1,
  1000. 'comment' => 'Fourth Comment for First Plugin Article',
  1001. 'published' => 'N',
  1002. 'created' => '2008-09-24 10:51:23',
  1003. 'updated' => '2008-09-24 10:53:31'
  1004. ))),
  1005. array(
  1006. 'TestPluginArticle' => array(
  1007. 'id' => 2,
  1008. 'user_id' => 3,
  1009. 'title' => 'Second Plugin Article',
  1010. 'body' => 'Second Plugin Article Body',
  1011. 'published' => 'Y',
  1012. 'created' => '2008-09-24 10:41:23',
  1013. 'updated' => '2008-09-24 10:43:31'
  1014. ),
  1015. 'User' => array(
  1016. 'id' => 3,
  1017. 'user' => 'larry',
  1018. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  1019. 'created' => '2007-03-17 01:20:23',
  1020. 'updated' => '2007-03-17 01:22:31'
  1021. ),
  1022. 'TestPluginComment' => array(
  1023. array(
  1024. 'id' => 5,
  1025. 'article_id' => 2,
  1026. 'user_id' => 1,
  1027. 'comment' => 'First Comment for Second Plugin Article',
  1028. 'published' => 'Y',
  1029. 'created' => '2008-09-24 10:53:23',
  1030. 'updated' => '2008-09-24 10:55:31'
  1031. ),
  1032. array(
  1033. 'id' => 6,
  1034. 'article_id' => 2,
  1035. 'user_id' => 2,
  1036. 'comment' => 'Second Comment for Second Plugin Article',
  1037. 'published' => 'Y',
  1038. 'created' => '2008-09-24 10:55:23',
  1039. 'updated' => '2008-09-24 10:57:31'
  1040. ))),
  1041. array(
  1042. 'TestPluginArticle' => array(
  1043. 'id' => 3,
  1044. 'user_id' => 1,
  1045. 'title' => 'Third Plugin Article',
  1046. 'body' => 'Third Plugin Article Body',
  1047. 'published' => 'Y',
  1048. 'created' => '2008-09-24 10:43:23',
  1049. 'updated' => '2008-09-24 10:45:31'
  1050. ),
  1051. 'User' => array(
  1052. 'id' => 1,
  1053. 'user' => 'mariano',
  1054. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  1055. 'created' => '2007-03-17 01:16:23',
  1056. 'updated' => '2007-03-17 01:18:31'
  1057. ),
  1058. 'TestPluginComment' => array()
  1059. ));
  1060. $this->assertEquals($expected, $result);
  1061. }
  1062. /**
  1063. * Tests getAssociated method
  1064. *
  1065. * @return void
  1066. */
  1067. public function testGetAssociated() {
  1068. $this->loadFixtures('Article', 'Tag');
  1069. $Article = ClassRegistry::init('Article');
  1070. $assocTypes = array('hasMany', 'hasOne', 'belongsTo', 'hasAndBelongsToMany');
  1071. foreach ($assocTypes as $type) {
  1072. $this->assertEquals($Article->getAssociated($type), array_keys($Article->{$type}));
  1073. }
  1074. $Article->bindModel(array('hasMany' => array('Category')));
  1075. $this->assertEquals($Article->getAssociated('hasMany'), array('Comment', 'Category'));
  1076. $results = $Article->getAssociated();
  1077. $results = array_keys($results);
  1078. sort($results);
  1079. $this->assertEquals($results, array('Category', 'Comment', 'Tag', 'User'));
  1080. $Article->unbindModel(array('hasAndBelongsToMany' => array('Tag')));
  1081. $this->assertEquals($Article->getAssociated('hasAndBelongsToMany'), array());
  1082. $result = $Article->getAssociated('Category');
  1083. $expected = array(
  1084. 'className' => 'Category',
  1085. 'foreignKey' => 'article_id',
  1086. 'conditions' => '',
  1087. 'fields' => '',
  1088. 'order' => '',
  1089. 'limit' => '',
  1090. 'offset' => '',
  1091. 'dependent' => '',
  1092. 'exclusive' => '',
  1093. 'finderQuery' => '',
  1094. 'counterQuery' => '',
  1095. 'association' => 'hasMany',
  1096. );
  1097. $this->assertEquals($expected, $result);
  1098. }
  1099. /**
  1100. * testAutoConstructAssociations method
  1101. *
  1102. * @return void
  1103. */
  1104. public function testAutoConstructAssociations() {
  1105. $this->loadFixtures('User', 'ArticleFeatured', 'Featured', 'ArticleFeaturedsTags');
  1106. $TestModel = new AssociationTest1();
  1107. $result = $TestModel->hasAndBelongsToMany;
  1108. $expected = array('AssociationTest2' => array(
  1109. 'unique' => false,
  1110. 'joinTable' => 'join_as_join_bs',
  1111. 'foreignKey' => false,
  1112. 'className' => 'AssociationTest2',
  1113. 'with' => 'JoinAsJoinB',
  1114. 'dynamicWith' => true,
  1115. 'associationForeignKey' => 'join_b_id',
  1116. 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '',
  1117. 'finderQuery' => '', 'deleteQuery' => '', 'insertQuery' => ''
  1118. ));
  1119. $this->assertEquals($expected, $result);
  1120. $TestModel = new ArticleFeatured();
  1121. $TestFakeModel = new ArticleFeatured(array('table' => false));
  1122. $expected = array(
  1123. 'User' => array(
  1124. 'className' => 'User', 'foreignKey' => 'user_id',
  1125. 'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => ''
  1126. ),
  1127. 'Category' => array(
  1128. 'className' => 'Category', 'foreignKey' => 'category_id',
  1129. 'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => ''
  1130. )
  1131. );
  1132. $this->assertSame($TestModel->belongsTo, $expected);
  1133. $this->assertSame($TestFakeModel->belongsTo, $expected);
  1134. $this->assertEquals($TestModel->User->name, 'User');
  1135. $this->assertEquals($TestFakeModel->User->name, 'User');
  1136. $this->assertEquals($TestModel->Category->name, 'Category');
  1137. $this->assertEquals($TestFakeModel->Category->name, 'Category');
  1138. $expected = array(
  1139. 'Featured' => array(
  1140. 'className' => 'Featured',
  1141. 'foreignKey' => 'article_featured_id',
  1142. 'conditions' => '',
  1143. 'fields' => '',
  1144. 'order' => '',
  1145. 'dependent' => ''
  1146. ));
  1147. $this->assertSame($TestModel->hasOne, $expected);
  1148. $this->assertSame($TestFakeModel->hasOne, $expected);
  1149. $this->assertEquals($TestModel->Featured->name, 'Featured');
  1150. $this->assertEquals($TestFakeModel->Featured->name, 'Featured');
  1151. $expected = array(
  1152. 'Comment' => array(
  1153. 'className' => 'Comment',
  1154. 'dependent' => true,
  1155. 'foreignKey' => 'article_featured_id',
  1156. 'conditions' => '',
  1157. 'fields' => '',
  1158. 'order' => '',
  1159. 'limit' => '',
  1160. 'offset' => '',
  1161. 'exclusive' => '',
  1162. 'finderQuery' => '',
  1163. 'counterQuery' => ''
  1164. ));
  1165. $this->assertSame($TestModel->hasMany, $expected);
  1166. $this->assertSame($TestFakeModel->hasMany, $expected);
  1167. $this->assertEquals($TestModel->Comment->name, 'Comment');
  1168. $this->assertEquals($TestFakeModel->Comment->name, 'Comment');
  1169. $expected = array(
  1170. 'Tag' => array(
  1171. 'className' => 'Tag',
  1172. 'joinTable' => 'article_featureds_tags',
  1173. 'with' => 'ArticleFeaturedsTag',
  1174. 'dynamicWith' => true,
  1175. 'foreignKey' => 'article_featured_id',
  1176. 'associationForeignKey' => 'tag_id',
  1177. 'conditions' => '',
  1178. 'fields' => '',
  1179. 'order' => '',
  1180. 'limit' => '',
  1181. 'offset' => '',
  1182. 'unique' => true,
  1183. 'finderQuery' => '',
  1184. 'deleteQuery' => '',
  1185. 'insertQuery' => ''
  1186. ));
  1187. $this->assertSame($TestModel->hasAndBelongsToMany, $expected);
  1188. $this->assertSame($TestFakeModel->hasAndBelongsToMany, $expected);
  1189. $this->assertEquals($TestModel->Tag->name, 'Tag');
  1190. $this->assertEquals($TestFakeModel->Tag->name, 'Tag');
  1191. }
  1192. /**
  1193. * test creating associations with plugins. Ensure a double alias isn't created
  1194. *
  1195. * @return void
  1196. */
  1197. public function testAutoConstructPluginAssociations() {
  1198. $Comment = ClassRegistry::init('TestPluginComment');
  1199. $this->assertEquals(2, count($Comment->belongsTo), 'Too many associations');
  1200. $this->assertFalse(isset($Comment->belongsTo['TestPlugin.User']));
  1201. $this->assertTrue(isset($Comment->belongsTo['User']), 'Missing association');
  1202. $this->assertTrue(isset($Comment->belongsTo['TestPluginArticle']), 'Missing association');
  1203. }
  1204. /**
  1205. * test Model::__construct
  1206. *
  1207. * ensure that $actsAS and $findMethods are merged.
  1208. *
  1209. * @return void
  1210. */
  1211. public function testConstruct() {
  1212. $this->loadFixtures('Post');
  1213. $TestModel = ClassRegistry::init('MergeVarPluginPost');
  1214. $this->assertEquals($TestModel->actsAs, array('Containable' => null, 'Tree' => null));
  1215. $this->assertTrue(isset($TestModel->Behaviors->Containable));
  1216. $this->assertTrue(isset($TestModel->Behaviors->Tree));
  1217. $TestModel = ClassRegistry::init('MergeVarPluginComment');
  1218. $expected = array('Containable' => array('some_settings'));
  1219. $this->assertEquals($TestModel->actsAs, $expected);
  1220. $this->assertTrue(isset($TestModel->Behaviors->Containable));
  1221. }
  1222. /**
  1223. * test Model::__construct
  1224. *
  1225. * ensure that $actsAS and $findMethods are merged.
  1226. *
  1227. * @return void
  1228. */
  1229. public function testConstructWithAlternateDataSource() {
  1230. $TestModel = ClassRegistry::init(array(
  1231. 'class' => 'DoesntMatter', 'ds' => 'test', 'table' => false
  1232. ));
  1233. $this->assertEquals('test', $TestModel->useDbConfig);
  1234. //deprecated but test it anyway
  1235. $NewVoid = new TheVoid(null, false, 'other');
  1236. $this->assertEquals('other', $NewVoid->useDbConfig);
  1237. }
  1238. /**
  1239. * testColumnTypeFetching method
  1240. *
  1241. * @return void
  1242. */
  1243. public function testColumnTypeFetching() {
  1244. $model = new Test();
  1245. $this->assertEquals($model->getColumnType('id'), 'integer');
  1246. $this->assertEquals($model->getColumnType('notes'), 'text');
  1247. $this->assertEquals($model->getColumnType('updated'), 'datetime');
  1248. $this->assertEquals($model->getColumnType('unknown'), null);
  1249. $model = new Article();
  1250. $this->assertEquals($model->getColumnType('User.created'), 'datetime');
  1251. $this->assertEquals($model->getColumnType('Tag.id'), 'integer');
  1252. $this->assertEquals($model->getColumnType('Article.id'), 'integer');
  1253. }
  1254. /**
  1255. * testHabtmUniqueKey method
  1256. *
  1257. * @return void
  1258. */
  1259. public function testHabtmUniqueKey() {
  1260. $model = new Item();
  1261. $this->assertFalse($model->hasAndBelongsToMany['Portfolio']['unique']);
  1262. }
  1263. /**
  1264. * testIdentity method
  1265. *
  1266. * @return void
  1267. */
  1268. public function testIdentity() {
  1269. $TestModel = new Test();
  1270. $result = $TestModel->alias;
  1271. $expected = 'Test';
  1272. $this->assertEquals($expected, $result);
  1273. $TestModel = new TestAlias();
  1274. $result = $TestModel->alias;
  1275. $expected = 'TestAlias';
  1276. $this->assertEquals($expected, $result);
  1277. $TestModel = new Test(array('alias' => 'AnotherTest'));
  1278. $result = $TestModel->alias;
  1279. $expected = 'AnotherTest';
  1280. $this->assertEquals($expected, $result);
  1281. }
  1282. /**
  1283. * testWithAssociation method
  1284. *
  1285. * @return void
  1286. */
  1287. public function testWithAssociation() {
  1288. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  1289. $TestModel = new Something();
  1290. $result = $TestModel->SomethingElse->find('all');
  1291. $expected = array(
  1292. array(
  1293. 'SomethingElse' => array(
  1294. 'id' => '1',
  1295. 'title' => 'First Post',
  1296. 'body' => 'First Post Body',
  1297. 'published' => 'Y',
  1298. 'created' => '2007-03-18 10:39:23',
  1299. 'updated' => '2007-03-18 10:41:31'
  1300. ),
  1301. 'Something' => array(
  1302. array(
  1303. 'id' => '3',
  1304. 'title' => 'Third Post',
  1305. 'body' => 'Third Post Body',
  1306. 'published' => 'Y',
  1307. 'created' => '2007-03-18 10:43:23',
  1308. 'updated' => '2007-03-18 10:45:31',
  1309. 'JoinThing' => array(
  1310. 'id' => '3',
  1311. 'something_id' => '3',
  1312. 'something_else_id' => '1',
  1313. 'doomed' => true,
  1314. 'created' => '2007-03-18 10:43:23',
  1315. 'updated' => '2007-03-18 10:45:31'
  1316. )))),
  1317. array(
  1318. 'SomethingElse' => array(
  1319. 'id' => '2',
  1320. 'title' => 'Second Post',
  1321. 'body' => 'Second Post Body',
  1322. 'published' => 'Y',
  1323. 'created' => '2007-03-18 10:41:23',
  1324. 'updated' => '2007-03-18 10:43:31'
  1325. ),
  1326. 'Something' => array(
  1327. array(
  1328. 'id' => '1',
  1329. 'title' => 'First Post',
  1330. 'body' => 'First Post Body',
  1331. 'published' => 'Y',
  1332. 'created' => '2007-03-18 10:39:23',
  1333. 'updated' => '2007-03-18 10:41:31',
  1334. 'JoinThing' => array(
  1335. 'id' => '1',
  1336. 'something_id' => '1',
  1337. 'something_else_id' => '2',
  1338. 'doomed' => true,
  1339. 'created' => '2007-03-18 10:39:23',
  1340. 'updated' => '2007-03-18 10:41:31'
  1341. )))),
  1342. array(
  1343. 'SomethingElse' => array(
  1344. 'id' => '3',
  1345. 'title' => 'Third Post',
  1346. 'body' => 'Third Post Body',
  1347. 'published' => 'Y',
  1348. 'created' => '2007-03-18 10:43:23',
  1349. 'updated' => '2007-03-18 10:45:31'
  1350. ),
  1351. 'Something' => array(
  1352. array(
  1353. 'id' => '2',
  1354. 'title' => 'Second Post',
  1355. 'body' => 'Second Post Body',
  1356. 'published' => 'Y',
  1357. 'created' => '2007-03-18 10:41:23',
  1358. 'updated' => '2007-03-18 10:43:31',
  1359. 'JoinThing' => array(
  1360. 'id' => '2',
  1361. 'something_id' => '2',
  1362. 'something_else_id' => '3',
  1363. 'doomed' => false,
  1364. 'created' => '2007-03-18 10:41:23',
  1365. 'updated' => '2007-03-18 10:43:31'
  1366. )))));
  1367. $this->assertEquals($expected, $result);
  1368. $result = $TestModel->find('all');
  1369. $expected = array(
  1370. array(
  1371. 'Something' => array(
  1372. 'id' => '1',
  1373. 'title' => 'First Post',
  1374. 'body' => 'First Post Body',
  1375. 'published' => 'Y',
  1376. 'created' => '2007-03-18 10:39:23',
  1377. 'updated' => '2007-03-18 10:41:31'
  1378. ),
  1379. 'SomethingElse' => array(
  1380. array(
  1381. 'id' => '2',
  1382. 'title' => 'Second Post',
  1383. 'body' => 'Second Post Body',
  1384. 'published' => 'Y',
  1385. 'created' => '2007-03-18 10:41:23',
  1386. 'updated' => '2007-03-18 10:43:31',
  1387. 'JoinThing' => array(
  1388. 'doomed' => true,
  1389. 'something_id' => '1',
  1390. 'something_else_id' => '2'
  1391. )))),
  1392. array(
  1393. 'Something' => array(
  1394. 'id' => '2',
  1395. 'title' => 'Second Post',
  1396. 'body' => 'Second Post Body',
  1397. 'published' => 'Y',
  1398. 'created' => '2007-03-18 10:41:23',
  1399. 'updated' => '2007-03-18 10:43:31'
  1400. ),
  1401. 'SomethingElse' => array(
  1402. array(
  1403. 'id' => '3',
  1404. 'title' => 'Third Post',
  1405. 'body' => 'Third Post Body',
  1406. 'published' => 'Y',
  1407. 'created' => '2007-03-18 10:43:23',
  1408. 'updated' => '2007-03-18 10:45:31',
  1409. 'JoinThing' => array(
  1410. 'doomed' => false,
  1411. 'something_id' => '2',
  1412. 'something_else_id' => '3'
  1413. )))),
  1414. array(
  1415. 'Something' => array(
  1416. 'id' => '3',
  1417. 'title' => 'Third Post',
  1418. 'body' => 'Third Post Body',
  1419. 'published' => 'Y',
  1420. 'created' => '2007-03-18 10:43:23',
  1421. 'updated' => '2007-03-18 10:45:31'
  1422. ),
  1423. 'SomethingElse' => array(
  1424. array(
  1425. 'id' => '1',
  1426. 'title' => 'First Post',
  1427. 'body' => 'First Post Body',
  1428. 'published' => 'Y',
  1429. 'created' => '2007-03-18 10:39:23',
  1430. 'updated' => '2007-03-18 10:41:31',
  1431. 'JoinThing' => array(
  1432. 'doomed' => true,
  1433. 'something_id' => '3',
  1434. 'something_else_id' => '1'
  1435. )))));
  1436. $this->assertEquals($expected, $result);
  1437. $result = $TestModel->findById(1);
  1438. $expected = array(
  1439. 'Something' => array(
  1440. 'id' => '1',
  1441. 'title' => 'First Post',
  1442. 'body' => 'First Post Body',
  1443. 'published' => 'Y',
  1444. 'created' => '2007-03-18 10:39:23',
  1445. 'updated' => '2007-03-18 10:41:31'
  1446. ),
  1447. 'SomethingElse' => array(
  1448. array(
  1449. 'id' => '2',
  1450. 'title' => 'Second Post',
  1451. 'body' => 'Second Post Body',
  1452. 'published' => 'Y',
  1453. 'created' => '2007-03-18 10:41:23',
  1454. 'updated' => '2007-03-18 10:43:31',
  1455. 'JoinThing' => array(
  1456. 'doomed' => true,
  1457. 'something_id' => '1',
  1458. 'something_else_id' => '2'
  1459. ))));
  1460. $this->assertEquals($expected, $result);
  1461. $expected = $TestModel->findById(1);
  1462. $TestModel->set($expected);
  1463. $TestModel->save();
  1464. $result = $TestModel->findById(1);
  1465. $this->assertEquals($expected, $result);
  1466. $TestModel->hasAndBelongsToMany['SomethingElse']['unique'] = false;
  1467. $TestModel->create(array(
  1468. 'Something' => array('id' => 1),
  1469. 'SomethingElse' => array(3, array(
  1470. 'something_else_id' => 1,
  1471. 'doomed' => true
  1472. ))));
  1473. $ts = date('Y-m-d H:i:s');
  1474. $TestModel->save();
  1475. $TestModel->hasAndBelongsToMany['SomethingElse']['order'] = 'SomethingElse.id ASC';
  1476. $result = $TestModel->findById(1);
  1477. $expected = array(
  1478. 'Something' => array(
  1479. 'id' => '1',
  1480. 'title' => 'First Post',
  1481. 'body' => 'First Post Body',
  1482. 'published' => 'Y',
  1483. 'created' => '2007-03-18 10:39:23'
  1484. ),
  1485. 'SomethingElse' => array(
  1486. array(
  1487. 'id' => '1',
  1488. 'title' => 'First Post',
  1489. 'body' => 'First Post Body',
  1490. 'published' => 'Y',
  1491. 'created' => '2007-03-18 10:39:23',
  1492. 'updated' => '2007-03-18 10:41:31',
  1493. 'JoinThing' => array(
  1494. 'doomed' => true,
  1495. 'something_id' => '1',
  1496. 'something_else_id' => '1'
  1497. )
  1498. ),
  1499. array(
  1500. 'id' => '2',
  1501. 'title' => 'Second Post',
  1502. 'body' => 'Second Post Body',
  1503. 'published' => 'Y',
  1504. 'created' => '2007-03-18 10:41:23',
  1505. 'updated' => '2007-03-18 10:43:31',
  1506. 'JoinThing' => array(
  1507. 'doomed' => true,
  1508. 'something_id' => '1',
  1509. 'something_else_id' => '2'
  1510. )
  1511. ),
  1512. array(
  1513. 'id' => '3',
  1514. 'title' => 'Third Post',
  1515. 'body' => 'Third Post Body',
  1516. 'published' => 'Y',
  1517. 'created' => '2007-03-18 10:43:23',
  1518. 'updated' => '2007-03-18 10:45:31',
  1519. 'JoinThing' => array(
  1520. 'doomed' => false,
  1521. 'something_id' => '1',
  1522. 'something_else_id' => '3')
  1523. )
  1524. )
  1525. );
  1526. $this->assertTrue($result['Something']['updated'] >= $ts);
  1527. unset($result['Something']['updated']);
  1528. $this->assertEquals($expected, $result);
  1529. }
  1530. /**
  1531. * testFindSelfAssociations method
  1532. *
  1533. * @return void
  1534. */
  1535. public function testFindSelfAssociations() {
  1536. $this->loadFixtures('Person');
  1537. $TestModel = new Person();
  1538. $TestModel->recursive = 2;
  1539. $result = $TestModel->read(null, 1);
  1540. $expected = array(
  1541. 'Person' => array(
  1542. 'id' => 1,
  1543. 'name' => 'person',
  1544. 'mother_id' => 2,
  1545. 'father_id' => 3
  1546. ),
  1547. 'Mother' => array(
  1548. 'id' => 2,
  1549. 'name' => 'mother',
  1550. 'mother_id' => 4,
  1551. 'father_id' => 5,
  1552. 'Mother' => array(
  1553. 'id' => 4,
  1554. 'name' => 'mother - grand mother',
  1555. 'mother_id' => 0,
  1556. 'father_id' => 0
  1557. ),
  1558. 'Father' => array(
  1559. 'id' => 5,
  1560. 'name' => 'mother - grand father',
  1561. 'mother_id' => 0,
  1562. 'father_id' => 0
  1563. )),
  1564. 'Father' => array(
  1565. 'id' => 3,
  1566. 'name' => 'father',
  1567. 'mother_id' => 6,
  1568. 'father_id' => 7,
  1569. 'Father' => array(
  1570. 'id' => 7,
  1571. 'name' => 'father - grand father',
  1572. 'mother_id' => 0,
  1573. 'father_id' => 0
  1574. ),
  1575. 'Mother' => array(
  1576. 'id' => 6,
  1577. 'name' => 'father - grand mother',
  1578. 'mother_id' => 0,
  1579. 'father_id' => 0
  1580. )));
  1581. $this->assertEquals($expected, $result);
  1582. $TestModel->recursive = 3;
  1583. $result = $TestModel->read(null, 1);
  1584. $expected = array(
  1585. 'Person' => array(
  1586. 'id' => 1,
  1587. 'name' => 'person',
  1588. 'mother_id' => 2,
  1589. 'father_id' => 3
  1590. ),
  1591. 'Mother' => array(
  1592. 'id' => 2,
  1593. 'name' => 'mother',
  1594. 'mother_id' => 4,
  1595. 'father_id' => 5,
  1596. 'Mother' => array(
  1597. 'id' => 4,
  1598. 'name' => 'mother - grand mother',
  1599. 'mother_id' => 0,
  1600. 'father_id' => 0,
  1601. 'Mother' => array(),
  1602. 'Father' => array()),
  1603. 'Father' => array(
  1604. 'id' => 5,
  1605. 'name' => 'mother - grand father',
  1606. 'mother_id' => 0,
  1607. 'father_id' => 0,
  1608. 'Father' => array(),
  1609. 'Mother' => array()
  1610. )),
  1611. 'Father' => array(
  1612. 'id' => 3,
  1613. 'name' => 'father',
  1614. 'mother_id' => 6,
  1615. 'father_id' => 7,
  1616. 'Father' => array(
  1617. 'id' => 7,
  1618. 'name' => 'father - grand father',
  1619. 'mother_id' => 0,
  1620. 'father_id' => 0,
  1621. 'Father' => array(),
  1622. 'Mother' => array()
  1623. ),
  1624. 'Mother' => array(
  1625. 'id' => 6,
  1626. 'name' => 'father - grand mother',
  1627. 'mother_id' => 0,
  1628. 'father_id' => 0,
  1629. 'Mother' => array(),
  1630. 'Father' => array()
  1631. )));
  1632. $this->assertEquals($expected, $result);
  1633. }
  1634. /**
  1635. * testDynamicAssociations method
  1636. *
  1637. * @return void
  1638. */
  1639. public function testDynamicAssociations() {
  1640. $this->loadFixtures('Article', 'Comment');
  1641. $TestModel = new Article();
  1642. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->hasOne = array();
  1643. $TestModel->hasMany['Comment'] = array_merge($TestModel->hasMany['Comment'], array(
  1644. 'foreignKey' => false,
  1645. 'conditions' => array('Comment.user_id =' => '2')
  1646. ));
  1647. $result = $TestModel->find('all');
  1648. $expected = array(
  1649. array(
  1650. 'Article' => array(
  1651. 'id' => '1',
  1652. 'user_id' => '1',
  1653. 'title' => 'First Article',
  1654. 'body' => 'First Article Body',
  1655. 'published' => 'Y',
  1656. 'created' => '2007-03-18 10:39:23',
  1657. 'updated' => '2007-03-18 10:41:31'
  1658. ),
  1659. 'Comment' => array(
  1660. array(
  1661. 'id' => '1',
  1662. 'article_id' => '1',
  1663. 'user_id' => '2',
  1664. 'comment' => 'First Comment for First Article',
  1665. 'published' => 'Y',
  1666. 'created' => '2007-03-18 10:45:23',
  1667. 'updated' => '2007-03-18 10:47:31'
  1668. ),
  1669. array(
  1670. 'id' => '6',
  1671. 'article_id' => '2',
  1672. 'user_id' => '2',
  1673. 'comment' => 'Second Comment for Second Article',
  1674. 'published' => 'Y',
  1675. 'created' => '2007-03-18 10:55:23',
  1676. 'updated' => '2007-03-18 10:57:31'
  1677. ))),
  1678. array(
  1679. 'Article' => array(
  1680. 'id' => '2',
  1681. 'user_id' => '3',
  1682. 'title' => 'Second Article',
  1683. 'body' => 'Second Article Body',
  1684. 'published' => 'Y',
  1685. 'created' => '2007-03-18 10:41:23',
  1686. 'updated' => '2007-03-18 10:43:31'
  1687. ),
  1688. 'Comment' => array(
  1689. array(
  1690. 'id' => '1',
  1691. 'article_id' => '1',
  1692. 'user_id' => '2',
  1693. 'comment' => 'First Comment for First Article',
  1694. 'published' => 'Y',
  1695. 'created' => '2007-03-18 10:45:23',
  1696. 'updated' => '2007-03-18 10:47:31'
  1697. ),
  1698. array(
  1699. 'id' => '6',
  1700. 'article_id' => '2',
  1701. 'user_id' => '2',
  1702. 'comment' => 'Second Comment for Second Article',
  1703. 'published' => 'Y',
  1704. 'created' => '2007-03-18 10:55:23',
  1705. 'updated' => '2007-03-18 10:57:31'
  1706. ))),
  1707. array(
  1708. 'Article' => array(
  1709. 'id' => '3',
  1710. 'user_id' => '1',
  1711. 'title' => 'Third Article',
  1712. 'body' => 'Third Article Body',
  1713. 'published' => 'Y',
  1714. 'created' => '2007-03-18 10:43:23',
  1715. 'updated' => '2007-03-18 10:45:31'
  1716. ),
  1717. 'Comment' => array(
  1718. array(
  1719. 'id' => '1',
  1720. 'article_id' => '1',
  1721. 'user_id' => '2',
  1722. 'comment' => 'First Comment for First Article',
  1723. 'published' => 'Y',
  1724. 'created' => '2007-03-18 10:45:23',
  1725. 'updated' => '2007-03-18 10:47:31'
  1726. ),
  1727. array(
  1728. 'id' => '6',
  1729. 'article_id' => '2',
  1730. 'user_id' => '2',
  1731. 'comment' => 'Second Comment for Second Article',
  1732. 'published' => 'Y',
  1733. 'created' => '2007-03-18 10:55:23',
  1734. 'updated' => '2007-03-18 10:57:31'
  1735. ))));
  1736. $this->assertEquals($expected, $result);
  1737. }
  1738. /**
  1739. * testCreation method
  1740. *
  1741. * @return void
  1742. */
  1743. public function testCreation() {
  1744. $this->loadFixtures('Article', 'ArticleFeaturedsTags', 'User', 'Featured');
  1745. $TestModel = new Test();
  1746. $result = $TestModel->create();
  1747. $expected = array('Test' => array('notes' => 'write some notes here'));
  1748. $this->assertEquals($expected, $result);
  1749. $TestModel = new User();
  1750. $result = $TestModel->schema();
  1751. if (isset($this->db->columns['primary_key']['length'])) {
  1752. $intLength = $this->db->columns['primary_key']['length'];
  1753. } elseif (isset($this->db->columns['integer']['length'])) {
  1754. $intLength = $this->db->columns['integer']['length'];
  1755. } else {
  1756. $intLength = 11;
  1757. }
  1758. foreach (array('collate', 'charset', 'comment') as $type) {
  1759. foreach ($result as $i => $r) {
  1760. unset($result[$i][$type]);
  1761. }
  1762. }
  1763. $expected = array(
  1764. 'id' => array(
  1765. 'type' => 'integer',
  1766. 'null' => false,
  1767. 'default' => null,
  1768. 'length' => $intLength,
  1769. 'key' => 'primary'
  1770. ),
  1771. 'user' => array(
  1772. 'type' => 'string',
  1773. 'null' => false,
  1774. 'default' => '',
  1775. 'length' => 255
  1776. ),
  1777. 'password' => array(
  1778. 'type' => 'string',
  1779. 'null' => false,
  1780. 'default' => '',
  1781. 'length' => 255
  1782. ),
  1783. 'created' => array(
  1784. 'type' => 'datetime',
  1785. 'null' => true,
  1786. 'default' => null,
  1787. 'length' => null
  1788. ),
  1789. 'updated' => array(
  1790. 'type' => 'datetime',
  1791. 'null' => true,
  1792. 'default' => null,
  1793. 'length' => null
  1794. ));
  1795. $this->assertEquals($expected, $result);
  1796. $TestModel = new Article();
  1797. $result = $TestModel->create();
  1798. $expected = array('Article' => array('published' => 'N'));
  1799. $this->assertEquals($expected, $result);
  1800. $FeaturedModel = new Featured();
  1801. $data = array(
  1802. 'article_featured_id' => 1,
  1803. 'category_id' => 1,
  1804. 'published_date' => array(
  1805. 'year' => 2008,
  1806. 'month' => 06,
  1807. 'day' => 11
  1808. ),
  1809. 'end_date' => array(
  1810. 'year' => 2008,
  1811. 'month' => 06,
  1812. 'day' => 20
  1813. ));
  1814. $expected = array(
  1815. 'Featured' => array(
  1816. 'article_featured_id' => 1,
  1817. 'category_id' => 1,
  1818. 'published_date' => '2008-06-11 00:00:00',
  1819. 'end_date' => '2008-06-20 00:00:00'
  1820. ));
  1821. $this->assertEquals($FeaturedModel->create($data), $expected);
  1822. $data = array(
  1823. 'published_date' => array(
  1824. 'year' => 2008,
  1825. 'month' => 06,
  1826. 'day' => 11
  1827. ),
  1828. 'end_date' => array(
  1829. 'year' => 2008,
  1830. 'month' => 06,
  1831. 'day' => 20
  1832. ),
  1833. 'article_featured_id' => 1,
  1834. 'category_id' => 1
  1835. );
  1836. $expected = array(
  1837. 'Featured' => array(
  1838. 'published_date' => '2008-06-11 00:00:00',
  1839. 'end_date' => '2008-06-20 00:00:00',
  1840. 'article_featured_id' => 1,
  1841. 'category_id' => 1
  1842. ));
  1843. $this->assertEquals($FeaturedModel->create($data), $expected);
  1844. }
  1845. /**
  1846. * testEscapeField to prove it escapes the field well even when it has part of the alias on it
  1847. *
  1848. * @return void
  1849. */
  1850. public function testEscapeField() {
  1851. $TestModel = new Test();
  1852. $db = $TestModel->getDataSource();
  1853. $result = $TestModel->escapeField('test_field');
  1854. $expected = $db->name('Test.test_field');
  1855. $this->assertEquals($expected, $result);
  1856. $result = $TestModel->escapeField('TestField');
  1857. $expected = $db->name('Test.TestField');
  1858. $this->assertEquals($expected, $result);
  1859. $result = $TestModel->escapeField('DomainHandle', 'Domain');
  1860. $expected = $db->name('Domain.DomainHandle');
  1861. $this->assertEquals($expected, $result);
  1862. ConnectionManager::create('mock', array('datasource' => 'DboMock'));
  1863. $TestModel->setDataSource('mock');
  1864. $db = $TestModel->getDataSource();
  1865. $result = $TestModel->escapeField('DomainHandle', 'Domain');
  1866. $expected = $db->name('Domain.DomainHandle');
  1867. $this->assertEquals($expected, $result);
  1868. ConnectionManager::drop('mock');
  1869. }
  1870. /**
  1871. * testGetID
  1872. *
  1873. * @return void
  1874. */
  1875. public function testGetID() {
  1876. $TestModel = new Test();
  1877. $result = $TestModel->getID();
  1878. $this->assertFalse($result);
  1879. $TestModel->id = 9;
  1880. $result = $TestModel->getID();
  1881. $this->assertEquals(9, $result);
  1882. $TestModel->id = array(10, 9, 8, 7);
  1883. $result = $TestModel->getID(2);
  1884. $this->assertEquals(8, $result);
  1885. $TestModel->id = array(array(), 1, 2, 3);
  1886. $result = $TestModel->getID();
  1887. $this->assertFalse($result);
  1888. }
  1889. /**
  1890. * test that model->hasMethod checks self and behaviors.
  1891. *
  1892. * @return void
  1893. */
  1894. public function testHasMethod() {
  1895. $Article = new Article();
  1896. $Article->Behaviors = $this->getMock('BehaviorCollection');
  1897. $Article->Behaviors->expects($this->at(0))
  1898. ->method('hasMethod')
  1899. ->will($this->returnValue(true));
  1900. $Article->Behaviors->expects($this->at(1))
  1901. ->method('hasMethod')
  1902. ->will($this->returnValue(false));
  1903. $this->assertTrue($Article->hasMethod('find'));
  1904. $this->assertTrue($Article->hasMethod('pass'));
  1905. $this->assertFalse($Article->hasMethod('fail'));
  1906. }
  1907. /**
  1908. * Tests that tablePrefix is taken from the datasource if none is defined in the model
  1909. *
  1910. * @return void
  1911. * @see http://cakephp.lighthouseapp.com/projects/42648/tickets/2277-caketestmodels-in-test-cases-do-not-set-model-tableprefix
  1912. */
  1913. public function testModelPrefixFromDatasource() {
  1914. ConnectionManager::create('mock', array(
  1915. 'datasource' => 'DboMock',
  1916. 'prefix' => 'custom_prefix_'
  1917. ));
  1918. $Article = new Article(false, null, 'mock');
  1919. $this->assertEquals('custom_prefix_', $Article->tablePrefix);
  1920. ConnectionManager::drop('mock');
  1921. }
  1922. }