PageRenderTime 104ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 2ms

/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
  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('CategoryThread');
  1070. $TestModel =& new CategoryThread();
  1071. $fullDebug = $this->db->fullDebug;
  1072. $this->db->fullDebug = true;
  1073. $TestModel->recursive = 6;
  1074. $TestModel->id = 7;
  1075. $result = $TestModel->read();
  1076. $expected = array(
  1077. 'CategoryThread' => array('id' => 7, 'parent_id' => 6, 'name' => 'Category 2.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'),
  1078. 'ParentCategory' => array('id' => 6, 'parent_id' => 5, 'name' => 'Category 2', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1079. 'ParentCategory' => array('id' => 5, 'parent_id' => 4, 'name' => 'Category 1.1.1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1080. 'ParentCategory' => array('id' => 4, 'parent_id' => 3, 'name' => 'Category 1.1.2', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1081. 'ParentCategory' => array('id' => 3, 'parent_id' => 2, 'name' => 'Category 1.1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1082. 'ParentCategory' => array('id' => 2, 'parent_id' => 1, 'name' => 'Category 1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1083. 'ParentCategory' => array('id' => 1, 'parent_id' => 0, 'name' => 'Category 1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31')))))
  1084. )
  1085. );
  1086. $this->db->fullDebug = $fullDebug;
  1087. $this->assertEqual($result, $expected);
  1088. }
  1089. /**
  1090. * testFindFakeThread method
  1091. *
  1092. * @access public
  1093. * @return void
  1094. */
  1095. function testFindFakeThread() {
  1096. $this->loadFixtures('CategoryThread');
  1097. $TestModel =& new CategoryThread();
  1098. $fullDebug = $this->db->fullDebug;
  1099. $this->db->fullDebug = true;
  1100. $TestModel->recursive = 6;
  1101. $result = $TestModel->find(array('CategoryThread.id' => 7));
  1102. $expected = array(
  1103. 'CategoryThread' => array('id' => 7, 'parent_id' => 6, 'name' => 'Category 2.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'),
  1104. 'ParentCategory' => array('id' => 6, 'parent_id' => 5, 'name' => 'Category 2', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1105. 'ParentCategory' => array('id' => 5, 'parent_id' => 4, 'name' => 'Category 1.1.1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1106. 'ParentCategory' => array('id' => 4, 'parent_id' => 3, 'name' => 'Category 1.1.2', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1107. 'ParentCategory' => array('id' => 3, 'parent_id' => 2, 'name' => 'Category 1.1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1108. 'ParentCategory' => array('id' => 2, 'parent_id' => 1, 'name' => 'Category 1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1109. 'ParentCategory' => array('id' => 1, 'parent_id' => 0, 'name' => 'Category 1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31')))))
  1110. )
  1111. );
  1112. $this->db->fullDebug = $fullDebug;
  1113. $this->assertEqual($result, $expected);
  1114. }
  1115. /**
  1116. * testFindAllFakeThread method
  1117. *
  1118. * @access public
  1119. * @return void
  1120. */
  1121. function testFindAllFakeThread() {
  1122. $this->loadFixtures('CategoryThread');
  1123. $TestModel =& new CategoryThread();
  1124. $fullDebug = $this->db->fullDebug;
  1125. $this->db->fullDebug = true;
  1126. $TestModel->recursive = 6;
  1127. $result = $TestModel->find('all', null, null, 'CategoryThread.id ASC');
  1128. $expected = array(
  1129. array('CategoryThread' => array('id' => 1, 'parent_id' => 0, 'name' => 'Category 1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'),
  1130. 'ParentCategory' => array('id' => null, 'parent_id' => null, 'name' => null, 'created' => null, 'updated' => null, 'ParentCategory' => array())),
  1131. array('CategoryThread' => array('id' => 2, 'parent_id' => 1, 'name' => 'Category 1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'),
  1132. 'ParentCategory' => array('id' => 1, 'parent_id' => 0, 'name' => 'Category 1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1133. 'ParentCategory' => array())),
  1134. array('CategoryThread' => array('id' => 3, 'parent_id' => 2, 'name' => 'Category 1.1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'),
  1135. 'ParentCategory' => array('id' => 2, 'parent_id' => 1, 'name' => 'Category 1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1136. 'ParentCategory' => array('id' => 1, 'parent_id' => 0, 'name' => 'Category 1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1137. 'ParentCategory' => array()))),
  1138. array('CategoryThread' => array('id' => 4, 'parent_id' => 3, 'name' => 'Category 1.1.2', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'),
  1139. 'ParentCategory' => array('id' => 3, 'parent_id' => 2, 'name' => 'Category 1.1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1140. 'ParentCategory' => array('id' => 2, 'parent_id' => 1, 'name' => 'Category 1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1141. 'ParentCategory' => array('id' => 1, 'parent_id' => 0, 'name' => 'Category 1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1142. 'ParentCategory' => array())))),
  1143. array('CategoryThread' => array('id' => 5, 'parent_id' => 4, 'name' => 'Category 1.1.1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'),
  1144. 'ParentCategory' => array('id' => 4, 'parent_id' => 3, 'name' => 'Category 1.1.2', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1145. 'ParentCategory' => array('id' => 3, 'parent_id' => 2, 'name' => 'Category 1.1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1146. 'ParentCategory' => array('id' => 2, 'parent_id' => 1, 'name' => 'Category 1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1147. 'ParentCategory' => array('id' => 1, 'parent_id' => 0, 'name' => 'Category 1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1148. 'ParentCategory' => array()))))),
  1149. array('CategoryThread' => array('id' => 6, 'parent_id' => 5, 'name' => 'Category 2', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'),
  1150. 'ParentCategory' => array('id' => 5, 'parent_id' => 4, 'name' => 'Category 1.1.1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1151. 'ParentCategory' => array('id' => 4, 'parent_id' => 3, 'name' => 'Category 1.1.2', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1152. 'ParentCategory' => array('id' => 3, 'parent_id' => 2, 'name' => 'Category 1.1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1153. 'ParentCategory' => array('id' => 2, 'parent_id' => 1, 'name' => 'Category 1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1154. 'ParentCategory' => array('id' => 1, 'parent_id' => 0, 'name' => 'Category 1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1155. 'ParentCategory' => array())))))),
  1156. array('CategoryThread' => array('id' => 7, 'parent_id' => 6, 'name' => 'Category 2.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'),
  1157. 'ParentCategory' => array('id' => 6, 'parent_id' => 5, 'name' => 'Category 2', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1158. 'ParentCategory' => array('id' => 5, 'parent_id' => 4, 'name' => 'Category 1.1.1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1159. 'ParentCategory' => array('id' => 4, 'parent_id' => 3, 'name' => 'Category 1.1.2', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1160. 'ParentCategory' => array('id' => 3, 'parent_id' => 2, 'name' => 'Category 1.1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1161. 'ParentCategory' => array('id' => 2, 'parent_id' => 1, 'name' => 'Category 1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31',
  1162. 'ParentCategory' => array('id' => 1, 'parent_id' => 0, 'name' => 'Category 1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'))))))));
  1163. $this->db->fullDebug = $fullDebug;
  1164. $this->assertEqual($result, $expected);
  1165. }
  1166. /**
  1167. * testConditionalNumerics method
  1168. *
  1169. * @access public
  1170. * @return void
  1171. */
  1172. function testConditionalNumerics() {
  1173. $this->loadFixtures('NumericArticle');
  1174. $NumericArticle =& new NumericArticle();
  1175. $data = array('title' => '12345abcde');
  1176. $result = $NumericArticle->find($data);
  1177. $this->assertTrue(!empty($result));
  1178. // @TODO: make this pass in Cake 2.0 with passing the column around in db->value()
  1179. // SELECT * from articles WHERE title = 12345 // will find the article with title = 12345abcde, too : /
  1180. // $data = array('title' => '12345');
  1181. // $result = $NumericArticle->find($data);
  1182. // $this->assertTrue(empty($result));
  1183. }
  1184. /**
  1185. * test find('all') method
  1186. *
  1187. * @access public
  1188. * @return void
  1189. */
  1190. function testFindAll() {
  1191. $this->loadFixtures('User');
  1192. $TestModel =& new User();
  1193. $TestModel->cacheQueries = false;
  1194. $result = $TestModel->find('all');
  1195. $expected = array(
  1196. array('User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31')),
  1197. array('User' => array('id' => '2', 'user' => 'nate', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:18:23', 'updated' => '2007-03-17 01:20:31')),
  1198. array('User' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31')),
  1199. array('User' => array('id' => '4', 'user' => 'garrett', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:22:23', 'updated' => '2007-03-17 01:24:31')));
  1200. $this->assertEqual($result, $expected);
  1201. $result = $TestModel->find('all', array('conditions' => 'User.id > 2'));
  1202. $expected = array(
  1203. array('User' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31')),
  1204. array('User' => array('id' => '4', 'user' => 'garrett', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:22:23', 'updated' => '2007-03-17 01:24:31')));
  1205. $this->assertEqual($result, $expected);
  1206. $result = $TestModel->find('all', array('conditions' => array('User.id !=' => '0', 'User.user LIKE' => '%arr%')));
  1207. $expected = array(
  1208. array('User' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31')),
  1209. array('User' => array('id' => '4', 'user' => 'garrett', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:22:23', 'updated' => '2007-03-17 01:24:31')));
  1210. $this->assertEqual($result, $expected);
  1211. $result = $TestModel->find('all', array('conditions' => array('User.id' => '0')));
  1212. $expected = array();
  1213. $this->assertEqual($result, $expected);
  1214. $result = $TestModel->find('all', array('conditions' => array('or' => array('User.id' => '0', 'User.user LIKE' => '%a%'))));
  1215. $expected = array(
  1216. array('User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31')),
  1217. array('User' => array('id' => '2', 'user' => 'nate', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:18:23', 'updated' => '2007-03-17 01:20:31')),
  1218. array('User' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31')),
  1219. array('User' => array('id' => '4', 'user' => 'garrett', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:22:23', 'updated' => '2007-03-17 01:24:31')));
  1220. $this->assertEqual($result, $expected);
  1221. $result = $TestModel->find('all', array('fields' => 'User.id, User.user'));
  1222. $expected = array(
  1223. array('User' => array('id' => '1', 'user' => 'mariano')),
  1224. array('User' => array('id' => '2', 'user' => 'nate')),
  1225. array('User' => array('id' => '3', 'user' => 'larry')),
  1226. array('User' => array('id' => '4', 'user' => 'garrett')));
  1227. $this->assertEqual($result, $expected);
  1228. $result = $TestModel->find('all', array('fields' => 'User.user', 'order' => 'User.user ASC'));
  1229. $expected = array(
  1230. array('User' => array('user' => 'garrett')),
  1231. array('User' => array('user' => 'larry')),
  1232. array('User' => array('user' => 'mariano')),
  1233. array('User' => array('user' => 'nate')));
  1234. $this->assertEqual($result, $expected);
  1235. $result = $TestModel->find('all', array('fields' => 'User.user', 'order' => 'User.user DESC'));
  1236. $expected = array(
  1237. array('User' => array('user' => 'nate')),
  1238. array('User' => array('user' => 'mariano')),
  1239. array('User' => array('user' => 'larry')),
  1240. array('User' => array('user' => 'garrett')));
  1241. $this->assertEqual($result, $expected);
  1242. $result = $TestModel->find('all', array('limit' => 3, 'page' => 1));
  1243. $expected = array(
  1244. array('User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31')),
  1245. array('User' => array('id' => '2', 'user' => 'nate', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:18:23', 'updated' => '2007-03-17 01:20:31')),
  1246. array('User' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31')));
  1247. $this->assertEqual($result, $expected);
  1248. $ids = array(4 => 1, 5 => 3);
  1249. $result = $TestModel->find('all', array('conditions' => array('User.id' => $ids)));
  1250. $expected = array(
  1251. array('User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31')),
  1252. array('User' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31')),
  1253. );
  1254. $this->assertEqual($result, $expected);
  1255. // These tests are expected to fail on SQL Server since the LIMIT/OFFSET
  1256. // hack can't handle small record counts.
  1257. if ($this->db->config['driver'] != 'mssql') {
  1258. $result = $TestModel->find('all', array('limit' => 3, 'page' => 2));
  1259. $expected = array(
  1260. array('User' => array('id' => '4', 'user' => 'garrett', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:22:23', 'updated' => '2007-03-17 01:24:31')));
  1261. $this->assertEqual($result, $expected);
  1262. $result = $TestModel->find('all', array('limit' => 3, 'page' => 3));
  1263. $expected = array();
  1264. $this->assertEqual($result, $expected);
  1265. }
  1266. }
  1267. /**
  1268. * test find('list') method
  1269. *
  1270. * @access public
  1271. * @return void
  1272. */
  1273. function testGenerateFindList() {
  1274. $this->loadFixtures('Article', 'Apple', 'Post', 'Author', 'User');
  1275. $TestModel =& new Article();
  1276. $TestModel->displayField = 'title';
  1277. $result = $TestModel->find('list', array('order' => 'Article.title ASC'));
  1278. $expected = array(1 => 'First Article', 2 => 'Second Article', 3 => 'Third Article');
  1279. $this->assertEqual($result, $expected);
  1280. $db =& ConnectionManager::getDataSource('test_suite');
  1281. if ($db->config['driver'] == 'mysql') {
  1282. $result = $TestModel->find('list', array('order' => array('FIELD(Article.id, 3, 2) ASC', 'Article.title ASC')));
  1283. $expected = array(1 => 'First Article', 3 => 'Third Article', 2 => 'Second Article');
  1284. $this->assertEqual($result, $expected);
  1285. }
  1286. $result = Set::combine($TestModel->find('all', array('order' => 'Article.title ASC', 'fields' => array('id', 'title'))), '{n}.Article.id', '{n}.Article.title');
  1287. $expected = array(1 => 'First Article', 2 => 'Second Article', 3 => 'Third Article');
  1288. $this->assertEqual($result, $expected);
  1289. $result = Set::combine($TestModel->find('all', array('order' => 'Article.title ASC')), '{n}.Article.id', '{n}.Article');
  1290. $expected = array(
  1291. 1 => 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'),
  1292. 2 => 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'),
  1293. 3 => 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'));
  1294. $this->assertEqual($result, $expected);
  1295. $result = Set::combine($TestModel->find('all', array('order' => 'Article.title ASC')), '{n}.Article.id', '{n}.Article', '{n}.Article.user_id');
  1296. $expected = array(1 => array(
  1297. 1 => 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'),
  1298. 3 => 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')),
  1299. 3 => array(2 => 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')));
  1300. $this->assertEqual($result, $expected);
  1301. $result = Set::combine($TestModel->find('all', array('order' => 'Article.title ASC', 'fields' => array('id', 'title', 'user_id'))), '{n}.Article.id', '{n}.Article.title', '{n}.Article.user_id');
  1302. $expected = array(1 => array(1 => 'First Article', 3 => 'Third Article'), 3 => array(2 => 'Second Article'));
  1303. $this->assertEqual($result, $expected);
  1304. $TestModel =& new Apple();
  1305. $expected = array(1 => 'Red Apple 1', 2 => 'Bright Red Apple', 3 => 'green blue', 4 => 'Test Name', 5 => 'Blue Green', 6 => 'My new apple', 7 => 'Some odd color');
  1306. $this->assertEqual($TestModel->find('list'), $expected);
  1307. $this->assertEqual($TestModel->Parent->find('list'), $expected);
  1308. $TestModel =& new Post();
  1309. $result = $TestModel->find('list', array('fields' => 'Post.title'));
  1310. $expected = array(1 => 'First Post', 2 => 'Second Post', 3 => 'Third Post');
  1311. $this->assertEqual($result, $expected);
  1312. $result = $TestModel->find('list', array('fields' => 'title'));
  1313. $expected = array(1 => 'First Post', 2 => 'Second Post', 3 => 'Third Post');
  1314. $this->assertEqual($result, $expected);
  1315. $result = $TestModel->find('list', array('fields' => array('title', 'id')));
  1316. $expected = array('First Post' => '1', 'Second Post' => '2', 'Third Post' => '3');
  1317. $this->assertEqual($result, $expected);
  1318. $result = $TestModel->find('list', array('fields' => array('title', 'id', 'created')));
  1319. $expected = array(
  1320. '2007-03-18 10:39:23' => array('First Post' => '1'),
  1321. '2007-03-18 10:41:23' => array('Second Post' => '2'),
  1322. '2007-03-18 10:43:23' => array('Third Post' => '3'),
  1323. );
  1324. $this->assertEqual($result, $expected);
  1325. $result = $TestModel->find('list', array('fields' => array('Post.body')));
  1326. $expected = array(1 => 'First Post Body', 2 => 'Second Post Body', 3 => 'Third Post Body');
  1327. $this->assertEqual($result, $expected);
  1328. $result = $TestModel->find('list', array('fields' => array('Post.title', 'Post.body')));
  1329. $expected = array('First Post' => 'First Post Body', 'Second Post' => 'Second Post Body', 'Third Post' => 'Third Post Body');
  1330. $this->assertEqual($result, $expected);
  1331. $result = $TestModel->find('list', array('fields' => array('Post.id', 'Post.title', 'Author.user'), 'recursive' => 1));
  1332. $expected = array('mariano' => array(1 => 'First Post', 3 => 'Third Post'), 'larry' => array(2 => 'Second Post'));
  1333. $this->assertEqual($result, $expected);
  1334. $TestModel =& new User();
  1335. $result = $TestModel->find('list', array('fields' => array('User.user', 'User.password')));
  1336. $expected = array('mariano' => '5f4dcc3b5aa765d61d8327deb882cf99', 'nate' => '5f4dcc3b5aa765d61d8327deb882cf99', 'larry' => '5f4dcc3b5aa765d61d8327deb882cf99', 'garrett' => '5f4dcc3b5aa765d61d8327deb882cf99');
  1337. $this->assertEqual($result, $expected);
  1338. $TestModel =& new ModifiedAuthor();
  1339. $result = $TestModel->find('list', array('fields' => array('Author.id', 'Author.user')));
  1340. $expected = array(1 => 'mariano (CakePHP)', 2 => 'nate (CakePHP)', 3 => 'larry (CakePHP)', 4 => 'garrett (CakePHP)');
  1341. $this->assertEqual($result, $expected);
  1342. }
  1343. /**
  1344. * testRecordExists method
  1345. *
  1346. * @access public
  1347. * @return void
  1348. */
  1349. function testRecordExists() {
  1350. $this->loadFixtures('User');
  1351. $TestModel =& new User();
  1352. $this->assertFalse($TestModel->exists());
  1353. $TestModel->read(null, 1);
  1354. $this->assertTrue($TestModel->exists());
  1355. $TestModel->create();
  1356. $this->assertFalse($TestModel->exists());
  1357. $TestModel->id = 4;
  1358. $this->assertTrue($TestModel->exists());
  1359. $TestModel =& new TheVoid();
  1360. $this->assertFalse($TestModel->exists());
  1361. $TestModel->id = 5;
  1362. $this->assertFalse($TestModel->exists());
  1363. }
  1364. /**
  1365. * testFindField method
  1366. *
  1367. * @access public
  1368. * @return void
  1369. */
  1370. function testFindField() {
  1371. $this->loadFixtures('User');
  1372. $TestModel =& new User();
  1373. $TestModel->id = 1;
  1374. $result = $TestModel->field('user');
  1375. $this->assertEqual($result, 'mariano');
  1376. $result = $TestModel->field('User.user');
  1377. $this->assertEqual($result, 'mariano');
  1378. $TestModel->id = false;
  1379. $result = $TestModel->field('user', array('user' => 'mariano'));
  1380. $this->assertEqual($result, 'mariano');
  1381. $result = $TestModel->field('COUNT(*) AS count', true);
  1382. $this->assertEqual($result, 4);
  1383. $result = $TestModel->field('COUNT(*)', true);
  1384. $this->assertEqual($result, 4);
  1385. }
  1386. /**
  1387. * testFindUnique method
  1388. *
  1389. * @access public
  1390. * @return void
  1391. */
  1392. function testFindUnique() {
  1393. $this->loadFixtures('User');
  1394. $TestModel =& new User();
  1395. $this->assertFalse($TestModel->isUnique(array('user' => 'nate')));
  1396. $TestModel->id = 2;
  1397. $this->assertTrue($TestModel->isUnique(array('user' => 'nate')));
  1398. $this->assertFalse($TestModel->isUnique(array('user' => 'nate', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99')));
  1399. }
  1400. /**
  1401. * testUpdateExisting method
  1402. *
  1403. * @access public
  1404. * @return void
  1405. */
  1406. function testUpdateExisting() {
  1407. $this->loadFixtures('User', 'Article', 'Comment');
  1408. $TestModel =& new User();
  1409. $TestModel->create();
  1410. $TestModel->save(array('User' => array('user' => 'some user', 'password' => 'some password')));
  1411. $this->assertTrue(is_int($TestModel->id) || (intval($TestModel->id) === 5));
  1412. $id = $TestModel->id;
  1413. $TestModel->save(array('User' => array('user' => 'updated user')));
  1414. $this->assertEqual($TestModel->id, $id);
  1415. $result = $TestModel->findById($id);
  1416. $this->assertEqual($result['User']['user'], 'updated user');
  1417. $this->assertEqual($result['User']['password'], 'some password');
  1418. $Article =& new Article();
  1419. $Comment =& new Comment();
  1420. $data = array('Comment' => array('id' => 1, 'comment' => 'First Comment for First Article'),
  1421. 'Article' => array('id' => 2, 'title' => 'Second Article'));
  1422. $result = $Article->save($data);
  1423. $this->assertTrue($result);
  1424. $result = $Comment->save($data);
  1425. $this->assertTrue($result);
  1426. }
  1427. /**
  1428. * testUpdateMultiple method
  1429. *
  1430. * @access public
  1431. * @return void
  1432. */
  1433. function testUpdateMultiple() {
  1434. $this->loadFixtures('Comment', 'Article', 'User', 'CategoryThread');
  1435. $TestModel =& new Comment();
  1436. $result = Set::extract($TestModel->find('all'), '{n}.Comment.user_id');
  1437. $expected = array('2', '4', '1', '1', '1', '2');
  1438. $this->assertEqual($result, $expected);
  1439. $TestModel->updateAll(array('Comment.user_id' => 5), array('Comment.user_id' => 2));
  1440. $result = Set::combine($TestModel->find('all'), '{n}.Comment.id', '{n}.Comment.user_id');
  1441. $expected = array(1 => 5, 2 => 4, 3 => 1, 4 => 1, 5 => 1, 6 => 5);
  1442. $this->assertEqual($result, $expected);
  1443. $result = $TestModel->updateAll(array('Comment.comment' => "'Updated today'"), array('Comment.user_id' => 5));
  1444. $this->assertTrue($result);
  1445. $result = Set::extract($TestModel->find('all', array('conditions' => array('Comment.user_id' => 5))), '{n}.Comment.comment');
  1446. $expected = array_fill(0, 2, 'Updated today');
  1447. $this->assertEqual($result, $expected);
  1448. }
  1449. /**
  1450. * testUpdateWithCalculation method
  1451. *
  1452. * @access public
  1453. * @return void
  1454. */
  1455. function testUpdateWithCalculation() {
  1456. $this->loadFixtures('DataTest');
  1457. $model =& new DataTest();
  1458. $result = $model->saveAll(array(
  1459. array('count' => 5, 'float' => 1.1),
  1460. array('count' => 3, 'float' => 1.2),
  1461. array('count' => 4, 'float' => 1.3),
  1462. array('count' => 1, 'float' => 2.0),
  1463. ));
  1464. $this->assertTrue($result);
  1465. $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count')));
  1466. $this->assertEqual($result, array(5, 3, 4, 1));
  1467. $this->assertTrue($model->updateAll(array('count' => 'count + 2')));
  1468. $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count')));
  1469. $this->assertEqual($result, array(7, 5, 6, 3));
  1470. $this->assertTrue($model->updateAll(array('DataTest.count' => 'DataTest.count - 1')));
  1471. $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count')));
  1472. $this->assertEqual($result, array(6, 4, 5, 2));
  1473. }
  1474. /**
  1475. * testBindUnbind method
  1476. *
  1477. * @access public
  1478. * @return void
  1479. */
  1480. function testBindUnbind() {
  1481. $this->loadFixtures('User', 'Comment', 'FeatureSet');
  1482. $TestModel =& new User();
  1483. $result = $TestModel->hasMany;
  1484. $expected = array();
  1485. $this->assertEqual($result, $expected);
  1486. $result = $TestModel->bindModel(array('hasMany' => array('Comment')));
  1487. $this->assertTrue($result);
  1488. $result = $TestModel->find('all', array('fields' => 'User.id, User.user'));
  1489. $expected = array(
  1490. array('User' => array('id' => '1', 'user' => 'mariano'), 'Comment' => array(
  1491. array('id' => '3', 'article_id' => '1', 'user_id' => '1', 'comment' => 'Third Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:49:23', 'updated' => '2007-03-18 10:51:31'),
  1492. array('id' => '4', 'article_id' => '1', 'user_id' => '1', 'comment' => 'Fourth Comment for First Article', 'published' => 'N', 'created' => '2007-03-18 10:51:23', 'updated' => '2007-03-18 10:53:31'),
  1493. 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'))),
  1494. array('User' => array('id' => '2', 'user' => 'nate'), 'Comment' => array(
  1495. 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'),
  1496. 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'))),
  1497. array('User' => array('id' => '3', 'user' => 'larry'), 'Comment' => array()),
  1498. array('User' => array('id' => '4', 'user' => 'garrett'), 'Comment' => array(
  1499. array('id' => '2', 'article_id' => '1', 'user_id' => '4', 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'))));
  1500. $this->assertEqual($result, $expected);
  1501. $TestModel->resetAssociations();
  1502. $result = $TestModel->hasMany;
  1503. $this->assertEqual($result, array());
  1504. $result = $TestModel->bindModel(array('hasMany' => array('Comment')), false);
  1505. $this->assertTrue($result);
  1506. $result = $TestModel->find('all', array('fields' => 'User.id, User.user'));
  1507. $expected = array(
  1508. array('User' => array('id' => '1', 'user' => 'mariano'), 'Comment' => array(
  1509. array('id' => '3', 'article_id' => '1', 'user_id' => '1', 'comment' => 'Third Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:49:23', 'updated' => '2007-03-18 10:51:31'),
  1510. array('id' => '4', 'article_id' => '1', 'user_id' => '1', 'comment' => 'Fourth Comment for First Article', 'published' => 'N', 'created' => '2007-03-18 10:51:23', 'updated' => '2007-03-18 10:53:31'),
  1511. 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'))),
  1512. array('User' => array('id' => '2', 'user' => 'nate'), 'Comment' => array(
  1513. 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'),
  1514. 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'))),
  1515. array('User' => array('id' => '3', 'user' => 'larry'), 'Comment' => array()),
  1516. array('User' => array('id' => '4', 'user' => 'garrett'), 'Comment' => array(
  1517. array('id' => '2', 'article_id' => '1', 'user_id' => '4', 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'))));
  1518. $this->assertEqual($result, $expected);
  1519. $result = $TestModel->hasMany;
  1520. $expected = array('Comment' => array('className' => 'Comment', 'foreignKey' => 'user_id', 'conditions' => null, 'fields' => null, 'order' => null, 'limit' => null, 'offset' => null, 'dependent' => null, 'exclusive' => null, 'finderQuery' => null, 'counterQuery' => null) );
  1521. $this->assertEqual($result, $expected);
  1522. $result = $TestModel->unbindModel(array('hasMany' => array('Comment')));
  1523. $this->assertTrue($result);
  1524. $result = $TestModel->hasMany;
  1525. $expected = array();
  1526. $this->assertEqual($result, $expected);
  1527. $result = $TestModel->find('all', array('fields' => 'User.id, User.user'));
  1528. $expected = array(
  1529. array('User' => array('id' => '1', 'user' => 'mariano')),
  1530. array('User' => array('id' => '2', 'user' => 'nate')),
  1531. array('User' => array('id' => '3', 'user' => 'larry')),
  1532. array('User' => array('id' => '4', 'user' => 'garrett')));
  1533. $this->assertEqual($result, $expected);
  1534. $result = $TestModel->find('all', array('fields' => 'User.id, User.user'));
  1535. $expected = array(
  1536. array('User' => array('id' => '1', 'user' => 'mariano'), 'Comment' => array(
  1537. array('id' => '3', 'article_id' => '1', 'user_id' => '1', 'comment' => 'Third Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:49:23', 'updated' => '2007-03-18 10:51:31'),
  1538. array('id' => '4', 'article_id' => '1', 'user_id' => '1', 'comment' => 'Fourth Comment for First Article', 'published' => 'N', 'created' => '2007-03-18 10:51:23', 'updated' => '2007-03-18 10:53:31'),
  1539. 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'))),
  1540. array('User' => array('id' => '2', 'user' => 'nate'), 'Comment' => array(
  1541. 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'),
  1542. 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'))),
  1543. array('User' => array('id' => '3', 'user' => 'larry'), 'Comment' => array()),
  1544. array('User' => array('id' => '4', 'user' => 'garrett'), 'Comment' => array(
  1545. array('id' => '2', 'article_id' => '1', 'user_id' => '4', 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'))));
  1546. $this->assertEqual($result, $expected);
  1547. $result = $TestModel->unbindModel(array('hasMany' => array('Comment')), false);
  1548. $this->assertTrue($result);
  1549. $result = $TestModel->find('all', array('fields' => 'User.id, User.user'));
  1550. $expected = array(
  1551. array('User' => array('id' => '1', 'user' => 'mariano')),
  1552. array('User' => array('id' => '2', 'user' => 'nate')),
  1553. array('User' => array('id' => '3', 'user' => 'larry')),
  1554. array('User' => array('id' => '4', 'user' => 'garrett')));
  1555. $this->assertEqual($result, $expected);
  1556. $result = $TestModel->hasMany;
  1557. $expected = array();
  1558. $this->assertEqual($result, $expected);
  1559. $result = $TestModel->bindModel(array('hasMany' => array('Comment' => array('className' => 'Comment', 'conditions' => 'Comment.published = \'Y\'') )));
  1560. $this->assertTrue($result);
  1561. $result = $TestModel->find('all', array('fields' => 'User.id, User.user'));
  1562. $expected = array(
  1563. array('User' => array('id' => '1', 'user' => 'mariano'), 'Comment' => array(
  1564. array('id' => '3', 'article_id' => '1', 'user_id' => '1', 'comment' => 'Third Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:49:23', 'updated' => '2007-03-18 10:51:31'),
  1565. 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'))),
  1566. array('User' => array('id' => '2', 'user' => 'nate'), 'Comment' => array(
  1567. 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'),
  1568. 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'))),
  1569. array('User' => array('id' => '3', 'user' => 'larry'), 'Comment' => array()),
  1570. array('User' => array('id' => '4', 'user' => 'garrett'), 'Comment' => array(
  1571. array('id' => '2', 'article_id' => '1', 'user_id' => '4', 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'))));
  1572. $this->assertEqual($result, $expected);
  1573. $TestModel2 =& new DeviceType();
  1574. $expected = array('className' => 'FeatureSet', 'foreignKey' => 'feature_set_id', 'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => '');
  1575. $this->assertEqual($TestModel2->belongsTo['FeatureSet'], $expected);
  1576. $TestModel2->bind('FeatureSet', array('conditions' => array('active' => true)));
  1577. $expected['conditions'] = array('active' => true);
  1578. $this->assertEqual($TestModel2->belongsTo['FeatureSet'], $expected);
  1579. $TestModel2->bind('FeatureSet', array('foreignKey' => false, 'conditions' => array('Feature.name' => 'DeviceType.name')));
  1580. $expected['conditions'] = array('Feature.name' => 'DeviceType.name');
  1581. $expected['foreignKey'] = false;
  1582. $this->assertEqual($TestModel2->belongsTo['FeatureSet'], $expected);
  1583. $TestModel2->bind('NewFeatureSet', array('type' => 'hasMany', 'className' => 'FeatureSet', 'conditions' => array('active' => true)));
  1584. $expected = array('className' => 'FeatureSet', 'conditions' => array('active' => true), 'foreignKey' => 'device_type_id', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'dependent' => '', 'exclusive' => '', 'finderQuery' => '', 'counterQuery' => '');
  1585. $this->assertEqual($TestModel2->hasMany['NewFeatureSet'], $expected);
  1586. $this->assertTrue(is_object($TestModel2->NewFeatureSet));
  1587. }
  1588. /**
  1589. * testBindMultipleTimes method
  1590. *
  1591. * @access public
  1592. * @return void
  1593. */
  1594. function testBindMultipleTimes() {
  1595. $this->loadFixtures('User', 'Comment', 'Article');
  1596. $TestModel =& new User();
  1597. $result = $TestModel->hasMany;
  1598. $expected = array();
  1599. $this->assertEqual($result, $expected);
  1600. $result = $TestModel->bindModel(array('hasMany' => array('Items' => array('className' => 'Comment'))));
  1601. $this->assertTrue($result);
  1602. $result = $TestModel->find('all', array('fields' => 'User.id, User.user'));
  1603. $expected = array(
  1604. array('User' => array('id' => '1', 'user' => 'mariano'), 'Items' => array(
  1605. array('id' => '3', 'article_id' => '1', 'user_id' => '1', 'comment' => 'Third Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:49:23', 'updated' => '2007-03-18 10:51:31'),
  1606. array('id' => '4', 'article_id' => '1', 'user_id' => '1', 'comment' => 'Fourth Comment for First Article', 'published' => 'N', 'created' => '2007-03-18 10:51:23', 'updated' => '2007-03-18 10:53:31'),
  1607. 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'))),
  1608. array('User' => array('id' => '2', 'user' => 'nate'), 'Items' => array(
  1609. 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'),
  1610. 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'))),
  1611. array('User' => array('id' => '3', 'user' => 'larry'), 'Items' => array()),
  1612. array('User' => array('id' => '4', 'user' => 'garrett'), 'Items' => array(
  1613. array('id' => '2', 'article_id' => '1', 'user_id' => '4', 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'))));
  1614. $this->assertEqual($result, $expected);
  1615. $result = $TestModel->bindModel(array('hasMany' => array('Items' => array('className' => 'Article'))));
  1616. $this->assertTrue($result);
  1617. $result = $TestModel->find('all', array('fields' => 'User.id, User.user'));
  1618. $expected = array(
  1619. array('User' => array('id' => '1', 'user' => 'mariano'), 'Items' => array(
  1620. 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'),
  1621. 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'))),
  1622. array('User' => array('id' => '2', 'user' => 'nate'), 'Items' => array()),
  1623. array('User' => array('id' => '3', 'user' => 'larry'), 'Items' => array(
  1624. 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'))),
  1625. array('User' => array('id' => '4', 'user' => 'garrett'), 'Items' => array()));
  1626. $this->assertEqual($result, $expected);
  1627. }
  1628. /**
  1629. * test find('count') method
  1630. *
  1631. * @access public
  1632. * @return void
  1633. */
  1634. function testFindCount() {
  1635. $this->loadFixtures('User', 'Project');
  1636. $TestModel =& new User();
  1637. $result = $TestModel->find('count');
  1638. $this->assertEqual($result, 4);
  1639. $fullDebug = $this->db->fullDebug;
  1640. $this->db->fullDebug = true;
  1641. $TestModel->order = 'User.id';
  1642. $this->db->_queriesLog = array();
  1643. $result = $TestModel->find('count');
  1644. $this->assertEqual($result, 4);
  1645. $this->assertTrue(isset($this->db->_queriesLog[0]['query']));
  1646. $this->assertNoPattern('/ORDER\s+BY/', $this->db->_queriesLog[0]['query']);
  1647. $this->db->_queriesLog = array();
  1648. $this->db->fullDebug = $fullDebug;
  1649. $TestModel =& new Project();
  1650. $TestModel->create(array('name' => 'project')) && $TestModel->save();
  1651. $TestModel->create(array('name' => 'project')) && $TestModel->save();
  1652. $TestModel->create(array('name' => 'project')) && $TestModel->save();
  1653. $result = $TestModel->find('count', array('fields' => 'DISTINCT Project.name'));
  1654. $this->assertEqual($result, 4);
  1655. }
  1656. /**
  1657. * Test find(count) with Db::expression
  1658. *
  1659. * @return void
  1660. **/
  1661. function testFindCountWithDbExpressions() {
  1662. if ($this->skipif($this->db->config['driver'] == 'postgres', 'testFindCountWithExpressions is not compatible with Postgres')) {
  1663. return;
  1664. }
  1665. $this->loadFixtures('Project');
  1666. $db = ConnectionManager::getDataSource('test_suite');
  1667. $TestModel =& new Project();
  1668. $result = $TestModel->find('count', array('conditions' => array(
  1669. $db->expression('Project.name = \'Project 3\'')
  1670. )));
  1671. $this->assertEqual($result, 1);
  1672. $result = $TestModel->find('count', array('conditions' => array(
  1673. 'Project.name' => $db->expression('\'Project 3\'')
  1674. )));
  1675. $this->assertEqual($result, 1);
  1676. }
  1677. /**
  1678. * testFindMagic method
  1679. *
  1680. * @access public
  1681. * @return void
  1682. */
  1683. function testFindMagic() {
  1684. $this->loadFixtures('User');
  1685. $TestModel =& new User();
  1686. $result = $TestModel->findByUser('mariano');
  1687. $expected = array('User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'));
  1688. $this->assertEqual($result, $expected);
  1689. $result = $TestModel->findByPassword('5f4dcc3b5aa765d61d8327deb882cf99');
  1690. $expected = array('User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'));
  1691. $this->assertEqual($result, $expected);
  1692. }
  1693. /**
  1694. * testRead method
  1695. *
  1696. * @access public
  1697. * @return void
  1698. */
  1699. function testRead() {
  1700. $this->loadFixtures('User', 'Article');
  1701. $TestModel =& new User();
  1702. $result = $TestModel->read();
  1703. $this->assertFalse($result);
  1704. $TestModel->id = 2;
  1705. $result = $TestModel->read();
  1706. $expected = array('User' => array('id' => '2', 'user' => 'nate', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:18:23', 'updated' => '2007-03-17 01:20:31'));
  1707. $this->assertEqual($result, $expected);
  1708. $result = $TestModel->read(null, 2);
  1709. $expected = array('User' => array('id' => '2', 'user' => 'nate', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:18:23', 'updated' => '2007-03-17 01:20:31'));
  1710. $this->assertEqual($result, $expected);
  1711. $TestModel->id = 2;
  1712. $result = $TestModel->read(array('id', 'user'));
  1713. $expected = array('User' => array('id' => '2', 'user' => 'nate'));
  1714. $this->assertEqual($result, $expected);
  1715. $result = $TestModel->read('id, user', 2);
  1716. $expected = array('User' => array('id' => '2', 'user' => 'nate'));
  1717. $this->assertEqual($result, $expected);
  1718. $result = $TestModel->bindModel(array('hasMany' => array('Article')));
  1719. $this->assertTrue($result);
  1720. $TestModel->id = 1;
  1721. $result = $TestModel->read('id, user');
  1722. $expected = array('User' => array('id' => '1', 'user' => 'mariano'),
  1723. 'Article' => array(
  1724. 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' ),
  1725. 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' )));
  1726. $this->assertEqual($result, $expected);
  1727. }
  1728. /**
  1729. * testRecursiveRead method
  1730. *
  1731. * @access public
  1732. * @return void
  1733. */
  1734. function testRecursiveRead() {
  1735. $this->loadFixtures('User', 'Article', 'Comment', 'Tag', 'ArticlesTag', 'Featured', 'ArticleFeatured');
  1736. $TestModel =& new User();
  1737. $result = $TestModel->bindModel(array('hasMany' => array('Article')), false);
  1738. $this->assertTrue($result);
  1739. $TestModel->recursive = 0;
  1740. $result = $TestModel->read('id, user', 1);
  1741. $expected = array(
  1742. 'User' => array('id' => '1', 'user' => 'mariano'),
  1743. );
  1744. $this->assertEqual($result, $expected);
  1745. $TestModel->recursive = 1;
  1746. $result = $TestModel->read('id, user', 1);
  1747. $expected = array('User' => array('id' => '1', 'user' => 'mariano'),
  1748. 'Article' => array(
  1749. 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' ),
  1750. 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' )));
  1751. $this->assertEqual($result, $expected);
  1752. $TestModel->recursive = 2;
  1753. $result = $TestModel->read('id, user', 3);
  1754. $expected = array('User' => array('id' => '3', 'user' => 'larry'),
  1755. 'Article' => array(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',
  1756. 'User' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31'),
  1757. 'Comment' => array(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'),
  1758. 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')),
  1759. 'Tag' => array(
  1760. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  1761. array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')))));
  1762. $this->assertEqual($result, $expected);
  1763. }
  1764. /**
  1765. * @todo Figure out why Featured is not getting truncated properly
  1766. */
  1767. function testRecursiveFindAll() {
  1768. $this->db->truncate(new Featured());
  1769. $this->loadFixtures('User', 'Article', 'Comment', 'Tag', 'ArticlesTag', 'Attachment', 'ArticleFeatured', 'Featured', 'Category');
  1770. $TestModel =& new Article();
  1771. $result = $TestModel->find('all', array('conditions' => array('Article.user_id' => 1)));
  1772. $expected = array(
  1773. array(
  1774. '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'),
  1775. 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  1776. 'Comment' => array(
  1777. 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'),
  1778. array('id' => '2', 'article_id' => '1', 'user_id' => '4', 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'),
  1779. array('id' => '3', 'article_id' => '1', 'user_id' => '1', 'comment' => 'Third Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:49:23', 'updated' => '2007-03-18 10:51:31'),
  1780. array('id' => '4', 'article_id' => '1', 'user_id' => '1', 'comment' => 'Fourth Comment for First Article', 'published' => 'N', 'created' => '2007-03-18 10:51:23', 'updated' => '2007-03-18 10:53:31')
  1781. ),
  1782. 'Tag' => array(
  1783. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  1784. array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')
  1785. )
  1786. ),
  1787. array(
  1788. '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'),
  1789. 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  1790. 'Comment' => array(),
  1791. 'Tag' => array()
  1792. )
  1793. );
  1794. $this->assertEqual($result, $expected);
  1795. $result = $TestModel->find('all', array('conditions' => array('Article.user_id' => 3), 'limit' => 1, 'recursive' => 2));
  1796. $expected = array(
  1797. array(
  1798. 'Article' => array(
  1799. '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'
  1800. ),
  1801. 'User' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31'),
  1802. 'Comment' => array(
  1803. array(
  1804. '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',
  1805. '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'),
  1806. 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  1807. 'Attachment' => array('id' => '1', 'comment_id' => 5, 'attachment' => 'attachment.zip', 'created' => '2007-03-18 10:51:23', 'updated' => '2007-03-18 10:53:31')
  1808. ),
  1809. array(
  1810. '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',
  1811. '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'),
  1812. 'User' => array('id' => '2', 'user' => 'nate', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:18:23', 'updated' => '2007-03-17 01:20:31'),
  1813. 'Attachment' => false
  1814. )
  1815. ),
  1816. 'Tag' => array(
  1817. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  1818. array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')
  1819. )
  1820. )
  1821. );
  1822. $this->assertEqual($result, $expected);
  1823. $Featured = new Featured();
  1824. $Featured->recursive = 2;
  1825. $Featured->bindModel(array(
  1826. 'belongsTo' => array(
  1827. 'ArticleFeatured' => array(
  1828. 'conditions' => "ArticleFeatured.published = 'Y'",
  1829. 'fields' => 'id, title, user_id, published'
  1830. )
  1831. )
  1832. ));
  1833. $Featured->ArticleFeatured->unbindModel(array(
  1834. 'hasMany' => array('Attachment', 'Comment'),
  1835. 'hasAndBelongsToMany' => array('Tag'))
  1836. );
  1837. $orderBy = 'ArticleFeatured.id ASC';
  1838. $result = $Featured->find('all', array('order' => $orderBy, 'limit' => 3));
  1839. $expected = array(
  1840. array('Featured' => array('id' => '1', 'article_featured_id' => '1', 'category_id' => '1', 'published_date' => '2007-03-31 10:39:23', 'end_date' => '2007-05-15 10:39:23', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
  1841. 'ArticleFeatured' => array('id' => '1', 'title' => 'First Article', 'user_id' => '1', 'published' => 'Y',
  1842. 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  1843. 'Category' => array(),
  1844. 'Featured' => array('id' => '1', 'article_featured_id' => '1', 'category_id' => '1', 'published_date' => '2007-03-31 10:39:23', 'end_date' => '2007-05-15 10:39:23', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31')),
  1845. 'Category' => array('id' => '1', 'parent_id' => '0', 'name' => 'Category 1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31')),
  1846. array('Featured' => array('id' => '2', 'article_featured_id' => '2', 'category_id' => '1', 'published_date' => '2007-03-31 10:39:23', 'end_date' => '2007-05-15 10:39:23', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
  1847. 'ArticleFeatured' => array('id' => '2', 'title' => 'Second Article', 'user_id' => '3', 'published' => 'Y',
  1848. 'User' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31'),
  1849. 'Category' => array(),
  1850. 'Featured' => array('id' => '2', 'article_featured_id' => '2', 'category_id' => '1', 'published_date' => '2007-03-31 10:39:23', 'end_date' => '2007-05-15 10:39:23', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31')),
  1851. 'Category' => array( 'id' => '1', 'parent_id' => '0', 'name' => 'Category 1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31')
  1852. )
  1853. );
  1854. $this->assertEqual($result, $expected);
  1855. }
  1856. /**
  1857. * testRecursiveFindAllWithLimit method
  1858. *
  1859. * @access public
  1860. * @return void
  1861. */
  1862. function testRecursiveFindAllWithLimit() {
  1863. $this->loadFixtures('Article', 'User', 'Tag', 'ArticlesTag', 'Comment', 'Attachment');
  1864. $TestModel =& new Article();
  1865. $TestModel->hasMany['Comment']['limit'] = 2;
  1866. $result = $TestModel->find('all', array('conditions' => array('Article.user_id' => 1)));
  1867. $expected = array(
  1868. array(
  1869. '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'),
  1870. 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  1871. 'Comment' => array(
  1872. 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'),
  1873. array('id' => '2', 'article_id' => '1', 'user_id' => '4', 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'),
  1874. ),
  1875. 'Tag' => array(
  1876. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  1877. array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')
  1878. )
  1879. ),
  1880. array(
  1881. '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'),
  1882. 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  1883. 'Comment' => array(),
  1884. 'Tag' => array()
  1885. )
  1886. );
  1887. $this->assertEqual($result, $expected);
  1888. $TestModel->hasMany['Comment']['limit'] = 1;
  1889. $result = $TestModel->find('all', array('conditions' => array('Article.user_id' => 3), 'limit' => 1, 'recursive' => 2));
  1890. $expected = array(
  1891. array(
  1892. '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'),
  1893. 'User' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31'),
  1894. 'Comment' => array(
  1895. array(
  1896. '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',
  1897. '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'),
  1898. 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  1899. 'Attachment' => array('id' => '1', 'comment_id' => 5, 'attachment' => 'attachment.zip', 'created' => '2007-03-18 10:51:23', 'updated' => '2007-03-18 10:53:31')
  1900. )
  1901. ),
  1902. 'Tag' => array(
  1903. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  1904. array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')
  1905. )
  1906. )
  1907. );
  1908. $this->assertEqual($result, $expected);
  1909. }
  1910. /**
  1911. * testAssociationAfterFind method
  1912. *
  1913. * @access public
  1914. * @return void
  1915. */
  1916. function testAssociationAfterFind() {
  1917. $this->loadFixtures('Post', 'Author', 'Comment');
  1918. $TestModel =& new Post();
  1919. $result = $TestModel->find('all');
  1920. $expected = array(
  1921. array(
  1922. 'Post' => array('id' => '1', 'author_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'),
  1923. 'Author' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31', 'test' => 'working'),
  1924. ), array(
  1925. 'Post' => array('id' => '2', 'author_id' => '3', 'title' => 'Second Post', 'body' => 'Second Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'),
  1926. 'Author' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31', 'test' => 'working'),
  1927. ), array(
  1928. 'Post' => array('id' => '3', 'author_id' => '1', 'title' => 'Third Post', 'body' => 'Third Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'),
  1929. 'Author' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31', 'test' => 'working')
  1930. )
  1931. );
  1932. $this->assertEqual($result, $expected);
  1933. unset($TestModel);
  1934. $Author =& new Author();
  1935. $Author->Post->bindModel(array(
  1936. 'hasMany' => array(
  1937. 'Comment' => array(
  1938. 'className' => 'ModifiedComment',
  1939. 'foreignKey' => 'article_id',
  1940. )
  1941. )));
  1942. $result = $Author->find('all', array(
  1943. 'conditions' => array('Author.id' => 1),
  1944. 'recursive' => 2
  1945. ));
  1946. $expected = array(
  1947. 'id' => 1,
  1948. 'article_id' => 1,
  1949. 'user_id' => 2,
  1950. 'comment' => 'First Comment for First Article',
  1951. 'published' => 'Y',
  1952. 'created' => '2007-03-18 10:45:23',
  1953. 'updated' => '2007-03-18 10:47:31',
  1954. 'callback' => 'Fire'
  1955. );
  1956. $this->assertEqual($result[0]['Post'][0]['Comment'][0], $expected);
  1957. }
  1958. /**
  1959. * Tests that callbacks can be properly disabled
  1960. *
  1961. * @access public
  1962. * @return void
  1963. */
  1964. function testCallbackDisabling() {
  1965. $this->loadFixtures('Author');
  1966. $TestModel = new ModifiedAuthor();
  1967. $result = Set::extract($TestModel->find('all'), '/Author/user');
  1968. $expected = array('mariano (CakePHP)', 'nate (CakePHP)', 'larry (CakePHP)', 'garrett (CakePHP)');
  1969. $this->assertEqual($result, $expected);
  1970. $result = Set::extract($TestModel->find('all', array('callbacks' => 'after')), '/Author/user');
  1971. $expected = array('mariano (CakePHP)', 'nate (CakePHP)', 'larry (CakePHP)', 'garrett (CakePHP)');
  1972. $this->assertEqual($result, $expected);
  1973. $result = Set::extract($TestModel->find('all', array('callbacks' => 'before')), '/Author/user');
  1974. $expected = array('mariano', 'nate', 'larry', 'garrett');
  1975. $this->assertEqual($result, $expected);
  1976. $result = Set::extract($TestModel->find('all', array('callbacks' => false)), '/Author/user');
  1977. $expected = array('mariano', 'nate', 'larry', 'garrett');
  1978. $this->assertEqual($result, $expected);
  1979. }
  1980. /**
  1981. * testValidatesBackwards method
  1982. *
  1983. * @access public
  1984. * @return void
  1985. */
  1986. function testValidatesBackwards() {
  1987. $TestModel =& new TestValidate();
  1988. $TestModel->validate = array(
  1989. 'user_id' => VALID_NUMBER,
  1990. 'title' => VALID_NOT_EMPTY,
  1991. 'body' => VALID_NOT_EMPTY
  1992. );
  1993. $data = array('TestValidate' => array('user_id' => '1', 'title' => '', 'body' => ''));
  1994. $result = $TestModel->create($data);
  1995. $this->assertTrue($result);
  1996. $result = $TestModel->validates();
  1997. $this->assertFalse($result);
  1998. $data = array('TestValidate' => array('user_id' => '1', 'title' => 'title', 'body' => ''));
  1999. $result = $TestModel->create($data);
  2000. $this->assertTrue($result);
  2001. $result = $TestModel->validates();
  2002. $this->assertFalse($result);
  2003. $data = array('TestValidate' => array('user_id' => '', 'title' => 'title', 'body' => 'body'));
  2004. $result = $TestModel->create($data);
  2005. $this->assertTrue($result);
  2006. $result = $TestModel->validates();
  2007. $this->assertFalse($result);
  2008. $data = array('TestValidate' => array('user_id' => 'not a number', 'title' => 'title', 'body' => 'body'));
  2009. $result = $TestModel->create($data);
  2010. $this->assertTrue($result);
  2011. $result = $TestModel->validates();
  2012. $this->assertFalse($result);
  2013. $data = array('TestValidate' => array('user_id' => '1', 'title' => 'title', 'body' => 'body'));
  2014. $result = $TestModel->create($data);
  2015. $this->assertTrue($result);
  2016. $result = $TestModel->validates();
  2017. $this->assertTrue($result);
  2018. }
  2019. /**
  2020. * testValidates method
  2021. *
  2022. * @access public
  2023. * @return void
  2024. */
  2025. function testValidates() {
  2026. $TestModel =& new TestValidate();
  2027. $TestModel->validate = array(
  2028. 'user_id' => 'numeric',
  2029. 'title' => array('allowEmpty' => false, 'rule' => 'notEmpty'),
  2030. 'body' => 'notEmpty'
  2031. );
  2032. $data = array('TestValidate' => array('user_id' => '1', 'title' => '', 'body' => 'body'));
  2033. $result = $TestModel->create($data);
  2034. $this->assertTrue($result);
  2035. $result = $TestModel->validates();
  2036. $this->assertFalse($result);
  2037. $data = array('TestValidate' => array('user_id' => '1', 'title' => 'title', 'body' => 'body'));
  2038. $result = $TestModel->create($data) && $TestModel->validates();
  2039. $this->assertTrue($result);
  2040. $data = array('TestValidate' => array('user_id' => '1', 'title' => '0', 'body' => 'body'));
  2041. $result = $TestModel->create($data);
  2042. $this->assertTrue($result);
  2043. $result = $TestModel->validates();
  2044. $this->assertTrue($result);
  2045. $data = array('TestValidate' => array('user_id' => '1', 'title' => 0, 'body' => 'body'));
  2046. $result = $TestModel->create($data);
  2047. $this->assertTrue($result);
  2048. $result = $TestModel->validates();
  2049. $this->assertTrue($result);
  2050. $TestModel->validate['modified'] = array('allowEmpty' => true, 'rule' => 'date');
  2051. $data = array('TestValidate' => array('user_id' => '1', 'title' => 0, 'body' => 'body', 'modified' => ''));
  2052. $result = $TestModel->create($data);
  2053. $this->assertTrue($result);
  2054. $result = $TestModel->validates();
  2055. $this->assertTrue($result);
  2056. $data = array('TestValidate' => array('user_id' => '1', 'title' => 0, 'body' => 'body', 'modified' => '2007-05-01'));
  2057. $result = $TestModel->create($data);
  2058. $this->assertTrue($result);
  2059. $result = $TestModel->validates();
  2060. $this->assertTrue($result);
  2061. $data = array('TestValidate' => array('user_id' => '1', 'title' => 0, 'body' => 'body', 'modified' => 'invalid-date-here'));
  2062. $result = $TestModel->create($data);
  2063. $this->assertTrue($result);
  2064. $result = $TestModel->validates();
  2065. $this->assertFalse($result);
  2066. $data = array('TestValidate' => array('user_id' => '1', 'title' => 0, 'body' => 'body', 'modified' => 0));
  2067. $result = $TestModel->create($data);
  2068. $this->assertTrue($result);
  2069. $result = $TestModel->validates();
  2070. $this->assertFalse($result);
  2071. $data = array('TestValidate' => array('user_id' => '1', 'title' => 0, 'body' => 'body', 'modified' => '0'));
  2072. $result = $TestModel->create($data);
  2073. $this->assertTrue($result);
  2074. $result = $TestModel->validates();
  2075. $this->assertFalse($result);
  2076. $TestModel->validate['modified'] = array('allowEmpty' => false, 'rule' => 'date');
  2077. $data = array('TestValidate' => array('modified' => null));
  2078. $result = $TestModel->create($data);
  2079. $this->assertTrue($result);
  2080. $result = $TestModel->validates();
  2081. $this->assertFalse($result);
  2082. $data = array('TestValidate' => array('modified' => false));
  2083. $result = $TestModel->create($data);
  2084. $this->assertTrue($result);
  2085. $result = $TestModel->validates();
  2086. $this->assertFalse($result);
  2087. $data = array('TestValidate' => array('modified' => ''));
  2088. $result = $TestModel->create($data);
  2089. $this->assertTrue($result);
  2090. $result = $TestModel->validates();
  2091. $this->assertFalse($result);
  2092. $data = array('TestValidate' => array('modified' => '2007-05-01'));
  2093. $result = $TestModel->create($data);
  2094. $this->assertTrue($result);
  2095. $result = $TestModel->validates();
  2096. $this->assertTrue($result);
  2097. $TestModel->validate['slug'] = array('allowEmpty' => false, 'rule' => array('maxLength', 45));
  2098. $data = array('TestValidate' => array('user_id' => '1', 'title' => 0, 'body' => 'body', 'slug' => ''));
  2099. $result = $TestModel->create($data);
  2100. $this->assertTrue($result);
  2101. $result = $TestModel->validates();
  2102. $this->assertFalse($result);
  2103. $data = array('TestValidate' => array('user_id' => '1', 'title' => 0, 'body' => 'body', 'slug' => 'slug-right-here'));
  2104. $result = $TestModel->create($data);
  2105. $this->assertTrue($result);
  2106. $result = $TestModel->validates();
  2107. $this->assertTrue($result);
  2108. $data = array('TestValidate' => array('user_id' => '1', 'title' => 0, 'body' => 'body', 'slug' => 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'));
  2109. $result = $TestModel->create($data);
  2110. $this->assertTrue($result);
  2111. $result = $TestModel->validates();
  2112. $this->assertFalse($result);
  2113. $TestModel->validate = array(
  2114. 'number' => array('rule' => 'validateNumber', 'min' => 3, 'max' => 5),
  2115. 'title' => array('allowEmpty' => false, 'rule' => 'notEmpty')
  2116. );
  2117. $data = array('TestValidate' => array('title' => 'title', 'number' => '0'));
  2118. $result = $TestModel->create($data);
  2119. $this->assertTrue($result);
  2120. $result = $TestModel->validates();
  2121. $this->assertFalse($result);
  2122. $data = array('TestValidate' => array('title' => 'title', 'number' => 0));
  2123. $result = $TestModel->create($data);
  2124. $this->assertTrue($result);
  2125. $result = $TestModel->validates();
  2126. $this->assertFalse($result);
  2127. $data = array('TestValidate' => array('title' => 'title', 'number' => '3'));
  2128. $result = $TestModel->create($data);
  2129. $this->assertTrue($result);
  2130. $result = $TestModel->validates();
  2131. $this->assertTrue($result);
  2132. $data = array('TestValidate' => array('title' => 'title', 'number' => 3));
  2133. $result = $TestModel->create($data);
  2134. $this->assertTrue($result);
  2135. $result = $TestModel->validates();
  2136. $this->assertTrue($result);
  2137. $TestModel->validate = array(
  2138. 'number' => array('rule' => 'validateNumber', 'min' => 5, 'max' => 10),
  2139. 'title' => array('allowEmpty' => false, 'rule' => 'notEmpty')
  2140. );
  2141. $data = array('TestValidate' => array('title' => 'title', 'number' => '3'));
  2142. $result = $TestModel->create($data);
  2143. $this->assertTrue($result);
  2144. $result = $TestModel->validates();
  2145. $this->assertFalse($result);
  2146. $data = array('TestValidate' => array('title' => 'title', 'number' => 3));
  2147. $result = $TestModel->create($data);
  2148. $this->assertTrue($result);
  2149. $result = $TestModel->validates();
  2150. $this->assertFalse($result);
  2151. $TestModel->validate = array(
  2152. 'title' => array('allowEmpty' => false, 'rule' => 'validateTitle')
  2153. );
  2154. $data = array('TestValidate' => array('title' => ''));
  2155. $result = $TestModel->create($data);
  2156. $this->assertTrue($result);
  2157. $result = $TestModel->validates();
  2158. $this->assertFalse($result);
  2159. $data = array('TestValidate' => array('title' => 'new title'));
  2160. $result = $TestModel->create($data);
  2161. $this->assertTrue($result);
  2162. $result = $TestModel->validates();
  2163. $this->assertFalse($result);
  2164. $data = array('TestValidate' => array('title' => 'title-new'));
  2165. $result = $TestModel->create($data);
  2166. $this->assertTrue($result);
  2167. $result = $TestModel->validates();
  2168. $this->assertTrue($result);
  2169. $TestModel->validate = array('title' => array('allowEmpty' => true, 'rule' => 'validateTitle'));
  2170. $data = array('TestValidate' => array('title' => ''));
  2171. $result = $TestModel->create($data);
  2172. $this->assertTrue($result);
  2173. $result = $TestModel->validates();
  2174. $this->assertTrue($result);
  2175. $TestModel->validate = array('title' => array('length' => array('allowEmpty' => true, 'rule' => array('maxLength', 10))));
  2176. $data = array('TestValidate' => array('title' => ''));
  2177. $result = $TestModel->create($data);
  2178. $this->assertTrue($result);
  2179. $result = $TestModel->validates();
  2180. $this->assertTrue($result);
  2181. $TestModel->validate = array('title' => array('rule' => array('userDefined', 'Article', 'titleDuplicate')));
  2182. $data = array('TestValidate' => array('title' => 'My Article Title'));
  2183. $result = $TestModel->create($data);
  2184. $this->assertTrue($result);
  2185. $result = $TestModel->validates();
  2186. $this->assertFalse($result);
  2187. $data = array('TestValidate' => array('title' => 'My Article With a Different Title'));
  2188. $result = $TestModel->create($data);
  2189. $this->assertTrue($result);
  2190. $result = $TestModel->validates();
  2191. $this->assertTrue($result);
  2192. $TestModel->validate = array(
  2193. 'title' => array(
  2194. 'tooShort' => array('rule' => array('minLength', 50)),
  2195. 'onlyLetters' => array('rule' => '/^[a-z]+$/i')
  2196. ),
  2197. );
  2198. $data = array('TestValidate' => array(
  2199. 'title' => 'I am a short string'
  2200. ));
  2201. $TestModel->create($data);
  2202. $result = $TestModel->validates();
  2203. $this->assertFalse($result);
  2204. $result = $TestModel->validationErrors;
  2205. $expected = array(
  2206. 'title' => 'onlyLetters'
  2207. );
  2208. $this->assertEqual($result, $expected);
  2209. $TestModel->validate = array(
  2210. 'title' => array(
  2211. 'tooShort' => array('rule' => array('minLength', 50), 'last' => true),
  2212. 'onlyLetters' => array('rule' => '/^[a-z]+$/i')
  2213. ),
  2214. );
  2215. $data = array('TestValidate' => array(
  2216. 'title' => 'I am a short string'
  2217. ));
  2218. $TestModel->create($data);
  2219. $result = $TestModel->validates();
  2220. $this->assertFalse($result);
  2221. $result = $TestModel->validationErrors;
  2222. $expected = array(
  2223. 'title' => 'tooShort'
  2224. );
  2225. $this->assertEqual($result, $expected);
  2226. }
  2227. /**
  2228. * testSaveField method
  2229. *
  2230. * @access public
  2231. * @return void
  2232. */
  2233. function testSaveField() {
  2234. $this->loadFixtures('Article');
  2235. $TestModel =& new Article();
  2236. $TestModel->id = 1;
  2237. $result = $TestModel->saveField('title', 'New First Article');
  2238. $this->assertTrue($result);
  2239. $TestModel->recursive = -1;
  2240. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  2241. $expected = array('Article' => array(
  2242. 'id' => '1', 'user_id' => '1', 'title' => 'New First Article', 'body' => 'First Article Body'
  2243. ));
  2244. $this->assertEqual($result, $expected);
  2245. $TestModel->id = 1;
  2246. $result = $TestModel->saveField('title', '');
  2247. $this->assertTrue($result);
  2248. $TestModel->recursive = -1;
  2249. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  2250. $expected = array('Article' => array(
  2251. 'id' => '1', 'user_id' => '1', 'title' => '', 'body' => 'First Article Body'
  2252. ));
  2253. $result['Article']['title'] = trim($result['Article']['title']);
  2254. $this->assertEqual($result, $expected);
  2255. $TestModel->id = 1;
  2256. $TestModel->set('body', 'Messed up data');
  2257. $this->assertTrue($TestModel->saveField('title', 'First Article'));
  2258. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  2259. $expected = array('Article' => array(
  2260. 'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body'
  2261. ));
  2262. $this->assertEqual($result, $expected);
  2263. $TestModel->recursive = -1;
  2264. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  2265. $TestModel->id = 1;
  2266. $result = $TestModel->saveField('title', '', true);
  2267. $this->assertFalse($result);
  2268. $this->loadFixtures('Node', 'Dependency');
  2269. $Node =& new Node();
  2270. $Node->set('id', 1);
  2271. $result = $Node->read();
  2272. $this->assertEqual(Set::extract('/ParentNode/name', $result), array('Second'));
  2273. $Node->saveField('state', 10);
  2274. $result = $Node->read();
  2275. $this->assertEqual(Set::extract('/ParentNode/name', $result), array('Second'));
  2276. }
  2277. /**
  2278. * testSaveWithCreate method
  2279. *
  2280. * @access public
  2281. * @return void
  2282. */
  2283. function testSaveWithCreate() {
  2284. $this->loadFixtures('User', 'Article', 'User', 'Comment', 'Tag', 'ArticlesTag', 'Attachment');
  2285. $TestModel =& new User();
  2286. $data = array('User' => array('user' => 'user', 'password' => ''));
  2287. $result = $TestModel->save($data);
  2288. $this->assertFalse($result);
  2289. $this->assertTrue(!empty($TestModel->validationErrors));
  2290. $TestModel =& new Article();
  2291. $data = array('Article' => array('user_id' => '', 'title' => '', 'body' => ''));
  2292. $result = $TestModel->create($data) && $TestModel->save();
  2293. $this->assertFalse($result);
  2294. $this->assertTrue(!empty($TestModel->validationErrors));
  2295. $data = array('Article' => array('id' => 1, 'user_id' => '1', 'title' => 'New First Article', 'body' => ''));
  2296. $result = $TestModel->create($data) && $TestModel->save();
  2297. $this->assertFalse($result);
  2298. $data = array('Article' => array('id' => 1, 'title' => 'New First Article'));
  2299. $result = $TestModel->create() && $TestModel->save($data, false);
  2300. $this->assertTrue($result);
  2301. $TestModel->recursive = -1;
  2302. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
  2303. $expected = array('Article' => array(
  2304. 'id' => '1', 'user_id' => '1', 'title' => 'New First Article', 'body' => 'First Article Body', 'published' => 'N'
  2305. ));
  2306. $this->assertEqual($result, $expected);
  2307. $data = array('Article' => array('id' => 1, 'user_id' => '2', 'title' => 'First Article', 'body' => 'New First Article Body', 'published' => 'Y'));
  2308. $result = $TestModel->create() && $TestModel->save($data, true, array('id', 'title', 'published'));
  2309. $this->assertTrue($result);
  2310. $TestModel->recursive = -1;
  2311. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
  2312. $expected = array('Article' => array(
  2313. 'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y'
  2314. ));
  2315. $this->assertEqual($result, $expected);
  2316. $data = array(
  2317. 'Article' => array('user_id' => '2', 'title' => 'New Article', 'body' => 'New Article Body', 'created' => '2007-03-18 14:55:23', 'updated' => '2007-03-18 14:57:31'),
  2318. 'Tag' => array('Tag' => array(1, 3))
  2319. );
  2320. $TestModel->create();
  2321. $result = $TestModel->create() && $TestModel->save($data);
  2322. $this->assertTrue($result);
  2323. $TestModel->recursive = 2;
  2324. $result = $TestModel->read(null, 4);
  2325. $expected = array(
  2326. 'Article' => array('id' => '4', 'user_id' => '2', 'title' => 'New Article', 'body' => 'New Article Body', 'published' => 'N', 'created' => '2007-03-18 14:55:23', 'updated' => '2007-03-18 14:57:31'),
  2327. 'User' => array('id' => '2', 'user' => 'nate', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:18:23', 'updated' => '2007-03-17 01:20:31'),
  2328. 'Comment' => array(),
  2329. 'Tag' => array(
  2330. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  2331. array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')
  2332. )
  2333. );
  2334. $this->assertEqual($result, $expected);
  2335. $data = array('Comment' => array('article_id' => '4', 'user_id' => '1', 'comment' => 'Comment New Article', 'published' => 'Y', 'created' => '2007-03-18 14:57:23', 'updated' => '2007-03-18 14:59:31'));
  2336. $result = $TestModel->Comment->create() && $TestModel->Comment->save($data);
  2337. $this->assertTrue($result);
  2338. $data = array('Attachment' => array('comment_id' => '7', 'attachment' => 'newattachment.zip', 'created' => '2007-03-18 15:02:23', 'updated' => '2007-03-18 15:04:31'));
  2339. $result = $TestModel->Comment->Attachment->save($data);
  2340. $this->assertTrue($result);
  2341. $TestModel->recursive = 2;
  2342. $result = $TestModel->read(null, 4);
  2343. $expected = array(
  2344. 'Article' => array('id' => '4', 'user_id' => '2', 'title' => 'New Article', 'body' => 'New Article Body', 'published' => 'N', 'created' => '2007-03-18 14:55:23', 'updated' => '2007-03-18 14:57:31'),
  2345. 'User' => array('id' => '2', 'user' => 'nate', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:18:23', 'updated' => '2007-03-17 01:20:31'),
  2346. 'Comment' => array(
  2347. array(
  2348. 'id' => '7', 'article_id' => '4', 'user_id' => '1', 'comment' => 'Comment New Article', 'published' => 'Y', 'created' => '2007-03-18 14:57:23', 'updated' => '2007-03-18 14:59:31',
  2349. 'Article' => array('id' => '4', 'user_id' => '2', 'title' => 'New Article', 'body' => 'New Article Body', 'published' => 'N', 'created' => '2007-03-18 14:55:23', 'updated' => '2007-03-18 14:57:31'),
  2350. 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  2351. 'Attachment' => array('id' => '2', 'comment_id' => '7', 'attachment' => 'newattachment.zip', 'created' => '2007-03-18 15:02:23', 'updated' => '2007-03-18 15:04:31')
  2352. )
  2353. ),
  2354. 'Tag' => array(
  2355. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  2356. array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')
  2357. )
  2358. );
  2359. $this->assertEqual($result, $expected);
  2360. }
  2361. /**
  2362. * testSaveWithSet method
  2363. *
  2364. * @access public
  2365. * @return void
  2366. */
  2367. function testSaveWithSet() {
  2368. $this->loadFixtures('Article');
  2369. $TestModel =& new Article();
  2370. // Create record we will be updating later
  2371. $data = array('Article' => array('user_id' => '1', 'title' => 'Fourth Article', 'body' => 'Fourth Article Body', 'published' => 'Y'));
  2372. $result = $TestModel->create() && $TestModel->save($data);
  2373. $this->assertTrue($result);
  2374. // Check record we created
  2375. $TestModel->recursive = -1;
  2376. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  2377. $expected = array('Article' => array('id' => '4', 'user_id' => '1', 'title' => 'Fourth Article', 'body' => 'Fourth Article Body', 'published' => 'Y'));
  2378. $this->assertEqual($result, $expected);
  2379. // Create new record just to overlap Model->id on previously created record
  2380. $data = array('Article' => array('user_id' => '4', 'title' => 'Fifth Article', 'body' => 'Fifth Article Body', 'published' => 'Y'));
  2381. $result = $TestModel->create() && $TestModel->save($data);
  2382. $this->assertTrue($result);
  2383. $TestModel->recursive = -1;
  2384. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  2385. $expected = array('Article' => array(
  2386. 'id' => '5', 'user_id' => '4', 'title' => 'Fifth Article', 'body' => 'Fifth Article Body', 'published' => 'Y'
  2387. ));
  2388. $this->assertEqual($result, $expected);
  2389. // Go back and edit the first article we created, starting by checking it's still there
  2390. $TestModel->recursive = -1;
  2391. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  2392. $expected = array('Article' => array('id' => '4', 'user_id' => '1', 'title' => 'Fourth Article', 'body' => 'Fourth Article Body', 'published' => 'Y'));
  2393. $this->assertEqual($result, $expected);
  2394. // And now do the update with set()
  2395. $data = array('Article' => array('id' => '4', 'title' => 'Fourth Article - New Title', 'published' => 'N'));
  2396. $result = $TestModel->set($data) && $TestModel->save();
  2397. $this->assertTrue($result);
  2398. $TestModel->recursive = -1;
  2399. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  2400. $expected = array('Article' => array('id' => '4', 'user_id' => '1', 'title' => 'Fourth Article - New Title', 'body' => 'Fourth Article Body', 'published' => 'N'));
  2401. $this->assertEqual($result, $expected);
  2402. $TestModel->recursive = -1;
  2403. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  2404. $expected = array('Article' => array(
  2405. 'id' => '5', 'user_id' => '4', 'title' => 'Fifth Article', 'body' => 'Fifth Article Body', 'published' => 'Y'
  2406. ));
  2407. $this->assertEqual($result, $expected);
  2408. $data = array('Article' => array('id' => '5', 'title' => 'Fifth Article - New Title 5'));
  2409. $result = ($TestModel->set($data) && $TestModel->save());
  2410. $this->assertTrue($result);
  2411. $TestModel->recursive = -1;
  2412. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  2413. $expected = array('Article' => array('id' => '5', 'user_id' => '4', 'title' => 'Fifth Article - New Title 5', 'body' => 'Fifth Article Body', 'published' => 'Y'));
  2414. $this->assertEqual($result, $expected);
  2415. $TestModel->recursive = -1;
  2416. $result = $TestModel->find('all', array('fields' => array('id', 'title')));
  2417. $expected = array(
  2418. array('Article' => array('id' => 1, 'title' => 'First Article' )),
  2419. array('Article' => array('id' => 2, 'title' => 'Second Article' )),
  2420. array('Article' => array('id' => 3, 'title' => 'Third Article' )),
  2421. array('Article' => array('id' => 4, 'title' => 'Fourth Article - New Title' )),
  2422. array('Article' => array('id' => 5, 'title' => 'Fifth Article - New Title 5' ))
  2423. );
  2424. $this->assertEqual($result, $expected);
  2425. }
  2426. /**
  2427. * testSaveWithNonExistentFields method
  2428. *
  2429. * @access public
  2430. * @return void
  2431. */
  2432. function testSaveWithNonExistentFields() {
  2433. $this->loadFixtures('Article');
  2434. $TestModel =& new Article();
  2435. $TestModel->recursive = -1;
  2436. $data = array(
  2437. 'non_existent' => 'This field does not exist',
  2438. 'user_id' => '1',
  2439. 'title' => 'Fourth Article - New Title',
  2440. 'body' => 'Fourth Article Body',
  2441. 'published' => 'N'
  2442. );
  2443. $result = $TestModel->create() && $TestModel->save($data);
  2444. $this->assertTrue($result);
  2445. $expected = array('Article' => array(
  2446. 'id' => '4',
  2447. 'user_id' => '1',
  2448. 'title' => 'Fourth Article - New Title',
  2449. 'body' => 'Fourth Article Body',
  2450. 'published' => 'N'
  2451. ));
  2452. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  2453. $this->assertEqual($result, $expected);
  2454. $data = array(
  2455. 'user_id' => '1',
  2456. 'non_existent' => 'This field does not exist',
  2457. 'title' => 'Fiveth Article - New Title',
  2458. 'body' => 'Fiveth Article Body',
  2459. 'published' => 'N'
  2460. );
  2461. $result = $TestModel->create() && $TestModel->save($data);
  2462. $this->assertTrue($result);
  2463. $expected = array('Article' => array(
  2464. 'id' => '5',
  2465. 'user_id' => '1',
  2466. 'title' => 'Fiveth Article - New Title',
  2467. 'body' => 'Fiveth Article Body',
  2468. 'published' => 'N'
  2469. ));
  2470. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  2471. $this->assertEqual($result, $expected);
  2472. }
  2473. /**
  2474. * testSaveFromXml method
  2475. *
  2476. * @access public
  2477. * @return void
  2478. */
  2479. function testSaveFromXml() {
  2480. $this->loadFixtures('Article');
  2481. App::import('Core', 'Xml');
  2482. $Article = new Article();
  2483. $Article->save(new Xml('<article title="test xml" user_id="5" />'));
  2484. $this->assertTrue($Article->save(new Xml('<article title="test xml" user_id="5" />')));
  2485. $results = $Article->find(array('Article.title' => 'test xml'));
  2486. $this->assertTrue($results);
  2487. }
  2488. /**
  2489. * testSaveHabtm method
  2490. *
  2491. * @access public
  2492. * @return void
  2493. */
  2494. function testSaveHabtm() {
  2495. $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
  2496. $TestModel =& new Article();
  2497. $result = $TestModel->findById(2);
  2498. $expected = array(
  2499. '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'),
  2500. 'User' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31'),
  2501. 'Comment' => array(
  2502. 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'),
  2503. 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')
  2504. ),
  2505. 'Tag' => array(
  2506. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  2507. array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')
  2508. )
  2509. );
  2510. $this->assertEqual($result, $expected);
  2511. $data = array(
  2512. 'Article' => array('id' => '2', 'title' => 'New Second Article'),
  2513. 'Tag' => array('Tag' => array(1, 2))
  2514. );
  2515. $this->assertTrue($TestModel->set($data));
  2516. $this->assertTrue($TestModel->save());
  2517. $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
  2518. $result = $TestModel->find(array('Article.id' => 2), array('id', 'user_id', 'title', 'body'));
  2519. $expected = array(
  2520. 'Article' => array('id' => '2', 'user_id' => '3', 'title' => 'New Second Article', 'body' => 'Second Article Body'),
  2521. 'Tag' => array(
  2522. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  2523. array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')
  2524. )
  2525. );
  2526. $this->assertEqual($result, $expected);
  2527. $data = array('Article' => array('id' => '2'), 'Tag' => array('Tag' => array(2, 3)));
  2528. $result = $TestModel->set($data);
  2529. $this->assertTrue($result);
  2530. $result = $TestModel->save();
  2531. $this->assertTrue($result);
  2532. $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
  2533. $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body'));
  2534. $expected = array(
  2535. 'Article' => array('id' => '2', 'user_id' => '3', 'title' => 'New Second Article', 'body' => 'Second Article Body'),
  2536. 'Tag' => array(
  2537. array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31'),
  2538. array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')
  2539. )
  2540. );
  2541. $this->assertEqual($result, $expected);
  2542. $data = array('Tag' => array('Tag' => array(1, 2, 3)));
  2543. $result = $TestModel->set($data);
  2544. $this->assertTrue($result);
  2545. $result = $TestModel->save();
  2546. $this->assertTrue($result);
  2547. $TestModel->unbindModel(array(
  2548. 'belongsTo' => array('User'),
  2549. 'hasMany' => array('Comment')
  2550. ));
  2551. $result = $TestModel->find(array('Article.id' => 2), array('id', 'user_id', 'title', 'body'));
  2552. $expected = array(
  2553. 'Article' => array('id' => '2', 'user_id' => '3', 'title' => 'New Second Article', 'body' => 'Second Article Body'),
  2554. 'Tag' => array(
  2555. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  2556. array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31'),
  2557. array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')
  2558. )
  2559. );
  2560. $this->assertEqual($result, $expected);
  2561. $data = array('Tag' => array('Tag' => array()));
  2562. $result = $TestModel->set($data);
  2563. $this->assertTrue($result);
  2564. $result = $TestModel->save();
  2565. $this->assertTrue($result);
  2566. $data = array('Tag' => array('Tag' => ''));
  2567. $result = $TestModel->set($data);
  2568. $this->assertTrue($result);
  2569. $result = $TestModel->save();
  2570. $this->assertTrue($result);
  2571. $TestModel->unbindModel(array(
  2572. 'belongsTo' => array('User'),
  2573. 'hasMany' => array('Comment')
  2574. ));
  2575. $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body'));
  2576. $expected = array(
  2577. 'Article' => array('id' => '2', 'user_id' => '3', 'title' => 'New Second Article', 'body' => 'Second Article Body'),
  2578. 'Tag' => array()
  2579. );
  2580. $this->assertEqual($result, $expected);
  2581. $data = array('Tag' => array('Tag' => array(2, 3)));
  2582. $result = $TestModel->set($data);
  2583. $this->assertTrue($result);
  2584. $result = $TestModel->save();
  2585. $this->assertTrue($result);
  2586. $TestModel->unbindModel(array(
  2587. 'belongsTo' => array('User'),
  2588. 'hasMany' => array('Comment')
  2589. ));
  2590. $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body'));
  2591. $expected = array(
  2592. 'Article' => array(
  2593. 'id' => '2', 'user_id' => '3', 'title' => 'New Second Article', 'body' => 'Second Article Body'
  2594. ),
  2595. 'Tag' => array(
  2596. array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31'),
  2597. array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')
  2598. )
  2599. );
  2600. $this->assertEqual($result, $expected);
  2601. $data = array('Tag' => array('Tag' => array(1, 2)), 'Article' => array('id' => '2', 'title' => 'New Second Article'));
  2602. $this->assertTrue($TestModel->set($data));
  2603. $this->assertTrue($TestModel->save());
  2604. $TestModel->unbindModel(array(
  2605. 'belongsTo' => array('User'),
  2606. 'hasMany' => array('Comment')
  2607. ));
  2608. $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body'));
  2609. $expected = array(
  2610. 'Article' => array(
  2611. 'id' => '2', 'user_id' => '3', 'title' => 'New Second Article', 'body' => 'Second Article Body'
  2612. ),
  2613. 'Tag' => array(
  2614. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  2615. array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')
  2616. )
  2617. );
  2618. $this->assertEqual($result, $expected);
  2619. $data = array('Tag' => array('Tag' => array(1, 2)), 'Article' => array('id' => '2', 'title' => 'New Second Article Title'));
  2620. $result = $TestModel->set($data);
  2621. $this->assertTrue($result);
  2622. $this->assertTrue($TestModel->save());
  2623. $TestModel->unbindModel(array(
  2624. 'belongsTo' => array('User'),
  2625. 'hasMany' => array('Comment')
  2626. ));
  2627. $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body'));
  2628. $expected = array(
  2629. 'Article' => array(
  2630. 'id' => '2', 'user_id' => '3', 'title' => 'New Second Article Title', 'body' => 'Second Article Body'
  2631. ),
  2632. 'Tag' => array(
  2633. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  2634. array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')
  2635. )
  2636. );
  2637. $this->assertEqual($result, $expected);
  2638. $data = array('Tag' => array('Tag' => array(2, 3)), 'Article' => array('id' => '2', 'title' => 'Changed Second Article'));
  2639. $this->assertTrue($TestModel->set($data));
  2640. $this->assertTrue($TestModel->save());
  2641. $TestModel->unbindModel(array(
  2642. 'belongsTo' => array('User'),
  2643. 'hasMany' => array('Comment')
  2644. ));
  2645. $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body'));
  2646. $expected = array(
  2647. 'Article' => array(
  2648. 'id' => '2', 'user_id' => '3', 'title' => 'Changed Second Article', 'body' => 'Second Article Body'
  2649. ),
  2650. 'Tag' => array(
  2651. array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31'),
  2652. array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')
  2653. )
  2654. );
  2655. $this->assertEqual($result, $expected);
  2656. $data = array(
  2657. 'Tag' => array(
  2658. 'Tag' => array( 1, 3 )
  2659. ),
  2660. 'Article' => array('id' => '2' ),
  2661. );
  2662. $result = $TestModel->set($data);
  2663. $this->assertTrue($result);
  2664. $result = $TestModel->save();
  2665. $this->assertTrue($result);
  2666. $TestModel->unbindModel(array(
  2667. 'belongsTo' => array('User'),
  2668. 'hasMany' => array('Comment')
  2669. ));
  2670. $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body'));
  2671. $expected = array(
  2672. 'Article' => array(
  2673. 'id' => '2', 'user_id' => '3', 'title' => 'Changed Second Article', 'body' => 'Second Article Body'
  2674. ),
  2675. 'Tag' => array(
  2676. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  2677. array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')
  2678. )
  2679. );
  2680. $this->assertEqual($result, $expected);
  2681. $data = array(
  2682. 'Article' => array('id' => 10, 'user_id' => '2', 'title' => 'New Article With Tags and fieldList', 'body' => 'New Article Body with Tags and fieldList', 'created' => '2007-03-18 14:55:23', 'updated' => '2007-03-18 14:57:31'),
  2683. 'Tag' => array('Tag' => array(1, 2, 3))
  2684. );
  2685. $result = $TestModel->create() && $TestModel->save($data, true, array('user_id', 'title', 'published'));
  2686. $this->assertTrue($result);
  2687. $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
  2688. $result = $TestModel->read();
  2689. $expected = array(
  2690. 'Article' => array('id' => 4, 'user_id' => 2, 'title' => 'New Article With Tags and fieldList', 'body' => '', 'published' => 'N', 'created' => '', 'updated' => ''),
  2691. 'Tag' => array(
  2692. 0 => array('id' => 1, 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  2693. 1 => array('id' => 2, 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31'),
  2694. 2 => array('id' => 3, 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')
  2695. )
  2696. );
  2697. $this->assertEqual($result, $expected);
  2698. $this->loadFixtures('JoinA', 'JoinC', 'JoinAC', 'JoinB', 'JoinAB');
  2699. $TestModel = new JoinA();
  2700. $TestModel->hasBelongsToMany['JoinC']['unique'] = true;
  2701. $data = array(
  2702. 'JoinA' => array(
  2703. 'id' => 1,
  2704. 'name' => 'Join A 1',
  2705. 'body' => 'Join A 1 Body',
  2706. ),
  2707. 'JoinC' => array(
  2708. 'JoinC' => array(
  2709. array('join_c_id' => 2, 'other' => 'new record'),
  2710. array('join_c_id' => 3, 'other' => 'new record')
  2711. )
  2712. )
  2713. );
  2714. $TestModel->save($data);
  2715. $result = $TestModel->read(null, 1);
  2716. $time = date('Y-M-D H:i:s');
  2717. $expected = array(4, 5);
  2718. $this->assertEqual(Set::extract('/JoinC/JoinAsJoinC/id', $result), $expected);
  2719. $expected = array('new record', 'new record');
  2720. $this->assertEqual(Set::extract('/JoinC/JoinAsJoinC/other', $result), $expected);
  2721. }
  2722. /**
  2723. * testSaveHabtmCustomKeys method
  2724. *
  2725. * @access public
  2726. * @return void
  2727. */
  2728. function testSaveHabtmCustomKeys() {
  2729. $this->loadFixtures('Story', 'StoriesTag', 'Tag');
  2730. $Story =& new Story();
  2731. $data = array('Story' => array('story' => '1'), 'Tag' => array('Tag' => array(2, 3)));
  2732. $result = $Story->set($data);
  2733. $this->assertTrue($result);
  2734. $result = $Story->save();
  2735. $this->assertTrue($result);
  2736. $result = $Story->find('all');
  2737. $expected = array(
  2738. array(
  2739. 'Story' => array('story' => 1, 'title' => 'First Story'),
  2740. 'Tag' => array(
  2741. array('id' => 2, 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31'),
  2742. array('id' => 3, 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')
  2743. )
  2744. ),
  2745. array(
  2746. 'Story' => array('story' => 2, 'title' => 'Second Story'),
  2747. 'Tag' => array()
  2748. )
  2749. );
  2750. $this->assertEqual($result, $expected);
  2751. }
  2752. /**
  2753. * testHabtmSaveKeyResolution method
  2754. *
  2755. * @access public
  2756. * @return void
  2757. */
  2758. function testHabtmSaveKeyResolution() {
  2759. $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies');
  2760. $ThePaper =& new ThePaper();
  2761. $ThePaper->id = 1;
  2762. $ThePaper->save(array('Monkey' => array(2, 3)));
  2763. $result = $ThePaper->findById(1);
  2764. $expected = array(
  2765. array('id' => '2', 'device_type_id' => '1', 'name' => 'Device 2', 'typ' => '1'),
  2766. array('id' => '3', 'device_type_id' => '1', 'name' => 'Device 3', 'typ' => '2')
  2767. );
  2768. $this->assertEqual($result['Monkey'], $expected);
  2769. $ThePaper->id = 2;
  2770. $ThePaper->save(array('Monkey' => array(1, 2, 3)));
  2771. $result = $ThePaper->findById(2);
  2772. $expected = array(
  2773. array('id' => '1', 'device_type_id' => '1', 'name' => 'Device 1', 'typ' => '1'),
  2774. array('id' => '2', 'device_type_id' => '1', 'name' => 'Device 2', 'typ' => '1'),
  2775. array('id' => '3', 'device_type_id' => '1', 'name' => 'Device 3', 'typ' => '2'),
  2776. );
  2777. $this->assertEqual($result['Monkey'], $expected);
  2778. $ThePaper->id = 2;
  2779. $ThePaper->save(array('Monkey' => array(1, 3)));
  2780. $result = $ThePaper->findById(2);
  2781. $expected = array(
  2782. array('id' => '1', 'device_type_id' => '1', 'name' => 'Device 1', 'typ' => '1'),
  2783. array('id' => '3', 'device_type_id' => '1', 'name' => 'Device 3', 'typ' => '2')
  2784. );
  2785. $this->assertEqual($result['Monkey'], $expected);
  2786. $result = $ThePaper->findById(1);
  2787. $expected = array(
  2788. array('id' => '2', 'device_type_id' => '1', 'name' => 'Device 2', 'typ' => '1'),
  2789. array('id' => '3', 'device_type_id' => '1', 'name' => 'Device 3', 'typ' => '2')
  2790. );
  2791. $this->assertEqual($result['Monkey'], $expected);
  2792. }
  2793. /**
  2794. * testHabtmDeleteLinksWhenNoPrimaryKeyInJoinTable method
  2795. *
  2796. * @access public
  2797. * @return void
  2798. */
  2799. function testHabtmDeleteLinksWhenNoPrimaryKeyInJoinTable() {
  2800. $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies');
  2801. $ThePaper =& new ThePaper();
  2802. $ThePaper->id = 1;
  2803. $ThePaper->save(array('Monkey' => array(2, 3)));
  2804. $result = $ThePaper->findById(1);
  2805. $expected = array(
  2806. array('id' => '2', 'device_type_id' => '1', 'name' => 'Device 2', 'typ' => '1'),
  2807. array('id' => '3', 'device_type_id' => '1', 'name' => 'Device 3', 'typ' => '2')
  2808. );
  2809. $this->assertEqual($result['Monkey'], $expected);
  2810. $ThePaper =& new ThePaper();
  2811. $ThePaper->id = 2;
  2812. $ThePaper->save(array('Monkey' => array(2, 3)));
  2813. $result = $ThePaper->findById(2);
  2814. $expected = array(
  2815. array('id' => '2', 'device_type_id' => '1', 'name' => 'Device 2', 'typ' => '1'),
  2816. array('id' => '3', 'device_type_id' => '1', 'name' => 'Device 3', 'typ' => '2')
  2817. );
  2818. $this->assertEqual($result['Monkey'], $expected);
  2819. $ThePaper->delete(1);
  2820. $result = $ThePaper->findById(2);
  2821. $expected = array(
  2822. array('id' => '2', 'device_type_id' => '1', 'name' => 'Device 2', 'typ' => '1'),
  2823. array('id' => '3', 'device_type_id' => '1', 'name' => 'Device 3', 'typ' => '2')
  2824. );
  2825. $this->assertEqual($result['Monkey'], $expected);
  2826. }
  2827. /**
  2828. * test that Caches are getting cleared on save().
  2829. * ensure that both inflections of controller names are getting cleared
  2830. * as url for controller could be either overallFavorites/index or overall_favorites/index
  2831. *
  2832. * @return void
  2833. **/
  2834. function testCacheClearOnSave() {
  2835. $_back = array(
  2836. 'check' => Configure::read('Cache.check'),
  2837. 'disable' => Configure::read('Cache.disable'),
  2838. );
  2839. Configure::write('Cache.check', true);
  2840. Configure::write('Cache.disable', false);
  2841. $this->loadFixtures('OverallFavorite');
  2842. $OverallFavorite =& new OverallFavorite();
  2843. touch(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php');
  2844. touch(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php');
  2845. $data = array(
  2846. 'OverallFavorite' => array(
  2847. 'model_type' => '8-track',
  2848. 'model_id' => '3',
  2849. 'priority' => '1'
  2850. )
  2851. );
  2852. $OverallFavorite->create($data);
  2853. $OverallFavorite->save();
  2854. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php'));
  2855. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php'));
  2856. Configure::write('Cache.check', $_back['check']);
  2857. Configure::write('Cache.disable', $_back['disable']);
  2858. }
  2859. /**
  2860. * testSaveAll method
  2861. *
  2862. * @access public
  2863. * @return void
  2864. */
  2865. function testSaveAll() {
  2866. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  2867. $TestModel =& new Post();
  2868. $result = $TestModel->find('all');
  2869. $this->assertEqual(count($result), 3);
  2870. $this->assertFalse(isset($result[3]));
  2871. $ts = date('Y-m-d H:i:s');
  2872. $TestModel->saveAll(array(
  2873. 'Post' => array('title' => 'Post with Author', 'body' => 'This post will be saved with an author'),
  2874. 'Author' => array('user' => 'bob', 'password' => '5f4dcc3b5aa765d61d8327deb882cf90')
  2875. ));
  2876. $result = $TestModel->find('all');
  2877. $expected = array(
  2878. 'Post' => array('id' => '4', 'author_id' => '5', 'title' => 'Post with Author', 'body' => 'This post will be saved with an author', 'published' => 'N', 'created' => $ts, 'updated' => $ts),
  2879. 'Author' => array('id' => '5', 'user' => 'bob', 'password' => '5f4dcc3b5aa765d61d8327deb882cf90', 'created' => $ts, 'updated' => $ts, 'test' => 'working')
  2880. );
  2881. $this->assertEqual($result[3], $expected);
  2882. $this->assertEqual(count($result), 4);
  2883. $TestModel->deleteAll(true);
  2884. $this->assertEqual($TestModel->find('all'), array());
  2885. // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
  2886. $this->db->truncate($TestModel);
  2887. $ts = date('Y-m-d H:i:s');
  2888. $TestModel->saveAll(array(
  2889. array('title' => 'Multi-record post 1', 'body' => 'First multi-record post', 'author_id' => 2),
  2890. array('title' => 'Multi-record post 2', 'body' => 'Second multi-record post', 'author_id' => 2)
  2891. ));
  2892. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  2893. $expected = array(
  2894. array('Post' => array('id' => '1', 'author_id' => '2', 'title' => 'Multi-record post 1', 'body' => 'First multi-record post', 'published' => 'N', 'created' => $ts, 'updated' => $ts)),
  2895. array('Post' => array('id' => '2', 'author_id' => '2', 'title' => 'Multi-record post 2', 'body' => 'Second multi-record post', 'published' => 'N', 'created' => $ts, 'updated' => $ts))
  2896. );
  2897. $this->assertEqual($result, $expected);
  2898. $TestModel =& new Comment();
  2899. $ts = date('Y-m-d H:i:s');
  2900. $result = $TestModel->saveAll(array(
  2901. 'Comment' => array('article_id' => 2, 'user_id' => 2, 'comment' => 'New comment with attachment', 'published' => 'Y'),
  2902. 'Attachment' => array('attachment' => 'some_file.tgz')
  2903. ));
  2904. $this->assertTrue($result);
  2905. $result = $TestModel->find('all');
  2906. $expected = array('id' => '7', 'article_id' => '2', 'user_id' => '2', 'comment' => 'New comment with attachment', 'published' => 'Y', 'created' => $ts, 'updated' => $ts);
  2907. $this->assertEqual($result[6]['Comment'], $expected);
  2908. $expected = array('id' => '7', 'article_id' => '2', 'user_id' => '2', 'comment' => 'New comment with attachment', 'published' => 'Y', 'created' => $ts, 'updated' => $ts);
  2909. $this->assertEqual($result[6]['Comment'], $expected);
  2910. $expected = array('id' => '2', 'comment_id' => '7', 'attachment' => 'some_file.tgz', 'created' => $ts, 'updated' => $ts);
  2911. $this->assertEqual($result[6]['Attachment'], $expected);
  2912. }
  2913. /**
  2914. * Test SaveAll with Habtm relations
  2915. *
  2916. * @access public
  2917. * @return void
  2918. */
  2919. function testSaveAllHabtm() {
  2920. $this->loadFixtures('Article', 'Tag', 'Comment', 'User');
  2921. $data = array(
  2922. 'Article' => array(
  2923. 'user_id' => 1, 'title' => 'Article Has and belongs to Many Tags'
  2924. ),
  2925. 'Tag' => array(
  2926. 'Tag' => array(
  2927. 1, 2
  2928. )
  2929. ),
  2930. 'Comment' => array(
  2931. array('comment' => 'Article comment', 'user_id' => 1),
  2932. ),
  2933. );
  2934. $Article =& new Article();
  2935. $result = $Article->saveAll($data);
  2936. $this->assertTrue($result);
  2937. $result = $Article->read();
  2938. $this->assertEqual(count($result['Tag']), 2);
  2939. $this->assertEqual($result['Tag'][0]['tag'], 'tag1');
  2940. $this->assertEqual(count($result['Comment']), 1);
  2941. $this->assertEqual(count($result['Comment'][0]['comment']['Article comment']), 1);
  2942. }
  2943. /**
  2944. * Test SaveAll with Habtm relations and extra join table fields
  2945. *
  2946. * @access public
  2947. * @return void
  2948. */
  2949. function testSaveAllHabtmWithExtraJoinTableFields() {
  2950. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  2951. $data = array(
  2952. 'Something' => array(
  2953. 'id' => 4,
  2954. 'title' => 'Extra Fields',
  2955. 'body' => 'Extra Fields Body',
  2956. 'published' => '1'
  2957. ),
  2958. 'SomethingElse' => array(
  2959. array('something_else_id' => 1, 'doomed' => '1'),
  2960. array('something_else_id' => 2, 'doomed' => '0'),
  2961. array('something_else_id' => 3, 'doomed' => '1')
  2962. )
  2963. );
  2964. $Something =& new Something();
  2965. $result = $Something->saveAll($data);
  2966. $this->assertTrue($result);
  2967. $result = $Something->read();
  2968. $this->assertEqual(count($result['SomethingElse']), 3);
  2969. $this->assertTrue(Set::matches('/Something[id=4]', $result));
  2970. $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
  2971. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
  2972. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
  2973. $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
  2974. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
  2975. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
  2976. $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
  2977. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
  2978. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
  2979. }
  2980. /**
  2981. * testSaveAllHasOne method
  2982. *
  2983. * @access public
  2984. * @return void
  2985. */
  2986. function testSaveAllHasOne() {
  2987. $model = new Comment();
  2988. $model->deleteAll(true);
  2989. $this->assertEqual($model->find('all'), array());
  2990. $model->Attachment->deleteAll(true);
  2991. $this->assertEqual($model->Attachment->find('all'), array());
  2992. $this->assertTrue($model->saveAll(array(
  2993. 'Comment' => array('comment' => 'Comment with attachment', 'article_id' => 1, 'user_id' => 1),
  2994. 'Attachment' => array('attachment' => 'some_file.zip')
  2995. )));
  2996. $result = $model->find('all', array('fields' => array(
  2997. 'Comment.id', 'Comment.comment', 'Attachment.id', 'Attachment.comment_id', 'Attachment.attachment'
  2998. )));
  2999. $expected = array(array(
  3000. 'Comment' => array('id' => '1', 'comment' => 'Comment with attachment'),
  3001. 'Attachment' => array('id' => '1', 'comment_id' => '1', 'attachment' => 'some_file.zip')
  3002. ));
  3003. $this->assertEqual($result, $expected);
  3004. }
  3005. /**
  3006. * testSaveAllBelongsTo method
  3007. *
  3008. * @access public
  3009. * @return void
  3010. */
  3011. function testSaveAllBelongsTo() {
  3012. $model = new Comment();
  3013. $model->deleteAll(true);
  3014. $this->assertEqual($model->find('all'), array());
  3015. $model->Article->deleteAll(true);
  3016. $this->assertEqual($model->Article->find('all'), array());
  3017. $this->assertTrue($model->saveAll(array(
  3018. 'Comment' => array('comment' => 'Article comment', 'article_id' => 1, 'user_id' => 1),
  3019. 'Article' => array('title' => 'Model Associations 101', 'user_id' => 1)
  3020. )));
  3021. $result = $model->find('all', array('fields' => array(
  3022. 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
  3023. )));
  3024. $expected = array(array(
  3025. 'Comment' => array('id' => '1', 'article_id' => '1', 'comment' => 'Article comment'),
  3026. 'Article' => array('id' => '1', 'title' => 'Model Associations 101')
  3027. ));
  3028. $this->assertEqual($result, $expected);
  3029. }
  3030. /**
  3031. * testSaveAllHasOneValidation method
  3032. *
  3033. * @access public
  3034. * @return void
  3035. */
  3036. function testSaveAllHasOneValidation() {
  3037. $model = new Comment();
  3038. $model->deleteAll(true);
  3039. $this->assertEqual($model->find('all'), array());
  3040. $model->Attachment->deleteAll(true);
  3041. $this->assertEqual($model->Attachment->find('all'), array());
  3042. $model->validate = array('comment' => 'notEmpty');
  3043. $model->Attachment->validate = array('attachment' => 'notEmpty');
  3044. $model->Attachment->bind('Comment');
  3045. $this->assertFalse($model->saveAll(
  3046. array(
  3047. 'Comment' => array('comment' => '', 'article_id' => 1, 'user_id' => 1),
  3048. 'Attachment' => array('attachment' => '')
  3049. ),
  3050. array('validate' => 'first')
  3051. ));
  3052. $expected = array(
  3053. 'Comment' => array('comment' => 'This field cannot be left blank'),
  3054. 'Attachment' => array('attachment' => 'This field cannot be left blank')
  3055. );
  3056. $this->assertEqual($model->validationErrors, $expected['Comment']);
  3057. $this->assertEqual($model->Attachment->validationErrors, $expected['Attachment']);
  3058. $this->assertFalse($model->saveAll(
  3059. array(
  3060. 'Comment' => array('comment' => '', 'article_id' => 1, 'user_id' => 1),
  3061. 'Attachment' => array('attachment' => '')
  3062. ),
  3063. array('validate' => 'only')
  3064. ));
  3065. $this->assertEqual($model->validationErrors, $expected['Comment']);
  3066. $this->assertEqual($model->Attachment->validationErrors, $expected['Attachment']);
  3067. }
  3068. /**
  3069. * testSaveAllAtomic method
  3070. *
  3071. * @access public
  3072. * @return void
  3073. */
  3074. function testSaveAllAtomic() {
  3075. $this->loadFixtures('Article', 'User');
  3076. $TestModel =& new Article();
  3077. $result = $TestModel->saveAll(array(
  3078. 'Article' => array('title' => 'Post with Author', 'body' => 'This post will be saved with an author', 'user_id' => 2),
  3079. 'Comment' => array(array('comment' => 'First new comment', 'user_id' => 2))
  3080. ), array('atomic' => false));
  3081. $this->assertIdentical($result, array('Article' => true, 'Comment' => array(true)));
  3082. $result = $TestModel->saveAll(array(
  3083. array('id' => '1', 'title' => 'Baleeted First Post', 'body' => 'Baleeted!', 'published' => 'N'),
  3084. array('id' => '2', 'title' => 'Just update the title'),
  3085. array('title' => 'Creating a fourth post', 'body' => 'Fourth post body', 'user_id' => 2)
  3086. ), array('atomic' => false));
  3087. $this->assertIdentical($result, array(true, true, true));
  3088. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  3089. $result = $TestModel->saveAll(array(
  3090. array('id' => '1', 'title' => 'Un-Baleeted First Post', 'body' => 'Not Baleeted!', 'published' => 'Y'),
  3091. array('id' => '2', 'title' => '', 'body' => 'Trying to get away with an empty title'),
  3092. ), array('atomic' => false));
  3093. $this->assertIdentical($result, array(true, false));
  3094. $result = $TestModel->saveAll(array(
  3095. 'Article' => array('id' => 2),
  3096. 'Comment' => array(
  3097. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  3098. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3099. )
  3100. ), array('atomic' => false));
  3101. $this->assertIdentical($result, array('Article' => true, 'Comment' => array(true, true)));
  3102. }
  3103. /**
  3104. * testSaveAllHasMany method
  3105. *
  3106. * @access public
  3107. * @return void
  3108. */
  3109. function testSaveAllHasMany() {
  3110. $this->loadFixtures('Article', 'Comment');
  3111. $TestModel =& new Article();
  3112. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  3113. $result = $TestModel->saveAll(array(
  3114. 'Article' => array('id' => 2),
  3115. 'Comment' => array(
  3116. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  3117. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3118. )
  3119. ));
  3120. $this->assertTrue($result);
  3121. $result = $TestModel->findById(2);
  3122. $expected = array('First Comment for Second Article', 'Second Comment for Second Article', 'First new comment', 'Second new comment');
  3123. $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected);
  3124. $result = $TestModel->saveAll(
  3125. array(
  3126. 'Article' => array('id' => 2),
  3127. 'Comment' => array(
  3128. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 1),
  3129. )
  3130. ),
  3131. array('atomic' => false)
  3132. );
  3133. $this->assertTrue($result);
  3134. $result = $TestModel->findById(2);
  3135. $expected = array('First Comment for Second Article', 'Second Comment for Second Article', 'First new comment', 'Second new comment', 'Third new comment');
  3136. $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected);
  3137. $TestModel->beforeSaveReturn = false;
  3138. $result = $TestModel->saveAll(
  3139. array(
  3140. 'Article' => array('id' => 2),
  3141. 'Comment' => array(
  3142. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 1),
  3143. )
  3144. ),
  3145. array('atomic' => false)
  3146. );
  3147. $this->assertEqual($result, array('Article' => false));
  3148. $result = $TestModel->findById(2);
  3149. $expected = array('First Comment for Second Article', 'Second Comment for Second Article', 'First new comment', 'Second new comment', 'Third new comment');
  3150. $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected);
  3151. }
  3152. /**
  3153. * testSaveAllHasManyValidation method
  3154. *
  3155. * @access public
  3156. * @return void
  3157. */
  3158. function testSaveAllHasManyValidation() {
  3159. $this->loadFixtures('Article', 'Comment');
  3160. $TestModel =& new Article();
  3161. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  3162. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  3163. $result = $TestModel->saveAll(array(
  3164. 'Article' => array('id' => 2),
  3165. 'Comment' => array(
  3166. array('comment' => '', 'published' => 'Y', 'user_id' => 1),
  3167. )
  3168. ));
  3169. $expected = array('Comment' => array(false));
  3170. $this->assertEqual($result, $expected);
  3171. $expected = array('Comment' => array(
  3172. array('comment' => 'This field cannot be left blank')
  3173. ));
  3174. $this->assertEqual($TestModel->validationErrors, $expected);
  3175. $expected = array(
  3176. array('comment' => 'This field cannot be left blank')
  3177. );
  3178. $this->assertEqual($TestModel->Comment->validationErrors, $expected);
  3179. $result = $TestModel->saveAll(array(
  3180. 'Article' => array('id' => 2),
  3181. 'Comment' => array(
  3182. array('comment' => '', 'published' => 'Y', 'user_id' => 1),
  3183. )
  3184. ), array('validate' => 'only'));
  3185. }
  3186. /**
  3187. * testSaveAllTransaction method
  3188. *
  3189. * @access public
  3190. * @return void
  3191. */
  3192. function testSaveAllTransaction() {
  3193. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  3194. $TestModel =& new Post();
  3195. $TestModel->validate = array('title' => 'notEmpty');
  3196. $data = array(
  3197. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3198. array('author_id' => 1, 'title' => 'New Fifth Post'),
  3199. array('author_id' => 1, 'title' => '')
  3200. );
  3201. $ts = date('Y-m-d H:i:s');
  3202. $this->assertFalse($TestModel->saveAll($data));
  3203. $result = $TestModel->find('all', array('recursive' => -1));
  3204. $expected = array(
  3205. array('Post' => array('id' => '1', 'author_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')),
  3206. array('Post' => array('id' => '2', 'author_id' => 3, 'title' => 'Second Post', 'body' => 'Second Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31')),
  3207. array('Post' => array('id' => '3', 'author_id' => 1, 'title' => 'Third Post', 'body' => 'Third Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'))
  3208. );
  3209. if (count($result) != 3) {
  3210. // Database doesn't support transactions
  3211. $expected[] = array('Post' => array('id' => '4', 'author_id' => 1, 'title' => 'New Fourth Post', 'body' => null, 'published' => 'N', 'created' => $ts, 'updated' => $ts));
  3212. $expected[] = array('Post' => array('id' => '5', 'author_id' => 1, 'title' => 'New Fifth Post', 'body' => null, 'published' => 'N', 'created' => $ts, 'updated' => $ts));
  3213. $this->assertEqual($result, $expected);
  3214. // Skip the rest of the transactional tests
  3215. return;
  3216. }
  3217. $this->assertEqual($result, $expected);
  3218. $data = array(
  3219. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3220. array('author_id' => 1, 'title' => ''),
  3221. array('author_id' => 1, 'title' => 'New Sixth Post')
  3222. );
  3223. $ts = date('Y-m-d H:i:s');
  3224. $this->assertFalse($TestModel->saveAll($data));
  3225. $result = $TestModel->find('all', array('recursive' => -1));
  3226. $expected = array(
  3227. array('Post' => array('id' => '1', 'author_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')),
  3228. array('Post' => array('id' => '2', 'author_id' => 3, 'title' => 'Second Post', 'body' => 'Second Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31')),
  3229. array('Post' => array('id' => '3', 'author_id' => 1, 'title' => 'Third Post', 'body' => 'Third Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'))
  3230. );
  3231. if (count($result) != 3) {
  3232. // Database doesn't support transactions
  3233. $expected[] = array('Post' => array('id' => '4', 'author_id' => 1, 'title' => 'New Fourth Post', 'body' => 'Third Post Body', 'published' => 'N', 'created' => $ts, 'updated' => $ts));
  3234. $expected[] = array('Post' => array('id' => '5', 'author_id' => 1, 'title' => 'Third Post', 'body' => 'Third Post Body', 'published' => 'N', 'created' => $ts, 'updated' => $ts));
  3235. }
  3236. $this->assertEqual($result, $expected);
  3237. $TestModel->validate = array('title' => 'notEmpty');
  3238. $data = array(
  3239. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3240. array('author_id' => 1, 'title' => 'New Fifth Post'),
  3241. array('author_id' => 1, 'title' => 'New Sixth Post')
  3242. );
  3243. $this->assertTrue($TestModel->saveAll($data));
  3244. $result = $TestModel->find('all', array('recursive' => -1, 'fields' => array('author_id', 'title','body','published')));
  3245. $expected = array(
  3246. array('Post' => array('author_id' => 1, 'title' => 'First Post', 'body' => 'First Post Body', 'published' => 'Y')),
  3247. array('Post' => array('author_id' => 3, 'title' => 'Second Post', 'body' => 'Second Post Body', 'published' => 'Y')),
  3248. array('Post' => array('author_id' => 1, 'title' => 'Third Post', 'body' => 'Third Post Body', 'published' => 'Y')),
  3249. array('Post' => array('author_id' => 1, 'title' => 'New Fourth Post', 'body' => '', 'published' => 'N')),
  3250. array('Post' => array('author_id' => 1, 'title' => 'New Fifth Post', 'body' => '', 'published' => 'N')),
  3251. array('Post' => array('author_id' => 1, 'title' => 'New Sixth Post', 'body' => '', 'published' => 'N'))
  3252. );
  3253. $this->assertEqual($result, $expected);
  3254. }
  3255. /**
  3256. * testSaveAllValidation method
  3257. *
  3258. * @access public
  3259. * @return void
  3260. */
  3261. function testSaveAllValidation() {
  3262. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  3263. $TestModel =& new Post();
  3264. $data = array(
  3265. array('id' => '1', 'title' => 'Baleeted First Post', 'body' => 'Baleeted!', 'published' => 'N'),
  3266. array('id' => '2', 'title' => 'Just update the title'),
  3267. array('title' => 'Creating a fourth post', 'body' => 'Fourth post body', 'author_id' => 2)
  3268. );
  3269. $this->assertTrue($TestModel->saveAll($data));
  3270. $result = $TestModel->find('all', array('recursive' => -1));
  3271. $ts = date('Y-m-d H:i:s');
  3272. $expected = array(
  3273. array('Post' => array('id' => '1', 'author_id' => '1', 'title' => 'Baleeted First Post', 'body' => 'Baleeted!', 'published' => 'N', 'created' => '2007-03-18 10:39:23', 'updated' => $ts)),
  3274. array('Post' => array('id' => '2', 'author_id' => '3', 'title' => 'Just update the title', 'body' => 'Second Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => $ts)),
  3275. array('Post' => array('id' => '3', 'author_id' => '1', 'title' => 'Third Post', 'body' => 'Third Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31')),
  3276. array('Post' => array('id' => '4', 'author_id' => '2', 'title' => 'Creating a fourth post', 'body' => 'Fourth post body', 'published' => 'N', 'created' => $ts, 'updated' => $ts))
  3277. );
  3278. $this->assertEqual($result, $expected);
  3279. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  3280. $data = array(
  3281. array('id' => '1', 'title' => 'Un-Baleeted First Post', 'body' => 'Not Baleeted!', 'published' => 'Y'),
  3282. array('id' => '2', 'title' => '', 'body' => 'Trying to get away with an empty title'),
  3283. );
  3284. $result = $TestModel->saveAll($data);
  3285. $this->assertEqual($result, false);
  3286. $result = $TestModel->find('all', array('recursive' => -1));
  3287. $errors = array(1 => array('title' => 'This field cannot be left blank'));
  3288. $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
  3289. if (!$transactionWorked) {
  3290. $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
  3291. $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
  3292. }
  3293. $this->assertEqual($TestModel->validationErrors, $errors);
  3294. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  3295. $data = array(
  3296. array('id' => '1', 'title' => 'Un-Baleeted First Post', 'body' => 'Not Baleeted!', 'published' => 'Y'),
  3297. array('id' => '2', 'title' => '', 'body' => 'Trying to get away with an empty title'),
  3298. );
  3299. $result = $TestModel->saveAll($data, array('atomic' => false));
  3300. $this->assertEqual($result, array(true, false));
  3301. $result = $TestModel->find('all', array('recursive' => -1));
  3302. $errors = array(1 => array('title' => 'This field cannot be left blank'));
  3303. $newTs = date('Y-m-d H:i:s');
  3304. $expected = array(
  3305. array('Post' => array('id' => '1', 'author_id' => '1', 'title' => 'Un-Baleeted First Post', 'body' => 'Not Baleeted!', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => $newTs)),
  3306. array('Post' => array('id' => '2', 'author_id' => '3', 'title' => 'Just update the title', 'body' => 'Second Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => $ts)),
  3307. array('Post' => array('id' => '3', 'author_id' => '1', 'title' => 'Third Post', 'body' => 'Third Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31')),
  3308. array('Post' => array('id' => '4', 'author_id' => '2', 'title' => 'Creating a fourth post', 'body' => 'Fourth post body', 'published' => 'N', 'created' => $ts, 'updated' => $ts))
  3309. );
  3310. $this->assertEqual($result, $expected);
  3311. $this->assertEqual($TestModel->validationErrors, $errors);
  3312. $data = array(
  3313. array('id' => '1', 'title' => 'Re-Baleeted First Post', 'body' => 'Baleeted!', 'published' => 'N'),
  3314. array('id' => '2', 'title' => '', 'body' => 'Trying to get away with an empty title'),
  3315. );
  3316. $this->assertFalse($TestModel->saveAll($data, array('validate' => 'first')));
  3317. $result = $TestModel->find('all', array('recursive' => -1));
  3318. $this->assertEqual($result, $expected);
  3319. $this->assertEqual($TestModel->validationErrors, $errors);
  3320. $data = array(
  3321. array('title' => 'First new post', 'body' => 'Woohoo!', 'published' => 'Y'),
  3322. array('title' => 'Empty body', 'body' => '')
  3323. );
  3324. $TestModel->validate['body'] = 'notEmpty';
  3325. }
  3326. /**
  3327. * testSaveAllValidationOnly method
  3328. *
  3329. * @access public
  3330. * @return void
  3331. */
  3332. function testSaveAllValidationOnly() {
  3333. $TestModel =& new Comment();
  3334. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  3335. $data = array(
  3336. 'Comment' => array(
  3337. 'comment' => 'This is the comment'
  3338. ),
  3339. 'Attachment' => array(
  3340. 'attachment' => ''
  3341. )
  3342. );
  3343. $result = $TestModel->saveAll($data, array('validate' => 'only'));
  3344. $this->assertFalse($result);
  3345. $TestModel =& new Article();
  3346. $TestModel->validate = array('title' => 'notEmpty');
  3347. $result = $TestModel->saveAll(
  3348. array(
  3349. 0 => array('title' => ''),
  3350. 1 => array('title' => 'title 1'),
  3351. 2 => array('title' => 'title 2'),
  3352. ),
  3353. array('validate'=>'only')
  3354. );
  3355. $this->assertFalse($result);
  3356. $expected = array(
  3357. 0 => array('title' => 'This field cannot be left blank'),
  3358. );
  3359. $this->assertEqual($TestModel->validationErrors, $expected);
  3360. $result = $TestModel->saveAll(
  3361. array(
  3362. 0 => array('title' => 'title 0'),
  3363. 1 => array('title' => ''),
  3364. 2 => array('title' => 'title 2'),
  3365. ),
  3366. array('validate'=>'only')
  3367. );
  3368. $this->assertFalse($result);
  3369. $expected = array(
  3370. 1 => array('title' => 'This field cannot be left blank'),
  3371. );
  3372. $this->assertEqual($TestModel->validationErrors, $expected);
  3373. }
  3374. /**
  3375. * testSaveAllValidateFirst method
  3376. *
  3377. * @access public
  3378. * @return void
  3379. */
  3380. function testSaveAllValidateFirst() {
  3381. $model =& new Article();
  3382. $model->deleteAll(true);
  3383. $model->Comment->validate = array('comment' => 'notEmpty');
  3384. $result = $model->saveAll(array(
  3385. 'Article' => array('title' => 'Post with Author', 'body' => 'This post will be saved author'),
  3386. 'Comment' => array(
  3387. array('comment' => 'First new comment'),
  3388. array('comment' => '')
  3389. )
  3390. ), array('validate' => 'first'));
  3391. $this->assertFalse($result);
  3392. $result = $model->find('all');
  3393. $this->assertEqual($result, array());
  3394. $expected = array('Comment' => array(1 => array('comment' => 'This field cannot be left blank')));
  3395. $this->assertEqual($model->Comment->validationErrors, $expected['Comment']);
  3396. $this->assertIdentical($model->Comment->find('count'), 0);
  3397. $result = $model->saveAll(
  3398. array(
  3399. 'Article' => array('title' => 'Post with Author', 'body' => 'This post will be saved with an author', 'user_id' => 2),
  3400. 'Comment' => array(array('comment' => 'Only new comment', 'user_id' => 2))
  3401. ),
  3402. array('validate' => 'first')
  3403. );
  3404. $this->assertIdentical($result, true);
  3405. $result = $model->Comment->find('all');
  3406. $this->assertIdentical(count($result), 1);
  3407. $result = Set::extract('/Comment/article_id', $result);
  3408. $this->assertTrue($result[0] === 1 || $result[0] === '1');
  3409. $model->deleteAll(true);
  3410. $data = array(
  3411. 'Article' => array(
  3412. 'title' => 'Post with Author saveAlled from comment',
  3413. 'body' => 'This post will be saved with an author',
  3414. 'user_id' => 2
  3415. ),
  3416. 'Comment' => array(
  3417. 'comment' => 'Only new comment', 'user_id' => 2
  3418. )
  3419. );
  3420. $result = $model->Comment->saveAll($data, array('validate' => 'first'));
  3421. $this->assertTrue($result);
  3422. $result = $model->find('all');
  3423. $this->assertEqual($result[0]['Article']['title'], 'Post with Author saveAlled from comment');
  3424. $this->assertEqual($result[0]['Comment'][0]['comment'], 'Only new comment');
  3425. }
  3426. /**
  3427. * testSaveWithCounterCache method
  3428. *
  3429. * @access public
  3430. * @return void
  3431. */
  3432. function testSaveWithCounterCache() {
  3433. $this->loadFixtures('Syfile', 'Item');
  3434. $TestModel =& new Syfile();
  3435. $TestModel2 =& new Item();
  3436. $result = $TestModel->findById(1);
  3437. $this->assertIdentical($result['Syfile']['item_count'], null);
  3438. $TestModel2->save(array('name' => 'Item 7', 'syfile_id' => 1, 'published' => false));
  3439. $result = $TestModel->findById(1);
  3440. $this->assertIdentical($result['Syfile']['item_count'], '2');
  3441. $TestModel2->delete(1);
  3442. $result = $TestModel->findById(1);
  3443. $this->assertIdentical($result['Syfile']['item_count'], '1');
  3444. $TestModel2->id = 2;
  3445. $TestModel2->saveField('syfile_id', 1);
  3446. $result = $TestModel->findById(1);
  3447. $this->assertIdentical($result['Syfile']['item_count'], '2');
  3448. $result = $TestModel->findById(2);
  3449. $this->assertIdentical($result['Syfile']['item_count'], '0');
  3450. }
  3451. /**
  3452. * Tests that counter caches are updated when records are added
  3453. *
  3454. * @access public
  3455. * @return void
  3456. */
  3457. function testCounterCacheIncrease() {
  3458. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  3459. $User = new CounterCacheUser();
  3460. $Post = new CounterCachePost();
  3461. $data = array('Post' => array('title' => 'New Post', 'user_id' => 66));
  3462. $Post->save($data);
  3463. $user = $User->find('first', array(
  3464. 'conditions' => array('id' => 66),'recursive' => -1
  3465. ));
  3466. $result = $user[$User->alias]['post_count'];
  3467. $expected = 3;
  3468. $this->assertEqual($result, $expected);
  3469. }
  3470. /**
  3471. * Tests that counter caches are updated when records are deleted
  3472. *
  3473. * @access public
  3474. * @return void
  3475. */
  3476. function testCounterCacheDecrease() {
  3477. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  3478. $User = new CounterCacheUser();
  3479. $Post = new CounterCachePost();
  3480. $Post->del(2);
  3481. $user = $User->find('first', array(
  3482. 'conditions' => array('id' => 66),'recursive' => -1
  3483. ));
  3484. $result = $user[$User->alias]['post_count'];
  3485. $expected = 1;
  3486. $this->assertEqual($result, $expected);
  3487. }
  3488. /**
  3489. * Tests that counter caches are updated when foreign keys of counted records change
  3490. *
  3491. * @access public
  3492. * @return void
  3493. */
  3494. function testCounterCacheUpdated() {
  3495. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  3496. $User = new CounterCacheUser();
  3497. $Post = new CounterCachePost();
  3498. $data = $Post->find('first', array(
  3499. 'conditions' => array('id' => 1),'recursive' => -1
  3500. ));
  3501. $data[$Post->alias]['user_id'] = 301;
  3502. $Post->save($data);
  3503. $users = $User->find('all',array('order' => 'User.id'));
  3504. $this->assertEqual($users[0]['User']['post_count'], 1);
  3505. $this->assertEqual($users[1]['User']['post_count'], 2);
  3506. }
  3507. /**
  3508. * test Counter Cache With Self Joining table
  3509. *
  3510. * @return void
  3511. * @access public
  3512. */
  3513. function testCounterCacheWithSelfJoin() {
  3514. $this->loadFixtures('CategoryThread');
  3515. $this->db->query('ALTER TABLE '. $this->db->fullTableName('category_threads') . " ADD column child_count INT(11) DEFAULT '0'");
  3516. $Category =& new CategoryThread();
  3517. $result = $Category->updateAll(array('CategoryThread.name' => "'updated'"), array('CategoryThread.parent_id' => 5));
  3518. $this->assertTrue($result);
  3519. $Category =& new CategoryThread();
  3520. $Category->belongsTo['ParentCategory']['counterCache'] = 'child_count';
  3521. $Category->updateCounterCache(array('parent_id' => 5));
  3522. $result = Set::extract($Category->find('all', array('conditions' => array('CategoryThread.id' => 5))), '{n}.CategoryThread.child_count');
  3523. $expected = array_fill(0, 1, 1);
  3524. $this->assertEqual($result, $expected);
  3525. }
  3526. /**
  3527. * testSaveWithCounterCacheScope method
  3528. *
  3529. * @access public
  3530. * @return void
  3531. */
  3532. function testSaveWithCounterCacheScope() {
  3533. $this->loadFixtures('Syfile', 'Item');
  3534. $TestModel =& new Syfile();
  3535. $TestModel2 =& new Item();
  3536. $TestModel2->belongsTo['Syfile']['counterCache'] = true;
  3537. $TestModel2->belongsTo['Syfile']['counterScope'] = array('published' => true);
  3538. $result = $TestModel->findById(1);
  3539. $this->assertIdentical($result['Syfile']['item_count'], null);
  3540. $TestModel2->save(array('name' => 'Item 7', 'syfile_id' => 1, 'published'=> true));
  3541. $result = $TestModel->findById(1);
  3542. $this->assertIdentical($result['Syfile']['item_count'], '1');
  3543. $TestModel2->id = 1;
  3544. $TestModel2->saveField('published', true);
  3545. $result = $TestModel->findById(1);
  3546. $this->assertIdentical($result['Syfile']['item_count'], '2');
  3547. }
  3548. /**
  3549. * testDel method
  3550. *
  3551. * @access public
  3552. * @return void
  3553. */
  3554. function testDel() {
  3555. $this->loadFixtures('Article');
  3556. $TestModel =& new Article();
  3557. $result = $TestModel->del(2);
  3558. $this->assertTrue($result);
  3559. $result = $TestModel->read(null, 2);
  3560. $this->assertFalse($result);
  3561. $TestModel->recursive = -1;
  3562. $result = $TestModel->find('all', array('fields' => array('id', 'title')));
  3563. $expected = array(
  3564. array('Article' => array('id' => 1, 'title' => 'First Article' )),
  3565. array('Article' => array('id' => 3, 'title' => 'Third Article' ))
  3566. );
  3567. $this->assertEqual($result, $expected);
  3568. $result = $TestModel->del(3);
  3569. $this->assertTrue($result);
  3570. $result = $TestModel->read(null, 3);
  3571. $this->assertFalse($result);
  3572. $TestModel->recursive = -1;
  3573. $result = $TestModel->find('all', array('fields' => array('id', 'title')));
  3574. $expected = array(
  3575. array('Article' => array('id' => 1, 'title' => 'First Article' ))
  3576. );
  3577. $this->assertEqual($result, $expected);
  3578. }
  3579. /**
  3580. * testDeleteAll method
  3581. *
  3582. * @access public
  3583. * @return void
  3584. */
  3585. function testDeleteAll() {
  3586. $this->loadFixtures('Article');
  3587. $TestModel =& new Article();
  3588. $data = array('Article' => array('user_id' => 2, 'id' => 4, 'title' => 'Fourth Article', 'published' => 'N'));
  3589. $result = $TestModel->set($data) && $TestModel->save();
  3590. $this->assertTrue($result);
  3591. $data = array('Article' => array('user_id' => 2, 'id' => 5, 'title' => 'Fifth Article', 'published' => 'Y'));
  3592. $result = $TestModel->set($data) && $TestModel->save();
  3593. $this->assertTrue($result);
  3594. $data = array('Article' => array('user_id' => 1, 'id' => 6, 'title' => 'Sixth Article', 'published' => 'N'));
  3595. $result = $TestModel->set($data) && $TestModel->save();
  3596. $this->assertTrue($result);
  3597. $TestModel->recursive = -1;
  3598. $result = $TestModel->find('all', array('fields' => array('id', 'user_id', 'title', 'published')));
  3599. $expected = array(
  3600. array('Article' => array('id' => 1, 'user_id' => 1, 'title' => 'First Article', 'published' => 'Y' )),
  3601. array('Article' => array('id' => 2, 'user_id' => 3, 'title' => 'Second Article', 'published' => 'Y' )),
  3602. array('Article' => array('id' => 3, 'user_id' => 1, 'title' => 'Third Article', 'published' => 'Y' )),
  3603. array('Article' => array('id' => 4, 'user_id' => 2, 'title' => 'Fourth Article', 'published' => 'N' )),
  3604. array('Article' => array('id' => 5, 'user_id' => 2, 'title' => 'Fifth Article', 'published' => 'Y' )),
  3605. array('Article' => array('id' => 6, 'user_id' => 1, 'title' => 'Sixth Article', 'published' => 'N' ))
  3606. );
  3607. $this->assertEqual($result, $expected);
  3608. $result = $TestModel->deleteAll(array('Article.published' => 'N'));
  3609. $this->assertTrue($result);
  3610. $TestModel->recursive = -1;
  3611. $result = $TestModel->find('all', array('fields' => array('id', 'user_id', 'title', 'published')));
  3612. $expected = array(
  3613. array('Article' => array('id' => 1, 'user_id' => 1, 'title' => 'First Article', 'published' => 'Y' )),
  3614. array('Article' => array('id' => 2, 'user_id' => 3, 'title' => 'Second Article', 'published' => 'Y' )),
  3615. array('Article' => array('id' => 3, 'user_id' => 1, 'title' => 'Third Article', 'published' => 'Y' )),
  3616. array('Article' => array('id' => 5, 'user_id' => 2, 'title' => 'Fifth Article', 'published' => 'Y' ))
  3617. );
  3618. $this->assertEqual($result, $expected);
  3619. $result = $TestModel->deleteAll(array('Article.user_id' => array(2, 3)), true, true);
  3620. $this->assertTrue($result);
  3621. $TestModel->recursive = -1;
  3622. $result = $TestModel->find('all', array('fields' => array('id', 'user_id', 'title', 'published')));
  3623. $expected = array(
  3624. array('Article' => array('id' => 1, 'user_id' => 1, 'title' => 'First Article', 'published' => 'Y' )),
  3625. array('Article' => array('id' => 3, 'user_id' => 1, 'title' => 'Third Article', 'published' => 'Y' ))
  3626. );
  3627. $this->assertEqual($result, $expected);
  3628. }
  3629. /**
  3630. * testRecursiveDel method
  3631. *
  3632. * @access public
  3633. * @return void
  3634. */
  3635. function testRecursiveDel() {
  3636. $this->loadFixtures('Article', 'Comment', 'Attachment');
  3637. $TestModel =& new Article();
  3638. $result = $TestModel->del(2);
  3639. $this->assertTrue($result);
  3640. $TestModel->recursive = 2;
  3641. $result = $TestModel->read(null, 2);
  3642. $this->assertFalse($result);
  3643. $result = $TestModel->Comment->read(null, 5);
  3644. $this->assertFalse($result);
  3645. $result = $TestModel->Comment->read(null, 6);
  3646. $this->assertFalse($result);
  3647. $result = $TestModel->Comment->Attachment->read(null, 1);
  3648. $this->assertFalse($result);
  3649. $result = $TestModel->find('count');
  3650. $this->assertEqual($result, 2);
  3651. $result = $TestModel->Comment->find('count');
  3652. $this->assertEqual($result, 4);
  3653. $result = $TestModel->Comment->Attachment->find('count');
  3654. $this->assertEqual($result, 0);
  3655. }
  3656. /**
  3657. * testDependentExclusiveDelete method
  3658. *
  3659. * @access public
  3660. * @return void
  3661. */
  3662. function testDependentExclusiveDelete() {
  3663. $this->loadFixtures('Article', 'Comment');
  3664. $TestModel =& new Article10();
  3665. $result = $TestModel->find('all');
  3666. $this->assertEqual(count($result[0]['Comment']), 4);
  3667. $this->assertEqual(count($result[1]['Comment']), 2);
  3668. $this->assertEqual($TestModel->Comment->find('count'), 6);
  3669. $TestModel->delete(1);
  3670. $this->assertEqual($TestModel->Comment->find('count'), 2);
  3671. }
  3672. /**
  3673. * testDeleteLinks method
  3674. *
  3675. * @access public
  3676. * @return void
  3677. */
  3678. function testDeleteLinks() {
  3679. $this->loadFixtures('Article', 'ArticlesTag', 'Tag');
  3680. $TestModel =& new Article();
  3681. $result = $TestModel->ArticlesTag->find('all');
  3682. $expected = array(
  3683. array('ArticlesTag' => array('article_id' => '1', 'tag_id' => '1')),
  3684. array('ArticlesTag' => array('article_id' => '1', 'tag_id' => '2')),
  3685. array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '1')),
  3686. array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '3'))
  3687. );
  3688. $this->assertEqual($result, $expected);
  3689. $TestModel->delete(1);
  3690. $result = $TestModel->ArticlesTag->find('all');
  3691. $expected = array(
  3692. array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '1')),
  3693. array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '3'))
  3694. );
  3695. $this->assertEqual($result, $expected);
  3696. }
  3697. /**
  3698. * testFindAllThreaded method
  3699. *
  3700. * @access public
  3701. * @return void
  3702. */
  3703. function testFindAllThreaded() {
  3704. $this->loadFixtures('Category');
  3705. $TestModel =& new Category();
  3706. $result = $TestModel->find('threaded');
  3707. $expected = array(
  3708. array(
  3709. 'Category' => array(
  3710. 'id' => '1',
  3711. 'parent_id' => '0',
  3712. 'name' => 'Category 1',
  3713. 'created' => '2007-03-18 15:30:23',
  3714. 'updated' => '2007-03-18 15:32:31'
  3715. ),
  3716. 'children' => array(
  3717. array(
  3718. 'Category' => array(
  3719. 'id' => '2',
  3720. 'parent_id' => '1',
  3721. 'name' => 'Category 1.1',
  3722. 'created' => '2007-03-18 15:30:23',
  3723. 'updated' => '2007-03-18 15:32:31'
  3724. ),
  3725. 'children' => array(
  3726. array('Category' => array(
  3727. 'id' => '7',
  3728. 'parent_id' => '2',
  3729. 'name' => 'Category 1.1.1',
  3730. 'created' => '2007-03-18 15:30:23',
  3731. 'updated' => '2007-03-18 15:32:31'),
  3732. 'children' => array()),
  3733. array('Category' => array(
  3734. 'id' => '8',
  3735. 'parent_id' => '2',
  3736. 'name' => 'Category 1.1.2',
  3737. 'created' => '2007-03-18 15:30:23',
  3738. 'updated' => '2007-03-18 15:32:31'),
  3739. 'children' => array()))
  3740. ),
  3741. array(
  3742. 'Category' => array(
  3743. 'id' => '3',
  3744. 'parent_id' => '1',
  3745. 'name' => 'Category 1.2',
  3746. 'created' => '2007-03-18 15:30:23',
  3747. 'updated' => '2007-03-18 15:32:31'
  3748. ),
  3749. 'children' => array()
  3750. )
  3751. )
  3752. ),
  3753. array(
  3754. 'Category' => array(
  3755. 'id' => '4',
  3756. 'parent_id' => '0',
  3757. 'name' => 'Category 2',
  3758. 'created' => '2007-03-18 15:30:23',
  3759. 'updated' => '2007-03-18 15:32:31'
  3760. ),
  3761. 'children' => array()
  3762. ),
  3763. array(
  3764. 'Category' => array(
  3765. 'id' => '5',
  3766. 'parent_id' => '0',
  3767. 'name' => 'Category 3',
  3768. 'created' => '2007-03-18 15:30:23',
  3769. 'updated' => '2007-03-18 15:32:31'
  3770. ),
  3771. 'children' => array(
  3772. array(
  3773. 'Category' => array(
  3774. 'id' => '6',
  3775. 'parent_id' => '5',
  3776. 'name' => 'Category 3.1',
  3777. 'created' => '2007-03-18 15:30:23',
  3778. 'updated' => '2007-03-18 15:32:31'
  3779. ),
  3780. 'children' => array()
  3781. )
  3782. )
  3783. )
  3784. );
  3785. $this->assertEqual($result, $expected);
  3786. $result = $TestModel->find('threaded', array('conditions' => array('Category.name LIKE' => 'Category 1%')));
  3787. $expected = array(
  3788. array(
  3789. 'Category' => array(
  3790. 'id' => '1',
  3791. 'parent_id' => '0',
  3792. 'name' => 'Category 1',
  3793. 'created' => '2007-03-18 15:30:23',
  3794. 'updated' => '2007-03-18 15:32:31'
  3795. ),
  3796. 'children' => array(
  3797. array(
  3798. 'Category' => array(
  3799. 'id' => '2',
  3800. 'parent_id' => '1',
  3801. 'name' => 'Category 1.1',
  3802. 'created' => '2007-03-18 15:30:23',
  3803. 'updated' => '2007-03-18 15:32:31'
  3804. ),
  3805. 'children' => array(
  3806. array('Category' => array(
  3807. 'id' => '7',
  3808. 'parent_id' => '2',
  3809. 'name' => 'Category 1.1.1',
  3810. 'created' => '2007-03-18 15:30:23',
  3811. 'updated' => '2007-03-18 15:32:31'),
  3812. 'children' => array()),
  3813. array('Category' => array(
  3814. 'id' => '8',
  3815. 'parent_id' => '2',
  3816. 'name' => 'Category 1.1.2',
  3817. 'created' => '2007-03-18 15:30:23',
  3818. 'updated' => '2007-03-18 15:32:31'),
  3819. 'children' => array()))
  3820. ),
  3821. array(
  3822. 'Category' => array(
  3823. 'id' => '3',
  3824. 'parent_id' => '1',
  3825. 'name' => 'Category 1.2',
  3826. 'created' => '2007-03-18 15:30:23',
  3827. 'updated' => '2007-03-18 15:32:31'
  3828. ),
  3829. 'children' => array()
  3830. )
  3831. )
  3832. )
  3833. );
  3834. $this->assertEqual($result, $expected);
  3835. $result = $TestModel->find('threaded', array('fields' => 'id, parent_id, name'));
  3836. $expected = array(
  3837. array(
  3838. 'Category' => array(
  3839. 'id' => '1',
  3840. 'parent_id' => '0',
  3841. 'name' => 'Category 1'
  3842. ),
  3843. 'children' => array(
  3844. array(
  3845. 'Category' => array(
  3846. 'id' => '2',
  3847. 'parent_id' => '1',
  3848. 'name' => 'Category 1.1'
  3849. ),
  3850. 'children' => array(
  3851. array('Category' => array(
  3852. 'id' => '7',
  3853. 'parent_id' => '2',
  3854. 'name' => 'Category 1.1.1'),
  3855. 'children' => array()),
  3856. array('Category' => array(
  3857. 'id' => '8',
  3858. 'parent_id' => '2',
  3859. 'name' => 'Category 1.1.2'),
  3860. 'children' => array()))
  3861. ),
  3862. array(
  3863. 'Category' => array(
  3864. 'id' => '3',
  3865. 'parent_id' => '1',
  3866. 'name' => 'Category 1.2'
  3867. ),
  3868. 'children' => array()
  3869. )
  3870. )
  3871. ),
  3872. array(
  3873. 'Category' => array(
  3874. 'id' => '4',
  3875. 'parent_id' => '0',
  3876. 'name' => 'Category 2'
  3877. ),
  3878. 'children' => array()
  3879. ),
  3880. array(
  3881. 'Category' => array(
  3882. 'id' => '5',
  3883. 'parent_id' => '0',
  3884. 'name' => 'Category 3'
  3885. ),
  3886. 'children' => array(
  3887. array(
  3888. 'Category' => array(
  3889. 'id' => '6',
  3890. 'parent_id' => '5',
  3891. 'name' => 'Category 3.1'
  3892. ),
  3893. 'children' => array()
  3894. )
  3895. )
  3896. )
  3897. );
  3898. $this->assertEqual($result, $expected);
  3899. $result = $TestModel->find('threaded', array('order' => 'id DESC'));
  3900. $expected = array(
  3901. array(
  3902. 'Category' => array(
  3903. 'id' => 5,
  3904. 'parent_id' => 0,
  3905. 'name' => 'Category 3',
  3906. 'created' => '2007-03-18 15:30:23',
  3907. 'updated' => '2007-03-18 15:32:31'
  3908. ),
  3909. 'children' => array(
  3910. array(
  3911. 'Category' => array(
  3912. 'id' => 6,
  3913. 'parent_id' => 5,
  3914. 'name' => 'Category 3.1',
  3915. 'created' => '2007-03-18 15:30:23',
  3916. 'updated' => '2007-03-18 15:32:31'
  3917. ),
  3918. 'children' => array()
  3919. )
  3920. )
  3921. ),
  3922. array(
  3923. 'Category' => array(
  3924. 'id' => 4,
  3925. 'parent_id' => 0,
  3926. 'name' => 'Category 2',
  3927. 'created' => '2007-03-18 15:30:23',
  3928. 'updated' => '2007-03-18 15:32:31'
  3929. ),
  3930. 'children' => array()
  3931. ),
  3932. array(
  3933. 'Category' => array(
  3934. 'id' => 1,
  3935. 'parent_id' => 0,
  3936. 'name' => 'Category 1',
  3937. 'created' => '2007-03-18 15:30:23',
  3938. 'updated' => '2007-03-18 15:32:31'
  3939. ),
  3940. 'children' => array(
  3941. array(
  3942. 'Category' => array(
  3943. 'id' => 3,
  3944. 'parent_id' => 1,
  3945. 'name' => 'Category 1.2',
  3946. 'created' => '2007-03-18 15:30:23',
  3947. 'updated' => '2007-03-18 15:32:31'
  3948. ),
  3949. 'children' => array()
  3950. ),
  3951. array(
  3952. 'Category' => array(
  3953. 'id' => 2,
  3954. 'parent_id' => 1,
  3955. 'name' => 'Category 1.1',
  3956. 'created' => '2007-03-18 15:30:23',
  3957. 'updated' => '2007-03-18 15:32:31'
  3958. ),
  3959. 'children' => array(
  3960. array('Category' => array(
  3961. 'id' => '8',
  3962. 'parent_id' => '2',
  3963. 'name' => 'Category 1.1.2',
  3964. 'created' => '2007-03-18 15:30:23',
  3965. 'updated' => '2007-03-18 15:32:31'),
  3966. 'children' => array()),
  3967. array('Category' => array(
  3968. 'id' => '7',
  3969. 'parent_id' => '2',
  3970. 'name' => 'Category 1.1.1',
  3971. 'created' => '2007-03-18 15:30:23',
  3972. 'updated' => '2007-03-18 15:32:31'),
  3973. 'children' => array()))
  3974. )
  3975. )
  3976. )
  3977. );
  3978. $this->assertEqual($result, $expected);
  3979. $result = $TestModel->find('threaded', array('conditions' => array('Category.name LIKE' => 'Category 3%')));
  3980. $expected = array(
  3981. array(
  3982. 'Category' => array(
  3983. 'id' => '5',
  3984. 'parent_id' => '0',
  3985. 'name' => 'Category 3',
  3986. 'created' => '2007-03-18 15:30:23',
  3987. 'updated' => '2007-03-18 15:32:31'
  3988. ),
  3989. 'children' => array(
  3990. array(
  3991. 'Category' => array(
  3992. 'id' => '6',
  3993. 'parent_id' => '5',
  3994. 'name' => 'Category 3.1',
  3995. 'created' => '2007-03-18 15:30:23',
  3996. 'updated' => '2007-03-18 15:32:31'
  3997. ),
  3998. 'children' => array()
  3999. )
  4000. )
  4001. )
  4002. );
  4003. $this->assertEqual($result, $expected);
  4004. $result = $TestModel->find('threaded', array('conditions' => array('Category.name LIKE' => 'Category 1.1%')));
  4005. $expected = array(
  4006. array('Category' =>
  4007. array(
  4008. 'id' => '2',
  4009. 'parent_id' => '1',
  4010. 'name' => 'Category 1.1',
  4011. 'created' => '2007-03-18 15:30:23',
  4012. 'updated' => '2007-03-18 15:32:31'),
  4013. 'children' => array(
  4014. array('Category' => array(
  4015. 'id' => '7',
  4016. 'parent_id' => '2',
  4017. 'name' => 'Category 1.1.1',
  4018. 'created' => '2007-03-18 15:30:23',
  4019. 'updated' => '2007-03-18 15:32:31'),
  4020. 'children' => array()),
  4021. array('Category' => array(
  4022. 'id' => '8',
  4023. 'parent_id' => '2',
  4024. 'name' => 'Category 1.1.2',
  4025. 'created' => '2007-03-18 15:30:23',
  4026. 'updated' => '2007-03-18 15:32:31'),
  4027. 'children' => array()))));
  4028. $this->assertEqual($result, $expected);
  4029. $result = $TestModel->find('threaded', array('fields' => 'id, parent_id, name', 'conditions' => array('Category.id !=' => 2)));
  4030. $expected = array(
  4031. array(
  4032. 'Category' => array(
  4033. 'id' => '1',
  4034. 'parent_id' => '0',
  4035. 'name' => 'Category 1'
  4036. ),
  4037. 'children' => array(
  4038. array(
  4039. 'Category' => array(
  4040. 'id' => '3',
  4041. 'parent_id' => '1',
  4042. 'name' => 'Category 1.2'
  4043. ),
  4044. 'children' => array()
  4045. )
  4046. )
  4047. ),
  4048. array(
  4049. 'Category' => array(
  4050. 'id' => '4',
  4051. 'parent_id' => '0',
  4052. 'name' => 'Category 2'
  4053. ),
  4054. 'children' => array()
  4055. ),
  4056. array(
  4057. 'Category' => array(
  4058. 'id' => '5',
  4059. 'parent_id' => '0',
  4060. 'name' => 'Category 3'
  4061. ),
  4062. 'children' => array(
  4063. array(
  4064. 'Category' => array(
  4065. 'id' => '6',
  4066. 'parent_id' => '5',
  4067. 'name' => 'Category 3.1'
  4068. ),
  4069. 'children' => array()
  4070. )
  4071. )
  4072. )
  4073. );
  4074. $this->assertEqual($result, $expected);
  4075. $result = $TestModel->find('all', array('fields' => 'id, name, parent_id', 'conditions' => array('Category.id !=' => 1)));
  4076. $expected = array (
  4077. array ('Category' => array('id' => '2', 'name' => 'Category 1.1', 'parent_id' => '1' )),
  4078. array ('Category' => array('id' => '3', 'name' => 'Category 1.2', 'parent_id' => '1' )),
  4079. array ('Category' => array('id' => '4', 'name' => 'Category 2', 'parent_id' => '0' )),
  4080. array ('Category' => array('id' => '5', 'name' => 'Category 3', 'parent_id' => '0' )),
  4081. array ('Category' => array('id' => '6', 'name' => 'Category 3.1', 'parent_id' => '5' )),
  4082. array ('Category' => array('id' => '7', 'name' => 'Category 1.1.1', 'parent_id' => '2' )),
  4083. array ('Category' => array('id' => '8', 'name' => 'Category 1.1.2', 'parent_id' => '2' )),
  4084. );
  4085. $this->assertEqual($result, $expected);
  4086. $result = $TestModel->find('threaded', array('fields' => 'id, parent_id, name', 'conditions' => array('Category.id !=' => 1)));
  4087. $expected = array(
  4088. array(
  4089. 'Category' => array(
  4090. 'id' => '2',
  4091. 'parent_id' => '1',
  4092. 'name' => 'Category 1.1'
  4093. ),
  4094. 'children' => array(
  4095. array('Category' => array(
  4096. 'id' => '7',
  4097. 'parent_id' => '2',
  4098. 'name' => 'Category 1.1.1'),
  4099. 'children' => array()),
  4100. array('Category' => array(
  4101. 'id' => '8',
  4102. 'parent_id' => '2',
  4103. 'name' => 'Category 1.1.2'),
  4104. 'children' => array()))
  4105. ),
  4106. array(
  4107. 'Category' => array(
  4108. 'id' => '3',
  4109. 'parent_id' => '1',
  4110. 'name' => 'Category 1.2'
  4111. ),
  4112. 'children' => array()
  4113. )
  4114. );
  4115. $this->assertEqual($result, $expected);
  4116. }
  4117. /**
  4118. * test find('neighbors')
  4119. *
  4120. * @return void
  4121. * @access public
  4122. */
  4123. function testFindNeighbors() {
  4124. $this->loadFixtures('User', 'Article');
  4125. $TestModel =& new Article();
  4126. $TestModel->id = 1;
  4127. $result = $TestModel->find('neighbors', array('fields' => array('id')));
  4128. $expected = array('prev' => null, 'next' => array('Article' => array('id' => 2)));
  4129. $this->assertEqual($result, $expected);
  4130. $TestModel->id = 2;
  4131. $result = $TestModel->find('neighbors', array('fields' => array('id')));
  4132. $expected = array('prev' => array('Article' => array('id' => 1)), 'next' => array('Article' => array('id' => 3)));
  4133. $this->assertEqual($result, $expected);
  4134. $TestModel->id = 3;
  4135. $result = $TestModel->find('neighbors', array('fields' => array('id')));
  4136. $expected = array('prev' => array('Article' => array('id' => 2)), 'next' => null);
  4137. $this->assertEqual($result, $expected);
  4138. $TestModel->id = 1;
  4139. $result = $TestModel->find('neighbors', array('recursive' => -1));
  4140. $expected = array(
  4141. 'prev' => null,
  4142. 'next' => array(
  4143. 'Article' => array(
  4144. 'id' => 2,
  4145. 'user_id' => 3,
  4146. 'title' => 'Second Article',
  4147. 'body' => 'Second Article Body',
  4148. 'published' => 'Y',
  4149. 'created' => '2007-03-18 10:41:23',
  4150. 'updated' => '2007-03-18 10:43:31'
  4151. )
  4152. )
  4153. );
  4154. $this->assertEqual($result, $expected);
  4155. $TestModel->id = 2;
  4156. $result = $TestModel->find('neighbors', array('recursive' => -1));
  4157. $expected = array(
  4158. 'prev' => array(
  4159. 'Article' => array(
  4160. 'id' => 1,
  4161. 'user_id' => 1,
  4162. 'title' => 'First Article',
  4163. 'body' => 'First Article Body',
  4164. 'published' => 'Y',
  4165. 'created' => '2007-03-18 10:39:23',
  4166. 'updated' => '2007-03-18 10:41:31'
  4167. )
  4168. ),
  4169. 'next' => array(
  4170. 'Article' => array(
  4171. 'id' => 3,
  4172. 'user_id' => 1,
  4173. 'title' => 'Third Article',
  4174. 'body' => 'Third Article Body',
  4175. 'published' => 'Y',
  4176. 'created' => '2007-03-18 10:43:23',
  4177. 'updated' => '2007-03-18 10:45:31'
  4178. )
  4179. )
  4180. );
  4181. $this->assertEqual($result, $expected);
  4182. $TestModel->id = 3;
  4183. $result = $TestModel->find('neighbors', array('recursive' => -1));
  4184. $expected = array(
  4185. 'prev' => array(
  4186. 'Article' => array(
  4187. 'id' => 2,
  4188. 'user_id' => 3,
  4189. 'title' => 'Second Article',
  4190. 'body' => 'Second Article Body',
  4191. 'published' => 'Y',
  4192. 'created' => '2007-03-18 10:41:23',
  4193. 'updated' => '2007-03-18 10:43:31'
  4194. )
  4195. ),
  4196. 'next' => null
  4197. );
  4198. $this->assertEqual($result, $expected);
  4199. $TestModel->recursive = 0;
  4200. $TestModel->id = 1;
  4201. $one = $TestModel->read();
  4202. $TestModel->id = 2;
  4203. $two = $TestModel->read();
  4204. $TestModel->id = 3;
  4205. $three = $TestModel->read();
  4206. $TestModel->id = 1;
  4207. $result = $TestModel->find('neighbors');
  4208. $expected = array('prev' => null, 'next' => $two);
  4209. $this->assertEqual($result, $expected);
  4210. $TestModel->id = 2;
  4211. $result = $TestModel->find('neighbors');
  4212. $expected = array('prev' => $one, 'next' => $three);
  4213. $this->assertEqual($result, $expected);
  4214. $TestModel->id = 3;
  4215. $result = $TestModel->find('neighbors');
  4216. $expected = array('prev' => $two, 'next' => null);
  4217. $this->assertEqual($result, $expected);
  4218. $TestModel->recursive = 2;
  4219. $TestModel->id = 1;
  4220. $one = $TestModel->read();
  4221. $TestModel->id = 2;
  4222. $two = $TestModel->read();
  4223. $TestModel->id = 3;
  4224. $three = $TestModel->read();
  4225. $TestModel->id = 1;
  4226. $result = $TestModel->find('neighbors', array('recursive' => 2));
  4227. $expected = array('prev' => null, 'next' => $two);
  4228. $this->assertEqual($result, $expected);
  4229. $TestModel->id = 2;
  4230. $result = $TestModel->find('neighbors', array('recursive' => 2));
  4231. $expected = array('prev' => $one, 'next' => $three);
  4232. $this->assertEqual($result, $expected);
  4233. $TestModel->id = 3;
  4234. $result = $TestModel->find('neighbors', array('recursive' => 2));
  4235. $expected = array('prev' => $two, 'next' => null);
  4236. $this->assertEqual($result, $expected);
  4237. }
  4238. /**
  4239. * test findNeighbours() method
  4240. *
  4241. * @return void
  4242. * @access public
  4243. */
  4244. function testFindNeighboursLegacy() {
  4245. $this->loadFixtures('User', 'Article');
  4246. $TestModel =& new Article();
  4247. $result = $TestModel->findNeighbours(null, 'Article.id', '2');
  4248. $expected = array('prev' => array('Article' => array('id' => 1)), 'next' => array('Article' => array('id' => 3)));
  4249. $this->assertEqual($result, $expected);
  4250. $result = $TestModel->findNeighbours(null, 'Article.id', '3');
  4251. $expected = array('prev' => array('Article' => array('id' => 2)), 'next' => array());
  4252. $this->assertEqual($result, $expected);
  4253. $result = $TestModel->findNeighbours(array('User.id' => 1), array('Article.id', 'Article.title'), 2);
  4254. $expected = array(
  4255. 'prev' => array('Article' => array('id' => 1, 'title' => 'First Article')),
  4256. 'next' => array('Article' => array('id' => 3, 'title' => 'Third Article')),
  4257. );
  4258. $this->assertEqual($result, $expected);
  4259. }
  4260. /**
  4261. * testFindCombinedRelations method
  4262. *
  4263. * @access public
  4264. * @return void
  4265. */
  4266. function testFindCombinedRelations() {
  4267. $this->loadFixtures('Apple', 'Sample');
  4268. $TestModel =& new Apple();
  4269. $result = $TestModel->find('all');
  4270. $expected = array(
  4271. array('Apple' => array(
  4272. 'id' => '1', 'apple_id' => '2', 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4273. 'Parent' => array('id' => '2', 'apple_id' => '1', 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
  4274. 'Sample' => array('id' => null, 'apple_id' => null, 'name' => null),
  4275. 'Child' => array(array('id' => '2', 'apple_id' => '1', 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'))),
  4276. array('Apple' => array(
  4277. 'id' => '2', 'apple_id' => '1', 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
  4278. 'Parent' => array('id' => '1', 'apple_id' => '2', 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4279. 'Sample' => array('id' => '2', 'apple_id' => '2', 'name' => 'sample2' ),
  4280. 'Child' => array(array('id' => '1', 'apple_id' => '2', 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4281. array('id' => '3', 'apple_id' => '2', 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4282. array('id' => '4', 'apple_id' => '2', 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'))),
  4283. array('Apple' => array(
  4284. 'id' => '3', 'apple_id' => '2', 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4285. 'Parent' => array('id' => '2', 'apple_id' => '1', 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
  4286. 'Sample' => array('id' => '1', 'apple_id' => '3', 'name' => 'sample1'),
  4287. 'Child' => array()),
  4288. array('Apple' => array(
  4289. 'id' => '4', 'apple_id' => '2', 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'),
  4290. 'Parent' => array('id' => '2', 'apple_id' => '1', 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
  4291. 'Sample' => array('id' => '3', 'apple_id' => '4', 'name' => 'sample3'),
  4292. 'Child' => array(array('id' => '6', 'apple_id' => '4', 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17'))),
  4293. array('Apple' => array(
  4294. 'id' => '5', 'apple_id' => '5', 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'),
  4295. 'Parent' => array('id' => '5', 'apple_id' => '5', 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'),
  4296. 'Sample' => array('id' => '4', 'apple_id' => '5', 'name' => 'sample4'),
  4297. 'Child' => array(array('id' => '5', 'apple_id' => '5', 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'))),
  4298. array('Apple' => array(
  4299. 'id' => '6', 'apple_id' => '4', 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17'),
  4300. 'Parent' => array('id' => '4', 'apple_id' => '2', 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'),
  4301. 'Sample' => array('id' => null, 'apple_id' => null, 'name' => null),
  4302. 'Child' => array(array('id' => '7', 'apple_id' => '6', 'color' => 'Some wierd color', 'name' => 'Some odd color', 'created' => '2006-12-25 05:34:21', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:34:21', 'mytime' => '22:57:17'))),
  4303. array('Apple' => array(
  4304. 'id' => '7', 'apple_id' => '6', 'color' => 'Some wierd color', 'name' => 'Some odd color', 'created' => '2006-12-25 05:34:21', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:34:21', 'mytime' => '22:57:17'),
  4305. 'Parent' => array('id' => '6', 'apple_id' => '4', 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17'),
  4306. 'Sample' => array('id' => null, 'apple_id' => null, 'name' => null),
  4307. 'Child' => array()));
  4308. $this->assertEqual($result, $expected);
  4309. }
  4310. /**
  4311. * testSaveEmpty method
  4312. *
  4313. * @access public
  4314. * @return void
  4315. */
  4316. function testSaveEmpty() {
  4317. $this->loadFixtures('Thread');
  4318. $TestModel =& new Thread();
  4319. $data = array();
  4320. $expected = $TestModel->save($data);
  4321. $this->assertFalse($expected);
  4322. }
  4323. // function testBasicValidation() {
  4324. // $TestModel =& new ValidationTest();
  4325. // $TestModel->testing = true;
  4326. // $TestModel->set(array('title' => '', 'published' => 1));
  4327. // $this->assertEqual($TestModel->invalidFields(), array('title' => 'This field cannot be left blank'));
  4328. //
  4329. // $TestModel->create();
  4330. // $TestModel->set(array('title' => 'Hello', 'published' => 0));
  4331. // $this->assertEqual($TestModel->invalidFields(), array('published' => 'This field cannot be left blank'));
  4332. //
  4333. // $TestModel->create();
  4334. // $TestModel->set(array('title' => 'Hello', 'published' => 1, 'body' => ''));
  4335. // $this->assertEqual($TestModel->invalidFields(), array('body' => 'This field cannot be left blank'));
  4336. // }
  4337. /**
  4338. * testFindAllWithConditionInChildQuery
  4339. *
  4340. * @todo external conditions like this are going to need to be revisited at some point
  4341. * @access public
  4342. * @return void
  4343. */
  4344. function testFindAllWithConditionInChildQuery() {
  4345. $this->loadFixtures('Basket', 'FilmFile');
  4346. $TestModel =& new Basket();
  4347. $recursive = 3;
  4348. $result = $TestModel->find('all', compact('conditions', 'recursive'));
  4349. $expected = array(
  4350. array(
  4351. 'Basket' => array(
  4352. 'id' => 1,
  4353. 'type' => 'nonfile',
  4354. 'name' => 'basket1',
  4355. 'object_id' => 1,
  4356. 'user_id' => 1,
  4357. ),
  4358. 'FilmFile' => array(
  4359. 'id' => '',
  4360. 'name' => '',
  4361. )
  4362. ),
  4363. array(
  4364. 'Basket' => array(
  4365. 'id' => 2,
  4366. 'type' => 'file',
  4367. 'name' => 'basket2',
  4368. 'object_id' => 2,
  4369. 'user_id' => 1,
  4370. ),
  4371. 'FilmFile' => array(
  4372. 'id' => 2,
  4373. 'name' => 'two',
  4374. )
  4375. ),
  4376. );
  4377. $this->assertEqual($result, $expected);
  4378. }
  4379. /**
  4380. * testFindAllWithConditionsHavingMixedDataTypes method
  4381. *
  4382. * @access public
  4383. * @return void
  4384. */
  4385. function testFindAllWithConditionsHavingMixedDataTypes() {
  4386. $this->loadFixtures('Article');
  4387. $TestModel =& new Article();
  4388. $expected = array(
  4389. array(
  4390. 'Article' => array(
  4391. 'id' => 1,
  4392. 'user_id' => 1,
  4393. 'title' => 'First Article',
  4394. 'body' => 'First Article Body',
  4395. 'published' => 'Y',
  4396. 'created' => '2007-03-18 10:39:23',
  4397. 'updated' => '2007-03-18 10:41:31'
  4398. )
  4399. ),
  4400. array(
  4401. 'Article' => array(
  4402. 'id' => 2,
  4403. 'user_id' => 3,
  4404. 'title' => 'Second Article',
  4405. 'body' => 'Second Article Body',
  4406. 'published' => 'Y',
  4407. 'created' => '2007-03-18 10:41:23',
  4408. 'updated' => '2007-03-18 10:43:31'
  4409. )
  4410. )
  4411. );
  4412. $conditions = array('id' => array('1', 2));
  4413. $recursive = -1;
  4414. $result = $TestModel->find('all', compact('conditions', 'recursive'));
  4415. $this->assertEqual($result, $expected);
  4416. $conditions = array('id' => array('1', 2, '3.0'));
  4417. $result = $TestModel->find('all', compact('recursive', 'conditions'));
  4418. $expected = array(
  4419. array(
  4420. 'Article' => array(
  4421. 'id' => 1,
  4422. 'user_id' => 1,
  4423. 'title' => 'First Article',
  4424. 'body' => 'First Article Body',
  4425. 'published' => 'Y',
  4426. 'created' => '2007-03-18 10:39:23',
  4427. 'updated' => '2007-03-18 10:41:31'
  4428. )
  4429. ),
  4430. array(
  4431. 'Article' => array(
  4432. 'id' => 2,
  4433. 'user_id' => 3,
  4434. 'title' => 'Second Article',
  4435. 'body' => 'Second Article Body',
  4436. 'published' => 'Y',
  4437. 'created' => '2007-03-18 10:41:23',
  4438. 'updated' => '2007-03-18 10:43:31'
  4439. )
  4440. ),
  4441. array(
  4442. 'Article' => array(
  4443. 'id' => 3,
  4444. 'user_id' => 1,
  4445. 'title' => 'Third Article',
  4446. 'body' => 'Third Article Body',
  4447. 'published' => 'Y',
  4448. 'created' => '2007-03-18 10:43:23',
  4449. 'updated' => '2007-03-18 10:45:31'
  4450. )
  4451. )
  4452. );
  4453. $this->assertEqual($result, $expected);
  4454. }
  4455. /**
  4456. * testMultipleValidation method
  4457. *
  4458. * @access public
  4459. * @return void
  4460. */
  4461. function testMultipleValidation() {
  4462. $TestModel =& new ValidationTest();
  4463. }
  4464. /**
  4465. * Tests validation parameter order in custom validation methods
  4466. *
  4467. * @access public
  4468. * @return void
  4469. */
  4470. function testValidationParams() {
  4471. $TestModel =& new ValidationTest();
  4472. $TestModel->validate['title'] = array('rule' => 'customValidatorWithParams', 'required' => true);
  4473. $TestModel->create(array('title' => 'foo'));
  4474. $TestModel->invalidFields();
  4475. $expected = array(
  4476. 'data' => array('title' => 'foo'),
  4477. 'validator' => array(
  4478. 'rule' => 'customValidatorWithParams', 'on' => null,
  4479. 'last' => false, 'allowEmpty' => false, 'required' => true
  4480. ),
  4481. 'or' => true,
  4482. 'ignore_on_same' => 'id'
  4483. );
  4484. $this->assertEqual($TestModel->validatorParams, $expected);
  4485. $TestModel->validate['title'] = array('rule' => 'customValidatorWithMessage', 'required' => true);
  4486. $expected = array('title' => 'This field will *never* validate! Muhahaha!');
  4487. $this->assertEqual($TestModel->invalidFields(), $expected);
  4488. }
  4489. /**
  4490. * Tests validation parameter fieldList in invalidFields
  4491. *
  4492. * @access public
  4493. * @return void
  4494. */
  4495. function testInvalidFieldsWithFieldListParams() {
  4496. $TestModel =& new ValidationTest();
  4497. $TestModel->validate = $validate = array(
  4498. 'title' => array('rule' => 'customValidator', 'required' => true),
  4499. 'name' => array('rule' => 'allowEmpty', 'required' => true),
  4500. );
  4501. $TestModel->invalidFields(array('fieldList' => array('title')));
  4502. $expected = array('title' => 'This field cannot be left blank');
  4503. $this->assertEqual($TestModel->validationErrors, $expected);
  4504. $TestModel->validationErrors = array();
  4505. $TestModel->invalidFields(array('fieldList' => array('name')));
  4506. $expected = array('name' => 'This field cannot be left blank');
  4507. $this->assertEqual($TestModel->validationErrors, $expected);
  4508. $TestModel->validationErrors = array();
  4509. $TestModel->invalidFields(array('fieldList' => array('name', 'title')));
  4510. $expected = array('name' => 'This field cannot be left blank', 'title' => 'This field cannot be left blank');
  4511. $this->assertEqual($TestModel->validationErrors, $expected);
  4512. $TestModel->validationErrors = array();
  4513. $TestModel->whitelist = array('name');
  4514. $TestModel->invalidFields();
  4515. $expected = array('name' => 'This field cannot be left blank');
  4516. $this->assertEqual($TestModel->validationErrors, $expected);
  4517. $TestModel->validationErrors = array();
  4518. $this->assertEqual($TestModel->validate, $validate);
  4519. }
  4520. /**
  4521. * Tests validation parameter order in custom validation methods
  4522. *
  4523. * @access public
  4524. * @return void
  4525. */
  4526. function testAllowSimulatedFields() {
  4527. $TestModel =& new ValidationTest();
  4528. $TestModel->create(array('title' => 'foo', 'bar' => 'baz'));
  4529. $expected = array('ValidationTest' => array('title' => 'foo', 'bar' => 'baz'));
  4530. $this->assertEqual($TestModel->data, $expected);
  4531. }
  4532. /**
  4533. * Tests validation parameter order in custom validation methods
  4534. *
  4535. * @access public
  4536. * @return void
  4537. */
  4538. function testInvalidAssociation() {
  4539. $TestModel =& new ValidationTest();
  4540. $this->assertNull($TestModel->getAssociated('Foo'));
  4541. }
  4542. /**
  4543. * testLoadModelSecondIteration method
  4544. *
  4545. * @access public
  4546. * @return void
  4547. */
  4548. function testLoadModelSecondIteration() {
  4549. $model = new ModelA();
  4550. $this->assertIsA($model,'ModelA');
  4551. $this->assertIsA($model->ModelB, 'ModelB');
  4552. $this->assertIsA($model->ModelB->ModelD, 'ModelD');
  4553. $this->assertIsA($model->ModelC, 'ModelC');
  4554. $this->assertIsA($model->ModelC->ModelD, 'ModelD');
  4555. }
  4556. /**
  4557. * testRecursiveUnbind method
  4558. *
  4559. * @access public
  4560. * @return void
  4561. */
  4562. function testRecursiveUnbind() {
  4563. $this->loadFixtures('Apple', 'Sample');
  4564. $TestModel =& new Apple();
  4565. $TestModel->recursive = 2;
  4566. $result = $TestModel->find('all');
  4567. $expected = array(
  4568. array('Apple' => array (
  4569. 'id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4570. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17',
  4571. 'Parent' => array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4572. 'Sample' => array('id' => 2, 'apple_id' => 2, 'name' => 'sample2'),
  4573. 'Child' => array(
  4574. array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4575. array('id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4576. array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'))),
  4577. 'Sample' => array('id' =>'', 'apple_id' => '', 'name' => ''),
  4578. 'Child' => array(
  4579. array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17',
  4580. 'Parent' => array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4581. 'Sample' => array('id' => 2, 'apple_id' => 2, 'name' => 'sample2'),
  4582. 'Child' => array(
  4583. array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4584. array('id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4585. array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'))))),
  4586. array('Apple' => array(
  4587. 'id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
  4588. 'Parent' => array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17',
  4589. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
  4590. 'Sample' => array(),
  4591. 'Child' => array(
  4592. array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'))),
  4593. 'Sample' => array('id' => 2, 'apple_id' => 2, 'name' => 'sample2',
  4594. 'Apple' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17')),
  4595. 'Child' => array(
  4596. array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17',
  4597. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
  4598. 'Sample' => array(),
  4599. 'Child' => array(
  4600. array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'))),
  4601. array('id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17',
  4602. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
  4603. 'Sample' => array('id' => 1, 'apple_id' => 3, 'name' => 'sample1')),
  4604. array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17',
  4605. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
  4606. 'Sample' => array('id' => 3, 'apple_id' => 4, 'name' => 'sample3'),
  4607. 'Child' => array(
  4608. array('id' => 6, 'apple_id' => 4, 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17'))))),
  4609. array('Apple' => array(
  4610. 'id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4611. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17',
  4612. 'Parent' => array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4613. 'Sample' => array('id' => 2, 'apple_id' => 2, 'name' => 'sample2'),
  4614. 'Child' => array(
  4615. array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4616. array('id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4617. array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'))),
  4618. 'Sample' => array('id' => 1, 'apple_id' => 3, 'name' => 'sample1',
  4619. 'Apple' => array('id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17')),
  4620. 'Child' => array()),
  4621. array('Apple' => array(
  4622. 'id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'),
  4623. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17',
  4624. 'Parent' => array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4625. 'Sample' => array('id' => 2, 'apple_id' => 2, 'name' => 'sample2'),
  4626. 'Child' => array(
  4627. array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4628. array('id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4629. array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'))),
  4630. 'Sample' => array('id' => 3, 'apple_id' => 4, 'name' => 'sample3',
  4631. 'Apple' => array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17')),
  4632. 'Child' => array(
  4633. array('id' => 6, 'apple_id' => 4, 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17',
  4634. 'Parent' => array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'),
  4635. 'Sample' => array(),
  4636. 'Child' => array(
  4637. array('id' => 7, 'apple_id' => 6, 'color' => 'Some wierd color', 'name' => 'Some odd color', 'created' => '2006-12-25 05:34:21', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:34:21', 'mytime' => '22:57:17'))))),
  4638. array('Apple' => array(
  4639. 'id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'),
  4640. 'Parent' => array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17',
  4641. 'Parent' => array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'),
  4642. 'Sample' => array('id' => 4, 'apple_id' => 5, 'name' => 'sample4'),
  4643. 'Child' => array(
  4644. array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'))),
  4645. 'Sample' => array('id' => 4, 'apple_id' => 5, 'name' => 'sample4',
  4646. 'Apple' => array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17')),
  4647. 'Child' => array(
  4648. array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17',
  4649. 'Parent' => array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'),
  4650. 'Sample' => array('id' => 4, 'apple_id' => 5, 'name' => 'sample4'),
  4651. 'Child' => array(
  4652. array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'))))),
  4653. array('Apple' => array(
  4654. 'id' => 6, 'apple_id' => 4, 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17'),
  4655. 'Parent' => array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17',
  4656. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
  4657. 'Sample' => array('id' => 3, 'apple_id' => 4, 'name' => 'sample3'),
  4658. 'Child' => array(
  4659. array('id' => 6, 'apple_id' => 4, 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17'))),
  4660. 'Sample' => array('id' => '', 'apple_id' => '', 'name' => ''),
  4661. 'Child' => array(
  4662. array('id' => 7, 'apple_id' => 6, 'color' => 'Some wierd color', 'name' => 'Some odd color', 'created' => '2006-12-25 05:34:21', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:34:21', 'mytime' => '22:57:17',
  4663. 'Parent' => array('id' => 6, 'apple_id' => 4, 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17'),
  4664. 'Sample' => array()))),
  4665. array('Apple' => array(
  4666. 'id' => 7, 'apple_id' => 6, 'color' => 'Some wierd color', 'name' => 'Some odd color', 'created' => '2006-12-25 05:34:21', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:34:21', 'mytime' => '22:57:17'),
  4667. 'Parent' => array('id' => 6, 'apple_id' => 4, 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17',
  4668. 'Parent' => array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'),
  4669. 'Sample' => array(),
  4670. 'Child' => array(
  4671. array('id' => 7, 'apple_id' => 6, 'color' => 'Some wierd color', 'name' => 'Some odd color', 'created' => '2006-12-25 05:34:21', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:34:21', 'mytime' => '22:57:17'))),
  4672. 'Sample' => array('id' => '', 'apple_id' => '', 'name' => ''),
  4673. 'Child' => array()));
  4674. $this->assertEqual($result, $expected);
  4675. $result = $TestModel->Parent->unbindModel(array('hasOne' => array('Sample')));
  4676. $this->assertTrue($result);
  4677. $result = $TestModel->find('all');
  4678. $expected = array(
  4679. array('Apple' => array(
  4680. 'id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4681. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17',
  4682. 'Parent' => array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4683. 'Child' => array(
  4684. array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4685. array('id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4686. array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'))),
  4687. 'Sample' => array('id' =>'', 'apple_id' => '', 'name' => ''),
  4688. 'Child' => array(
  4689. array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17',
  4690. 'Parent' => array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4691. 'Sample' => array('id' => 2, 'apple_id' => 2, 'name' => 'sample2'),
  4692. 'Child' => array(
  4693. array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4694. array('id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4695. array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'))))),
  4696. array('Apple' => array(
  4697. 'id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
  4698. 'Parent' => array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17',
  4699. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
  4700. 'Child' => array(
  4701. array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'))),
  4702. 'Sample' => array('id' => 2, 'apple_id' => 2, 'name' => 'sample2',
  4703. 'Apple' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17')),
  4704. 'Child' => array(
  4705. array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17',
  4706. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
  4707. 'Sample' => array(),
  4708. 'Child' => array(
  4709. array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'))),
  4710. array('id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17',
  4711. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
  4712. 'Sample' => array('id' => 1, 'apple_id' => 3, 'name' => 'sample1')),
  4713. array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17',
  4714. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
  4715. 'Sample' => array('id' => 3, 'apple_id' => 4, 'name' => 'sample3'),
  4716. 'Child' => array(
  4717. array('id' => 6, 'apple_id' => 4, 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17'))))),
  4718. array('Apple' => array(
  4719. 'id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4720. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17',
  4721. 'Parent' => array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4722. 'Child' => array(
  4723. array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4724. array('id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4725. array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'))),
  4726. 'Sample' => array('id' => 1, 'apple_id' => 3, 'name' => 'sample1',
  4727. 'Apple' => array('id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17')),
  4728. 'Child' => array()),
  4729. array('Apple' => array(
  4730. 'id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'),
  4731. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17',
  4732. 'Parent' => array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4733. 'Child' => array(
  4734. array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4735. array('id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4736. array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'))),
  4737. 'Sample' => array('id' => 3, 'apple_id' => 4, 'name' => 'sample3',
  4738. 'Apple' => array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17')),
  4739. 'Child' => array(
  4740. array('id' => 6, 'apple_id' => 4, 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17',
  4741. 'Parent' => array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'),
  4742. 'Sample' => array(),
  4743. 'Child' => array(
  4744. array('id' => 7, 'apple_id' => 6, 'color' => 'Some wierd color', 'name' => 'Some odd color', 'created' => '2006-12-25 05:34:21', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:34:21', 'mytime' => '22:57:17'))))),
  4745. array('Apple' => array(
  4746. 'id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'),
  4747. 'Parent' => array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17',
  4748. 'Parent' => array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'),
  4749. 'Child' => array(
  4750. array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'))),
  4751. 'Sample' => array('id' => 4, 'apple_id' => 5, 'name' => 'sample4',
  4752. 'Apple' => array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17')),
  4753. 'Child' => array(
  4754. array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17',
  4755. 'Parent' => array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'),
  4756. 'Sample' => array('id' => 4, 'apple_id' => 5, 'name' => 'sample4'),
  4757. 'Child' => array(
  4758. array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'))))),
  4759. array('Apple' => array(
  4760. 'id' => 6, 'apple_id' => 4, 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17'),
  4761. 'Parent' => array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17',
  4762. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
  4763. 'Child' => array(
  4764. array('id' => 6, 'apple_id' => 4, 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17'))),
  4765. 'Sample' => array('id' => '', 'apple_id' => '', 'name' => ''),
  4766. 'Child' => array(
  4767. array('id' => 7, 'apple_id' => 6, 'color' => 'Some wierd color', 'name' => 'Some odd color', 'created' => '2006-12-25 05:34:21', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:34:21', 'mytime' => '22:57:17',
  4768. 'Parent' => array('id' => 6, 'apple_id' => 4, 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17'),
  4769. 'Sample' => array()))),
  4770. array('Apple' => array(
  4771. 'id' => 7, 'apple_id' => 6, 'color' => 'Some wierd color', 'name' => 'Some odd color', 'created' => '2006-12-25 05:34:21', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:34:21', 'mytime' => '22:57:17'),
  4772. 'Parent' => array('id' => 6, 'apple_id' => 4, 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17',
  4773. 'Parent' => array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'),
  4774. 'Child' => array(
  4775. array('id' => 7, 'apple_id' => 6, 'color' => 'Some wierd color', 'name' => 'Some odd color', 'created' => '2006-12-25 05:34:21', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:34:21', 'mytime' => '22:57:17'))),
  4776. 'Sample' => array('id' => '', 'apple_id' => '', 'name' => ''),
  4777. 'Child' => array()));
  4778. $this->assertEqual($result, $expected);
  4779. $result = $TestModel->Parent->unbindModel(array('hasOne' => array('Sample')));
  4780. $this->assertTrue($result);
  4781. $result = $TestModel->unbindModel(array('hasMany' => array('Child')));
  4782. $this->assertTrue($result);
  4783. $result = $TestModel->find('all');
  4784. $expected = array(array('Apple' => array (
  4785. 'id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4786. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17',
  4787. 'Parent' => array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4788. 'Child' => array(
  4789. array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4790. array('id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4791. array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'))),
  4792. 'Sample' => array('id' =>'', 'apple_id' => '', 'name' => '')),
  4793. array('Apple' => array(
  4794. 'id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
  4795. 'Parent' => array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17',
  4796. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
  4797. 'Child' => array(
  4798. array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'))),
  4799. 'Sample' => array('id' => 2, 'apple_id' => 2, 'name' => 'sample2',
  4800. 'Apple' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'))),
  4801. array('Apple' => array(
  4802. 'id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4803. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17',
  4804. 'Parent' => array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4805. 'Child' => array(
  4806. array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4807. array('id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4808. array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'))),
  4809. 'Sample' => array('id' => 1, 'apple_id' => 3, 'name' => 'sample1',
  4810. 'Apple' => array('id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'))),
  4811. array('Apple' => array(
  4812. 'id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'),
  4813. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17',
  4814. 'Parent' => array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4815. 'Child' => array(
  4816. array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4817. array('id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4818. array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'))),
  4819. 'Sample' => array('id' => 3, 'apple_id' => 4, 'name' => 'sample3',
  4820. 'Apple' => array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'))),
  4821. array('Apple' => array(
  4822. 'id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'),
  4823. 'Parent' => array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17',
  4824. 'Parent' => array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'),
  4825. 'Child' => array(
  4826. array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'))),
  4827. 'Sample' => array('id' => 4, 'apple_id' => 5, 'name' => 'sample4',
  4828. 'Apple' => array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'))),
  4829. array('Apple' => array(
  4830. 'id' => 6, 'apple_id' => 4, 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17'),
  4831. 'Parent' => array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17',
  4832. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
  4833. 'Child' => array(
  4834. array('id' => 6, 'apple_id' => 4, 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17'))),
  4835. 'Sample' => array('id' => '', 'apple_id' => '', 'name' => '')),
  4836. array('Apple' => array(
  4837. 'id' => 7, 'apple_id' => 6, 'color' => 'Some wierd color', 'name' => 'Some odd color', 'created' => '2006-12-25 05:34:21', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:34:21', 'mytime' => '22:57:17'),
  4838. 'Parent' => array('id' => 6, 'apple_id' => 4, 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17',
  4839. 'Parent' => array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'),
  4840. 'Child' => array(
  4841. array('id' => 7, 'apple_id' => 6, 'color' => 'Some wierd color', 'name' => 'Some odd color', 'created' => '2006-12-25 05:34:21', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:34:21', 'mytime' => '22:57:17'))),
  4842. 'Sample' => array('id' => '', 'apple_id' => '', 'name' => '')));
  4843. $this->assertEqual($result, $expected);
  4844. $result = $TestModel->unbindModel(array('hasMany' => array('Child')));
  4845. $this->assertTrue($result);
  4846. $result = $TestModel->Sample->unbindModel(array('belongsTo' => array('Apple')));
  4847. $this->assertTrue($result);
  4848. $result = $TestModel->find('all');
  4849. $expected = array(
  4850. array('Apple' => array(
  4851. 'id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4852. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17',
  4853. 'Parent' => array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4854. 'Sample' => array('id' => 2, 'apple_id' => 2, 'name' => 'sample2'),
  4855. 'Child' => array(
  4856. array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4857. array('id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4858. array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'))),
  4859. 'Sample' => array('id' =>'', 'apple_id' => '', 'name' => '')),
  4860. array('Apple' => array(
  4861. 'id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
  4862. 'Parent' => array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17',
  4863. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
  4864. 'Sample' => array(),
  4865. 'Child' => array(
  4866. array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'))),
  4867. 'Sample' => array('id' => 2, 'apple_id' => 2, 'name' => 'sample2')),
  4868. array('Apple' => array(
  4869. 'id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4870. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17',
  4871. 'Parent' => array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4872. 'Sample' => array('id' => 2, 'apple_id' => 2, 'name' => 'sample2'),
  4873. 'Child' => array(
  4874. array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4875. array('id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4876. array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'))),
  4877. 'Sample' => array('id' => 1, 'apple_id' => 3, 'name' => 'sample1')),
  4878. array('Apple' => array(
  4879. 'id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'),
  4880. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17',
  4881. 'Parent' => array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4882. 'Sample' => array('id' => 2, 'apple_id' => 2, 'name' => 'sample2'),
  4883. 'Child' => array(
  4884. array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4885. array('id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4886. array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'))),
  4887. 'Sample' => array('id' => 3, 'apple_id' => 4, 'name' => 'sample3')),
  4888. array('Apple' => array(
  4889. 'id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'),
  4890. 'Parent' => array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17',
  4891. 'Parent' => array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'),
  4892. 'Sample' => array('id' => 4, 'apple_id' => 5, 'name' => 'sample4'),
  4893. 'Child' => array(
  4894. array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'))),
  4895. 'Sample' => array('id' => 4, 'apple_id' => 5, 'name' => 'sample4')),
  4896. array('Apple' => array(
  4897. 'id' => 6, 'apple_id' => 4, 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17'),
  4898. 'Parent' => array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17',
  4899. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
  4900. 'Sample' => array('id' => 3, 'apple_id' => 4, 'name' => 'sample3'),
  4901. 'Child' => array(
  4902. array('id' => 6, 'apple_id' => 4, 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17'))),
  4903. 'Sample' => array('id' => '', 'apple_id' => '', 'name' => '')),
  4904. array('Apple' => array(
  4905. 'id' => 7, 'apple_id' => 6, 'color' => 'Some wierd color', 'name' => 'Some odd color', 'created' => '2006-12-25 05:34:21', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:34:21', 'mytime' => '22:57:17'),
  4906. 'Parent' => array('id' => 6, 'apple_id' => 4, 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17',
  4907. 'Parent' => array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'),
  4908. 'Sample' => array(),
  4909. 'Child' => array(
  4910. array('id' => 7, 'apple_id' => 6, 'color' => 'Some wierd color', 'name' => 'Some odd color', 'created' => '2006-12-25 05:34:21', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:34:21', 'mytime' => '22:57:17'))),
  4911. 'Sample' => array('id' => '', 'apple_id' => '', 'name' => '')));
  4912. $this->assertEqual($result, $expected);
  4913. $result = $TestModel->Parent->unbindModel(array('belongsTo' => array('Parent')));
  4914. $this->assertTrue($result);
  4915. $result = $TestModel->unbindModel(array('hasMany' => array('Child')));
  4916. $this->assertTrue($result);
  4917. $result = $TestModel->find('all');
  4918. $expected = array(array('Apple' => array (
  4919. 'id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4920. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17',
  4921. 'Sample' => array('id' => 2, 'apple_id' => 2, 'name' => 'sample2'),
  4922. 'Child' => array(
  4923. array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4924. array('id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4925. array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'))),
  4926. 'Sample' => array('id' =>'', 'apple_id' => '', 'name' => '')),
  4927. array('Apple' => array(
  4928. 'id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
  4929. 'Parent' => array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17',
  4930. 'Sample' => array(),
  4931. 'Child' => array(
  4932. array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'))),
  4933. 'Sample' => array('id' => 2, 'apple_id' => 2, 'name' => 'sample2',
  4934. 'Apple' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'))),
  4935. array('Apple' => array(
  4936. 'id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4937. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17',
  4938. 'Sample' => array('id' => 2, 'apple_id' => 2, 'name' => 'sample2'),
  4939. 'Child' => array(
  4940. array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4941. array('id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4942. array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'))),
  4943. 'Sample' => array('id' => 1, 'apple_id' => 3, 'name' => 'sample1',
  4944. 'Apple' => array('id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'))),
  4945. array('Apple' => array(
  4946. 'id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'),
  4947. 'Parent' => array('id' => 2, 'apple_id' => 1, 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17',
  4948. 'Sample' => array('id' => 2, 'apple_id' => 2, 'name' => 'sample2'),
  4949. 'Child' => array(
  4950. array('id' => 1, 'apple_id' => 2, 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  4951. array('id' => 3, 'apple_id' => 2, 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
  4952. array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'))),
  4953. 'Sample' => array('id' => 3, 'apple_id' => 4, 'name' => 'sample3',
  4954. 'Apple' => array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'))),
  4955. array('Apple' => array(
  4956. 'id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'),
  4957. 'Parent' => array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17',
  4958. 'Sample' => array('id' => 4, 'apple_id' => 5, 'name' => 'sample4'),
  4959. 'Child' => array(
  4960. array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'))),
  4961. 'Sample' => array('id' => 4, 'apple_id' => 5, 'name' => 'sample4',
  4962. 'Apple' => array('id' => 5, 'apple_id' => 5, 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'))),
  4963. array('Apple' => array(
  4964. 'id' => 6, 'apple_id' => 4, 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17'),
  4965. 'Parent' => array('id' => 4, 'apple_id' => 2, 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17',
  4966. 'Sample' => array('id' => 3, 'apple_id' => 4, 'name' => 'sample3'),
  4967. 'Child' => array(
  4968. array('id' => 6, 'apple_id' => 4, 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17'))),
  4969. 'Sample' => array('id' => '', 'apple_id' => '', 'name' => '')),
  4970. array('Apple' => array(
  4971. 'id' => 7, 'apple_id' => 6, 'color' => 'Some wierd color', 'name' => 'Some odd color', 'created' => '2006-12-25 05:34:21', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:34:21', 'mytime' => '22:57:17'),
  4972. 'Parent' => array('id' => 6, 'apple_id' => 4, 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17',
  4973. 'Sample' => array(),
  4974. 'Child' => array(
  4975. array('id' => 7, 'apple_id' => 6, 'color' => 'Some wierd color', 'name' => 'Some odd color', 'created' => '2006-12-25 05:34:21', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:34:21', 'mytime' => '22:57:17'))),
  4976. 'Sample' => array('id' => '', 'apple_id' => '', 'name' => '')));
  4977. $this->assertEqual($result, $expected);
  4978. }
  4979. /**
  4980. * testSelfAssociationAfterFind method
  4981. *
  4982. * @access public
  4983. * @return void
  4984. */
  4985. function testSelfAssociationAfterFind() {
  4986. $this->loadFixtures('Apple');
  4987. $afterFindModel = new NodeAfterFind();
  4988. $afterFindModel->recursive = 3;
  4989. $afterFindData = $afterFindModel->find('all');
  4990. $duplicateModel = new NodeAfterFind();
  4991. $duplicateModel->recursive = 3;
  4992. $duplicateModelData = $duplicateModel->find('all');
  4993. $noAfterFindModel = new NodeNoAfterFind();
  4994. $noAfterFindModel->recursive = 3;
  4995. $noAfterFindData = $noAfterFindModel->find('all');
  4996. $this->assertFalse($afterFindModel == $noAfterFindModel);
  4997. // Limitation of PHP 4 and PHP 5 > 5.1.6 when comparing recursive objects
  4998. if (PHP_VERSION === '5.1.6') {
  4999. $this->assertFalse($afterFindModel != $duplicateModel);
  5000. }
  5001. $this->assertEqual($afterFindData, $noAfterFindData);
  5002. }
  5003. /**
  5004. * testAutoSaveUuid method
  5005. *
  5006. * @access public
  5007. * @return void
  5008. */
  5009. function testAutoSaveUuid() {
  5010. // SQLite does not support non-integer primary keys, and SQL Server
  5011. // is still having problems with custom PK's
  5012. if ($this->db->config['driver'] == 'sqlite' || $this->db->config['driver'] == 'mssql') {
  5013. return;
  5014. }
  5015. $this->loadFixtures('Uuid');
  5016. $TestModel =& new Uuid();
  5017. $TestModel->save(array('title' => 'Test record'));
  5018. $result = $TestModel->findByTitle('Test record');
  5019. $this->assertEqual(array_keys($result['Uuid']), array('id', 'title', 'count', 'created', 'updated'));
  5020. $this->assertEqual(strlen($result['Uuid']['id']), 36);
  5021. }
  5022. /**
  5023. * testZeroDefaultFieldValue method
  5024. *
  5025. * @access public
  5026. * @return void
  5027. */
  5028. function testZeroDefaultFieldValue() {
  5029. $this->skipIf(
  5030. $this->db->config['driver'] == 'sqlite',
  5031. 'SQLite uses loose typing, this operation is unsupported'
  5032. );
  5033. $this->loadFixtures('DataTest');
  5034. $TestModel =& new DataTest();
  5035. $TestModel->create(array());
  5036. $TestModel->save();
  5037. $result = $TestModel->findById($TestModel->id);
  5038. $this->assertIdentical($result['DataTest']['count'], '0');
  5039. $this->assertIdentical($result['DataTest']['float'], '0');
  5040. }
  5041. /**
  5042. * testNonNumericHabtmJoinKey method
  5043. *
  5044. * @access public
  5045. * @return void
  5046. */
  5047. function testNonNumericHabtmJoinKey() {
  5048. $this->loadFixtures('Post', 'Tag', 'PostsTag');
  5049. $Post =& new Post();
  5050. $Post->bind('Tag', array('type' => 'hasAndBelongsToMany'));
  5051. $Post->Tag->primaryKey = 'tag';
  5052. $result = $Post->find('all');
  5053. $expected = array(
  5054. array(
  5055. 'Post' => array('id' => '1', 'author_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'),
  5056. 'Author' => array('id' => null, 'user' => null, 'password' => null, 'created' => null, 'updated' => null, 'test' => 'working'),
  5057. 'Tag' => array(
  5058. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  5059. array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31'),
  5060. )
  5061. ),
  5062. array(
  5063. 'Post' => array('id' => '2', 'author_id' => '3', 'title' => 'Second Post', 'body' => 'Second Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'),
  5064. 'Author' => array('id' => null, 'user' => null, 'password' => null, 'created' => null, 'updated' => null, 'test' => 'working'),
  5065. 'Tag' => array(
  5066. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  5067. array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')
  5068. )
  5069. ),
  5070. array(
  5071. 'Post' => array('id' => '3', 'author_id' => '1', 'title' => 'Third Post', 'body' => 'Third Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'),
  5072. 'Author' => array('id' => null, 'user' => null, 'password' => null, 'created' => null, 'updated' => null, 'test' => 'working'),
  5073. 'Tag' => array()
  5074. )
  5075. );
  5076. $this->assertEqual($result, $expected);
  5077. }
  5078. /**
  5079. * testDeconstructFields method
  5080. *
  5081. * @access public
  5082. * @return void
  5083. */
  5084. function testDeconstructFields() {
  5085. $this->loadFixtures('Apple');
  5086. $TestModel =& new Apple();
  5087. //test null/empty values first
  5088. $data['Apple']['created']['year'] = '';
  5089. $data['Apple']['created']['month'] = '';
  5090. $data['Apple']['created']['day'] = '';
  5091. $data['Apple']['created']['hour'] = '';
  5092. $data['Apple']['created']['min'] = '';
  5093. $data['Apple']['created']['sec'] = '';
  5094. $TestModel->data = null;
  5095. $TestModel->set($data);
  5096. $expected = array('Apple'=> array('created'=> ''));
  5097. $this->assertEqual($TestModel->data, $expected);
  5098. $data = array();
  5099. $data['Apple']['date']['year'] = '';
  5100. $data['Apple']['date']['month'] = '';
  5101. $data['Apple']['date']['day'] = '';
  5102. $TestModel->data = null;
  5103. $TestModel->set($data);
  5104. $expected = array('Apple'=> array('date'=> ''));
  5105. $this->assertEqual($TestModel->data, $expected);
  5106. $data = array();
  5107. $data['Apple']['mytime']['hour'] = '';
  5108. $data['Apple']['mytime']['min'] = '';
  5109. $data['Apple']['mytime']['sec'] = '';
  5110. $TestModel->data = null;
  5111. $TestModel->set($data);
  5112. $expected = array('Apple'=> array('mytime'=> ''));
  5113. $this->assertEqual($TestModel->data, $expected);
  5114. //test other data variations
  5115. $data = array();
  5116. $data['Apple']['created']['year'] = '2007';
  5117. $data['Apple']['created']['month'] = '08';
  5118. $data['Apple']['created']['day'] = '20';
  5119. $data['Apple']['created']['hour'] = '';
  5120. $data['Apple']['created']['min'] = '';
  5121. $data['Apple']['created']['sec'] = '';
  5122. $TestModel->data = null;
  5123. $TestModel->set($data);
  5124. $expected = array('Apple'=> array('created'=> '2007-08-20 00:00:00'));
  5125. $this->assertEqual($TestModel->data, $expected);
  5126. $data = array();
  5127. $data['Apple']['created']['year'] = '2007';
  5128. $data['Apple']['created']['month'] = '08';
  5129. $data['Apple']['created']['day'] = '20';
  5130. $data['Apple']['created']['hour'] = '10';
  5131. $data['Apple']['created']['min'] = '12';
  5132. $data['Apple']['created']['sec'] = '';
  5133. $TestModel->data = null;
  5134. $TestModel->set($data);
  5135. $expected = array('Apple'=> array('created'=> '2007-08-20 10:12:00'));
  5136. $this->assertEqual($TestModel->data, $expected);
  5137. $data = array();
  5138. $data['Apple']['created']['year'] = '2007';
  5139. $data['Apple']['created']['month'] = '';
  5140. $data['Apple']['created']['day'] = '12';
  5141. $data['Apple']['created']['hour'] = '20';
  5142. $data['Apple']['created']['min'] = '';
  5143. $data['Apple']['created']['sec'] = '';
  5144. $TestModel->data = null;
  5145. $TestModel->set($data);
  5146. $expected = array('Apple'=> array('created'=> ''));
  5147. $this->assertEqual($TestModel->data, $expected);
  5148. $data = array();
  5149. $data['Apple']['created']['hour'] = '20';
  5150. $data['Apple']['created']['min'] = '33';
  5151. $TestModel->data = null;
  5152. $TestModel->set($data);
  5153. $expected = array('Apple'=> array('created'=> ''));
  5154. $this->assertEqual($TestModel->data, $expected);
  5155. $data = array();
  5156. $data['Apple']['created']['hour'] = '20';
  5157. $data['Apple']['created']['min'] = '33';
  5158. $data['Apple']['created']['sec'] = '33';
  5159. $TestModel->data = null;
  5160. $TestModel->set($data);
  5161. $expected = array('Apple'=> array('created'=> ''));
  5162. $this->assertEqual($TestModel->data, $expected);
  5163. $data = array();
  5164. $data['Apple']['created']['hour'] = '13';
  5165. $data['Apple']['created']['min'] = '00';
  5166. $data['Apple']['date']['year'] = '2006';
  5167. $data['Apple']['date']['month'] = '12';
  5168. $data['Apple']['date']['day'] = '25';
  5169. $TestModel->data = null;
  5170. $TestModel->set($data);
  5171. $expected = array('Apple'=> array('created'=> '', 'date'=> '2006-12-25'));
  5172. $this->assertEqual($TestModel->data, $expected);
  5173. $data = array();
  5174. $data['Apple']['created']['year'] = '2007';
  5175. $data['Apple']['created']['month'] = '08';
  5176. $data['Apple']['created']['day'] = '20';
  5177. $data['Apple']['created']['hour'] = '10';
  5178. $data['Apple']['created']['min'] = '12';
  5179. $data['Apple']['created']['sec'] = '09';
  5180. $data['Apple']['date']['year'] = '2006';
  5181. $data['Apple']['date']['month'] = '12';
  5182. $data['Apple']['date']['day'] = '25';
  5183. $TestModel->data = null;
  5184. $TestModel->set($data);
  5185. $expected = array('Apple'=> array('created'=> '2007-08-20 10:12:09', 'date'=> '2006-12-25'));
  5186. $this->assertEqual($TestModel->data, $expected);
  5187. $data = array();
  5188. $data['Apple']['created']['year'] = '--';
  5189. $data['Apple']['created']['month'] = '--';
  5190. $data['Apple']['created']['day'] = '--';
  5191. $data['Apple']['created']['hour'] = '--';
  5192. $data['Apple']['created']['min'] = '--';
  5193. $data['Apple']['created']['sec'] = '--';
  5194. $data['Apple']['date']['year'] = '--';
  5195. $data['Apple']['date']['month'] = '--';
  5196. $data['Apple']['date']['day'] = '--';
  5197. $TestModel->data = null;
  5198. $TestModel->set($data);
  5199. $expected = array('Apple'=> array('created'=> '', 'date'=> ''));
  5200. $this->assertEqual($TestModel->data, $expected);
  5201. $data = array();
  5202. $data['Apple']['created']['year'] = '2007';
  5203. $data['Apple']['created']['month'] = '--';
  5204. $data['Apple']['created']['day'] = '20';
  5205. $data['Apple']['created']['hour'] = '10';
  5206. $data['Apple']['created']['min'] = '12';
  5207. $data['Apple']['created']['sec'] = '09';
  5208. $data['Apple']['date']['year'] = '2006';
  5209. $data['Apple']['date']['month'] = '12';
  5210. $data['Apple']['date']['day'] = '25';
  5211. $TestModel->data = null;
  5212. $TestModel->set($data);
  5213. $expected = array('Apple'=> array('created'=> '', 'date'=> '2006-12-25'));
  5214. $this->assertEqual($TestModel->data, $expected);
  5215. $data = array();
  5216. $data['Apple']['date']['year'] = '2006';
  5217. $data['Apple']['date']['month'] = '12';
  5218. $data['Apple']['date']['day'] = '25';
  5219. $TestModel->data = null;
  5220. $TestModel->set($data);
  5221. $expected = array('Apple'=> array('date'=> '2006-12-25'));
  5222. $this->assertEqual($TestModel->data, $expected);
  5223. $data = array();
  5224. $data['Apple']['mytime']['hour'] = '03';
  5225. $data['Apple']['mytime']['min'] = '04';
  5226. $data['Apple']['mytime']['sec'] = '04';
  5227. $TestModel->data = null;
  5228. $TestModel->set($data);
  5229. $expected = array('Apple'=> array('mytime'=> '03:04:04'));
  5230. $this->assertEqual($TestModel->data, $expected);
  5231. $data = array();
  5232. $data['Apple']['mytime']['hour'] = '3';
  5233. $data['Apple']['mytime']['min'] = '4';
  5234. $data['Apple']['mytime']['sec'] = '4';
  5235. $TestModel->data = null;
  5236. $TestModel->set($data);
  5237. $expected = array('Apple' => array('mytime'=> '03:04:04'));
  5238. $this->assertEqual($TestModel->data, $expected);
  5239. $data = array();
  5240. $data['Apple']['mytime']['hour'] = '03';
  5241. $data['Apple']['mytime']['min'] = '4';
  5242. $data['Apple']['mytime']['sec'] = '4';
  5243. $TestModel->data = null;
  5244. $TestModel->set($data);
  5245. $expected = array('Apple'=> array('mytime'=> '03:04:04'));
  5246. $this->assertEqual($TestModel->data, $expected);
  5247. }
  5248. /**
  5249. * testTablePrefixSwitching method
  5250. *
  5251. * @access public
  5252. * @return void
  5253. */
  5254. function testTablePrefixSwitching() {
  5255. ConnectionManager::create('database1', array_merge($this->db->config, array('prefix' => 'aaa_')));
  5256. ConnectionManager::create('database2', array_merge($this->db->config, array('prefix' => 'bbb_')));
  5257. $db1 = ConnectionManager::getDataSource('database1');
  5258. $db2 = ConnectionManager::getDataSource('database2');
  5259. $TestModel = new Apple();
  5260. $TestModel->setDataSource('database1');
  5261. $this->assertEqual($this->db->fullTableName($TestModel, false), 'aaa_apples');
  5262. $this->assertEqual($db1->fullTableName($TestModel, false), 'aaa_apples');
  5263. $this->assertEqual($db2->fullTableName($TestModel, false), 'aaa_apples');
  5264. $TestModel->setDataSource('database2');
  5265. $this->assertEqual($this->db->fullTableName($TestModel, false), 'bbb_apples');
  5266. $this->assertEqual($db1->fullTableName($TestModel, false), 'bbb_apples');
  5267. $this->assertEqual($db2->fullTableName($TestModel, false), 'bbb_apples');
  5268. $TestModel = new Apple();
  5269. $TestModel->tablePrefix = 'custom_';
  5270. $this->assertEqual($this->db->fullTableName($TestModel, false), 'custom_apples');
  5271. $TestModel->setDataSource('database1');
  5272. $this->assertEqual($this->db->fullTableName($TestModel, false), 'custom_apples');
  5273. $this->assertEqual($db1->fullTableName($TestModel, false), 'custom_apples');
  5274. $TestModel = new Apple();
  5275. $TestModel->setDataSource('database1');
  5276. $this->assertEqual($this->db->fullTableName($TestModel, false), 'aaa_apples');
  5277. $TestModel->tablePrefix = '';
  5278. $TestModel->setDataSource('database2');
  5279. $this->assertEqual($db2->fullTableName($TestModel, false), 'apples');
  5280. $this->assertEqual($db1->fullTableName($TestModel, false), 'apples');
  5281. $TestModel->tablePrefix = null;
  5282. $TestModel->setDataSource('database1');
  5283. $this->assertEqual($db2->fullTableName($TestModel, false), 'aaa_apples');
  5284. $this->assertEqual($db1->fullTableName($TestModel, false), 'aaa_apples');
  5285. $TestModel->tablePrefix = false;
  5286. $TestModel->setDataSource('database2');
  5287. $this->assertEqual($db2->fullTableName($TestModel, false), 'apples');
  5288. $this->assertEqual($db1->fullTableName($TestModel, false), 'apples');
  5289. }
  5290. /**
  5291. * testDynamicBehaviorAttachment method
  5292. *
  5293. * @access public
  5294. * @return void
  5295. */
  5296. function testDynamicBehaviorAttachment() {
  5297. $this->loadFixtures('Apple');
  5298. $TestModel =& new Apple();
  5299. $this->assertEqual($TestModel->Behaviors->attached(), array());
  5300. $TestModel->Behaviors->attach('Tree', array('left' => 'left_field', 'right' => 'right_field'));
  5301. $this->assertTrue(is_object($TestModel->Behaviors->Tree));
  5302. $this->assertEqual($TestModel->Behaviors->attached(), array('Tree'));
  5303. $expected = array(
  5304. 'parent' => 'parent_id', 'left' => 'left_field', 'right' => 'right_field', 'scope' => '1 = 1',
  5305. 'type' => 'nested', '__parentChange' => false, 'recursive' => -1
  5306. );
  5307. $this->assertEqual($TestModel->Behaviors->Tree->settings['Apple'], $expected);
  5308. $expected['enabled'] = false;
  5309. $TestModel->Behaviors->attach('Tree', array('enabled' => false));
  5310. $this->assertEqual($TestModel->Behaviors->Tree->settings['Apple'], $expected);
  5311. $this->assertEqual($TestModel->Behaviors->attached(), array('Tree'));
  5312. $TestModel->Behaviors->detach('Tree');
  5313. $this->assertEqual($TestModel->Behaviors->attached(), array());
  5314. $this->assertFalse(isset($TestModel->Behaviors->Tree));
  5315. }
  5316. /**
  5317. * Tests cross database joins. Requires $test and $test2 to both be set in DATABASE_CONFIG
  5318. * NOTE: When testing on MySQL, you must set 'persistent' => false on *both* database connections,
  5319. * or one connection will step on the other.
  5320. */
  5321. function testCrossDatabaseJoins() {
  5322. $config = new DATABASE_CONFIG();
  5323. if (!isset($config->test) || !isset($config->test2)) {
  5324. echo "<br />Primary and secondary test databases not configured, skipping cross-database join tests<br />";
  5325. echo "To run these tests, you must define \$test and \$test2 in your database configuration.<br />";
  5326. return;
  5327. }
  5328. $this->loadFixtures('Article', 'Tag', 'ArticlesTag', 'User', 'Comment');
  5329. $TestModel =& new Article();
  5330. $expected = array(
  5331. array(
  5332. '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'),
  5333. 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  5334. 'Comment' => array(
  5335. 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'),
  5336. array('id' => '2', 'article_id' => '1', 'user_id' => '4', 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'),
  5337. array('id' => '3', 'article_id' => '1', 'user_id' => '1', 'comment' => 'Third Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:49:23', 'updated' => '2007-03-18 10:51:31'),
  5338. array('id' => '4', 'article_id' => '1', 'user_id' => '1', 'comment' => 'Fourth Comment for First Article', 'published' => 'N', 'created' => '2007-03-18 10:51:23', 'updated' => '2007-03-18 10:53:31')
  5339. ),
  5340. 'Tag' => array(
  5341. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  5342. array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')
  5343. )
  5344. ),
  5345. array(
  5346. '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'),
  5347. 'User' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31'),
  5348. 'Comment' => array(
  5349. 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'),
  5350. 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')
  5351. ),
  5352. 'Tag' => array(
  5353. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  5354. array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')
  5355. )
  5356. ),
  5357. array(
  5358. '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'),
  5359. 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  5360. 'Comment' => array(),
  5361. 'Tag' => array()
  5362. )
  5363. );
  5364. $this->assertEqual($TestModel->find('all'), $expected);
  5365. $db2 =& ConnectionManager::getDataSource('test2');
  5366. foreach (array('User', 'Comment') as $class) {
  5367. $this->_fixtures[$this->_fixtureClassMap[$class]]->create($db2);
  5368. $this->_fixtures[$this->_fixtureClassMap[$class]]->insert($db2);
  5369. $this->db->truncate(Inflector::pluralize(Inflector::underscore($class)));
  5370. }
  5371. $this->assertEqual($TestModel->User->find('all'), array());
  5372. $this->assertEqual($TestModel->Comment->find('all'), array());
  5373. $this->assertEqual($TestModel->find('count'), 3);
  5374. $TestModel->User->setDataSource('test2');
  5375. $TestModel->Comment->setDataSource('test2');
  5376. $result = Set::extract($TestModel->User->find('all'), '{n}.User.id');
  5377. $this->assertEqual($result, array('1', '2', '3', '4'));
  5378. $this->assertEqual($TestModel->find('all'), $expected);
  5379. $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')));
  5380. $expected = array(
  5381. array(
  5382. 'Comment' => 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'),
  5383. 'User' => array('id' => '2', 'user' => 'nate', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:18:23', 'updated' => '2007-03-17 01:20:31'),
  5384. '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')
  5385. ),
  5386. array(
  5387. 'Comment' => array('id' => '2', 'article_id' => '1', 'user_id' => '4', 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'),
  5388. 'User' => array('id' => '4', 'user' => 'garrett', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:22:23', 'updated' => '2007-03-17 01:24:31'),
  5389. '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')
  5390. ),
  5391. array(
  5392. 'Comment' => array('id' => '3', 'article_id' => '1', 'user_id' => '1', 'comment' => 'Third Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:49:23', 'updated' => '2007-03-18 10:51:31'),
  5393. 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  5394. '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')
  5395. ),
  5396. array(
  5397. 'Comment' => array('id' => '4', 'article_id' => '1', 'user_id' => '1', 'comment' => 'Fourth Comment for First Article', 'published' => 'N', 'created' => '2007-03-18 10:51:23', 'updated' => '2007-03-18 10:53:31'),
  5398. 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  5399. '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')
  5400. ),
  5401. array(
  5402. 'Comment' => 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'),
  5403. 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  5404. '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')
  5405. ),
  5406. array(
  5407. 'Comment' => 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'),
  5408. 'User' => array('id' => '2', 'user' => 'nate', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:18:23', 'updated' => '2007-03-17 01:20:31'),
  5409. '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')
  5410. )
  5411. );
  5412. $this->assertEqual($TestModel->Comment->find('all'), $expected);
  5413. foreach (array('User', 'Comment') as $class) {
  5414. $this->_fixtures[$this->_fixtureClassMap[$class]]->drop($db2);
  5415. }
  5416. }
  5417. /**
  5418. * testDisplayField method
  5419. *
  5420. * @access public
  5421. * @return void
  5422. */
  5423. function testDisplayField() {
  5424. $this->loadFixtures('Post', 'Comment', 'Person');
  5425. $Post = new Post();
  5426. $Comment = new Comment();
  5427. $Person = new Person();
  5428. $this->assertEqual($Post->displayField, 'title');
  5429. $this->assertEqual($Person->displayField, 'name');
  5430. $this->assertEqual($Comment->displayField, 'id');
  5431. }
  5432. /**
  5433. * testSchema method
  5434. *
  5435. * @access public
  5436. * @return void
  5437. */
  5438. function testSchema() {
  5439. $Post = new Post();
  5440. $result = $Post->schema();
  5441. $columns = array('id', 'author_id', 'title', 'body', 'published', 'created', 'updated');
  5442. $this->assertEqual(array_keys($result), $columns);
  5443. $types = array('integer', 'integer', 'string', 'text', 'string', 'datetime', 'datetime');
  5444. $this->assertEqual(Set::extract(array_values($result), '{n}.type'), $types);
  5445. $result = $Post->schema('body');
  5446. $this->assertEqual($result['type'], 'text');
  5447. $this->assertNull($Post->schema('foo'));
  5448. $this->assertEqual($Post->getColumnTypes(), array_combine($columns, $types));
  5449. }
  5450. /**
  5451. * testOldQuery method
  5452. *
  5453. * @access public
  5454. * @return void
  5455. */
  5456. function testOldQuery() {
  5457. $this->loadFixtures('Article');
  5458. $Article =& new Article();
  5459. $query = 'SELECT title FROM ' . $this->db->fullTableName('articles') . ' WHERE ' . $this->db->fullTableName('articles') . '.id IN (1,2)';
  5460. $results = $Article->query($query);
  5461. $this->assertTrue(is_array($results));
  5462. $this->assertEqual(count($results), 2);
  5463. $query = 'SELECT title, body FROM ' . $this->db->fullTableName('articles') . ' WHERE ' . $this->db->fullTableName('articles') . '.id = 1';
  5464. $results = $Article->query($query, false);
  5465. $this->assertTrue(!isset($this->db->_queryCache[$query]));
  5466. $this->assertTrue(is_array($results));
  5467. $query = 'SELECT title, id FROM ' . $this->db->fullTableName('articles') . ' WHERE ' . $this->db->fullTableName('articles') . '.published = ' . $this->db->value('Y');
  5468. $results = $Article->query($query, true);
  5469. $this->assertTrue(isset($this->db->_queryCache[$query]));
  5470. $this->assertTrue(is_array($results));
  5471. }
  5472. /**
  5473. * testPreparedQuery method
  5474. *
  5475. * @access public
  5476. * @return void
  5477. */
  5478. function testPreparedQuery() {
  5479. $this->loadFixtures('Article');
  5480. $Article =& new Article();
  5481. $this->db->_queryCache = array();
  5482. $finalQuery = 'SELECT title, published FROM ' . $this->db->fullTableName('articles') . ' WHERE ' . $this->db->fullTableName('articles') . '.id = ' . $this->db->value(1) . ' AND ' . $this->db->fullTableName('articles') . '.published = ' . $this->db->value('Y');
  5483. $query = 'SELECT title, published FROM ' . $this->db->fullTableName('articles') . ' WHERE ' . $this->db->fullTableName('articles') . '.id = ? AND ' . $this->db->fullTableName('articles') . '.published = ?';
  5484. $params = array(1, 'Y');
  5485. $result = $Article->query($query, $params);
  5486. $expected = array('0' => array($this->db->fullTableName('articles', false) => array('title' => 'First Article', 'published' => 'Y')));
  5487. if (isset($result[0][0])) {
  5488. $expected[0][0] = $expected[0][$this->db->fullTableName('articles', false)];
  5489. unset($expected[0][$this->db->fullTableName('articles', false)]);
  5490. }
  5491. $this->assertEqual($result, $expected);
  5492. $this->assertTrue(isset($this->db->_queryCache[$finalQuery]));
  5493. $finalQuery = 'SELECT id, created FROM ' . $this->db->fullTableName('articles') . ' WHERE ' . $this->db->fullTableName('articles') . '.title = ' . $this->db->value('First Article');
  5494. $query = 'SELECT id, created FROM ' . $this->db->fullTableName('articles') . ' WHERE ' . $this->db->fullTableName('articles') . '.title = ?';
  5495. $params = array('First Article');
  5496. $result = $Article->query($query, $params, false);
  5497. $this->assertTrue(is_array($result));
  5498. $this->assertTrue(isset($result[0][$this->db->fullTableName('articles', false)]) || isset($result[0][0]));
  5499. $this->assertFalse(isset($this->db->_queryCache[$finalQuery]));
  5500. $query = 'SELECT title FROM ' . $this->db->fullTableName('articles') . ' WHERE ' . $this->db->fullTableName('articles') . '.title LIKE ?';
  5501. $params = array('%First%');
  5502. $result = $Article->query($query, $params);
  5503. $this->assertTrue(is_array($result));
  5504. $this->assertTrue(
  5505. isset($result[0][$this->db->fullTableName('articles', false)]['title']) ||
  5506. isset($result[0][0]['title'])
  5507. );
  5508. //related to ticket #5035
  5509. $query = 'SELECT title FROM ' . $this->db->fullTableName('articles') . ' WHERE title = ? AND published = ?';
  5510. $params = array('First? Article', 'Y');
  5511. $Article->query($query, $params);
  5512. $expected = 'SELECT title FROM ' . $this->db->fullTableName('articles') . " WHERE title = 'First? Article' AND published = 'Y'";
  5513. $this->assertTrue(isset($this->db->_queryCache[$expected]));
  5514. }
  5515. /**
  5516. * testParameterMismatch method
  5517. *
  5518. * @access public
  5519. * @return void
  5520. */
  5521. function testParameterMismatch() {
  5522. $this->loadFixtures('Article');
  5523. $Article =& new Article();
  5524. $query = 'SELECT * FROM ' . $this->db->fullTableName('articles') . ' WHERE ' . $this->db->fullTableName('articles') . '.published = ? AND ' . $this->db->fullTableName('articles') . '.user_id = ?';
  5525. $params = array('Y');
  5526. $this->expectError();
  5527. ob_start();
  5528. $result = $Article->query($query, $params);
  5529. ob_end_clean();
  5530. $this->assertEqual($result, null);
  5531. }
  5532. /**
  5533. * testVeryStrangeUseCase method
  5534. *
  5535. * @access public
  5536. * @return void
  5537. */
  5538. function testVeryStrangeUseCase() {
  5539. if ($this->db->config['driver'] == 'mssql') {
  5540. return;
  5541. }
  5542. $this->loadFixtures('Article');
  5543. $Article =& new Article();
  5544. $query = 'SELECT * FROM ? WHERE ? = ? AND ? = ?';
  5545. $param = array($this->db->fullTableName('articles'), $this->db->fullTableName('articles') . '.user_id', '3', $this->db->fullTableName('articles') . '.published', 'Y');
  5546. $this->expectError();
  5547. ob_start();
  5548. $result = $Article->query($query, $param);
  5549. ob_end_clean();
  5550. }
  5551. /**
  5552. * testUnderscoreFieldSave method
  5553. *
  5554. * @access public
  5555. * @return void
  5556. */
  5557. function testUnderscoreFieldSave() {
  5558. $this->loadFixtures('UnderscoreField');
  5559. $UnderscoreField =& new UnderscoreField();
  5560. $currentCount = $UnderscoreField->find('count');
  5561. $this->assertEqual($currentCount, 3);
  5562. $data = array('UnderscoreField' => array(
  5563. 'user_id' => '1',
  5564. 'my_model_has_a_field' => 'Content here',
  5565. 'body' => 'Body',
  5566. 'published' => 'Y',
  5567. 'another_field' => 4
  5568. ));
  5569. $ret = $UnderscoreField->save($data);
  5570. $this->assertTrue($ret);
  5571. $currentCount = $UnderscoreField->find('count');
  5572. $this->assertEqual($currentCount, 4);
  5573. }
  5574. /**
  5575. * testGroupBy method
  5576. *
  5577. * These tests will never pass with Postgres or Oracle as all fields in a select must be
  5578. * part of an aggregate function or in the GROUP BY statement.
  5579. *
  5580. * @access public
  5581. * @return void
  5582. */
  5583. function testGroupBy() {
  5584. $db = ConnectionManager::getDataSource('test_suite');
  5585. $isStrictGroupBy = in_array($db->config['driver'], array('postgres', 'oracle'));
  5586. if ($this->skipif($isStrictGroupBy, 'Postgresql and Oracle have strict GROUP BY and are incompatible with this test.')) {
  5587. return;
  5588. }
  5589. $this->loadFixtures('Project', 'Product', 'Thread', 'Message', 'Bid');
  5590. $Thread =& new Thread();
  5591. $Product =& new Product();
  5592. $result = $Thread->find('all', array(
  5593. 'group' => 'Thread.project_id'
  5594. ));
  5595. $expected = array(
  5596. array(
  5597. 'Thread' => array('id' => 1, 'project_id' => 1, 'name' => 'Project 1, Thread 1'),
  5598. 'Project' => array('id' => 1, 'name' => 'Project 1'),
  5599. 'Message' => array(array('id' => 1, 'thread_id' => 1, 'name' => 'Thread 1, Message 1')),
  5600. ),
  5601. array(
  5602. 'Thread' => array('id' => 3, 'project_id' => 2, 'name' => 'Project 2, Thread 1'),
  5603. 'Project' => array('id' => 2, 'name' => 'Project 2'),
  5604. 'Message' => array(array('id' => 3, 'thread_id' => 3, 'name' => 'Thread 3, Message 1')),
  5605. ),
  5606. );
  5607. $this->assertEqual($result, $expected);
  5608. $rows = $Thread->find('all', array(
  5609. 'group' => 'Thread.project_id', 'fields' => array('Thread.project_id', 'COUNT(*) AS total')
  5610. ));
  5611. $result = array();
  5612. foreach($rows as $row) {
  5613. $result[$row['Thread']['project_id']] = $row[0]['total'];
  5614. }
  5615. $expected = array(
  5616. 1 => 2,
  5617. 2 => 1
  5618. );
  5619. $this->assertEqual($result, $expected);
  5620. $rows = $Thread->find('all', array(
  5621. 'group' => 'Thread.project_id', 'fields' => array('Thread.project_id', 'COUNT(*) AS total'), 'order'=> 'Thread.project_id'
  5622. ));
  5623. $result = array();
  5624. foreach($rows as $row) {
  5625. $result[$row['Thread']['project_id']] = $row[0]['total'];
  5626. }
  5627. $expected = array(
  5628. 1 => 2,
  5629. 2 => 1
  5630. );
  5631. $this->assertEqual($result, $expected);
  5632. $result = $Thread->find('all', array(
  5633. 'conditions' => array('Thread.project_id' => 1), 'group' => 'Thread.project_id')
  5634. );
  5635. $expected = array(
  5636. array(
  5637. 'Thread' => array('id' => 1, 'project_id' => 1, 'name' => 'Project 1, Thread 1'),
  5638. 'Project' => array('id' => 1, 'name' => 'Project 1'),
  5639. 'Message' => array(array('id' => 1, 'thread_id' => 1, 'name' => 'Thread 1, Message 1')),
  5640. )
  5641. );
  5642. $this->assertEqual($result, $expected);
  5643. $result = $Thread->find('all', array(
  5644. 'conditions' => array('Thread.project_id' => 1), 'group' => 'Thread.project_id, Project.id')
  5645. );
  5646. $this->assertEqual($result, $expected);
  5647. $result = $Thread->find('all', array(
  5648. 'conditions' => array('Thread.project_id' => 1), 'group' => 'project_id')
  5649. );
  5650. $this->assertEqual($result, $expected);
  5651. $result = $Thread->find('all', array(
  5652. 'conditions' => array('Thread.project_id' => 1), 'group' => array('project_id'))
  5653. );
  5654. $this->assertEqual($result, $expected);
  5655. $result = $Thread->find('all', array(
  5656. 'conditions' => array('Thread.project_id' => 1), 'group' => array('project_id', 'Project.id'))
  5657. );
  5658. $this->assertEqual($result, $expected);
  5659. $result = $Thread->find('all', array(
  5660. 'conditions' => array('Thread.project_id' => 1), 'group' => array('Thread.project_id', 'Project.id'))
  5661. );
  5662. $this->assertEqual($result, $expected);
  5663. $expected = array(
  5664. array('Product' => array('type' => 'Clothing'), array('price' => 32)),
  5665. array('Product' => array('type' => 'Food'), array('price' => 9)),
  5666. array('Product' => array('type' => 'Music'), array( 'price' => 4)),
  5667. array('Product' => array('type' => 'Toy'), array('price' => 3))
  5668. );
  5669. $result = $Product->find('all',array('fields'=>array('Product.type','MIN(Product.price) as price'), 'group'=> 'Product.type'));
  5670. $this->assertEqual($result, $expected);
  5671. $result = $Product->find('all', array('fields'=>array('Product.type','MIN(Product.price) as price'), 'group'=> array('Product.type')));
  5672. $this->assertEqual($result, $expected);
  5673. }
  5674. /**
  5675. * testSaveDateAsFirstEntry method
  5676. *
  5677. * @access public
  5678. * @return void
  5679. */
  5680. function testSaveDateAsFirstEntry() {
  5681. $this->loadFixtures('Article');
  5682. $Article =& new Article();
  5683. $data = array('Article' => array(
  5684. 'created' => array(
  5685. 'day' => '1', 'month' => '1', 'year' => '2008'
  5686. ),
  5687. 'title' => 'Test Title',
  5688. // schreck - Jul 30, 2008 - should this be set to something else?
  5689. 'user_id' => 1
  5690. ));
  5691. $Article->create();
  5692. $this->assertTrue($Article->save($data));
  5693. $testResult = $Article->find(array('Article.title' => 'Test Title'));
  5694. $this->assertEqual($testResult['Article']['title'], $data['Article']['title']);
  5695. $this->assertEqual($testResult['Article']['created'], '2008-01-01 00:00:00');
  5696. }
  5697. /**
  5698. * testDeleteDependentWithConditions method
  5699. *
  5700. * @access public
  5701. * @return void
  5702. */
  5703. function testDeleteDependentWithConditions() {
  5704. $this->loadFixtures('Cd','Book','OverallFavorite');
  5705. $Cd =& new Cd();
  5706. $OverallFavorite =& new OverallFavorite();
  5707. $Cd->del(1);
  5708. $result = $OverallFavorite->find('all', array('fields' => array('model_type', 'model_id', 'priority')));
  5709. $expected = array(array('OverallFavorite' => array('model_type' => 'Book', 'model_id' => 1, 'priority' => 2)));
  5710. $this->assertTrue(is_array($result));
  5711. $this->assertEqual($result, $expected);
  5712. }
  5713. /**
  5714. * testSaveAllHasManyValidationOnly method
  5715. *
  5716. * @access public
  5717. * @return void
  5718. */
  5719. function testSaveAllHasManyValidationOnly() {
  5720. $this->loadFixtures('Article', 'Comment');
  5721. $TestModel =& new Article();
  5722. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  5723. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  5724. $result = $TestModel->saveAll(
  5725. array(
  5726. 'Article' => array('id' => 2),
  5727. 'Comment' => array(
  5728. array('id' => 1, 'comment' => '', 'published' => 'Y', 'user_id' => 1),
  5729. array('id' => 2, 'comment' => 'comment', 'published' => 'Y', 'user_id' => 1),
  5730. )
  5731. ),
  5732. array('validate' => 'only')
  5733. );
  5734. $this->assertFalse($result);
  5735. $result = $TestModel->saveAll(
  5736. array(
  5737. 'Article' => array('id' => 2),
  5738. 'Comment' => array(
  5739. array('id' => 1, 'comment' => '', 'published' => 'Y', 'user_id' => 1),
  5740. array('id' => 2, 'comment' => 'comment', 'published' => 'Y', 'user_id' => 1),
  5741. array('id' => 3, 'comment' => '', 'published' => 'Y', 'user_id' => 1),
  5742. )
  5743. ),
  5744. array('validate' => 'only', 'atomic' => false)
  5745. );
  5746. $expected = array('Article' => true, 'Comment' => array(false, true, false));
  5747. $this->assertIdentical($result, $expected);
  5748. $expected = array('Comment' => array(
  5749. 0 => array('comment' => 'This field cannot be left blank'),
  5750. 2 => array('comment' => 'This field cannot be left blank')
  5751. ));
  5752. $this->assertEqual($TestModel->validationErrors, $expected);
  5753. $expected = array(
  5754. 0 => array('comment' => 'This field cannot be left blank'),
  5755. 2 => array('comment' => 'This field cannot be left blank')
  5756. );
  5757. $this->assertEqual($TestModel->Comment->validationErrors, $expected);
  5758. }
  5759. /**
  5760. * testPkInHabtmLinkModel method
  5761. *
  5762. * @access public
  5763. * @return void
  5764. */
  5765. function testPkInHabtmLinkModel() {
  5766. //Test Nonconformant Models
  5767. $this->loadFixtures('Content', 'ContentAccount', 'Account');
  5768. $TestModel =& new Content();
  5769. $this->assertEqual($TestModel->ContentAccount->primaryKey, 'iContentAccountsId');
  5770. //test conformant models with no PK in the join table
  5771. $this->loadFixtures('Article', 'Tag');
  5772. $TestModel2 =& new Article();
  5773. $this->assertEqual($TestModel2->ArticlesTag->primaryKey, 'article_id');
  5774. //test conformant models with PK in join table
  5775. $this->loadFixtures('Item', 'Portfolio', 'ItemsPortfolio');
  5776. $TestModel3 =& new Portfolio();
  5777. $this->assertEqual($TestModel3->ItemsPortfolio->primaryKey, 'id');
  5778. //test conformant models with PK in join table - join table contains extra field
  5779. $this->loadFixtures('JoinA', 'JoinB', 'JoinAB');
  5780. $TestModel4 =& new JoinA();
  5781. $this->assertEqual($TestModel4->JoinAsJoinB->primaryKey, 'id');
  5782. }
  5783. /**
  5784. * testInsertAnotherHabtmRecordWithSameForeignKey method
  5785. *
  5786. * @access public
  5787. * @return void
  5788. */
  5789. function testInsertAnotherHabtmRecordWithSameForeignKey() {
  5790. $this->loadFixtures('JoinA', 'JoinB', 'JoinAB');
  5791. $TestModel = new JoinA();
  5792. $result = $TestModel->JoinAsJoinB->findById(1);
  5793. $expected = array('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'));
  5794. $this->assertEqual($result, $expected);
  5795. $TestModel->JoinAsJoinB->create();
  5796. $result = $TestModel->JoinAsJoinB->save(array('join_a_id' => 1, 'join_b_id' => 1, 'other' => 'Data for Join A 1 Join B 1', 'created' => '2008-01-03 10:56:44', 'updated' => '2008-01-03 10:56:44'));
  5797. $this->assertTrue($result);
  5798. $lastInsertId = $TestModel->JoinAsJoinB->getLastInsertID();
  5799. $this->assertTrue($lastInsertId != null);
  5800. $result = $TestModel->JoinAsJoinB->findById(1);
  5801. $expected = array('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'));
  5802. $this->assertEqual($result, $expected);
  5803. $updatedValue = 'UPDATED Data for Join A 1 Join B 2';
  5804. $TestModel->JoinAsJoinB->id = 1;
  5805. $result = $TestModel->JoinAsJoinB->saveField('other', $updatedValue, false);
  5806. $this->assertTrue($result);
  5807. $result = $TestModel->JoinAsJoinB->findById(1);
  5808. $this->assertEqual($result['JoinAsJoinB']['other'], $updatedValue);
  5809. }
  5810. /**
  5811. * Tests that $cacheSources can only be disabled in the db using model settings, not enabled
  5812. *
  5813. * @access public
  5814. * @return void
  5815. */
  5816. function testCacheSourcesDisabling() {
  5817. $this->db->cacheSources = true;
  5818. $TestModel = new JoinA();
  5819. $TestModel->cacheSources = false;
  5820. $TestModel->setSource('join_as');
  5821. $this->assertFalse($this->db->cacheSources);
  5822. $this->db->cacheSources = false;
  5823. $TestModel = new JoinA();
  5824. $TestModel->cacheSources = true;
  5825. $TestModel->setSource('join_as');
  5826. $this->assertFalse($this->db->cacheSources);
  5827. }
  5828. /**
  5829. * testDeleteHabtmReferenceWithConditions method
  5830. *
  5831. * @access public
  5832. * @return void
  5833. */
  5834. function testDeleteHabtmReferenceWithConditions() {
  5835. $this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio');
  5836. $Portfolio =& new Portfolio();
  5837. $Portfolio->hasAndBelongsToMany['Item']['conditions'] = array('ItemsPortfolio.item_id >' => 1);
  5838. $result = $Portfolio->find('first', array('conditions' => array('Portfolio.id' => 1)));
  5839. $expected = array(
  5840. array('id' => 3, 'syfile_id' => 3, 'published' => 0, 'name' => 'Item 3', 'ItemsPortfolio' => array('id' => 3, 'item_id' => 3, 'portfolio_id' => 1)),
  5841. array('id' => 4, 'syfile_id' => 4, 'published' => 0, 'name' => 'Item 4', 'ItemsPortfolio' => array('id' => 4, 'item_id' => 4, 'portfolio_id' => 1)),
  5842. array('id' => 5, 'syfile_id' => 5, 'published' => 0, 'name' => 'Item 5', 'ItemsPortfolio' => array('id' => 5, 'item_id' => 5, 'portfolio_id' => 1)),
  5843. );
  5844. $this->assertEqual($result['Item'], $expected);
  5845. $result = $Portfolio->ItemsPortfolio->find('all', array('conditions' => array('ItemsPortfolio.portfolio_id' => 1)));
  5846. $expected = array(
  5847. array('ItemsPortfolio' => array('id' => 1, 'item_id' => 1, 'portfolio_id' => 1)),
  5848. array('ItemsPortfolio' => array('id' => 3, 'item_id' => 3, 'portfolio_id' => 1)),
  5849. array('ItemsPortfolio' => array('id' => 4, 'item_id' => 4, 'portfolio_id' => 1)),
  5850. array('ItemsPortfolio' => array('id' => 5, 'item_id' => 5, 'portfolio_id' => 1))
  5851. );
  5852. $this->assertEqual($result, $expected);
  5853. $Portfolio->delete(1);
  5854. $result = $Portfolio->find('first', array('conditions' => array('Portfolio.id' => 1)));
  5855. $this->assertFalse($result);
  5856. $result = $Portfolio->ItemsPortfolio->find('all', array('conditions' => array('ItemsPortfolio.portfolio_id' => 1)));
  5857. $this->assertFalse($result);
  5858. }
  5859. /**
  5860. * testDeleteArticleBLinks method
  5861. *
  5862. * @access public
  5863. * @return void
  5864. */
  5865. function testDeleteArticleBLinks() {
  5866. $this->loadFixtures('Article', 'ArticlesTag', 'Tag');
  5867. $TestModel =& new ArticleB();
  5868. $result = $TestModel->ArticlesTag->find('all');
  5869. $expected = array(
  5870. array('ArticlesTag' => array('article_id' => '1', 'tag_id' => '1')),
  5871. array('ArticlesTag' => array('article_id' => '1', 'tag_id' => '2')),
  5872. array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '1')),
  5873. array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '3'))
  5874. );
  5875. $this->assertEqual($result, $expected);
  5876. $TestModel->delete(1);
  5877. $result = $TestModel->ArticlesTag->find('all');
  5878. $expected = array(
  5879. array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '1')),
  5880. array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '3'))
  5881. );
  5882. $this->assertEqual($result, $expected);
  5883. }
  5884. /**
  5885. * testPkInHAbtmLinkModelArticleB
  5886. *
  5887. * @access public
  5888. * @return void
  5889. */
  5890. function testPkInHabtmLinkModelArticleB() {
  5891. $this->loadFixtures('Article', 'Tag');
  5892. $TestModel2 =& new ArticleB();
  5893. $this->assertEqual($TestModel2->ArticlesTag->primaryKey, 'article_id');
  5894. }
  5895. }
  5896. ?>