2009-07-24 22:17:14 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ModelWriteTest file
|
|
|
|
*
|
2012-04-27 02:49:18 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-07-24 22:17:14 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-10-03 16:31:21 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
2009-07-24 22:17:14 +00:00
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-04-27 02:49:18 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Model
|
2009-07-24 22:17:14 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.4206
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2009-07-24 22:17:14 +00:00
|
|
|
*/
|
2013-05-30 22:11:14 +00:00
|
|
|
|
2013-03-13 20:58:55 +00:00
|
|
|
App::uses('MockTransactionDboSource', 'Model/Datasource');
|
|
|
|
App::uses('MockTransactionAssociatedDboSource', 'Model/Datasource');
|
|
|
|
App::uses('MockManyTransactionDboSource', 'Model/Datasource');
|
|
|
|
App::uses('MockAssociatedTransactionDboSource', 'Model/Datasource');
|
|
|
|
|
2011-04-10 21:10:30 +00:00
|
|
|
require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
|
2013-05-30 22:11:14 +00:00
|
|
|
|
2014-03-01 18:01:36 +00:00
|
|
|
/**
|
|
|
|
* Helper class for testing with mocked datasources
|
|
|
|
*/
|
|
|
|
class TestAuthor extends Author {
|
|
|
|
|
|
|
|
public $hasMany = array(
|
|
|
|
'Post' => array(
|
2014-03-01 22:01:33 +00:00
|
|
|
'className' => 'TestPost'
|
2014-03-01 18:01:36 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
protected $_dataSourceObject;
|
|
|
|
|
2014-08-03 13:34:11 +00:00
|
|
|
public $dataForAfterSave;
|
|
|
|
|
2014-03-01 18:01:36 +00:00
|
|
|
/**
|
|
|
|
* Helper method to set a datasource object
|
|
|
|
*
|
|
|
|
* @param Object $object The datasource object
|
2014-04-02 01:02:37 +00:00
|
|
|
* @return void
|
2014-03-01 18:01:36 +00:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
|
2014-08-03 13:34:11 +00:00
|
|
|
public $dataForAfterSave;
|
|
|
|
|
2014-03-01 18:01:36 +00:00
|
|
|
/**
|
|
|
|
* Helper method to set a datasource object
|
|
|
|
*
|
|
|
|
* @param Object $object The datasource object
|
2014-04-02 01:02:37 +00:00
|
|
|
* @return void
|
2014-03-01 18:01:36 +00:00
|
|
|
*/
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* ModelWriteTest
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Model
|
2009-07-24 22:17:14 +00:00
|
|
|
*/
|
|
|
|
class ModelWriteTest extends BaseModelTest {
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2013-06-11 18:03:05 +00:00
|
|
|
/**
|
|
|
|
* override locale to the default (eng).
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
Configure::write('Config.language', 'eng');
|
|
|
|
}
|
|
|
|
|
2012-12-16 18:18:59 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2012-01-25 01:32:06 +00:00
|
|
|
/**
|
|
|
|
* testInsertAnotherHabtmRecordWithSameForeignKey method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInsertAnotherHabtmRecordWithSameForeignKey() {
|
|
|
|
$this->loadFixtures('JoinA', 'JoinB', 'JoinAB', 'JoinC', 'JoinAC');
|
|
|
|
$TestModel = new JoinA();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2012-01-25 01:32:06 +00:00
|
|
|
$result = $TestModel->JoinAsJoinB->findById(1);
|
|
|
|
$expected = array(
|
|
|
|
'JoinAsJoinB' => array(
|
|
|
|
'id' => 1,
|
2009-07-24 22:17:14 +00:00
|
|
|
'join_a_id' => 1,
|
2012-01-25 01:32:06 +00:00
|
|
|
'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;
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('JoinAsJoinB' => $data), $result);
|
2012-10-25 01:00:56 +00:00
|
|
|
$this->assertTrue($lastInsertId > 0);
|
2010-06-02 05:00:52 +00:00
|
|
|
|
2012-01-25 01:32:06 +00:00
|
|
|
$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);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2012-01-25 01:32:06 +00:00
|
|
|
$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));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2012-01-25 01:32:06 +00:00
|
|
|
$result = $TestModel->JoinAsJoinB->findById(1);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($updatedValue, $result['JoinAsJoinB']['other']);
|
2012-01-25 01:32:06 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testSaveDateAsFirstEntry method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSaveDateAsFirstEntry() {
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->loadFixtures('Article', 'User', 'Comment', 'Attachment', 'Tag', 'ArticlesTag');
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2010-06-02 05:00:52 +00:00
|
|
|
$Article = new Article();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Article' => array(
|
|
|
|
'created' => array(
|
|
|
|
'day' => '1',
|
|
|
|
'month' => '1',
|
|
|
|
'year' => '2008'
|
|
|
|
),
|
|
|
|
'title' => 'Test Title',
|
|
|
|
'user_id' => 1
|
|
|
|
));
|
|
|
|
$Article->create();
|
2010-06-02 05:00:52 +00:00
|
|
|
$result = $Article->save($data);
|
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2011-02-26 00:26:35 +00:00
|
|
|
$testResult = $Article->find('first', array('conditions' => array('Article.title' => 'Test Title')));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2012-03-22 11:18:57 +00:00
|
|
|
$this->assertEquals($data['Article']['title'], $testResult['Article']['title']);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('2008-01-01 00:00:00', $testResult['Article']['created']);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testUnderscoreFieldSave method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testUnderscoreFieldSave() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('UnderscoreField');
|
2010-06-02 05:00:52 +00:00
|
|
|
$UnderscoreField = new UnderscoreField();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$currentCount = $UnderscoreField->find('count');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(3, $currentCount);
|
2009-07-24 22:17:14 +00:00
|
|
|
$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);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($ret));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$currentCount = $UnderscoreField->find('count');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(4, $currentCount);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testAutoSaveUuid method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAutoSaveUuid() {
|
2009-08-02 06:47:28 +00:00
|
|
|
// SQLite does not support non-integer primary keys
|
2011-05-31 00:49:46 +00:00
|
|
|
$this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with SQLite.');
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$this->loadFixtures('Uuid');
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new Uuid();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->save(array('title' => 'Test record'));
|
|
|
|
$result = $TestModel->findByTitle('Test record');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(
|
2012-03-22 11:18:57 +00:00
|
|
|
array('id', 'title', 'count', 'created', 'updated'),
|
|
|
|
array_keys($result['Uuid'])
|
2009-07-24 22:17:14 +00:00
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(36, strlen($result['Uuid']['id']));
|
2010-03-15 10:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testSaveUuidNull() {
|
2010-03-15 10:55:47 +00:00
|
|
|
// SQLite does not support non-integer primary keys
|
2011-05-31 00:49:46 +00:00
|
|
|
$this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with SQLite.');
|
2010-03-15 10:55:47 +00:00
|
|
|
|
|
|
|
$this->loadFixtures('Uuid');
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new Uuid();
|
2010-03-15 10:55:47 +00:00
|
|
|
|
|
|
|
$TestModel->save(array('title' => 'Test record', 'id' => null));
|
|
|
|
$result = $TestModel->findByTitle('Test record');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(
|
2012-03-22 11:18:57 +00:00
|
|
|
array('id', 'title', 'count', 'created', 'updated'),
|
|
|
|
array_keys($result['Uuid'])
|
2010-03-15 10:55:47 +00:00
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(36, strlen($result['Uuid']['id']));
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testZeroDefaultFieldValue method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testZeroDefaultFieldValue() {
|
2011-05-31 00:49:46 +00:00
|
|
|
$this->skipIf($this->db instanceof Sqlite, 'SQLite uses loose typing, this operation is unsupported.');
|
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('DataTest');
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new DataTest();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->create(array());
|
|
|
|
$TestModel->save();
|
|
|
|
$result = $TestModel->findById($TestModel->id);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(0, $result['DataTest']['count']);
|
|
|
|
$this->assertEquals(0, $result['DataTest']['float']);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* Tests validation parameter order in custom validation methods
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAllowSimulatedFields() {
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new ValidationTest1();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->create(array(
|
|
|
|
'title' => 'foo',
|
|
|
|
'bar' => 'baz'
|
|
|
|
));
|
|
|
|
$expected = array(
|
|
|
|
'ValidationTest1' => array(
|
|
|
|
'title' => 'foo',
|
|
|
|
'bar' => 'baz'
|
|
|
|
));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->data);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* test that Caches are getting cleared on save().
|
|
|
|
* ensure that both inflections of controller names are getting cleared
|
2013-10-08 03:17:58 +00:00
|
|
|
* as URL for controller could be either overallFavorites/index or overall_favorites/index
|
2009-07-24 22:17:14 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCacheClearOnSave() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$_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');
|
2010-06-02 05:00:52 +00:00
|
|
|
$OverallFavorite = new OverallFavorite();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
touch(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php');
|
|
|
|
touch(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php');
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'OverallFavorite' => array(
|
2010-03-16 03:07:18 +00:00
|
|
|
'id' => 22,
|
2012-05-27 18:20:19 +00:00
|
|
|
'model_type' => '8-track',
|
2009-07-24 22:17:14 +00:00
|
|
|
'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']);
|
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2013-02-26 01:57:39 +00:00
|
|
|
/**
|
|
|
|
* test that save() resets whitelist on failed save
|
2014-04-02 01:02:37 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2013-02-26 01:57:39 +00:00
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-03-08 17:51:46 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testSaveWithCounterCache method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSaveWithCounterCache() {
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->loadFixtures('Syfile', 'Item', 'Image', 'Portfolio', 'ItemsPortfolio');
|
|
|
|
$TestModel = new Syfile();
|
|
|
|
$TestModel2 = new Item();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->findById(1);
|
2013-09-18 22:17:21 +00:00
|
|
|
$this->assertNull($result['Syfile']['item_count']);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel2->save(array(
|
|
|
|
'name' => 'Item 7',
|
|
|
|
'syfile_id' => 1,
|
|
|
|
'published' => false
|
|
|
|
));
|
|
|
|
|
|
|
|
$result = $TestModel->findById(1);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(2, $result['Syfile']['item_count']);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel2->delete(1);
|
|
|
|
$result = $TestModel->findById(1);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(1, $result['Syfile']['item_count']);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel2->id = 2;
|
|
|
|
$TestModel2->saveField('syfile_id', 1);
|
|
|
|
|
|
|
|
$result = $TestModel->findById(1);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(2, $result['Syfile']['item_count']);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->findById(2);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(0, $result['Syfile']['item_count']);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* Tests that counter caches are updated when records are added
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCounterCacheIncrease() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('CounterCacheUser', 'CounterCachePost');
|
|
|
|
$User = new CounterCacheUser();
|
|
|
|
$Post = new CounterCachePost();
|
|
|
|
$data = array('Post' => array(
|
2010-03-16 03:07:18 +00:00
|
|
|
'id' => 22,
|
2009-07-24 22:17:14 +00:00
|
|
|
'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;
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* Tests that counter caches are updated when records are deleted
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCounterCacheDecrease() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('CounterCacheUser', 'CounterCachePost');
|
|
|
|
$User = new CounterCacheUser();
|
|
|
|
$Post = new CounterCachePost();
|
|
|
|
|
2009-09-01 03:40:47 +00:00
|
|
|
$Post->delete(2);
|
2009-07-24 22:17:14 +00:00
|
|
|
$user = $User->find('first', array(
|
|
|
|
'conditions' => array('id' => 66),
|
|
|
|
'recursive' => -1
|
|
|
|
));
|
|
|
|
|
|
|
|
$result = $user[$User->alias]['post_count'];
|
|
|
|
$expected = 1;
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* Tests that counter caches are updated when foreign keys of counted records change
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCounterCacheUpdated() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$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);
|
|
|
|
|
2013-05-17 08:36:17 +00:00
|
|
|
$users = $User->find('all', array('order' => 'User.id'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(1, $users[0]['User']['post_count']);
|
|
|
|
$this->assertEquals(2, $users[1]['User']['post_count']);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* Test counter cache with models that use a non-standard (i.e. not using 'id')
|
|
|
|
* as their primary key.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCounterCacheWithNonstandardPrimaryKey() {
|
2009-07-26 09:59:51 +00:00
|
|
|
$this->loadFixtures(
|
2009-07-24 22:17:14 +00:00
|
|
|
'CounterCacheUserNonstandardPrimaryKey',
|
|
|
|
'CounterCachePostNonstandardPrimaryKey'
|
|
|
|
);
|
|
|
|
|
2009-07-26 09:59:51 +00:00
|
|
|
$User = new CounterCacheUserNonstandardPrimaryKey();
|
|
|
|
$Post = new CounterCachePostNonstandardPrimaryKey();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$data = $Post->find('first', array(
|
|
|
|
'conditions' => array('pid' => 1),
|
|
|
|
'recursive' => -1
|
|
|
|
));
|
|
|
|
$data[$Post->alias]['uid'] = 301;
|
|
|
|
$Post->save($data);
|
|
|
|
|
2013-05-17 08:36:17 +00:00
|
|
|
$users = $User->find('all', array('order' => 'User.uid'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(1, $users[0]['User']['post_count']);
|
|
|
|
$this->assertEquals(2, $users[1]['User']['post_count']);
|
2009-07-26 09:59:51 +00:00
|
|
|
}
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test Counter Cache With Self Joining table
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testCounterCacheWithSelfJoin() {
|
2011-05-31 00:49:46 +00:00
|
|
|
$this->skipIf($this->db instanceof Sqlite, 'SQLite 2.x does not support ALTER TABLE ADD COLUMN');
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$this->loadFixtures('CategoryThread');
|
2011-05-23 04:31:21 +00:00
|
|
|
$column = 'COLUMN ';
|
|
|
|
if ($this->db instanceof Sqlserver) {
|
|
|
|
$column = '';
|
|
|
|
}
|
|
|
|
$column .= $this->db->buildColumn(array('name' => 'child_count', 'type' => 'integer'));
|
2012-03-19 01:20:17 +00:00
|
|
|
$this->db->query('ALTER TABLE ' . $this->db->fullTableName('category_threads') . ' ADD ' . $column);
|
2011-07-18 04:15:22 +00:00
|
|
|
$this->db->flushMethodCache();
|
2010-06-02 05:00:52 +00:00
|
|
|
$Category = new CategoryThread();
|
2009-07-24 22:17:14 +00:00
|
|
|
$result = $Category->updateAll(array('CategoryThread.name' => "'updated'"), array('CategoryThread.parent_id' => 5));
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2010-06-02 05:00:52 +00:00
|
|
|
$Category = new CategoryThread();
|
2009-07-24 22:17:14 +00:00
|
|
|
$Category->belongsTo['ParentCategory']['counterCache'] = 'child_count';
|
|
|
|
$Category->updateCounterCache(array('parent_id' => 5));
|
2012-03-11 01:57:18 +00:00
|
|
|
$result = Hash::extract($Category->find('all', array('conditions' => array('CategoryThread.id' => 5))), '{n}.CategoryThread.child_count');
|
2011-05-23 04:31:21 +00:00
|
|
|
$expected = array(1);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testSaveWithCounterCacheScope method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSaveWithCounterCacheScope() {
|
2010-10-26 00:17:32 +00:00
|
|
|
$this->loadFixtures('Syfile', 'Item', 'Image', 'ItemsPortfolio', 'Portfolio');
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new Syfile();
|
|
|
|
$TestModel2 = new Item();
|
2009-07-24 22:17:14 +00:00
|
|
|
$TestModel2->belongsTo['Syfile']['counterCache'] = true;
|
|
|
|
$TestModel2->belongsTo['Syfile']['counterScope'] = array('published' => true);
|
|
|
|
|
|
|
|
$result = $TestModel->findById(1);
|
2013-09-18 22:17:21 +00:00
|
|
|
$this->assertNull($result['Syfile']['item_count']);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel2->save(array(
|
|
|
|
'name' => 'Item 7',
|
|
|
|
'syfile_id' => 1,
|
2011-12-01 07:21:31 +00:00
|
|
|
'published' => true
|
2009-07-24 22:17:14 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$result = $TestModel->findById(1);
|
2010-10-26 00:55:07 +00:00
|
|
|
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(1, $result['Syfile']['item_count']);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel2->id = 1;
|
|
|
|
$TestModel2->saveField('published', true);
|
|
|
|
$result = $TestModel->findById(1);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(2, $result['Syfile']['item_count']);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel2->save(array(
|
|
|
|
'id' => 1,
|
|
|
|
'syfile_id' => 1,
|
2011-12-01 07:21:31 +00:00
|
|
|
'published' => false
|
2009-07-24 22:17:14 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$result = $TestModel->findById(1);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(1, $result['Syfile']['item_count']);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2011-08-17 12:40:32 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
2013-05-17 08:36:17 +00:00
|
|
|
$result = $User->find('all', array('order' => 'User.id'));
|
2011-08-17 12:40:32 +00:00
|
|
|
$this->assertEquals(2, $result[0]['User']['post_count']);
|
|
|
|
$this->assertEquals(1, $result[1]['User']['posts_published']);
|
|
|
|
}
|
|
|
|
|
2013-01-29 03:40:42 +00:00
|
|
|
/**
|
|
|
|
* 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']);
|
|
|
|
}
|
|
|
|
|
2009-11-04 17:36:17 +00:00
|
|
|
/**
|
|
|
|
* test that beforeValidate returning false can abort saves.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testBeforeValidateSaveAbortion() {
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->loadFixtures('Post');
|
|
|
|
$Model = new CallbackPostTestModel();
|
2009-11-04 17:36:17 +00:00
|
|
|
$Model->beforeValidateReturn = false;
|
2009-11-22 22:56:46 +00:00
|
|
|
|
2009-11-04 17:36:17 +00:00
|
|
|
$data = array(
|
|
|
|
'title' => 'new article',
|
|
|
|
'body' => 'this is some text.'
|
|
|
|
);
|
|
|
|
$Model->create();
|
|
|
|
$result = $Model->save($data);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
2011-12-06 20:52:48 +00:00
|
|
|
|
2009-11-04 17:36:17 +00:00
|
|
|
/**
|
|
|
|
* test that beforeSave returning false can abort saves.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testBeforeSaveSaveAbortion() {
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->loadFixtures('Post');
|
|
|
|
$Model = new CallbackPostTestModel();
|
2009-11-04 17:36:17 +00:00
|
|
|
$Model->beforeSaveReturn = false;
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'title' => 'new article',
|
|
|
|
'body' => 'this is some text.'
|
|
|
|
);
|
|
|
|
$Model->create();
|
|
|
|
$result = $Model->save($data);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2014-06-03 07:37:17 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2014-10-10 14:52:01 +00:00
|
|
|
* @return bool false to stop event propagation
|
2014-06-03 07:37:17 +00:00
|
|
|
*/
|
|
|
|
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);
|
2014-07-03 17:43:55 +00:00
|
|
|
|
2014-06-03 07:37:17 +00:00
|
|
|
// 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
|
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$PostModel->validate = array('title' => 'notBlank');
|
2014-06-03 07:37:17 +00:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testSaveField method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSaveField() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('Article');
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new Article();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->id = 1;
|
|
|
|
$result = $TestModel->saveField('title', 'New First Article');
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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'
|
|
|
|
));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->id = 1;
|
|
|
|
$result = $TestModel->saveField('title', '');
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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']);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->id = 1;
|
|
|
|
$TestModel->set('body', 'Messed up data');
|
2010-06-02 05:00:52 +00:00
|
|
|
$result = $TestModel->saveField('title', 'First Article');
|
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
$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'
|
|
|
|
));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->recursive = -1;
|
2012-01-25 01:32:48 +00:00
|
|
|
$TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->id = 1;
|
|
|
|
$result = $TestModel->saveField('title', '', true);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
2012-01-25 01:32:48 +00:00
|
|
|
$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);
|
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('Node', 'Dependency');
|
2010-06-02 05:00:52 +00:00
|
|
|
$Node = new Node();
|
2009-07-24 22:17:14 +00:00
|
|
|
$Node->set('id', 1);
|
|
|
|
$result = $Node->read();
|
2012-04-04 23:33:57 +00:00
|
|
|
$this->assertEquals(array('Second'), Hash::extract($result, 'ParentNode.{n}.name'));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$Node->saveField('state', 10);
|
|
|
|
$result = $Node->read();
|
2012-04-04 23:33:57 +00:00
|
|
|
$this->assertEquals(array('Second'), Hash::extract($result, 'ParentNode.{n}.name'));
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testSaveWithCreate method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSaveWithCreate() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures(
|
|
|
|
'User',
|
|
|
|
'Article',
|
|
|
|
'User',
|
|
|
|
'Comment',
|
|
|
|
'Tag',
|
|
|
|
'ArticlesTag',
|
|
|
|
'Attachment'
|
|
|
|
);
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new User();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$data = array('User' => array(
|
|
|
|
'user' => 'user',
|
|
|
|
'password' => ''
|
|
|
|
));
|
|
|
|
$result = $TestModel->save($data);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
$this->assertTrue(!empty($TestModel->validationErrors));
|
|
|
|
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new Article();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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'
|
|
|
|
));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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'));
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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'
|
|
|
|
));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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'
|
|
|
|
)));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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'
|
|
|
|
)));
|
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2010-05-07 02:24:11 +00:00
|
|
|
/**
|
|
|
|
* test that a null Id doesn't cause errors
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSaveWithNullId() {
|
2010-05-12 02:40:56 +00:00
|
|
|
$this->loadFixtures('User');
|
2010-06-02 05:00:52 +00:00
|
|
|
$User = new User();
|
2010-05-07 02:24:11 +00:00
|
|
|
$User->read(null, 1);
|
|
|
|
$User->data['User']['id'] = null;
|
2010-06-02 05:00:52 +00:00
|
|
|
$result = $User->save(array('password' => 'test'));
|
|
|
|
$this->assertFalse(empty($result));
|
2010-05-07 02:24:11 +00:00
|
|
|
$this->assertTrue($User->id > 0);
|
2010-05-08 02:23:18 +00:00
|
|
|
|
2010-06-02 05:00:52 +00:00
|
|
|
$User->read(null, 2);
|
2010-05-08 02:23:18 +00:00
|
|
|
$User->data['User']['id'] = null;
|
2010-06-02 05:00:52 +00:00
|
|
|
$result = $User->save(array('password' => 'test'));
|
|
|
|
$this->assertFalse(empty($result));
|
2010-05-08 02:23:18 +00:00
|
|
|
$this->assertTrue($User->id > 0);
|
2010-05-12 03:01:40 +00:00
|
|
|
|
|
|
|
$User->data['User'] = array('password' => 'something');
|
2010-06-02 05:00:52 +00:00
|
|
|
$result = $User->save();
|
|
|
|
$this->assertFalse(empty($result));
|
2010-05-12 03:01:40 +00:00
|
|
|
$result = $User->read();
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('something', $User->data['User']['password']);
|
2010-05-07 02:24:11 +00:00
|
|
|
}
|
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testSaveWithSet method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSaveWithSet() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('Article');
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new Article();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
// 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);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
// 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'
|
|
|
|
));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
// 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);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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'
|
|
|
|
));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
// 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'
|
|
|
|
));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
// 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();
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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'
|
|
|
|
));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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'
|
|
|
|
));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$data = array('Article' => array('id' => '5', 'title' => 'Fifth Article - New Title 5'));
|
|
|
|
$result = ($TestModel->set($data) && $TestModel->save());
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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'
|
|
|
|
));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->recursive = -1;
|
2012-03-14 14:01:19 +00:00
|
|
|
$result = $TestModel->find('all', array(
|
|
|
|
'fields' => array('id', 'title'),
|
|
|
|
'order' => array('Article.id' => 'ASC')
|
|
|
|
));
|
2009-07-24 22:17:14 +00:00
|
|
|
$expected = array(
|
2011-12-01 07:21:31 +00:00
|
|
|
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'))
|
2009-07-24 22:17:14 +00:00
|
|
|
);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testSaveWithNonExistentFields method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSaveWithNonExistentFields() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('Article');
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new Article();
|
2009-07-24 22:17:14 +00:00
|
|
|
$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);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'user_id' => '1',
|
|
|
|
'non_existent' => 'This field does not exist',
|
2012-02-23 23:29:53 +00:00
|
|
|
'title' => 'Fifth Article - New Title',
|
|
|
|
'body' => 'Fifth Article Body',
|
2009-07-24 22:17:14 +00:00
|
|
|
'published' => 'N'
|
|
|
|
);
|
|
|
|
$result = $TestModel->create() && $TestModel->save($data);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$expected = array('Article' => array(
|
|
|
|
'id' => '5',
|
|
|
|
'user_id' => '1',
|
2012-02-23 23:29:53 +00:00
|
|
|
'title' => 'Fifth Article - New Title',
|
|
|
|
'body' => 'Fifth Article Body',
|
2009-07-24 22:17:14 +00:00
|
|
|
'published' => 'N'
|
|
|
|
));
|
|
|
|
$result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testSaveFromXml method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSaveFromXml() {
|
2010-10-17 15:59:49 +00:00
|
|
|
$this->markTestSkipped('This feature needs to be fixed or dropped');
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('Article');
|
2010-12-09 05:55:24 +00:00
|
|
|
App::uses('Xml', 'Utility');
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$Article = new Article();
|
2010-10-18 00:47:43 +00:00
|
|
|
$result = $Article->save(Xml::build('<article title="test xml" user_id="5" />'));
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2011-02-26 00:26:35 +00:00
|
|
|
$results = $Article->find('first', array('conditions' => array('Article.title' => 'test xml')));
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($results));
|
2010-10-18 00:47:43 +00:00
|
|
|
|
|
|
|
$result = $Article->save(Xml::build('<article><title>testing</title><user_id>6</user_id></article>'));
|
|
|
|
$this->assertFalse(empty($result));
|
2011-02-26 00:26:35 +00:00
|
|
|
$results = $Article->find('first', array('conditions' => array('Article.title' => 'testing')));
|
2010-10-18 00:47:43 +00:00
|
|
|
$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));
|
2011-02-26 00:26:35 +00:00
|
|
|
$results = $Article->find('first', array('conditions' => array('Article.title' => 'testing with DOMDocument')));
|
2010-10-18 00:47:43 +00:00
|
|
|
$this->assertFalse(empty($results));
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testSaveHabtm method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSaveHabtm() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new Article();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Article' => array(
|
|
|
|
'id' => '2',
|
|
|
|
'title' => 'New Second Article'
|
|
|
|
),
|
|
|
|
'Tag' => array('Tag' => array(1, 2))
|
|
|
|
);
|
|
|
|
|
2010-06-02 05:00:52 +00:00
|
|
|
$result = $TestModel->set($data);
|
|
|
|
$this->assertFalse(empty($result));
|
|
|
|
$result = $TestModel->save();
|
|
|
|
$this->assertFalse(empty($result));
|
2012-08-23 22:49:00 +00:00
|
|
|
$this->assertEquals($data['Tag'], $result['Tag']);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
|
2011-02-26 00:26:35 +00:00
|
|
|
$result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
|
2009-07-24 22:17:14 +00:00
|
|
|
$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'
|
|
|
|
)));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$data = array('Article' => array('id' => '2'), 'Tag' => array('Tag' => array(2, 3)));
|
|
|
|
$result = $TestModel->set($data);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->save();
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->unbindModel(array(
|
|
|
|
'belongsTo' => array('User'),
|
|
|
|
'hasMany' => array('Comment')
|
|
|
|
));
|
2011-02-26 00:26:35 +00:00
|
|
|
$result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
|
2009-07-24 22:17:14 +00:00
|
|
|
$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'
|
|
|
|
)));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$data = array('Tag' => array('Tag' => array(1, 2, 3)));
|
|
|
|
|
|
|
|
$result = $TestModel->set($data);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->save();
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->unbindModel(array(
|
|
|
|
'belongsTo' => array('User'),
|
|
|
|
'hasMany' => array('Comment')
|
|
|
|
));
|
2011-02-26 00:26:35 +00:00
|
|
|
$result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
|
2009-07-24 22:17:14 +00:00
|
|
|
$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'
|
|
|
|
)));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$data = array('Tag' => array('Tag' => array()));
|
|
|
|
$result = $TestModel->set($data);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->save();
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$data = array('Tag' => array('Tag' => ''));
|
|
|
|
$result = $TestModel->set($data);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->save();
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->unbindModel(array(
|
|
|
|
'belongsTo' => array('User'),
|
|
|
|
'hasMany' => array('Comment')
|
|
|
|
));
|
2011-02-26 00:26:35 +00:00
|
|
|
$result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
|
2009-07-24 22:17:14 +00:00
|
|
|
$expected = array(
|
|
|
|
'Article' => array(
|
|
|
|
'id' => '2',
|
|
|
|
'user_id' => '3',
|
|
|
|
'title' => 'New Second Article',
|
|
|
|
'body' => 'Second Article Body'
|
|
|
|
),
|
|
|
|
'Tag' => array()
|
|
|
|
);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$data = array('Tag' => array('Tag' => array(2, 3)));
|
|
|
|
$result = $TestModel->set($data);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->save();
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->unbindModel(array(
|
|
|
|
'belongsTo' => array('User'),
|
|
|
|
'hasMany' => array('Comment')
|
|
|
|
));
|
2011-02-26 00:26:35 +00:00
|
|
|
$result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
|
2009-07-24 22:17:14 +00:00
|
|
|
$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'
|
|
|
|
)));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Tag' => array(
|
|
|
|
'Tag' => array(1, 2)
|
|
|
|
),
|
|
|
|
'Article' => array(
|
|
|
|
'id' => '2',
|
|
|
|
'title' => 'New Second Article'
|
|
|
|
));
|
2010-06-02 05:00:52 +00:00
|
|
|
$result = $TestModel->set($data);
|
|
|
|
$this->assertFalse(empty($result));
|
|
|
|
$result = $TestModel->save();
|
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->unbindModel(array(
|
|
|
|
'belongsTo' => array('User'),
|
|
|
|
'hasMany' => array('Comment')
|
|
|
|
));
|
2011-02-26 00:26:35 +00:00
|
|
|
$result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
|
2009-07-24 22:17:14 +00:00
|
|
|
$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'
|
|
|
|
)));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Tag' => array(
|
|
|
|
'Tag' => array(1, 2)
|
|
|
|
),
|
|
|
|
'Article' => array(
|
|
|
|
'id' => '2',
|
|
|
|
'title' => 'New Second Article Title'
|
|
|
|
));
|
|
|
|
$result = $TestModel->set($data);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
|
|
|
$result = $TestModel->save();
|
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->unbindModel(array(
|
|
|
|
'belongsTo' => array('User'),
|
|
|
|
'hasMany' => array('Comment')
|
|
|
|
));
|
2011-02-26 00:26:35 +00:00
|
|
|
$result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
|
2009-07-24 22:17:14 +00:00
|
|
|
$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(
|
|
|
|
'id' => '2',
|
|
|
|
'tag' => 'tag2',
|
|
|
|
'created' => '2007-03-18 12:24:23',
|
|
|
|
'updated' => '2007-03-18 12:26:31'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Tag' => array(
|
|
|
|
'Tag' => array(2, 3)
|
|
|
|
),
|
|
|
|
'Article' => array(
|
|
|
|
'id' => '2',
|
|
|
|
'title' => 'Changed Second Article'
|
|
|
|
));
|
2010-06-02 05:00:52 +00:00
|
|
|
$result = $TestModel->set($data);
|
|
|
|
$this->assertFalse(empty($result));
|
|
|
|
$result = $TestModel->save();
|
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->unbindModel(array(
|
|
|
|
'belongsTo' => array('User'),
|
|
|
|
'hasMany' => array('Comment')
|
|
|
|
));
|
2011-02-26 00:26:35 +00:00
|
|
|
$result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
|
2009-07-24 22:17:14 +00:00
|
|
|
$expected = array(
|
|
|
|
'Article' => array(
|
|
|
|
'id' => '2',
|
|
|
|
'user_id' => '3',
|
|
|
|
'title' => 'Changed 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'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Tag' => array(
|
|
|
|
'Tag' => array(1, 3)
|
|
|
|
),
|
|
|
|
'Article' => array('id' => '2'),
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $TestModel->set($data);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->save();
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->unbindModel(array(
|
|
|
|
'belongsTo' => array('User'),
|
|
|
|
'hasMany' => array('Comment')
|
|
|
|
));
|
2011-02-26 00:26:35 +00:00
|
|
|
$result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
|
2009-07-24 22:17:14 +00:00
|
|
|
$expected = array(
|
|
|
|
'Article' => array(
|
|
|
|
'id' => '2',
|
|
|
|
'user_id' => '3',
|
|
|
|
'title' => 'Changed 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' => '3',
|
|
|
|
'tag' => 'tag3',
|
|
|
|
'created' => '2007-03-18 12:26:23',
|
|
|
|
'updated' => '2007-03-18 12:28:31'
|
|
|
|
)));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Article' => array(
|
|
|
|
'id' => 10,
|
|
|
|
'user_id' => '2',
|
|
|
|
'title' => 'New Article With Tags and fieldList',
|
|
|
|
'body' => 'New Article Body with Tags and fieldList',
|
|
|
|
'created' => '2007-03-18 14:55:23',
|
|
|
|
'updated' => '2007-03-18 14:57:31'
|
|
|
|
),
|
|
|
|
'Tag' => array(
|
|
|
|
'Tag' => array(1, 2, 3)
|
2012-03-19 01:20:17 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $TestModel->create()
|
2009-07-24 22:17:14 +00:00
|
|
|
&& $TestModel->save($data, true, array('user_id', 'title', 'published'));
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2012-03-19 01:20:17 +00:00
|
|
|
$TestModel->unbindModel(array(
|
|
|
|
'belongsTo' => array('User'),
|
|
|
|
'hasMany' => array('Comment')
|
|
|
|
));
|
2009-07-24 22:17:14 +00:00
|
|
|
$result = $TestModel->read();
|
|
|
|
$expected = array(
|
|
|
|
'Article' => array(
|
|
|
|
'id' => 4,
|
|
|
|
'user_id' => 2,
|
|
|
|
'title' => 'New Article With Tags and fieldList',
|
|
|
|
'body' => '',
|
|
|
|
'published' => 'N',
|
|
|
|
'created' => '',
|
|
|
|
'updated' => ''
|
|
|
|
),
|
|
|
|
'Tag' => array(
|
|
|
|
0 => array(
|
|
|
|
'id' => 1,
|
|
|
|
'tag' => 'tag1',
|
|
|
|
'created' => '2007-03-18 12:22:23',
|
|
|
|
'updated' => '2007-03-18 12:24:31'
|
|
|
|
),
|
|
|
|
1 => array(
|
|
|
|
'id' => 2,
|
|
|
|
'tag' => 'tag2',
|
|
|
|
'created' => '2007-03-18 12:24:23',
|
|
|
|
'updated' => '2007-03-18 12:26:31'
|
|
|
|
),
|
|
|
|
2 => array(
|
|
|
|
'id' => 3,
|
|
|
|
'tag' => 'tag3',
|
|
|
|
'created' => '2007-03-18 12:26:23',
|
|
|
|
'updated' => '2007-03-18 12:28:31'
|
|
|
|
)));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$this->loadFixtures('JoinA', 'JoinC', 'JoinAC', 'JoinB', 'JoinAB');
|
|
|
|
$TestModel = new JoinA();
|
2010-07-14 21:28:12 +00:00
|
|
|
$TestModel->hasBelongsToMany = array('JoinC' => array('unique' => true));
|
2009-07-24 22:17:14 +00:00
|
|
|
$data = array(
|
|
|
|
'JoinA' => array(
|
|
|
|
'id' => 1,
|
|
|
|
'name' => 'Join A 1',
|
|
|
|
'body' => 'Join A 1 Body',
|
|
|
|
),
|
|
|
|
'JoinC' => array(
|
|
|
|
'JoinC' => array(
|
|
|
|
array('join_c_id' => 2, 'other' => 'new record'),
|
|
|
|
array('join_c_id' => 3, 'other' => 'new record')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$TestModel->save($data);
|
|
|
|
$result = $TestModel->read(null, 1);
|
|
|
|
$expected = array(4, 5);
|
2012-04-04 23:33:57 +00:00
|
|
|
$this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.id'));
|
2009-07-24 22:17:14 +00:00
|
|
|
$expected = array('new record', 'new record');
|
2012-04-04 23:33:57 +00:00
|
|
|
$this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.other'));
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2014-08-28 22:49:30 +00:00
|
|
|
/**
|
|
|
|
* test that saving HABTM with an empty array will clear existing HABTM if
|
|
|
|
* unique is true
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveHabtmEmptyData() {
|
|
|
|
$this->loadFixtures('Node', 'Dependency');
|
|
|
|
$Node = new Node();
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Node' => array('name' => 'New First')
|
|
|
|
);
|
|
|
|
$Node->id = 1;
|
|
|
|
$Node->save($data);
|
|
|
|
|
|
|
|
$node = $Node->find('first', array(
|
|
|
|
'conditions' => array('Node.id' => 1),
|
|
|
|
'contain' => array('ParentNode')
|
|
|
|
));
|
|
|
|
|
|
|
|
$result = Hash::extract($node, 'ParentNode.{n}.id');
|
|
|
|
$expected = array(2);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'ParentNode' => array()
|
|
|
|
);
|
|
|
|
$Node->id = 1;
|
|
|
|
$Node->save($data);
|
|
|
|
|
|
|
|
$node = $Node->find('first', array(
|
|
|
|
'conditions' => array('Node.id' => 1),
|
|
|
|
'contain' => array('ParentNode')
|
|
|
|
));
|
|
|
|
|
|
|
|
$result = Hash::extract($node, 'ParentNode.{n}.id');
|
|
|
|
$expected = array();
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
2011-11-15 15:47:25 +00:00
|
|
|
/**
|
|
|
|
* testSaveHabtmNoPrimaryData method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveHabtmNoPrimaryData() {
|
|
|
|
$this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
|
|
|
|
$TestModel = new Article();
|
|
|
|
|
|
|
|
$TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')), false);
|
|
|
|
$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'
|
|
|
|
),
|
|
|
|
'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'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2011-11-15 15:47:25 +00:00
|
|
|
|
|
|
|
$TestModel->id = 2;
|
|
|
|
$data = array('Tag' => array('Tag' => array(2)));
|
2013-04-19 17:37:23 +00:00
|
|
|
$result = $TestModel->save($data);
|
|
|
|
|
|
|
|
$this->assertEquals($data['Tag'], $result['Tag']);
|
2011-11-15 15:47:25 +00:00
|
|
|
|
|
|
|
$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',
|
2012-03-28 15:49:41 +00:00
|
|
|
'updated' => self::date()
|
2011-11-15 15:47:25 +00:00
|
|
|
),
|
|
|
|
'Tag' => array(
|
|
|
|
array(
|
|
|
|
'id' => '2',
|
|
|
|
'tag' => 'tag2',
|
|
|
|
'created' => '2007-03-18 12:24:23',
|
|
|
|
'updated' => '2007-03-18 12:26:31'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2011-11-15 15:47:25 +00:00
|
|
|
|
|
|
|
$this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio');
|
|
|
|
$TestModel = new Portfolio();
|
|
|
|
$result = $TestModel->findById(2);
|
|
|
|
$expected = array(
|
|
|
|
'Portfolio' => array(
|
|
|
|
'id' => 2,
|
|
|
|
'seller_id' => 1,
|
|
|
|
'name' => 'Portfolio 2'
|
|
|
|
),
|
|
|
|
'Item' => array(
|
|
|
|
array(
|
|
|
|
'id' => 2,
|
|
|
|
'syfile_id' => 2,
|
|
|
|
'published' => '',
|
|
|
|
'name' => 'Item 2',
|
|
|
|
'ItemsPortfolio' => array(
|
|
|
|
'id' => 2,
|
|
|
|
'item_id' => 2,
|
|
|
|
'portfolio_id' => 2
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => 6,
|
|
|
|
'syfile_id' => 6,
|
|
|
|
'published' => '',
|
|
|
|
'name' => 'Item 6',
|
|
|
|
'ItemsPortfolio' => array(
|
|
|
|
'id' => 6,
|
|
|
|
'item_id' => 6,
|
|
|
|
'portfolio_id' => 2
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2011-11-15 15:47:25 +00:00
|
|
|
|
|
|
|
$data = array('Item' => array('Item' => array(1, 2)));
|
|
|
|
$TestModel->id = 2;
|
2014-05-28 16:17:16 +00:00
|
|
|
$result = $TestModel->save($data);
|
|
|
|
$this->assertTrue((bool)$result);
|
|
|
|
|
2011-11-15 15:47:25 +00:00
|
|
|
$result = $TestModel->findById(2);
|
2012-03-11 01:57:18 +00:00
|
|
|
$result['Item'] = Hash::sort($result['Item'], '{n}.id', 'asc');
|
2011-11-15 15:47:25 +00:00
|
|
|
$expected = array(
|
|
|
|
'Portfolio' => array(
|
|
|
|
'id' => 2,
|
|
|
|
'seller_id' => 1,
|
|
|
|
'name' => 'Portfolio 2'
|
|
|
|
),
|
|
|
|
'Item' => array(
|
|
|
|
array(
|
2011-11-15 16:18:27 +00:00
|
|
|
'id' => 1,
|
|
|
|
'syfile_id' => 1,
|
2011-11-15 15:47:25 +00:00
|
|
|
'published' => '',
|
2011-11-15 16:18:27 +00:00
|
|
|
'name' => 'Item 1',
|
2011-11-15 15:47:25 +00:00
|
|
|
'ItemsPortfolio' => array(
|
2011-11-15 16:18:27 +00:00
|
|
|
'id' => 7,
|
|
|
|
'item_id' => 1,
|
2011-11-15 15:47:25 +00:00
|
|
|
'portfolio_id' => 2
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array(
|
2011-11-15 16:18:27 +00:00
|
|
|
'id' => 2,
|
|
|
|
'syfile_id' => 2,
|
2011-11-15 15:47:25 +00:00
|
|
|
'published' => '',
|
2011-11-15 16:18:27 +00:00
|
|
|
'name' => 'Item 2',
|
2011-11-15 15:47:25 +00:00
|
|
|
'ItemsPortfolio' => array(
|
2011-11-15 16:18:27 +00:00
|
|
|
'id' => 8,
|
|
|
|
'item_id' => 2,
|
2011-11-15 15:47:25 +00:00
|
|
|
'portfolio_id' => 2
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2011-11-15 15:47:25 +00:00
|
|
|
}
|
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testSaveHabtmCustomKeys method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSaveHabtmCustomKeys() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('Story', 'StoriesTag', 'Tag');
|
2010-06-02 05:00:52 +00:00
|
|
|
$Story = new Story();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Story' => array('story' => '1'),
|
|
|
|
'Tag' => array(
|
|
|
|
'Tag' => array(2, 3)
|
|
|
|
));
|
|
|
|
$result = $Story->set($data);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $Story->save();
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2010-03-16 21:30:20 +00:00
|
|
|
$result = $Story->find('all', array('order' => array('Story.story')));
|
2009-07-24 22:17:14 +00:00
|
|
|
$expected = array(
|
|
|
|
array(
|
|
|
|
'Story' => array(
|
|
|
|
'story' => 1,
|
|
|
|
'title' => 'First Story'
|
|
|
|
),
|
|
|
|
'Tag' => array(
|
|
|
|
array(
|
|
|
|
'id' => 2,
|
|
|
|
'tag' => 'tag2',
|
|
|
|
'created' => '2007-03-18 12:24:23',
|
|
|
|
'updated' => '2007-03-18 12:26:31'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => 3,
|
|
|
|
'tag' => 'tag3',
|
|
|
|
'created' => '2007-03-18 12:26:23',
|
|
|
|
'updated' => '2007-03-18 12:28:31'
|
|
|
|
))),
|
|
|
|
array(
|
|
|
|
'Story' => array(
|
|
|
|
'story' => 2,
|
|
|
|
'title' => 'Second Story'
|
|
|
|
),
|
|
|
|
'Tag' => array()
|
|
|
|
));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-11-22 22:56:46 +00:00
|
|
|
/**
|
2010-07-19 22:34:08 +00:00
|
|
|
* test that saving habtm records respects conditions set in the 'conditions' key
|
2009-11-22 22:56:46 +00:00
|
|
|
* for the association.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testHabtmSaveWithConditionsInAssociation() {
|
2009-11-22 22:56:46 +00:00
|
|
|
$this->loadFixtures('JoinThing', 'Something', 'SomethingElse');
|
2010-06-02 05:00:52 +00:00
|
|
|
$Something = new Something();
|
2009-11-22 22:56:46 +00:00
|
|
|
$Something->unbindModel(array('hasAndBelongsToMany' => array('SomethingElse')), false);
|
|
|
|
|
|
|
|
$Something->bindModel(array(
|
|
|
|
'hasAndBelongsToMany' => array(
|
|
|
|
'DoomedSomethingElse' => array(
|
|
|
|
'className' => 'SomethingElse',
|
|
|
|
'joinTable' => 'join_things',
|
2010-11-29 03:18:34 +00:00
|
|
|
'conditions' => array('JoinThing.doomed' => true),
|
2009-11-22 22:56:46 +00:00
|
|
|
'unique' => true
|
|
|
|
),
|
|
|
|
'NotDoomedSomethingElse' => array(
|
|
|
|
'className' => 'SomethingElse',
|
|
|
|
'joinTable' => 'join_things',
|
|
|
|
'conditions' => array('JoinThing.doomed' => 0),
|
|
|
|
'unique' => true
|
|
|
|
)
|
|
|
|
)
|
|
|
|
), false);
|
|
|
|
$result = $Something->read(null, 1);
|
|
|
|
$this->assertTrue(empty($result['NotDoomedSomethingElse']));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(1, count($result['DoomedSomethingElse']));
|
2009-11-22 22:56:46 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Something' => array('id' => 1),
|
|
|
|
'NotDoomedSomethingElse' => array(
|
|
|
|
'NotDoomedSomethingElse' => array(
|
|
|
|
array('something_else_id' => 2, 'doomed' => 0),
|
|
|
|
array('something_else_id' => 3, 'doomed' => 0)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$Something->create($data);
|
|
|
|
$result = $Something->save();
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-11-22 22:56:46 +00:00
|
|
|
|
|
|
|
$result = $Something->read(null, 1);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(2, count($result['NotDoomedSomethingElse']));
|
|
|
|
$this->assertEquals(1, count($result['DoomedSomethingElse']));
|
2009-11-22 22:56:46 +00:00
|
|
|
}
|
2011-12-06 20:52:48 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testHabtmSaveKeyResolution method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testHabtmSaveKeyResolution() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('Apple', 'Device', 'ThePaperMonkies');
|
2010-06-02 05:00:52 +00:00
|
|
|
$ThePaper = new ThePaper();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$ThePaper->id = 1;
|
|
|
|
$ThePaper->save(array('Monkey' => array(2, 3)));
|
|
|
|
|
|
|
|
$result = $ThePaper->findById(1);
|
|
|
|
$expected = array(
|
|
|
|
array(
|
|
|
|
'id' => '2',
|
|
|
|
'device_type_id' => '1',
|
|
|
|
'name' => 'Device 2',
|
|
|
|
'typ' => '1'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => '3',
|
|
|
|
'device_type_id' => '1',
|
|
|
|
'name' => 'Device 3',
|
|
|
|
'typ' => '2'
|
|
|
|
));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $result['Monkey']);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$ThePaper->id = 2;
|
|
|
|
$ThePaper->save(array('Monkey' => array(1, 2, 3)));
|
|
|
|
|
|
|
|
$result = $ThePaper->findById(2);
|
|
|
|
$expected = 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'
|
|
|
|
));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $result['Monkey']);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$ThePaper->id = 2;
|
|
|
|
$ThePaper->save(array('Monkey' => array(1, 3)));
|
|
|
|
|
|
|
|
$result = $ThePaper->findById(2);
|
|
|
|
$expected = array(
|
|
|
|
array(
|
|
|
|
'id' => '1',
|
|
|
|
'device_type_id' => '1',
|
|
|
|
'name' => 'Device 1',
|
|
|
|
'typ' => '1'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => '3',
|
|
|
|
'device_type_id' => '1',
|
|
|
|
'name' => 'Device 3',
|
|
|
|
'typ' => '2'
|
|
|
|
));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $result['Monkey']);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $ThePaper->findById(1);
|
|
|
|
$expected = array(
|
|
|
|
array(
|
|
|
|
'id' => '2',
|
|
|
|
'device_type_id' => '1',
|
|
|
|
'name' => 'Device 2',
|
|
|
|
'typ' => '1'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => '3',
|
|
|
|
'device_type_id' => '1',
|
|
|
|
'name' => 'Device 3',
|
|
|
|
'typ' => '2'
|
|
|
|
));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $result['Monkey']);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testCreationOfEmptyRecord method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCreationOfEmptyRecord() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('Author');
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new Author();
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(4, $TestModel->find('count'));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->deleteAll(true, false, false);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(0, $TestModel->find('count'));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->save();
|
|
|
|
$this->assertTrue(isset($result['Author']['created']));
|
|
|
|
$this->assertTrue(isset($result['Author']['updated']));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(1, $TestModel->find('count'));
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testCreateWithPKFiltering method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCreateWithPKFiltering() {
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new Article();
|
2009-07-24 22:17:14 +00:00
|
|
|
$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'
|
|
|
|
));
|
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(5, $TestModel->id);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->create($data, true);
|
|
|
|
$expected = array(
|
|
|
|
'Article' => array(
|
|
|
|
'published' => 'N',
|
|
|
|
'id' => false,
|
|
|
|
'user_id' => 2,
|
|
|
|
'title' => 'My article',
|
|
|
|
'body' => 'Some text'
|
|
|
|
));
|
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
$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'
|
|
|
|
));
|
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
$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'
|
|
|
|
));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(6, $TestModel->id);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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'
|
|
|
|
));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->assertFalse($TestModel->id);
|
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testCreationWithMultipleData method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCreationWithMultipleData() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('Article', 'Comment');
|
2010-06-02 05:00:52 +00:00
|
|
|
$Article = new Article();
|
|
|
|
$Comment = new Comment();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$articles = $Article->find('all', array(
|
2013-06-09 15:39:48 +00:00
|
|
|
'fields' => array('id', 'title'),
|
2012-03-14 00:12:35 +00:00
|
|
|
'recursive' => -1,
|
|
|
|
'order' => array('Article.id' => 'ASC')
|
2009-07-24 22:17:14 +00:00
|
|
|
));
|
2012-03-14 00:12:35 +00:00
|
|
|
$expected = array(
|
2009-07-24 22:17:14 +00:00
|
|
|
array('Article' => array(
|
|
|
|
'id' => 1,
|
|
|
|
'title' => 'First Article'
|
|
|
|
)),
|
|
|
|
array('Article' => array(
|
|
|
|
'id' => 2,
|
|
|
|
'title' => 'Second Article'
|
|
|
|
)),
|
|
|
|
array('Article' => array(
|
|
|
|
'id' => 3,
|
|
|
|
'title' => 'Third Article'
|
2012-03-14 00:12:35 +00:00
|
|
|
)));
|
|
|
|
$this->assertEquals($expected, $articles);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2012-03-14 00:12:35 +00:00
|
|
|
$comments = $Comment->find('all', array(
|
2013-06-09 15:39:48 +00:00
|
|
|
'fields' => array('id', 'article_id', 'user_id', 'comment', 'published'),
|
2012-03-14 00:12:35 +00:00
|
|
|
'recursive' => -1,
|
|
|
|
'order' => array('Comment.id' => 'ASC')
|
|
|
|
));
|
|
|
|
$expected = array(
|
2009-07-24 22:17:14 +00:00
|
|
|
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'
|
2012-03-14 00:12:35 +00:00
|
|
|
)));
|
|
|
|
$this->assertEquals($expected, $comments);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2012-03-14 00:12:35 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
$result = $Comment->save();
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$articles = $Article->find('all', array(
|
2013-06-09 15:39:48 +00:00
|
|
|
'fields' => array('id', 'title'),
|
2012-03-14 00:12:35 +00:00
|
|
|
'recursive' => -1,
|
|
|
|
'order' => array('Article.id' => 'ASC')
|
2009-07-24 22:17:14 +00:00
|
|
|
));
|
2012-03-14 00:12:35 +00:00
|
|
|
$expected = array(
|
2009-07-24 22:17:14 +00:00
|
|
|
array('Article' => array(
|
|
|
|
'id' => 1,
|
|
|
|
'title' => 'First Article'
|
|
|
|
)),
|
|
|
|
array('Article' => array(
|
|
|
|
'id' => 2,
|
|
|
|
'title' => 'Second Article'
|
|
|
|
)),
|
|
|
|
array('Article' => array(
|
|
|
|
'id' => 3,
|
|
|
|
'title' => 'Third Article'
|
2012-03-14 00:12:35 +00:00
|
|
|
)));
|
|
|
|
$this->assertEquals($expected, $articles);
|
|
|
|
|
|
|
|
$comments = $Comment->find('all', array(
|
2013-06-09 15:39:48 +00:00
|
|
|
'fields' => array('id', 'article_id', 'user_id', 'comment', 'published'),
|
2012-03-14 00:12:35 +00:00
|
|
|
'recursive' => -1,
|
|
|
|
'order' => array('Comment.id' => 'ASC')
|
|
|
|
));
|
|
|
|
$expected = array(
|
2009-07-24 22:17:14 +00:00
|
|
|
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'
|
2012-03-14 00:12:35 +00:00
|
|
|
)));
|
|
|
|
$this->assertEquals($expected, $comments);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testCreationWithMultipleDataSameModel method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCreationWithMultipleDataSameModel() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('Article');
|
2010-06-02 05:00:52 +00:00
|
|
|
$Article = new Article();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $Article->field('title', array('id' => 1));
|
2012-03-14 00:12:35 +00:00
|
|
|
$this->assertEquals('First Article', $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $Article->getInsertID();
|
|
|
|
$this->assertTrue(!empty($result));
|
|
|
|
|
|
|
|
$result = $Article->field('title', array('id' => 1));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('First Article', $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$articles = $Article->find('all', array(
|
2013-06-09 15:39:48 +00:00
|
|
|
'fields' => array('id', 'title'),
|
2012-03-14 00:12:35 +00:00
|
|
|
'recursive' => -1,
|
|
|
|
'order' => array('Article.id' => 'ASC')
|
2009-07-24 22:17:14 +00:00
|
|
|
));
|
2012-03-14 00:12:35 +00:00
|
|
|
$expected = array(
|
2009-07-24 22:17:14 +00:00
|
|
|
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'
|
2012-03-14 00:12:35 +00:00
|
|
|
)));
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $articles);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testCreationWithMultipleDataSameModelManualInstances method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCreationWithMultipleDataSameModelManualInstances() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('PrimaryModel');
|
2010-06-02 05:00:52 +00:00
|
|
|
$Primary = new PrimaryModel();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $Primary->field('primary_name', array('id' => 1));
|
2012-03-14 00:12:35 +00:00
|
|
|
$this->assertEquals('Primary Name Existing', $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'PrimaryModel' => array(
|
|
|
|
'primary_name' => 'Primary Name New'
|
|
|
|
),
|
|
|
|
'SecondaryModel' => array(
|
|
|
|
'id' => array(1)
|
|
|
|
));
|
|
|
|
|
|
|
|
$Primary->create();
|
|
|
|
$result = $Primary->save($data);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $Primary->field('primary_name', array('id' => 1));
|
2012-03-14 00:12:35 +00:00
|
|
|
$this->assertEquals('Primary Name Existing', $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $Primary->getInsertID();
|
|
|
|
$this->assertTrue(!empty($result));
|
|
|
|
|
|
|
|
$result = $Primary->field('primary_name', array('id' => $result));
|
2012-03-14 00:12:35 +00:00
|
|
|
$this->assertEquals('Primary Name New', $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $Primary->find('count');
|
2012-03-14 00:12:35 +00:00
|
|
|
$this->assertEquals(2, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testRecordExists method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRecordExists() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('User');
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new User();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$this->assertFalse($TestModel->exists());
|
|
|
|
$TestModel->read(null, 1);
|
|
|
|
$this->assertTrue($TestModel->exists());
|
|
|
|
$TestModel->create();
|
|
|
|
$this->assertFalse($TestModel->exists());
|
|
|
|
$TestModel->id = 4;
|
|
|
|
$this->assertTrue($TestModel->exists());
|
|
|
|
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new TheVoid();
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->assertFalse($TestModel->exists());
|
2011-09-10 16:23:28 +00:00
|
|
|
}
|
2010-01-17 01:24:27 +00:00
|
|
|
|
2011-09-10 16:23:28 +00:00
|
|
|
/**
|
|
|
|
* testRecordExistsMissingTable method
|
|
|
|
*
|
|
|
|
* @expectedException PDOException
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRecordExistsMissingTable() {
|
|
|
|
$TestModel = new TheVoid();
|
2009-07-24 22:17:14 +00:00
|
|
|
$TestModel->id = 5;
|
2011-09-10 16:23:28 +00:00
|
|
|
$TestModel->exists();
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testUpdateExisting method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testUpdateExisting() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('User', 'Article', 'Comment');
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new User();
|
2009-07-24 22:17:14 +00:00
|
|
|
$TestModel->create();
|
|
|
|
|
|
|
|
$TestModel->save(array(
|
|
|
|
'User' => array(
|
|
|
|
'user' => 'some user',
|
|
|
|
'password' => 'some password'
|
|
|
|
)));
|
2014-09-10 14:29:23 +00:00
|
|
|
$this->assertTrue(is_int($TestModel->id) || ((int)$TestModel->id === 5));
|
2009-07-24 22:17:14 +00:00
|
|
|
$id = $TestModel->id;
|
|
|
|
|
|
|
|
$TestModel->save(array(
|
|
|
|
'User' => array(
|
|
|
|
'user' => 'updated user'
|
|
|
|
)));
|
2012-03-22 11:18:57 +00:00
|
|
|
$this->assertEquals($id, $TestModel->id);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->findById($id);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('updated user', $result['User']['user']);
|
|
|
|
$this->assertEquals('some password', $result['User']['password']);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2010-06-02 05:00:52 +00:00
|
|
|
$Article = new Article();
|
|
|
|
$Comment = new Comment();
|
2009-07-24 22:17:14 +00:00
|
|
|
$data = array(
|
|
|
|
'Comment' => array(
|
|
|
|
'id' => 1,
|
|
|
|
'comment' => 'First Comment for First Article'
|
|
|
|
),
|
|
|
|
'Article' => array(
|
|
|
|
'id' => 2,
|
|
|
|
'title' => 'Second Article'
|
|
|
|
));
|
|
|
|
|
|
|
|
$result = $Article->save($data);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $Comment->save($data);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2010-10-29 04:13:09 +00:00
|
|
|
/**
|
|
|
|
* test updating records and saving blank values.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testUpdateSavingBlankValues() {
|
2010-10-29 04:13:09 +00:00
|
|
|
$this->loadFixtures('Article');
|
2010-11-13 04:05:44 +00:00
|
|
|
$Article = new Article();
|
2010-10-29 04:13:09 +00:00
|
|
|
$Article->validate = array();
|
|
|
|
$Article->create();
|
|
|
|
$result = $Article->save(array(
|
|
|
|
'id' => 1,
|
|
|
|
'title' => '',
|
|
|
|
'body' => ''
|
|
|
|
));
|
2010-11-01 02:42:16 +00:00
|
|
|
$this->assertTrue((bool)$result);
|
2010-10-29 04:13:09 +00:00
|
|
|
$result = $Article->find('first', array('conditions' => array('Article.id' => 1)));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals('', $result['Article']['title'], 'Title is not blank');
|
|
|
|
$this->assertEquals('', $result['Article']['body'], 'Body is not blank');
|
2010-10-29 04:13:09 +00:00
|
|
|
}
|
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testUpdateMultiple method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testUpdateMultiple() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('Comment', 'Article', 'User', 'CategoryThread');
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new Comment();
|
2012-03-11 01:57:18 +00:00
|
|
|
$result = Hash::extract($TestModel->find('all'), '{n}.Comment.user_id');
|
2009-07-24 22:17:14 +00:00
|
|
|
$expected = array('2', '4', '1', '1', '1', '2');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->updateAll(array('Comment.user_id' => 5), array('Comment.user_id' => 2));
|
2012-03-11 01:57:18 +00:00
|
|
|
$result = Hash::combine($TestModel->find('all'), '{n}.Comment.id', '{n}.Comment.user_id');
|
2009-07-24 22:17:14 +00:00
|
|
|
$expected = array(1 => 5, 2 => 4, 3 => 1, 4 => 1, 5 => 1, 6 => 5);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->updateAll(
|
|
|
|
array('Comment.comment' => "'Updated today'"),
|
|
|
|
array('Comment.user_id' => 5)
|
|
|
|
);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2012-03-11 01:57:18 +00:00
|
|
|
$result = Hash::extract(
|
2009-07-24 22:17:14 +00:00
|
|
|
$TestModel->find('all', array(
|
|
|
|
'conditions' => array(
|
|
|
|
'Comment.user_id' => 5
|
|
|
|
))),
|
|
|
|
'{n}.Comment.comment'
|
|
|
|
);
|
|
|
|
$expected = array_fill(0, 2, 'Updated today');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testHabtmUuidWithUuidId method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testHabtmUuidWithUuidId() {
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolio', 'UuiditemsUuidportfolioNumericid');
|
|
|
|
$TestModel = new Uuidportfolio();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(1, count($result['Uuiditem']));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(36, strlen($result['Uuiditem'][0]['UuiditemsUuidportfolio']['id']));
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* test HABTM saving when join table has no primary key and only 2 columns.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testHabtmSavingWithNoPrimaryKeyUuidJoinTable() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
|
2010-06-02 05:00:52 +00:00
|
|
|
$Fruit = new Fruit();
|
2013-07-19 20:31:09 +00:00
|
|
|
$Fruit->FruitsUuidTag->order = null;
|
2009-07-24 22:17:14 +00:00
|
|
|
$data = array(
|
|
|
|
'Fruit' => array(
|
|
|
|
'color' => 'Red',
|
|
|
|
'shape' => 'Heart-shaped',
|
|
|
|
'taste' => 'sweet',
|
|
|
|
'name' => 'Strawberry',
|
|
|
|
),
|
|
|
|
'UuidTag' => array(
|
|
|
|
'UuidTag' => array(
|
|
|
|
'481fc6d0-b920-43e0-e50f-6d1740cf8569'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2010-06-02 05:00:52 +00:00
|
|
|
$result = $Fruit->save($data);
|
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* test HABTM saving when join table has no primary key and only 2 columns, no with model is used.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testHabtmSavingWithNoPrimaryKeyUuidJoinTableNoWith() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
|
2010-06-02 05:00:52 +00:00
|
|
|
$Fruit = new FruitNoWith();
|
2009-07-24 22:17:14 +00:00
|
|
|
$data = array(
|
|
|
|
'Fruit' => array(
|
|
|
|
'color' => 'Red',
|
|
|
|
'shape' => 'Heart-shaped',
|
|
|
|
'taste' => 'sweet',
|
|
|
|
'name' => 'Strawberry',
|
|
|
|
),
|
|
|
|
'UuidTag' => array(
|
|
|
|
'UuidTag' => array(
|
|
|
|
'481fc6d0-b920-43e0-e50f-6d1740cf8569'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2010-06-02 05:00:52 +00:00
|
|
|
$result = $Fruit->save($data);
|
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testHabtmUuidWithNumericId method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testHabtmUuidWithNumericId() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolioNumericid');
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new Uuiditem();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(1, count($result['Uuidportfolio']));
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testSaveMultipleHabtm method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSaveMultipleHabtm() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$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'
|
|
|
|
))));
|
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->id = 1;
|
|
|
|
$data = array(
|
|
|
|
'JoinA' => array(
|
|
|
|
'id' => '1',
|
|
|
|
'name' => 'New name for Join A 1',
|
2012-03-28 15:49:41 +00:00
|
|
|
'updated' => self::date()
|
2009-07-24 22:17:14 +00:00
|
|
|
),
|
|
|
|
'JoinB' => array(
|
|
|
|
array(
|
|
|
|
'id' => 1,
|
|
|
|
'join_b_id' => 2,
|
|
|
|
'other' => 'New data for Join A 1 Join B 2',
|
2012-03-28 15:49:41 +00:00
|
|
|
'created' => self::date(),
|
|
|
|
'updated' => self::date()
|
2009-07-24 22:17:14 +00:00
|
|
|
)),
|
|
|
|
'JoinC' => array(
|
|
|
|
array(
|
|
|
|
'id' => 1,
|
|
|
|
'join_c_id' => 2,
|
|
|
|
'other' => 'New data for Join A 1 Join C 2',
|
2012-03-28 15:49:41 +00:00
|
|
|
'created' => self::date(),
|
|
|
|
'updated' => self::date()
|
2009-07-24 22:17:14 +00:00
|
|
|
)));
|
|
|
|
|
|
|
|
$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',
|
2012-03-28 15:49:41 +00:00
|
|
|
'updated' => self::date()
|
2009-07-24 22:17:14 +00:00
|
|
|
),
|
|
|
|
'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',
|
2012-03-28 15:49:41 +00:00
|
|
|
'created' => self::date(),
|
|
|
|
'updated' => self::date()
|
2009-07-24 22:17:14 +00:00
|
|
|
))),
|
|
|
|
'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',
|
2012-03-28 15:49:41 +00:00
|
|
|
'created' => self::date(),
|
|
|
|
'updated' => self::date()
|
2009-07-24 22:17:14 +00:00
|
|
|
))));
|
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testSaveAll method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSaveAll() {
|
2010-10-26 00:17:32 +00:00
|
|
|
$this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new Post();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->find('all');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(3, count($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->assertFalse(isset($result[3]));
|
|
|
|
|
|
|
|
$TestModel->saveAll(array(
|
|
|
|
'Post' => array(
|
|
|
|
'title' => 'Post with Author',
|
|
|
|
'body' => 'This post will be saved with an author'
|
|
|
|
),
|
|
|
|
'Author' => array(
|
|
|
|
'user' => 'bob',
|
|
|
|
'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
|
|
|
|
)));
|
|
|
|
|
|
|
|
$result = $TestModel->find('all');
|
|
|
|
$expected = array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '4',
|
|
|
|
'author_id' => '5',
|
|
|
|
'title' => 'Post with Author',
|
|
|
|
'body' => 'This post will be saved with an author',
|
2011-10-03 19:21:07 +00:00
|
|
|
'published' => 'N'
|
2009-07-24 22:17:14 +00:00
|
|
|
),
|
|
|
|
'Author' => array(
|
|
|
|
'id' => '5',
|
|
|
|
'user' => 'bob',
|
|
|
|
'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
|
|
|
|
'test' => 'working'
|
|
|
|
));
|
2012-03-28 15:49:41 +00:00
|
|
|
$this->assertEquals(self::date(), $result[3]['Post']['created']);
|
|
|
|
$this->assertEquals(self::date(), $result[3]['Post']['updated']);
|
|
|
|
$this->assertEquals(self::date(), $result[3]['Author']['created']);
|
|
|
|
$this->assertEquals(self::date(), $result[3]['Author']['updated']);
|
2011-10-03 19:21:07 +00:00
|
|
|
unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
|
|
|
|
unset($result[3]['Author']['created'], $result[3]['Author']['updated']);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $result[3]);
|
|
|
|
$this->assertEquals(4, count($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->deleteAll(true);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array(), $TestModel->find('all'));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
// SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
|
|
|
|
$this->db->truncate($TestModel);
|
|
|
|
|
|
|
|
$TestModel->saveAll(array(
|
|
|
|
array(
|
|
|
|
'title' => 'Multi-record post 1',
|
|
|
|
'body' => 'First multi-record post',
|
|
|
|
'author_id' => 2
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'title' => 'Multi-record post 2',
|
|
|
|
'body' => 'Second multi-record post',
|
|
|
|
'author_id' => 2
|
|
|
|
)));
|
|
|
|
|
|
|
|
$result = $TestModel->find('all', array(
|
|
|
|
'recursive' => -1,
|
|
|
|
'order' => 'Post.id ASC'
|
|
|
|
));
|
|
|
|
$expected = array(
|
|
|
|
array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '1',
|
|
|
|
'author_id' => '2',
|
|
|
|
'title' => 'Multi-record post 1',
|
|
|
|
'body' => 'First multi-record post',
|
2011-10-03 19:21:07 +00:00
|
|
|
'published' => 'N'
|
2009-07-24 22:17:14 +00:00
|
|
|
)),
|
|
|
|
array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '2',
|
|
|
|
'author_id' => '2',
|
|
|
|
'title' => 'Multi-record post 2',
|
|
|
|
'body' => 'Second multi-record post',
|
2011-10-03 19:21:07 +00:00
|
|
|
'published' => 'N'
|
2009-07-24 22:17:14 +00:00
|
|
|
)));
|
2012-03-28 15:49:41 +00:00
|
|
|
$this->assertEquals(self::date(), $result[0]['Post']['created']);
|
|
|
|
$this->assertEquals(self::date(), $result[0]['Post']['updated']);
|
|
|
|
$this->assertEquals(self::date(), $result[1]['Post']['created']);
|
|
|
|
$this->assertEquals(self::date(), $result[1]['Post']['updated']);
|
2011-10-03 19:21:07 +00:00
|
|
|
unset($result[0]['Post']['created'], $result[0]['Post']['updated']);
|
|
|
|
unset($result[1]['Post']['created'], $result[1]['Post']['updated']);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new Comment();
|
2009-07-24 22:17:14 +00:00
|
|
|
$result = $TestModel->saveAll(array(
|
|
|
|
'Comment' => array(
|
|
|
|
'article_id' => 2,
|
|
|
|
'user_id' => 2,
|
|
|
|
'comment' => 'New comment with attachment',
|
|
|
|
'published' => 'Y'
|
|
|
|
),
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => 'some_file.tgz'
|
|
|
|
)));
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->find('all');
|
|
|
|
$expected = array(
|
|
|
|
'id' => '7',
|
|
|
|
'article_id' => '2',
|
|
|
|
'user_id' => '2',
|
|
|
|
'comment' => 'New comment with attachment',
|
2011-10-03 19:21:07 +00:00
|
|
|
'published' => 'Y'
|
2009-07-24 22:17:14 +00:00
|
|
|
);
|
2012-03-28 15:49:41 +00:00
|
|
|
$this->assertEquals(self::date(), $result[6]['Comment']['created']);
|
|
|
|
$this->assertEquals(self::date(), $result[6]['Comment']['updated']);
|
2011-10-03 19:21:07 +00:00
|
|
|
unset($result[6]['Comment']['created'], $result[6]['Comment']['updated']);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $result[6]['Comment']);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'id' => '2',
|
|
|
|
'comment_id' => '7',
|
2011-10-03 19:21:07 +00:00
|
|
|
'attachment' => 'some_file.tgz'
|
2009-07-24 22:17:14 +00:00
|
|
|
);
|
2012-03-28 15:49:41 +00:00
|
|
|
$this->assertEquals(self::date(), $result[6]['Attachment']['created']);
|
|
|
|
$this->assertEquals(self::date(), $result[6]['Attachment']['updated']);
|
2011-10-03 19:21:07 +00:00
|
|
|
unset($result[6]['Attachment']['created'], $result[6]['Attachment']['updated']);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $result[6]['Attachment']);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* Test SaveAll with Habtm relations
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSaveAllHabtm() {
|
2010-10-26 00:17:32 +00:00
|
|
|
$this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
|
2009-07-24 22:17:14 +00:00
|
|
|
$data = array(
|
|
|
|
'Article' => array(
|
|
|
|
'user_id' => 1,
|
|
|
|
'title' => 'Article Has and belongs to Many Tags'
|
|
|
|
),
|
|
|
|
'Tag' => array(
|
|
|
|
'Tag' => array(1, 2)
|
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'comment' => 'Article comment',
|
|
|
|
'user_id' => 1
|
|
|
|
)));
|
2010-06-02 05:00:52 +00:00
|
|
|
$Article = new Article();
|
2009-07-24 22:17:14 +00:00
|
|
|
$result = $Article->saveAll($data);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $Article->read();
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(2, count($result['Tag']));
|
|
|
|
$this->assertEquals('tag1', $result['Tag'][0]['tag']);
|
|
|
|
$this->assertEquals(1, count($result['Comment']));
|
|
|
|
$this->assertEquals(1, count($result['Comment'][0]['comment']));
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* Test SaveAll with Habtm relations and extra join table fields
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSaveAllHabtmWithExtraJoinTableFields() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Something' => array(
|
|
|
|
'id' => 4,
|
|
|
|
'title' => 'Extra Fields',
|
|
|
|
'body' => 'Extra Fields Body',
|
|
|
|
'published' => '1'
|
|
|
|
),
|
|
|
|
'SomethingElse' => array(
|
|
|
|
array('something_else_id' => 1, 'doomed' => '1'),
|
|
|
|
array('something_else_id' => 2, 'doomed' => '0'),
|
|
|
|
array('something_else_id' => 3, 'doomed' => '1')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2010-06-02 05:00:52 +00:00
|
|
|
$Something = new Something();
|
2009-07-24 22:17:14 +00:00
|
|
|
$result = $Something->saveAll($data);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
$result = $Something->read();
|
|
|
|
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(3, count($result['SomethingElse']));
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->assertTrue(Set::matches('/Something[id=4]', $result));
|
|
|
|
|
|
|
|
$this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
|
|
|
|
$this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
|
|
|
|
$this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
|
|
|
|
|
|
|
|
$this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
|
|
|
|
$this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
|
|
|
|
$this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
|
|
|
|
|
|
|
|
$this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
|
|
|
|
$this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
|
|
|
|
$this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
|
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testSaveAllHasOne method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSaveAllHasOne() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$model = new Comment();
|
|
|
|
$model->deleteAll(true);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array(), $model->find('all'));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$model->Attachment->deleteAll(true);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array(), $model->Attachment->find('all'));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$this->assertTrue($model->saveAll(array(
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => 'Comment with attachment',
|
|
|
|
'article_id' => 1,
|
|
|
|
'user_id' => 1
|
|
|
|
),
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => 'some_file.zip'
|
|
|
|
))));
|
|
|
|
$result = $model->find('all', array('fields' => array(
|
|
|
|
'Comment.id', 'Comment.comment', 'Attachment.id',
|
|
|
|
'Attachment.comment_id', 'Attachment.attachment'
|
|
|
|
)));
|
|
|
|
$expected = array(array(
|
|
|
|
'Comment' => array(
|
|
|
|
'id' => '1',
|
|
|
|
'comment' => 'Comment with attachment'
|
|
|
|
),
|
|
|
|
'Attachment' => array(
|
|
|
|
'id' => '1',
|
|
|
|
'comment_id' => '1',
|
|
|
|
'attachment' => 'some_file.zip'
|
|
|
|
)));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-12-04 19:39:12 +00:00
|
|
|
|
|
|
|
$model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
|
|
|
|
$data = array(
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => 'Comment with attachment',
|
|
|
|
'article_id' => 1,
|
|
|
|
'user_id' => 1
|
|
|
|
),
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => 'some_file.zip'
|
|
|
|
));
|
|
|
|
$this->assertTrue($model->saveAll($data, array('validate' => 'first')));
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testSaveAllBelongsTo method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSaveAllBelongsTo() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$model = new Comment();
|
|
|
|
$model->deleteAll(true);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array(), $model->find('all'));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$model->Article->deleteAll(true);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array(), $model->Article->find('all'));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$this->assertTrue($model->saveAll(array(
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => 'Article comment',
|
|
|
|
'article_id' => 1,
|
|
|
|
'user_id' => 1
|
|
|
|
),
|
|
|
|
'Article' => array(
|
|
|
|
'title' => 'Model Associations 101',
|
|
|
|
'user_id' => 1
|
|
|
|
))));
|
|
|
|
$result = $model->find('all', array('fields' => array(
|
|
|
|
'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
|
|
|
|
)));
|
|
|
|
$expected = array(array(
|
|
|
|
'Comment' => array(
|
|
|
|
'id' => '1',
|
|
|
|
'article_id' => '1',
|
|
|
|
'comment' => 'Article comment'
|
|
|
|
),
|
|
|
|
'Article' => array(
|
|
|
|
'id' => '1',
|
|
|
|
'title' => 'Model Associations 101'
|
|
|
|
)));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testSaveAllHasOneValidation method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSaveAllHasOneValidation() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$model = new Comment();
|
|
|
|
$model->deleteAll(true);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array(), $model->find('all'));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$model->Attachment->deleteAll(true);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array(), $model->Attachment->find('all'));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$model->validate = array('comment' => 'notBlank');
|
|
|
|
$model->Attachment->validate = array('attachment' => 'notBlank');
|
2009-09-01 03:40:47 +00:00
|
|
|
$model->Attachment->bindModel(array('belongsTo' => array('Comment')));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2012-03-22 11:18:57 +00:00
|
|
|
$result = $model->saveAll(
|
2009-07-24 22:17:14 +00:00
|
|
|
array(
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => '',
|
|
|
|
'article_id' => 1,
|
|
|
|
'user_id' => 1
|
|
|
|
),
|
|
|
|
'Attachment' => array('attachment' => '')
|
|
|
|
),
|
|
|
|
array('validate' => 'first')
|
2012-03-22 11:18:57 +00:00
|
|
|
);
|
|
|
|
$this->assertEquals(false, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
$expected = array(
|
2012-06-18 11:46:19 +00:00
|
|
|
'comment' => array('This field cannot be left blank'),
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => array('This field cannot be left blank')
|
|
|
|
)
|
2009-07-24 22:17:14 +00:00
|
|
|
);
|
2012-06-18 11:46:19 +00:00
|
|
|
$this->assertEquals($expected, $model->validationErrors);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testSaveAllAtomic method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSaveAllAtomic() {
|
2012-02-13 00:00:28 +00:00
|
|
|
$this->loadFixtures('Article', 'User', 'Comment');
|
|
|
|
$TestModel = new Article();
|
|
|
|
|
|
|
|
$result = $TestModel->saveAll(array(
|
|
|
|
'Article' => array(
|
|
|
|
'title' => 'Post with Author',
|
|
|
|
'body' => 'This post will be saved with an author',
|
|
|
|
'user_id' => 2
|
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment', 'user_id' => 2))
|
|
|
|
), array('atomic' => false));
|
|
|
|
|
|
|
|
$this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
|
|
|
|
|
|
|
|
$result = $TestModel->saveAll(array(
|
|
|
|
array(
|
|
|
|
'id' => '1',
|
|
|
|
'title' => 'Baleeted First Post',
|
|
|
|
'body' => 'Baleeted!',
|
|
|
|
'published' => 'N'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => '2',
|
|
|
|
'title' => 'Just update the title'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'title' => 'Creating a fourth post',
|
|
|
|
'body' => 'Fourth post body',
|
|
|
|
'user_id' => 2
|
|
|
|
)
|
|
|
|
), array('atomic' => false));
|
|
|
|
$this->assertSame($result, array(true, true, true));
|
|
|
|
|
|
|
|
$result = $TestModel->saveAll(array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'comment' => 'First new comment',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 1
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'comment' => 'Second new comment',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 2
|
|
|
|
))
|
|
|
|
), array('validate' => true, 'atomic' => false));
|
|
|
|
$this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
|
2012-06-25 00:06:14 +00:00
|
|
|
|
|
|
|
$TestModel->validate = array(
|
2015-05-17 20:27:16 +00:00
|
|
|
'title' => 'notBlank',
|
2012-06-25 00:06:14 +00:00
|
|
|
'author_id' => 'numeric'
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll(array(
|
|
|
|
array(
|
|
|
|
'id' => '1',
|
|
|
|
'title' => 'Un-Baleeted First Post',
|
|
|
|
'body' => 'Not Baleeted!',
|
|
|
|
'published' => 'Y'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => '2',
|
|
|
|
'title' => '',
|
|
|
|
'body' => 'Trying to get away with an empty title'
|
|
|
|
)
|
|
|
|
), array('validate' => true, 'atomic' => false));
|
|
|
|
$this->assertSame(array(true, false), $result);
|
2012-02-13 00:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveAllDeepAssociated method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAllDeepAssociated() {
|
|
|
|
$this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
|
|
|
|
$TestModel = new Article();
|
|
|
|
$TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
|
|
|
|
$TestModel->hasAndBelongsToMany = array();
|
|
|
|
|
|
|
|
$result = $TestModel->saveAll(array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
|
|
|
|
array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
|
|
|
), array('deep' => true));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$result = $TestModel->findById(2);
|
|
|
|
$expected = array(
|
|
|
|
'First Comment for Second Article',
|
|
|
|
'Second Comment for Second Article',
|
|
|
|
'First new comment',
|
|
|
|
'Second new comment'
|
|
|
|
);
|
2012-03-11 01:57:18 +00:00
|
|
|
$result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
|
2012-03-14 14:01:19 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2012-02-13 00:00:28 +00:00
|
|
|
|
|
|
|
$result = $TestModel->Comment->User->field('id', array('user' => 'newuser', 'password' => 'newuserpass'));
|
|
|
|
$this->assertEquals(5, $result);
|
|
|
|
$result = $TestModel->saveAll(array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
|
|
|
|
array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
|
|
|
|
)
|
|
|
|
), array('deep' => true));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$result = $TestModel->findById(2);
|
|
|
|
$expected = array(
|
|
|
|
'First Comment for Second Article',
|
|
|
|
'Second Comment for Second Article',
|
|
|
|
'First new comment',
|
|
|
|
'Second new comment',
|
|
|
|
'Third new comment',
|
|
|
|
'Fourth new comment'
|
|
|
|
);
|
2012-03-11 01:57:18 +00:00
|
|
|
$result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
|
2012-03-14 14:01:19 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2012-02-13 00:00:28 +00:00
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->field('id', array('attachment' => 'deepsaved'));
|
|
|
|
$this->assertEquals(2, $result);
|
|
|
|
$data = array(
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => 'deepsave insert',
|
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => 'First comment deepsave insert',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 5,
|
|
|
|
'Article' => array(
|
|
|
|
'title' => 'First Article deepsave insert',
|
|
|
|
'body' => 'First Article Body deepsave insert',
|
|
|
|
'User' => array(
|
|
|
|
'user' => '',
|
|
|
|
'password' => 'magic'
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$TestModel->Comment->Attachment->create();
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => true));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$expected = array('User' => array('user' => array('This field cannot be left blank')));
|
|
|
|
$this->assertEquals($expected, $TestModel->validationErrors);
|
|
|
|
|
|
|
|
$data['Comment']['Article']['User']['user'] = 'deepsave';
|
|
|
|
$TestModel->Comment->Attachment->create();
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => true));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->findById($TestModel->Comment->Attachment->id);
|
|
|
|
$expected = array(
|
|
|
|
'Attachment' => array(
|
|
|
|
'id' => '3',
|
|
|
|
'comment_id' => '11',
|
|
|
|
'attachment' => 'deepsave insert',
|
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
'id' => '11',
|
|
|
|
'article_id' => '4',
|
|
|
|
'user_id' => '5',
|
|
|
|
'comment' => 'First comment deepsave insert',
|
|
|
|
'published' => 'Y',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
unset($result['Attachment']['created'], $result['Attachment']['updated']);
|
|
|
|
$this->assertEquals($expected['Attachment'], $result['Attachment']);
|
|
|
|
|
|
|
|
unset($result['Comment']['created'], $result['Comment']['updated']);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected['Comment'], $result['Comment']);
|
2012-02-13 00:00:28 +00:00
|
|
|
|
|
|
|
$result = $TestModel->findById($result['Comment']['article_id']);
|
|
|
|
$expected = array(
|
|
|
|
'Article' => array(
|
|
|
|
'id' => '4',
|
|
|
|
'user_id' => '6',
|
|
|
|
'title' => 'First Article deepsave insert',
|
|
|
|
'body' => 'First Article Body deepsave insert',
|
|
|
|
'published' => 'N',
|
|
|
|
),
|
|
|
|
'User' => array(
|
|
|
|
'id' => '6',
|
|
|
|
'user' => 'deepsave',
|
|
|
|
'password' => 'magic',
|
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'id' => '11',
|
|
|
|
'article_id' => '4',
|
|
|
|
'user_id' => '5',
|
|
|
|
'comment' => 'First comment deepsave insert',
|
|
|
|
'published' => 'Y',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
unset(
|
|
|
|
$result['Article']['created'], $result['Article']['updated'],
|
|
|
|
$result['User']['created'], $result['User']['updated'],
|
|
|
|
$result['Comment'][0]['created'], $result['Comment'][0]['updated']
|
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2012-02-13 00:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveAllDeepMany
|
|
|
|
* tests the validate methods with deeper recursive data
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAllDeepMany() {
|
|
|
|
$this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
|
|
|
|
$TestModel = new Article();
|
|
|
|
$TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
|
|
|
|
$TestModel->hasAndBelongsToMany = array();
|
|
|
|
|
2012-06-25 00:06:14 +00:00
|
|
|
$data = array(
|
|
|
|
array(
|
|
|
|
'Article' => array('id' => 1),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First comment deepsaved article 1', 'published' => 'Y', 'User' => array('user' => 'savemany', 'password' => 'manysaved')),
|
|
|
|
array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => 'moresaved')),
|
|
|
|
array('comment' => 'Second comment deepsaved article 2', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($data, array('deep' => true));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
2012-02-13 00:00:28 +00:00
|
|
|
$data = array(
|
|
|
|
array(
|
|
|
|
'id' => 1, 'body' => '',
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
|
|
|
|
array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => '')),
|
|
|
|
array('comment' => '', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->Comment->validate['comment'] = 'notBlank';
|
2012-02-13 00:00:28 +00:00
|
|
|
$result = $TestModel->saveAll($data, array('deep' => true));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
0 => array(
|
2012-06-18 11:46:19 +00:00
|
|
|
'body' => array('This field cannot be left blank'),
|
|
|
|
'Comment' => array(
|
|
|
|
0 => array(
|
|
|
|
'comment' => array('This field cannot be left blank'),
|
|
|
|
'User' => array(
|
|
|
|
'user' => array('This field cannot be left blank')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2012-02-13 00:00:28 +00:00
|
|
|
),
|
|
|
|
1 => array(
|
|
|
|
'Comment' => array(
|
|
|
|
0 => array(
|
|
|
|
'User' => array(
|
|
|
|
'password' => array('This field cannot be left blank')
|
|
|
|
)
|
|
|
|
),
|
|
|
|
1 => array(
|
|
|
|
'comment' => array('This field cannot be left blank')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $TestModel->validationErrors;
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* testSaveAllDeepValidateOnly
|
|
|
|
* tests the validate methods with deeper recursive data
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAllDeepValidateOnly() {
|
|
|
|
$this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
|
|
|
|
$TestModel = new Article();
|
|
|
|
$TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
|
|
|
|
$TestModel->hasAndBelongsToMany = array();
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->Comment->Attachment->validate['attachment'] = 'notBlank';
|
|
|
|
$TestModel->Comment->validate['comment'] = 'notBlank';
|
2012-02-13 00:00:28 +00:00
|
|
|
|
|
|
|
$result = $TestModel->saveAll(
|
|
|
|
array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
|
|
|
|
array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array('validate' => 'only', 'deep' => true)
|
|
|
|
);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$result = $TestModel->saveAll(
|
|
|
|
array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
|
|
|
|
array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array('validate' => 'only', 'deep' => true)
|
|
|
|
);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = $TestModel->saveAll(
|
|
|
|
array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
|
|
|
|
array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array('validate' => 'only', 'atomic' => false, 'deep' => true)
|
|
|
|
);
|
|
|
|
$expected = array(
|
|
|
|
'Article' => true,
|
|
|
|
'Comment' => array(
|
2012-04-27 02:27:34 +00:00
|
|
|
true,
|
|
|
|
true
|
2012-02-13 00:00:28 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$result = $TestModel->saveAll(
|
|
|
|
array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
|
|
|
|
array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array('validate' => 'only', 'atomic' => false, 'deep' => true)
|
|
|
|
);
|
|
|
|
$expected = array(
|
|
|
|
'Article' => true,
|
|
|
|
'Comment' => array(
|
2012-04-27 02:27:34 +00:00
|
|
|
false,
|
|
|
|
true
|
2012-02-13 00:00:28 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$result = $TestModel->saveAll(array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
|
|
|
|
array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array('validate' => 'only', 'deep' => true)
|
|
|
|
);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$result = $TestModel->saveAll(array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
|
|
|
|
array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array('validate' => 'only', 'deep' => true)
|
|
|
|
);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = $TestModel->saveAll(array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
|
|
|
|
array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsave'))
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array('validate' => 'only', 'atomic' => false, 'deep' => true)
|
|
|
|
);
|
|
|
|
$expected = array(
|
|
|
|
'Article' => true,
|
|
|
|
'Comment' => array(
|
2012-04-27 02:27:34 +00:00
|
|
|
true,
|
|
|
|
true
|
2012-02-13 00:00:28 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$result = $TestModel->saveAll(array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
|
|
|
|
array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array('validate' => 'only', 'atomic' => false, 'deep' => true)
|
|
|
|
);
|
|
|
|
$expected = array(
|
|
|
|
'Article' => true,
|
|
|
|
'Comment' => array(
|
2012-04-27 02:27:34 +00:00
|
|
|
true,
|
|
|
|
false
|
2012-02-13 00:00:28 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'Comment' => array(
|
|
|
|
1 => array(
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => array('This field cannot be left blank')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $TestModel->validationErrors;
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => 'deepsave insert',
|
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => 'First comment deepsave insert',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 5,
|
|
|
|
'Article' => array(
|
|
|
|
'title' => 'First Article deepsave insert',
|
|
|
|
'body' => 'First Article Body deepsave insert',
|
|
|
|
'User' => array(
|
|
|
|
'user' => 'deepsave',
|
|
|
|
'password' => 'magic'
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
|
|
|
|
$expected = array(
|
|
|
|
'Attachment' => true,
|
|
|
|
'Comment' => true
|
|
|
|
);
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => 'deepsave insert',
|
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => 'First comment deepsave insert',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 5,
|
|
|
|
'Article' => array(
|
|
|
|
'title' => 'First Article deepsave insert',
|
|
|
|
'body' => 'First Article Body deepsave insert',
|
|
|
|
'User' => array(
|
|
|
|
'user' => '',
|
|
|
|
'password' => 'magic'
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->validationErrors;
|
|
|
|
$expected = array(
|
|
|
|
'Comment' => array(
|
|
|
|
'Article' => array(
|
|
|
|
'User' => array(
|
|
|
|
'user' => array('This field cannot be left blank')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
|
|
|
|
$expected = array(
|
|
|
|
'Attachment' => true,
|
|
|
|
'Comment' => false
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$data['Comment']['Article']['body'] = '';
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->validationErrors;
|
|
|
|
$expected = array(
|
|
|
|
'Comment' => array(
|
|
|
|
'Article' => array(
|
2012-06-18 11:46:19 +00:00
|
|
|
'body' => array('This field cannot be left blank'),
|
|
|
|
'User' => array(
|
|
|
|
'user' => array('This field cannot be left blank')
|
|
|
|
)
|
2012-02-13 00:00:28 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
|
|
|
|
$expected = array(
|
|
|
|
'Attachment' => true,
|
|
|
|
'Comment' => false
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$data['Comment']['comment'] = '';
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->validationErrors;
|
|
|
|
$expected = array(
|
|
|
|
'Comment' => array(
|
2012-06-18 11:46:19 +00:00
|
|
|
'comment' => array('This field cannot be left blank'),
|
|
|
|
'Article' => array(
|
|
|
|
'body' => array('This field cannot be left blank'),
|
|
|
|
'User' => array(
|
|
|
|
'user' => array('This field cannot be left blank')
|
|
|
|
)
|
|
|
|
)
|
2012-02-13 00:00:28 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
|
|
|
|
$expected = array(
|
|
|
|
'Attachment' => true,
|
|
|
|
'Comment' => false
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$data['Attachment']['attachment'] = '';
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->validationErrors;
|
2012-06-18 11:46:19 +00:00
|
|
|
$expected = array(
|
|
|
|
'attachment' => array('This field cannot be left blank'),
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => array('This field cannot be left blank'),
|
|
|
|
'Article' => array(
|
|
|
|
'body' => array('This field cannot be left blank'),
|
|
|
|
'User' => array(
|
|
|
|
'user' => array('This field cannot be left blank')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2012-02-13 00:00:28 +00:00
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->validationErrors;
|
2012-06-18 11:46:19 +00:00
|
|
|
$expected = array(
|
|
|
|
'comment' => array('This field cannot be left blank'),
|
|
|
|
'Article' => array(
|
|
|
|
'body' => array('This field cannot be left blank'),
|
|
|
|
'User' => array(
|
|
|
|
'user' => array('This field cannot be left blank')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2012-02-13 00:00:28 +00:00
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
|
|
|
|
$expected = array(
|
|
|
|
'Attachment' => false,
|
|
|
|
'Comment' => false
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveAllNotDeepAssociated method
|
|
|
|
* test that only directly associated data gets saved
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAllNotDeepAssociated() {
|
|
|
|
$this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new Article();
|
2012-02-13 00:00:28 +00:00
|
|
|
$TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
|
|
|
|
$TestModel->hasAndBelongsToMany = array();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->saveAll(array(
|
2012-02-13 00:00:28 +00:00
|
|
|
'Article' => array('id' => 2),
|
2009-07-24 22:17:14 +00:00
|
|
|
'Comment' => array(
|
2012-02-13 04:56:10 +00:00
|
|
|
array(
|
|
|
|
'comment' => 'First new comment', 'published' => 'Y', 'user_id' => 2,
|
|
|
|
'User' => array('user' => 'newuser', 'password' => 'newuserpass')
|
|
|
|
),
|
2012-02-13 00:00:28 +00:00
|
|
|
array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
|
|
|
), array('deep' => false));
|
|
|
|
$this->assertTrue($result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2012-02-13 00:00:28 +00:00
|
|
|
$result = $TestModel->Comment->User->field('id', array('user' => 'newuser', 'password' => 'newuserpass'));
|
|
|
|
$this->assertFalse($result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->saveAll(array(
|
2012-02-13 00:00:28 +00:00
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 4),
|
|
|
|
array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
|
|
|
|
)
|
|
|
|
), array('deep' => false));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->field('id', array('attachment' => 'deepsaved'));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => 'deepsave insert',
|
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => 'First comment deepsave insert',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 4,
|
2012-02-13 04:56:10 +00:00
|
|
|
'article_id' => 1,
|
2012-02-13 00:00:28 +00:00
|
|
|
'Article' => array(
|
|
|
|
'title' => 'First Article deepsave insert',
|
|
|
|
'body' => 'First Article Body deepsave insert',
|
|
|
|
'User' => array(
|
|
|
|
'user' => 'deepsave',
|
|
|
|
'password' => 'magic'
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$expected = $TestModel->User->find('count');
|
|
|
|
|
|
|
|
$TestModel->Comment->Attachment->create();
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => false));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$result = $TestModel->User->find('count');
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->findById($TestModel->Comment->Attachment->id);
|
|
|
|
$expected = array(
|
|
|
|
'Attachment' => array(
|
|
|
|
'id' => '2',
|
|
|
|
'comment_id' => '11',
|
|
|
|
'attachment' => 'deepsave insert',
|
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
'id' => '11',
|
2012-02-13 04:56:10 +00:00
|
|
|
'article_id' => 1,
|
2012-02-13 00:00:28 +00:00
|
|
|
'user_id' => '4',
|
|
|
|
'comment' => 'First comment deepsave insert',
|
|
|
|
'published' => 'Y',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
unset($result['Attachment']['created'], $result['Attachment']['updated']);
|
|
|
|
$this->assertEquals($expected['Attachment'], $result['Attachment']);
|
|
|
|
|
|
|
|
unset($result['Comment']['created'], $result['Comment']['updated']);
|
|
|
|
$this->assertEquals($expected['Comment'], $result['Comment']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveAllNotDeepMany
|
|
|
|
* tests the save methods to not save deeper recursive data
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAllNotDeepMany() {
|
|
|
|
$this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
|
|
|
|
$TestModel = new Article();
|
|
|
|
$TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
|
|
|
|
$TestModel->hasAndBelongsToMany = array();
|
|
|
|
|
|
|
|
$data = array(
|
2009-07-24 22:17:14 +00:00
|
|
|
array(
|
2012-06-25 00:06:14 +00:00
|
|
|
'id' => 1,
|
|
|
|
'body' => '',
|
2012-02-13 00:00:28 +00:00
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
|
|
|
|
array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
2009-07-24 22:17:14 +00:00
|
|
|
),
|
|
|
|
array(
|
2012-02-13 00:00:28 +00:00
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => '')),
|
|
|
|
array('comment' => '', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->Comment->validate['comment'] = 'notBlank';
|
2012-02-13 00:00:28 +00:00
|
|
|
$result = $TestModel->saveAll($data, array('deep' => false));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
0 => array(
|
|
|
|
'body' => array('This field cannot be left blank')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $TestModel->validationErrors;
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
array(
|
|
|
|
'Article' => array('id' => 1, 'body' => 'Ignore invalid comment'),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => '', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
2009-07-24 22:17:14 +00:00
|
|
|
),
|
|
|
|
array(
|
2012-08-25 11:29:16 +00:00
|
|
|
'Article' => array('id' => 2),
|
2012-02-13 00:00:28 +00:00
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => '', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
2009-07-24 22:17:14 +00:00
|
|
|
)
|
2012-02-13 00:00:28 +00:00
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($data, array('deep' => false));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* testSaveAllNotDeepValidateOnly
|
|
|
|
* tests the validate methods to not validate deeper recursive data
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAllNotDeepValidateOnly() {
|
|
|
|
$this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
|
|
|
|
$TestModel = new Article();
|
|
|
|
$TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
|
|
|
|
$TestModel->hasAndBelongsToMany = array();
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->Comment->Attachment->validate['attachment'] = 'notBlank';
|
|
|
|
$TestModel->Comment->validate['comment'] = 'notBlank';
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2012-02-13 00:00:28 +00:00
|
|
|
$result = $TestModel->saveAll(
|
2009-07-24 22:17:14 +00:00
|
|
|
array(
|
2012-02-13 00:00:28 +00:00
|
|
|
'Article' => array('id' => 2, 'body' => ''),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
|
|
|
|
array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array('validate' => 'only', 'deep' => false)
|
|
|
|
);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$expected = array('body' => array('This field cannot be left blank'));
|
|
|
|
$result = $TestModel->validationErrors;
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$result = $TestModel->saveAll(
|
|
|
|
array(
|
|
|
|
'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
|
|
|
|
array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
2009-07-24 22:17:14 +00:00
|
|
|
),
|
2012-02-13 00:00:28 +00:00
|
|
|
array('validate' => 'only', 'deep' => false)
|
|
|
|
);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$result = $TestModel->saveAll(
|
2009-07-24 22:17:14 +00:00
|
|
|
array(
|
2012-02-13 00:00:28 +00:00
|
|
|
'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
|
|
|
|
array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array('validate' => 'only', 'atomic' => false, 'deep' => false)
|
|
|
|
);
|
|
|
|
$expected = array(
|
|
|
|
'Article' => true,
|
|
|
|
'Comment' => array(
|
2012-04-27 02:27:34 +00:00
|
|
|
true,
|
|
|
|
true
|
2009-07-24 22:17:14 +00:00
|
|
|
)
|
2012-02-13 00:00:28 +00:00
|
|
|
);
|
|
|
|
$this->assertSame($expected, $result);
|
2010-03-27 23:59:42 +00:00
|
|
|
|
2012-02-13 00:00:28 +00:00
|
|
|
$result = $TestModel->saveAll(array(
|
|
|
|
'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
|
|
|
|
array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array('validate' => 'only', 'deep' => false)
|
|
|
|
);
|
|
|
|
$this->assertTrue($result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->saveAll(array(
|
2012-02-13 00:00:28 +00:00
|
|
|
'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
|
2009-07-24 22:17:14 +00:00
|
|
|
'Comment' => array(
|
2012-02-13 00:00:28 +00:00
|
|
|
array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
|
|
|
|
array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array('validate' => 'only', 'atomic' => false, 'deep' => false)
|
|
|
|
);
|
|
|
|
$expected = array(
|
|
|
|
'Article' => true,
|
|
|
|
'Comment' => array(
|
2012-04-27 02:27:34 +00:00
|
|
|
true,
|
|
|
|
true
|
2012-02-13 00:00:28 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$expected = array();
|
|
|
|
$result = $TestModel->validationErrors;
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => 'deepsave insert',
|
2009-07-24 22:17:14 +00:00
|
|
|
),
|
2012-02-13 00:00:28 +00:00
|
|
|
'Comment' => array(
|
|
|
|
'comment' => 'First comment deepsave insert',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 5,
|
|
|
|
'Article' => array(
|
|
|
|
'title' => 'First Article deepsave insert ignored',
|
|
|
|
'body' => 'First Article Body deepsave insert',
|
|
|
|
'User' => array(
|
|
|
|
'user' => '',
|
|
|
|
'password' => 'magic'
|
|
|
|
),
|
2009-07-24 22:17:14 +00:00
|
|
|
),
|
|
|
|
)
|
2012-02-13 00:00:28 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->validationErrors;
|
|
|
|
$expected = array();
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
|
|
|
|
$expected = array(
|
|
|
|
'Attachment' => true,
|
|
|
|
'Comment' => true
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$data['Comment']['Article']['body'] = '';
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->validationErrors;
|
|
|
|
$expected = array();
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
|
|
|
|
$expected = array(
|
|
|
|
'Attachment' => true,
|
|
|
|
'Comment' => true
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testSaveAllHasMany method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSaveAllHasMany() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('Article', 'Comment');
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new Article();
|
2011-10-06 02:06:36 +00:00
|
|
|
$TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
|
2009-07-24 22:17:14 +00:00
|
|
|
$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
|
|
|
|
|
|
|
|
$result = $TestModel->saveAll(array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
|
|
|
|
array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
|
|
|
));
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->findById(2);
|
|
|
|
$expected = array(
|
|
|
|
'First Comment for Second Article',
|
|
|
|
'Second Comment for Second Article',
|
|
|
|
'First new comment',
|
|
|
|
'Second new comment'
|
|
|
|
);
|
2012-03-11 01:57:18 +00:00
|
|
|
$result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
|
2012-03-14 14:01:19 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->saveAll(
|
|
|
|
array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'comment' => 'Third new comment',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 1
|
|
|
|
))),
|
|
|
|
array('atomic' => false)
|
|
|
|
);
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->findById(2);
|
|
|
|
$expected = array(
|
|
|
|
'First Comment for Second Article',
|
|
|
|
'Second Comment for Second Article',
|
|
|
|
'First new comment',
|
|
|
|
'Second new comment',
|
|
|
|
'Third new comment'
|
|
|
|
);
|
2012-03-11 01:57:18 +00:00
|
|
|
$result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
|
2012-03-14 14:01:19 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->beforeSaveReturn = false;
|
|
|
|
$result = $TestModel->saveAll(
|
|
|
|
array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'comment' => 'Fourth new comment',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 1
|
|
|
|
))),
|
|
|
|
array('atomic' => false)
|
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('Article' => false), $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->findById(2);
|
|
|
|
$expected = array(
|
|
|
|
'First Comment for Second Article',
|
|
|
|
'Second Comment for Second Article',
|
|
|
|
'First new comment',
|
|
|
|
'Second new comment',
|
|
|
|
'Third new comment'
|
|
|
|
);
|
2012-03-11 01:57:18 +00:00
|
|
|
$result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
|
2012-03-14 14:01:19 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* testSaveAllHasManyValidation method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSaveAllHasManyValidation() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('Article', 'Comment');
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new Article();
|
2009-07-24 22:17:14 +00:00
|
|
|
$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->Comment->validate = array('comment' => 'notBlank');
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->saveAll(array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => '', 'published' => 'Y', 'user_id' => 1),
|
|
|
|
)
|
2010-03-27 23:59:42 +00:00
|
|
|
), array('validate' => true));
|
2011-07-25 07:39:03 +00:00
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$expected = array('Comment' => array(
|
|
|
|
array('comment' => array('This field cannot be left blank'))
|
|
|
|
));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->validationErrors);
|
2011-07-25 07:39:03 +00:00
|
|
|
$expected = array(
|
|
|
|
array('comment' => array('This field cannot be left blank'))
|
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->Comment->validationErrors);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$result = $TestModel->saveAll(array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'comment' => '',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 1
|
|
|
|
))
|
|
|
|
), array('validate' => 'first'));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test saveAll with transactions and ensure there is no missing rollback.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAllManyRowsTransactionNoRollback() {
|
|
|
|
$this->loadFixtures('Post');
|
|
|
|
|
2014-08-01 21:41:00 +00:00
|
|
|
$Post = new TestPost();
|
|
|
|
$Post->validate = array(
|
2015-05-17 20:27:16 +00:00
|
|
|
'title' => array('rule' => array('notBlank'))
|
2014-08-01 21:41:00 +00:00
|
|
|
);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
2014-08-01 21:41:00 +00:00
|
|
|
// If validation error occurs, rollback() should be called.
|
|
|
|
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
|
|
|
|
$db->expects($this->once())->method('begin')->will($this->returnValue(true));
|
|
|
|
$db->expects($this->never())->method('commit');
|
2011-07-25 07:39:03 +00:00
|
|
|
$db->expects($this->once())->method('rollback');
|
|
|
|
|
2014-03-01 18:01:36 +00:00
|
|
|
$Post->setDataSourceObject($db);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
2014-08-01 21:41:00 +00:00
|
|
|
$data = array(
|
|
|
|
array('author_id' => 1, 'title' => 'New Fourth Post'),
|
|
|
|
array('author_id' => 1, 'title' => '')
|
2011-07-25 07:39:03 +00:00
|
|
|
);
|
2014-08-01 21:41:00 +00:00
|
|
|
$Post->saveAll($data, array('atomic' => true, 'validate' => true));
|
|
|
|
|
2014-08-02 01:12:33 +00:00
|
|
|
// If exception thrown, rollback() should be called too.
|
|
|
|
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
|
|
|
|
$db->expects($this->once())->method('begin')->will($this->returnValue(true));
|
|
|
|
$db->expects($this->never())->method('commit');
|
|
|
|
$db->expects($this->once())->method('rollback');
|
|
|
|
|
|
|
|
$Post->setDataSourceObject($db);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
array('author_id' => 1, 'title' => 'New Fourth Post'),
|
2014-08-02 01:12:33 +00:00
|
|
|
array('author_id' => 1, 'title' => 'New Fifth Post', 'body' => $db->expression('PDO_EXCEPTION()'))
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$Post->saveAll($data, array('atomic' => true, 'validate' => true));
|
|
|
|
$this->fail('No exception thrown');
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
}
|
|
|
|
|
2014-08-01 21:41:00 +00:00
|
|
|
// Otherwise, commit() should be called.
|
|
|
|
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
|
|
|
|
$db->expects($this->once())->method('begin')->will($this->returnValue(true));
|
|
|
|
$db->expects($this->once())->method('commit');
|
|
|
|
$db->expects($this->never())->method('rollback');
|
|
|
|
|
|
|
|
$Post->setDataSourceObject($db);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
array('author_id' => 1, 'title' => 'New Fourth Post'),
|
2014-08-01 21:41:00 +00:00
|
|
|
array('author_id' => 1, 'title' => 'New Fifth Post')
|
2011-07-25 07:39:03 +00:00
|
|
|
);
|
2014-03-01 18:01:36 +00:00
|
|
|
$Post->saveAll($data, array('atomic' => true, 'validate' => true));
|
2011-07-25 07:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test saveAll with transactions and ensure there is no missing rollback.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAllAssociatedTransactionNoRollback() {
|
2014-08-01 21:41:00 +00:00
|
|
|
$this->loadFixtures('Post', 'Author');
|
2011-07-25 07:39:03 +00:00
|
|
|
|
2014-08-01 21:41:00 +00:00
|
|
|
$Post = new TestPost();
|
|
|
|
$Post->Author->validate = array(
|
2015-05-17 20:27:16 +00:00
|
|
|
'user' => array('rule' => array('notBlank'))
|
2014-08-01 21:41:00 +00:00
|
|
|
);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
2014-08-01 21:41:00 +00:00
|
|
|
// If validation error occurs, rollback() should be called.
|
|
|
|
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
|
|
|
|
$db->expects($this->once())->method('begin')->will($this->returnValue(true));
|
|
|
|
$db->expects($this->never())->method('commit');
|
2011-07-25 07:39:03 +00:00
|
|
|
$db->expects($this->once())->method('rollback');
|
|
|
|
|
2014-03-01 18:01:36 +00:00
|
|
|
$Post->setDataSourceObject($db);
|
|
|
|
$Post->Author->setDataSourceObject($db);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
2014-08-01 21:41:00 +00:00
|
|
|
$data = array(
|
|
|
|
'Post' => array(
|
|
|
|
'title' => 'New post',
|
|
|
|
'body' => 'Content',
|
|
|
|
'published' => 'Y'
|
|
|
|
),
|
|
|
|
'Author' => array(
|
|
|
|
'user' => '',
|
|
|
|
'password' => "sekret"
|
|
|
|
)
|
2011-07-25 07:39:03 +00:00
|
|
|
);
|
2014-08-01 21:41:00 +00:00
|
|
|
$Post->saveAll($data, array('validate' => true));
|
|
|
|
|
2014-08-02 01:12:33 +00:00
|
|
|
// If exception thrown, rollback() should be called too.
|
|
|
|
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
|
|
|
|
$db->expects($this->once())->method('begin')->will($this->returnValue(true));
|
|
|
|
$db->expects($this->never())->method('commit');
|
|
|
|
$db->expects($this->once())->method('rollback');
|
|
|
|
|
|
|
|
$Post->setDataSourceObject($db);
|
|
|
|
$Post->Author->setDataSourceObject($db);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Post' => array(
|
|
|
|
'title' => 'New post',
|
|
|
|
'body' => $db->expression('PDO_EXCEPTION()'),
|
|
|
|
'published' => 'Y'
|
|
|
|
),
|
|
|
|
'Author' => array(
|
|
|
|
'user' => 'New user',
|
|
|
|
'password' => "sekret"
|
|
|
|
)
|
2011-07-25 07:39:03 +00:00
|
|
|
);
|
|
|
|
|
2014-08-02 01:12:33 +00:00
|
|
|
try {
|
|
|
|
$Post->saveAll($data, array('validate' => true));
|
|
|
|
$this->fail('No exception thrown');
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
}
|
|
|
|
|
2014-08-01 21:41:00 +00:00
|
|
|
// Otherwise, commit() should be called.
|
|
|
|
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
|
|
|
|
$db->expects($this->once())->method('begin')->will($this->returnValue(true));
|
|
|
|
$db->expects($this->once())->method('commit');
|
|
|
|
$db->expects($this->never())->method('rollback');
|
|
|
|
|
|
|
|
$Post->setDataSourceObject($db);
|
|
|
|
$Post->Author->setDataSourceObject($db);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Post' => array(
|
|
|
|
'title' => 'New post',
|
|
|
|
'body' => 'Content',
|
|
|
|
'published' => 'Y'
|
|
|
|
),
|
|
|
|
'Author' => array(
|
2014-08-01 21:41:00 +00:00
|
|
|
'user' => 'New user',
|
2011-07-25 07:39:03 +00:00
|
|
|
'password' => "sekret"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$Post->saveAll($data, array('validate' => true));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test saveAll with nested saveAll call.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAllNestedSaveAll() {
|
|
|
|
$this->loadFixtures('Sample');
|
|
|
|
$TransactionTestModel = new TransactionTestModel();
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
array('apple_id' => 1, 'name' => 'sample5'),
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertTrue($TransactionTestModel->saveAll($data, array('atomic' => true)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveAllTransaction method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAllTransaction() {
|
|
|
|
$this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
|
|
|
|
$TestModel = new Post();
|
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->validate = array('title' => 'notBlank');
|
2011-07-25 07:39:03 +00:00
|
|
|
$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($TestModel->saveAll($data));
|
|
|
|
|
|
|
|
$result = $TestModel->find('all', array('recursive' => -1));
|
|
|
|
$expected = 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'
|
|
|
|
)));
|
|
|
|
|
|
|
|
if (count($result) != 3) {
|
|
|
|
// Database doesn't support transactions
|
|
|
|
$expected[] = array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '4',
|
|
|
|
'author_id' => 1,
|
|
|
|
'title' => 'New Fourth Post',
|
|
|
|
'body' => null,
|
|
|
|
'published' => 'N',
|
2012-03-28 15:49:41 +00:00
|
|
|
'created' => self::date(),
|
|
|
|
'updated' => self::date()
|
2011-07-25 07:39:03 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$expected[] = array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '5',
|
|
|
|
'author_id' => 1,
|
|
|
|
'title' => 'New Fifth Post',
|
|
|
|
'body' => null,
|
|
|
|
'published' => 'N',
|
2012-03-28 15:49:41 +00:00
|
|
|
'created' => self::date(),
|
|
|
|
'updated' => self::date()
|
2011-07-25 07:39:03 +00:00
|
|
|
));
|
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2011-07-25 07:39:03 +00:00
|
|
|
// Skip the rest of the transactional tests
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
array('author_id' => 1, 'title' => 'New Fourth Post'),
|
|
|
|
array('author_id' => 1, 'title' => ''),
|
|
|
|
array('author_id' => 1, 'title' => 'New Sixth Post')
|
|
|
|
);
|
|
|
|
$this->assertFalse($TestModel->saveAll($data));
|
|
|
|
|
|
|
|
$result = $TestModel->find('all', array('recursive' => -1));
|
|
|
|
$expected = 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'
|
|
|
|
)));
|
|
|
|
|
|
|
|
if (count($result) != 3) {
|
|
|
|
// Database doesn't support transactions
|
|
|
|
$expected[] = array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '4',
|
|
|
|
'author_id' => 1,
|
|
|
|
'title' => 'New Fourth Post',
|
|
|
|
'body' => 'Third Post Body',
|
|
|
|
'published' => 'N',
|
2012-03-28 15:49:41 +00:00
|
|
|
'created' => self::date(),
|
|
|
|
'updated' => self::date()
|
2011-07-25 07:39:03 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$expected[] = array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '5',
|
|
|
|
'author_id' => 1,
|
|
|
|
'title' => 'Third Post',
|
|
|
|
'body' => 'Third Post Body',
|
|
|
|
'published' => 'N',
|
2012-03-28 15:49:41 +00:00
|
|
|
'created' => self::date(),
|
|
|
|
'updated' => self::date()
|
2011-07-25 07:39:03 +00:00
|
|
|
));
|
|
|
|
}
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->validate = array('title' => 'notBlank');
|
2011-07-25 07:39:03 +00:00
|
|
|
$data = array(
|
|
|
|
array('author_id' => 1, 'title' => 'New Fourth Post'),
|
|
|
|
array('author_id' => 1, 'title' => 'New Fifth Post'),
|
|
|
|
array('author_id' => 1, 'title' => 'New Sixth Post')
|
|
|
|
);
|
|
|
|
$this->assertTrue($TestModel->saveAll($data));
|
|
|
|
|
|
|
|
$result = $TestModel->find('all', array(
|
|
|
|
'recursive' => -1,
|
2013-06-09 15:39:48 +00:00
|
|
|
'fields' => array('author_id', 'title', 'body', 'published'),
|
2012-02-10 22:51:41 +00:00
|
|
|
'order' => array('Post.created' => 'ASC')
|
2011-07-25 07:39:03 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
array('Post' => array(
|
|
|
|
'author_id' => 1,
|
|
|
|
'title' => 'First Post',
|
|
|
|
'body' => 'First Post Body',
|
|
|
|
'published' => 'Y'
|
|
|
|
)),
|
|
|
|
array('Post' => array(
|
|
|
|
'author_id' => 3,
|
|
|
|
'title' => 'Second Post',
|
|
|
|
'body' => 'Second Post Body',
|
|
|
|
'published' => 'Y'
|
|
|
|
)),
|
|
|
|
array('Post' => array(
|
|
|
|
'author_id' => 1,
|
|
|
|
'title' => 'Third Post',
|
|
|
|
'body' => 'Third Post Body',
|
|
|
|
'published' => 'Y'
|
|
|
|
)),
|
|
|
|
array('Post' => array(
|
|
|
|
'author_id' => 1,
|
|
|
|
'title' => 'New Fourth Post',
|
|
|
|
'body' => '',
|
|
|
|
'published' => 'N'
|
|
|
|
)),
|
|
|
|
array('Post' => array(
|
|
|
|
'author_id' => 1,
|
|
|
|
'title' => 'New Fifth Post',
|
|
|
|
'body' => '',
|
|
|
|
'published' => 'N'
|
|
|
|
)),
|
|
|
|
array('Post' => array(
|
|
|
|
'author_id' => 1,
|
|
|
|
'title' => 'New Sixth Post',
|
|
|
|
'body' => '',
|
|
|
|
'published' => 'N'
|
|
|
|
)));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2011-07-25 07:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveAllValidation method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAllValidation() {
|
|
|
|
$this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
|
|
|
|
$TestModel = new Post();
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
array(
|
|
|
|
'id' => '1',
|
|
|
|
'title' => 'Baleeted First Post',
|
|
|
|
'body' => 'Baleeted!',
|
|
|
|
'published' => 'N'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => '2',
|
|
|
|
'title' => 'Just update the title'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'title' => 'Creating a fourth post',
|
|
|
|
'body' => 'Fourth post body',
|
|
|
|
'author_id' => 2
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertTrue($TestModel->saveAll($data));
|
|
|
|
|
|
|
|
$result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
|
|
|
|
$expected = array(
|
|
|
|
array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '1',
|
|
|
|
'author_id' => '1',
|
|
|
|
'title' => 'Baleeted First Post',
|
|
|
|
'body' => 'Baleeted!',
|
|
|
|
'published' => 'N',
|
2011-10-05 14:46:38 +00:00
|
|
|
'created' => '2007-03-18 10:39:23'
|
2011-07-25 07:39:03 +00:00
|
|
|
)),
|
|
|
|
array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '2',
|
|
|
|
'author_id' => '3',
|
|
|
|
'title' => 'Just update the title',
|
|
|
|
'body' => 'Second Post Body',
|
2012-05-15 14:57:42 +00:00
|
|
|
'published' => 'Y',
|
2011-10-05 14:46:38 +00:00
|
|
|
'created' => '2007-03-18 10:41:23'
|
2011-07-25 07:39:03 +00:00
|
|
|
)),
|
|
|
|
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'
|
|
|
|
)),
|
|
|
|
array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '4',
|
|
|
|
'author_id' => '2',
|
|
|
|
'title' => 'Creating a fourth post',
|
|
|
|
'body' => 'Fourth post body',
|
2011-10-05 16:46:00 +00:00
|
|
|
'published' => 'N'
|
2011-07-25 07:39:03 +00:00
|
|
|
)));
|
2012-03-28 15:49:41 +00:00
|
|
|
$this->assertEquals(self::date(), $result[0]['Post']['updated']);
|
|
|
|
$this->assertEquals(self::date(), $result[1]['Post']['updated']);
|
|
|
|
$this->assertEquals(self::date(), $result[3]['Post']['created']);
|
|
|
|
$this->assertEquals(self::date(), $result[3]['Post']['updated']);
|
2011-10-05 14:46:38 +00:00
|
|
|
unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
|
2011-10-05 16:46:00 +00:00
|
|
|
unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->validate = array('title' => 'notBlank', 'author_id' => 'numeric');
|
2011-07-25 07:39:03 +00:00
|
|
|
$data = array(
|
|
|
|
array(
|
|
|
|
'id' => '1',
|
|
|
|
'title' => 'Un-Baleeted First Post',
|
|
|
|
'body' => 'Not Baleeted!',
|
|
|
|
'published' => 'Y'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => '2',
|
|
|
|
'title' => '',
|
|
|
|
'body' => 'Trying to get away with an empty title'
|
|
|
|
));
|
|
|
|
$result = $TestModel->saveAll($data);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
|
|
|
|
$errors = array(1 => array('title' => array('This field cannot be left blank')));
|
|
|
|
$transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
|
|
|
|
if (!$transactionWorked) {
|
|
|
|
$this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
|
|
|
|
$this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
|
|
|
|
}
|
|
|
|
|
2012-03-22 11:18:57 +00:00
|
|
|
$this->assertEquals($errors, $TestModel->validationErrors);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->validate = array('title' => 'notBlank', 'author_id' => 'numeric');
|
2011-07-25 07:39:03 +00:00
|
|
|
$data = array(
|
|
|
|
array(
|
|
|
|
'id' => '1',
|
|
|
|
'title' => 'Un-Baleeted First Post',
|
|
|
|
'body' => 'Not Baleeted!',
|
|
|
|
'published' => 'Y'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => '2',
|
|
|
|
'title' => '',
|
|
|
|
'body' => 'Trying to get away with an empty title'
|
|
|
|
));
|
|
|
|
$result = $TestModel->saveAll($data, array('validate' => true, 'atomic' => false));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array(true, false), $result);
|
2011-07-25 07:39:03 +00:00
|
|
|
$result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
|
|
|
|
$errors = array(1 => array('title' => array('This field cannot be left blank')));
|
|
|
|
$expected = array(
|
|
|
|
array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '1',
|
|
|
|
'author_id' => '1',
|
|
|
|
'title' => 'Un-Baleeted First Post',
|
|
|
|
'body' => 'Not Baleeted!',
|
|
|
|
'published' => 'Y',
|
2011-10-03 19:21:07 +00:00
|
|
|
'created' => '2007-03-18 10:39:23'
|
|
|
|
)
|
|
|
|
),
|
2011-07-25 07:39:03 +00:00
|
|
|
array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '2',
|
|
|
|
'author_id' => '3',
|
|
|
|
'title' => 'Just update the title',
|
|
|
|
'body' => 'Second Post Body',
|
2012-05-15 14:57:42 +00:00
|
|
|
'published' => 'Y',
|
2011-10-03 19:21:07 +00:00
|
|
|
'created' => '2007-03-18 10:41:23'
|
|
|
|
)
|
|
|
|
),
|
2011-07-25 07:39:03 +00:00
|
|
|
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'
|
2011-10-03 19:21:07 +00:00
|
|
|
)
|
|
|
|
),
|
2011-07-25 07:39:03 +00:00
|
|
|
array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '4',
|
|
|
|
'author_id' => '2',
|
|
|
|
'title' => 'Creating a fourth post',
|
|
|
|
'body' => 'Fourth post body',
|
2011-10-03 19:21:07 +00:00
|
|
|
'published' => 'N'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2011-10-28 05:01:17 +00:00
|
|
|
|
2012-03-28 15:49:41 +00:00
|
|
|
$this->assertEquals(self::date(), $result[0]['Post']['updated']);
|
|
|
|
$this->assertEquals(self::date(), $result[1]['Post']['updated']);
|
|
|
|
$this->assertEquals(self::date(), $result[3]['Post']['updated']);
|
|
|
|
$this->assertEquals(self::date(), $result[3]['Post']['created']);
|
2011-10-03 19:21:07 +00:00
|
|
|
unset(
|
|
|
|
$result[0]['Post']['updated'], $result[1]['Post']['updated'],
|
|
|
|
$result[3]['Post']['updated'], $result[3]['Post']['created']
|
|
|
|
);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2012-03-22 11:18:57 +00:00
|
|
|
$this->assertEquals($errors, $TestModel->validationErrors);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
array(
|
|
|
|
'id' => '1',
|
|
|
|
'title' => 'Re-Baleeted First Post',
|
|
|
|
'body' => 'Baleeted!',
|
|
|
|
'published' => 'N'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => '2',
|
|
|
|
'title' => '',
|
|
|
|
'body' => 'Trying to get away with an empty title'
|
|
|
|
));
|
|
|
|
$this->assertFalse($TestModel->saveAll($data, array('validate' => 'first')));
|
|
|
|
|
|
|
|
$result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
|
2011-10-03 19:21:07 +00:00
|
|
|
unset(
|
|
|
|
$result[0]['Post']['updated'], $result[1]['Post']['updated'],
|
|
|
|
$result[3]['Post']['updated'], $result[3]['Post']['created']
|
|
|
|
);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2012-03-22 11:18:57 +00:00
|
|
|
$this->assertEquals($errors, $TestModel->validationErrors);
|
2011-07-25 07:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveAllValidationOnly method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAllValidationOnly() {
|
|
|
|
$this->loadFixtures('Comment', 'Attachment');
|
|
|
|
$TestModel = new Comment();
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->Attachment->validate = array('attachment' => 'notBlank');
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => 'This is the comment'
|
|
|
|
),
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => ''
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $TestModel->saveAll($data, array('validate' => 'only'));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$TestModel = new Article();
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->validate = array('title' => 'notBlank');
|
2011-07-25 07:39:03 +00:00
|
|
|
$result = $TestModel->saveAll(
|
|
|
|
array(
|
|
|
|
0 => array('title' => ''),
|
|
|
|
1 => array('title' => 'title 1'),
|
|
|
|
2 => array('title' => 'title 2'),
|
|
|
|
),
|
2011-12-01 07:21:31 +00:00
|
|
|
array('validate' => 'only')
|
2011-07-25 07:39:03 +00:00
|
|
|
);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
$expected = array(
|
|
|
|
0 => array('title' => array('This field cannot be left blank')),
|
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->validationErrors);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$result = $TestModel->saveAll(
|
|
|
|
array(
|
|
|
|
0 => array('title' => 'title 0'),
|
|
|
|
1 => array('title' => ''),
|
|
|
|
2 => array('title' => 'title 2'),
|
|
|
|
),
|
2011-12-01 07:21:31 +00:00
|
|
|
array('validate' => 'only')
|
2011-07-25 07:39:03 +00:00
|
|
|
);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
$expected = array(
|
|
|
|
1 => array('title' => array('This field cannot be left blank')),
|
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->validationErrors);
|
2011-07-25 07:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveAllValidateFirst method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAllValidateFirst() {
|
2012-04-04 23:33:57 +00:00
|
|
|
$this->loadFixtures('Article', 'Comment', 'Attachment', 'User', 'ArticlesTag', 'Tag');
|
2011-07-25 07:39:03 +00:00
|
|
|
$model = new Article();
|
|
|
|
$model->deleteAll(true);
|
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$model->Comment->validate = array('comment' => 'notBlank');
|
2011-07-25 07:39:03 +00:00
|
|
|
$result = $model->saveAll(array(
|
|
|
|
'Article' => array(
|
|
|
|
'title' => 'Post with Author',
|
2013-07-05 12:36:40 +00:00
|
|
|
'body' => 'This post will be saved author'
|
2011-07-25 07:39:03 +00:00
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment'),
|
|
|
|
array('comment' => '')
|
|
|
|
)
|
|
|
|
), array('validate' => 'first'));
|
|
|
|
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = $model->find('all');
|
2012-10-26 22:18:05 +00:00
|
|
|
$this->assertSame(array(), $result);
|
2011-07-25 07:39:03 +00:00
|
|
|
$expected = array('Comment' => array(
|
|
|
|
1 => array('comment' => array('This field cannot be left blank'))
|
|
|
|
));
|
|
|
|
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected['Comment'], $model->Comment->validationErrors);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertSame($model->Comment->find('count'), 0);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$result = $model->saveAll(
|
|
|
|
array(
|
|
|
|
'Article' => array(
|
|
|
|
'title' => 'Post with Author',
|
|
|
|
'body' => 'This post will be saved with an author',
|
|
|
|
'user_id' => 2
|
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'comment' => 'Only new comment',
|
|
|
|
'user_id' => 2
|
|
|
|
))),
|
|
|
|
array('validate' => 'first')
|
|
|
|
);
|
|
|
|
|
2013-09-18 22:17:21 +00:00
|
|
|
$this->assertTrue($result);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$result = $model->Comment->find('all');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertSame(count($result), 1);
|
2012-04-04 23:33:57 +00:00
|
|
|
$result = Hash::extract($result, '{n}.Comment.article_id');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(4, $result[0]);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$model->deleteAll(true);
|
|
|
|
$data = array(
|
|
|
|
'Article' => array(
|
|
|
|
'title' => 'Post with Author saveAlled from comment',
|
|
|
|
'body' => 'This post will be saved with an author',
|
|
|
|
'user_id' => 2
|
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => 'Only new comment', 'user_id' => 2
|
|
|
|
));
|
|
|
|
|
|
|
|
$result = $model->Comment->saveAll($data, array('validate' => 'first'));
|
|
|
|
$this->assertFalse(empty($result));
|
|
|
|
|
|
|
|
$result = $model->find('all');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(
|
2011-07-25 07:39:03 +00:00
|
|
|
$result[0]['Article']['title'],
|
|
|
|
'Post with Author saveAlled from comment'
|
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('Only new comment', $result[0]['Comment'][0]['comment']);
|
2011-07-25 07:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test saveAll()'s return is correct when using atomic = false and validate = first.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAllValidateFirstAtomicFalse() {
|
2012-04-04 23:33:57 +00:00
|
|
|
$this->loadFixtures('Something');
|
2011-07-25 07:39:03 +00:00
|
|
|
$Something = new Something();
|
|
|
|
$invalidData = array(
|
|
|
|
array(
|
|
|
|
'title' => 'foo',
|
|
|
|
'body' => 'bar',
|
|
|
|
'published' => 'baz',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'body' => 3,
|
2011-12-16 07:00:07 +00:00
|
|
|
'published' => 'sd',
|
2011-07-25 07:39:03 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
$Something->create();
|
|
|
|
$Something->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'rule' => 'alphaNumeric',
|
|
|
|
'required' => true,
|
|
|
|
),
|
|
|
|
'body' => array(
|
|
|
|
'rule' => 'alphaNumeric',
|
|
|
|
'required' => true,
|
|
|
|
'allowEmpty' => true,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$result = $Something->saveAll($invalidData, array(
|
|
|
|
'atomic' => false,
|
|
|
|
'validate' => 'first',
|
|
|
|
));
|
|
|
|
$expected = array(true, false);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$Something = new Something();
|
|
|
|
$validData = array(
|
|
|
|
array(
|
|
|
|
'title' => 'title value',
|
|
|
|
'body' => 'body value',
|
|
|
|
'published' => 'baz',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'title' => 'valid',
|
|
|
|
'body' => 'this body',
|
2011-12-01 07:21:31 +00:00
|
|
|
'published' => 'sd',
|
2011-07-25 07:39:03 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
$Something->create();
|
|
|
|
$result = $Something->saveAll($validData, array(
|
|
|
|
'atomic' => false,
|
|
|
|
'validate' => 'first',
|
|
|
|
));
|
|
|
|
$expected = array(true, true);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2011-07-25 07:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveAllHasManyValidationOnly method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAllHasManyValidationOnly() {
|
|
|
|
$this->loadFixtures('Article', 'Comment', 'Attachment');
|
|
|
|
$TestModel = new Article();
|
|
|
|
$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->Comment->validate = array('comment' => 'notBlank');
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$result = $TestModel->saveAll(
|
|
|
|
array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'id' => 1,
|
|
|
|
'comment' => '',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 1),
|
|
|
|
array(
|
|
|
|
'id' => 2,
|
|
|
|
'comment' =>
|
|
|
|
'comment',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 1
|
|
|
|
))),
|
|
|
|
array('validate' => 'only')
|
|
|
|
);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = $TestModel->saveAll(
|
|
|
|
array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'id' => 1,
|
|
|
|
'comment' => '',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 1
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => 2,
|
|
|
|
'comment' => 'comment',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 1
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => 3,
|
|
|
|
'comment' => '',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 1
|
|
|
|
))),
|
|
|
|
array(
|
|
|
|
'validate' => 'only',
|
|
|
|
'atomic' => false
|
|
|
|
));
|
|
|
|
$expected = array(
|
|
|
|
'Article' => true,
|
|
|
|
'Comment' => array(false, true, false)
|
|
|
|
);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertSame($expected, $result);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$expected = array('Comment' => array(
|
|
|
|
0 => array('comment' => array('This field cannot be left blank')),
|
|
|
|
2 => array('comment' => array('This field cannot be left blank'))
|
|
|
|
));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->validationErrors);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
0 => array('comment' => array('This field cannot be left blank')),
|
|
|
|
2 => array('comment' => array('This field cannot be left blank'))
|
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->Comment->validationErrors);
|
2011-07-25 07:39:03 +00:00
|
|
|
}
|
|
|
|
|
2012-10-11 12:03:59 +00:00
|
|
|
/**
|
|
|
|
* test that saveAll still behaves like previous versions (does not necessarily need a first argument)
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAllWithSet() {
|
|
|
|
$this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
|
|
|
|
$data = array(
|
|
|
|
'Article' => array(
|
|
|
|
'user_id' => 1,
|
|
|
|
'title' => 'Article Has and belongs to Many Tags'
|
|
|
|
),
|
|
|
|
'Tag' => array(
|
|
|
|
'Tag' => array(1, 2)
|
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'comment' => 'Article comment',
|
|
|
|
'user_id' => 1
|
|
|
|
)));
|
|
|
|
$Article = new Article();
|
|
|
|
$Article->set($data);
|
|
|
|
$result = $Article->saveAll();
|
|
|
|
$this->assertFalse(empty($result));
|
|
|
|
}
|
|
|
|
|
2011-07-25 07:39:03 +00:00
|
|
|
/**
|
2012-02-23 23:29:53 +00:00
|
|
|
* test that saveAll behaves like plain save() when supplied empty data
|
2011-07-25 07:39:03 +00:00
|
|
|
*
|
2013-06-25 20:58:30 +00:00
|
|
|
* @link https://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
|
2011-07-25 07:39:03 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAllEmptyData() {
|
|
|
|
$this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
|
|
|
|
|
|
|
|
$this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
|
|
|
|
$model = new Article();
|
|
|
|
$result = $model->saveAll(array(), array('validate' => 'first'));
|
|
|
|
$this->assertFalse(empty($result));
|
|
|
|
|
|
|
|
$model = new ProductUpdateAll();
|
2012-10-11 12:03:59 +00:00
|
|
|
$result = $model->saveAll();
|
2011-07-25 07:39:03 +00:00
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveAssociated method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAssociated() {
|
|
|
|
$this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
|
|
|
|
$TestModel = new Post();
|
|
|
|
|
|
|
|
$result = $TestModel->find('all');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(3, count($result));
|
2011-07-25 07:39:03 +00:00
|
|
|
$this->assertFalse(isset($result[3]));
|
|
|
|
|
|
|
|
$TestModel->saveAssociated(array(
|
|
|
|
'Post' => array(
|
|
|
|
'title' => 'Post with Author',
|
|
|
|
'body' => 'This post will be saved with an author'
|
|
|
|
),
|
|
|
|
'Author' => array(
|
|
|
|
'user' => 'bob',
|
|
|
|
'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
|
|
|
|
)));
|
|
|
|
|
2012-05-15 14:57:42 +00:00
|
|
|
$result = $TestModel->find('all', array('order' => array('Post.id ' => 'ASC')));
|
2011-07-25 07:39:03 +00:00
|
|
|
$expected = array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '4',
|
|
|
|
'author_id' => '5',
|
|
|
|
'title' => 'Post with Author',
|
|
|
|
'body' => 'This post will be saved with an author',
|
2011-10-03 19:21:07 +00:00
|
|
|
'published' => 'N'
|
2011-07-25 07:39:03 +00:00
|
|
|
),
|
|
|
|
'Author' => array(
|
|
|
|
'id' => '5',
|
|
|
|
'user' => 'bob',
|
|
|
|
'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
|
|
|
|
'test' => 'working'
|
|
|
|
));
|
2012-03-28 15:49:41 +00:00
|
|
|
$this->assertEquals(self::date(), $result[3]['Post']['updated']);
|
|
|
|
$this->assertEquals(self::date(), $result[3]['Post']['created']);
|
|
|
|
$this->assertEquals(self::date(), $result[3]['Author']['created']);
|
|
|
|
$this->assertEquals(self::date(), $result[3]['Author']['updated']);
|
2011-10-03 19:21:07 +00:00
|
|
|
unset(
|
|
|
|
$result[3]['Post']['updated'], $result[3]['Post']['created'],
|
|
|
|
$result[3]['Author']['updated'], $result[3]['Author']['created']
|
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $result[3]);
|
|
|
|
$this->assertEquals(4, count($result));
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$TestModel = new Comment();
|
|
|
|
$result = $TestModel->saveAssociated(array(
|
|
|
|
'Comment' => array(
|
|
|
|
'article_id' => 2,
|
|
|
|
'user_id' => 2,
|
|
|
|
'comment' => 'New comment with attachment',
|
|
|
|
'published' => 'Y'
|
|
|
|
),
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => 'some_file.tgz'
|
|
|
|
)));
|
|
|
|
$this->assertFalse(empty($result));
|
|
|
|
|
|
|
|
$result = $TestModel->find('all');
|
|
|
|
$expected = array(
|
|
|
|
'id' => '7',
|
|
|
|
'article_id' => '2',
|
|
|
|
'user_id' => '2',
|
|
|
|
'comment' => 'New comment with attachment',
|
2011-10-03 19:21:07 +00:00
|
|
|
'published' => 'Y'
|
2011-07-25 07:39:03 +00:00
|
|
|
);
|
2012-03-28 15:49:41 +00:00
|
|
|
$this->assertEquals(self::date(), $result[6]['Comment']['updated']);
|
|
|
|
$this->assertEquals(self::date(), $result[6]['Comment']['created']);
|
2011-10-03 19:21:07 +00:00
|
|
|
unset($result[6]['Comment']['updated'], $result[6]['Comment']['created']);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $result[6]['Comment']);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'id' => '2',
|
|
|
|
'comment_id' => '7',
|
2011-10-03 19:21:07 +00:00
|
|
|
'attachment' => 'some_file.tgz'
|
2011-07-25 07:39:03 +00:00
|
|
|
);
|
2012-03-28 15:49:41 +00:00
|
|
|
$this->assertEquals(self::date(), $result[6]['Attachment']['updated']);
|
|
|
|
$this->assertEquals(self::date(), $result[6]['Attachment']['created']);
|
2011-10-03 19:21:07 +00:00
|
|
|
unset($result[6]['Attachment']['updated'], $result[6]['Attachment']['created']);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $result[6]['Attachment']);
|
2011-07-25 07:39:03 +00:00
|
|
|
}
|
|
|
|
|
2012-10-20 19:12:05 +00:00
|
|
|
/**
|
|
|
|
* Test that validate = first, atomic = false works when associated records
|
|
|
|
* fail validation.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAssociatedAtomicFalseValidateFirstWithErrors() {
|
|
|
|
$this->loadFixtures('Comment', 'Article', 'User');
|
|
|
|
$Article = ClassRegistry::init('Article');
|
|
|
|
$Article->Comment->validator()->add('comment', array(
|
2015-05-17 20:27:16 +00:00
|
|
|
array('rule' => 'notBlank')
|
2012-10-20 19:12:05 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Article' => array(
|
|
|
|
'user_id' => 1,
|
|
|
|
'title' => 'Foo',
|
|
|
|
'body' => 'text',
|
|
|
|
'published' => 'N'
|
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'user_id' => 1,
|
|
|
|
'comment' => '',
|
|
|
|
'published' => 'N',
|
|
|
|
)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$Article->saveAssociated(
|
|
|
|
$data,
|
|
|
|
array('validate' => 'first', 'atomic' => false)
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $Article->validationErrors;
|
|
|
|
$expected = array(
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
2013-08-07 23:03:21 +00:00
|
|
|
'comment' => array('This field cannot be left blank')
|
2012-10-20 19:12:05 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
2011-07-25 07:39:03 +00:00
|
|
|
/**
|
|
|
|
* testSaveMany method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveMany() {
|
|
|
|
$this->loadFixtures('Post');
|
|
|
|
$TestModel = new Post();
|
|
|
|
$TestModel->deleteAll(true);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array(), $TestModel->find('all'));
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
// SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
|
|
|
|
$this->db->truncate($TestModel);
|
|
|
|
|
|
|
|
$TestModel->saveMany(array(
|
|
|
|
array(
|
|
|
|
'title' => 'Multi-record post 1',
|
|
|
|
'body' => 'First multi-record post',
|
|
|
|
'author_id' => 2
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'title' => 'Multi-record post 2',
|
|
|
|
'body' => 'Second multi-record post',
|
|
|
|
'author_id' => 2
|
|
|
|
)));
|
|
|
|
|
|
|
|
$result = $TestModel->find('all', array(
|
|
|
|
'recursive' => -1,
|
|
|
|
'order' => 'Post.id ASC'
|
|
|
|
));
|
|
|
|
$expected = array(
|
|
|
|
array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '1',
|
|
|
|
'author_id' => '2',
|
|
|
|
'title' => 'Multi-record post 1',
|
|
|
|
'body' => 'First multi-record post',
|
2011-10-03 19:21:07 +00:00
|
|
|
'published' => 'N'
|
|
|
|
)
|
|
|
|
),
|
2011-07-25 07:39:03 +00:00
|
|
|
array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '2',
|
|
|
|
'author_id' => '2',
|
|
|
|
'title' => 'Multi-record post 2',
|
|
|
|
'body' => 'Second multi-record post',
|
2011-10-03 19:21:07 +00:00
|
|
|
'published' => 'N'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2012-03-28 15:49:41 +00:00
|
|
|
$this->assertEquals(self::date(), $result[0]['Post']['updated']);
|
|
|
|
$this->assertEquals(self::date(), $result[0]['Post']['created']);
|
|
|
|
$this->assertEquals(self::date(), $result[1]['Post']['updated']);
|
|
|
|
$this->assertEquals(self::date(), $result[1]['Post']['created']);
|
2011-10-03 19:21:07 +00:00
|
|
|
unset($result[0]['Post']['updated'], $result[0]['Post']['created']);
|
|
|
|
unset($result[1]['Post']['updated'], $result[1]['Post']['created']);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2011-07-25 07:39:03 +00:00
|
|
|
}
|
|
|
|
|
2013-12-11 17:43:18 +00:00
|
|
|
/**
|
|
|
|
* Test SaveMany with validate=false.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveManyValidateFalse() {
|
|
|
|
$this->loadFixtures('Post');
|
|
|
|
$TestModel = new Post();
|
|
|
|
$TestModel->deleteAll(true);
|
|
|
|
$data = array(
|
|
|
|
array('id' => 1, 'author_id' => 1, 'title' => 'hi'),
|
|
|
|
array('id' => 2, 'author_id' => 1, 'title' => 'bye')
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($data, array('validate' => false));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
2011-07-25 07:39:03 +00:00
|
|
|
/**
|
|
|
|
* Test SaveAssociated with Habtm relations
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAssociatedHabtm() {
|
|
|
|
$this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
|
|
|
|
$data = array(
|
|
|
|
'Article' => array(
|
|
|
|
'user_id' => 1,
|
|
|
|
'title' => 'Article Has and belongs to Many Tags'
|
|
|
|
),
|
|
|
|
'Tag' => array(
|
|
|
|
'Tag' => array(1, 2)
|
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'comment' => 'Article comment',
|
|
|
|
'user_id' => 1
|
|
|
|
)));
|
|
|
|
$Article = new Article();
|
|
|
|
$result = $Article->saveAssociated($data);
|
|
|
|
$this->assertFalse(empty($result));
|
|
|
|
|
|
|
|
$result = $Article->read();
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(2, count($result['Tag']));
|
|
|
|
$this->assertEquals('tag1', $result['Tag'][0]['tag']);
|
|
|
|
$this->assertEquals(1, count($result['Comment']));
|
|
|
|
$this->assertEquals(1, count($result['Comment'][0]['comment']));
|
2011-07-25 07:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test SaveAssociated with Habtm relations and extra join table fields
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAssociatedHabtmWithExtraJoinTableFields() {
|
|
|
|
$this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Something' => array(
|
|
|
|
'id' => 4,
|
|
|
|
'title' => 'Extra Fields',
|
|
|
|
'body' => 'Extra Fields Body',
|
|
|
|
'published' => '1'
|
|
|
|
),
|
|
|
|
'SomethingElse' => array(
|
|
|
|
array('something_else_id' => 1, 'doomed' => '1'),
|
|
|
|
array('something_else_id' => 2, 'doomed' => '0'),
|
|
|
|
array('something_else_id' => 3, 'doomed' => '1')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$Something = new Something();
|
|
|
|
$result = $Something->saveAssociated($data);
|
|
|
|
$this->assertFalse(empty($result));
|
|
|
|
$result = $Something->read();
|
|
|
|
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(3, count($result['SomethingElse']));
|
2011-07-25 07:39:03 +00:00
|
|
|
$this->assertTrue(Set::matches('/Something[id=4]', $result));
|
|
|
|
|
|
|
|
$this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
|
|
|
|
$this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
|
|
|
|
$this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
|
|
|
|
|
|
|
|
$this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
|
|
|
|
$this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
|
|
|
|
$this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
|
|
|
|
|
|
|
|
$this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
|
|
|
|
$this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
|
|
|
|
$this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveAssociatedHasOne method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAssociatedHasOne() {
|
|
|
|
$model = new Comment();
|
|
|
|
$model->deleteAll(true);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array(), $model->find('all'));
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$model->Attachment->deleteAll(true);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array(), $model->Attachment->find('all'));
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$this->assertTrue($model->saveAssociated(array(
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => 'Comment with attachment',
|
|
|
|
'article_id' => 1,
|
|
|
|
'user_id' => 1
|
|
|
|
),
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => 'some_file.zip'
|
|
|
|
))));
|
|
|
|
$result = $model->find('all', array('fields' => array(
|
|
|
|
'Comment.id', 'Comment.comment', 'Attachment.id',
|
|
|
|
'Attachment.comment_id', 'Attachment.attachment'
|
|
|
|
)));
|
|
|
|
$expected = array(array(
|
|
|
|
'Comment' => array(
|
|
|
|
'id' => '1',
|
|
|
|
'comment' => 'Comment with attachment'
|
|
|
|
),
|
|
|
|
'Attachment' => array(
|
|
|
|
'id' => '1',
|
|
|
|
'comment_id' => '1',
|
|
|
|
'attachment' => 'some_file.zip'
|
|
|
|
)));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
|
|
|
|
$data = array(
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => 'Comment with attachment',
|
|
|
|
'article_id' => 1,
|
|
|
|
'user_id' => 1
|
|
|
|
),
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => 'some_file.zip'
|
|
|
|
));
|
|
|
|
$this->assertTrue($model->saveAssociated($data, array('validate' => 'first')));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveAssociatedBelongsTo method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAssociatedBelongsTo() {
|
|
|
|
$model = new Comment();
|
|
|
|
$model->deleteAll(true);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array(), $model->find('all'));
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$model->Article->deleteAll(true);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array(), $model->Article->find('all'));
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$this->assertTrue($model->saveAssociated(array(
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => 'Article comment',
|
|
|
|
'article_id' => 1,
|
|
|
|
'user_id' => 1
|
|
|
|
),
|
|
|
|
'Article' => array(
|
|
|
|
'title' => 'Model Associations 101',
|
|
|
|
'user_id' => 1
|
|
|
|
))));
|
|
|
|
$result = $model->find('all', array('fields' => array(
|
|
|
|
'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
|
|
|
|
)));
|
|
|
|
$expected = array(array(
|
|
|
|
'Comment' => array(
|
|
|
|
'id' => '1',
|
|
|
|
'article_id' => '1',
|
|
|
|
'comment' => 'Article comment'
|
|
|
|
),
|
|
|
|
'Article' => array(
|
|
|
|
'id' => '1',
|
|
|
|
'title' => 'Model Associations 101'
|
|
|
|
)));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2011-07-25 07:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveAssociatedHasOneValidation method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAssociatedHasOneValidation() {
|
|
|
|
$model = new Comment();
|
|
|
|
$model->deleteAll(true);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array(), $model->find('all'));
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$model->Attachment->deleteAll(true);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array(), $model->Attachment->find('all'));
|
2011-07-25 07:39:03 +00:00
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$model->validate = array('comment' => 'notBlank');
|
|
|
|
$model->Attachment->validate = array('attachment' => 'notBlank');
|
2011-07-25 07:39:03 +00:00
|
|
|
$model->Attachment->bindModel(array('belongsTo' => array('Comment')));
|
|
|
|
|
2012-03-22 11:18:57 +00:00
|
|
|
$result = $model->saveAssociated(
|
2011-07-25 07:39:03 +00:00
|
|
|
array(
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => '',
|
|
|
|
'article_id' => 1,
|
|
|
|
'user_id' => 1
|
|
|
|
),
|
|
|
|
'Attachment' => array('attachment' => '')
|
2012-01-20 10:02:13 +00:00
|
|
|
)
|
2012-03-22 11:18:57 +00:00
|
|
|
);
|
|
|
|
$this->assertFalse($result);
|
2011-07-25 07:39:03 +00:00
|
|
|
$expected = array(
|
2012-06-18 11:46:19 +00:00
|
|
|
'comment' => array(
|
|
|
|
'This field cannot be left blank'
|
|
|
|
),
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => array(
|
|
|
|
'This field cannot be left blank'
|
|
|
|
)
|
|
|
|
)
|
2011-07-25 07:39:03 +00:00
|
|
|
);
|
2012-06-18 11:46:19 +00:00
|
|
|
$this->assertEquals($expected, $model->validationErrors);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
|
2011-07-25 07:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveAssociatedAtomic method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAssociatedAtomic() {
|
|
|
|
$this->loadFixtures('Article', 'User');
|
|
|
|
$TestModel = new Article();
|
|
|
|
|
|
|
|
$result = $TestModel->saveAssociated(array(
|
|
|
|
'Article' => array(
|
|
|
|
'title' => 'Post with Author',
|
|
|
|
'body' => 'This post will be saved with an author',
|
|
|
|
'user_id' => 2
|
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment', 'user_id' => 2))
|
|
|
|
), array('atomic' => false));
|
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$result = $TestModel->saveAssociated(array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'comment' => 'First new comment',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 1
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'comment' => 'Second new comment',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 2
|
|
|
|
))
|
|
|
|
), array('validate' => true, 'atomic' => false));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
|
2011-07-25 07:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveManyAtomic method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveManyAtomic() {
|
|
|
|
$this->loadFixtures('Article', 'User');
|
|
|
|
$TestModel = new Article();
|
|
|
|
|
|
|
|
$result = $TestModel->saveMany(array(
|
|
|
|
array(
|
|
|
|
'id' => '1',
|
|
|
|
'title' => 'Baleeted First Post',
|
|
|
|
'body' => 'Baleeted!',
|
|
|
|
'published' => 'N'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => '2',
|
|
|
|
'title' => 'Just update the title'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'title' => 'Creating a fourth post',
|
|
|
|
'body' => 'Fourth post body',
|
|
|
|
'user_id' => 2
|
|
|
|
)
|
|
|
|
), array('atomic' => false));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertSame($result, array(true, true, true));
|
2011-07-25 07:39:03 +00:00
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->validate = array('title' => 'notBlank', 'author_id' => 'numeric');
|
2011-07-25 07:39:03 +00:00
|
|
|
$result = $TestModel->saveMany(array(
|
|
|
|
array(
|
|
|
|
'id' => '1',
|
|
|
|
'title' => 'Un-Baleeted First Post',
|
|
|
|
'body' => 'Not Baleeted!',
|
|
|
|
'published' => 'Y'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => '2',
|
|
|
|
'title' => '',
|
|
|
|
'body' => 'Trying to get away with an empty title'
|
|
|
|
)
|
|
|
|
), array('validate' => true, 'atomic' => false));
|
|
|
|
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertSame(array(true, false), $result);
|
2011-07-25 07:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveAssociatedHasMany method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAssociatedHasMany() {
|
|
|
|
$this->loadFixtures('Article', 'Comment');
|
|
|
|
$TestModel = new Article();
|
|
|
|
$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
|
|
|
|
|
|
|
|
$result = $TestModel->saveAssociated(array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
|
|
|
|
array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
|
|
|
));
|
|
|
|
$this->assertFalse(empty($result));
|
|
|
|
|
|
|
|
$result = $TestModel->findById(2);
|
|
|
|
$expected = array(
|
|
|
|
'First Comment for Second Article',
|
|
|
|
'Second Comment for Second Article',
|
|
|
|
'First new comment',
|
|
|
|
'Second new comment'
|
|
|
|
);
|
2012-04-04 23:33:57 +00:00
|
|
|
$this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$result = $TestModel->saveAssociated(
|
|
|
|
array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'comment' => 'Third new comment',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 1
|
|
|
|
))),
|
|
|
|
array('atomic' => false)
|
|
|
|
);
|
|
|
|
$this->assertFalse(empty($result));
|
|
|
|
|
|
|
|
$result = $TestModel->findById(2);
|
|
|
|
$expected = array(
|
|
|
|
'First Comment for Second Article',
|
|
|
|
'Second Comment for Second Article',
|
|
|
|
'First new comment',
|
|
|
|
'Second new comment',
|
|
|
|
'Third new comment'
|
|
|
|
);
|
2012-04-04 23:33:57 +00:00
|
|
|
$this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$TestModel->beforeSaveReturn = false;
|
|
|
|
$result = $TestModel->saveAssociated(
|
|
|
|
array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'comment' => 'Fourth new comment',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 1
|
|
|
|
))),
|
|
|
|
array('atomic' => false)
|
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('Article' => false), $result);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$result = $TestModel->findById(2);
|
|
|
|
$expected = array(
|
|
|
|
'First Comment for Second Article',
|
|
|
|
'Second Comment for Second Article',
|
|
|
|
'First new comment',
|
|
|
|
'Second new comment',
|
|
|
|
'Third new comment'
|
|
|
|
);
|
2012-04-04 23:33:57 +00:00
|
|
|
$this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
|
2011-07-25 07:39:03 +00:00
|
|
|
}
|
|
|
|
|
2012-04-16 20:46:58 +00:00
|
|
|
/**
|
|
|
|
* testSaveAssociatedHasManyEmpty method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAssociatedHasManyEmpty() {
|
|
|
|
$this->loadFixtures('Article', 'Comment');
|
|
|
|
$TestModel = new Article();
|
|
|
|
$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->validate = $TestModel->Comment->validate = array('user_id' => array('notBlank' => array('rule' => 'notBlank', 'required' => true)));
|
2012-04-16 22:22:13 +00:00
|
|
|
|
|
|
|
//empty hasMany data is ignored in save
|
2012-04-16 20:46:58 +00:00
|
|
|
$result = $TestModel->saveAssociated(array(
|
2012-04-16 22:22:13 +00:00
|
|
|
'Article' => array('title' => 'title', 'user_id' => 1),
|
2012-04-16 20:46:58 +00:00
|
|
|
'Comment' => array()
|
|
|
|
), array('validate' => true));
|
|
|
|
$this->assertTrue($result);
|
2012-04-16 22:22:13 +00:00
|
|
|
|
|
|
|
$result = $TestModel->saveAssociated(array(
|
|
|
|
'Article' => array('title' => 'title', 'user_id' => 1),
|
|
|
|
'Comment' => array()
|
|
|
|
), array('validate' => true, 'atomic' => false));
|
|
|
|
$this->assertEquals(array('Article' => true), $result);
|
|
|
|
|
|
|
|
//empty primary data is not ignored
|
|
|
|
$result = $TestModel->saveAssociated(array('Article' => array()), array('validate' => true));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = $TestModel->saveAssociated(array('Article' => array()), array('validate' => true, 'atomic' => false));
|
|
|
|
$this->assertEquals(array('Article' => false), $result);
|
2012-04-16 20:46:58 +00:00
|
|
|
}
|
|
|
|
|
2011-07-25 07:39:03 +00:00
|
|
|
/**
|
|
|
|
* testSaveAssociatedHasManyValidation method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAssociatedHasManyValidation() {
|
|
|
|
$this->loadFixtures('Article', 'Comment');
|
|
|
|
$TestModel = new Article();
|
|
|
|
$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->Comment->validate = array('comment' => 'notBlank');
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$result = $TestModel->saveAssociated(array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => '', 'published' => 'Y', 'user_id' => 1),
|
|
|
|
)
|
|
|
|
), array('validate' => true));
|
|
|
|
$this->assertFalse($result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$expected = array('Comment' => array(
|
2010-12-26 06:40:02 +00:00
|
|
|
array('comment' => array('This field cannot be left blank'))
|
2009-07-24 22:17:14 +00:00
|
|
|
));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->validationErrors);
|
2009-07-24 22:17:14 +00:00
|
|
|
$expected = array(
|
2010-12-26 06:40:02 +00:00
|
|
|
array('comment' => array('This field cannot be left blank'))
|
2009-07-24 22:17:14 +00:00
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->Comment->validationErrors);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2011-07-25 07:39:03 +00:00
|
|
|
$result = $TestModel->saveAssociated(array(
|
2009-07-24 22:17:14 +00:00
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'comment' => '',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 1
|
|
|
|
))
|
2011-03-15 19:26:31 +00:00
|
|
|
), array('validate' => 'first'));
|
|
|
|
$this->assertFalse($result);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2010-06-09 17:48:54 +00:00
|
|
|
/**
|
2011-07-25 07:39:03 +00:00
|
|
|
* test saveMany with transactions and ensure there is no missing rollback.
|
2010-06-09 17:48:54 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-07-25 07:39:03 +00:00
|
|
|
public function testSaveManyTransactionNoRollback() {
|
2010-06-09 17:48:54 +00:00
|
|
|
$this->loadFixtures('Post');
|
|
|
|
|
2014-08-01 21:41:00 +00:00
|
|
|
$Post = new TestPost();
|
|
|
|
$Post->validate = array(
|
2015-05-17 20:27:16 +00:00
|
|
|
'title' => array('rule' => array('notBlank'))
|
2014-08-01 21:41:00 +00:00
|
|
|
);
|
2010-07-16 01:46:52 +00:00
|
|
|
|
2014-08-01 21:41:00 +00:00
|
|
|
// If validation error occurs, rollback() should be called.
|
|
|
|
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
|
|
|
|
$db->expects($this->once())->method('begin')->will($this->returnValue(true));
|
|
|
|
$db->expects($this->never())->method('commit');
|
2010-07-14 02:12:07 +00:00
|
|
|
$db->expects($this->once())->method('rollback');
|
2010-06-09 17:48:54 +00:00
|
|
|
|
2014-03-01 18:01:36 +00:00
|
|
|
$Post->setDataSourceObject($db);
|
2010-06-09 17:48:54 +00:00
|
|
|
|
2014-08-01 21:41:00 +00:00
|
|
|
$data = array(
|
|
|
|
array('author_id' => 1, 'title' => 'New Fourth Post'),
|
|
|
|
array('author_id' => 1, 'title' => '')
|
2010-06-09 17:48:54 +00:00
|
|
|
);
|
2014-08-01 21:41:00 +00:00
|
|
|
$Post->saveMany($data, array('validate' => true));
|
|
|
|
|
2014-08-02 01:12:33 +00:00
|
|
|
// If exception thrown, rollback() should be called too.
|
|
|
|
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
|
|
|
|
$db->expects($this->once())->method('begin')->will($this->returnValue(true));
|
|
|
|
$db->expects($this->never())->method('commit');
|
|
|
|
$db->expects($this->once())->method('rollback');
|
|
|
|
|
|
|
|
$Post->setDataSourceObject($db);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
array('author_id' => 1, 'title' => 'New Fourth Post'),
|
|
|
|
array('author_id' => 1, 'title' => 'New Fifth Post', 'body' => $db->expression('PDO_EXCEPTION()'))
|
2010-06-09 17:48:54 +00:00
|
|
|
);
|
|
|
|
|
2014-08-02 01:12:33 +00:00
|
|
|
try {
|
|
|
|
$Post->saveMany($data, array('validate' => true));
|
|
|
|
$this->fail('No exception thrown');
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
}
|
|
|
|
|
2014-08-01 21:41:00 +00:00
|
|
|
// Otherwise, commit() should be called.
|
|
|
|
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
|
|
|
|
$db->expects($this->once())->method('begin')->will($this->returnValue(true));
|
|
|
|
$db->expects($this->once())->method('commit');
|
|
|
|
$db->expects($this->never())->method('rollback');
|
|
|
|
|
|
|
|
$Post->setDataSourceObject($db);
|
2010-06-09 17:48:54 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
array('author_id' => 1, 'title' => 'New Fourth Post'),
|
2014-08-01 21:41:00 +00:00
|
|
|
array('author_id' => 1, 'title' => 'New Fifth Post')
|
2010-06-09 17:48:54 +00:00
|
|
|
);
|
2014-03-01 18:01:36 +00:00
|
|
|
$Post->saveMany($data, array('validate' => true));
|
2010-06-09 17:48:54 +00:00
|
|
|
}
|
|
|
|
|
2010-09-29 03:37:28 +00:00
|
|
|
/**
|
2011-07-25 07:39:03 +00:00
|
|
|
* test saveAssociated with transactions and ensure there is no missing rollback.
|
2010-09-29 03:37:28 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-07-25 07:39:03 +00:00
|
|
|
public function testSaveAssociatedTransactionNoRollback() {
|
2014-08-01 21:41:00 +00:00
|
|
|
$this->loadFixtures('Post', 'Author');
|
2010-09-29 03:37:28 +00:00
|
|
|
|
2014-08-01 21:41:00 +00:00
|
|
|
$Post = new TestPost();
|
|
|
|
$Post->Author->validate = array(
|
2015-05-17 20:27:16 +00:00
|
|
|
'user' => array('rule' => array('notBlank'))
|
2014-08-01 21:41:00 +00:00
|
|
|
);
|
2010-09-29 03:37:28 +00:00
|
|
|
|
2014-08-01 21:41:00 +00:00
|
|
|
// If validation error occurs, rollback() should be called.
|
|
|
|
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
|
|
|
|
$db->expects($this->once())->method('begin')->will($this->returnValue(true));
|
|
|
|
$db->expects($this->never())->method('commit');
|
2010-10-14 03:09:55 +00:00
|
|
|
$db->expects($this->once())->method('rollback');
|
2010-09-29 03:37:28 +00:00
|
|
|
|
2014-03-01 18:01:36 +00:00
|
|
|
$Post->setDataSourceObject($db);
|
|
|
|
$Post->Author->setDataSourceObject($db);
|
2010-09-29 03:37:28 +00:00
|
|
|
|
2014-08-01 21:41:00 +00:00
|
|
|
$data = array(
|
|
|
|
'Post' => array(
|
|
|
|
'title' => 'New post',
|
|
|
|
'body' => 'Content',
|
|
|
|
'published' => 'Y'
|
|
|
|
),
|
|
|
|
'Author' => array(
|
|
|
|
'user' => '',
|
|
|
|
'password' => "sekret"
|
|
|
|
)
|
2010-09-29 03:37:28 +00:00
|
|
|
);
|
2014-08-01 21:41:00 +00:00
|
|
|
$Post->saveAssociated($data, array('validate' => true, 'atomic' => true));
|
|
|
|
|
2014-08-02 01:12:33 +00:00
|
|
|
// If exception thrown, commit() should be called.
|
|
|
|
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
|
|
|
|
$db->expects($this->once())->method('begin')->will($this->returnValue(true));
|
|
|
|
$db->expects($this->never())->method('commit');
|
|
|
|
$db->expects($this->once())->method('rollback');
|
|
|
|
|
|
|
|
$Post->setDataSourceObject($db);
|
|
|
|
$Post->Author->setDataSourceObject($db);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Post' => array(
|
|
|
|
'title' => 'New post',
|
|
|
|
'body' => $db->expression('PDO_EXCEPTION()'),
|
|
|
|
'published' => 'Y'
|
|
|
|
),
|
|
|
|
'Author' => array(
|
|
|
|
'user' => 'New user',
|
|
|
|
'password' => "sekret"
|
|
|
|
)
|
2010-09-29 03:37:28 +00:00
|
|
|
);
|
|
|
|
|
2014-08-02 01:12:33 +00:00
|
|
|
try {
|
|
|
|
$Post->saveAssociated($data, array('validate' => true, 'atomic' => true));
|
|
|
|
$this->fail('No exception thrown');
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
}
|
|
|
|
|
2014-08-01 21:41:00 +00:00
|
|
|
// Otherwise, commit() should be called.
|
|
|
|
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
|
|
|
|
$db->expects($this->once())->method('begin')->will($this->returnValue(true));
|
|
|
|
$db->expects($this->once())->method('commit');
|
|
|
|
$db->expects($this->never())->method('rollback');
|
|
|
|
|
|
|
|
$Post->setDataSourceObject($db);
|
|
|
|
$Post->Author->setDataSourceObject($db);
|
2010-09-29 03:37:28 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Post' => array(
|
|
|
|
'title' => 'New post',
|
|
|
|
'body' => 'Content',
|
|
|
|
'published' => 'Y'
|
|
|
|
),
|
|
|
|
'Author' => array(
|
2014-08-01 21:41:00 +00:00
|
|
|
'user' => 'New user',
|
2010-09-29 03:37:28 +00:00
|
|
|
'password' => "sekret"
|
|
|
|
)
|
|
|
|
);
|
2011-07-25 07:39:03 +00:00
|
|
|
$Post->saveAssociated($data, array('validate' => true, 'atomic' => true));
|
2010-09-29 03:37:28 +00:00
|
|
|
}
|
|
|
|
|
2010-11-03 13:25:42 +00:00
|
|
|
/**
|
2011-07-25 07:39:03 +00:00
|
|
|
* test saveMany with nested saveMany call.
|
2010-11-03 13:25:42 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-07-25 07:39:03 +00:00
|
|
|
public function testSaveManyNestedSaveMany() {
|
2010-11-03 13:25:42 +00:00
|
|
|
$this->loadFixtures('Sample');
|
2011-07-25 07:39:03 +00:00
|
|
|
$TransactionManyTestModel = new TransactionManyTestModel();
|
2010-11-03 13:25:42 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
array('apple_id' => 1, 'name' => 'sample5'),
|
|
|
|
);
|
|
|
|
|
2011-07-25 07:39:03 +00:00
|
|
|
$this->assertTrue($TransactionManyTestModel->saveMany($data, array('atomic' => true)));
|
2010-11-03 13:25:42 +00:00
|
|
|
}
|
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
2011-07-25 07:39:03 +00:00
|
|
|
* testSaveManyTransaction method
|
2009-07-24 22:17:14 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-07-25 07:39:03 +00:00
|
|
|
public function testSaveManyTransaction() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new Post();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->validate = array('title' => 'notBlank');
|
2009-07-24 22:17:14 +00:00
|
|
|
$data = array(
|
|
|
|
array('author_id' => 1, 'title' => 'New Fourth Post'),
|
|
|
|
array('author_id' => 1, 'title' => 'New Fifth Post'),
|
|
|
|
array('author_id' => 1, 'title' => '')
|
|
|
|
);
|
2011-07-25 07:39:03 +00:00
|
|
|
$this->assertFalse($TestModel->saveMany($data));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->find('all', array('recursive' => -1));
|
|
|
|
$expected = 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'
|
|
|
|
)));
|
|
|
|
|
|
|
|
if (count($result) != 3) {
|
|
|
|
// Database doesn't support transactions
|
|
|
|
$expected[] = array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '4',
|
|
|
|
'author_id' => 1,
|
|
|
|
'title' => 'New Fourth Post',
|
|
|
|
'body' => null,
|
2011-10-03 19:21:07 +00:00
|
|
|
'published' => 'N'
|
2009-07-24 22:17:14 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$expected[] = array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '5',
|
|
|
|
'author_id' => 1,
|
|
|
|
'title' => 'New Fifth Post',
|
|
|
|
'body' => null,
|
|
|
|
'published' => 'N',
|
|
|
|
));
|
|
|
|
|
2012-03-28 15:49:41 +00:00
|
|
|
$this->assertEquals(self::date(), $result[3]['Post']['created']);
|
|
|
|
$this->assertEquals(self::date(), $result[3]['Post']['updated']);
|
|
|
|
$this->assertEquals(self::date(), $result[4]['Post']['created']);
|
|
|
|
$this->assertEquals(self::date(), $result[4]['Post']['updated']);
|
2011-10-03 19:21:07 +00:00
|
|
|
unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
|
|
|
|
unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
// Skip the rest of the transactional tests
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
array('author_id' => 1, 'title' => 'New Fourth Post'),
|
|
|
|
array('author_id' => 1, 'title' => ''),
|
|
|
|
array('author_id' => 1, 'title' => 'New Sixth Post')
|
|
|
|
);
|
2011-07-25 07:39:03 +00:00
|
|
|
$this->assertFalse($TestModel->saveMany($data));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->find('all', array('recursive' => -1));
|
|
|
|
$expected = 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'
|
|
|
|
)));
|
|
|
|
|
|
|
|
if (count($result) != 3) {
|
|
|
|
// Database doesn't support transactions
|
|
|
|
$expected[] = array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '4',
|
|
|
|
'author_id' => 1,
|
|
|
|
'title' => 'New Fourth Post',
|
|
|
|
'body' => 'Third Post Body',
|
2011-10-03 19:21:07 +00:00
|
|
|
'published' => 'N'
|
2009-07-24 22:17:14 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$expected[] = array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '5',
|
|
|
|
'author_id' => 1,
|
|
|
|
'title' => 'Third Post',
|
|
|
|
'body' => 'Third Post Body',
|
2011-10-03 19:21:07 +00:00
|
|
|
'published' => 'N'
|
2009-07-24 22:17:14 +00:00
|
|
|
));
|
2012-03-28 15:49:41 +00:00
|
|
|
$this->assertEquals(self::date(), $result[3]['Post']['created']);
|
|
|
|
$this->assertEquals(self::date(), $result[3]['Post']['updated']);
|
|
|
|
$this->assertEquals(self::date(), $result[4]['Post']['created']);
|
|
|
|
$this->assertEquals(self::date(), $result[4]['Post']['updated']);
|
2011-10-03 19:21:07 +00:00
|
|
|
unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
|
|
|
|
unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->validate = array('title' => 'notBlank');
|
2009-07-24 22:17:14 +00:00
|
|
|
$data = array(
|
|
|
|
array('author_id' => 1, 'title' => 'New Fourth Post'),
|
|
|
|
array('author_id' => 1, 'title' => 'New Fifth Post'),
|
|
|
|
array('author_id' => 1, 'title' => 'New Sixth Post')
|
|
|
|
);
|
2011-07-25 07:39:03 +00:00
|
|
|
$this->assertTrue($TestModel->saveMany($data));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->find('all', array(
|
|
|
|
'recursive' => -1,
|
2013-06-09 15:39:48 +00:00
|
|
|
'fields' => array('author_id', 'title', 'body', 'published'),
|
2012-02-10 16:26:05 +00:00
|
|
|
'order' => array('Post.created' => 'ASC')
|
2009-07-24 22:17:14 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
array('Post' => array(
|
|
|
|
'author_id' => 1,
|
|
|
|
'title' => 'First Post',
|
|
|
|
'body' => 'First Post Body',
|
|
|
|
'published' => 'Y'
|
|
|
|
)),
|
|
|
|
array('Post' => array(
|
|
|
|
'author_id' => 3,
|
|
|
|
'title' => 'Second Post',
|
|
|
|
'body' => 'Second Post Body',
|
|
|
|
'published' => 'Y'
|
|
|
|
)),
|
|
|
|
array('Post' => array(
|
|
|
|
'author_id' => 1,
|
|
|
|
'title' => 'Third Post',
|
|
|
|
'body' => 'Third Post Body',
|
|
|
|
'published' => 'Y'
|
|
|
|
)),
|
|
|
|
array('Post' => array(
|
|
|
|
'author_id' => 1,
|
|
|
|
'title' => 'New Fourth Post',
|
|
|
|
'body' => '',
|
|
|
|
'published' => 'N'
|
|
|
|
)),
|
|
|
|
array('Post' => array(
|
|
|
|
'author_id' => 1,
|
|
|
|
'title' => 'New Fifth Post',
|
|
|
|
'body' => '',
|
|
|
|
'published' => 'N'
|
|
|
|
)),
|
|
|
|
array('Post' => array(
|
|
|
|
'author_id' => 1,
|
|
|
|
'title' => 'New Sixth Post',
|
|
|
|
'body' => '',
|
|
|
|
'published' => 'N'
|
|
|
|
)));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-07-25 07:39:03 +00:00
|
|
|
* testSaveManyValidation method
|
2009-07-24 22:17:14 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-07-25 07:39:03 +00:00
|
|
|
public function testSaveManyValidation() {
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new Post();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
array(
|
|
|
|
'id' => '1',
|
|
|
|
'title' => 'Baleeted First Post',
|
|
|
|
'body' => 'Baleeted!',
|
|
|
|
'published' => 'N'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => '2',
|
|
|
|
'title' => 'Just update the title'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'title' => 'Creating a fourth post',
|
|
|
|
'body' => 'Fourth post body',
|
|
|
|
'author_id' => 2
|
|
|
|
));
|
|
|
|
|
2011-07-25 07:39:03 +00:00
|
|
|
$this->assertTrue($TestModel->saveMany($data));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
|
|
|
|
$expected = array(
|
|
|
|
array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '1',
|
|
|
|
'author_id' => '1',
|
|
|
|
'title' => 'Baleeted First Post',
|
|
|
|
'body' => 'Baleeted!',
|
|
|
|
'published' => 'N',
|
2011-10-03 19:21:07 +00:00
|
|
|
'created' => '2007-03-18 10:39:23'
|
|
|
|
)
|
|
|
|
),
|
2009-07-24 22:17:14 +00:00
|
|
|
array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '2',
|
|
|
|
'author_id' => '3',
|
|
|
|
'title' => 'Just update the title',
|
|
|
|
'body' => 'Second Post Body',
|
2012-05-15 14:57:42 +00:00
|
|
|
'published' => 'Y',
|
2011-10-03 19:21:07 +00:00
|
|
|
'created' => '2007-03-18 10:41:23'
|
|
|
|
)
|
|
|
|
),
|
2009-07-24 22:17:14 +00:00
|
|
|
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'
|
|
|
|
)),
|
|
|
|
array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '4',
|
|
|
|
'author_id' => '2',
|
|
|
|
'title' => 'Creating a fourth post',
|
|
|
|
'body' => 'Fourth post body',
|
2011-10-03 19:21:07 +00:00
|
|
|
'published' => 'N'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2012-03-28 15:49:41 +00:00
|
|
|
$this->assertEquals(self::date(), $result[0]['Post']['updated']);
|
|
|
|
$this->assertEquals(self::date(), $result[1]['Post']['updated']);
|
|
|
|
$this->assertEquals(self::date(), $result[3]['Post']['created']);
|
|
|
|
$this->assertEquals(self::date(), $result[3]['Post']['updated']);
|
2011-10-03 19:21:07 +00:00
|
|
|
unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
|
|
|
|
unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->validate = array('title' => 'notBlank', 'author_id' => 'numeric');
|
2009-07-24 22:17:14 +00:00
|
|
|
$data = array(
|
|
|
|
array(
|
|
|
|
'id' => '1',
|
|
|
|
'title' => 'Un-Baleeted First Post',
|
|
|
|
'body' => 'Not Baleeted!',
|
|
|
|
'published' => 'Y'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => '2',
|
|
|
|
'title' => '',
|
|
|
|
'body' => 'Trying to get away with an empty title'
|
|
|
|
));
|
2011-07-25 07:39:03 +00:00
|
|
|
$result = $TestModel->saveMany($data);
|
2010-07-14 02:12:07 +00:00
|
|
|
$this->assertFalse($result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
|
2010-12-26 06:40:02 +00:00
|
|
|
$errors = array(1 => array('title' => array('This field cannot be left blank')));
|
2009-07-24 22:17:14 +00:00
|
|
|
$transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
|
|
|
|
if (!$transactionWorked) {
|
|
|
|
$this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
|
|
|
|
$this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
|
|
|
|
}
|
|
|
|
|
2012-03-22 11:18:57 +00:00
|
|
|
$this->assertEquals($errors, $TestModel->validationErrors);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->validate = array('title' => 'notBlank', 'author_id' => 'numeric');
|
2009-07-24 22:17:14 +00:00
|
|
|
$data = array(
|
|
|
|
array(
|
|
|
|
'id' => '1',
|
|
|
|
'title' => 'Un-Baleeted First Post',
|
|
|
|
'body' => 'Not Baleeted!',
|
|
|
|
'published' => 'Y'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => '2',
|
|
|
|
'title' => '',
|
|
|
|
'body' => 'Trying to get away with an empty title'
|
|
|
|
));
|
2011-07-25 07:39:03 +00:00
|
|
|
$result = $TestModel->saveMany($data, array('validate' => true, 'atomic' => false));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array(true, false), $result);
|
2011-09-06 01:49:34 +00:00
|
|
|
|
|
|
|
$result = $TestModel->find('all', array(
|
|
|
|
'fields' => array('id', 'author_id', 'title', 'body', 'published'),
|
2011-10-28 05:01:17 +00:00
|
|
|
'recursive' => -1,
|
2011-09-06 01:49:34 +00:00
|
|
|
'order' => 'Post.id ASC'
|
|
|
|
));
|
2010-12-26 06:40:02 +00:00
|
|
|
$errors = array(1 => array('title' => array('This field cannot be left blank')));
|
2009-07-24 22:17:14 +00:00
|
|
|
$expected = array(
|
|
|
|
array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '1',
|
|
|
|
'author_id' => '1',
|
|
|
|
'title' => 'Un-Baleeted First Post',
|
|
|
|
'body' => 'Not Baleeted!',
|
|
|
|
'published' => 'Y',
|
|
|
|
)),
|
|
|
|
array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '2',
|
|
|
|
'author_id' => '3',
|
|
|
|
'title' => 'Just update the title',
|
|
|
|
'body' => 'Second Post Body',
|
2012-05-15 14:57:42 +00:00
|
|
|
'published' => 'Y',
|
2009-07-24 22:17:14 +00:00
|
|
|
)),
|
|
|
|
array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '3',
|
|
|
|
'author_id' => '1',
|
|
|
|
'title' => 'Third Post',
|
|
|
|
'body' => 'Third Post Body',
|
|
|
|
'published' => 'Y',
|
|
|
|
)),
|
|
|
|
array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '4',
|
|
|
|
'author_id' => '2',
|
|
|
|
'title' => 'Creating a fourth post',
|
|
|
|
'body' => 'Fourth post body',
|
|
|
|
'published' => 'N',
|
|
|
|
)));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2012-03-22 11:18:57 +00:00
|
|
|
$this->assertEquals($errors, $TestModel->validationErrors);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
array(
|
|
|
|
'id' => '1',
|
|
|
|
'title' => 'Re-Baleeted First Post',
|
|
|
|
'body' => 'Baleeted!',
|
|
|
|
'published' => 'N'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => '2',
|
|
|
|
'title' => '',
|
|
|
|
'body' => 'Trying to get away with an empty title'
|
|
|
|
));
|
2011-07-25 07:39:03 +00:00
|
|
|
$this->assertFalse($TestModel->saveMany($data, array('validate' => 'first')));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2011-09-06 01:49:34 +00:00
|
|
|
$result = $TestModel->find('all', array(
|
|
|
|
'fields' => array('id', 'author_id', 'title', 'body', 'published'),
|
2011-10-28 05:01:17 +00:00
|
|
|
'recursive' => -1,
|
2011-09-06 01:49:34 +00:00
|
|
|
'order' => 'Post.id ASC'
|
|
|
|
));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2012-03-22 11:18:57 +00:00
|
|
|
$this->assertEquals($errors, $TestModel->validationErrors);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
2011-07-25 07:39:03 +00:00
|
|
|
* testValidateMany method
|
2009-07-24 22:17:14 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-07-25 07:39:03 +00:00
|
|
|
public function testValidateMany() {
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new Article();
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->validate = array('title' => 'notBlank');
|
2012-05-10 04:21:27 +00:00
|
|
|
$data = array(
|
2009-07-24 22:17:14 +00:00
|
|
|
0 => array('title' => ''),
|
|
|
|
1 => array('title' => 'title 1'),
|
|
|
|
2 => array('title' => 'title 2'),
|
2012-05-10 04:21:27 +00:00
|
|
|
);
|
|
|
|
$result = $TestModel->validateMany($data);
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->assertFalse($result);
|
|
|
|
$expected = array(
|
2010-12-26 06:40:02 +00:00
|
|
|
0 => array('title' => array('This field cannot be left blank')),
|
2009-07-24 22:17:14 +00:00
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->validationErrors);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2012-05-10 04:21:27 +00:00
|
|
|
$data = array(
|
2009-07-24 22:17:14 +00:00
|
|
|
0 => array('title' => 'title 0'),
|
|
|
|
1 => array('title' => ''),
|
|
|
|
2 => array('title' => 'title 2'),
|
2012-05-10 04:21:27 +00:00
|
|
|
);
|
|
|
|
$result = $TestModel->validateMany($data);
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->assertFalse($result);
|
|
|
|
$expected = array(
|
2010-12-26 06:40:02 +00:00
|
|
|
1 => array('title' => array('This field cannot be left blank')),
|
2009-07-24 22:17:14 +00:00
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->validationErrors);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
2011-07-25 07:39:03 +00:00
|
|
|
* testSaveAssociatedValidateFirst method
|
2009-07-24 22:17:14 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-07-25 07:39:03 +00:00
|
|
|
public function testSaveAssociatedValidateFirst() {
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->loadFixtures('Article', 'Comment', 'Attachment');
|
|
|
|
$model = new Article();
|
2009-07-24 22:17:14 +00:00
|
|
|
$model->deleteAll(true);
|
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$model->Comment->validate = array('comment' => 'notBlank');
|
2011-07-25 07:39:03 +00:00
|
|
|
$result = $model->saveAssociated(array(
|
2009-07-24 22:17:14 +00:00
|
|
|
'Article' => array(
|
|
|
|
'title' => 'Post with Author',
|
2013-07-05 12:36:40 +00:00
|
|
|
'body' => 'This post will be saved author'
|
2009-07-24 22:17:14 +00:00
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment'),
|
|
|
|
array('comment' => '')
|
|
|
|
)
|
|
|
|
), array('validate' => 'first'));
|
|
|
|
|
2010-10-14 03:09:55 +00:00
|
|
|
$this->assertFalse($result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $model->find('all');
|
2012-10-26 22:18:05 +00:00
|
|
|
$this->assertSame(array(), $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
$expected = array('Comment' => array(
|
2010-12-26 06:40:02 +00:00
|
|
|
1 => array('comment' => array('This field cannot be left blank'))
|
2009-07-24 22:17:14 +00:00
|
|
|
));
|
|
|
|
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected['Comment'], $model->Comment->validationErrors);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertSame($model->Comment->find('count'), 0);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2011-07-25 07:39:03 +00:00
|
|
|
$result = $model->saveAssociated(
|
2009-07-24 22:17:14 +00:00
|
|
|
array(
|
|
|
|
'Article' => array(
|
|
|
|
'title' => 'Post with Author',
|
|
|
|
'body' => 'This post will be saved with an author',
|
|
|
|
'user_id' => 2
|
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'comment' => 'Only new comment',
|
|
|
|
'user_id' => 2
|
|
|
|
))),
|
|
|
|
array('validate' => 'first')
|
|
|
|
);
|
|
|
|
|
2013-09-18 22:17:21 +00:00
|
|
|
$this->assertTrue($result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $model->Comment->find('all');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertSame(count($result), 1);
|
2012-04-04 23:33:57 +00:00
|
|
|
$result = Hash::extract($result, '{n}.Comment.article_id');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(4, $result[0]);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$model->deleteAll(true);
|
|
|
|
$data = array(
|
|
|
|
'Article' => array(
|
|
|
|
'title' => 'Post with Author saveAlled from comment',
|
|
|
|
'body' => 'This post will be saved with an author',
|
|
|
|
'user_id' => 2
|
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => 'Only new comment', 'user_id' => 2
|
|
|
|
));
|
|
|
|
|
2011-07-25 07:39:03 +00:00
|
|
|
$result = $model->Comment->saveAssociated($data, array('validate' => 'first'));
|
2010-06-02 05:00:52 +00:00
|
|
|
$this->assertFalse(empty($result));
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$result = $model->find('all');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(
|
2012-03-22 11:18:57 +00:00
|
|
|
'Post with Author saveAlled from comment',
|
|
|
|
$result[0]['Article']['title']
|
2009-07-24 22:17:14 +00:00
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('Only new comment', $result[0]['Comment'][0]['comment']);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2010-08-27 02:21:39 +00:00
|
|
|
/**
|
2011-07-25 07:39:03 +00:00
|
|
|
* test saveMany()'s return is correct when using atomic = false and validate = first.
|
2010-08-27 02:21:39 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-07-25 07:39:03 +00:00
|
|
|
public function testSaveManyValidateFirstAtomicFalse() {
|
2010-11-13 04:05:44 +00:00
|
|
|
$Something = new Something();
|
2010-08-27 02:21:39 +00:00
|
|
|
$invalidData = array(
|
|
|
|
array(
|
|
|
|
'title' => 'foo',
|
|
|
|
'body' => 'bar',
|
|
|
|
'published' => 'baz',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'body' => 3,
|
2011-12-01 07:21:31 +00:00
|
|
|
'published' => 'sd',
|
2010-08-27 02:21:39 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
$Something->create();
|
|
|
|
$Something->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'rule' => 'alphaNumeric',
|
|
|
|
'required' => true,
|
|
|
|
),
|
|
|
|
'body' => array(
|
|
|
|
'rule' => 'alphaNumeric',
|
|
|
|
'required' => true,
|
|
|
|
'allowEmpty' => true,
|
|
|
|
),
|
|
|
|
);
|
2011-07-25 07:39:03 +00:00
|
|
|
$result = $Something->saveMany($invalidData, array(
|
2010-08-27 02:21:39 +00:00
|
|
|
'atomic' => false,
|
|
|
|
'validate' => 'first',
|
|
|
|
));
|
|
|
|
$expected = array(true, false);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-08-27 02:21:39 +00:00
|
|
|
|
2010-11-13 04:05:44 +00:00
|
|
|
$Something = new Something();
|
2010-08-27 02:21:39 +00:00
|
|
|
$validData = array(
|
|
|
|
array(
|
|
|
|
'title' => 'title value',
|
|
|
|
'body' => 'body value',
|
|
|
|
'published' => 'baz',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'title' => 'valid',
|
|
|
|
'body' => 'this body',
|
2011-12-01 07:21:31 +00:00
|
|
|
'published' => 'sd',
|
2010-08-27 02:21:39 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
$Something->create();
|
2011-07-25 07:39:03 +00:00
|
|
|
$result = $Something->saveMany($validData, array(
|
2010-08-27 02:21:39 +00:00
|
|
|
'atomic' => false,
|
|
|
|
'validate' => 'first',
|
|
|
|
));
|
|
|
|
$expected = array(true, true);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-08-27 02:21:39 +00:00
|
|
|
}
|
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
2011-07-25 07:39:03 +00:00
|
|
|
* testValidateAssociated method
|
2009-07-24 22:17:14 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-07-25 07:39:03 +00:00
|
|
|
public function testValidateAssociated() {
|
2012-06-06 14:37:01 +00:00
|
|
|
$this->loadFixtures('Attachment', 'Article', 'Comment');
|
2011-07-25 07:39:03 +00:00
|
|
|
$TestModel = new Comment();
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->Attachment->validate = array('attachment' => 'notBlank');
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2011-07-25 07:39:03 +00:00
|
|
|
$data = array(
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => 'This is the comment'
|
|
|
|
),
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => ''
|
|
|
|
)
|
|
|
|
);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2011-07-25 07:39:03 +00:00
|
|
|
$result = $TestModel->validateAssociated($data);
|
|
|
|
$this->assertFalse($result);
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2010-06-02 05:00:52 +00:00
|
|
|
$TestModel = new Article();
|
2009-07-24 22:17:14 +00:00
|
|
|
$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->Comment->validate = array('comment' => 'notBlank');
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2012-05-10 04:21:27 +00:00
|
|
|
$data = array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'id' => 1,
|
|
|
|
'comment' => '',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 1),
|
|
|
|
array(
|
|
|
|
'id' => 2,
|
|
|
|
'comment' =>
|
|
|
|
'comment',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 1
|
|
|
|
)));
|
|
|
|
$result = $TestModel->validateAssociated($data);
|
2009-07-24 22:17:14 +00:00
|
|
|
$this->assertFalse($result);
|
|
|
|
|
2012-05-10 04:21:27 +00:00
|
|
|
$data = array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'id' => 1,
|
|
|
|
'comment' => '',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 1
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => 2,
|
|
|
|
'comment' => 'comment',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 1
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => 3,
|
|
|
|
'comment' => '',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 1
|
|
|
|
)));
|
|
|
|
$result = $TestModel->validateAssociated($data, array(
|
2009-07-24 22:17:14 +00:00
|
|
|
'atomic' => false
|
|
|
|
));
|
|
|
|
$expected = array(
|
|
|
|
'Article' => true,
|
|
|
|
'Comment' => array(false, true, false)
|
|
|
|
);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertSame($expected, $result);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$expected = array('Comment' => array(
|
2010-12-26 06:40:02 +00:00
|
|
|
0 => array('comment' => array('This field cannot be left blank')),
|
|
|
|
2 => array('comment' => array('This field cannot be left blank'))
|
2009-07-24 22:17:14 +00:00
|
|
|
));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->validationErrors);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$expected = array(
|
2010-12-26 06:40:02 +00:00
|
|
|
0 => array('comment' => array('This field cannot be left blank')),
|
|
|
|
2 => array('comment' => array('This field cannot be left blank'))
|
2009-07-24 22:17:14 +00:00
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->Comment->validationErrors);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2010-07-29 03:42:36 +00:00
|
|
|
|
2011-07-25 07:39:03 +00:00
|
|
|
/**
|
|
|
|
* test that saveMany behaves like plain save() when suplied empty data
|
|
|
|
*
|
2013-06-25 20:58:30 +00:00
|
|
|
* @link https://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
|
2011-07-25 07:39:03 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveManyEmptyData() {
|
|
|
|
$this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
|
|
|
|
|
|
|
|
$this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
|
|
|
|
$model = new Article();
|
|
|
|
$result = $model->saveMany(array(), array('validate' => true));
|
|
|
|
$this->assertFalse(empty($result));
|
|
|
|
|
|
|
|
$model = new ProductUpdateAll();
|
|
|
|
$result = $model->saveMany(array());
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-23 23:29:53 +00:00
|
|
|
* test that saveAssociated behaves like plain save() when supplied empty data
|
2011-07-25 07:39:03 +00:00
|
|
|
*
|
2013-06-25 20:58:30 +00:00
|
|
|
* @link https://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
|
2011-07-25 07:39:03 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAssociatedEmptyData() {
|
|
|
|
$this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
|
|
|
|
|
|
|
|
$this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
|
|
|
|
$model = new Article();
|
|
|
|
$result = $model->saveAssociated(array(), array('validate' => true));
|
|
|
|
$this->assertFalse(empty($result));
|
|
|
|
|
|
|
|
$model = new ProductUpdateAll();
|
|
|
|
$result = $model->saveAssociated(array());
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
|
|
|
|
2013-11-16 15:51:14 +00:00
|
|
|
/**
|
|
|
|
* Test that saveAssociated will accept expression object values when saving.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAssociatedExpressionObjects() {
|
|
|
|
$this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
|
|
|
|
$TestModel = new Post();
|
|
|
|
$db = $TestModel->getDataSource();
|
|
|
|
|
|
|
|
$TestModel->saveAssociated(array(
|
|
|
|
'Post' => array(
|
2013-12-08 16:58:26 +00:00
|
|
|
'title' => $db->expression("(SELECT 'Post with Author')"),
|
2013-11-16 15:51:14 +00:00
|
|
|
'body' => 'This post will be saved with an author'
|
|
|
|
),
|
|
|
|
'Author' => array(
|
|
|
|
'user' => 'bob',
|
|
|
|
'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
|
2013-12-08 16:46:17 +00:00
|
|
|
)), array('atomic' => false));
|
2013-11-16 15:51:14 +00:00
|
|
|
|
|
|
|
$result = $TestModel->find('first', array(
|
|
|
|
'order' => array('Post.id ' => 'DESC')
|
|
|
|
));
|
|
|
|
$this->assertEquals('Post with Author', $result['Post']['title']);
|
|
|
|
}
|
|
|
|
|
2011-07-25 07:39:03 +00:00
|
|
|
/**
|
|
|
|
* testUpdateWithCalculation method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testUpdateWithCalculation() {
|
|
|
|
$this->loadFixtures('DataTest');
|
|
|
|
$model = new DataTest();
|
|
|
|
$model->deleteAll(true);
|
|
|
|
$result = $model->saveMany(array(
|
|
|
|
array('count' => 5, 'float' => 1.1),
|
|
|
|
array('count' => 3, 'float' => 1.2),
|
|
|
|
array('count' => 4, 'float' => 1.3),
|
|
|
|
array('count' => 1, 'float' => 2.0),
|
|
|
|
));
|
|
|
|
$this->assertFalse(empty($result));
|
|
|
|
|
2012-04-04 23:33:57 +00:00
|
|
|
$result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array(5, 3, 4, 1), $result);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$this->assertTrue($model->updateAll(array('count' => 'count + 2')));
|
2012-04-04 23:33:57 +00:00
|
|
|
$result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array(7, 5, 6, 3), $result);
|
2011-07-25 07:39:03 +00:00
|
|
|
|
|
|
|
$this->assertTrue($model->updateAll(array('DataTest.count' => 'DataTest.count - 1')));
|
2012-04-04 23:33:57 +00:00
|
|
|
$result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array(6, 4, 5, 2), $result);
|
2011-07-25 07:39:03 +00:00
|
|
|
}
|
|
|
|
|
2014-07-13 16:21:09 +00:00
|
|
|
/**
|
|
|
|
* testToggleBoolFields method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-03-23 21:21:13 +00:00
|
|
|
public function testToggleBoolFields() {
|
2012-03-24 01:15:11 +00:00
|
|
|
$this->loadFixtures('CounterCacheUser', 'CounterCachePost');
|
2012-03-23 21:21:13 +00:00
|
|
|
$Post = new CounterCachePost();
|
|
|
|
$Post->unbindModel(array('belongsTo' => array('User')), true);
|
2012-03-24 01:15:11 +00:00
|
|
|
|
2012-03-23 21:21:13 +00:00
|
|
|
$true = array('Post' => array('published' => true, 'id' => 2));
|
|
|
|
$false = array('Post' => array('published' => false, 'id' => 2));
|
|
|
|
$fields = array('Post.published', 'Post.id');
|
|
|
|
$updateConditions = array('Post.id' => 2);
|
2012-03-24 01:15:11 +00:00
|
|
|
|
2012-03-23 21:21:13 +00:00
|
|
|
// check its true
|
|
|
|
$result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
|
2012-03-24 22:50:54 +00:00
|
|
|
$this->assertEquals($true, $result);
|
2012-03-24 01:15:11 +00:00
|
|
|
|
2012-03-23 21:21:13 +00:00
|
|
|
// Testing without the alias
|
2012-03-24 01:15:11 +00:00
|
|
|
$this->assertTrue($Post->updateAll(array('published' => 'NOT published'), $updateConditions));
|
2012-03-23 21:21:13 +00:00
|
|
|
$result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
|
2012-03-24 22:50:54 +00:00
|
|
|
$this->assertEquals($false, $result);
|
2012-03-24 01:15:11 +00:00
|
|
|
|
|
|
|
$this->assertTrue($Post->updateAll(array('published' => 'NOT published'), $updateConditions));
|
2012-03-23 21:21:13 +00:00
|
|
|
$result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
|
2012-03-24 22:50:54 +00:00
|
|
|
$this->assertEquals($true, $result);
|
2012-03-24 01:15:11 +00:00
|
|
|
|
|
|
|
$db = ConnectionManager::getDataSource('test');
|
|
|
|
$alias = $db->name('Post.published');
|
|
|
|
|
2012-03-23 21:21:13 +00:00
|
|
|
// Testing with the alias
|
2012-03-24 01:15:11 +00:00
|
|
|
$this->assertTrue($Post->updateAll(array('Post.published' => "NOT $alias"), $updateConditions));
|
2012-03-23 21:21:13 +00:00
|
|
|
$result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
|
2012-03-24 22:50:54 +00:00
|
|
|
$this->assertEquals($false, $result);
|
2012-03-24 01:15:11 +00:00
|
|
|
|
|
|
|
$this->assertTrue($Post->updateAll(array('Post.published' => "NOT $alias"), $updateConditions));
|
2012-03-23 21:21:13 +00:00
|
|
|
$result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
|
2012-03-24 22:50:54 +00:00
|
|
|
$this->assertEquals($true, $result);
|
2012-03-23 21:21:13 +00:00
|
|
|
}
|
2011-07-25 07:39:03 +00:00
|
|
|
|
2009-09-09 03:48:32 +00:00
|
|
|
/**
|
|
|
|
* TestFindAllWithoutForeignKey
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testFindAllForeignKey() {
|
2009-09-09 03:48:32 +00:00
|
|
|
$this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
|
2010-06-02 05:00:52 +00:00
|
|
|
$ProductUpdateAll = new ProductUpdateAll();
|
2009-09-09 03:48:32 +00:00
|
|
|
|
|
|
|
$conditions = array('Group.name' => 'group one');
|
|
|
|
|
2010-07-29 03:42:36 +00:00
|
|
|
$ProductUpdateAll->bindModel(array(
|
2009-09-09 03:48:32 +00:00
|
|
|
'belongsTo' => array(
|
|
|
|
'Group' => array('className' => 'GroupUpdateAll')
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
2010-07-29 03:42:36 +00:00
|
|
|
$ProductUpdateAll->belongsTo = array(
|
2009-09-09 03:48:32 +00:00
|
|
|
'Group' => array('className' => 'GroupUpdateAll', 'foreignKey' => 'group_id')
|
|
|
|
);
|
|
|
|
|
2010-07-29 03:42:36 +00:00
|
|
|
$results = $ProductUpdateAll->find('all', compact('conditions'));
|
2009-09-09 03:48:32 +00:00
|
|
|
$this->assertTrue(!empty($results));
|
|
|
|
|
2011-12-01 07:21:31 +00:00
|
|
|
$ProductUpdateAll->bindModel(array('belongsTo' => array('Group')));
|
2010-07-29 03:42:36 +00:00
|
|
|
$ProductUpdateAll->belongsTo = array(
|
|
|
|
'Group' => array(
|
2009-09-09 03:48:32 +00:00
|
|
|
'className' => 'GroupUpdateAll',
|
|
|
|
'foreignKey' => false,
|
|
|
|
'conditions' => 'ProductUpdateAll.groupcode = Group.code'
|
|
|
|
));
|
|
|
|
|
2010-07-29 03:42:36 +00:00
|
|
|
$resultsFkFalse = $ProductUpdateAll->find('all', compact('conditions'));
|
|
|
|
$this->assertTrue(!empty($resultsFkFalse));
|
|
|
|
$expected = array(
|
|
|
|
'0' => array(
|
|
|
|
'ProductUpdateAll' => array(
|
2011-12-16 07:00:07 +00:00
|
|
|
'id' => 1,
|
2010-07-29 03:42:36 +00:00
|
|
|
'name' => 'product one',
|
2012-11-21 14:39:03 +00:00
|
|
|
'groupcode' => 120,
|
|
|
|
'group_id' => 1),
|
2010-07-29 03:42:36 +00:00
|
|
|
'Group' => array(
|
|
|
|
'id' => 1,
|
|
|
|
'name' => 'group one',
|
|
|
|
'code' => 120)
|
|
|
|
),
|
|
|
|
'1' => array(
|
|
|
|
'ProductUpdateAll' => array(
|
2011-12-16 07:00:07 +00:00
|
|
|
'id' => 2,
|
2010-07-29 03:42:36 +00:00
|
|
|
'name' => 'product two',
|
2012-11-21 14:39:03 +00:00
|
|
|
'groupcode' => 120,
|
2010-07-29 03:42:36 +00:00
|
|
|
'group_id' => 1),
|
|
|
|
'Group' => array(
|
|
|
|
'id' => 1,
|
|
|
|
'name' => 'group one',
|
|
|
|
'code' => 120)
|
|
|
|
)
|
|
|
|
|
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $results);
|
|
|
|
$this->assertEquals($expected, $resultsFkFalse);
|
2010-07-29 03:42:36 +00:00
|
|
|
}
|
|
|
|
|
2009-09-09 03:48:32 +00:00
|
|
|
/**
|
2011-03-26 15:10:43 +00:00
|
|
|
* test updateAll with empty values.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testUpdateAllEmptyValues() {
|
2011-09-03 21:30:16 +00:00
|
|
|
$this->skipIf($this->db instanceof Sqlserver || $this->db instanceof Postgres, 'This test is not compatible with Postgres or SQL Server.');
|
2011-05-24 03:16:13 +00:00
|
|
|
|
2011-03-26 15:10:43 +00:00
|
|
|
$this->loadFixtures('Author', 'Post');
|
|
|
|
$model = new Author();
|
|
|
|
$result = $model->updateAll(array('user' => '""'));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
2009-09-09 03:48:32 +00:00
|
|
|
/**
|
2011-03-27 22:00:37 +00:00
|
|
|
* testUpdateAllWithJoins
|
2009-09-09 03:48:32 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testUpdateAllWithJoins() {
|
2011-05-31 00:49:46 +00:00
|
|
|
$this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql or sqlite');
|
|
|
|
|
2009-09-09 03:48:32 +00:00
|
|
|
$this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
|
2010-06-02 05:00:52 +00:00
|
|
|
$ProductUpdateAll = new ProductUpdateAll();
|
2009-09-09 03:48:32 +00:00
|
|
|
|
|
|
|
$conditions = array('Group.name' => 'group one');
|
|
|
|
|
2010-07-29 03:42:36 +00:00
|
|
|
$ProductUpdateAll->bindModel(array('belongsTo' => array(
|
2009-09-09 03:48:32 +00:00
|
|
|
'Group' => array('className' => 'GroupUpdateAll')))
|
|
|
|
);
|
|
|
|
|
2010-07-29 03:42:36 +00:00
|
|
|
$ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
|
|
|
|
$results = $ProductUpdateAll->find('all', array(
|
2009-09-09 03:48:32 +00:00
|
|
|
'conditions' => array('ProductUpdateAll.name' => 'new product')
|
|
|
|
));
|
2010-07-29 03:42:36 +00:00
|
|
|
$expected = array(
|
|
|
|
'0' => array(
|
|
|
|
'ProductUpdateAll' => array(
|
2011-12-16 07:00:07 +00:00
|
|
|
'id' => 1,
|
2012-11-21 14:39:03 +00:00
|
|
|
'name' => 'new product',
|
|
|
|
'groupcode' => 120,
|
|
|
|
'group_id' => 1),
|
2010-07-29 03:42:36 +00:00
|
|
|
'Group' => array(
|
|
|
|
'id' => 1,
|
|
|
|
'name' => 'group one',
|
|
|
|
'code' => 120)
|
|
|
|
),
|
|
|
|
'1' => array(
|
|
|
|
'ProductUpdateAll' => array(
|
2011-12-16 07:00:07 +00:00
|
|
|
'id' => 2,
|
2012-11-21 14:39:03 +00:00
|
|
|
'name' => 'new product',
|
|
|
|
'groupcode' => 120,
|
|
|
|
'group_id' => 1),
|
2010-07-29 03:42:36 +00:00
|
|
|
'Group' => array(
|
|
|
|
'id' => 1,
|
|
|
|
'name' => 'group one',
|
|
|
|
'code' => 120)));
|
|
|
|
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $results);
|
2010-07-29 03:42:36 +00:00
|
|
|
}
|
2010-03-16 23:16:04 +00:00
|
|
|
|
2009-09-09 03:48:32 +00:00
|
|
|
/**
|
2011-03-27 22:00:37 +00:00
|
|
|
* testUpdateAllWithoutForeignKey
|
2009-09-09 03:48:32 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-12-16 06:52:07 +00:00
|
|
|
public function testUpdateAllWithoutForeignKey() {
|
2011-05-31 00:49:46 +00:00
|
|
|
$this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql');
|
|
|
|
|
2009-09-09 03:48:32 +00:00
|
|
|
$this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
|
2010-06-02 05:00:52 +00:00
|
|
|
$ProductUpdateAll = new ProductUpdateAll();
|
2009-09-09 03:48:32 +00:00
|
|
|
|
|
|
|
$conditions = array('Group.name' => 'group one');
|
|
|
|
|
2011-12-16 06:52:07 +00:00
|
|
|
$ProductUpdateAll->bindModel(array('belongsTo' => array(
|
2009-09-09 03:48:32 +00:00
|
|
|
'Group' => array('className' => 'GroupUpdateAll')
|
|
|
|
)));
|
|
|
|
|
2011-12-16 06:52:07 +00:00
|
|
|
$ProductUpdateAll->belongsTo = array(
|
|
|
|
'Group' => array(
|
2009-09-09 03:48:32 +00:00
|
|
|
'className' => 'GroupUpdateAll',
|
|
|
|
'foreignKey' => false,
|
|
|
|
'conditions' => 'ProductUpdateAll.groupcode = Group.code'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2010-07-29 03:42:36 +00:00
|
|
|
$ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
|
2011-12-01 07:21:31 +00:00
|
|
|
$resultsFkFalse = $ProductUpdateAll->find('all', array('conditions' => array('ProductUpdateAll.name' => 'new product')));
|
2010-07-29 03:42:36 +00:00
|
|
|
$expected = array(
|
|
|
|
'0' => array(
|
|
|
|
'ProductUpdateAll' => array(
|
2011-12-16 07:00:07 +00:00
|
|
|
'id' => 1,
|
2012-11-21 14:39:03 +00:00
|
|
|
'name' => 'new product',
|
|
|
|
'groupcode' => 120,
|
|
|
|
'group_id' => 1),
|
2010-07-29 03:42:36 +00:00
|
|
|
'Group' => array(
|
|
|
|
'id' => 1,
|
|
|
|
'name' => 'group one',
|
|
|
|
'code' => 120)
|
|
|
|
),
|
|
|
|
'1' => array(
|
|
|
|
'ProductUpdateAll' => array(
|
2011-12-16 07:00:07 +00:00
|
|
|
'id' => 2,
|
2012-11-21 14:39:03 +00:00
|
|
|
'name' => 'new product',
|
|
|
|
'groupcode' => 120,
|
|
|
|
'group_id' => 1),
|
2010-07-29 03:42:36 +00:00
|
|
|
'Group' => array(
|
|
|
|
'id' => 1,
|
|
|
|
'name' => 'group one',
|
|
|
|
'code' => 120)));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $resultsFkFalse);
|
2010-07-29 03:42:36 +00:00
|
|
|
}
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2010-08-21 04:19:30 +00:00
|
|
|
/**
|
|
|
|
* test writing floats in german locale.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testWriteFloatAsGerman() {
|
2012-06-19 21:20:08 +00:00
|
|
|
$restore = setlocale(LC_NUMERIC, 0);
|
2013-08-14 20:32:04 +00:00
|
|
|
|
|
|
|
$this->skipIf(setlocale(LC_NUMERIC, 'de_DE') === false, "The German locale isn't available.");
|
2010-08-21 04:19:30 +00:00
|
|
|
|
|
|
|
$model = new DataTest();
|
|
|
|
$result = $model->save(array(
|
|
|
|
'count' => 1,
|
|
|
|
'float' => 3.14593
|
|
|
|
));
|
2010-09-06 20:54:32 +00:00
|
|
|
$this->assertTrue((bool)$result);
|
2012-06-19 21:20:08 +00:00
|
|
|
setlocale(LC_NUMERIC, $restore);
|
2010-08-21 04:19:30 +00:00
|
|
|
}
|
|
|
|
|
2011-08-13 21:26:08 +00:00
|
|
|
/**
|
|
|
|
* Test returned array contains primary key when save creates a new record
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testPkInReturnArrayForCreate() {
|
|
|
|
$this->loadFixtures('Article');
|
|
|
|
$TestModel = new Article();
|
|
|
|
|
|
|
|
$data = array('Article' => array(
|
|
|
|
'user_id' => '1',
|
|
|
|
'title' => 'Fourth Article',
|
|
|
|
'body' => 'Fourth Article Body',
|
|
|
|
'published' => 'Y'
|
|
|
|
));
|
|
|
|
$result = $TestModel->save($data);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertSame($result['Article']['id'], $TestModel->id);
|
2011-08-13 21:26:08 +00:00
|
|
|
}
|
|
|
|
|
2011-12-22 20:37:48 +00:00
|
|
|
/**
|
2012-02-13 00:00:28 +00:00
|
|
|
* testSaveAllFieldListValidateBelongsTo
|
2011-12-22 20:37:48 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-02-13 00:00:28 +00:00
|
|
|
public function testSaveAllFieldListValidateBelongsTo() {
|
2011-12-22 20:37:48 +00:00
|
|
|
$this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
|
|
|
|
$TestModel = new Post();
|
|
|
|
|
|
|
|
$result = $TestModel->find('all');
|
2012-04-21 13:02:35 +00:00
|
|
|
$this->assertEquals(3, count($result));
|
2011-12-22 20:37:48 +00:00
|
|
|
$this->assertFalse(isset($result[3]));
|
|
|
|
|
|
|
|
// test belongsTo
|
|
|
|
$fieldList = array(
|
2012-11-02 09:56:49 +00:00
|
|
|
'Post' => array('title'),
|
2011-12-22 20:37:48 +00:00
|
|
|
'Author' => array('user')
|
|
|
|
);
|
2012-11-02 09:56:49 +00:00
|
|
|
$data = array(
|
2011-12-22 20:37:48 +00:00
|
|
|
'Post' => array(
|
|
|
|
'title' => 'Post without body',
|
|
|
|
'body' => 'This will not be saved',
|
|
|
|
),
|
|
|
|
'Author' => array(
|
|
|
|
'user' => 'bob',
|
|
|
|
'test' => 'This will not be saved',
|
|
|
|
|
2012-11-02 09:56:49 +00:00
|
|
|
));
|
|
|
|
$TestModel->saveAll($data, array('fieldList' => $fieldList));
|
2011-12-22 20:37:48 +00:00
|
|
|
|
2013-06-21 21:47:08 +00:00
|
|
|
$result = $TestModel->find('all', array(
|
|
|
|
'order' => 'Post.id ASC',
|
|
|
|
));
|
2011-12-22 20:37:48 +00:00
|
|
|
$expected = array(
|
2013-08-02 21:19:41 +00:00
|
|
|
'Post' => array(
|
2011-12-22 20:37:48 +00:00
|
|
|
'id' => '4',
|
|
|
|
'author_id' => '5',
|
|
|
|
'title' => 'Post without body',
|
2012-03-19 01:20:17 +00:00
|
|
|
'body' => null,
|
2011-12-22 20:37:48 +00:00
|
|
|
'published' => 'N',
|
2012-03-28 15:49:41 +00:00
|
|
|
'created' => self::date(),
|
|
|
|
'updated' => self::date(),
|
2011-12-22 20:37:48 +00:00
|
|
|
),
|
2013-08-02 21:19:41 +00:00
|
|
|
'Author' => array(
|
2011-12-22 20:37:48 +00:00
|
|
|
'id' => '5',
|
|
|
|
'user' => 'bob',
|
2012-03-19 01:20:17 +00:00
|
|
|
'password' => null,
|
2012-03-28 15:49:41 +00:00
|
|
|
'created' => self::date(),
|
|
|
|
'updated' => self::date(),
|
2011-12-22 20:37:48 +00:00
|
|
|
'test' => 'working',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result[3]);
|
2012-04-21 13:02:35 +00:00
|
|
|
$this->assertEquals(4, count($result));
|
2011-12-22 20:37:48 +00:00
|
|
|
$this->assertEquals('', $result[3]['Post']['body']);
|
|
|
|
$this->assertEquals('working', $result[3]['Author']['test']);
|
|
|
|
|
2013-08-02 21:19:41 +00:00
|
|
|
$fieldList = array(
|
|
|
|
'Post' => array('title')
|
|
|
|
);
|
|
|
|
$data = array(
|
|
|
|
'Post' => array(
|
|
|
|
'title' => 'Post without body 2',
|
|
|
|
'body' => 'This will not be saved'
|
|
|
|
),
|
|
|
|
'Author' => array(
|
|
|
|
'user' => 'jack'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$TestModel->saveAll($data, array('fieldList' => $fieldList));
|
|
|
|
$result = $TestModel->find('all', array(
|
|
|
|
'order' => 'Post.id ASC',
|
|
|
|
));
|
|
|
|
$this->assertNull($result[4]['Post']['body']);
|
|
|
|
|
|
|
|
$fieldList = array(
|
|
|
|
'Author' => array('password')
|
|
|
|
);
|
|
|
|
$data = array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '5',
|
|
|
|
'title' => 'Post title',
|
|
|
|
'body' => 'Post body'
|
|
|
|
),
|
|
|
|
'Author' => array(
|
|
|
|
'id' => '6',
|
|
|
|
'user' => 'will not change',
|
|
|
|
'password' => 'foobar'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($data, array('fieldList' => $fieldList));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$result = $TestModel->find('all', array(
|
|
|
|
'order' => 'Post.id ASC',
|
|
|
|
));
|
|
|
|
$expected = array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '5',
|
|
|
|
'author_id' => '6',
|
|
|
|
'title' => 'Post title',
|
|
|
|
'body' => 'Post body',
|
|
|
|
'published' => 'N',
|
|
|
|
'created' => self::date(),
|
|
|
|
'updated' => self::date()
|
|
|
|
),
|
|
|
|
'Author' => array(
|
|
|
|
'id' => '6',
|
|
|
|
'user' => 'jack',
|
|
|
|
'password' => 'foobar',
|
|
|
|
'created' => self::date(),
|
|
|
|
'updated' => self::date(),
|
|
|
|
'test' => 'working'
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result[4]);
|
|
|
|
|
2011-12-22 20:37:48 +00:00
|
|
|
// test multirecord
|
|
|
|
$this->db->truncate($TestModel);
|
|
|
|
|
|
|
|
$fieldList = array('title', 'author_id');
|
|
|
|
$TestModel->saveAll(array(
|
|
|
|
array(
|
|
|
|
'title' => 'Multi-record post 1',
|
|
|
|
'body' => 'First multi-record post',
|
|
|
|
'author_id' => 2
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'title' => 'Multi-record post 2',
|
|
|
|
'body' => 'Second multi-record post',
|
|
|
|
'author_id' => 2
|
|
|
|
)), array('fieldList' => $fieldList));
|
|
|
|
|
|
|
|
$result = $TestModel->find('all', array(
|
|
|
|
'recursive' => -1,
|
|
|
|
'order' => 'Post.id ASC'
|
|
|
|
));
|
|
|
|
$expected = array(
|
|
|
|
array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '1',
|
|
|
|
'author_id' => '2',
|
|
|
|
'title' => 'Multi-record post 1',
|
|
|
|
'body' => '',
|
|
|
|
'published' => 'N',
|
2012-03-28 15:49:41 +00:00
|
|
|
'created' => self::date(),
|
|
|
|
'updated' => self::date()
|
2011-12-22 20:37:48 +00:00
|
|
|
)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'Post' => array(
|
|
|
|
'id' => '2',
|
|
|
|
'author_id' => '2',
|
|
|
|
'title' => 'Multi-record post 2',
|
|
|
|
'body' => '',
|
|
|
|
'published' => 'N',
|
2012-03-28 15:49:41 +00:00
|
|
|
'created' => self::date(),
|
|
|
|
'updated' => self::date()
|
2011-12-22 20:37:48 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2011-12-22 20:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveAllFieldListHasMany method
|
|
|
|
*
|
2014-04-02 00:23:43 +00:00
|
|
|
* @return void
|
2011-12-22 20:37:48 +00:00
|
|
|
*/
|
|
|
|
public function testSaveAllFieldListHasMany() {
|
|
|
|
$this->loadFixtures('Article', 'Comment');
|
|
|
|
$TestModel = new Article();
|
|
|
|
$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
|
|
|
|
|
|
|
|
$this->db->truncate($TestModel);
|
|
|
|
$this->db->truncate(new Comment());
|
|
|
|
|
2012-11-02 09:56:49 +00:00
|
|
|
$data = array(
|
|
|
|
'Article' => array('title' => 'I will not save'),
|
2011-12-22 20:37:48 +00:00
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
|
|
|
|
array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
2012-11-02 09:56:49 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$fieldList = array(
|
|
|
|
'Article' => array('id'),
|
|
|
|
'Comment' => array('article_id', 'user_id')
|
|
|
|
);
|
|
|
|
$TestModel->saveAll($data, array('fieldList' => $fieldList));
|
2011-12-22 20:37:48 +00:00
|
|
|
|
|
|
|
$result = $TestModel->find('all');
|
|
|
|
$this->assertEquals('', $result[0]['Article']['title']);
|
|
|
|
$this->assertEquals('', $result[0]['Comment'][0]['comment']);
|
|
|
|
$this->assertEquals('', $result[0]['Comment'][1]['comment']);
|
2012-11-02 09:56:49 +00:00
|
|
|
|
|
|
|
$fieldList = array(
|
|
|
|
'Article' => array('id'),
|
|
|
|
'Comment' => array('user_id')
|
|
|
|
);
|
|
|
|
$TestModel->saveAll($data, array('fieldList' => $fieldList));
|
|
|
|
$result = $TestModel->find('all');
|
|
|
|
|
|
|
|
$this->assertEquals('', $result[1]['Article']['title']);
|
|
|
|
$this->assertEquals(2, count($result[1]['Comment']));
|
|
|
|
|
|
|
|
$TestModel->whitelist = array('id');
|
|
|
|
$TestModel->Comment->whitelist = array('user_id');
|
|
|
|
$TestModel->saveAll($data);
|
|
|
|
$result = $TestModel->find('all');
|
|
|
|
|
|
|
|
$this->assertEquals('', $result[2]['Article']['title']);
|
|
|
|
$this->assertEquals(2, count($result[2]['Comment']));
|
2011-12-22 20:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveAllFieldListHasOne method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAllFieldListHasOne() {
|
|
|
|
$this->loadFixtures('Attachment', 'Comment', 'Article', 'User');
|
|
|
|
$TestModel = new Comment();
|
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->validate = array('comment' => 'notBlank');
|
|
|
|
$TestModel->Attachment->validate = array('attachment' => 'notBlank');
|
2011-12-22 20:37:48 +00:00
|
|
|
|
2011-12-23 19:48:07 +00:00
|
|
|
$record = array(
|
2011-12-22 20:37:48 +00:00
|
|
|
'Comment' => array(
|
2011-12-23 19:48:07 +00:00
|
|
|
'user_id' => 1,
|
|
|
|
'article_id' => 1,
|
|
|
|
'comment' => '',
|
2011-12-22 20:37:48 +00:00
|
|
|
),
|
|
|
|
'Attachment' => array(
|
2011-12-23 19:48:07 +00:00
|
|
|
'attachment' => ''
|
2011-12-22 20:37:48 +00:00
|
|
|
)
|
2011-12-23 19:48:07 +00:00
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($record, array('validate' => 'only'));
|
|
|
|
$this->assertFalse($result);
|
2011-12-22 20:37:48 +00:00
|
|
|
|
2011-12-23 19:48:07 +00:00
|
|
|
$fieldList = array(
|
|
|
|
'Comment' => array('id', 'article_id', 'user_id'),
|
|
|
|
'Attachment' => array('comment_id')
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($record, array(
|
|
|
|
'fieldList' => $fieldList, 'validate' => 'only'
|
2011-12-23 19:29:51 +00:00
|
|
|
));
|
2011-12-23 19:48:07 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
$this->assertEmpty($TestModel->validationErrors);
|
2012-11-02 11:16:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveAllFieldListHasOneAddFkToWhitelist method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAllFieldListHasOneAddFkToWhitelist() {
|
|
|
|
$this->loadFixtures('ArticleFeatured', 'Featured');
|
|
|
|
$Article = new ArticleFeatured();
|
|
|
|
$Article->belongsTo = $Article->hasMany = array();
|
2015-05-17 20:27:16 +00:00
|
|
|
$Article->Featured->validate = array('end_date' => 'notBlank');
|
2012-11-02 11:16:32 +00:00
|
|
|
|
|
|
|
$record = array(
|
|
|
|
'ArticleFeatured' => array(
|
|
|
|
'user_id' => 1,
|
|
|
|
'title' => 'First Article',
|
|
|
|
'body' => '',
|
|
|
|
'published' => 'Y'
|
|
|
|
),
|
|
|
|
'Featured' => array(
|
|
|
|
'category_id' => 1,
|
|
|
|
'end_date' => ''
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $Article->saveAll($record, array('validate' => 'only'));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
$expected = array(
|
|
|
|
'body' => array(
|
|
|
|
'This field cannot be left blank'
|
|
|
|
),
|
|
|
|
'Featured' => array(
|
|
|
|
'end_date' => array(
|
|
|
|
'This field cannot be left blank'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $Article->validationErrors);
|
2012-11-02 09:56:49 +00:00
|
|
|
|
|
|
|
$fieldList = array(
|
2012-11-02 11:16:32 +00:00
|
|
|
'ArticleFeatured' => array('user_id', 'title'),
|
|
|
|
'Featured' => array('category_id')
|
2012-11-02 09:56:49 +00:00
|
|
|
);
|
2012-11-02 11:16:32 +00:00
|
|
|
|
|
|
|
$result = $Article->saveAll($record, array(
|
|
|
|
'fieldList' => $fieldList, 'validate' => 'first'
|
2012-11-02 09:56:49 +00:00
|
|
|
));
|
|
|
|
$this->assertTrue($result);
|
2012-11-02 11:16:32 +00:00
|
|
|
$this->assertEmpty($Article->validationErrors);
|
|
|
|
|
|
|
|
$Article->recursive = 0;
|
|
|
|
$result = $Article->find('first', array('order' => array('ArticleFeatured.created' => 'DESC')));
|
|
|
|
$this->assertSame($result['ArticleFeatured']['id'], $result['Featured']['article_featured_id']);
|
2011-12-22 20:37:48 +00:00
|
|
|
}
|
|
|
|
|
2012-02-13 00:00:28 +00:00
|
|
|
/**
|
|
|
|
* testSaveAllDeepFieldListValidateBelongsTo
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAllDeepFieldListValidateBelongsTo() {
|
|
|
|
$this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
|
|
|
|
$TestModel = new Post();
|
|
|
|
$TestModel->Author->bindModel(array('hasMany' => array('Comment' => array('foreignKey' => 'user_id'))), false);
|
|
|
|
$TestModel->recursive = 2;
|
|
|
|
|
|
|
|
$result = $TestModel->find('all');
|
2012-04-21 13:02:35 +00:00
|
|
|
$this->assertEquals(3, count($result));
|
2012-02-13 00:00:28 +00:00
|
|
|
$this->assertFalse(isset($result[3]));
|
|
|
|
|
|
|
|
// test belongsTo
|
|
|
|
$fieldList = array(
|
|
|
|
'Post' => array('title', 'author_id'),
|
|
|
|
'Author' => array('user'),
|
|
|
|
'Comment' => array('comment')
|
|
|
|
);
|
|
|
|
$TestModel->saveAll(array(
|
|
|
|
'Post' => array(
|
|
|
|
'title' => 'Post without body',
|
|
|
|
'body' => 'This will not be saved',
|
|
|
|
),
|
|
|
|
'Author' => array(
|
|
|
|
'user' => 'bob',
|
|
|
|
'test' => 'This will not be saved',
|
|
|
|
'Comment' => array(
|
|
|
|
array('id' => 5, 'comment' => 'I am still published', 'published' => 'N'))
|
|
|
|
|
|
|
|
)), array('fieldList' => $fieldList, 'deep' => true));
|
|
|
|
|
|
|
|
$result = $TestModel->Author->Comment->find('first', array(
|
|
|
|
'conditions' => array('Comment.id' => 5),
|
|
|
|
'fields' => array('comment', 'published')
|
|
|
|
));
|
|
|
|
$expected = array(
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => 'I am still published',
|
|
|
|
'published' => 'Y'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveAllDeepFieldListHasMany method
|
|
|
|
*
|
2014-04-02 00:23:43 +00:00
|
|
|
* @return void
|
2012-02-13 00:00:28 +00:00
|
|
|
*/
|
|
|
|
public function testSaveAllDeepFieldListHasMany() {
|
|
|
|
$this->loadFixtures('Article', 'Comment', 'User');
|
|
|
|
$TestModel = new Article();
|
|
|
|
$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
|
|
|
|
|
|
|
|
$this->db->truncate($TestModel);
|
|
|
|
$this->db->truncate(new Comment());
|
|
|
|
|
|
|
|
$fieldList = array(
|
|
|
|
'Article' => array('id'),
|
|
|
|
'Comment' => array('article_id', 'user_id'),
|
|
|
|
'User' => array('user')
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $TestModel->saveAll(array(
|
|
|
|
'Article' => array('id' => 2, 'title' => 'I will not save'),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
|
2012-02-13 04:56:10 +00:00
|
|
|
array(
|
|
|
|
'comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2,
|
|
|
|
'User' => array('user' => 'nopassword', 'password' => 'not saved')
|
|
|
|
)
|
2012-02-13 00:00:28 +00:00
|
|
|
)
|
|
|
|
), array('fieldList' => $fieldList, 'deep' => true));
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->User->find('first', array(
|
|
|
|
'conditions' => array('User.user' => 'nopassword'),
|
|
|
|
'fields' => array('user', 'password')
|
|
|
|
));
|
|
|
|
$expected = array(
|
|
|
|
'User' => array(
|
|
|
|
'user' => 'nopassword',
|
|
|
|
'password' => ''
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
2012-03-19 00:27:35 +00:00
|
|
|
/**
|
|
|
|
* testSaveAllDeepHasManyBelongsTo method
|
|
|
|
*
|
2014-04-02 00:23:43 +00:00
|
|
|
* @return void
|
2012-03-19 00:27:35 +00:00
|
|
|
*/
|
|
|
|
public function testSaveAllDeepHasManyBelongsTo() {
|
|
|
|
$this->loadFixtures('Article', 'Comment', 'User');
|
|
|
|
$TestModel = new Article();
|
|
|
|
$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
|
|
|
|
|
|
|
|
$this->db->truncate($TestModel);
|
|
|
|
$this->db->truncate(new Comment());
|
|
|
|
|
|
|
|
$result = $TestModel->saveAll(array(
|
2012-03-21 09:38:04 +00:00
|
|
|
'Article' => array('id' => 2, 'title' => 'The title'),
|
2012-03-19 00:27:35 +00:00
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
|
|
|
|
array(
|
|
|
|
'comment' => 'belongsto', 'published' => 'Y',
|
|
|
|
'User' => array('user' => 'findme', 'password' => 'somepass')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
), array('deep' => true));
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->User->find('first', array(
|
|
|
|
'conditions' => array('User.user' => 'findme'),
|
|
|
|
'fields' => array('id', 'user', 'password')
|
|
|
|
));
|
|
|
|
$expected = array(
|
|
|
|
'User' => array(
|
|
|
|
'id' => 5,
|
|
|
|
'user' => 'findme',
|
|
|
|
'password' => 'somepass',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->find('first', array(
|
|
|
|
'conditions' => array('Comment.user_id' => 5),
|
|
|
|
'fields' => array('id', 'comment', 'published', 'user_id')
|
|
|
|
));
|
|
|
|
$expected = array(
|
|
|
|
'Comment' => array(
|
|
|
|
'id' => 2,
|
|
|
|
'comment' => 'belongsto',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 5
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
2012-03-20 19:41:41 +00:00
|
|
|
/**
|
|
|
|
* testSaveAllDeepHasManyhasMany method
|
|
|
|
*
|
2014-04-02 00:23:43 +00:00
|
|
|
* @return void
|
2012-03-20 19:41:41 +00:00
|
|
|
*/
|
|
|
|
public function testSaveAllDeepHasManyHasMany() {
|
|
|
|
$this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
|
|
|
|
$TestModel = new Article();
|
2012-03-21 09:38:04 +00:00
|
|
|
$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
|
2012-03-20 19:41:41 +00:00
|
|
|
$TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
|
|
|
|
$TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
|
|
|
|
|
|
|
|
$this->db->truncate($TestModel);
|
|
|
|
$this->db->truncate(new Comment());
|
|
|
|
$this->db->truncate(new Attachment());
|
|
|
|
|
|
|
|
$result = $TestModel->saveAll(array(
|
2012-03-21 09:38:04 +00:00
|
|
|
'Article' => array('id' => 2, 'title' => 'The title'),
|
2012-03-20 19:41:41 +00:00
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
|
|
|
|
array(
|
|
|
|
'comment' => 'hasmany', 'published' => 'Y', 'user_id' => 5,
|
|
|
|
'Attachment' => array(
|
|
|
|
array('attachment' => 'first deep attachment'),
|
|
|
|
array('attachment' => 'second deep attachment'),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
), array('deep' => true));
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->find('first', array(
|
|
|
|
'conditions' => array('Comment.comment' => 'hasmany'),
|
|
|
|
'fields' => array('id', 'comment', 'published', 'user_id'),
|
|
|
|
'recursive' => -1
|
|
|
|
));
|
|
|
|
$expected = array(
|
|
|
|
'Comment' => array(
|
|
|
|
'id' => 2,
|
|
|
|
'comment' => 'hasmany',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 5
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->find('all', array(
|
|
|
|
'fields' => array('attachment', 'comment_id'),
|
|
|
|
'order' => array('Attachment.id' => 'ASC')
|
|
|
|
));
|
|
|
|
$expected = array(
|
|
|
|
array('Attachment' => array('attachment' => 'first deep attachment', 'comment_id' => 2)),
|
|
|
|
array('Attachment' => array('attachment' => 'second deep attachment', 'comment_id' => 2)),
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
2012-03-21 09:38:04 +00:00
|
|
|
/**
|
2012-03-21 10:07:51 +00:00
|
|
|
* testSaveAllDeepOrderHasManyHasMany method
|
2012-03-21 09:38:04 +00:00
|
|
|
*
|
2014-04-02 00:23:43 +00:00
|
|
|
* @return void
|
2012-03-21 09:38:04 +00:00
|
|
|
*/
|
2012-03-21 10:07:51 +00:00
|
|
|
public function testSaveAllDeepOrderHasManyHasMany() {
|
2012-03-21 09:38:04 +00:00
|
|
|
$this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
|
|
|
|
$TestModel = new Article();
|
|
|
|
$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
|
|
|
|
$TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
|
|
|
|
$TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
|
|
|
|
|
|
|
|
$this->db->truncate($TestModel);
|
|
|
|
$this->db->truncate(new Comment());
|
|
|
|
$this->db->truncate(new Attachment());
|
|
|
|
|
|
|
|
$result = $TestModel->saveAll(array(
|
|
|
|
'Article' => array('id' => 2, 'title' => 'Comment has its data after Attachment'),
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'Attachment' => array(
|
|
|
|
array('attachment' => 'attachment should be created with comment_id'),
|
|
|
|
array('attachment' => 'comment should be created with article_id'),
|
|
|
|
),
|
2012-03-21 10:07:51 +00:00
|
|
|
'comment' => 'after associated data',
|
|
|
|
'user_id' => 1
|
2012-03-21 09:38:04 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
), array('deep' => true));
|
|
|
|
$result = $TestModel->Comment->find('first', array(
|
|
|
|
'conditions' => array('Comment.article_id' => 2),
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals(2, $result['Comment']['article_id']);
|
|
|
|
$this->assertEquals(2, count($result['Attachment']));
|
2012-03-21 10:07:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveAllDeepEmptyHasManyHasMany method
|
|
|
|
*
|
2014-04-02 00:23:43 +00:00
|
|
|
* @return void
|
2012-03-21 10:07:51 +00:00
|
|
|
*/
|
|
|
|
public function testSaveAllDeepEmptyHasManyHasMany() {
|
|
|
|
$this->skipIf(!$this->db instanceof Mysql, 'This test is only compatible with Mysql.');
|
2012-09-04 21:24:10 +00:00
|
|
|
|
2012-03-21 10:07:51 +00:00
|
|
|
$this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
|
|
|
|
$TestModel = new Article();
|
|
|
|
$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
|
|
|
|
$TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
|
|
|
|
$TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
|
|
|
|
|
|
|
|
$this->db->truncate($TestModel);
|
|
|
|
$this->db->truncate(new Comment());
|
|
|
|
$this->db->truncate(new Attachment());
|
2012-03-21 09:38:04 +00:00
|
|
|
|
|
|
|
$result = $TestModel->saveAll(array(
|
2012-09-04 21:24:10 +00:00
|
|
|
'Article' => array('id' => 3, 'user_id' => 1, 'title' => 'Comment has no data'),
|
2012-03-21 09:38:04 +00:00
|
|
|
'Comment' => array(
|
|
|
|
array(
|
2012-09-04 21:24:10 +00:00
|
|
|
'user_id' => 1,
|
2012-03-21 09:38:04 +00:00
|
|
|
'Attachment' => array(
|
|
|
|
array('attachment' => 'attachment should be created with comment_id'),
|
|
|
|
array('attachment' => 'comment should be created with article_id'),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
), array('deep' => true));
|
|
|
|
$result = $TestModel->Comment->find('first', array(
|
|
|
|
'conditions' => array('Comment.article_id' => 3),
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals(3, $result['Comment']['article_id']);
|
|
|
|
$this->assertEquals(2, count($result['Attachment']));
|
|
|
|
}
|
|
|
|
|
2015-07-22 04:40:45 +00:00
|
|
|
/**
|
|
|
|
* Test that boolean fields don't cause saveMany to fail
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveManyBooleanFields() {
|
|
|
|
$this->loadFixtures('Item', 'Syfile', 'Image');
|
|
|
|
$data = array(
|
|
|
|
array(
|
|
|
|
'Item' => array(
|
|
|
|
'name' => 'testing',
|
|
|
|
'syfile_id' => 1,
|
|
|
|
'published' => false
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'Item' => array(
|
|
|
|
'name' => 'testing 2',
|
|
|
|
'syfile_id' => 1,
|
|
|
|
'published' => true
|
|
|
|
)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$item = ClassRegistry::init('Item');
|
|
|
|
$result = $item->saveMany($data, array('atomic' => false));
|
|
|
|
|
|
|
|
$this->assertCount(2, $result, '2 records should have been saved.');
|
|
|
|
$this->assertTrue($result[0], 'Both should have succeded');
|
|
|
|
$this->assertTrue($result[1], 'Both should have succeded');
|
|
|
|
}
|
|
|
|
|
2015-07-04 06:10:58 +00:00
|
|
|
/**
|
|
|
|
* testSaveManyDeepHasManyValidationFailure method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveManyDeepHasManyValidationFailure() {
|
|
|
|
$this->loadFixtures('Article', 'Comment');
|
|
|
|
$TestModel = new Article();
|
|
|
|
$TestModel->Comment->validate = array(
|
|
|
|
'comment' => array(
|
2015-07-13 06:37:11 +00:00
|
|
|
'notBlank' => array(
|
|
|
|
'rule' => array('notBlank'),
|
2015-07-04 06:10:58 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $TestModel->saveMany(array(
|
|
|
|
array(
|
|
|
|
'user_id' => 1,
|
|
|
|
'title' => 'New Article',
|
|
|
|
'body' => 'This article contains a invalid comment',
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'user_id' => 1,
|
|
|
|
'comment' => ''
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
), array('deep' => true));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
$this->assertEquals(array(
|
|
|
|
array(
|
|
|
|
'Comment' => array(
|
2015-07-13 06:37:11 +00:00
|
|
|
array('comment' => array('notBlank'))
|
2015-07-04 06:10:58 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
), $TestModel->validationErrors);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveAssociatedDeepHasOneHasManyValidateTrueValidationFailure method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAssociatedDeepHasOneHasManyValidateTrueValidationFailure() {
|
|
|
|
$this->loadFixtures('User', 'Article', 'Comment');
|
|
|
|
$TestModel = new UserHasOneArticle();
|
|
|
|
$TestModel->Article->Comment->validate = array(
|
|
|
|
'comment' => array(
|
2015-07-13 06:37:11 +00:00
|
|
|
'notBlank' => array(
|
|
|
|
'rule' => array('notBlank'),
|
2015-07-04 06:10:58 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $TestModel->saveAssociated(array(
|
|
|
|
'User' => array(
|
|
|
|
'user' => 'hiromi',
|
|
|
|
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
|
|
|
|
),
|
|
|
|
'Article' => array(
|
|
|
|
'title' => 'Article with User',
|
|
|
|
'body' => 'This article will be saved with an user and contains a invalid comment',
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'user_id' => 1,
|
|
|
|
'comment' => ''
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
), array('deep' => true, 'validate' => true));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'Article' => array(
|
|
|
|
'Comment' => array(
|
2015-07-13 06:37:11 +00:00
|
|
|
array('comment' => array('notBlank'))
|
2015-07-04 06:10:58 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
), $TestModel->validationErrors);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveAssociatedDeepBelongsToHasManyValidateTrueValidationFailure method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAssociatedDeepBelongsToHasManyValidateTrueValidationFailure() {
|
|
|
|
$this->loadFixtures('ArticlesTag', 'Article', 'Comment');
|
|
|
|
$TestModel = new ArticlesTagBelongsToArticle();
|
|
|
|
$TestModel->Article->Comment->validate = array(
|
|
|
|
'comment' => array(
|
2015-07-13 06:37:11 +00:00
|
|
|
'notBlank' => array(
|
|
|
|
'rule' => array('notBlank'),
|
2015-07-04 06:10:58 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $TestModel->saveAssociated(array(
|
|
|
|
'ArticlesTagBelongsToArticle' => array(
|
|
|
|
'tag_id' => 1,
|
|
|
|
),
|
|
|
|
'Article' => array(
|
|
|
|
'title' => 'Article with User',
|
|
|
|
'body' => 'This article will be saved with an user and contains a invalid comment',
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'user_id' => 1,
|
|
|
|
'comment' => ''
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
), array('deep' => true, 'validate' => true));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'Article' => array(
|
|
|
|
'Comment' => array(
|
2015-07-13 06:37:11 +00:00
|
|
|
array('comment' => array('notBlank'))
|
2015-07-04 06:10:58 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
), $TestModel->validationErrors);
|
|
|
|
}
|
|
|
|
|
2015-07-22 04:40:45 +00:00
|
|
|
/**
|
|
|
|
* Test that boolean fields don't cause saveAssociated to fail
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAssociatedHasOneBooleanFields() {
|
|
|
|
$this->loadFixtures('Item', 'Syfile', 'Image');
|
|
|
|
$data = array(
|
|
|
|
'Syfile' => array(
|
|
|
|
'image_id' => 1,
|
|
|
|
'name' => 'Some file',
|
|
|
|
),
|
|
|
|
'Item' => array(
|
|
|
|
'name' => 'testing',
|
|
|
|
'published' => false
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$syfile = ClassRegistry::init('Syfile');
|
|
|
|
$syfile->bindModel(array('hasOne' => array('Item')), false);
|
|
|
|
$result = $syfile->saveAssociated($data, array('atomic' => false));
|
|
|
|
|
|
|
|
$this->assertCount(2, $result, '2 records should have been saved.');
|
|
|
|
$this->assertTrue($result['Syfile'], 'Both should have succeded');
|
|
|
|
$this->assertTrue($result['Item'], 'Both should have succeded');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that boolean fields don't cause saveAssociated to fail
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAssociatedBelongsToBooleanFields() {
|
|
|
|
$this->loadFixtures('Item', 'Syfile', 'Image');
|
|
|
|
$data = array(
|
|
|
|
'Syfile' => array(
|
|
|
|
'image_id' => 1,
|
|
|
|
'name' => 'Some file',
|
|
|
|
),
|
|
|
|
'Item' => array(
|
|
|
|
'name' => 'testing',
|
|
|
|
'syfile_id' => 2,
|
|
|
|
'published' => false
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$item = ClassRegistry::init('Item');
|
|
|
|
$item->bindModel(array(
|
|
|
|
'belongsTo' => array(
|
|
|
|
'Item' => array(
|
|
|
|
'foreignKey' => 'image_id'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
), false);
|
|
|
|
$result = $item->saveAssociated($data, array('atomic' => false));
|
|
|
|
|
|
|
|
$this->assertCount(2, $result, '2 records should have been saved.');
|
|
|
|
$this->assertTrue($result['Syfile'], 'Both should have succeded');
|
|
|
|
$this->assertTrue($result['Item'], 'Both should have succeded');
|
|
|
|
}
|
|
|
|
|
2012-03-19 12:27:26 +00:00
|
|
|
/**
|
|
|
|
* testUpdateAllBoolean
|
|
|
|
*
|
2014-04-02 00:23:43 +00:00
|
|
|
* @return void
|
2012-03-19 12:27:26 +00:00
|
|
|
*/
|
|
|
|
public function testUpdateAllBoolean() {
|
|
|
|
$this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
|
|
|
|
$TestModel = new Item();
|
|
|
|
$result = $TestModel->updateAll(array('published' => true));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$result = $TestModel->find('first', array('fields' => array('id', 'published')));
|
|
|
|
$this->assertEquals(true, $result['Item']['published']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testUpdateAllBooleanConditions
|
|
|
|
*
|
2014-04-02 00:23:43 +00:00
|
|
|
* @return void
|
2012-03-19 12:27:26 +00:00
|
|
|
*/
|
|
|
|
public function testUpdateAllBooleanConditions() {
|
|
|
|
$this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
|
|
|
|
$TestModel = new Item();
|
|
|
|
|
|
|
|
$result = $TestModel->updateAll(array('published' => true), array('Item.id' => 1));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$result = $TestModel->find('first', array(
|
|
|
|
'fields' => array('id', 'published'),
|
|
|
|
'conditions' => array('Item.id' => 1)));
|
|
|
|
$this->assertEquals(true, $result['Item']['published']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testUpdateBoolean
|
|
|
|
*
|
2014-04-02 00:23:43 +00:00
|
|
|
* @return void
|
2012-03-19 12:27:26 +00:00
|
|
|
*/
|
|
|
|
public function testUpdateBoolean() {
|
|
|
|
$this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
|
|
|
|
$TestModel = new Item();
|
|
|
|
|
|
|
|
$result = $TestModel->save(array('published' => true, 'id' => 1));
|
2013-09-17 13:15:25 +00:00
|
|
|
$this->assertTrue((bool)$result);
|
2012-03-19 12:27:26 +00:00
|
|
|
$result = $TestModel->find('first', array(
|
|
|
|
'fields' => array('id', 'published'),
|
|
|
|
'conditions' => array('Item.id' => 1)));
|
|
|
|
$this->assertEquals(true, $result['Item']['published']);
|
|
|
|
}
|
2013-06-14 13:21:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the clear() method.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testClear() {
|
|
|
|
$this->loadFixtures('Bid');
|
|
|
|
$model = ClassRegistry::init('Bid');
|
|
|
|
$model->set(array('name' => 'Testing', 'message_id' => 3));
|
|
|
|
$this->assertTrue(isset($model->data['Bid']['name']));
|
|
|
|
$this->assertTrue($model->clear());
|
|
|
|
$this->assertFalse(isset($model->data['Bid']['name']));
|
|
|
|
$this->assertFalse(isset($model->data['Bid']['message_id']));
|
|
|
|
}
|
2014-07-12 02:56:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that Model::save() doesn't generate a query with WHERE 1 = 1 on race condition.
|
|
|
|
*
|
|
|
|
* @link https://github.com/cakephp/cakephp/issues/3857
|
2014-07-13 16:21:09 +00:00
|
|
|
* @return void
|
2014-07-12 02:56:36 +00:00
|
|
|
*/
|
|
|
|
public function testSafeUpdateMode() {
|
|
|
|
$this->loadFixtures('User');
|
|
|
|
|
|
|
|
$User = ClassRegistry::init('User');
|
|
|
|
$this->assertFalse($User->__safeUpdateMode);
|
|
|
|
|
|
|
|
$User->getEventManager()->attach(array($this, 'deleteMe'), 'Model.beforeSave');
|
|
|
|
|
|
|
|
$User->id = 1;
|
|
|
|
$User->set(array('user' => 'nobody'));
|
|
|
|
$User->save();
|
|
|
|
|
|
|
|
$users = $User->find('list', array('fields' => 'User.user'));
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
2 => 'nate',
|
|
|
|
3 => 'larry',
|
|
|
|
4 => 'garrett',
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $users);
|
|
|
|
$this->assertFalse($User->__safeUpdateMode);
|
|
|
|
|
|
|
|
$User->id = 2;
|
|
|
|
$User->set(array('user' => $User->getDataSource()->expression('PDO_EXCEPTION()')));
|
|
|
|
try {
|
|
|
|
$User->save(null, false);
|
|
|
|
$this->fail('No exception thrown');
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
$this->assertFalse($User->__safeUpdateMode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Emulates race condition
|
|
|
|
*
|
2014-07-13 16:21:09 +00:00
|
|
|
* @param CakeEvent $event containing the Model
|
|
|
|
* @return void
|
2014-07-12 02:56:36 +00:00
|
|
|
*/
|
|
|
|
public function deleteMe($event) {
|
|
|
|
$Model = $event->subject;
|
|
|
|
$Model->getDataSource()->delete($Model, array($Model->alias . '.' . $Model->primaryKey => $Model->id));
|
|
|
|
}
|
2014-08-01 21:41:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a convenient mock DboSource
|
|
|
|
*
|
|
|
|
* We cannot call several methods via mock DboSource, such as DboSource::value()
|
|
|
|
* because mock DboSource has no $_connection.
|
|
|
|
* This method helps us to avoid this problem.
|
|
|
|
*
|
|
|
|
* @param array $methods Configurable method names.
|
|
|
|
* @return DboSource
|
|
|
|
*/
|
|
|
|
protected function _getMockDboSource($methods = array()) {
|
|
|
|
$testDb = ConnectionManager::getDataSource('test');
|
|
|
|
|
|
|
|
$passthrough = array_diff(array('value', 'begin', 'rollback', 'commit', 'describe', 'lastInsertId', 'execute'), $methods);
|
|
|
|
|
|
|
|
$methods = array_merge($methods, $passthrough);
|
|
|
|
if (!in_array('connect', $methods)) {
|
|
|
|
$methods[] = 'connect'; // This will be called by DboSource::__construct().
|
|
|
|
}
|
|
|
|
|
|
|
|
$db = $this->getMock('DboSource', $methods);
|
|
|
|
$db->columns = $testDb->columns;
|
|
|
|
$db->startQuote = $testDb->startQuote;
|
|
|
|
$db->endQuote = $testDb->endQuote;
|
|
|
|
|
|
|
|
foreach ($passthrough as $method) {
|
|
|
|
$db->expects($this->any())
|
|
|
|
->method($method)
|
|
|
|
->will($this->returnCallback(array($testDb, $method)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $db;
|
|
|
|
}
|
2014-08-03 13:34:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that transactions behave correctly on nested saveMany calls.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTransactionOnNestedSaveMany() {
|
|
|
|
$this->loadFixtures('Post');
|
|
|
|
$Post = new TestPost();
|
|
|
|
$Post->getEventManager()->attach(array($this, 'nestedSaveMany'), 'Model.afterSave');
|
|
|
|
|
|
|
|
// begin -> [ begin -> commit ] -> commit
|
|
|
|
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
|
|
|
|
$db->expects($this->exactly(2))->method('begin')->will($this->returnValue(true));
|
|
|
|
$db->expects($this->exactly(2))->method('commit');
|
|
|
|
$db->expects($this->never())->method('rollback');
|
|
|
|
$Post->setDataSourceObject($db);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
array('author_id' => 1, 'title' => 'Outer Post'),
|
|
|
|
);
|
|
|
|
$Post->dataForAfterSave = array(
|
|
|
|
array('author_id' => 1, 'title' => 'Inner Post'),
|
|
|
|
);
|
|
|
|
$this->assertTrue($Post->saveMany($data));
|
|
|
|
|
|
|
|
// begin -> [ begin(false) ] -> commit
|
|
|
|
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
|
|
|
|
$db->expects($this->at(0))->method('begin')->will($this->returnValue(true));
|
|
|
|
$db->expects($this->at(1))->method('begin')->will($this->returnValue(false));
|
|
|
|
$db->expects($this->once())->method('commit');
|
|
|
|
$db->expects($this->never())->method('rollback');
|
|
|
|
$Post->setDataSourceObject($db);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
array('author_id' => 1, 'title' => 'Outer Post'),
|
|
|
|
);
|
|
|
|
$Post->dataForAfterSave = array(
|
|
|
|
array('author_id' => 1, 'title' => 'Inner Post'),
|
|
|
|
);
|
|
|
|
$this->assertTrue($Post->saveMany($data));
|
|
|
|
|
|
|
|
// begin -> [ begin -> rollback ] -> rollback
|
|
|
|
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
|
|
|
|
$db->expects($this->exactly(2))->method('begin')->will($this->returnValue(true));
|
|
|
|
$db->expects($this->never())->method('commit');
|
|
|
|
$db->expects($this->exactly(2))->method('rollback');
|
|
|
|
$Post->setDataSourceObject($db);
|
|
|
|
$data = array(
|
|
|
|
array('author_id' => 1, 'title' => 'Outer Post'),
|
|
|
|
);
|
|
|
|
$Post->dataForAfterSave = array(
|
|
|
|
array('author_id' => 1, 'title' => 'Inner Post', 'body' => $db->expression('PDO_EXCEPTION()')),
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$Post->saveMany($data);
|
|
|
|
$this->fail('No exception thrown');
|
|
|
|
} catch(Exception $e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that transaction behaves correctly on nested saveAssociated calls.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTransactionOnNestedSaveAssociated() {
|
|
|
|
$this->loadFixtures('Author', 'Post');
|
|
|
|
|
|
|
|
$Author = new TestAuthor();
|
|
|
|
$Author->getEventManager()->attach(array($this, 'nestedSaveAssociated'), 'Model.afterSave');
|
|
|
|
|
|
|
|
// begin -> [ begin -> commit ] -> commit
|
|
|
|
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
|
|
|
|
$db->expects($this->exactly(2))->method('begin')->will($this->returnValue(true));
|
|
|
|
$db->expects($this->exactly(2))->method('commit');
|
|
|
|
$db->expects($this->never())->method('rollback');
|
|
|
|
$Author->setDataSourceObject($db);
|
|
|
|
$Author->Post->setDataSourceObject($db);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Author' => array('user' => 'outer'),
|
|
|
|
'Post' => array(
|
|
|
|
array('title' => 'Outer Post'),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$Author->dataForAfterSave = array(
|
|
|
|
'Author' => array('user' => 'inner'),
|
|
|
|
'Post' => array(
|
|
|
|
array('title' => 'Inner Post'),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertTrue($Author->saveAssociated($data));
|
|
|
|
|
|
|
|
// begin -> [ begin(false) ] -> commit
|
|
|
|
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
|
|
|
|
$db->expects($this->at(0))->method('begin')->will($this->returnValue(true));
|
|
|
|
$db->expects($this->at(1))->method('begin')->will($this->returnValue(false));
|
|
|
|
$db->expects($this->once())->method('commit');
|
|
|
|
$db->expects($this->never())->method('rollback');
|
|
|
|
$Author->setDataSourceObject($db);
|
|
|
|
$Author->Post->setDataSourceObject($db);
|
|
|
|
$data = array(
|
|
|
|
'Author' => array('user' => 'outer'),
|
|
|
|
'Post' => array(
|
|
|
|
array('title' => 'Outer Post'),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$Author->dataForAfterSave = array(
|
|
|
|
'Author' => array('user' => 'inner'),
|
|
|
|
'Post' => array(
|
|
|
|
array('title' => 'Inner Post'),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertTrue($Author->saveAssociated($data));
|
|
|
|
|
|
|
|
// begin -> [ begin -> rollback ] -> rollback
|
|
|
|
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
|
|
|
|
$db->expects($this->exactly(2))->method('begin')->will($this->returnValue(true));
|
|
|
|
$db->expects($this->never())->method('commit');
|
|
|
|
$db->expects($this->exactly(2))->method('rollback');
|
|
|
|
$Author->setDataSourceObject($db);
|
|
|
|
$Author->Post->setDataSourceObject($db);
|
|
|
|
$data = array(
|
|
|
|
'Author' => array('user' => 'outer'),
|
|
|
|
'Post' => array(
|
|
|
|
array('title' => 'Outer Post'),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$Author->dataForAfterSave = array(
|
|
|
|
'Author' => array('user' => 'inner', 'password' => $db->expression('PDO_EXCEPTION()')),
|
|
|
|
'Post' => array(
|
|
|
|
array('title' => 'Inner Post'),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$Author->saveAssociated($data);
|
|
|
|
$this->fail('No exception thrown');
|
|
|
|
} catch(Exception $e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A callback for testing nested saveMany.
|
|
|
|
*
|
|
|
|
* @param CakeEvent $event containing the Model
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function nestedSaveMany($event) {
|
|
|
|
$Model = $event->subject;
|
|
|
|
$Model->saveMany($Model->dataForAfterSave, array('callbacks' => false));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A callback for testing nested saveAssociated.
|
|
|
|
*
|
|
|
|
* @param CakeEvent $event containing the Model
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function nestedSaveAssociated($event) {
|
|
|
|
$Model = $event->subject;
|
|
|
|
$Model->saveAssociated($Model->dataForAfterSave, array('callbacks' => false));
|
|
|
|
}
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|