From 4b2b8fcc1cc84963a6a0c9e4a118cf7c0744c477 Mon Sep 17 00:00:00 2001 From: "mariano.iglesias" Date: Sun, 1 Jun 2008 21:04:30 +0000 Subject: [PATCH] Adding tests to show HABTM saving with custom model keys, disproves and closes #4791 git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7082 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/tests/cases/libs/model/model.test.php | 39 +++++++++++++---- cake/tests/cases/libs/model/models.php | 26 +++++++++++- cake/tests/fixtures/stories_tag_fixture.php | 46 +++++++++++++++++++++ cake/tests/fixtures/story_fixture.php | 46 +++++++++++++++++++++ 4 files changed, 146 insertions(+), 11 deletions(-) create mode 100644 cake/tests/fixtures/stories_tag_fixture.php create mode 100644 cake/tests/fixtures/story_fixture.php diff --git a/cake/tests/cases/libs/model/model.test.php b/cake/tests/cases/libs/model/model.test.php index 76bee8810..5f3fb9de9 100644 --- a/cake/tests/cases/libs/model/model.test.php +++ b/cake/tests/cases/libs/model/model.test.php @@ -48,7 +48,8 @@ class ModelTest extends CakeTestCase { 'core.document', 'core.device', 'core.document_directory', 'core.primary_model', 'core.secondary_model', 'core.something', 'core.something_else', 'core.join_thing', 'core.join_a', 'core.join_b', 'core.join_c', 'core.join_a_b', 'core.join_a_c', 'core.uuid', 'core.data_test', 'core.posts_tag', 'core.the_paper_monkies', 'core.person', 'core.underscore_field', - 'core.node', 'core.dependency' + 'core.node', 'core.dependency', + 'core.story', 'core.stories_tag' ); function start() { @@ -1880,8 +1881,6 @@ class ModelTest extends CakeTestCase { ); $this->assertEqual($result, $expected); - // Save with parent model data - $data = array( 'Article' => array('id' => '2', 'title' => 'New Second Article'), 'Tag' => array('Tag' => array(1, 2)) @@ -1983,8 +1982,6 @@ class ModelTest extends CakeTestCase { ); $this->assertEqual($result, $expected); - // Parent data after HABTM data - $data = array('Tag' => array('Tag' => array(1, 2)), 'Article' => array('id' => '2', 'title' => 'New Second Article')); $this->assertTrue($TestModel->set($data)); $this->assertTrue($TestModel->save()); @@ -2094,6 +2091,34 @@ class ModelTest extends CakeTestCase { ); $this->assertEqual($result, $expected); } + + function testSaveHabtmCustomKeys() { + $this->loadFixtures('Story', 'StoriesTag', 'Tag'); + $Story =& new Story(); + + $data = array('Story' => array('story' => '1'), 'Tag' => array('Tag' => array(2, 3))); + $result = $Story->set($data); + $this->assertTrue($result); + + $result = $Story->save(); + $this->assertTrue($result); + + $result = $Story->find('all'); + $expected = array( + array( + 'Story' => array('story' => 1, 'title' => 'First Story'), + 'Tag' => array( + array('id' => 2, 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31'), + array('id' => 3, 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31') + ) + ), + array( + 'Story' => array('story' => 2, 'title' => 'Second Story'), + 'Tag' => array() + ) + ); + $this->assertEqual($result, $expected); + } /** * @todo This is technically incorrect (ThePaperMonkies.apple_id should be ThePaperMonkies.the_paper_id), @@ -3685,10 +3710,6 @@ class ModelTest extends CakeTestCase { $this->assertEqual($result, $expected); } - function testAfterFindAssociation() { - - } - function testDeconstructFields() { $this->loadFixtures('Apple'); $TestModel =& new Apple(); diff --git a/cake/tests/cases/libs/model/models.php b/cake/tests/cases/libs/model/models.php index 809dbfbbb..5e73a0a5f 100644 --- a/cake/tests/cases/libs/model/models.php +++ b/cake/tests/cases/libs/model/models.php @@ -662,12 +662,34 @@ class Person extends CakeTestModel { 'className' => 'Person', 'foreignKey' => 'father_id')); } - +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ class UnderscoreField extends CakeTestModel { var $name = 'UnderscoreField'; } - +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ class Product extends CakeTestModel { var $name = 'Product'; } +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Story extends CakeTestModel { + var $name = 'Story'; + var $primaryKey = 'story'; + var $hasAndBelongsToMany = array('Tag' => array('foreignKey' => 'story')); + var $validate = array('title' => VALID_NOT_EMPTY); +} ?> \ No newline at end of file diff --git a/cake/tests/fixtures/stories_tag_fixture.php b/cake/tests/fixtures/stories_tag_fixture.php new file mode 100644 index 000000000..541d57da5 --- /dev/null +++ b/cake/tests/fixtures/stories_tag_fixture.php @@ -0,0 +1,46 @@ + + * Copyright 2005-2008, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The Open Group Test Suite License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake.tests + * @subpackage cake.tests.fixtures + * @since CakePHP(tm) v 1.2.0.4667 + * @version $Revision$ + * @modifiedby $LastChangedBy$ + * @lastmodified $Date$ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.fixtures + */ +class StoriesTagFixture extends CakeTestFixture { + var $name = 'StoriesTag'; + var $fields = array( + 'story' => array('type' => 'integer', 'null' => false), + 'tag_id' => array('type' => 'integer', 'null' => false), + 'indexes' => array('UNIQUE_TAG' => array('column'=> array('story', 'tag_id'), 'unique'=>1)) + ); + var $records = array( + array('story' => 1, 'tag_id' => 1) + ); +} +?> \ No newline at end of file diff --git a/cake/tests/fixtures/story_fixture.php b/cake/tests/fixtures/story_fixture.php new file mode 100644 index 000000000..6144930a8 --- /dev/null +++ b/cake/tests/fixtures/story_fixture.php @@ -0,0 +1,46 @@ + + * Copyright 2005-2008, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The Open Group Test Suite License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake.tests + * @subpackage cake.tests.fixtures + * @since CakePHP(tm) v 1.2.0.4667 + * @version $Revision$ + * @modifiedby $LastChangedBy$ + * @lastmodified $Date$ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.fixtures + */ +class StoryFixture extends CakeTestFixture { + var $name = 'Story'; + var $fields = array( + 'story' => array('type' => 'integer', 'key' => 'primary'), + 'title' => array('type' => 'string', 'null' => false) + ); + var $records = array( + array('title' => 'First Story'), + array('title' => 'Second Story') + ); +} +?> \ No newline at end of file