Merge pull request #1472 from ADmad/bugfix/3948

Fixed bug when using multi model fieldList and whitelists for all models...
This commit is contained in:
Christian Winther 2013-08-03 05:56:22 -07:00
commit cd44c2afae
4 changed files with 82 additions and 5 deletions

View file

@ -1621,9 +1621,10 @@ class Model extends Object implements CakeEventListener {
}
if (!empty($options['fieldList'])) {
$this->whitelist = $options['fieldList'];
if (!empty($options['fieldList'][$this->alias]) && is_array($options['fieldList'][$this->alias])) {
$this->whitelist = $options['fieldList'][$this->alias];
} elseif (Hash::dimensions($options['fieldList']) < 2) {
$this->whitelist = $options['fieldList'];
}
} elseif ($options['fieldList'] === null) {
$this->whitelist = array();
@ -2338,7 +2339,10 @@ class Model extends Object implements CakeEventListener {
$options['fieldList'][$this->alias][] = $key;
return $options;
}
if (!empty($options['fieldList']) && is_array($options['fieldList'])) {
if (!empty($options['fieldList']) &&
is_array($options['fieldList']) &&
Hash::dimensions($options['fieldList']) < 2
) {
$options['fieldList'][] = $key;
}
return $options;

View file

@ -21,6 +21,7 @@
*/
App::uses('CakeValidationSet', 'Model/Validator');
App::uses('Hash', 'Utility');
/**
* ModelValidator object encapsulates all methods related to data validations for a model
@ -394,11 +395,11 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
}
unset($fieldList);
$validateList = array();
if (empty($whitelist)) {
if (empty($whitelist) || Hash::dimensions($whitelist) > 1) {
return $this->_fields;
}
$validateList = array();
$this->validationErrors = array();
foreach ((array)$whitelist as $f) {
if (!empty($this->_fields[$f])) {

View file

@ -1522,7 +1522,7 @@ class ModelValidationTest extends BaseModelTest {
* @return void
*/
public function testValidateAssociated() {
$this->loadFixtures('Comment', 'Attachment');
$this->loadFixtures('Comment', 'Attachment', 'Article', 'User');
$TestModel = new Comment();
$TestModel->Attachment->validate = array('attachment' => 'notEmpty');
@ -1539,6 +1539,18 @@ class ModelValidationTest extends BaseModelTest {
$result = $TestModel->validateAssociated($data);
$this->assertFalse($result);
$fieldList = array(
'Attachment' => array('comment_id')
);
$result = $TestModel->saveAll($data, array(
'fieldList' => $fieldList, 'validate' => 'only'
));
$this->assertTrue($result);
$this->assertEmpty($TestModel->validationErrors);
$result = $TestModel->validateAssociated($data, array('fieldList' => $fieldList));
$this->assertTrue($result);
$this->assertEmpty($TestModel->validationErrors);
$TestModel->validate = array('comment' => 'notEmpty');
$record = array(
'Comment' => array(

View file

@ -6559,6 +6559,66 @@ class ModelWriteTest extends BaseModelTest {
$this->assertEquals('', $result[3]['Post']['body']);
$this->assertEquals('working', $result[3]['Author']['test']);
$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]);
// test multirecord
$this->db->truncate($TestModel);