ModelIntegrationTest.php 69 KB

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