ModelIntegrationTest.php 57 KB

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