PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/cake/tests/cases/libs/model/model.test.php

https://github.com/hardsshah/bookmarks
PHP | 6524 lines | 5039 code | 628 blank | 857 comment | 51 complexity | 8267a8c60898d113179a90770fcd8b4a MD5 | raw file

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

  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * ModelTest file
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake
  20. * @subpackage cake.tests.cases.libs.model
  21. * @since CakePHP(tm) v 1.2.0.4206
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. App::import('Core', array('AppModel', 'Model'));
  28. require_once dirname(__FILE__) . DS . 'models.php';
  29. /**
  30. * ModelTest
  31. *
  32. * @package cake
  33. * @subpackage cake.tests.cases.libs.model
  34. */
  35. class ModelTest extends CakeTestCase {
  36. /**
  37. * autoFixtures property
  38. *
  39. * @var bool false
  40. * @access public
  41. */
  42. var $autoFixtures = false;
  43. /**
  44. * fixtures property
  45. *
  46. * @var array
  47. * @access public
  48. */
  49. var $fixtures = array(
  50. 'core.category', 'core.category_thread', 'core.user', 'core.my_category', 'core.my_product',
  51. 'core.my_user', 'core.my_categories_my_users', 'core.my_categories_my_products',
  52. 'core.article', 'core.featured', 'core.article_featureds_tags', 'core.article_featured',
  53. 'core.articles', 'core.numeric_article', 'core.tag', 'core.articles_tag', 'core.comment',
  54. 'core.attachment', 'core.apple', 'core.sample', 'core.another_article', 'core.item',
  55. 'core.advertisement', 'core.home', 'core.post', 'core.author', 'core.bid', 'core.portfolio',
  56. 'core.product', 'core.project', 'core.thread', 'core.message', 'core.items_portfolio',
  57. 'core.syfile', 'core.image', 'core.device_type', 'core.device_type_category',
  58. 'core.feature_set', 'core.exterior_type_category', 'core.document', 'core.device',
  59. 'core.document_directory', 'core.primary_model', 'core.secondary_model', 'core.something',
  60. 'core.something_else', 'core.join_thing', 'core.join_a', 'core.join_b', 'core.join_c',
  61. 'core.join_a_b', 'core.join_a_c', 'core.uuid', 'core.data_test', 'core.posts_tag',
  62. 'core.the_paper_monkies', 'core.person', 'core.underscore_field', 'core.node',
  63. 'core.dependency', 'core.story', 'core.stories_tag', 'core.cd', 'core.book', 'core.basket',
  64. 'core.overall_favorite', 'core.account', 'core.content', 'core.content_account',
  65. 'core.film_file', 'core.test_plugin_article', 'core.test_plugin_comment', 'core.uuiditem',
  66. 'core.counter_cache_user', 'core.counter_cache_post', 'core.uuidportfolio',
  67. 'core.uuiditems_uuidportfolio', 'core.uuiditems_uuidportfolio_numericid',
  68. 'core.fruit', 'core.fruits_uuid_tag', 'core.uuid_tag'
  69. );
  70. /**
  71. * start method
  72. *
  73. * @access public
  74. * @return void
  75. */
  76. function start() {
  77. parent::start();
  78. $this->debug = Configure::read('debug');
  79. Configure::write('debug', 2);
  80. }
  81. /**
  82. * end method
  83. *
  84. * @access public
  85. * @return void
  86. */
  87. function end() {
  88. parent::end();
  89. Configure::write('debug', $this->debug);
  90. }
  91. /**
  92. * endTest method
  93. *
  94. * @access public
  95. * @return void
  96. */
  97. function endTest() {
  98. ClassRegistry::flush();
  99. }
  100. /**
  101. * testAutoConstructAssociations method
  102. *
  103. * @access public
  104. * @return void
  105. */
  106. function testAutoConstructAssociations() {
  107. $this->loadFixtures('User', 'ArticleFeatured');
  108. $TestModel =& new AssociationTest1();
  109. $result = $TestModel->hasAndBelongsToMany;
  110. $expected = array('AssociationTest2' => array(
  111. 'unique' => false,
  112. 'joinTable' => 'join_as_join_bs',
  113. 'foreignKey' => false,
  114. 'className' => 'AssociationTest2',
  115. 'with' => 'JoinAsJoinB',
  116. 'associationForeignKey' => 'join_b_id',
  117. 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '',
  118. 'finderQuery' => '', 'deleteQuery' => '', 'insertQuery' => ''
  119. ));
  120. $this->assertEqual($result, $expected);
  121. // Tests related to ticket https://trac.cakephp.org/ticket/5594
  122. $TestModel =& new ArticleFeatured();
  123. $TestFakeModel =& new ArticleFeatured(array('table' => false));
  124. $expected = array(
  125. 'User' => array(
  126. 'className' => 'User', 'foreignKey' => 'user_id',
  127. 'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => ''
  128. ),
  129. 'Category' => array(
  130. 'className' => 'Category', 'foreignKey' => 'category_id',
  131. 'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => ''
  132. )
  133. );
  134. $this->assertIdentical($TestModel->belongsTo, $expected);
  135. $this->assertIdentical($TestFakeModel->belongsTo, $expected);
  136. $this->assertEqual($TestModel->User->name, 'User');
  137. $this->assertEqual($TestFakeModel->User->name, 'User');
  138. $this->assertEqual($TestModel->Category->name, 'Category');
  139. $this->assertEqual($TestFakeModel->Category->name, 'Category');
  140. $expected = array(
  141. 'Featured' => array(
  142. 'className' => 'Featured', 'foreignKey' => 'article_featured_id',
  143. 'conditions' => '', 'fields' => '', 'order' => '', 'dependent' => ''
  144. )
  145. );
  146. $this->assertIdentical($TestModel->hasOne, $expected);
  147. $this->assertIdentical($TestFakeModel->hasOne, $expected);
  148. $this->assertEqual($TestModel->Featured->name, 'Featured');
  149. $this->assertEqual($TestFakeModel->Featured->name, 'Featured');
  150. $expected = array(
  151. 'Comment' => array(
  152. 'className' => 'Comment', 'dependent' => true,
  153. 'foreignKey' => 'article_featured_id', 'conditions' => '', 'fields' => '',
  154. 'order' => '', 'limit' => '', 'offset' => '', 'exclusive' => '',
  155. 'finderQuery' => '', 'counterQuery' => ''
  156. )
  157. );
  158. $this->assertIdentical($TestModel->hasMany, $expected);
  159. $this->assertIdentical($TestFakeModel->hasMany, $expected);
  160. $this->assertEqual($TestModel->Comment->name, 'Comment');
  161. $this->assertEqual($TestFakeModel->Comment->name, 'Comment');
  162. $expected = array(
  163. 'Tag' => array(
  164. 'className' => 'Tag', 'joinTable' => 'article_featureds_tags',
  165. 'with' => 'ArticleFeaturedsTag', 'foreignKey' => 'article_featured_id',
  166. 'associationForeignKey' => 'tag_id', 'conditions' => '', 'fields' => '',
  167. 'order' => '', 'limit' => '', 'offset' => '', 'unique' => true, 'finderQuery' => '',
  168. 'deleteQuery' => '', 'insertQuery' => ''
  169. )
  170. );
  171. $this->assertIdentical($TestModel->hasAndBelongsToMany, $expected);
  172. $this->assertIdentical($TestFakeModel->hasAndBelongsToMany, $expected);
  173. $this->assertEqual($TestModel->Tag->name, 'Tag');
  174. $this->assertEqual($TestFakeModel->Tag->name, 'Tag');
  175. }
  176. /**
  177. * test Model::__construct
  178. *
  179. * ensure that $actsAS and $_findMethods are merged.
  180. *
  181. * @return void
  182. **/
  183. function testConstruct() {
  184. $this->loadFixtures('Post', 'Comment');
  185. $TestModel =& ClassRegistry::init('MergeVarPluginPost');
  186. $this->assertEqual($TestModel->actsAs, array('Containable', 'Tree'));
  187. $this->assertTrue(isset($TestModel->Behaviors->Containable));
  188. $this->assertTrue(isset($TestModel->Behaviors->Tree));
  189. $TestModel =& ClassRegistry::init('MergeVarPluginComment');
  190. $expected = array('Containable', 'Containable' => array('some_settings'));
  191. $this->assertEqual($TestModel->actsAs, $expected);
  192. $this->assertTrue(isset($TestModel->Behaviors->Containable));
  193. }
  194. /**
  195. * testColumnTypeFetching method
  196. *
  197. * @access public
  198. * @return void
  199. */
  200. function testColumnTypeFetching() {
  201. $model =& new Test();
  202. $this->assertEqual($model->getColumnType('id'), 'integer');
  203. $this->assertEqual($model->getColumnType('notes'), 'text');
  204. $this->assertEqual($model->getColumnType('updated'), 'datetime');
  205. $this->assertEqual($model->getColumnType('unknown'), null);
  206. $model =& new Article();
  207. $this->assertEqual($model->getColumnType('User.created'), 'datetime');
  208. $this->assertEqual($model->getColumnType('Tag.id'), 'integer');
  209. $this->assertEqual($model->getColumnType('Article.id'), 'integer');
  210. }
  211. /**
  212. * testMultipleBelongsToWithSameClass method
  213. *
  214. * @access public
  215. * @return void
  216. */
  217. function testMultipleBelongsToWithSameClass() {
  218. $this->loadFixtures(
  219. 'DeviceType', 'DeviceTypeCategory', 'FeatureSet', 'ExteriorTypeCategory',
  220. 'Document', 'Device', 'DocumentDirectory'
  221. );
  222. $DeviceType =& new DeviceType();
  223. $DeviceType->recursive = 2;
  224. $result = $DeviceType->read(null, 1);
  225. $expected = array(
  226. 'DeviceType' => array(
  227. 'id' => 1, 'device_type_category_id' => 1, 'feature_set_id' => 1,
  228. 'exterior_type_category_id' => 1, 'image_id' => 1, 'extra1_id' => 1,
  229. 'extra2_id' => 1, 'name' => 'DeviceType 1', 'order' => 0
  230. ),
  231. 'Image' => array('id' => 1, 'document_directory_id' => 1, 'name' => 'Document 1',
  232. 'DocumentDirectory' => array('id' => 1, 'name' => 'DocumentDirectory 1')),
  233. 'Extra1' => array(
  234. 'id' => 1, 'document_directory_id' => 1, 'name' => 'Document 1',
  235. 'DocumentDirectory' => array('id' => 1, 'name' => 'DocumentDirectory 1')
  236. ),
  237. 'Extra2' => array(
  238. 'id' => 1, 'document_directory_id' => 1, 'name' => 'Document 1',
  239. 'DocumentDirectory' => array('id' => 1, 'name' => 'DocumentDirectory 1')
  240. ),
  241. 'DeviceTypeCategory' => array('id' => 1, 'name' => 'DeviceTypeCategory 1'),
  242. 'FeatureSet' => array('id' => 1, 'name' => 'FeatureSet 1'),
  243. 'ExteriorTypeCategory' => array(
  244. 'id' => 1, 'image_id' => 1, 'name' => 'ExteriorTypeCategory 1',
  245. 'Image' => array('id' => 1, 'device_type_id' => 1, 'name' => 'Device 1', 'typ' => 1)
  246. ),
  247. 'Device' => array(
  248. array('id' => 1, 'device_type_id' => 1, 'name' => 'Device 1', 'typ' => 1),
  249. array('id' => 2, 'device_type_id' => 1, 'name' => 'Device 2', 'typ' => 1),
  250. array('id' => 3, 'device_type_id' => 1, 'name' => 'Device 3', 'typ' => 2)
  251. )
  252. );
  253. $this->assertEqual($result, $expected);
  254. }
  255. /**
  256. * testHabtmRecursiveBelongsTo method
  257. *
  258. * @access public
  259. * @return void
  260. */
  261. function testHabtmRecursiveBelongsTo() {
  262. $this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio', 'Syfile', 'Image');
  263. $Portfolio =& new Portfolio();
  264. $result = $Portfolio->find(array('id' => 2), null, null, 3);
  265. $expected = array('Portfolio' => array(
  266. 'id' => 2, 'seller_id' => 1, 'name' => 'Portfolio 2'),
  267. 'Item' => array(
  268. array(
  269. 'id' => 2, 'syfile_id' => 2, 'published' => 0, 'name' => 'Item 2',
  270. 'ItemsPortfolio' => array('id' => 2, 'item_id' => 2, 'portfolio_id' => 2),
  271. 'Syfile' => array(
  272. 'id' => 2, 'image_id' => 2, 'name' => 'Syfile 2', 'item_count' => null,
  273. 'Image' => array('id' => 2, 'name' => 'Image 2'
  274. ))
  275. ),
  276. array(
  277. 'id' => 6, 'syfile_id' => 6, 'published' => 0, 'name' => 'Item 6',
  278. 'ItemsPortfolio' => array('id' => 6, 'item_id' => 6, 'portfolio_id' => 2),
  279. 'Syfile' => array(
  280. 'id' => 6, 'image_id' => null, 'name' => 'Syfile 6', 'item_count' => null,
  281. 'Image' => array()
  282. )
  283. )
  284. ));
  285. $this->assertEqual($result, $expected);
  286. }
  287. /**
  288. * testHabtmUuidWithUuidId method
  289. *
  290. * @access public
  291. * @return void
  292. */
  293. function testHabtmUuidWithUuidId() {
  294. $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolio');
  295. $TestModel =& new Uuidportfolio();
  296. $data = array('Uuidportfolio' => array('name' => 'Portfolio 3'));
  297. $data['Uuiditem']['Uuiditem'] = array('483798c8-c7cc-430e-8cf9-4fcc40cf8569');
  298. $TestModel->create($data);
  299. $TestModel->save();
  300. $id = $TestModel->id;
  301. $result = $TestModel->read(null, $id);
  302. $this->assertEqual(1, count($result['Uuiditem']));
  303. $this->assertEqual(strlen($result['Uuiditem'][0]['UuiditemsUuidportfolio']['id']), 36);
  304. }
  305. /**
  306. * test HABTM saving when join table has no primary key and only 2 columns.
  307. *
  308. * @return void
  309. **/
  310. function testHabtmSavingWithNoPrimaryKeyUuidJoinTable() {
  311. $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
  312. $Fruit =& new Fruit();
  313. $data = array(
  314. 'Fruit' => array(
  315. 'color' => 'Red',
  316. 'shape' => 'Heart-shaped',
  317. 'taste' => 'sweet',
  318. 'name' => 'Strawberry',
  319. ),
  320. 'UuidTag' => array(
  321. 'UuidTag' => array(
  322. '481fc6d0-b920-43e0-e50f-6d1740cf8569'
  323. )
  324. )
  325. );
  326. $this->assertTrue($Fruit->save($data));
  327. }
  328. /**
  329. * test HABTM saving when join table has no primary key and only 2 columns, no with model is used.
  330. *
  331. * @return void
  332. **/
  333. function testHabtmSavingWithNoPrimaryKeyUuidJoinTableNoWith() {
  334. $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
  335. $Fruit =& new FruitNoWith();
  336. $data = array(
  337. 'Fruit' => array(
  338. 'color' => 'Red',
  339. 'shape' => 'Heart-shaped',
  340. 'taste' => 'sweet',
  341. 'name' => 'Strawberry',
  342. ),
  343. 'UuidTag' => array(
  344. 'UuidTag' => array(
  345. '481fc6d0-b920-43e0-e50f-6d1740cf8569'
  346. )
  347. )
  348. );
  349. $this->assertTrue($Fruit->save($data));
  350. }
  351. /**
  352. * testHabtmUuidWithNumericId method
  353. *
  354. * @access public
  355. * @return void
  356. */
  357. function testHabtmUuidWithNumericId() {
  358. $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolioNumericid');
  359. $TestModel =& new Uuiditem();
  360. $data = array('Uuiditem' => array('name' => 'Item 7', 'published' => 0));
  361. $data['Uuidportfolio']['Uuidportfolio'] = array('480af662-eb8c-47d3-886b-230540cf8569');
  362. $TestModel->create($data);
  363. $TestModel->save();
  364. $id = $TestModel->id;
  365. $result = $TestModel->read(null, $id);
  366. $this->assertEqual(1, count($result['Uuidportfolio']));
  367. }
  368. /**
  369. * testHabtmFinderQuery method
  370. *
  371. * @access public
  372. * @return void
  373. */
  374. function testHabtmFinderQuery() {
  375. $this->loadFixtures('Article', 'Tag', 'ArticlesTag');
  376. $Article =& new Article();
  377. $sql = $this->db->buildStatement(
  378. array(
  379. 'fields' => $this->db->fields($Article->Tag, null, array(
  380. 'Tag.id', 'Tag.tag', 'ArticlesTag.article_id', 'ArticlesTag.tag_id'
  381. )),
  382. 'table' => $this->db->fullTableName('tags'),
  383. 'alias' => 'Tag',
  384. 'limit' => null,
  385. 'offset' => null,
  386. 'group' => null,
  387. 'joins' => array(array(
  388. 'alias' => 'ArticlesTag',
  389. 'table' => $this->db->fullTableName('articles_tags'),
  390. 'conditions' => array(
  391. array("ArticlesTag.article_id" => '{$__cakeID__$}'),
  392. array("ArticlesTag.tag_id" => $this->db->identifier('Tag.id'))
  393. )
  394. )),
  395. 'conditions' => array(),
  396. 'order' => null
  397. ),
  398. $Article
  399. );
  400. $Article->hasAndBelongsToMany['Tag']['finderQuery'] = $sql;
  401. $result = $Article->find('first');
  402. $expected = array(array('id' => '1', 'tag' => 'tag1'), array('id' => '2', 'tag' => 'tag2'));
  403. $this->assertEqual($result['Tag'], $expected);
  404. }
  405. /**
  406. * testHabtmLimitOptimization method
  407. *
  408. * @access public
  409. * @return void
  410. */
  411. function testHabtmLimitOptimization() {
  412. $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
  413. $TestModel =& new Article();
  414. $TestModel->hasAndBelongsToMany['Tag']['limit'] = 2;
  415. $result = $TestModel->read(null, 2);
  416. $expected = array(
  417. 'Article' => array('id' => '2', 'user_id' => '3', 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'),
  418. 'User' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31'),
  419. 'Comment' => array(
  420. array('id' => '5', 'article_id' => '2', 'user_id' => '1', 'comment' => 'First Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:53:23', 'updated' => '2007-03-18 10:55:31'),
  421. array('id' => '6', 'article_id' => '2', 'user_id' => '2', 'comment' => 'Second Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:55:23', 'updated' => '2007-03-18 10:57:31')
  422. ),
  423. 'Tag' => array(
  424. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  425. array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')
  426. )
  427. );
  428. $this->assertEqual($result, $expected);
  429. $TestModel->hasAndBelongsToMany['Tag']['limit'] = 1;
  430. $result = $TestModel->read(null, 2);
  431. unset($expected['Tag'][1]);
  432. $this->assertEqual($result, $expected);
  433. }
  434. /**
  435. * testHabtmUniqueKey method
  436. *
  437. * @access public
  438. * @return void
  439. */
  440. function testHabtmUniqueKey() {
  441. $model =& new Item();
  442. $this->assertFalse($model->hasAndBelongsToMany['Portfolio']['unique']);
  443. }
  444. /**
  445. * testHasManyLimitOptimization method
  446. *
  447. * @access public
  448. * @return void
  449. */
  450. function testHasManyLimitOptimization() {
  451. $this->loadFixtures('Project', 'Thread', 'Message', 'Bid');
  452. $Project =& new Project();
  453. $Project->recursive = 3;
  454. $result = $Project->find('all');
  455. $expected = array(
  456. array('Project' => array('id' => 1, 'name' => 'Project 1'),
  457. 'Thread' => array(
  458. array(
  459. 'id' => 1, 'project_id' => 1, 'name' => 'Project 1, Thread 1',
  460. 'Project' => array(
  461. 'id' => 1, 'name' => 'Project 1',
  462. 'Thread' => array(
  463. array('id' => 1, 'project_id' => 1, 'name' => 'Project 1, Thread 1'),
  464. array('id' => 2, 'project_id' => 1, 'name' => 'Project 1, Thread 2')
  465. )
  466. ),
  467. 'Message' => array(
  468. array(
  469. 'id' => 1, 'thread_id' => 1, 'name' => 'Thread 1, Message 1',
  470. 'Bid' => array('id' => 1, 'message_id' => 1, 'name' => 'Bid 1.1')
  471. )
  472. )
  473. ),
  474. array(
  475. 'id' => 2, 'project_id' => 1, 'name' => 'Project 1, Thread 2',
  476. 'Project' => array(
  477. 'id' => 1, 'name' => 'Project 1',
  478. 'Thread' => array(
  479. array('id' => 1, 'project_id' => 1, 'name' => 'Project 1, Thread 1'),
  480. array('id' => 2, 'project_id' => 1, 'name' => 'Project 1, Thread 2')
  481. )
  482. ),
  483. 'Message' => array(
  484. array(
  485. 'id' => 2, 'thread_id' => 2, 'name' => 'Thread 2, Message 1',
  486. 'Bid' => array('id' => 4, 'message_id' => 2, 'name' => 'Bid 2.1')
  487. )
  488. )
  489. )
  490. )
  491. ),
  492. array('Project' => array('id' => 2, 'name' => 'Project 2'),
  493. 'Thread' => array(
  494. array(
  495. 'id' => 3, 'project_id' => 2, 'name' => 'Project 2, Thread 1',
  496. 'Project' => array(
  497. 'id' => 2, 'name' => 'Project 2',
  498. 'Thread' => array(
  499. array('id' => 3, 'project_id' => 2, 'name' => 'Project 2, Thread 1'),
  500. )
  501. ),
  502. 'Message' => array(
  503. array(
  504. 'id' => 3, 'thread_id' => 3, 'name' => 'Thread 3, Message 1',
  505. 'Bid' => array('id' => 3, 'message_id' => 3, 'name' => 'Bid 3.1')
  506. )
  507. )
  508. )
  509. )
  510. ),
  511. array('Project' => array('id' => 3, 'name' => 'Project 3'),
  512. 'Thread' => array()
  513. )
  514. );
  515. $this->assertEqual($result, $expected);
  516. }
  517. /**
  518. * testWithAssociation method
  519. *
  520. * @access public
  521. * @return void
  522. */
  523. function testWithAssociation() {
  524. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  525. $TestModel =& new Something();
  526. $result = $TestModel->SomethingElse->find('all');
  527. $expected = array(
  528. array('SomethingElse' => array('id' => '1', 'title' => 'First Post', 'body' => 'First Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
  529. 'Something' => array(array('id' => '3', 'title' => 'Third Post', 'body' => 'Third Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31',
  530. 'JoinThing' => array('id' => '3', 'something_id' => '3', 'something_else_id' => '1', 'doomed' => '1', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31')))),
  531. array('SomethingElse' => array('id' => '2', 'title' => 'Second Post', 'body' => 'Second Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'),
  532. 'Something' => array(array('id' => '1', 'title' => 'First Post', 'body' => 'First Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31',
  533. 'JoinThing' => array('id' => '1', 'something_id' => '1', 'something_else_id' => '2', 'doomed' => '1', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31')))),
  534. array('SomethingElse' => array('id' => '3', 'title' => 'Third Post', 'body' => 'Third Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'),
  535. 'Something' => array (array('id' => '2', 'title' => 'Second Post', 'body' => 'Second Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31',
  536. 'JoinThing' => array('id' => '2', 'something_id' => '2', 'something_else_id' => '3', 'doomed' => '0', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31')))));
  537. $this->assertEqual($result, $expected);
  538. $result = $TestModel->find('all');
  539. $expected = array(
  540. array('Something' => array('id' => '1', 'title' => 'First Post', 'body' => 'First Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
  541. 'SomethingElse' => array(
  542. array('id' => '2', 'title' => 'Second Post', 'body' => 'Second Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31',
  543. 'JoinThing' => array('doomed' => '1', 'something_id' => '1', 'something_else_id' => '2')))),
  544. array('Something' => array('id' => '2', 'title' => 'Second Post', 'body' => 'Second Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'),
  545. 'SomethingElse' => array(
  546. array('id' => '3', 'title' => 'Third Post', 'body' => 'Third Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31',
  547. 'JoinThing' => array('doomed' => '0', 'something_id' => '2', 'something_else_id' => '3')))),
  548. array('Something' => array('id' => '3', 'title' => 'Third Post', 'body' => 'Third Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'),
  549. 'SomethingElse' => array(
  550. array('id' => '1', 'title' => 'First Post', 'body' => 'First Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31',
  551. 'JoinThing' => array('doomed' => '1', 'something_id' => '3', 'something_else_id' => '1')))));
  552. $this->assertEqual($result, $expected);
  553. $result = $TestModel->findById(1);
  554. $expected = array(
  555. 'Something' => array('id' => '1', 'title' => 'First Post', 'body' => 'First Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
  556. 'SomethingElse' => array(array('id' => '2', 'title' => 'Second Post', 'body' => 'Second Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31',
  557. 'JoinThing' => array('doomed' => '1', 'something_id' => '1', 'something_else_id' => '2'))));
  558. $this->assertEqual($result, $expected);
  559. $expected = $TestModel->findById(1);
  560. $TestModel->set($expected);
  561. $TestModel->save();
  562. $result = $TestModel->findById(1);
  563. $this->assertEqual($result, $expected);
  564. $TestModel->hasAndBelongsToMany['SomethingElse']['unique'] = false;
  565. $TestModel->create(array(
  566. 'Something' => array('id' => 1),
  567. 'SomethingElse' => array(3, array('something_else_id' => 1, 'doomed' => '1'))
  568. ));
  569. $ts = date('Y-m-d H:i:s');
  570. $TestModel->save();
  571. $TestModel->hasAndBelongsToMany['SomethingElse']['order'] = 'SomethingElse.id ASC';
  572. $result = $TestModel->findById(1);
  573. $expected = array(
  574. 'Something' => array('id' => '1', 'title' => 'First Post', 'body' => 'First Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => $ts),
  575. 'SomethingElse' => array(
  576. array('id' => '1', 'title' => 'First Post', 'body' => 'First Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31',
  577. 'JoinThing' => array('doomed' => '1', 'something_id' => '1', 'something_else_id' => '1')),
  578. array('id' => '2', 'title' => 'Second Post', 'body' => 'Second Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31',
  579. 'JoinThing' => array('doomed' => '1', 'something_id' => '1', 'something_else_id' => '2')),
  580. array('id' => '3', 'title' => 'Third Post', 'body' => 'Third Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31',
  581. 'JoinThing' => array('doomed' => '0', 'something_id' => '1', 'something_else_id' => '3'))));
  582. $this->assertEqual($result, $expected);
  583. }
  584. /**
  585. * testDynamicAssociations method
  586. *
  587. * @access public
  588. * @return void
  589. */
  590. function testDynamicAssociations() {
  591. $this->loadFixtures('Article', 'Comment');
  592. $TestModel =& new Article();
  593. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->hasOne = array();
  594. $TestModel->hasMany['Comment'] = array_merge($TestModel->hasMany['Comment'], array(
  595. 'foreignKey' => false,
  596. 'conditions' => array('Comment.user_id =' => '2')
  597. ));
  598. $result = $TestModel->find('all');
  599. $expected = array(
  600. array(
  601. 'Article' => array('id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
  602. 'Comment' => array(
  603. array('id' => '1', 'article_id' => '1', 'user_id' => '2', 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31'),
  604. array('id' => '6', 'article_id' => '2', 'user_id' => '2', 'comment' => 'Second Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:55:23', 'updated' => '2007-03-18 10:57:31')
  605. )
  606. ),
  607. array(
  608. 'Article' => array('id' => '2', 'user_id' => '3', 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'),
  609. 'Comment' => array(
  610. array('id' => '1', 'article_id' => '1', 'user_id' => '2', 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31'),
  611. array('id' => '6', 'article_id' => '2', 'user_id' => '2', 'comment' => 'Second Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:55:23', 'updated' => '2007-03-18 10:57:31')
  612. )
  613. ),
  614. array(
  615. 'Article' => array('id' => '3', 'user_id' => '1', 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'),
  616. 'Comment' => array(
  617. array('id' => '1', 'article_id' => '1', 'user_id' => '2', 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31'),
  618. array('id' => '6', 'article_id' => '2', 'user_id' => '2', 'comment' => 'Second Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:55:23', 'updated' => '2007-03-18 10:57:31')
  619. )
  620. )
  621. );
  622. $this->assertEqual($result, $expected);
  623. }
  624. /**
  625. * testSaveMultipleHabtm method
  626. *
  627. * @access public
  628. * @return void
  629. */
  630. function testSaveMultipleHabtm() {
  631. $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC');
  632. $TestModel = new JoinA();
  633. $result = $TestModel->findById(1);
  634. $expected = array(
  635. 'JoinA' => array('id' => 1, 'name' => 'Join A 1', 'body' => 'Join A 1 Body', 'created' => '2008-01-03 10:54:23', 'updated' => '2008-01-03 10:54:23'),
  636. 'JoinB' => array(
  637. 0 => array('id' => 2, 'name' => 'Join B 2', 'created' => '2008-01-03 10:55:02', 'updated' => '2008-01-03 10:55:02',
  638. 'JoinAsJoinB' => array('id' => 1, 'join_a_id' => 1, 'join_b_id' => 2, 'other' => 'Data for Join A 1 Join B 2', 'created' => '2008-01-03 10:56:33', 'updated' => '2008-01-03 10:56:33'))),
  639. 'JoinC' => array(
  640. 0 => array('id' => 2, 'name' => 'Join C 2', 'created' => '2008-01-03 10:56:12', 'updated' => '2008-01-03 10:56:12',
  641. 'JoinAsJoinC' => array('id' => 1, 'join_a_id' => 1, 'join_c_id' => 2, 'other' => 'Data for Join A 1 Join C 2', 'created' => '2008-01-03 10:57:22', 'updated' => '2008-01-03 10:57:22'))));
  642. $this->assertEqual($result, $expected);
  643. $ts = date('Y-m-d H:i:s');
  644. $TestModel->id = 1;
  645. $data = array(
  646. 'JoinA' => array('id' => '1', 'name' => 'New name for Join A 1', 'updated' => $ts),
  647. 'JoinB' => array(array('id' => 1, 'join_b_id' => 2, 'other' => 'New data for Join A 1 Join B 2', 'created' => $ts, 'updated' => $ts)),
  648. 'JoinC' => array(array('id' => 1, 'join_c_id' => 2, 'other' => 'New data for Join A 1 Join C 2', 'created' => $ts, 'updated' => $ts)));
  649. $TestModel->set($data);
  650. $TestModel->save();
  651. $result = $TestModel->findById(1);
  652. $expected = array(
  653. 'JoinA' => array('id' => 1, 'name' => 'New name for Join A 1', 'body' => 'Join A 1 Body', 'created' => '2008-01-03 10:54:23', 'updated' => $ts),
  654. 'JoinB' => array(
  655. 0 => array('id' => 2, 'name' => 'Join B 2', 'created' => '2008-01-03 10:55:02', 'updated' => '2008-01-03 10:55:02',
  656. 'JoinAsJoinB' => array('id' => 1, 'join_a_id' => 1, 'join_b_id' => 2, 'other' => 'New data for Join A 1 Join B 2', 'created' => $ts, 'updated' => $ts))),
  657. 'JoinC' => array(
  658. 0 => array('id' => 2, 'name' => 'Join C 2', 'created' => '2008-01-03 10:56:12', 'updated' => '2008-01-03 10:56:12',
  659. 'JoinAsJoinC' => array('id' => 1, 'join_a_id' => 1, 'join_c_id' => 2, 'other' => 'New data for Join A 1 Join C 2', 'created' => $ts, 'updated' => $ts))));
  660. $this->assertEqual($result, $expected);
  661. }
  662. /**
  663. * testFindAllRecursiveSelfJoin method
  664. *
  665. * @access public
  666. * @return void
  667. */
  668. function testFindAllRecursiveSelfJoin() {
  669. $this->loadFixtures('Home', 'AnotherArticle', 'Advertisement');
  670. $TestModel =& new Home();
  671. $TestModel->recursive = 2;
  672. $result = $TestModel->find('all');
  673. $expected = array(array('Home' => array(
  674. 'id' => '1', 'another_article_id' => '1', 'advertisement_id' => '1', 'title' => 'First Home', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
  675. 'AnotherArticle' => array('id' => '1', 'title' => 'First Article', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31',
  676. 'Home' => array(array('id' => '1', 'another_article_id' => '1', 'advertisement_id' => '1', 'title' => 'First Home', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'))),
  677. 'Advertisement' => array('id' => '1', 'title' => 'First Ad', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31',
  678. 'Home' => array(array('id' => '1', 'another_article_id' => '1', 'advertisement_id' => '1', 'title' => 'First Home', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
  679. array('id' => '2', 'another_article_id' => '3', 'advertisement_id' => '1', 'title' => 'Second Home', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31')))),
  680. array('Home' => array(
  681. 'id' => '2', 'another_article_id' => '3', 'advertisement_id' => '1', 'title' => 'Second Home', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'),
  682. 'AnotherArticle' => array('id' => '3', 'title' => 'Third Article', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31',
  683. 'Home' => array(array('id' => '2', 'another_article_id' => '3', 'advertisement_id' => '1', 'title' => 'Second Home', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'))),
  684. 'Advertisement' => array('id' => '1', 'title' => 'First Ad', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31',
  685. 'Home' => array(array('id' => '1', 'another_article_id' => '1', 'advertisement_id' => '1', 'title' => 'First Home', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
  686. array('id' => '2', 'another_article_id' => '3', 'advertisement_id' => '1', 'title' => 'Second Home', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31')))));
  687. $this->assertEqual($result, $expected);
  688. }
  689. /**
  690. * testFindAllRecursiveWithHabtm method
  691. *
  692. * @return void
  693. * @access public
  694. */
  695. function testFindAllRecursiveWithHabtm() {
  696. $this->loadFixtures(
  697. 'MyCategoriesMyUsers', 'MyCategoriesMyProducts', 'MyCategory', 'MyUser', 'MyProduct'
  698. );
  699. $MyUser =& new MyUser();
  700. $MyUser->recursive = 2;
  701. $result = $MyUser->find('all');
  702. $expected = array(
  703. array(
  704. 'MyUser' => array('id' => '1', 'firstname' => 'userA'),
  705. 'MyCategory' => array(
  706. array(
  707. 'id' => '1',
  708. 'name' => 'A',
  709. 'MyProduct' => array(
  710. array('id' => '1', 'name' => 'book')
  711. )
  712. ),
  713. array(
  714. 'id' => '3',
  715. 'name' => 'C',
  716. 'MyProduct' => array(array('id' => '2', 'name' => 'computer'))
  717. )
  718. )
  719. ),
  720. array(
  721. 'MyUser' => array('id' => '2', 'firstname' => 'userB'),
  722. 'MyCategory' => array(
  723. array(
  724. 'id' => '1',
  725. 'name' => 'A',
  726. 'MyProduct' => array(array('id' => '1', 'name' => 'book'))
  727. ),
  728. array(
  729. 'id' => '2',
  730. 'name' => 'B',
  731. 'MyProduct' => array(
  732. array('id' => '1', 'name' => 'book'),
  733. array('id' => '2', 'name' => 'computer')
  734. )
  735. )
  736. )
  737. )
  738. );
  739. $this->assertIdentical($result, $expected);
  740. }
  741. /**
  742. * testFindSelfAssociations method
  743. *
  744. * @access public
  745. * @return void
  746. */
  747. function testFindSelfAssociations() {
  748. $this->loadFixtures('Person');
  749. $TestModel =& new Person();
  750. $TestModel->recursive = 2;
  751. $result = $TestModel->read(null, 1);
  752. $expected = array(
  753. 'Person' => array('id' => 1, 'name' => 'person', 'mother_id' => 2, 'father_id' => 3),
  754. 'Mother' => array('id' => 2, 'name' => 'mother', 'mother_id' => 4, 'father_id' => 5,
  755. 'Mother' => array('id' => 4, 'name' => 'mother - grand mother', 'mother_id' => 0, 'father_id' => 0),
  756. 'Father' => array('id' => 5, 'name' => 'mother - grand father', 'mother_id' => 0, 'father_id' => 0)),
  757. 'Father' => array('id' => 3, 'name' => 'father', 'mother_id' => 6, 'father_id' => 7,
  758. 'Father' => array('id' => 7, 'name' => 'father - grand father', 'mother_id' => 0, 'father_id' => 0),
  759. 'Mother' => array('id' => 6, 'name' => 'father - grand mother', 'mother_id' => 0, 'father_id' => 0)));
  760. $this->assertEqual($result, $expected);
  761. $TestModel->recursive = 3;
  762. $result = $TestModel->read(null, 1);
  763. $expected = array(
  764. 'Person' => array('id' => 1, 'name' => 'person', 'mother_id' => 2, 'father_id' => 3),
  765. 'Mother' => array('id' => 2, 'name' => 'mother', 'mother_id' => 4, 'father_id' => 5,
  766. 'Mother' => array('id' => 4, 'name' => 'mother - grand mother', 'mother_id' => 0, 'father_id' => 0,
  767. 'Mother' => array(),
  768. 'Father' => array()),
  769. 'Father' => array('id' => 5, 'name' => 'mother - grand father', 'mother_id' => 0, 'father_id' => 0,
  770. 'Father' => array(),
  771. 'Mother' => array())),
  772. 'Father' => array('id' => 3, 'name' => 'father', 'mother_id' => 6, 'father_id' => 7,
  773. 'Father' => array('id' => 7, 'name' => 'father - grand father', 'mother_id' => 0, 'father_id' => 0,
  774. 'Father' => array(),
  775. 'Mother' => array()),
  776. 'Mother' => array('id' => 6, 'name' => 'father - grand mother', 'mother_id' => 0, 'father_id' => 0,
  777. 'Mother' => array(),
  778. 'Father' => array())));
  779. $this->assertEqual($result, $expected);
  780. }
  781. /**
  782. * testPluginAssociations method
  783. *
  784. * @access public
  785. * @return void
  786. */
  787. function testPluginAssociations() {
  788. $this->loadFixtures('TestPluginArticle', 'User', 'TestPluginComment');
  789. $TestModel =& new TestPluginArticle();
  790. $result = $TestModel->find('all');
  791. $expected = array(
  792. array(
  793. 'TestPluginArticle' => array('id' => 1, 'user_id' => 1, 'title' => 'First Plugin Article', 'body' => 'First Plugin Article Body', 'published' => 'Y', 'created' => '2008-09-24 10:39:23', 'updated' => '2008-09-24 10:41:31'),
  794. 'User' => array('id' => 1, 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  795. 'TestPluginComment' => array(
  796. array('id' => 1, 'article_id' => 1, 'user_id' => 2, 'comment' => 'First Comment for First Plugin Article', 'published' => 'Y', 'created' => '2008-09-24 10:45:23', 'updated' => '2008-09-24 10:47:31'),
  797. array('id' => 2, 'article_id' => 1, 'user_id' => 4, 'comment' => 'Second Comment for First Plugin Article', 'published' => 'Y', 'created' => '2008-09-24 10:47:23', 'updated' => '2008-09-24 10:49:31'),
  798. array('id' => 3, 'article_id' => 1, 'user_id' => 1, 'comment' => 'Third Comment for First Plugin Article', 'published' => 'Y', 'created' => '2008-09-24 10:49:23', 'updated' => '2008-09-24 10:51:31'),
  799. array('id' => 4, 'article_id' => 1, 'user_id' => 1, 'comment' => 'Fourth Comment for First Plugin Article', 'published' => 'N', 'created' => '2008-09-24 10:51:23', 'updated' => '2008-09-24 10:53:31')
  800. )
  801. ),
  802. array(
  803. 'TestPluginArticle' => array('id' => 2, 'user_id' => 3, 'title' => 'Second Plugin Article', 'body' => 'Second Plugin Article Body', 'published' => 'Y', 'created' => '2008-09-24 10:41:23', 'updated' => '2008-09-24 10:43:31'),
  804. 'User' => array('id' => 3, 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31'),
  805. 'TestPluginComment' => array(
  806. array('id' => 5, 'article_id' => 2, 'user_id' => 1, 'comment' => 'First Comment for Second Plugin Article', 'published' => 'Y', 'created' => '2008-09-24 10:53:23', 'updated' => '2008-09-24 10:55:31'),
  807. array('id' => 6, 'article_id' => 2, 'user_id' => 2, 'comment' => 'Second Comment for Second Plugin Article', 'published' => 'Y', 'created' => '2008-09-24 10:55:23', 'updated' => '2008-09-24 10:57:31')
  808. )
  809. ),
  810. array(
  811. 'TestPluginArticle' => array('id' => 3,'user_id' => 1, 'title' => 'Third Plugin Article', 'body' => 'Third Plugin Article Body', 'published' => 'Y', 'created' => '2008-09-24 10:43:23', 'updated' => '2008-09-24 10:45:31'),
  812. 'User' => array('id' => 1, 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  813. 'TestPluginComment' => array()
  814. )
  815. );
  816. $this->assertEqual($result, $expected);
  817. }
  818. /**
  819. * testIdentity method
  820. *
  821. * @access public
  822. * @return void
  823. */
  824. function testIdentity() {
  825. $TestModel =& new Test();
  826. $result = $TestModel->alias;
  827. $expected = 'Test';
  828. $this->assertEqual($result, $expected);
  829. $TestModel =& new TestAlias();
  830. $result = $TestModel->alias;
  831. $expected = 'TestAlias';
  832. $this->assertEqual($result, $expected);
  833. $TestModel =& new Test(array('alias' => 'AnotherTest'));
  834. $result = $TestModel->alias;
  835. $expected = 'AnotherTest';
  836. $this->assertEqual($result, $expected);
  837. }
  838. /**
  839. * testCreation method
  840. *
  841. * @access public
  842. * @return void
  843. */
  844. function testCreation() {
  845. $this->loadFixtures('Article');
  846. $TestModel =& new Test();
  847. $result = $TestModel->create();
  848. $expected = array('Test' => array('notes' => 'write some notes here'));
  849. $this->assertEqual($result, $expected);
  850. $TestModel =& new User();
  851. $result = $TestModel->schema();
  852. if (isset($this->db->columns['primary_key']['length'])) {
  853. $intLength = $this->db->columns['primary_key']['length'];
  854. } elseif (isset($this->db->columns['integer']['length'])) {
  855. $intLength = $this->db->columns['integer']['length'];
  856. } else {
  857. $intLength = 11;
  858. }
  859. foreach (array('collate', 'charset') as $type) {
  860. unset($result['user'][$type]);
  861. unset($result['password'][$type]);
  862. }
  863. $expected = array(
  864. 'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => $intLength, 'key' => 'primary'),
  865. 'user' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => 255),
  866. 'password' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => 255),
  867. 'created' => array('type' => 'datetime', 'null' => true, 'default' => null, 'length' => null),
  868. 'updated'=> array('type' => 'datetime', 'null' => true, 'default' => null, 'length' => null));
  869. $this->assertEqual($result, $expected);
  870. $TestModel =& new Article();
  871. $result = $TestModel->create();
  872. $expected = array('Article' => array('published' => 'N'));
  873. $this->assertEqual($result, $expected);
  874. $FeaturedModel =& new Featured();
  875. $data = array(
  876. 'article_featured_id' => 1, 'category_id' => 1,
  877. 'published_date' => array('year' => 2008, 'month' => 06, 'day' => 11),
  878. 'end_date' => array('year' => 2008, 'month' => 06, 'day' => 20)
  879. );
  880. $expected = array('Featured' => array(
  881. 'article_featured_id' => 1, 'category_id' => 1,
  882. 'published_date' => '2008-6-11 00:00:00', 'end_date' => '2008-6-20 00:00:00'
  883. ));
  884. $this->assertEqual($FeaturedModel->create($data), $expected);
  885. $data = array(
  886. 'published_date' => array('year' => 2008, 'month' => 06, 'day' => 11),
  887. 'end_date' => array('year' => 2008, 'month' => 06, 'day' => 20),
  888. 'article_featured_id' => 1, 'category_id' => 1
  889. );
  890. $expected = array('Featured' => array(
  891. 'published_date' => '2008-6-11 00:00:00', 'end_date' => '2008-6-20 00:00:00',
  892. 'article_featured_id' => 1, 'category_id' => 1
  893. ));
  894. $this->assertEqual($FeaturedModel->create($data), $expected);
  895. }
  896. /**
  897. * ensure that __exists is reset on create
  898. *
  899. * @return void
  900. **/
  901. function testResetOfExistsOnCreate() {
  902. $this->loadFixtures('Article');
  903. $Article =& new Article();
  904. $Article->id = 1;
  905. $Article->saveField('title', 'Reset me');
  906. $Article->delete();
  907. $Article->id = 1;
  908. $this->assertFalse($Article->exists());
  909. $Article->create();
  910. $this->assertFalse($Article->exists());
  911. $Article->id = 2;
  912. $Article->saveField('title', 'Staying alive');
  913. $result = $Article->read(null, 2);
  914. $this->assertEqual($result['Article']['title'], 'Staying alive');
  915. }
  916. /**
  917. * testCreationOfEmptyRecord method
  918. *
  919. * @access public
  920. * @return void
  921. */
  922. function testCreationOfEmptyRecord() {
  923. $this->loadFixtures('Author');
  924. $TestModel =& new Author();
  925. $this->assertEqual($TestModel->find('count'), 4);
  926. $TestModel->deleteAll(true, false, false);
  927. $this->assertEqual($TestModel->find('count'), 0);
  928. $result = $TestModel->save();
  929. $this->assertTrue(isset($result['Author']['created']));
  930. $this->assertTrue(isset($result['Author']['updated']));
  931. $this->assertEqual($TestModel->find('count'), 1);
  932. }
  933. /**
  934. * testCreateWithPKFiltering method
  935. *
  936. * @access public
  937. * @return void
  938. */
  939. function testCreateWithPKFiltering() {
  940. $TestModel =& new Article();
  941. $data = array('id' => 5, 'user_id' => 2, 'title' => 'My article', 'body' => 'Some text');
  942. $result = $TestModel->create($data);
  943. $expected = array('Article' => array('published' => 'N', 'id' => 5, 'user_id' => 2, 'title' => 'My article', 'body' => 'Some text'));
  944. $this->assertEqual($result, $expected);
  945. $this->assertEqual($TestModel->id, 5);
  946. $result = $TestModel->create($data, true);
  947. $expected = array('Article' => array('published' => 'N', 'id' => false, 'user_id' => 2, 'title' => 'My article', 'body' => 'Some text'));
  948. $this->assertEqual($result, $expected);
  949. $this->assertFalse($TestModel->id);
  950. $result = $TestModel->create(array('Article' => $data), true);
  951. $expected = array('Article' => array('published' => 'N', 'id' => false, 'user_id' => 2, 'title' => 'My article', 'body' => 'Some text'));
  952. $this->assertEqual($result, $expected);
  953. $this->assertFalse($TestModel->id);
  954. $data = array('id' => 6, 'user_id' => 2, 'title' => 'My article', 'body' => 'Some text', 'created' => '1970-01-01 00:00:00', 'updated' => '1970-01-01 12:00:00', 'modified' => '1970-01-01 12:00:00');
  955. $result = $TestModel->create($data);
  956. $expected = array('Article' => array('published' => 'N', 'id' => 6, 'user_id' => 2, 'title' => 'My article', 'body' => 'Some text', 'created' => '1970-01-01 00:00:00', 'updated' => '1970-01-01 12:00:00', 'modified' => '1970-01-01 12:00:00'));
  957. $this->assertEqual($result, $expected);
  958. $this->assertEqual($TestModel->id, 6);
  959. $result = $TestModel->create(array('Article' => array_diff_key($data, array('created' => true, 'updated' => true, 'modified' => true))), true);
  960. $expected = array('Article' => array('published' => 'N', 'id' => false, 'user_id' => 2, 'title' => 'My article', 'body' => 'Some text'));
  961. $this->assertEqual($result, $expected);
  962. $this->assertFalse($TestModel->id);
  963. }
  964. /**
  965. * testCreationWithMultipleData method
  966. *
  967. * @access public
  968. * @return void
  969. */
  970. function testCreationWithMultipleData() {
  971. $this->loadFixtures('Article', 'Comment');
  972. $Article =& new Article();
  973. $Comment =& new Comment();
  974. $articles = $Article->find('all', array('fields' => array('id','title'), 'recursive' => -1));
  975. $comments = $Comment->find('all', array('fields' => array('id','article_id','user_id','comment','published'), 'recursive' => -1));
  976. $this->assertEqual($articles, array(
  977. array('Article' => array('id' => 1, 'title' => 'First Article')),
  978. array('Article' => array('id' => 2, 'title' => 'Second Article')),
  979. array('Article' => array('id' => 3, 'title' => 'Third Article'))));
  980. $this->assertEqual($comments, array(
  981. array('Comment' => array('id' => 1, 'article_id' => 1, 'user_id' => 2, 'comment' => 'First Comment for First Article', 'published' => 'Y')),
  982. array('Comment' => array('id' => 2, 'article_id' => 1, 'user_id' => 4, 'comment' => 'Second Comment for First Article', 'published' => 'Y')),
  983. array('Comment' => array('id' => 3, 'article_id' => 1, 'user_id' => 1, 'comment' => 'Third Comment for First Article', 'published' => 'Y')),
  984. array('Comment' => array('id' => 4, 'article_id' => 1, 'user_id' => 1, 'comment' => 'Fourth Comment for First Article', 'published' => 'N')),
  985. array('Comment' => array('id' => 5, 'article_id' => 2, 'user_id' => 1, 'comment' => 'First Comment for Second Article', 'published' => 'Y')),
  986. array('Comment' => array('id' => 6, 'article_id' => 2, 'user_id' => 2, 'comment' => 'Second Comment for Second Article', 'published' => 'Y'))));
  987. $data = array('Comment' => array('article_id' => 2, 'user_id' => 4, 'comment' => 'Brand New Comment', 'published' => 'N'),
  988. 'Article' => array('id' => 2, 'title' => 'Second Article Modified'));
  989. $result = $Comment->create($data);
  990. $this->assertTrue($result);
  991. $result = $Comment->save();
  992. $this->assertTrue($result);
  993. $articles = $Article->find('all', array('fields' => array('id','title'), 'recursive' => -1));
  994. $comments = $Comment->find('all', array('fields' => array('id','article_id','user_id','comment','published'), 'recursive' => -1));
  995. $this->assertEqual($articles, array(
  996. array('Article' => array('id' => 1, 'title' => 'First Article')),
  997. array('Article' => array('id' => 2, 'title' => 'Second Article')),
  998. array('Article' => array('id' => 3, 'title' => 'Third Article'))));
  999. $this->assertEqual($comments, array(
  1000. array('Comment' => array('id' => 1, 'article_id' => 1, 'user_id' => 2, 'comment' => 'First Comment for First Article', 'published' => 'Y')),
  1001. array('Comment' => array('id' => 2, 'article_id' => 1, 'user_id' => 4, 'comment' => 'Second Comment for First Article', 'published' => 'Y')),
  1002. array('Comment' => array('id' => 3, 'article_id' => 1, 'user_id' => 1, 'comment' => 'Third Comment for First Article', 'published' => 'Y')),
  1003. array('Comment' => array('id' => 4, 'article_id' => 1, 'user_id' => 1, 'comment' => 'Fourth Comment for First Article', 'published' => 'N')),
  1004. array('Comment' => array('id' => 5, 'article_id' => 2, 'user_id' => 1, 'comment' => 'First Comment for Second Article', 'published' => 'Y')),
  1005. array('Comment' => array('id' => 6, 'article_id' => 2, 'user_id' => 2, 'comment' => 'Second Comment for Second Article', 'published' => 'Y')),
  1006. array('Comment' => array('id' => 7, 'article_id' => 2, 'user_id' => 4, 'comment' => 'Brand New Comment', 'published' => 'N'))));
  1007. }
  1008. /**
  1009. * testCreationWithMultipleDataSameModel method
  1010. *
  1011. * @access public
  1012. * @return void
  1013. */
  1014. function testCreationWithMultipleDataSameModel() {
  1015. $this->loadFixtures('Article');
  1016. $Article =& new Article();
  1017. $SecondaryArticle =& new Article();
  1018. $result = $Article->field('title', array('id' => 1));
  1019. $this->assertEqual($result, 'First Article');
  1020. $data = array('Article' => array('user_id' => 2, 'title' => 'Brand New Article', 'body' => 'Brand New Article Body', 'published' => 'Y'),
  1021. 'SecondaryArticle' => array('id' => 1));
  1022. $Article->create();
  1023. $result = $Article->save($data);
  1024. $this->assertTrue($result);
  1025. $result = $Article->getInsertID();
  1026. $this->assertTrue(!empty($result));
  1027. $result = $Article->field('title', array('id' => 1));
  1028. $this->assertEqual($result, 'First Article');
  1029. $articles = $Article->find('all', array('fields' => array('id','title'), 'recursive' => -1));
  1030. $this->assertEqual($articles, array(
  1031. array('Article' => array('id' => 1, 'title' => 'First Article')),
  1032. array('Article' => array('id' => 2, 'title' => 'Second Article')),
  1033. array('Article' => array('id' => 3, 'title' => 'Third Article')),
  1034. array('Article' => array('id' => 4, 'title' => 'Brand New Article'))));
  1035. }
  1036. /**
  1037. * testCreationWithMultipleDataSameModelManualInstances method
  1038. *
  1039. * @access public
  1040. * @return void
  1041. */
  1042. function testCreationWithMultipleDataSameModelManualInstances() {
  1043. $this->loadFixtures('PrimaryModel');
  1044. $Primary =& new PrimaryModel();
  1045. $Secondary =& new PrimaryModel();
  1046. $result = $Primary->field('primary_name', array('id' => 1));
  1047. $this->assertEqual($result, 'Primary Name Existing');
  1048. $data = array('PrimaryModel' => array('primary_name' => 'Primary Name New'),
  1049. 'SecondaryModel' => array('id' => array(1)));
  1050. $Primary->create();
  1051. $result = $Primary->save($data);
  1052. $this->assertTrue($result);
  1053. $result = $Primary->field('primary_name', array('id' => 1));
  1054. $this->assertEqual($result, 'Primary Name Existing');
  1055. $result = $Primary->getInsertID();
  1056. $this->assertTrue(!empty($result));
  1057. $result = $Primary->field('primary_name', array('id' => $result));
  1058. $this->assertEqual($result, 'Primary Name New');
  1059. $result = $Primary->find('count');
  1060. $this->assertEqual($result, 2);
  1061. }
  1062. /**
  1063. * testReadFakeThread method
  1064. *
  1065. * @access public
  1066. * @return void
  1067. */
  1068. function testReadFakeThread() {
  1069. $this->loadFixtures('CategoryThre…

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