PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Test/Case/Model/ModelIntegrationTest.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 2111 lines | 1685 code | 183 blank | 243 comment | 4 complexity | 255071caa53206d9be67403cb4daa5ae MD5 | raw file

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file