/cake/tests/cases/libs/model/model.test.php
PHP | 6524 lines | 5039 code | 628 blank | 857 comment | 51 complexity | 8267a8c60898d113179a90770fcd8b4a MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- <?php
- /* SVN FILE: $Id$ */
- /**
- * ModelTest file
- *
- * Long description for file
- *
- * PHP versions 4 and 5
- *
- * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
- * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
- *
- * 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. (http://www.cakefoundation.org)
- * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
- * @package cake
- * @subpackage cake.tests.cases.libs.model
- * @since CakePHP(tm) v 1.2.0.4206
- * @version $Revision$
- * @modifiedby $LastChangedBy$
- * @lastmodified $Date$
- * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
- */
- App::import('Core', array('AppModel', 'Model'));
- require_once dirname(__FILE__) . DS . 'models.php';
- /**
- * ModelTest
- *
- * @package cake
- * @subpackage cake.tests.cases.libs.model
- */
- class ModelTest extends CakeTestCase {
- /**
- * autoFixtures property
- *
- * @var bool false
- * @access public
- */
- var $autoFixtures = false;
- /**
- * fixtures property
- *
- * @var array
- * @access public
- */
- var $fixtures = array(
- 'core.category', 'core.category_thread', 'core.user', 'core.my_category', 'core.my_product',
- 'core.my_user', 'core.my_categories_my_users', 'core.my_categories_my_products',
- 'core.article', 'core.featured', 'core.article_featureds_tags', 'core.article_featured',
- 'core.articles', 'core.numeric_article', 'core.tag', 'core.articles_tag', 'core.comment',
- 'core.attachment', 'core.apple', 'core.sample', 'core.another_article', 'core.item',
- 'core.advertisement', 'core.home', 'core.post', 'core.author', 'core.bid', 'core.portfolio',
- 'core.product', 'core.project', 'core.thread', 'core.message', 'core.items_portfolio',
- 'core.syfile', 'core.image', 'core.device_type', 'core.device_type_category',
- 'core.feature_set', 'core.exterior_type_category', '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.story', 'core.stories_tag', 'core.cd', 'core.book', 'core.basket',
- 'core.overall_favorite', 'core.account', 'core.content', 'core.content_account',
- 'core.film_file', 'core.test_plugin_article', 'core.test_plugin_comment', 'core.uuiditem',
- 'core.counter_cache_user', 'core.counter_cache_post', 'core.uuidportfolio',
- 'core.uuiditems_uuidportfolio', 'core.uuiditems_uuidportfolio_numericid',
- 'core.fruit', 'core.fruits_uuid_tag', 'core.uuid_tag'
- );
- /**
- * start method
- *
- * @access public
- * @return void
- */
- function start() {
- parent::start();
- $this->debug = Configure::read('debug');
- Configure::write('debug', 2);
- }
- /**
- * end method
- *
- * @access public
- * @return void
- */
- function end() {
- parent::end();
- Configure::write('debug', $this->debug);
- }
- /**
- * endTest method
- *
- * @access public
- * @return void
- */
- function endTest() {
- ClassRegistry::flush();
- }
- /**
- * testAutoConstructAssociations method
- *
- * @access public
- * @return void
- */
- function testAutoConstructAssociations() {
- $this->loadFixtures('User', 'ArticleFeatured');
- $TestModel =& new AssociationTest1();
- $result = $TestModel->hasAndBelongsToMany;
- $expected = array('AssociationTest2' => array(
- 'unique' => false,
- 'joinTable' => 'join_as_join_bs',
- 'foreignKey' => false,
- 'className' => 'AssociationTest2',
- 'with' => 'JoinAsJoinB',
- 'associationForeignKey' => 'join_b_id',
- 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '',
- 'finderQuery' => '', 'deleteQuery' => '', 'insertQuery' => ''
- ));
- $this->assertEqual($result, $expected);
- // Tests related to ticket https://trac.cakephp.org/ticket/5594
- $TestModel =& new ArticleFeatured();
- $TestFakeModel =& new ArticleFeatured(array('table' => false));
- $expected = array(
- 'User' => array(
- 'className' => 'User', 'foreignKey' => 'user_id',
- 'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => ''
- ),
- 'Category' => array(
- 'className' => 'Category', 'foreignKey' => 'category_id',
- 'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => ''
- )
- );
- $this->assertIdentical($TestModel->belongsTo, $expected);
- $this->assertIdentical($TestFakeModel->belongsTo, $expected);
- $this->assertEqual($TestModel->User->name, 'User');
- $this->assertEqual($TestFakeModel->User->name, 'User');
- $this->assertEqual($TestModel->Category->name, 'Category');
- $this->assertEqual($TestFakeModel->Category->name, 'Category');
- $expected = array(
- 'Featured' => array(
- 'className' => 'Featured', 'foreignKey' => 'article_featured_id',
- 'conditions' => '', 'fields' => '', 'order' => '', 'dependent' => ''
- )
- );
- $this->assertIdentical($TestModel->hasOne, $expected);
- $this->assertIdentical($TestFakeModel->hasOne, $expected);
- $this->assertEqual($TestModel->Featured->name, 'Featured');
- $this->assertEqual($TestFakeModel->Featured->name, 'Featured');
- $expected = array(
- 'Comment' => array(
- 'className' => 'Comment', 'dependent' => true,
- 'foreignKey' => 'article_featured_id', 'conditions' => '', 'fields' => '',
- 'order' => '', 'limit' => '', 'offset' => '', 'exclusive' => '',
- 'finderQuery' => '', 'counterQuery' => ''
- )
- );
- $this->assertIdentical($TestModel->hasMany, $expected);
- $this->assertIdentical($TestFakeModel->hasMany, $expected);
- $this->assertEqual($TestModel->Comment->name, 'Comment');
- $this->assertEqual($TestFakeModel->Comment->name, 'Comment');
- $expected = array(
- 'Tag' => array(
- 'className' => 'Tag', 'joinTable' => 'article_featureds_tags',
- 'with' => 'ArticleFeaturedsTag', 'foreignKey' => 'article_featured_id',
- 'associationForeignKey' => 'tag_id', 'conditions' => '', 'fields' => '',
- 'order' => '', 'limit' => '', 'offset' => '', 'unique' => true, 'finderQuery' => '',
- 'deleteQuery' => '', 'insertQuery' => ''
- )
- );
- $this->assertIdentical($TestModel->hasAndBelongsToMany, $expected);
- $this->assertIdentical($TestFakeModel->hasAndBelongsToMany, $expected);
- $this->assertEqual($TestModel->Tag->name, 'Tag');
- $this->assertEqual($TestFakeModel->Tag->name, 'Tag');
- }
- /**
- * test Model::__construct
- *
- * ensure that $actsAS and $_findMethods are merged.
- *
- * @return void
- **/
- function testConstruct() {
- $this->loadFixtures('Post', 'Comment');
- $TestModel =& ClassRegistry::init('MergeVarPluginPost');
- $this->assertEqual($TestModel->actsAs, array('Containable', 'Tree'));
- $this->assertTrue(isset($TestModel->Behaviors->Containable));
- $this->assertTrue(isset($TestModel->Behaviors->Tree));
- $TestModel =& ClassRegistry::init('MergeVarPluginComment');
- $expected = array('Containable', 'Containable' => array('some_settings'));
- $this->assertEqual($TestModel->actsAs, $expected);
- $this->assertTrue(isset($TestModel->Behaviors->Containable));
- }
- /**
- * testColumnTypeFetching method
- *
- * @access public
- * @return void
- */
- function testColumnTypeFetching() {
- $model =& new Test();
- $this->assertEqual($model->getColumnType('id'), 'integer');
- $this->assertEqual($model->getColumnType('notes'), 'text');
- $this->assertEqual($model->getColumnType('updated'), 'datetime');
- $this->assertEqual($model->getColumnType('unknown'), null);
- $model =& new Article();
- $this->assertEqual($model->getColumnType('User.created'), 'datetime');
- $this->assertEqual($model->getColumnType('Tag.id'), 'integer');
- $this->assertEqual($model->getColumnType('Article.id'), 'integer');
- }
- /**
- * testMultipleBelongsToWithSameClass method
- *
- * @access public
- * @return void
- */
- function testMultipleBelongsToWithSameClass() {
- $this->loadFixtures(
- 'DeviceType', 'DeviceTypeCategory', 'FeatureSet', 'ExteriorTypeCategory',
- 'Document', 'Device', 'DocumentDirectory'
- );
- $DeviceType =& new DeviceType();
- $DeviceType->recursive = 2;
- $result = $DeviceType->read(null, 1);
- $expected = array(
- 'DeviceType' => array(
- 'id' => 1, 'device_type_category_id' => 1, 'feature_set_id' => 1,
- 'exterior_type_category_id' => 1, 'image_id' => 1, 'extra1_id' => 1,
- 'extra2_id' => 1, 'name' => 'DeviceType 1', 'order' => 0
- ),
- 'Image' => array('id' => 1, 'document_directory_id' => 1, 'name' => 'Document 1',
- 'DocumentDirectory' => array('id' => 1, 'name' => 'DocumentDirectory 1')),
- 'Extra1' => array(
- 'id' => 1, 'document_directory_id' => 1, 'name' => 'Document 1',
- 'DocumentDirectory' => array('id' => 1, 'name' => 'DocumentDirectory 1')
- ),
- 'Extra2' => array(
- 'id' => 1, 'document_directory_id' => 1, 'name' => 'Document 1',
- 'DocumentDirectory' => array('id' => 1, 'name' => 'DocumentDirectory 1')
- ),
- 'DeviceTypeCategory' => array('id' => 1, 'name' => 'DeviceTypeCategory 1'),
- 'FeatureSet' => array('id' => 1, 'name' => 'FeatureSet 1'),
- 'ExteriorTypeCategory' => array(
- 'id' => 1, 'image_id' => 1, 'name' => 'ExteriorTypeCategory 1',
- 'Image' => array('id' => 1, 'device_type_id' => 1, 'name' => 'Device 1', 'typ' => 1)
- ),
- 'Device' => array(
- array('id' => 1, 'device_type_id' => 1, 'name' => 'Device 1', 'typ' => 1),
- array('id' => 2, 'device_type_id' => 1, 'name' => 'Device 2', 'typ' => 1),
- array('id' => 3, 'device_type_id' => 1, 'name' => 'Device 3', 'typ' => 2)
- )
- );
- $this->assertEqual($result, $expected);
- }
- /**
- * testHabtmRecursiveBelongsTo method
- *
- * @access public
- * @return void
- */
- function testHabtmRecursiveBelongsTo() {
- $this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio', 'Syfile', 'Image');
- $Portfolio =& new Portfolio();
- $result = $Portfolio->find(array('id' => 2), null, null, 3);
- $expected = array('Portfolio' => array(
- 'id' => 2, 'seller_id' => 1, 'name' => 'Portfolio 2'),
- 'Item' => array(
- array(
- 'id' => 2, 'syfile_id' => 2, 'published' => 0, 'name' => 'Item 2',
- 'ItemsPortfolio' => array('id' => 2, 'item_id' => 2, 'portfolio_id' => 2),
- 'Syfile' => array(
- 'id' => 2, 'image_id' => 2, 'name' => 'Syfile 2', 'item_count' => null,
- 'Image' => array('id' => 2, 'name' => 'Image 2'
- ))
- ),
- array(
- 'id' => 6, 'syfile_id' => 6, 'published' => 0, 'name' => 'Item 6',
- 'ItemsPortfolio' => array('id' => 6, 'item_id' => 6, 'portfolio_id' => 2),
- 'Syfile' => array(
- 'id' => 6, 'image_id' => null, 'name' => 'Syfile 6', 'item_count' => null,
- 'Image' => array()
- )
- )
- ));
- $this->assertEqual($result, $expected);
- }
- /**
- * testHabtmUuidWithUuidId method
- *
- * @access public
- * @return void
- */
- function testHabtmUuidWithUuidId() {
- $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolio');
- $TestModel =& new Uuidportfolio();
- $data = array('Uuidportfolio' => array('name' => 'Portfolio 3'));
- $data['Uuiditem']['Uuiditem'] = array('483798c8-c7cc-430e-8cf9-4fcc40cf8569');
- $TestModel->create($data);
- $TestModel->save();
- $id = $TestModel->id;
- $result = $TestModel->read(null, $id);
- $this->assertEqual(1, count($result['Uuiditem']));
- $this->assertEqual(strlen($result['Uuiditem'][0]['UuiditemsUuidportfolio']['id']), 36);
- }
- /**
- * test HABTM saving when join table has no primary key and only 2 columns.
- *
- * @return void
- **/
- function testHabtmSavingWithNoPrimaryKeyUuidJoinTable() {
- $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
- $Fruit =& new Fruit();
- $data = array(
- 'Fruit' => array(
- 'color' => 'Red',
- 'shape' => 'Heart-shaped',
- 'taste' => 'sweet',
- 'name' => 'Strawberry',
- ),
- 'UuidTag' => array(
- 'UuidTag' => array(
- '481fc6d0-b920-43e0-e50f-6d1740cf8569'
- )
- )
- );
- $this->assertTrue($Fruit->save($data));
- }
- /**
- * test HABTM saving when join table has no primary key and only 2 columns, no with model is used.
- *
- * @return void
- **/
- function testHabtmSavingWithNoPrimaryKeyUuidJoinTableNoWith() {
- $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
- $Fruit =& new FruitNoWith();
- $data = array(
- 'Fruit' => array(
- 'color' => 'Red',
- 'shape' => 'Heart-shaped',
- 'taste' => 'sweet',
- 'name' => 'Strawberry',
- ),
- 'UuidTag' => array(
- 'UuidTag' => array(
- '481fc6d0-b920-43e0-e50f-6d1740cf8569'
- )
- )
- );
- $this->assertTrue($Fruit->save($data));
- }
- /**
- * testHabtmUuidWithNumericId method
- *
- * @access public
- * @return void
- */
- function testHabtmUuidWithNumericId() {
- $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolioNumericid');
- $TestModel =& new Uuiditem();
- $data = array('Uuiditem' => array('name' => 'Item 7', 'published' => 0));
- $data['Uuidportfolio']['Uuidportfolio'] = array('480af662-eb8c-47d3-886b-230540cf8569');
- $TestModel->create($data);
- $TestModel->save();
- $id = $TestModel->id;
- $result = $TestModel->read(null, $id);
- $this->assertEqual(1, count($result['Uuidportfolio']));
- }
- /**
- * testHabtmFinderQuery method
- *
- * @access public
- * @return void
- */
- function testHabtmFinderQuery() {
- $this->loadFixtures('Article', 'Tag', 'ArticlesTag');
- $Article =& new Article();
- $sql = $this->db->buildStatement(
- array(
- 'fields' => $this->db->fields($Article->Tag, null, array(
- 'Tag.id', 'Tag.tag', 'ArticlesTag.article_id', 'ArticlesTag.tag_id'
- )),
- 'table' => $this->db->fullTableName('tags'),
- 'alias' => 'Tag',
- 'limit' => null,
- 'offset' => null,
- 'group' => null,
- 'joins' => array(array(
- 'alias' => 'ArticlesTag',
- 'table' => $this->db->fullTableName('articles_tags'),
- 'conditions' => array(
- array("ArticlesTag.article_id" => '{$__cakeID__$}'),
- array("ArticlesTag.tag_id" => $this->db->identifier('Tag.id'))
- )
- )),
- 'conditions' => array(),
- 'order' => null
- ),
- $Article
- );
- $Article->hasAndBelongsToMany['Tag']['finderQuery'] = $sql;
- $result = $Article->find('first');
- $expected = array(array('id' => '1', 'tag' => 'tag1'), array('id' => '2', 'tag' => 'tag2'));
- $this->assertEqual($result['Tag'], $expected);
- }
- /**
- * testHabtmLimitOptimization method
- *
- * @access public
- * @return void
- */
- function testHabtmLimitOptimization() {
- $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
- $TestModel =& new Article();
- $TestModel->hasAndBelongsToMany['Tag']['limit'] = 2;
- $result = $TestModel->read(null, 2);
- $expected = array(
- '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'),
- 'User' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31'),
- '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'),
- 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')
- ),
- 'Tag' => array(
- array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
- array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')
- )
- );
- $this->assertEqual($result, $expected);
- $TestModel->hasAndBelongsToMany['Tag']['limit'] = 1;
- $result = $TestModel->read(null, 2);
- unset($expected['Tag'][1]);
- $this->assertEqual($result, $expected);
- }
- /**
- * testHabtmUniqueKey method
- *
- * @access public
- * @return void
- */
- function testHabtmUniqueKey() {
- $model =& new Item();
- $this->assertFalse($model->hasAndBelongsToMany['Portfolio']['unique']);
- }
- /**
- * testHasManyLimitOptimization method
- *
- * @access public
- * @return void
- */
- function testHasManyLimitOptimization() {
- $this->loadFixtures('Project', 'Thread', 'Message', 'Bid');
- $Project =& new Project();
- $Project->recursive = 3;
- $result = $Project->find('all');
- $expected = array(
- array('Project' => array('id' => 1, 'name' => 'Project 1'),
- 'Thread' => array(
- array(
- 'id' => 1, 'project_id' => 1, 'name' => 'Project 1, Thread 1',
- 'Project' => array(
- 'id' => 1, 'name' => 'Project 1',
- 'Thread' => array(
- array('id' => 1, 'project_id' => 1, 'name' => 'Project 1, Thread 1'),
- array('id' => 2, 'project_id' => 1, 'name' => 'Project 1, Thread 2')
- )
- ),
- 'Message' => array(
- array(
- 'id' => 1, 'thread_id' => 1, 'name' => 'Thread 1, Message 1',
- 'Bid' => array('id' => 1, 'message_id' => 1, 'name' => 'Bid 1.1')
- )
- )
- ),
- array(
- 'id' => 2, 'project_id' => 1, 'name' => 'Project 1, Thread 2',
- 'Project' => array(
- 'id' => 1, 'name' => 'Project 1',
- 'Thread' => array(
- array('id' => 1, 'project_id' => 1, 'name' => 'Project 1, Thread 1'),
- array('id' => 2, 'project_id' => 1, 'name' => 'Project 1, Thread 2')
- )
- ),
- 'Message' => array(
- array(
- 'id' => 2, 'thread_id' => 2, 'name' => 'Thread 2, Message 1',
- 'Bid' => array('id' => 4, 'message_id' => 2, 'name' => 'Bid 2.1')
- )
- )
- )
- )
- ),
- array('Project' => array('id' => 2, 'name' => 'Project 2'),
- 'Thread' => array(
- array(
- 'id' => 3, 'project_id' => 2, 'name' => 'Project 2, Thread 1',
- 'Project' => array(
- 'id' => 2, 'name' => 'Project 2',
- 'Thread' => array(
- array('id' => 3, 'project_id' => 2, 'name' => 'Project 2, Thread 1'),
- )
- ),
- 'Message' => array(
- array(
- 'id' => 3, 'thread_id' => 3, 'name' => 'Thread 3, Message 1',
- 'Bid' => array('id' => 3, 'message_id' => 3, 'name' => 'Bid 3.1')
- )
- )
- )
- )
- ),
- array('Project' => array('id' => 3, 'name' => 'Project 3'),
- 'Thread' => array()
- )
- );
- $this->assertEqual($result, $expected);
- }
- /**
- * testWithAssociation method
- *
- * @access public
- * @return void
- */
- function testWithAssociation() {
- $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
- $TestModel =& new Something();
- $result = $TestModel->SomethingElse->find('all');
- $expected = array(
- 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'),
- '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',
- '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')))),
- 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'),
- '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',
- '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')))),
- 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'),
- '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',
- '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')))));
- $this->assertEqual($result, $expected);
- $result = $TestModel->find('all');
- $expected = array(
- 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'),
- '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',
- 'JoinThing' => array('doomed' => '1', 'something_id' => '1', 'something_else_id' => '2')))),
- 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'),
- 'SomethingElse' => 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',
- 'JoinThing' => array('doomed' => '0', 'something_id' => '2', 'something_else_id' => '3')))),
- 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'),
- 'SomethingElse' => 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',
- 'JoinThing' => array('doomed' => '1', 'something_id' => '3', 'something_else_id' => '1')))));
- $this->assertEqual($result, $expected);
- $result = $TestModel->findById(1);
- $expected = 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'),
- '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',
- 'JoinThing' => array('doomed' => '1', 'something_id' => '1', 'something_else_id' => '2'))));
- $this->assertEqual($result, $expected);
- $expected = $TestModel->findById(1);
- $TestModel->set($expected);
- $TestModel->save();
- $result = $TestModel->findById(1);
- $this->assertEqual($result, $expected);
- $TestModel->hasAndBelongsToMany['SomethingElse']['unique'] = false;
- $TestModel->create(array(
- 'Something' => array('id' => 1),
- 'SomethingElse' => array(3, array('something_else_id' => 1, 'doomed' => '1'))
- ));
- $ts = date('Y-m-d H:i:s');
- $TestModel->save();
- $TestModel->hasAndBelongsToMany['SomethingElse']['order'] = 'SomethingElse.id ASC';
- $result = $TestModel->findById(1);
- $expected = array(
- 'Something' => array('id' => '1', 'title' => 'First Post', 'body' => 'First Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => $ts),
- 'SomethingElse' => 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',
- 'JoinThing' => array('doomed' => '1', 'something_id' => '1', 'something_else_id' => '1')),
- 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',
- 'JoinThing' => array('doomed' => '1', 'something_id' => '1', 'something_else_id' => '2')),
- 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',
- 'JoinThing' => array('doomed' => '0', 'something_id' => '1', 'something_else_id' => '3'))));
- $this->assertEqual($result, $expected);
- }
- /**
- * testDynamicAssociations method
- *
- * @access public
- * @return void
- */
- function testDynamicAssociations() {
- $this->loadFixtures('Article', 'Comment');
- $TestModel =& new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->hasOne = array();
- $TestModel->hasMany['Comment'] = array_merge($TestModel->hasMany['Comment'], array(
- 'foreignKey' => false,
- 'conditions' => array('Comment.user_id =' => '2')
- ));
- $result = $TestModel->find('all');
- $expected = array(
- array(
- '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'),
- 'Comment' => array(
- 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'),
- 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')
- )
- ),
- array(
- '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'),
- 'Comment' => array(
- 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'),
- 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')
- )
- ),
- array(
- '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'),
- 'Comment' => array(
- 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'),
- 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')
- )
- )
- );
- $this->assertEqual($result, $expected);
- }
- /**
- * testSaveMultipleHabtm method
- *
- * @access public
- * @return void
- */
- function testSaveMultipleHabtm() {
- $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC');
- $TestModel = new JoinA();
- $result = $TestModel->findById(1);
- $expected = array(
- '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'),
- 'JoinB' => array(
- 0 => array('id' => 2, 'name' => 'Join B 2', 'created' => '2008-01-03 10:55:02', 'updated' => '2008-01-03 10:55:02',
- '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'))),
- 'JoinC' => array(
- 0 => array('id' => 2, 'name' => 'Join C 2', 'created' => '2008-01-03 10:56:12', 'updated' => '2008-01-03 10:56:12',
- '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'))));
- $this->assertEqual($result, $expected);
- $ts = date('Y-m-d H:i:s');
- $TestModel->id = 1;
- $data = array(
- 'JoinA' => array('id' => '1', 'name' => 'New name for Join A 1', 'updated' => $ts),
- 'JoinB' => array(array('id' => 1, 'join_b_id' => 2, 'other' => 'New data for Join A 1 Join B 2', 'created' => $ts, 'updated' => $ts)),
- 'JoinC' => array(array('id' => 1, 'join_c_id' => 2, 'other' => 'New data for Join A 1 Join C 2', 'created' => $ts, 'updated' => $ts)));
- $TestModel->set($data);
- $TestModel->save();
- $result = $TestModel->findById(1);
- $expected = array(
- '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),
- 'JoinB' => array(
- 0 => array('id' => 2, 'name' => 'Join B 2', 'created' => '2008-01-03 10:55:02', 'updated' => '2008-01-03 10:55:02',
- '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))),
- 'JoinC' => array(
- 0 => array('id' => 2, 'name' => 'Join C 2', 'created' => '2008-01-03 10:56:12', 'updated' => '2008-01-03 10:56:12',
- '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))));
- $this->assertEqual($result, $expected);
- }
- /**
- * testFindAllRecursiveSelfJoin method
- *
- * @access public
- * @return void
- */
- function testFindAllRecursiveSelfJoin() {
- $this->loadFixtures('Home', 'AnotherArticle', 'Advertisement');
- $TestModel =& new Home();
- $TestModel->recursive = 2;
- $result = $TestModel->find('all');
- $expected = array(array('Home' => 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'),
- 'AnotherArticle' => array('id' => '1', 'title' => 'First Article', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31',
- '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'))),
- 'Advertisement' => array('id' => '1', 'title' => 'First Ad', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31',
- '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'),
- 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')))),
- array('Home' => 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'),
- 'AnotherArticle' => array('id' => '3', 'title' => 'Third Article', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31',
- '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'))),
- 'Advertisement' => array('id' => '1', 'title' => 'First Ad', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31',
- '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'),
- 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')))));
- $this->assertEqual($result, $expected);
- }
- /**
- * testFindAllRecursiveWithHabtm method
- *
- * @return void
- * @access public
- */
- function testFindAllRecursiveWithHabtm() {
- $this->loadFixtures(
- 'MyCategoriesMyUsers', 'MyCategoriesMyProducts', 'MyCategory', 'MyUser', 'MyProduct'
- );
- $MyUser =& new MyUser();
- $MyUser->recursive = 2;
- $result = $MyUser->find('all');
- $expected = array(
- array(
- 'MyUser' => array('id' => '1', 'firstname' => 'userA'),
- 'MyCategory' => array(
- array(
- 'id' => '1',
- 'name' => 'A',
- 'MyProduct' => array(
- array('id' => '1', 'name' => 'book')
- )
- ),
- array(
- 'id' => '3',
- 'name' => 'C',
- 'MyProduct' => array(array('id' => '2', 'name' => 'computer'))
- )
- )
- ),
- array(
- 'MyUser' => array('id' => '2', 'firstname' => 'userB'),
- 'MyCategory' => array(
- array(
- 'id' => '1',
- 'name' => 'A',
- 'MyProduct' => array(array('id' => '1', 'name' => 'book'))
- ),
- array(
- 'id' => '2',
- 'name' => 'B',
- 'MyProduct' => array(
- array('id' => '1', 'name' => 'book'),
- array('id' => '2', 'name' => 'computer')
- )
- )
- )
- )
- );
- $this->assertIdentical($result, $expected);
- }
- /**
- * testFindSelfAssociations method
- *
- * @access public
- * @return void
- */
- function testFindSelfAssociations() {
- $this->loadFixtures('Person');
- $TestModel =& new Person();
- $TestModel->recursive = 2;
- $result = $TestModel->read(null, 1);
- $expected = array(
- 'Person' => array('id' => 1, 'name' => 'person', 'mother_id' => 2, 'father_id' => 3),
- 'Mother' => array('id' => 2, 'name' => 'mother', 'mother_id' => 4, 'father_id' => 5,
- 'Mother' => array('id' => 4, 'name' => 'mother - grand mother', 'mother_id' => 0, 'father_id' => 0),
- 'Father' => array('id' => 5, 'name' => 'mother - grand father', 'mother_id' => 0, 'father_id' => 0)),
- 'Father' => array('id' => 3, 'name' => 'father', 'mother_id' => 6, 'father_id' => 7,
- 'Father' => array('id' => 7, 'name' => 'father - grand father', 'mother_id' => 0, 'father_id' => 0),
- 'Mother' => array('id' => 6, 'name' => 'father - grand mother', 'mother_id' => 0, 'father_id' => 0)));
- $this->assertEqual($result, $expected);
- $TestModel->recursive = 3;
- $result = $TestModel->read(null, 1);
- $expected = array(
- 'Person' => array('id' => 1, 'name' => 'person', 'mother_id' => 2, 'father_id' => 3),
- 'Mother' => array('id' => 2, 'name' => 'mother', 'mother_id' => 4, 'father_id' => 5,
- 'Mother' => array('id' => 4, 'name' => 'mother - grand mother', 'mother_id' => 0, 'father_id' => 0,
- 'Mother' => array(),
- 'Father' => array()),
- 'Father' => array('id' => 5, 'name' => 'mother - grand father', 'mother_id' => 0, 'father_id' => 0,
- 'Father' => array(),
- 'Mother' => array())),
- 'Father' => array('id' => 3, 'name' => 'father', 'mother_id' => 6, 'father_id' => 7,
- 'Father' => array('id' => 7, 'name' => 'father - grand father', 'mother_id' => 0, 'father_id' => 0,
- 'Father' => array(),
- 'Mother' => array()),
- 'Mother' => array('id' => 6, 'name' => 'father - grand mother', 'mother_id' => 0, 'father_id' => 0,
- 'Mother' => array(),
- 'Father' => array())));
- $this->assertEqual($result, $expected);
- }
- /**
- * testPluginAssociations method
- *
- * @access public
- * @return void
- */
- function testPluginAssociations() {
- $this->loadFixtures('TestPluginArticle', 'User', 'TestPluginComment');
- $TestModel =& new TestPluginArticle();
- $result = $TestModel->find('all');
- $expected = array(
- array(
- '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'),
- 'User' => array('id' => 1, 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
- 'TestPluginComment' => array(
- 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'),
- 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'),
- 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'),
- 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')
- )
- ),
- array(
- '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'),
- 'User' => array('id' => 3, 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31'),
- 'TestPluginComment' => array(
- 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'),
- 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')
- )
- ),
- array(
- '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'),
- 'User' => array('id' => 1, 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
- 'TestPluginComment' => array()
- )
- );
- $this->assertEqual($result, $expected);
- }
- /**
- * testIdentity method
- *
- * @access public
- * @return void
- */
- function testIdentity() {
- $TestModel =& new Test();
- $result = $TestModel->alias;
- $expected = 'Test';
- $this->assertEqual($result, $expected);
- $TestModel =& new TestAlias();
- $result = $TestModel->alias;
- $expected = 'TestAlias';
- $this->assertEqual($result, $expected);
- $TestModel =& new Test(array('alias' => 'AnotherTest'));
- $result = $TestModel->alias;
- $expected = 'AnotherTest';
- $this->assertEqual($result, $expected);
- }
- /**
- * testCreation method
- *
- * @access public
- * @return void
- */
- function testCreation() {
- $this->loadFixtures('Article');
- $TestModel =& new Test();
- $result = $TestModel->create();
- $expected = array('Test' => array('notes' => 'write some notes here'));
- $this->assertEqual($result, $expected);
- $TestModel =& new User();
- $result = $TestModel->schema();
- if (isset($this->db->columns['primary_key']['length'])) {
- $intLength = $this->db->columns['primary_key']['length'];
- } elseif (isset($this->db->columns['integer']['length'])) {
- $intLength = $this->db->columns['integer']['length'];
- } else {
- $intLength = 11;
- }
- foreach (array('collate', 'charset') as $type) {
- unset($result['user'][$type]);
- unset($result['password'][$type]);
- }
- $expected = array(
- 'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => $intLength, 'key' => 'primary'),
- 'user' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => 255),
- 'password' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => 255),
- 'created' => array('type' => 'datetime', 'null' => true, 'default' => null, 'length' => null),
- 'updated'=> array('type' => 'datetime', 'null' => true, 'default' => null, 'length' => null));
- $this->assertEqual($result, $expected);
- $TestModel =& new Article();
- $result = $TestModel->create();
- $expected = array('Article' => array('published' => 'N'));
- $this->assertEqual($result, $expected);
- $FeaturedModel =& new Featured();
- $data = array(
- 'article_featured_id' => 1, 'category_id' => 1,
- 'published_date' => array('year' => 2008, 'month' => 06, 'day' => 11),
- 'end_date' => array('year' => 2008, 'month' => 06, 'day' => 20)
- );
- $expected = array('Featured' => array(
- 'article_featured_id' => 1, 'category_id' => 1,
- 'published_date' => '2008-6-11 00:00:00', 'end_date' => '2008-6-20 00:00:00'
- ));
- $this->assertEqual($FeaturedModel->create($data), $expected);
- $data = array(
- 'published_date' => array('year' => 2008, 'month' => 06, 'day' => 11),
- 'end_date' => array('year' => 2008, 'month' => 06, 'day' => 20),
- 'article_featured_id' => 1, 'category_id' => 1
- );
- $expected = array('Featured' => array(
- 'published_date' => '2008-6-11 00:00:00', 'end_date' => '2008-6-20 00:00:00',
- 'article_featured_id' => 1, 'category_id' => 1
- ));
- $this->assertEqual($FeaturedModel->create($data), $expected);
- }
- /**
- * ensure that __exists is reset on create
- *
- * @return void
- **/
- function testResetOfExistsOnCreate() {
- $this->loadFixtures('Article');
- $Article =& new Article();
- $Article->id = 1;
- $Article->saveField('title', 'Reset me');
- $Article->delete();
- $Article->id = 1;
- $this->assertFalse($Article->exists());
- $Article->create();
- $this->assertFalse($Article->exists());
- $Article->id = 2;
- $Article->saveField('title', 'Staying alive');
- $result = $Article->read(null, 2);
- $this->assertEqual($result['Article']['title'], 'Staying alive');
- }
- /**
- * testCreationOfEmptyRecord method
- *
- * @access public
- * @return void
- */
- function testCreationOfEmptyRecord() {
- $this->loadFixtures('Author');
- $TestModel =& new Author();
- $this->assertEqual($TestModel->find('count'), 4);
- $TestModel->deleteAll(true, false, false);
- $this->assertEqual($TestModel->find('count'), 0);
- $result = $TestModel->save();
- $this->assertTrue(isset($result['Author']['created']));
- $this->assertTrue(isset($result['Author']['updated']));
- $this->assertEqual($TestModel->find('count'), 1);
- }
- /**
- * testCreateWithPKFiltering method
- *
- * @access public
- * @return void
- */
- function testCreateWithPKFiltering() {
- $TestModel =& new Article();
- $data = array('id' => 5, 'user_id' => 2, 'title' => 'My article', 'body' => 'Some text');
- $result = $TestModel->create($data);
- $expected = array('Article' => array('published' => 'N', 'id' => 5, 'user_id' => 2, 'title' => 'My article', 'body' => 'Some text'));
- $this->assertEqual($result, $expected);
- $this->assertEqual($TestModel->id, 5);
- $result = $TestModel->create($data, true);
- $expected = array('Article' => array('published' => 'N', 'id' => false, 'user_id' => 2, 'title' => 'My article', 'body' => 'Some text'));
- $this->assertEqual($result, $expected);
- $this->assertFalse($TestModel->id);
- $result = $TestModel->create(array('Article' => $data), true);
- $expected = array('Article' => array('published' => 'N', 'id' => false, 'user_id' => 2, 'title' => 'My article', 'body' => 'Some text'));
- $this->assertEqual($result, $expected);
- $this->assertFalse($TestModel->id);
- $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');
- $result = $TestModel->create($data);
- $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'));
- $this->assertEqual($result, $expected);
- $this->assertEqual($TestModel->id, 6);
- $result = $TestModel->create(array('Article' => array_diff_key($data, array('created' => true, 'updated' => true, 'modified' => true))), true);
- $expected = array('Article' => array('published' => 'N', 'id' => false, 'user_id' => 2, 'title' => 'My article', 'body' => 'Some text'));
- $this->assertEqual($result, $expected);
- $this->assertFalse($TestModel->id);
- }
- /**
- * testCreationWithMultipleData method
- *
- * @access public
- * @return void
- */
- function testCreationWithMultipleData() {
- $this->loadFixtures('Article', 'Comment');
- $Article =& new Article();
- $Comment =& new Comment();
- $articles = $Article->find('all', array('fields' => array('id','title'), 'recursive' => -1));
- $comments = $Comment->find('all', array('fields' => array('id','article_id','user_id','comment','published'), 'recursive' => -1));
- $this->assertEqual($articles, array(
- array('Article' => array('id' => 1, 'title' => 'First Article')),
- array('Article' => array('id' => 2, 'title' => 'Second Article')),
- array('Article' => array('id' => 3, 'title' => 'Third Article'))));
- $this->assertEqual($comments, array(
- array('Comment' => array('id' => 1, 'article_id' => 1, 'user_id' => 2, 'comment' => 'First Comment for First Article', 'published' => 'Y')),
- array('Comment' => array('id' => 2, 'article_id' => 1, 'user_id' => 4, 'comment' => 'Second Comment for First Article', 'published' => 'Y')),
- array('Comment' => array('id' => 3, 'article_id' => 1, 'user_id' => 1, 'comment' => 'Third Comment for First Article', 'published' => 'Y')),
- array('Comment' => array('id' => 4, 'article_id' => 1, 'user_id' => 1, 'comment' => 'Fourth Comment for First Article', 'published' => 'N')),
- array('Comment' => array('id' => 5, 'article_id' => 2, 'user_id' => 1, 'comment' => 'First Comment for Second Article', 'published' => 'Y')),
- array('Comment' => array('id' => 6, 'article_id' => 2, 'user_id' => 2, 'comment' => 'Second Comment for Second Article', 'published' => 'Y'))));
- $data = array('Comment' => array('article_id' => 2, 'user_id' => 4, 'comment' => 'Brand New Comment', 'published' => 'N'),
- 'Article' => array('id' => 2, 'title' => 'Second Article Modified'));
- $result = $Comment->create($data);
- $this->assertTrue($result);
- $result = $Comment->save();
- $this->assertTrue($result);
- $articles = $Article->find('all', array('fields' => array('id','title'), 'recursive' => -1));
- $comments = $Comment->find('all', array('fields' => array('id','article_id','user_id','comment','published'), 'recursive' => -1));
- $this->assertEqual($articles, array(
- array('Article' => array('id' => 1, 'title' => 'First Article')),
- array('Article' => array('id' => 2, 'title' => 'Second Article')),
- array('Article' => array('id' => 3, 'title' => 'Third Article'))));
- $this->assertEqual($comments, array(
- array('Comment' => array('id' => 1, 'article_id' => 1, 'user_id' => 2, 'comment' => 'First Comment for First Article', 'published' => 'Y')),
- array('Comment' => array('id' => 2, 'article_id' => 1, 'user_id' => 4, 'comment' => 'Second Comment for First Article', 'published' => 'Y')),
- array('Comment' => array('id' => 3, 'article_id' => 1, 'user_id' => 1, 'comment' => 'Third Comment for First Article', 'published' => 'Y')),
- array('Comment' => array('id' => 4, 'article_id' => 1, 'user_id' => 1, 'comment' => 'Fourth Comment for First Article', 'published' => 'N')),
- array('Comment' => array('id' => 5, 'article_id' => 2, 'user_id' => 1, 'comment' => 'First Comment for Second Article', 'published' => 'Y')),
- array('Comment' => array('id' => 6, 'article_id' => 2, 'user_id' => 2, 'comment' => 'Second Comment for Second Article', 'published' => 'Y')),
- array('Comment' => array('id' => 7, 'article_id' => 2, 'user_id' => 4, 'comment' => 'Brand New Comment', 'published' => 'N'))));
- }
- /**
- * testCreationWithMultipleDataSameModel method
- *
- * @access public
- * @return void
- */
- function testCreationWithMultipleDataSameModel() {
- $this->loadFixtures('Article');
- $Article =& new Article();
- $SecondaryArticle =& new Article();
- $result = $Article->field('title', array('id' => 1));
- $this->assertEqual($result, 'First Article');
- $data = array('Article' => array('user_id' => 2, 'title' => 'Brand New Article', 'body' => 'Brand New Article Body', 'published' => 'Y'),
- 'SecondaryArticle' => array('id' => 1));
- $Article->create();
- $result = $Article->save($data);
- $this->assertTrue($result);
- $result = $Article->getInsertID();
- $this->assertTrue(!empty($result));
- $result = $Article->field('title', array('id' => 1));
- $this->assertEqual($result, 'First Article');
- $articles = $Article->find('all', array('fields' => array('id','title'), 'recursive' => -1));
- $this->assertEqual($articles, array(
- array('Article' => array('id' => 1, 'title' => 'First Article')),
- array('Article' => array('id' => 2, 'title' => 'Second Article')),
- array('Article' => array('id' => 3, 'title' => 'Third Article')),
- array('Article' => array('id' => 4, 'title' => 'Brand New Article'))));
- }
- /**
- * testCreationWithMultipleDataSameModelManualInstances method
- *
- * @access public
- * @return void
- */
- function testCreationWithMultipleDataSameModelManualInstances() {
- $this->loadFixtures('PrimaryModel');
- $Primary =& new PrimaryModel();
- $Secondary =& new PrimaryModel();
- $result = $Primary->field('primary_name', array('id' => 1));
- $this->assertEqual($result, 'Primary Name Existing');
- $data = array('PrimaryModel' => array('primary_name' => 'Primary Name New'),
- 'SecondaryModel' => array('id' => array(1)));
- $Primary->create();
- $result = $Primary->save($data);
- $this->assertTrue($result);
- $result = $Primary->field('primary_name', array('id' => 1));
- $this->assertEqual($result, 'Primary Name Existing');
- $result = $Primary->getInsertID();
- $this->assertTrue(!empty($result));
- $result = $Primary->field('primary_name', array('id' => $result));
- $this->assertEqual($result, 'Primary Name New');
- $result = $Primary->find('count');
- $this->assertEqual($result, 2);
- }
- /**
- * testReadFakeThread method
- *
- * @access public
- * @return void
- */
- function testReadFakeThread() {
- $this->loadFixtures('CategoryThre…
Large files files are truncated, but you can click here to view the full file