ModelIntegrationTest.php 57 KB

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