ModelIntegrationTest.php 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307
  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($result, $expected);
  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. * test HABM operations without clobbering existing records #275
  581. *
  582. * @return void
  583. */
  584. function testHABTMKeepExisting() {
  585. $this->loadFixtures('Site', 'Domain', 'DomainsSite');
  586. $Site = new Site();
  587. $results = $Site->find('count');
  588. $expected = 3;
  589. $this->assertEquals($results, $expected);
  590. $data = $Site->findById(1);
  591. // include api.cakephp.org
  592. $data['Domain'] = array('Domain' => array(1, 2, 3));
  593. $Site->save($data);
  594. $Site->id = 1;
  595. $results = $Site->read();
  596. $expected = 3; // 3 domains belonging to cakephp
  597. $this->assertEquals($expected, count($results['Domain']));
  598. $Site->id = 2;
  599. $results = $Site->read();
  600. $expected = 2; // 2 domains belonging to markstory
  601. $this->assertEquals($expected, count($results['Domain']));
  602. $Site->id = 3;
  603. $results = $Site->read();
  604. $expected = 2;
  605. $this->assertEquals($expected, count($results['Domain']));
  606. $results['Domain'] = array('Domain' => array(7));
  607. $Site->save($results); // remove association from domain 6
  608. $results = $Site->read();
  609. $expected = 1; // only 1 domain left belonging to rchavik
  610. $this->assertEquals($expected, count($results['Domain']));
  611. // add deleted domain back
  612. $results['Domain'] = array('Domain' => array(6, 7));
  613. $Site->save($results);
  614. $results = $Site->read();
  615. $expected = 2; // 2 domains belonging to rchavik
  616. $this->assertEquals($expected, count($results['Domain']));
  617. $Site->DomainsSite->id = $results['Domain'][0]['DomainsSite']['id'];
  618. $Site->DomainsSite->saveField('active', true);
  619. $results = $Site->Domain->DomainsSite->find('count', array(
  620. 'conditions' => array(
  621. 'DomainsSite.active' => true,
  622. ),
  623. ));
  624. $expected = 5;
  625. $this->assertEquals($expected, $results);
  626. // activate api.cakephp.org
  627. $activated = $Site->DomainsSite->findByDomainId(3);
  628. $activated['DomainsSite']['active'] = true;
  629. $Site->DomainsSite->save($activated);
  630. $results = $Site->DomainsSite->find('count', array(
  631. 'conditions' => array(
  632. 'DomainsSite.active' => true,
  633. ),
  634. ));
  635. $expected = 6;
  636. $this->assertEquals($expected, $results);
  637. // remove 2 previously active domains, and leave $activated alone
  638. $data = array(
  639. 'Site' => array('id' => 1, 'name' => 'cakephp (modified)'),
  640. 'Domain' => array(
  641. 'Domain' => array(3),
  642. )
  643. );
  644. $Site->create($data);
  645. $Site->save($data);
  646. // tests that record is still identical prior to removal
  647. $Site->id = 1;
  648. $results = $Site->read();
  649. unset($results['Domain'][0]['DomainsSite']['updated']);
  650. unset($activated['DomainsSite']['updated']);
  651. $this->assertEquals($activated['DomainsSite'], $results['Domain'][0]['DomainsSite']);
  652. }
  653. /**
  654. * test HABM operations without clobbering existing records #275
  655. *
  656. * @return void
  657. */
  658. function testHABTMKeepExistingWithThreeDbs() {
  659. $config = ConnectionManager::enumConnectionObjects();
  660. $this->skipIf(
  661. !isset($config['test']) || !isset($config['test2']) || !isset($config['test_database_three']),
  662. 'Primary, secondary, and tertiary test databases not configured, skipping test. To run this test define $test, $test2, and $test_database_three in your database configuration.'
  663. );
  664. $this->loadFixtures('Player', 'Guild', 'GuildsPlayer', 'Armor', 'ArmorsPlayer');
  665. $Player = ClassRegistry::init('Player');
  666. $Player->bindModel(array(
  667. 'hasAndBelongsToMany' => array(
  668. 'Armor' => array(
  669. 'with' => 'ArmorsPlayer',
  670. 'unique' => 'keepExisting',
  671. ),
  672. ),
  673. ), false);
  674. $this->assertEquals('test', $Player->useDbConfig);
  675. $this->assertEquals('test', $Player->Guild->useDbConfig);
  676. $this->assertEquals('test2', $Player->Guild->GuildsPlayer->useDbConfig);
  677. $this->assertEquals('test2', $Player->Armor->useDbConfig);
  678. $this->assertEquals('test_database_three', $Player->ArmorsPlayer->useDbConfig);
  679. $players = $Player->find('all');
  680. $this->assertEquals(4 , count($players));
  681. $playersGuilds = Set::extract('/Guild/GuildsPlayer', $players);
  682. $this->assertEquals(3 , count($playersGuilds));
  683. $playersArmors = Set::extract('/Armor/ArmorsPlayer', $players);
  684. $this->assertEquals(3 , count($playersArmors));
  685. unset($players);
  686. $larry = $Player->findByName('larry');
  687. $larrysArmor = Set::extract('/Armor/ArmorsPlayer', $larry);
  688. $this->assertEquals(1 , count($larrysArmor));
  689. $larry['Guild']['Guild'] = array(1, 3); // larry joins another guild
  690. $larry['Armor']['Armor'] = array(2, 3); // purchases chainmail
  691. $Player->save($larry);
  692. unset($larry);
  693. $larry = $Player->findByName('larry');
  694. $larrysGuild = Set::extract('/Guild/GuildsPlayer', $larry);
  695. $this->assertEquals(2 , count($larrysGuild));
  696. $larrysArmor = Set::extract('/Armor/ArmorsPlayer', $larry);
  697. $this->assertEquals(2 , count($larrysArmor));
  698. $larrysArmorsPlayersIds = Set::extract('/Armor/ArmorsPlayer/id', $larry);
  699. $Player->ArmorsPlayer->id = 3;
  700. $Player->ArmorsPlayer->saveField('broken', true); // larry's cloak broke
  701. $larry = $Player->findByName('larry');
  702. $larrysArmor = Set::extract('/Armor/ArmorsPlayer', $larry);
  703. $larrysCloak = Set::extract('/ArmorsPlayer[armor_id=3]', $larrysArmor);
  704. $this->assertNotEmpty($larrysCloak);
  705. $this->assertTrue($larrysCloak[0]['ArmorsPlayer']['broken']); // still broken
  706. }
  707. /**
  708. * testDisplayField method
  709. *
  710. * @return void
  711. */
  712. public function testDisplayField() {
  713. $this->loadFixtures('Post', 'Comment', 'Person', 'User');
  714. $Post = new Post();
  715. $Comment = new Comment();
  716. $Person = new Person();
  717. $this->assertEquals($Post->displayField, 'title');
  718. $this->assertEquals($Person->displayField, 'name');
  719. $this->assertEquals($Comment->displayField, 'id');
  720. }
  721. /**
  722. * testSchema method
  723. *
  724. * @return void
  725. */
  726. public function testSchema() {
  727. $Post = new Post();
  728. $result = $Post->schema();
  729. $columns = array('id', 'author_id', 'title', 'body', 'published', 'created', 'updated');
  730. $this->assertEquals(array_keys($result), $columns);
  731. $types = array('integer', 'integer', 'string', 'text', 'string', 'datetime', 'datetime');
  732. $this->assertEquals(Set::extract(array_values($result), '{n}.type'), $types);
  733. $result = $Post->schema('body');
  734. $this->assertEquals($result['type'], 'text');
  735. $this->assertNull($Post->schema('foo'));
  736. $this->assertEquals($Post->getColumnTypes(), array_combine($columns, $types));
  737. }
  738. /**
  739. * test deconstruct() with time fields.
  740. *
  741. * @return void
  742. */
  743. public function testDeconstructFieldsTime() {
  744. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  745. $this->loadFixtures('Apple');
  746. $TestModel = new Apple();
  747. $data = array();
  748. $data['Apple']['mytime']['hour'] = '';
  749. $data['Apple']['mytime']['min'] = '';
  750. $data['Apple']['mytime']['sec'] = '';
  751. $TestModel->data = null;
  752. $TestModel->set($data);
  753. $expected = array('Apple' => array('mytime' => ''));
  754. $this->assertEquals($TestModel->data, $expected);
  755. $data = array();
  756. $data['Apple']['mytime']['hour'] = '';
  757. $data['Apple']['mytime']['min'] = '';
  758. $data['Apple']['mytime']['meridan'] = '';
  759. $TestModel->data = null;
  760. $TestModel->set($data);
  761. $expected = array('Apple' => array('mytime' => ''));
  762. $this->assertEquals($TestModel->data, $expected, 'Empty values are not returning properly. %s');
  763. $data = array();
  764. $data['Apple']['mytime']['hour'] = '12';
  765. $data['Apple']['mytime']['min'] = '0';
  766. $data['Apple']['mytime']['meridian'] = 'am';
  767. $TestModel->data = null;
  768. $TestModel->set($data);
  769. $expected = array('Apple' => array('mytime' => '00:00:00'));
  770. $this->assertEquals($TestModel->data, $expected, 'Midnight is not returning proper values. %s');
  771. $data = array();
  772. $data['Apple']['mytime']['hour'] = '00';
  773. $data['Apple']['mytime']['min'] = '00';
  774. $TestModel->data = null;
  775. $TestModel->set($data);
  776. $expected = array('Apple' => array('mytime' => '00:00:00'));
  777. $this->assertEquals($TestModel->data, $expected, 'Midnight is not returning proper values. %s');
  778. $data = array();
  779. $data['Apple']['mytime']['hour'] = '03';
  780. $data['Apple']['mytime']['min'] = '04';
  781. $data['Apple']['mytime']['sec'] = '04';
  782. $TestModel->data = null;
  783. $TestModel->set($data);
  784. $expected = array('Apple' => array('mytime' => '03:04:04'));
  785. $this->assertEquals($TestModel->data, $expected);
  786. $data = array();
  787. $data['Apple']['mytime']['hour'] = '3';
  788. $data['Apple']['mytime']['min'] = '4';
  789. $data['Apple']['mytime']['sec'] = '4';
  790. $TestModel->data = null;
  791. $TestModel->set($data);
  792. $expected = array('Apple' => array('mytime' => '03:04:04'));
  793. $this->assertEquals($TestModel->data, $expected);
  794. $data = array();
  795. $data['Apple']['mytime']['hour'] = '03';
  796. $data['Apple']['mytime']['min'] = '4';
  797. $data['Apple']['mytime']['sec'] = '4';
  798. $TestModel->data = null;
  799. $TestModel->set($data);
  800. $expected = array('Apple' => array('mytime' => '03:04:04'));
  801. $this->assertEquals($TestModel->data, $expected);
  802. $db = ConnectionManager::getDataSource('test');
  803. $data = array();
  804. $data['Apple']['mytime'] = $db->expression('NOW()');
  805. $TestModel->data = null;
  806. $TestModel->set($data);
  807. $this->assertEquals($TestModel->data, $data);
  808. }
  809. /**
  810. * testDeconstructFields with datetime, timestamp, and date fields
  811. *
  812. * @return void
  813. */
  814. public function testDeconstructFieldsDateTime() {
  815. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  816. $this->loadFixtures('Apple');
  817. $TestModel = new Apple();
  818. //test null/empty values first
  819. $data['Apple']['created']['year'] = '';
  820. $data['Apple']['created']['month'] = '';
  821. $data['Apple']['created']['day'] = '';
  822. $data['Apple']['created']['hour'] = '';
  823. $data['Apple']['created']['min'] = '';
  824. $data['Apple']['created']['sec'] = '';
  825. $TestModel->data = null;
  826. $TestModel->set($data);
  827. $expected = array('Apple' => array('created' => ''));
  828. $this->assertEquals($TestModel->data, $expected);
  829. $data = array();
  830. $data['Apple']['date']['year'] = '';
  831. $data['Apple']['date']['month'] = '';
  832. $data['Apple']['date']['day'] = '';
  833. $TestModel->data = null;
  834. $TestModel->set($data);
  835. $expected = array('Apple' => array('date' => ''));
  836. $this->assertEquals($TestModel->data, $expected);
  837. $data = array();
  838. $data['Apple']['created']['year'] = '2007';
  839. $data['Apple']['created']['month'] = '08';
  840. $data['Apple']['created']['day'] = '20';
  841. $data['Apple']['created']['hour'] = '';
  842. $data['Apple']['created']['min'] = '';
  843. $data['Apple']['created']['sec'] = '';
  844. $TestModel->data = null;
  845. $TestModel->set($data);
  846. $expected = array('Apple' => array('created' => '2007-08-20 00:00:00'));
  847. $this->assertEquals($TestModel->data, $expected);
  848. $data = array();
  849. $data['Apple']['created']['year'] = '2007';
  850. $data['Apple']['created']['month'] = '08';
  851. $data['Apple']['created']['day'] = '20';
  852. $data['Apple']['created']['hour'] = '10';
  853. $data['Apple']['created']['min'] = '12';
  854. $data['Apple']['created']['sec'] = '';
  855. $TestModel->data = null;
  856. $TestModel->set($data);
  857. $expected = array('Apple' => array('created' => '2007-08-20 10:12:00'));
  858. $this->assertEquals($TestModel->data, $expected);
  859. $data = array();
  860. $data['Apple']['created']['year'] = '2007';
  861. $data['Apple']['created']['month'] = '';
  862. $data['Apple']['created']['day'] = '12';
  863. $data['Apple']['created']['hour'] = '20';
  864. $data['Apple']['created']['min'] = '';
  865. $data['Apple']['created']['sec'] = '';
  866. $TestModel->data = null;
  867. $TestModel->set($data);
  868. $expected = array('Apple' => array('created' => ''));
  869. $this->assertEquals($TestModel->data, $expected);
  870. $data = array();
  871. $data['Apple']['created']['hour'] = '20';
  872. $data['Apple']['created']['min'] = '33';
  873. $TestModel->data = null;
  874. $TestModel->set($data);
  875. $expected = array('Apple' => array('created' => ''));
  876. $this->assertEquals($TestModel->data, $expected);
  877. $data = array();
  878. $data['Apple']['created']['hour'] = '20';
  879. $data['Apple']['created']['min'] = '33';
  880. $data['Apple']['created']['sec'] = '33';
  881. $TestModel->data = null;
  882. $TestModel->set($data);
  883. $expected = array('Apple' => array('created' => ''));
  884. $this->assertEquals($TestModel->data, $expected);
  885. $data = array();
  886. $data['Apple']['created']['hour'] = '13';
  887. $data['Apple']['created']['min'] = '00';
  888. $data['Apple']['date']['year'] = '2006';
  889. $data['Apple']['date']['month'] = '12';
  890. $data['Apple']['date']['day'] = '25';
  891. $TestModel->data = null;
  892. $TestModel->set($data);
  893. $expected = array(
  894. 'Apple' => array(
  895. 'created' => '',
  896. 'date' => '2006-12-25'
  897. ));
  898. $this->assertEquals($TestModel->data, $expected);
  899. $data = array();
  900. $data['Apple']['created']['year'] = '2007';
  901. $data['Apple']['created']['month'] = '08';
  902. $data['Apple']['created']['day'] = '20';
  903. $data['Apple']['created']['hour'] = '10';
  904. $data['Apple']['created']['min'] = '12';
  905. $data['Apple']['created']['sec'] = '09';
  906. $data['Apple']['date']['year'] = '2006';
  907. $data['Apple']['date']['month'] = '12';
  908. $data['Apple']['date']['day'] = '25';
  909. $TestModel->data = null;
  910. $TestModel->set($data);
  911. $expected = array(
  912. 'Apple' => array(
  913. 'created' => '2007-08-20 10:12:09',
  914. 'date' => '2006-12-25'
  915. ));
  916. $this->assertEquals($TestModel->data, $expected);
  917. $data = array();
  918. $data['Apple']['created']['year'] = '--';
  919. $data['Apple']['created']['month'] = '--';
  920. $data['Apple']['created']['day'] = '--';
  921. $data['Apple']['created']['hour'] = '--';
  922. $data['Apple']['created']['min'] = '--';
  923. $data['Apple']['created']['sec'] = '--';
  924. $data['Apple']['date']['year'] = '--';
  925. $data['Apple']['date']['month'] = '--';
  926. $data['Apple']['date']['day'] = '--';
  927. $TestModel->data = null;
  928. $TestModel->set($data);
  929. $expected = array('Apple' => array('created' => '', 'date' => ''));
  930. $this->assertEquals($TestModel->data, $expected);
  931. $data = array();
  932. $data['Apple']['created']['year'] = '2007';
  933. $data['Apple']['created']['month'] = '--';
  934. $data['Apple']['created']['day'] = '20';
  935. $data['Apple']['created']['hour'] = '10';
  936. $data['Apple']['created']['min'] = '12';
  937. $data['Apple']['created']['sec'] = '09';
  938. $data['Apple']['date']['year'] = '2006';
  939. $data['Apple']['date']['month'] = '12';
  940. $data['Apple']['date']['day'] = '25';
  941. $TestModel->data = null;
  942. $TestModel->set($data);
  943. $expected = array('Apple' => array('created' => '', 'date' => '2006-12-25'));
  944. $this->assertEquals($TestModel->data, $expected);
  945. $data = array();
  946. $data['Apple']['date']['year'] = '2006';
  947. $data['Apple']['date']['month'] = '12';
  948. $data['Apple']['date']['day'] = '25';
  949. $TestModel->data = null;
  950. $TestModel->set($data);
  951. $expected = array('Apple' => array('date' => '2006-12-25'));
  952. $this->assertEquals($TestModel->data, $expected);
  953. $db = ConnectionManager::getDataSource('test');
  954. $data = array();
  955. $data['Apple']['modified'] = $db->expression('NOW()');
  956. $TestModel->data = null;
  957. $TestModel->set($data);
  958. $this->assertEquals($TestModel->data, $data);
  959. }
  960. /**
  961. * testTablePrefixSwitching method
  962. *
  963. * @return void
  964. */
  965. public function testTablePrefixSwitching() {
  966. ConnectionManager::create('database1',
  967. array_merge($this->db->config, array('prefix' => 'aaa_')
  968. ));
  969. ConnectionManager::create('database2',
  970. array_merge($this->db->config, array('prefix' => 'bbb_')
  971. ));
  972. $db1 = ConnectionManager::getDataSource('database1');
  973. $db2 = ConnectionManager::getDataSource('database2');
  974. $TestModel = new Apple();
  975. $TestModel->setDataSource('database1');
  976. $this->assertContains('aaa_apples', $this->db->fullTableName($TestModel));
  977. $this->assertContains('aaa_apples', $db1->fullTableName($TestModel));
  978. $this->assertContains('aaa_apples', $db2->fullTableName($TestModel));
  979. $TestModel->setDataSource('database2');
  980. $this->assertContains('bbb_apples', $this->db->fullTableName($TestModel));
  981. $this->assertContains('bbb_apples', $db1->fullTableName($TestModel));
  982. $this->assertContains('bbb_apples', $db2->fullTableName($TestModel));
  983. $TestModel = new Apple();
  984. $TestModel->tablePrefix = 'custom_';
  985. $this->assertContains('custom_apples', $this->db->fullTableName($TestModel));
  986. $TestModel->setDataSource('database1');
  987. $this->assertContains('custom_apples', $this->db->fullTableName($TestModel));
  988. $this->assertContains('custom_apples', $db1->fullTableName($TestModel));
  989. $TestModel = new Apple();
  990. $TestModel->setDataSource('database1');
  991. $this->assertContains('aaa_apples', $this->db->fullTableName($TestModel));
  992. $TestModel->tablePrefix = '';
  993. $TestModel->setDataSource('database2');
  994. $this->assertContains('apples', $db2->fullTableName($TestModel));
  995. $this->assertContains('apples', $db1->fullTableName($TestModel));
  996. $TestModel->tablePrefix = null;
  997. $TestModel->setDataSource('database1');
  998. $this->assertContains('aaa_apples', $db2->fullTableName($TestModel));
  999. $this->assertContains('aaa_apples', $db1->fullTableName($TestModel));
  1000. $TestModel->tablePrefix = false;
  1001. $TestModel->setDataSource('database2');
  1002. $this->assertContains('apples', $db2->fullTableName($TestModel));
  1003. $this->assertContains('apples', $db1->fullTableName($TestModel));
  1004. }
  1005. /**
  1006. * Tests validation parameter order in custom validation methods
  1007. *
  1008. * @return void
  1009. */
  1010. public function testInvalidAssociation() {
  1011. $TestModel = new ValidationTest1();
  1012. $this->assertNull($TestModel->getAssociated('Foo'));
  1013. }
  1014. /**
  1015. * testLoadModelSecondIteration method
  1016. *
  1017. * @return void
  1018. */
  1019. public function testLoadModelSecondIteration() {
  1020. $this->loadFixtures('Apple', 'Message', 'Thread', 'Bid');
  1021. $model = new ModelA();
  1022. $this->assertInstanceOf('ModelA', $model);
  1023. $this->assertInstanceOf('ModelB', $model->ModelB);
  1024. $this->assertInstanceOf('ModelD', $model->ModelB->ModelD);
  1025. $this->assertInstanceOf('ModelC', $model->ModelC);
  1026. $this->assertInstanceOf('ModelD', $model->ModelC->ModelD);
  1027. }
  1028. /**
  1029. * ensure that exists() does not persist between method calls reset on create
  1030. *
  1031. * @return void
  1032. */
  1033. public function testResetOfExistsOnCreate() {
  1034. $this->loadFixtures('Article');
  1035. $Article = new Article();
  1036. $Article->id = 1;
  1037. $Article->saveField('title', 'Reset me');
  1038. $Article->delete();
  1039. $Article->id = 1;
  1040. $this->assertFalse($Article->exists());
  1041. $Article->create();
  1042. $this->assertFalse($Article->exists());
  1043. $Article->id = 2;
  1044. $Article->saveField('title', 'Staying alive');
  1045. $result = $Article->read(null, 2);
  1046. $this->assertEquals($result['Article']['title'], 'Staying alive');
  1047. }
  1048. /**
  1049. * testUseTableFalseExistsCheck method
  1050. *
  1051. * @return void
  1052. */
  1053. public function testUseTableFalseExistsCheck() {
  1054. $this->loadFixtures('Article');
  1055. $Article = new Article();
  1056. $Article->id = 1337;
  1057. $result = $Article->exists();
  1058. $this->assertFalse($result);
  1059. $Article->useTable = false;
  1060. $Article->id = null;
  1061. $result = $Article->exists();
  1062. $this->assertFalse($result);
  1063. // An article with primary key of '1' has been loaded by the fixtures.
  1064. $Article->useTable = false;
  1065. $Article->id = 1;
  1066. $result = $Article->exists();
  1067. $this->assertTrue($result);
  1068. }
  1069. /**
  1070. * testPluginAssociations method
  1071. *
  1072. * @return void
  1073. */
  1074. public function testPluginAssociations() {
  1075. $this->loadFixtures('TestPluginArticle', 'User', 'TestPluginComment');
  1076. $TestModel = new TestPluginArticle();
  1077. $result = $TestModel->find('all');
  1078. $expected = array(
  1079. array(
  1080. 'TestPluginArticle' => array(
  1081. 'id' => 1,
  1082. 'user_id' => 1,
  1083. 'title' => 'First Plugin Article',
  1084. 'body' => 'First Plugin Article Body',
  1085. 'published' => 'Y',
  1086. 'created' => '2008-09-24 10:39:23',
  1087. 'updated' => '2008-09-24 10:41:31'
  1088. ),
  1089. 'User' => array(
  1090. 'id' => 1,
  1091. 'user' => 'mariano',
  1092. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  1093. 'created' => '2007-03-17 01:16:23',
  1094. 'updated' => '2007-03-17 01:18:31'
  1095. ),
  1096. 'TestPluginComment' => array(
  1097. array(
  1098. 'id' => 1,
  1099. 'article_id' => 1,
  1100. 'user_id' => 2,
  1101. 'comment' => 'First Comment for First Plugin Article',
  1102. 'published' => 'Y',
  1103. 'created' => '2008-09-24 10:45:23',
  1104. 'updated' => '2008-09-24 10:47:31'
  1105. ),
  1106. array(
  1107. 'id' => 2,
  1108. 'article_id' => 1,
  1109. 'user_id' => 4,
  1110. 'comment' => 'Second Comment for First Plugin Article',
  1111. 'published' => 'Y',
  1112. 'created' => '2008-09-24 10:47:23',
  1113. 'updated' => '2008-09-24 10:49:31'
  1114. ),
  1115. array(
  1116. 'id' => 3,
  1117. 'article_id' => 1,
  1118. 'user_id' => 1,
  1119. 'comment' => 'Third Comment for First Plugin Article',
  1120. 'published' => 'Y',
  1121. 'created' => '2008-09-24 10:49:23',
  1122. 'updated' => '2008-09-24 10:51:31'
  1123. ),
  1124. array(
  1125. 'id' => 4,
  1126. 'article_id' => 1,
  1127. 'user_id' => 1,
  1128. 'comment' => 'Fourth Comment for First Plugin Article',
  1129. 'published' => 'N',
  1130. 'created' => '2008-09-24 10:51:23',
  1131. 'updated' => '2008-09-24 10:53:31'
  1132. ))),
  1133. array(
  1134. 'TestPluginArticle' => array(
  1135. 'id' => 2,
  1136. 'user_id' => 3,
  1137. 'title' => 'Second Plugin Article',
  1138. 'body' => 'Second Plugin Article Body',
  1139. 'published' => 'Y',
  1140. 'created' => '2008-09-24 10:41:23',
  1141. 'updated' => '2008-09-24 10:43:31'
  1142. ),
  1143. 'User' => array(
  1144. 'id' => 3,
  1145. 'user' => 'larry',
  1146. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  1147. 'created' => '2007-03-17 01:20:23',
  1148. 'updated' => '2007-03-17 01:22:31'
  1149. ),
  1150. 'TestPluginComment' => array(
  1151. array(
  1152. 'id' => 5,
  1153. 'article_id' => 2,
  1154. 'user_id' => 1,
  1155. 'comment' => 'First Comment for Second Plugin Article',
  1156. 'published' => 'Y',
  1157. 'created' => '2008-09-24 10:53:23',
  1158. 'updated' => '2008-09-24 10:55:31'
  1159. ),
  1160. array(
  1161. 'id' => 6,
  1162. 'article_id' => 2,
  1163. 'user_id' => 2,
  1164. 'comment' => 'Second Comment for Second Plugin Article',
  1165. 'published' => 'Y',
  1166. 'created' => '2008-09-24 10:55:23',
  1167. 'updated' => '2008-09-24 10:57:31'
  1168. ))),
  1169. array(
  1170. 'TestPluginArticle' => array(
  1171. 'id' => 3,
  1172. 'user_id' => 1,
  1173. 'title' => 'Third Plugin Article',
  1174. 'body' => 'Third Plugin Article Body',
  1175. 'published' => 'Y',
  1176. 'created' => '2008-09-24 10:43:23',
  1177. 'updated' => '2008-09-24 10:45:31'
  1178. ),
  1179. 'User' => array(
  1180. 'id' => 1,
  1181. 'user' => 'mariano',
  1182. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  1183. 'created' => '2007-03-17 01:16:23',
  1184. 'updated' => '2007-03-17 01:18:31'
  1185. ),
  1186. 'TestPluginComment' => array()
  1187. ));
  1188. $this->assertEquals($expected, $result);
  1189. }
  1190. /**
  1191. * Tests getAssociated method
  1192. *
  1193. * @return void
  1194. */
  1195. public function testGetAssociated() {
  1196. $this->loadFixtures('Article', 'Tag');
  1197. $Article = ClassRegistry::init('Article');
  1198. $assocTypes = array('hasMany', 'hasOne', 'belongsTo', 'hasAndBelongsToMany');
  1199. foreach ($assocTypes as $type) {
  1200. $this->assertEquals($Article->getAssociated($type), array_keys($Article->{$type}));
  1201. }
  1202. $Article->bindModel(array('hasMany' => array('Category')));
  1203. $this->assertEquals($Article->getAssociated('hasMany'), array('Comment', 'Category'));
  1204. $results = $Article->getAssociated();
  1205. $results = array_keys($results);
  1206. sort($results);
  1207. $this->assertEquals($results, array('Category', 'Comment', 'Tag', 'User'));
  1208. $Article->unbindModel(array('hasAndBelongsToMany' => array('Tag')));
  1209. $this->assertEquals($Article->getAssociated('hasAndBelongsToMany'), array());
  1210. $result = $Article->getAssociated('Category');
  1211. $expected = array(
  1212. 'className' => 'Category',
  1213. 'foreignKey' => 'article_id',
  1214. 'conditions' => '',
  1215. 'fields' => '',
  1216. 'order' => '',
  1217. 'limit' => '',
  1218. 'offset' => '',
  1219. 'dependent' => '',
  1220. 'exclusive' => '',
  1221. 'finderQuery' => '',
  1222. 'counterQuery' => '',
  1223. 'association' => 'hasMany',
  1224. );
  1225. $this->assertEquals($expected, $result);
  1226. }
  1227. /**
  1228. * testAutoConstructAssociations method
  1229. *
  1230. * @return void
  1231. */
  1232. public function testAutoConstructAssociations() {
  1233. $this->loadFixtures('User', 'ArticleFeatured', 'Featured', 'ArticleFeaturedsTags');
  1234. $TestModel = new AssociationTest1();
  1235. $result = $TestModel->hasAndBelongsToMany;
  1236. $expected = array('AssociationTest2' => array(
  1237. 'unique' => false,
  1238. 'joinTable' => 'join_as_join_bs',
  1239. 'foreignKey' => false,
  1240. 'className' => 'AssociationTest2',
  1241. 'with' => 'JoinAsJoinB',
  1242. 'dynamicWith' => true,
  1243. 'associationForeignKey' => 'join_b_id',
  1244. 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '',
  1245. 'finderQuery' => '', 'deleteQuery' => '', 'insertQuery' => ''
  1246. ));
  1247. $this->assertEquals($expected, $result);
  1248. $TestModel = new ArticleFeatured();
  1249. $TestFakeModel = new ArticleFeatured(array('table' => false));
  1250. $expected = array(
  1251. 'User' => array(
  1252. 'className' => 'User', 'foreignKey' => 'user_id',
  1253. 'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => ''
  1254. ),
  1255. 'Category' => array(
  1256. 'className' => 'Category', 'foreignKey' => 'category_id',
  1257. 'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => ''
  1258. )
  1259. );
  1260. $this->assertSame($TestModel->belongsTo, $expected);
  1261. $this->assertSame($TestFakeModel->belongsTo, $expected);
  1262. $this->assertEquals($TestModel->User->name, 'User');
  1263. $this->assertEquals($TestFakeModel->User->name, 'User');
  1264. $this->assertEquals($TestModel->Category->name, 'Category');
  1265. $this->assertEquals($TestFakeModel->Category->name, 'Category');
  1266. $expected = array(
  1267. 'Featured' => array(
  1268. 'className' => 'Featured',
  1269. 'foreignKey' => 'article_featured_id',
  1270. 'conditions' => '',
  1271. 'fields' => '',
  1272. 'order' => '',
  1273. 'dependent' => ''
  1274. ));
  1275. $this->assertSame($TestModel->hasOne, $expected);
  1276. $this->assertSame($TestFakeModel->hasOne, $expected);
  1277. $this->assertEquals($TestModel->Featured->name, 'Featured');
  1278. $this->assertEquals($TestFakeModel->Featured->name, 'Featured');
  1279. $expected = array(
  1280. 'Comment' => array(
  1281. 'className' => 'Comment',
  1282. 'dependent' => true,
  1283. 'foreignKey' => 'article_featured_id',
  1284. 'conditions' => '',
  1285. 'fields' => '',
  1286. 'order' => '',
  1287. 'limit' => '',
  1288. 'offset' => '',
  1289. 'exclusive' => '',
  1290. 'finderQuery' => '',
  1291. 'counterQuery' => ''
  1292. ));
  1293. $this->assertSame($TestModel->hasMany, $expected);
  1294. $this->assertSame($TestFakeModel->hasMany, $expected);
  1295. $this->assertEquals($TestModel->Comment->name, 'Comment');
  1296. $this->assertEquals($TestFakeModel->Comment->name, 'Comment');
  1297. $expected = array(
  1298. 'Tag' => array(
  1299. 'className' => 'Tag',
  1300. 'joinTable' => 'article_featureds_tags',
  1301. 'with' => 'ArticleFeaturedsTag',
  1302. 'dynamicWith' => true,
  1303. 'foreignKey' => 'article_featured_id',
  1304. 'associationForeignKey' => 'tag_id',
  1305. 'conditions' => '',
  1306. 'fields' => '',
  1307. 'order' => '',
  1308. 'limit' => '',
  1309. 'offset' => '',
  1310. 'unique' => true,
  1311. 'finderQuery' => '',
  1312. 'deleteQuery' => '',
  1313. 'insertQuery' => ''
  1314. ));
  1315. $this->assertSame($TestModel->hasAndBelongsToMany, $expected);
  1316. $this->assertSame($TestFakeModel->hasAndBelongsToMany, $expected);
  1317. $this->assertEquals($TestModel->Tag->name, 'Tag');
  1318. $this->assertEquals($TestFakeModel->Tag->name, 'Tag');
  1319. }
  1320. /**
  1321. * test creating associations with plugins. Ensure a double alias isn't created
  1322. *
  1323. * @return void
  1324. */
  1325. public function testAutoConstructPluginAssociations() {
  1326. $Comment = ClassRegistry::init('TestPluginComment');
  1327. $this->assertEquals(2, count($Comment->belongsTo), 'Too many associations');
  1328. $this->assertFalse(isset($Comment->belongsTo['TestPlugin.User']));
  1329. $this->assertTrue(isset($Comment->belongsTo['User']), 'Missing association');
  1330. $this->assertTrue(isset($Comment->belongsTo['TestPluginArticle']), 'Missing association');
  1331. }
  1332. /**
  1333. * test Model::__construct
  1334. *
  1335. * ensure that $actsAS and $findMethods are merged.
  1336. *
  1337. * @return void
  1338. */
  1339. public function testConstruct() {
  1340. $this->loadFixtures('Post');
  1341. $TestModel = ClassRegistry::init('MergeVarPluginPost');
  1342. $this->assertEquals($TestModel->actsAs, array('Containable' => null, 'Tree' => null));
  1343. $this->assertTrue(isset($TestModel->Behaviors->Containable));
  1344. $this->assertTrue(isset($TestModel->Behaviors->Tree));
  1345. $TestModel = ClassRegistry::init('MergeVarPluginComment');
  1346. $expected = array('Containable' => array('some_settings'));
  1347. $this->assertEquals($TestModel->actsAs, $expected);
  1348. $this->assertTrue(isset($TestModel->Behaviors->Containable));
  1349. }
  1350. /**
  1351. * test Model::__construct
  1352. *
  1353. * ensure that $actsAS and $findMethods are merged.
  1354. *
  1355. * @return void
  1356. */
  1357. public function testConstructWithAlternateDataSource() {
  1358. $TestModel = ClassRegistry::init(array(
  1359. 'class' => 'DoesntMatter', 'ds' => 'test', 'table' => false
  1360. ));
  1361. $this->assertEquals('test', $TestModel->useDbConfig);
  1362. //deprecated but test it anyway
  1363. $NewVoid = new TheVoid(null, false, 'other');
  1364. $this->assertEquals('other', $NewVoid->useDbConfig);
  1365. }
  1366. /**
  1367. * testColumnTypeFetching method
  1368. *
  1369. * @return void
  1370. */
  1371. public function testColumnTypeFetching() {
  1372. $model = new Test();
  1373. $this->assertEquals($model->getColumnType('id'), 'integer');
  1374. $this->assertEquals($model->getColumnType('notes'), 'text');
  1375. $this->assertEquals($model->getColumnType('updated'), 'datetime');
  1376. $this->assertEquals($model->getColumnType('unknown'), null);
  1377. $model = new Article();
  1378. $this->assertEquals($model->getColumnType('User.created'), 'datetime');
  1379. $this->assertEquals($model->getColumnType('Tag.id'), 'integer');
  1380. $this->assertEquals($model->getColumnType('Article.id'), 'integer');
  1381. }
  1382. /**
  1383. * testHabtmUniqueKey method
  1384. *
  1385. * @return void
  1386. */
  1387. public function testHabtmUniqueKey() {
  1388. $model = new Item();
  1389. $this->assertFalse($model->hasAndBelongsToMany['Portfolio']['unique']);
  1390. }
  1391. /**
  1392. * testIdentity method
  1393. *
  1394. * @return void
  1395. */
  1396. public function testIdentity() {
  1397. $TestModel = new Test();
  1398. $result = $TestModel->alias;
  1399. $expected = 'Test';
  1400. $this->assertEquals($expected, $result);
  1401. $TestModel = new TestAlias();
  1402. $result = $TestModel->alias;
  1403. $expected = 'TestAlias';
  1404. $this->assertEquals($expected, $result);
  1405. $TestModel = new Test(array('alias' => 'AnotherTest'));
  1406. $result = $TestModel->alias;
  1407. $expected = 'AnotherTest';
  1408. $this->assertEquals($expected, $result);
  1409. }
  1410. /**
  1411. * testWithAssociation method
  1412. *
  1413. * @return void
  1414. */
  1415. public function testWithAssociation() {
  1416. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  1417. $TestModel = new Something();
  1418. $result = $TestModel->SomethingElse->find('all');
  1419. $expected = array(
  1420. array(
  1421. 'SomethingElse' => array(
  1422. 'id' => '1',
  1423. 'title' => 'First Post',
  1424. 'body' => 'First Post Body',
  1425. 'published' => 'Y',
  1426. 'created' => '2007-03-18 10:39:23',
  1427. 'updated' => '2007-03-18 10:41:31'
  1428. ),
  1429. 'Something' => array(
  1430. array(
  1431. 'id' => '3',
  1432. 'title' => 'Third Post',
  1433. 'body' => 'Third Post Body',
  1434. 'published' => 'Y',
  1435. 'created' => '2007-03-18 10:43:23',
  1436. 'updated' => '2007-03-18 10:45:31',
  1437. 'JoinThing' => array(
  1438. 'id' => '3',
  1439. 'something_id' => '3',
  1440. 'something_else_id' => '1',
  1441. 'doomed' => true,
  1442. 'created' => '2007-03-18 10:43:23',
  1443. 'updated' => '2007-03-18 10:45:31'
  1444. )))),
  1445. array(
  1446. 'SomethingElse' => array(
  1447. 'id' => '2',
  1448. 'title' => 'Second Post',
  1449. 'body' => 'Second Post Body',
  1450. 'published' => 'Y',
  1451. 'created' => '2007-03-18 10:41:23',
  1452. 'updated' => '2007-03-18 10:43:31'
  1453. ),
  1454. 'Something' => array(
  1455. array(
  1456. 'id' => '1',
  1457. 'title' => 'First Post',
  1458. 'body' => 'First Post Body',
  1459. 'published' => 'Y',
  1460. 'created' => '2007-03-18 10:39:23',
  1461. 'updated' => '2007-03-18 10:41:31',
  1462. 'JoinThing' => array(
  1463. 'id' => '1',
  1464. 'something_id' => '1',
  1465. 'something_else_id' => '2',
  1466. 'doomed' => true,
  1467. 'created' => '2007-03-18 10:39:23',
  1468. 'updated' => '2007-03-18 10:41:31'
  1469. )))),
  1470. array(
  1471. 'SomethingElse' => array(
  1472. 'id' => '3',
  1473. 'title' => 'Third Post',
  1474. 'body' => 'Third Post Body',
  1475. 'published' => 'Y',
  1476. 'created' => '2007-03-18 10:43:23',
  1477. 'updated' => '2007-03-18 10:45:31'
  1478. ),
  1479. 'Something' => array(
  1480. array(
  1481. 'id' => '2',
  1482. 'title' => 'Second Post',
  1483. 'body' => 'Second Post Body',
  1484. 'published' => 'Y',
  1485. 'created' => '2007-03-18 10:41:23',
  1486. 'updated' => '2007-03-18 10:43:31',
  1487. 'JoinThing' => array(
  1488. 'id' => '2',
  1489. 'something_id' => '2',
  1490. 'something_else_id' => '3',
  1491. 'doomed' => false,
  1492. 'created' => '2007-03-18 10:41:23',
  1493. 'updated' => '2007-03-18 10:43:31'
  1494. )))));
  1495. $this->assertEquals($expected, $result);
  1496. $result = $TestModel->find('all');
  1497. $expected = array(
  1498. array(
  1499. 'Something' => array(
  1500. 'id' => '1',
  1501. 'title' => 'First Post',
  1502. 'body' => 'First Post Body',
  1503. 'published' => 'Y',
  1504. 'created' => '2007-03-18 10:39:23',
  1505. 'updated' => '2007-03-18 10:41:31'
  1506. ),
  1507. 'SomethingElse' => array(
  1508. array(
  1509. 'id' => '2',
  1510. 'title' => 'Second Post',
  1511. 'body' => 'Second Post Body',
  1512. 'published' => 'Y',
  1513. 'created' => '2007-03-18 10:41:23',
  1514. 'updated' => '2007-03-18 10:43:31',
  1515. 'JoinThing' => array(
  1516. 'doomed' => true,
  1517. 'something_id' => '1',
  1518. 'something_else_id' => '2'
  1519. )))),
  1520. array(
  1521. 'Something' => array(
  1522. 'id' => '2',
  1523. 'title' => 'Second Post',
  1524. 'body' => 'Second Post Body',
  1525. 'published' => 'Y',
  1526. 'created' => '2007-03-18 10:41:23',
  1527. 'updated' => '2007-03-18 10:43:31'
  1528. ),
  1529. 'SomethingElse' => array(
  1530. array(
  1531. 'id' => '3',
  1532. 'title' => 'Third Post',
  1533. 'body' => 'Third Post Body',
  1534. 'published' => 'Y',
  1535. 'created' => '2007-03-18 10:43:23',
  1536. 'updated' => '2007-03-18 10:45:31',
  1537. 'JoinThing' => array(
  1538. 'doomed' => false,
  1539. 'something_id' => '2',
  1540. 'something_else_id' => '3'
  1541. )))),
  1542. array(
  1543. 'Something' => array(
  1544. 'id' => '3',
  1545. 'title' => 'Third Post',
  1546. 'body' => 'Third Post Body',
  1547. 'published' => 'Y',
  1548. 'created' => '2007-03-18 10:43:23',
  1549. 'updated' => '2007-03-18 10:45:31'
  1550. ),
  1551. 'SomethingElse' => array(
  1552. array(
  1553. 'id' => '1',
  1554. 'title' => 'First Post',
  1555. 'body' => 'First Post Body',
  1556. 'published' => 'Y',
  1557. 'created' => '2007-03-18 10:39:23',
  1558. 'updated' => '2007-03-18 10:41:31',
  1559. 'JoinThing' => array(
  1560. 'doomed' => true,
  1561. 'something_id' => '3',
  1562. 'something_else_id' => '1'
  1563. )))));
  1564. $this->assertEquals($expected, $result);
  1565. $result = $TestModel->findById(1);
  1566. $expected = array(
  1567. 'Something' => array(
  1568. 'id' => '1',
  1569. 'title' => 'First Post',
  1570. 'body' => 'First Post Body',
  1571. 'published' => 'Y',
  1572. 'created' => '2007-03-18 10:39:23',
  1573. 'updated' => '2007-03-18 10:41:31'
  1574. ),
  1575. 'SomethingElse' => array(
  1576. array(
  1577. 'id' => '2',
  1578. 'title' => 'Second Post',
  1579. 'body' => 'Second Post Body',
  1580. 'published' => 'Y',
  1581. 'created' => '2007-03-18 10:41:23',
  1582. 'updated' => '2007-03-18 10:43:31',
  1583. 'JoinThing' => array(
  1584. 'doomed' => true,
  1585. 'something_id' => '1',
  1586. 'something_else_id' => '2'
  1587. ))));
  1588. $this->assertEquals($expected, $result);
  1589. $expected = $TestModel->findById(1);
  1590. $TestModel->set($expected);
  1591. $TestModel->save();
  1592. $result = $TestModel->findById(1);
  1593. $this->assertEquals($expected, $result);
  1594. $TestModel->hasAndBelongsToMany['SomethingElse']['unique'] = false;
  1595. $TestModel->create(array(
  1596. 'Something' => array('id' => 1),
  1597. 'SomethingElse' => array(3, array(
  1598. 'something_else_id' => 1,
  1599. 'doomed' => true
  1600. ))));
  1601. $ts = date('Y-m-d H:i:s');
  1602. $TestModel->save();
  1603. $TestModel->hasAndBelongsToMany['SomethingElse']['order'] = 'SomethingElse.id ASC';
  1604. $result = $TestModel->findById(1);
  1605. $expected = array(
  1606. 'Something' => array(
  1607. 'id' => '1',
  1608. 'title' => 'First Post',
  1609. 'body' => 'First Post Body',
  1610. 'published' => 'Y',
  1611. 'created' => '2007-03-18 10:39:23'
  1612. ),
  1613. 'SomethingElse' => array(
  1614. array(
  1615. 'id' => '1',
  1616. 'title' => 'First Post',
  1617. 'body' => 'First Post Body',
  1618. 'published' => 'Y',
  1619. 'created' => '2007-03-18 10:39:23',
  1620. 'updated' => '2007-03-18 10:41:31',
  1621. 'JoinThing' => array(
  1622. 'doomed' => true,
  1623. 'something_id' => '1',
  1624. 'something_else_id' => '1'
  1625. )
  1626. ),
  1627. array(
  1628. 'id' => '2',
  1629. 'title' => 'Second Post',
  1630. 'body' => 'Second Post Body',
  1631. 'published' => 'Y',
  1632. 'created' => '2007-03-18 10:41:23',
  1633. 'updated' => '2007-03-18 10:43:31',
  1634. 'JoinThing' => array(
  1635. 'doomed' => true,
  1636. 'something_id' => '1',
  1637. 'something_else_id' => '2'
  1638. )
  1639. ),
  1640. array(
  1641. 'id' => '3',
  1642. 'title' => 'Third Post',
  1643. 'body' => 'Third Post Body',
  1644. 'published' => 'Y',
  1645. 'created' => '2007-03-18 10:43:23',
  1646. 'updated' => '2007-03-18 10:45:31',
  1647. 'JoinThing' => array(
  1648. 'doomed' => false,
  1649. 'something_id' => '1',
  1650. 'something_else_id' => '3')
  1651. )
  1652. )
  1653. );
  1654. $this->assertTrue($result['Something']['updated'] >= $ts);
  1655. unset($result['Something']['updated']);
  1656. $this->assertEquals($expected, $result);
  1657. }
  1658. /**
  1659. * testFindSelfAssociations method
  1660. *
  1661. * @return void
  1662. */
  1663. public function testFindSelfAssociations() {
  1664. $this->loadFixtures('Person');
  1665. $TestModel = new Person();
  1666. $TestModel->recursive = 2;
  1667. $result = $TestModel->read(null, 1);
  1668. $expected = array(
  1669. 'Person' => array(
  1670. 'id' => 1,
  1671. 'name' => 'person',
  1672. 'mother_id' => 2,
  1673. 'father_id' => 3
  1674. ),
  1675. 'Mother' => array(
  1676. 'id' => 2,
  1677. 'name' => 'mother',
  1678. 'mother_id' => 4,
  1679. 'father_id' => 5,
  1680. 'Mother' => array(
  1681. 'id' => 4,
  1682. 'name' => 'mother - grand mother',
  1683. 'mother_id' => 0,
  1684. 'father_id' => 0
  1685. ),
  1686. 'Father' => array(
  1687. 'id' => 5,
  1688. 'name' => 'mother - grand father',
  1689. 'mother_id' => 0,
  1690. 'father_id' => 0
  1691. )),
  1692. 'Father' => array(
  1693. 'id' => 3,
  1694. 'name' => 'father',
  1695. 'mother_id' => 6,
  1696. 'father_id' => 7,
  1697. 'Father' => array(
  1698. 'id' => 7,
  1699. 'name' => 'father - grand father',
  1700. 'mother_id' => 0,
  1701. 'father_id' => 0
  1702. ),
  1703. 'Mother' => array(
  1704. 'id' => 6,
  1705. 'name' => 'father - grand mother',
  1706. 'mother_id' => 0,
  1707. 'father_id' => 0
  1708. )));
  1709. $this->assertEquals($expected, $result);
  1710. $TestModel->recursive = 3;
  1711. $result = $TestModel->read(null, 1);
  1712. $expected = array(
  1713. 'Person' => array(
  1714. 'id' => 1,
  1715. 'name' => 'person',
  1716. 'mother_id' => 2,
  1717. 'father_id' => 3
  1718. ),
  1719. 'Mother' => array(
  1720. 'id' => 2,
  1721. 'name' => 'mother',
  1722. 'mother_id' => 4,
  1723. 'father_id' => 5,
  1724. 'Mother' => array(
  1725. 'id' => 4,
  1726. 'name' => 'mother - grand mother',
  1727. 'mother_id' => 0,
  1728. 'father_id' => 0,
  1729. 'Mother' => array(),
  1730. 'Father' => array()),
  1731. 'Father' => array(
  1732. 'id' => 5,
  1733. 'name' => 'mother - grand father',
  1734. 'mother_id' => 0,
  1735. 'father_id' => 0,
  1736. 'Father' => array(),
  1737. 'Mother' => array()
  1738. )),
  1739. 'Father' => array(
  1740. 'id' => 3,
  1741. 'name' => 'father',
  1742. 'mother_id' => 6,
  1743. 'father_id' => 7,
  1744. 'Father' => array(
  1745. 'id' => 7,
  1746. 'name' => 'father - grand father',
  1747. 'mother_id' => 0,
  1748. 'father_id' => 0,
  1749. 'Father' => array(),
  1750. 'Mother' => array()
  1751. ),
  1752. 'Mother' => array(
  1753. 'id' => 6,
  1754. 'name' => 'father - grand mother',
  1755. 'mother_id' => 0,
  1756. 'father_id' => 0,
  1757. 'Mother' => array(),
  1758. 'Father' => array()
  1759. )));
  1760. $this->assertEquals($expected, $result);
  1761. }
  1762. /**
  1763. * testDynamicAssociations method
  1764. *
  1765. * @return void
  1766. */
  1767. public function testDynamicAssociations() {
  1768. $this->loadFixtures('Article', 'Comment');
  1769. $TestModel = new Article();
  1770. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->hasOne = array();
  1771. $TestModel->hasMany['Comment'] = array_merge($TestModel->hasMany['Comment'], array(
  1772. 'foreignKey' => false,
  1773. 'conditions' => array('Comment.user_id =' => '2')
  1774. ));
  1775. $result = $TestModel->find('all');
  1776. $expected = array(
  1777. array(
  1778. 'Article' => array(
  1779. 'id' => '1',
  1780. 'user_id' => '1',
  1781. 'title' => 'First Article',
  1782. 'body' => 'First Article Body',
  1783. 'published' => 'Y',
  1784. 'created' => '2007-03-18 10:39:23',
  1785. 'updated' => '2007-03-18 10:41:31'
  1786. ),
  1787. 'Comment' => array(
  1788. array(
  1789. 'id' => '1',
  1790. 'article_id' => '1',
  1791. 'user_id' => '2',
  1792. 'comment' => 'First Comment for First Article',
  1793. 'published' => 'Y',
  1794. 'created' => '2007-03-18 10:45:23',
  1795. 'updated' => '2007-03-18 10:47:31'
  1796. ),
  1797. array(
  1798. 'id' => '6',
  1799. 'article_id' => '2',
  1800. 'user_id' => '2',
  1801. 'comment' => 'Second Comment for Second Article',
  1802. 'published' => 'Y',
  1803. 'created' => '2007-03-18 10:55:23',
  1804. 'updated' => '2007-03-18 10:57:31'
  1805. ))),
  1806. array(
  1807. 'Article' => array(
  1808. 'id' => '2',
  1809. 'user_id' => '3',
  1810. 'title' => 'Second Article',
  1811. 'body' => 'Second Article Body',
  1812. 'published' => 'Y',
  1813. 'created' => '2007-03-18 10:41:23',
  1814. 'updated' => '2007-03-18 10:43:31'
  1815. ),
  1816. 'Comment' => array(
  1817. array(
  1818. 'id' => '1',
  1819. 'article_id' => '1',
  1820. 'user_id' => '2',
  1821. 'comment' => 'First Comment for First Article',
  1822. 'published' => 'Y',
  1823. 'created' => '2007-03-18 10:45:23',
  1824. 'updated' => '2007-03-18 10:47:31'
  1825. ),
  1826. array(
  1827. 'id' => '6',
  1828. 'article_id' => '2',
  1829. 'user_id' => '2',
  1830. 'comment' => 'Second Comment for Second Article',
  1831. 'published' => 'Y',
  1832. 'created' => '2007-03-18 10:55:23',
  1833. 'updated' => '2007-03-18 10:57:31'
  1834. ))),
  1835. array(
  1836. 'Article' => array(
  1837. 'id' => '3',
  1838. 'user_id' => '1',
  1839. 'title' => 'Third Article',
  1840. 'body' => 'Third Article Body',
  1841. 'published' => 'Y',
  1842. 'created' => '2007-03-18 10:43:23',
  1843. 'updated' => '2007-03-18 10:45:31'
  1844. ),
  1845. 'Comment' => array(
  1846. array(
  1847. 'id' => '1',
  1848. 'article_id' => '1',
  1849. 'user_id' => '2',
  1850. 'comment' => 'First Comment for First Article',
  1851. 'published' => 'Y',
  1852. 'created' => '2007-03-18 10:45:23',
  1853. 'updated' => '2007-03-18 10:47:31'
  1854. ),
  1855. array(
  1856. 'id' => '6',
  1857. 'article_id' => '2',
  1858. 'user_id' => '2',
  1859. 'comment' => 'Second Comment for Second Article',
  1860. 'published' => 'Y',
  1861. 'created' => '2007-03-18 10:55:23',
  1862. 'updated' => '2007-03-18 10:57:31'
  1863. ))));
  1864. $this->assertEquals($expected, $result);
  1865. }
  1866. /**
  1867. * testCreation method
  1868. *
  1869. * @return void
  1870. */
  1871. public function testCreation() {
  1872. $this->loadFixtures('Article', 'ArticleFeaturedsTags', 'User', 'Featured');
  1873. $TestModel = new Test();
  1874. $result = $TestModel->create();
  1875. $expected = array('Test' => array('notes' => 'write some notes here'));
  1876. $this->assertEquals($expected, $result);
  1877. $TestModel = new User();
  1878. $result = $TestModel->schema();
  1879. if (isset($this->db->columns['primary_key']['length'])) {
  1880. $intLength = $this->db->columns['primary_key']['length'];
  1881. } elseif (isset($this->db->columns['integer']['length'])) {
  1882. $intLength = $this->db->columns['integer']['length'];
  1883. } else {
  1884. $intLength = 11;
  1885. }
  1886. foreach (array('collate', 'charset', 'comment') as $type) {
  1887. foreach ($result as $i => $r) {
  1888. unset($result[$i][$type]);
  1889. }
  1890. }
  1891. $expected = array(
  1892. 'id' => array(
  1893. 'type' => 'integer',
  1894. 'null' => false,
  1895. 'default' => null,
  1896. 'length' => $intLength,
  1897. 'key' => 'primary'
  1898. ),
  1899. 'user' => array(
  1900. 'type' => 'string',
  1901. 'null' => false,
  1902. 'default' => '',
  1903. 'length' => 255
  1904. ),
  1905. 'password' => array(
  1906. 'type' => 'string',
  1907. 'null' => false,
  1908. 'default' => '',
  1909. 'length' => 255
  1910. ),
  1911. 'created' => array(
  1912. 'type' => 'datetime',
  1913. 'null' => true,
  1914. 'default' => null,
  1915. 'length' => null
  1916. ),
  1917. 'updated' => array(
  1918. 'type' => 'datetime',
  1919. 'null' => true,
  1920. 'default' => null,
  1921. 'length' => null
  1922. ));
  1923. $this->assertEquals($expected, $result);
  1924. $TestModel = new Article();
  1925. $result = $TestModel->create();
  1926. $expected = array('Article' => array('published' => 'N'));
  1927. $this->assertEquals($expected, $result);
  1928. $FeaturedModel = new Featured();
  1929. $data = array(
  1930. 'article_featured_id' => 1,
  1931. 'category_id' => 1,
  1932. 'published_date' => array(
  1933. 'year' => 2008,
  1934. 'month' => 06,
  1935. 'day' => 11
  1936. ),
  1937. 'end_date' => array(
  1938. 'year' => 2008,
  1939. 'month' => 06,
  1940. 'day' => 20
  1941. ));
  1942. $expected = array(
  1943. 'Featured' => array(
  1944. 'article_featured_id' => 1,
  1945. 'category_id' => 1,
  1946. 'published_date' => '2008-06-11 00:00:00',
  1947. 'end_date' => '2008-06-20 00:00:00'
  1948. ));
  1949. $this->assertEquals($FeaturedModel->create($data), $expected);
  1950. $data = array(
  1951. 'published_date' => array(
  1952. 'year' => 2008,
  1953. 'month' => 06,
  1954. 'day' => 11
  1955. ),
  1956. 'end_date' => array(
  1957. 'year' => 2008,
  1958. 'month' => 06,
  1959. 'day' => 20
  1960. ),
  1961. 'article_featured_id' => 1,
  1962. 'category_id' => 1
  1963. );
  1964. $expected = array(
  1965. 'Featured' => array(
  1966. 'published_date' => '2008-06-11 00:00:00',
  1967. 'end_date' => '2008-06-20 00:00:00',
  1968. 'article_featured_id' => 1,
  1969. 'category_id' => 1
  1970. ));
  1971. $this->assertEquals($FeaturedModel->create($data), $expected);
  1972. }
  1973. /**
  1974. * testEscapeField to prove it escapes the field well even when it has part of the alias on it
  1975. * @see ttp://cakephp.lighthouseapp.com/projects/42648-cakephp-1x/tickets/473-escapefield-doesnt-consistently-prepend-modelname
  1976. *
  1977. * @return void
  1978. */
  1979. public function testEscapeField() {
  1980. $TestModel = new Test();
  1981. $db = $TestModel->getDataSource();
  1982. $result = $TestModel->escapeField('test_field');
  1983. $expected = $db->name('Test.test_field');
  1984. $this->assertEquals($expected, $result);
  1985. $result = $TestModel->escapeField('TestField');
  1986. $expected = $db->name('Test.TestField');
  1987. $this->assertEquals($expected, $result);
  1988. $result = $TestModel->escapeField('DomainHandle', 'Domain');
  1989. $expected = $db->name('Domain.DomainHandle');
  1990. $this->assertEquals($expected, $result);
  1991. ConnectionManager::create('mock', array('datasource' => 'DboMock'));
  1992. $TestModel->setDataSource('mock');
  1993. $db = $TestModel->getDataSource();
  1994. $result = $TestModel->escapeField('DomainHandle', 'Domain');
  1995. $expected = $db->name('Domain.DomainHandle');
  1996. $this->assertEquals($expected, $result);
  1997. ConnectionManager::drop('mock');
  1998. }
  1999. /**
  2000. * test that model->hasMethod checks self and behaviors.
  2001. *
  2002. * @return void
  2003. */
  2004. public function testHasMethod() {
  2005. $Article = new Article();
  2006. $Article->Behaviors = $this->getMock('BehaviorCollection');
  2007. $Article->Behaviors->expects($this->at(0))
  2008. ->method('hasMethod')
  2009. ->will($this->returnValue(true));
  2010. $Article->Behaviors->expects($this->at(1))
  2011. ->method('hasMethod')
  2012. ->will($this->returnValue(false));
  2013. $this->assertTrue($Article->hasMethod('find'));
  2014. $this->assertTrue($Article->hasMethod('pass'));
  2015. $this->assertFalse($Article->hasMethod('fail'));
  2016. }
  2017. /**
  2018. * testMultischemaFixture
  2019. *
  2020. * @return void
  2021. */
  2022. public function testMultischemaFixture() {
  2023. $config = new DATABASE_CONFIG();
  2024. $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with Sqlite.');
  2025. $this->skipIf(!isset($config->test) || !isset($config->test2),
  2026. 'Primary and secondary test databases not configured, skipping cross-database join tests. To run these tests define $test and $test2 in your database configuration.'
  2027. );
  2028. $this->loadFixtures('Player', 'Guild', 'GuildsPlayer');
  2029. $Player = ClassRegistry::init('Player');
  2030. $this->assertEqual($Player->useDbConfig, 'test');
  2031. $this->assertEqual($Player->Guild->useDbConfig, 'test');
  2032. $this->assertEqual($Player->Guild->GuildsPlayer->useDbConfig, 'test2');
  2033. $this->assertEqual($Player->GuildsPlayer->useDbConfig, 'test2');
  2034. $players = $Player->find('all', array('recursive' => -1));
  2035. $guilds = $Player->Guild->find('all', array('recursive' => -1));
  2036. $guildsPlayers = $Player->GuildsPlayer->find('all', array('recursive' => -1));
  2037. $this->assertEqual(true, count($players) > 1);
  2038. $this->assertEqual(true, count($guilds) > 1);
  2039. $this->assertEqual(true, count($guildsPlayers) > 1);
  2040. }
  2041. /**
  2042. * testMultischemaFixtureWithThreeDatabases, three databases
  2043. *
  2044. * @return void
  2045. */
  2046. public function testMultischemaFixtureWithThreeDatabases() {
  2047. $config = new DATABASE_CONFIG();
  2048. $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with Sqlite.');
  2049. $this->skipIf(
  2050. !isset($config->test) || !isset($config->test2) || !isset($config->test_database_three),
  2051. 'Primary, secondary, and tertiary test databases not configured, skipping test. To run this test define $test, $test2, and $test_database_three in your database configuration.'
  2052. );
  2053. $this->loadFixtures('Player', 'Guild', 'GuildsPlayer', 'Armor', 'ArmorsPlayer');
  2054. $Player = ClassRegistry::init('Player');
  2055. $Player->bindModel(array(
  2056. 'hasAndBelongsToMany' => array(
  2057. 'Armor' => array(
  2058. 'with' => 'ArmorsPlayer',
  2059. ),
  2060. ),
  2061. ), false);
  2062. $this->assertEqual('test', $Player->useDbConfig);
  2063. $this->assertEqual('test', $Player->Guild->useDbConfig);
  2064. $this->assertEqual('test2', $Player->Guild->GuildsPlayer->useDbConfig);
  2065. $this->assertEqual('test2', $Player->GuildsPlayer->useDbConfig);
  2066. $this->assertEqual('test2', $Player->Armor->useDbConfig);
  2067. $this->assertEqual('test_database_three', $Player->Armor->ArmorsPlayer->useDbConfig);
  2068. $this->assertEqual('test', $Player->getDataSource()->configKeyName);
  2069. $this->assertEqual('test', $Player->Guild->getDataSource()->configKeyName);
  2070. $this->assertEqual('test2', $Player->GuildsPlayer->getDataSource()->configKeyName);
  2071. $this->assertEqual('test2', $Player->Armor->getDataSource()->configKeyName);
  2072. $this->assertEqual('test_database_three', $Player->Armor->ArmorsPlayer->getDataSource()->configKeyName);
  2073. $players = $Player->find('all', array('recursive' => -1));
  2074. $guilds = $Player->Guild->find('all', array('recursive' => -1));
  2075. $guildsPlayers = $Player->GuildsPlayer->find('all', array('recursive' => -1));
  2076. $armorsPlayers = $Player->ArmorsPlayer->find('all', array('recursive' => -1));
  2077. $this->assertEqual(true, count($players) > 1);
  2078. $this->assertEqual(true, count($guilds) > 1);
  2079. $this->assertEqual(true, count($guildsPlayers) > 1);
  2080. $this->assertEqual(true, count($armorsPlayers) > 1);
  2081. }
  2082. }