/lib/Cake/Test/Case/Model/ModelWriteTest.php
PHP | 7998 lines | 6545 code | 725 blank | 728 comment | 29 complexity | 851260c45540b8606a9786eb12af8f0b MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- <?php
- /**
- * ModelWriteTest file
- *
- * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
- * @package Cake.Test.Case.Model
- * @since CakePHP(tm) v 1.2.0.4206
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- App::uses('MockTransactionDboSource', 'Model/Datasource');
- App::uses('MockTransactionAssociatedDboSource', 'Model/Datasource');
- App::uses('MockManyTransactionDboSource', 'Model/Datasource');
- App::uses('MockAssociatedTransactionDboSource', 'Model/Datasource');
- require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
- /**
- * Helper class for testing with mocked datasources
- */
- class TestAuthor extends Author {
- public $hasMany = array(
- 'Post' => array(
- 'className' => 'TestPost'
- )
- );
- protected $_dataSourceObject;
- public $dataForAfterSave;
- /**
- * Helper method to set a datasource object
- *
- * @param Object $object The datasource object
- * @return void
- */
- public function setDataSourceObject($object) {
- $this->_dataSourceObject = $object;
- }
- /**
- * Overwritten in order to return the directly set datasource object if
- * available
- *
- * @return DataSource
- */
- public function getDataSource() {
- if ($this->_dataSourceObject !== null) {
- return $this->_dataSourceObject;
- }
- return parent::getDataSource();
- }
- }
- /**
- * Helper class for testing with mocked datasources
- */
- class TestPost extends Post {
- public $belongsTo = array(
- 'Author' => array(
- 'className' => 'TestAuthor'
- )
- );
- protected $_dataSourceObject;
- public $dataForAfterSave;
- /**
- * Helper method to set a datasource object
- *
- * @param Object $object The datasource object
- * @return void
- */
- public function setDataSourceObject($object) {
- $this->_dataSourceObject = $object;
- }
- /**
- * Overwritten in order to return the directly set datasource object if
- * available
- *
- * @return DataSource
- */
- public function getDataSource() {
- if ($this->_dataSourceObject !== null) {
- return $this->_dataSourceObject;
- }
- return parent::getDataSource();
- }
- }
- /**
- * ModelWriteTest
- *
- * @package Cake.Test.Case.Model
- */
- class ModelWriteTest extends BaseModelTest {
- /**
- * override locale to the default (eng).
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- Configure::write('Config.language', 'eng');
- }
- /**
- * Test save() failing when there is no data.
- *
- * @return void
- */
- public function testInsertNoData() {
- $this->loadFixtures('Bid');
- $Bid = ClassRegistry::init('Bid');
- $this->assertFalse($Bid->save());
- $result = $Bid->save(array('Bid' => array()));
- $this->assertFalse($result);
- $result = $Bid->save(array('Bid' => array('not in schema' => 1)));
- $this->assertFalse($result);
- }
- /**
- * testInsertAnotherHabtmRecordWithSameForeignKey method
- *
- * @return void
- */
- public function testInsertAnotherHabtmRecordWithSameForeignKey() {
- $this->loadFixtures('JoinA', 'JoinB', 'JoinAB', 'JoinC', 'JoinAC');
- $TestModel = new JoinA();
- $result = $TestModel->JoinAsJoinB->findById(1);
- $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'
- ));
- $this->assertEquals($expected, $result);
- $TestModel->JoinAsJoinB->create();
- $data = 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'
- );
- $result = $TestModel->JoinAsJoinB->save($data);
- $lastInsertId = $TestModel->JoinAsJoinB->getLastInsertID();
- $data['id'] = $lastInsertId;
- $this->assertEquals(array('JoinAsJoinB' => $data), $result);
- $this->assertTrue($lastInsertId > 0);
- $result = $TestModel->JoinAsJoinB->findById(1);
- $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'
- ));
- $this->assertEquals($expected, $result);
- $updatedValue = 'UPDATED Data for Join A 1 Join B 2';
- $TestModel->JoinAsJoinB->id = 1;
- $result = $TestModel->JoinAsJoinB->saveField('other', $updatedValue, false);
- $this->assertFalse(empty($result));
- $result = $TestModel->JoinAsJoinB->findById(1);
- $this->assertEquals($updatedValue, $result['JoinAsJoinB']['other']);
- }
- /**
- * testSaveDateAsFirstEntry method
- *
- * @return void
- */
- public function testSaveDateAsFirstEntry() {
- $this->loadFixtures('Article', 'User', 'Comment', 'Attachment', 'Tag', 'ArticlesTag');
- $Article = new Article();
- $data = array(
- 'Article' => array(
- 'created' => array(
- 'day' => '1',
- 'month' => '1',
- 'year' => '2008'
- ),
- 'title' => 'Test Title',
- 'user_id' => 1
- ));
- $Article->create();
- $result = $Article->save($data);
- $this->assertFalse(empty($result));
- $testResult = $Article->find('first', array('conditions' => array('Article.title' => 'Test Title')));
- $this->assertEquals($data['Article']['title'], $testResult['Article']['title']);
- $this->assertEquals('2008-01-01 00:00:00', $testResult['Article']['created']);
- }
- /**
- * testUnderscoreFieldSave method
- *
- * @return void
- */
- public function testUnderscoreFieldSave() {
- $this->loadFixtures('UnderscoreField');
- $UnderscoreField = new UnderscoreField();
- $currentCount = $UnderscoreField->find('count');
- $this->assertEquals(3, $currentCount);
- $data = array('UnderscoreField' => array(
- 'user_id' => '1',
- 'my_model_has_a_field' => 'Content here',
- 'body' => 'Body',
- 'published' => 'Y',
- 'another_field' => 4
- ));
- $ret = $UnderscoreField->save($data);
- $this->assertFalse(empty($ret));
- $currentCount = $UnderscoreField->find('count');
- $this->assertEquals(4, $currentCount);
- }
- /**
- * testAutoSaveUuid method
- *
- * @return void
- */
- public function testAutoSaveUuid() {
- // SQLite does not support non-integer primary keys
- $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with SQLite.');
- $this->loadFixtures('Uuid');
- $TestModel = new Uuid();
- $TestModel->save(array('title' => 'Test record'));
- $result = $TestModel->findByTitle('Test record');
- $this->assertEquals(
- array('id', 'title', 'count', 'created', 'updated'),
- array_keys($result['Uuid'])
- );
- $this->assertEquals(36, strlen($result['Uuid']['id']));
- }
- /**
- * Ensure that if the id key is null but present the save doesn't fail (with an
- * x sql error: "Column id specified twice")
- *
- * @return void
- */
- public function testSaveUuidNull() {
- // SQLite does not support non-integer primary keys
- $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with SQLite.');
- $this->loadFixtures('Uuid');
- $TestModel = new Uuid();
- $TestModel->save(array('title' => 'Test record', 'id' => null));
- $result = $TestModel->findByTitle('Test record');
- $this->assertEquals(
- array('id', 'title', 'count', 'created', 'updated'),
- array_keys($result['Uuid'])
- );
- $this->assertEquals(36, strlen($result['Uuid']['id']));
- }
- /**
- * testZeroDefaultFieldValue method
- *
- * @return void
- */
- public function testZeroDefaultFieldValue() {
- $this->skipIf($this->db instanceof Sqlite, 'SQLite uses loose typing, this operation is unsupported.');
- $this->loadFixtures('DataTest');
- $TestModel = new DataTest();
- $TestModel->create(array());
- $TestModel->save();
- $result = $TestModel->findById($TestModel->id);
- $this->assertEquals(0, $result['DataTest']['count']);
- $this->assertEquals(0, $result['DataTest']['float']);
- }
- /**
- * Tests validation parameter order in custom validation methods
- *
- * @return void
- */
- public function testAllowSimulatedFields() {
- $TestModel = new ValidationTest1();
- $TestModel->create(array(
- 'title' => 'foo',
- 'bar' => 'baz'
- ));
- $expected = array(
- 'ValidationTest1' => array(
- 'title' => 'foo',
- 'bar' => 'baz'
- ));
- $this->assertEquals($expected, $TestModel->data);
- }
- /**
- * test that Caches are getting cleared on save().
- * ensure that both inflections of controller names are getting cleared
- * as URL for controller could be either overallFavorites/index or overall_favorites/index
- *
- * @return void
- */
- public function testCacheClearOnSave() {
- $_back = array(
- 'check' => Configure::read('Cache.check'),
- 'disable' => Configure::read('Cache.disable'),
- );
- Configure::write('Cache.check', true);
- Configure::write('Cache.disable', false);
- $this->loadFixtures('OverallFavorite');
- $OverallFavorite = new OverallFavorite();
- touch(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php');
- touch(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php');
- $data = array(
- 'OverallFavorite' => array(
- 'id' => 22,
- 'model_type' => '8-track',
- 'model_id' => '3',
- 'priority' => '1'
- )
- );
- $OverallFavorite->create($data);
- $OverallFavorite->save();
- $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php'));
- $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php'));
- Configure::write('Cache.check', $_back['check']);
- Configure::write('Cache.disable', $_back['disable']);
- }
- /**
- * test that save() resets whitelist on failed save
- *
- * @return void
- */
- public function testSaveFieldListResetsWhitelistOnFailedSave() {
- $this->loadFixtures('Bidding');
- $model = new Bidding();
- $whitelist = array('title');
- $model->whitelist = $whitelist;
- $result = $model->save(
- array(),
- array('fieldList' => array('body'))
- );
- $this->assertFalse($result);
- $this->assertEquals($whitelist, $model->whitelist);
- }
- /**
- * Test save() resets the whitelist after afterSave
- *
- * @return void
- */
- public function testSaveResetWhitelistOnSuccess() {
- $this->loadFixtures('Post');
- $callback = array($this, 'callbackForWhitelistReset');
- $model = ClassRegistry::init('Post');
- $model->whitelist = array('author_id', 'title', 'body');
- $model->getEventManager()->attach($callback, 'Model.afterSave');
- $data = array(
- 'title' => 'New post',
- 'body' => 'Post body',
- 'author_id' => 1
- );
- $result = $model->save($data);
- $this->assertNotEmpty($result);
- }
- /**
- * Callback for testing whitelist in afterSave
- *
- * @param Model $model The model having save called.
- * @return void
- */
- public function callbackForWhitelistReset($event) {
- $expected = array('author_id', 'title', 'body', 'updated', 'created');
- $this->assertEquals($expected, $event->subject()->whitelist);
- }
- /**
- * testSaveWithCounterCache method
- *
- * @return void
- */
- public function testSaveWithCounterCache() {
- $this->loadFixtures('Syfile', 'Item', 'Image', 'Portfolio', 'ItemsPortfolio');
- $TestModel = new Syfile();
- $TestModel2 = new Item();
- $result = $TestModel->findById(1);
- $this->assertNull($result['Syfile']['item_count']);
- $TestModel2->save(array(
- 'name' => 'Item 7',
- 'syfile_id' => 1,
- 'published' => false
- ));
- $result = $TestModel->findById(1);
- $this->assertEquals(2, $result['Syfile']['item_count']);
- $TestModel2->delete(1);
- $result = $TestModel->findById(1);
- $this->assertEquals(1, $result['Syfile']['item_count']);
- $TestModel2->id = 2;
- $TestModel2->saveField('syfile_id', 1);
- $result = $TestModel->findById(1);
- $this->assertEquals(2, $result['Syfile']['item_count']);
- $result = $TestModel->findById(2);
- $this->assertEquals(0, $result['Syfile']['item_count']);
- }
- /**
- * Tests that counter caches are updated when records are added
- *
- * @return void
- */
- public function testCounterCacheIncrease() {
- $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
- $User = new CounterCacheUser();
- $Post = new CounterCachePost();
- $data = array('Post' => array(
- 'id' => 22,
- 'title' => 'New Post',
- 'user_id' => 66
- ));
- $Post->save($data);
- $user = $User->find('first', array(
- 'conditions' => array('id' => 66),
- 'recursive' => -1
- ));
- $result = $user[$User->alias]['post_count'];
- $expected = 3;
- $this->assertEquals($expected, $result);
- }
- /**
- * Tests that counter caches are updated when records are deleted
- *
- * @return void
- */
- public function testCounterCacheDecrease() {
- $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
- $User = new CounterCacheUser();
- $Post = new CounterCachePost();
- $Post->delete(2);
- $user = $User->find('first', array(
- 'conditions' => array('id' => 66),
- 'recursive' => -1
- ));
- $result = $user[$User->alias]['post_count'];
- $expected = 1;
- $this->assertEquals($expected, $result);
- }
- /**
- * Tests that counter caches are updated when foreign keys of counted records change
- *
- * @return void
- */
- public function testCounterCacheUpdated() {
- $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
- $User = new CounterCacheUser();
- $Post = new CounterCachePost();
- $data = $Post->find('first', array(
- 'conditions' => array('id' => 1),
- 'recursive' => -1
- ));
- $data[$Post->alias]['user_id'] = 301;
- $Post->save($data);
- $users = $User->find('all', array('order' => 'User.id'));
- $this->assertEquals(1, $users[0]['User']['post_count']);
- $this->assertEquals(2, $users[1]['User']['post_count']);
- }
- /**
- * Test counter cache with models that use a non-standard (i.e. not using 'id')
- * as their primary key.
- *
- * @return void
- */
- public function testCounterCacheWithNonstandardPrimaryKey() {
- $this->loadFixtures(
- 'CounterCacheUserNonstandardPrimaryKey',
- 'CounterCachePostNonstandardPrimaryKey'
- );
- $User = new CounterCacheUserNonstandardPrimaryKey();
- $Post = new CounterCachePostNonstandardPrimaryKey();
- $data = $Post->find('first', array(
- 'conditions' => array('pid' => 1),
- 'recursive' => -1
- ));
- $data[$Post->alias]['uid'] = 301;
- $Post->save($data);
- $users = $User->find('all', array('order' => 'User.uid'));
- $this->assertEquals(1, $users[0]['User']['post_count']);
- $this->assertEquals(2, $users[1]['User']['post_count']);
- }
- /**
- * test Counter Cache With Self Joining table
- *
- * @return void
- */
- public function testCounterCacheWithSelfJoin() {
- $this->skipIf($this->db instanceof Sqlite, 'SQLite 2.x does not support ALTER TABLE ADD COLUMN');
- $this->loadFixtures('CategoryThread');
- $column = 'COLUMN ';
- if ($this->db instanceof Sqlserver) {
- $column = '';
- }
- $column .= $this->db->buildColumn(array('name' => 'child_count', 'type' => 'integer'));
- $this->db->query('ALTER TABLE ' . $this->db->fullTableName('category_threads') . ' ADD ' . $column);
- $this->db->flushMethodCache();
- $Category = new CategoryThread();
- $result = $Category->updateAll(array('CategoryThread.name' => "'updated'"), array('CategoryThread.parent_id' => 5));
- $this->assertFalse(empty($result));
- $Category = new CategoryThread();
- $Category->belongsTo['ParentCategory']['counterCache'] = 'child_count';
- $Category->updateCounterCache(array('parent_id' => 5));
- $result = Hash::extract($Category->find('all', array('conditions' => array('CategoryThread.id' => 5))), '{n}.CategoryThread.child_count');
- $expected = array(1);
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveWithCounterCacheScope method
- *
- * @return void
- */
- public function testSaveWithCounterCacheScope() {
- $this->loadFixtures('Syfile', 'Item', 'Image', 'ItemsPortfolio', 'Portfolio');
- $TestModel = new Syfile();
- $TestModel2 = new Item();
- $TestModel2->belongsTo['Syfile']['counterCache'] = true;
- $TestModel2->belongsTo['Syfile']['counterScope'] = array('published' => true);
- $result = $TestModel->findById(1);
- $this->assertNull($result['Syfile']['item_count']);
- $TestModel2->save(array(
- 'name' => 'Item 7',
- 'syfile_id' => 1,
- 'published' => true
- ));
- $result = $TestModel->findById(1);
- $this->assertEquals(1, $result['Syfile']['item_count']);
- $TestModel2->id = 1;
- $TestModel2->saveField('published', true);
- $result = $TestModel->findById(1);
- $this->assertEquals(2, $result['Syfile']['item_count']);
- $TestModel2->save(array(
- 'id' => 1,
- 'syfile_id' => 1,
- 'published' => false
- ));
- $result = $TestModel->findById(1);
- $this->assertEquals(1, $result['Syfile']['item_count']);
- }
- /**
- * Tests having multiple counter caches for an associated model
- *
- * @return void
- */
- public function testCounterCacheMultipleCaches() {
- $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
- $User = new CounterCacheUser();
- $Post = new CounterCachePost();
- $Post->unbindModel(array('belongsTo' => array('User')), false);
- $Post->bindModel(array(
- 'belongsTo' => array(
- 'User' => array(
- 'className' => 'CounterCacheUser',
- 'foreignKey' => 'user_id',
- 'counterCache' => array(
- true,
- 'posts_published' => array('Post.published' => true)
- )
- )
- )
- ), false);
- // Count Increase
- $data = array('Post' => array(
- 'id' => 22,
- 'title' => 'New Post',
- 'user_id' => 66,
- 'published' => true
- ));
- $Post->save($data);
- $result = $User->find('first', array(
- 'conditions' => array('id' => 66),
- 'recursive' => -1
- ));
- $this->assertEquals(3, $result[$User->alias]['post_count']);
- $this->assertEquals(2, $result[$User->alias]['posts_published']);
- // Count decrease
- $Post->delete(1);
- $result = $User->find('first', array(
- 'conditions' => array('id' => 66),
- 'recursive' => -1
- ));
- $this->assertEquals(2, $result[$User->alias]['post_count']);
- $this->assertEquals(2, $result[$User->alias]['posts_published']);
- // Count update
- $data = $Post->find('first', array(
- 'conditions' => array('id' => 1),
- 'recursive' => -1
- ));
- $data[$Post->alias]['user_id'] = 301;
- $Post->save($data);
- $result = $User->find('all', array('order' => 'User.id'));
- $this->assertEquals(2, $result[0]['User']['post_count']);
- $this->assertEquals(1, $result[1]['User']['posts_published']);
- }
- /**
- * Tests that counter caches are unchanged when using 'counterCache' => false
- *
- * @return void
- */
- public function testCounterCacheSkip() {
- $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
- $User = new CounterCacheUser();
- $Post = new CounterCachePost();
- $data = $Post->find('first', array(
- 'conditions' => array('id' => 1),
- 'recursive' => -1
- ));
- $data[$Post->alias]['user_id'] = 301;
- $Post->save($data, array('counterCache' => false));
- $users = $User->find('all', array('order' => 'User.id'));
- $this->assertEquals(2, $users[0]['User']['post_count']);
- $this->assertEquals(1, $users[1]['User']['post_count']);
- }
- /**
- * test that beforeValidate returning false can abort saves.
- *
- * @return void
- */
- public function testBeforeValidateSaveAbortion() {
- $this->loadFixtures('Post');
- $Model = new CallbackPostTestModel();
- $Model->beforeValidateReturn = false;
- $data = array(
- 'title' => 'new article',
- 'body' => 'this is some text.'
- );
- $Model->create();
- $result = $Model->save($data);
- $this->assertFalse($result);
- }
- /**
- * test that beforeSave returning false can abort saves.
- *
- * @return void
- */
- public function testBeforeSaveSaveAbortion() {
- $this->loadFixtures('Post');
- $Model = new CallbackPostTestModel();
- $Model->beforeSaveReturn = false;
- $data = array(
- 'title' => 'new article',
- 'body' => 'this is some text.'
- );
- $Model->create();
- $result = $Model->save($data);
- $this->assertFalse($result);
- }
- /**
- * testSaveAtomic method
- *
- * @return void
- */
- public function testSaveAtomic() {
- $this->loadFixtures('Article');
- $TestModel = new Article();
- // Create record with 'atomic' = false
- $data = array(
- 'Article' => array(
- 'user_id' => '1',
- 'title' => 'Fourth Article',
- 'body' => 'Fourth Article Body',
- 'published' => 'Y'
- )
- );
- $TestModel->create();
- $result = $TestModel->save($data, array('atomic' => false));
- $this->assertTrue((bool)$result);
- // Check record we created
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
- $expected = array(
- 'Article' => array(
- 'id' => '4',
- 'user_id' => '1',
- 'title' => 'Fourth Article',
- 'body' => 'Fourth Article Body',
- 'published' => 'Y'
- )
- );
- $this->assertEquals($expected, $result);
- // Create record with 'atomic' = true
- $data = array(
- 'Article' => array(
- 'user_id' => '4',
- 'title' => 'Fifth Article',
- 'body' => 'Fifth Article Body',
- 'published' => 'Y'
- )
- );
- $TestModel->create();
- $result = $TestModel->save($data, array('atomic' => true));
- $this->assertTrue((bool)$result);
- // Check record we created
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
- $expected = array(
- 'Article' => array(
- 'id' => '5',
- 'user_id' => '4',
- 'title' => 'Fifth Article',
- 'body' => 'Fifth Article Body',
- 'published' => 'Y'
- )
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * test save with transaction and ensure there is no missing rollback.
- *
- * @return void
- */
- public function testSaveTransactionNoRollback() {
- $this->loadFixtures('Post', 'Article');
- $db = $this->getMock('DboSource', array('begin', 'connect', 'rollback', 'describe'));
- $db->expects($this->once())
- ->method('describe')
- ->will($this->returnValue(array()));
- $db->expects($this->once())
- ->method('begin')
- ->will($this->returnValue(true));
- $db->expects($this->once())
- ->method('rollback');
- $Post = new TestPost();
- $Post->setDataSourceObject($db);
- $callback = array($this, 'callbackForTestSaveTransaction');
- $Post->getEventManager()->attach($callback, 'Model.beforeSave');
- $data = array(
- 'Post' => array(
- 'author_id' => 1,
- 'title' => 'New Fourth Post'
- )
- );
- $Post->save($data, array('atomic' => true));
- }
- /**
- * test callback used in testSaveTransaction method
- *
- * @return bool false to stop event propagation
- */
- public function callbackForTestSaveTransaction($event) {
- $TestModel = new Article();
- // Create record. Do not use same model as in testSaveTransaction
- // to avoid infinite loop.
- $data = array(
- 'Article' => array(
- 'user_id' => '1',
- 'title' => 'Fourth Article',
- 'body' => 'Fourth Article Body',
- 'published' => 'Y'
- )
- );
- $TestModel->create();
- $result = $TestModel->save($data);
- $this->assertTrue((bool)$result);
- // force transaction to be rolled back in Post model
- $event->stopPropagation();
- return false;
- }
- /**
- * testSaveTransaction method
- *
- * @return void
- */
- public function testSaveTransaction() {
- $this->loadFixtures('Post', 'Article');
- $PostModel = new Post();
- // Check if Database supports transactions
- $PostModel->validate = array('title' => 'notEmpty');
- $data = array(
- array('author_id' => 1, 'title' => 'New Fourth Post'),
- array('author_id' => 1, 'title' => 'New Fifth Post'),
- array('author_id' => 1, 'title' => '')
- );
- $this->assertFalse($PostModel->saveAll($data));
- $result = $PostModel->find('all', array('recursive' => -1));
- $expectedPosts = array(
- 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'
- )
- ),
- 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'
- )
- ),
- 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'
- )
- )
- );
- $this->skipIf(count($result) !== 3, 'Database does not support transactions.');
- $this->assertEquals($expectedPosts, $result);
- // Database supports transactions --> continue tests
- $data = array(
- 'Post' => array(
- 'author_id' => 1,
- 'title' => 'New Fourth Post'
- )
- );
- $callback = array($this, 'callbackForTestSaveTransaction');
- $PostModel->getEventManager()->attach($callback, 'Model.beforeSave');
- $PostModel->create();
- $result = $PostModel->save($data, array('atomic' => true));
- $this->assertFalse($result);
- $result = $PostModel->find('all', array('recursive' => -1));
- $this->assertEquals($expectedPosts, $result);
- // Check record we created in callbackForTestSaveTransaction method.
- // record should not exist due to rollback
- $ArticleModel = new Article();
- $result = $ArticleModel->find('all', array('recursive' => -1));
- $expectedArticles = array(
- array(
- 'Article' => array(
- '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',
- 'id' => '1'
- )
- ),
- array(
- 'Article' => array(
- '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',
- 'id' => '2'
- )
- ),
- array(
- 'Article' => array(
- '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',
- 'id' => '3'
- )
- )
- );
- $this->assertEquals($expectedArticles, $result);
- }
- /**
- * testSaveField method
- *
- * @return void
- */
- public function testSaveField() {
- $this->loadFixtures('Article');
- $TestModel = new Article();
- $TestModel->id = 1;
- $result = $TestModel->saveField('title', 'New First Article');
- $this->assertFalse(empty($result));
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
- $expected = array('Article' => array(
- 'id' => '1',
- 'user_id' => '1',
- 'title' => 'New First Article',
- 'body' => 'First Article Body'
- ));
- $this->assertEquals($expected, $result);
- $TestModel->id = 1;
- $result = $TestModel->saveField('title', '');
- $this->assertFalse(empty($result));
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
- $expected = array('Article' => array(
- 'id' => '1',
- 'user_id' => '1',
- 'title' => '',
- 'body' => 'First Article Body'
- ));
- $result['Article']['title'] = trim($result['Article']['title']);
- $this->assertEquals($expected, $result);
- $TestModel->id = 1;
- $TestModel->set('body', 'Messed up data');
- $result = $TestModel->saveField('title', 'First Article');
- $this->assertFalse(empty($result));
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
- $expected = array('Article' => array(
- 'id' => '1',
- 'user_id' => '1',
- 'title' => 'First Article',
- 'body' => 'First Article Body'
- ));
- $this->assertEquals($expected, $result);
- $TestModel->recursive = -1;
- $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
- $TestModel->id = 1;
- $result = $TestModel->saveField('title', '', true);
- $this->assertFalse($result);
- $TestModel->recursive = -1;
- $TestModel->id = 1;
- $result = $TestModel->saveField('user_id', 9999);
- $this->assertTrue((bool)$result);
- $result = $TestModel->read(array('id', 'user_id'), 1);
- $expected = array('Article' => array(
- 'id' => '1',
- 'user_id' => '9999',
- ));
- $this->assertEquals($expected, $result);
- $this->loadFixtures('Node', 'Dependency');
- $Node = new Node();
- $Node->set('id', 1);
- $result = $Node->read();
- $this->assertEquals(array('Second'), Hash::extract($result, 'ParentNode.{n}.name'));
- $Node->saveField('state', 10);
- $result = $Node->read();
- $this->assertEquals(array('Second'), Hash::extract($result, 'ParentNode.{n}.name'));
- }
- /**
- * testSaveWithCreate method
- *
- * @return void
- */
- public function testSaveWithCreate() {
- $this->loadFixtures(
- 'User',
- 'Article',
- 'User',
- 'Comment',
- 'Tag',
- 'ArticlesTag',
- 'Attachment'
- );
- $TestModel = new User();
- $data = array('User' => array(
- 'user' => 'user',
- 'password' => ''
- ));
- $result = $TestModel->save($data);
- $this->assertFalse($result);
- $this->assertTrue(!empty($TestModel->validationErrors));
- $TestModel = new Article();
- $data = array('Article' => array(
- 'user_id' => '',
- 'title' => '',
- 'body' => ''
- ));
- $result = $TestModel->create($data) && $TestModel->save();
- $this->assertFalse($result);
- $this->assertTrue(!empty($TestModel->validationErrors));
- $data = array('Article' => array(
- 'id' => 1,
- 'user_id' => '1',
- 'title' => 'New First Article',
- 'body' => ''
- ));
- $result = $TestModel->create($data) && $TestModel->save();
- $this->assertFalse($result);
- $data = array('Article' => array(
- 'id' => 1,
- 'title' => 'New First Article'
- ));
- $result = $TestModel->create() && $TestModel->save($data, false);
- $this->assertFalse(empty($result));
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
- $expected = array('Article' => array(
- 'id' => '1',
- 'user_id' => '1',
- 'title' => 'New First Article',
- 'body' => 'First Article Body',
- 'published' => 'N'
- ));
- $this->assertEquals($expected, $result);
- $data = array('Article' => array(
- 'id' => 1,
- 'user_id' => '2',
- 'title' => 'First Article',
- 'body' => 'New First Article Body',
- 'published' => 'Y'
- ));
- $result = $TestModel->create() && $TestModel->save($data, true, array('id', 'title', 'published'));
- $this->assertFalse(empty($result));
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
- $expected = array('Article' => array(
- 'id' => '1',
- 'user_id' => '1',
- 'title' => 'First Article',
- 'body' => 'First Article Body',
- 'published' => 'Y'
- ));
- $this->assertEquals($expected, $result);
- $data = array(
- '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'
- ),
- 'Tag' => array('Tag' => array(1, 3))
- );
- $TestModel->create();
- $result = $TestModel->create() && $TestModel->save($data);
- $this->assertFalse(empty($result));
- $TestModel->recursive = 2;
- $result = $TestModel->read(null, 4);
- $expected = array(
- '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'
- ),
- 'User' => array(
- 'id' => '2',
- 'user' => 'nate',
- 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
- 'created' => '2007-03-17 01:18:23',
- 'updated' => '2007-03-17 01:20:31'
- ),
- 'Comment' => array(),
- '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->assertEquals($expected, $result);
- $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'
- ));
- $result = $TestModel->Comment->create() && $TestModel->Comment->save($data);
- $this->assertFalse(empty($result));
- $data = array('Attachment' => array(
- 'comment_id' => '7',
- 'attachment' => 'newattachment.zip',
- 'created' => '2007-03-18 15:02:23',
- 'updated' => '2007-03-18 15:04:31'
- ));
- $result = $TestModel->Comment->Attachment->save($data);
- $this->assertFalse(empty($result));
- $TestModel->recursive = 2;
- $result = $TestModel->read(null, 4);
- $expected = array(
- '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'
- ),
- 'User' => array(
- 'id' => '2',
- 'user' => 'nate',
- 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
- 'created' => '2007-03-17 01:18:23',
- 'updated' => '2007-03-17 01:20:31'
- ),
- 'Comment' => array(
- array(
- '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',
- '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'
- ),
- 'User' => array(
- 'id' => '1',
- 'user' => 'mariano',
- 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
- 'created' => '2007-03-17 01:16:23',
- 'updated' => '2007-03-17 01:18:31'
- ),
- 'Attachment' => array(
- 'id' => '2',
- 'comment_id' => '7',
- 'attachment' => 'newattachment.zip',
- 'created' => '2007-03-18 15:02:23',
- 'updated' => '2007-03-18 15:04: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->assertEquals($expected, $result);
- }
- /**
- * test that a null Id doesn't cause errors
- *
- * @return void
- */
- public function testSaveWithNullId() {
- $this->loadFixtures('User');
- $User = new User();
- $User->read(null, 1);
- $User->data['User']['id'] = null;
- $result = $User->save(array('password' => 'test'));
- $this->assertFalse(empty($result));
- $this->assertTrue($User->id > 0);
- $User->read(null, 2);
- $User->data['User']['id'] = null;
- $result = $User->save(array('password' => 'test'));
- $this->assertFalse(empty($result));
- $this->assertTrue($User->id > 0);
- $User->data['User'] = array('password' => 'something');
- $result = $User->save();
- $this->assertFalse(empty($result));
- $result = $User->read();
- $this->assertEquals('something', $User->data['User']['password']);
- }
- /**
- * testSaveWithSet method
- *
- * @return void
- */
- public function testSaveWithSet() {
- $this->loadFixtures('Article');
- $TestModel = new Article();
- // Create record we will be updating later
- $data = array('Article' => array(
- 'user_id' => '1',
- 'title' => 'Fourth Article',
- 'body' => 'Fourth Article Body',
- 'published' => 'Y'
- ));
- $result = $TestModel->create() && $TestModel->save($data);
- $this->assertFalse(empty($result));
- // Check record we created
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
- $expected = array('Article' => array(
- 'id' => '4',
- 'user_id' => '1',
- 'title' => 'Fourth Article',
- 'body' => 'Fourth Article Body',
- 'published' => 'Y'
- ));
- $this->assertEquals($expected, $result);
- // Create new record just to overlap Model->id on previously created record
- $data = array('Article' => array(
- 'user_id' => '4',
- 'title' => 'Fifth Article',
- 'body' => 'Fifth Article Body',
- 'published' => 'Y'
- ));
- $result = $TestModel->create() && $TestModel->save($data);
- $this->assertFalse(empty($result));
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
- $expected = array('Article' => array(
- 'id' => '5',
- 'user_id' => '4',
- 'title' => 'Fifth Article',
- 'body' => 'Fifth Article Body',
- 'published' => 'Y'
- ));
- $this->assertEquals($expected, $result);
- // Go back and edit the first article we created, starting by checking it's still there
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
- $expected = array('Article' => array(
- 'id' => '4',
- 'user_id' => '1',
- 'title' => 'Fourth Article',
- 'body' => 'Fourth Article Body',
- 'published' => 'Y'
- ));
- $this->assertEquals($expected, $result);
- // And now do the update with set()
- $data = array('Article' => array(
- 'id' => '4',
- 'title' => 'Fourth Article - New Title',
- 'published' => 'N'
- ));
- $result = $TestModel->set($data) && $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
- $expected = array('Article' => array(
- 'id' => '4',
- 'user_id' => '1',
- 'title' => 'Fourth Article - New Title',
- 'body' => 'Fourth Article Body',
- 'published' => 'N'
- ));
- $this->assertEquals($expected, $result);
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
- $expected = array('Article' => array(
- 'id' => '5',
- 'user_id' => '4',
- 'title' => 'Fifth Article',
- 'body' => 'Fifth Article Body',
- 'published' => 'Y'
- ));
- $this->assertEquals($expected, $result);
- $data = array('Article' => array('id' => '5', 'title' => 'Fifth Article - New Title 5'));
- $result = ($TestModel->set($data) && $TestModel->save());
- $this->assertFalse(empty($result));
- $TestModel->recursive = -1;
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
- $expected = array('Article' => array(
- 'id' => '5',
- 'user_id' => '4',
- 'title' => 'Fifth Article - New Title 5',
- 'body' => 'Fifth Article Body',
- 'published' => 'Y'
- ));
- $this->assertEquals($expected, $result);
- $TestModel->recursive = -1;
- $result = $TestModel->find('all', array(
- 'fields' => array('id', 'title'),
- 'order' => array('Article.id' => 'ASC')
- ));
- $expected = 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' => 'Fourth Article - New Title')),
- array('Article' => array('id' => 5, 'title' => 'Fifth Article - New Title 5'))
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveWithNonExistentFields method
- *
- * @return void
- */
- public function testSaveWithNonExistentFields() {
- $this->loadFixtures('Article');
- $TestModel = new Article();
- $TestModel->recursive = -1;
- $data = array(
- 'non_existent' => 'This field does not exist',
- 'user_id' => '1',
- 'title' => 'Fourth Article - New Title',
- 'body' => 'Fourth Article Body',
- 'published' => 'N'
- );
- $result = $TestModel->create() && $TestModel->save($data);
- $this->assertFalse(empty($result));
- $expected = array('Article' => array(
- 'id' => '4',
- 'user_id' => '1',
- 'title' => 'Fourth Article - New Title',
- 'body' => 'Fourth Article Body',
- 'published' => 'N'
- ));
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
- $this->assertEquals($expected, $result);
- $data = array(
- 'user_id' => '1',
- 'non_existent' => 'This field does not exist',
- 'title' => 'Fifth Article - New Title',
- 'body' => 'Fifth Article Body',
- 'published' => 'N'
- );
- $result = $TestModel->create() && $TestModel->save($data);
- $this->assertFalse(empty($result));
- $expected = array('Article' => array(
- 'id' => '5',
- 'user_id' => '1',
- 'title' => 'Fifth Article - New Title',
- 'body' => 'Fifth Article Body',
- 'published' => 'N'
- ));
- $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveFromXml method
- *
- * @return void
- */
- public function testSaveFromXml() {
- $this->markTestSkipped('This feature needs to be fixed or dropped');
- $this->loadFixtures('Article');
- App::uses('Xml', 'Utility');
- $Article = new Article();
- $result = $Article->save(Xml::build('<article title="test xml" user_id="5" />'));
- $this->assertFalse(empty($result));
- $results = $Article->find('first', array('conditions' => array('Article.title' => 'test xml')));
- $this->assertFalse(empty($results));
- $result = $Article->save(Xml::build('<article><title>testing</title><user_id>6</user_id></article>'));
- $this->assertFalse(empty($result));
- $results = $Article->find('first', array('conditions' => array('Article.title' => 'testing')));
- $this->assertFalse(empty($results));
- $result = $Article->save(Xml::build('<article><title>testing with DOMDocument</title><user_id>7</user_id></article>', array('return' => 'domdocument')));
- $this->assertFalse(empty($result));
- $results = $Article->find('first', array('conditions' => array('Article.title' => 'testing with DOMDocument')));
- $this->assertFalse(empty($results));
- }
- /**
- * testSaveHabtm method
- *
- * @return void
- */
- public function testSaveHabtm() {
- $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
- $TestModel = new Article();
- $result = $TestModel->findById(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->assertEquals($expected, $result);
- $data = array(
- 'Article' => array(
- 'id' => '2',
- 'title' => 'New Second Article'
- ),
- 'Tag' => array('Tag' => array(1, 2))
- );
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $this->assertEquals($data['Tag'], $result['Tag']);
- $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'New Second Article',
- 'body' => 'Second Article Body'
- ),
- 'Tag' => array(
- array(
- 'id' => '1',
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- array(
- 'id' => '2',
- 'tag' => 'tag2',
- 'created' => '2007-03-18 12:24:23',
- 'updated' => '2007-03-18 12:26:31'
- )));
- $this->assertEquals($expected, $result);
- $data = array('Article' => array('id' => '2'), 'Tag' => array('Tag' => array(2, 3)));
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'New Second Article',
- 'body' => 'Second Article Body'
- ),
- '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'
- )));
- $this->assertEquals($expected, $result);
- $data = array('Tag' => array('Tag' => array(1, 2, 3)));
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'New Second Article',
- 'body' => 'Second Article Body'
- ),
- 'Tag' => array(
- array(
- 'id' => '1',
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- 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'
- )));
- $this->assertEquals($expected, $result);
- $data = array('Tag' => array('Tag' => array()));
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $data = array('Tag' => array('Tag' => ''));
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'New Second Article',
- 'body' => 'Second Article Body'
- ),
- 'Tag' => array()
- );
- $this->assertEquals($expected, $result);
- $data = array('Tag' => array('Tag' => array(2, 3)));
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'New Second Article',
- 'body' => 'Second Article Body'
- ),
- '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'
- )));
- $this->assertEquals($expected, $result);
- $data = array(
- 'Tag' => array(
- 'Tag' => array(1, 2)
- ),
- 'Article' => array(
- 'id' => '2',
- 'title' => 'New Second Article'
- ));
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'New Second Article',
- 'body' => 'Second Article Body'
- ),
- 'Tag' => array(
- array(
- 'id' => '1',
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- array(
- 'id' => '2',
- 'tag' => 'tag2',
- 'created' => '2007-03-18 12:24:23',
- 'updated' => '2007-03-18 12:26:31'
- )));
- $this->assertEquals($expected, $result);
- $data = array(
- 'Tag' => array(
- 'Tag' => array(1, 2)
- ),
- 'Article' => array(
- 'id' => '2',
- 'title' => 'New Second Article Title'
- ));
- $result = $TestModel->set($data);
- $this->assertFalse(empty($result));
- $result = $TestModel->save();
- $this->assertFalse(empty($result));
- $TestModel->unbindModel(array(
- 'belongsTo' => array('User'),
- 'hasMany' => array('Comment')
- ));
- $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
- $expected = array(
- 'Article' => array(
- 'id' => '2',
- 'user_id' => '3',
- 'title' => 'New Second Article Title',
- 'body' => 'Second Article Body'
- ),
- 'Tag' => array(
- array(
- 'id' => '1',
- 'tag' => 'tag1',
- 'created' => '2007-03-18 12:22:23',
- 'updated' => '2007-03-18 12:24:31'
- ),
- array(
- …
Large files files are truncated, but you can click here to view the full file