ModelIntegrationTest.php 66 KB

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