Skip to content
Permalink
Browse files

#6935 Fix deep saving for hasMany could not handle validation errors …

…correctly
  • Loading branch information...
hiromi2424 committed Jul 4, 2015
1 parent c037e04 commit deb62c88875d1a81ab8e8fb7214d4af93bb06c58
Showing with 172 additions and 3 deletions.
  1. +3 −3 lib/Cake/Model/Model.php
  2. +122 −0 lib/Cake/Test/Case/Model/ModelWriteTest.php
  3. +47 −0 lib/Cake/Test/Case/Model/models.php
@@ -2339,7 +2339,7 @@ public function saveMany($data = null, $options = array()) {
}
}
$validates = ($validates && ($saved === true || (is_array($saved) && !in_array(false, $saved, true))));
$validates = ($validates && ($saved === true || (is_array($saved) && !in_array(false, Hash::flatten($saved), true))));
if (!$validates) {
$validationErrors[$key] = $this->validationErrors;
}
@@ -2480,7 +2480,7 @@ public function saveAssociated($data = null, $options = array()) {
} else {
$saved = $Model->save($values, array('atomic' => false) + $options);
}
$validates = ($saved === true || (is_array($saved) && !in_array(false, $saved, true)));
$validates = ($saved === true || (is_array($saved) && !in_array(false, Hash::flatten($saved), true)));
}
if ($validates) {
@@ -2538,7 +2538,7 @@ public function saveAssociated($data = null, $options = array()) {
}
}
$validates = ($validates && ($saved === true || (is_array($saved) && !in_array(false, $saved, true))));
$validates = ($validates && ($saved === true || (is_array($saved) && !in_array(false, Hash::flatten($saved), true))));
if (!$validates) {
$validationErrors[$association] = $Model->validationErrors;
}
@@ -7684,6 +7684,128 @@ public function testSaveAllDeepEmptyHasManyHasMany() {
$this->assertEquals(2, count($result['Attachment']));
}
/**
* testSaveManyDeepHasManyValidationFailure method
*
* @return void
*/
public function testSaveManyDeepHasManyValidationFailure() {
$this->loadFixtures('Article', 'Comment');
$TestModel = new Article();
$TestModel->Comment->validate = array(
'comment' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
)
)
);
$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(
array('comment' => array('notEmpty'))
)
)
), $TestModel->validationErrors);
}
/**
* testSaveAssociatedDeepHasOneHasManyValidateTrueValidationFailure method
*
* @return void
*/
public function testSaveAssociatedDeepHasOneHasManyValidateTrueValidationFailure() {
$this->loadFixtures('User', 'Article', 'Comment');
$TestModel = new UserHasOneArticle();
$TestModel->Article->Comment->validate = array(
'comment' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
)
)
);
$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(
array('comment' => array('notEmpty'))
)
)
), $TestModel->validationErrors);
}
/**
* testSaveAssociatedDeepBelongsToHasManyValidateTrueValidationFailure method
*
* @return void
*/
public function testSaveAssociatedDeepBelongsToHasManyValidateTrueValidationFailure() {
$this->loadFixtures('ArticlesTag', 'Article', 'Comment');
$TestModel = new ArticlesTagBelongsToArticle();
$TestModel->Article->Comment->validate = array(
'comment' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
)
)
);
$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(
array('comment' => array('notEmpty'))
)
)
), $TestModel->validationErrors);
}
/**
* testUpdateAllBoolean
*
@@ -5079,3 +5079,50 @@ class Example extends AppModel {
);
}
/**
* UserHasOneArticle class
*
* @package Cake.Test.Case.Model
*/
class UserHasOneArticle extends AppModel {
/**
* useTable property
*
* @var string
*/
public $useTable = 'users';
/**
* hasOne property
*
* @var array
*/
public $hasOne = array('Article');
}
/**
* ArticlesTagBelongsToArticle class
*
* @package Cake.Test.Case.Model
*/
class ArticlesTagBelongsToArticle extends CakeTestModel {
/**
* useTable property
*
* @var string
*/
public $useTable = 'articles_tags';
/**
* belongsTo property
*
* @var array
*/
public $belongsTo = array('Article');
}

0 comments on commit deb62c8

Please sign in to comment.
You can’t perform that action at this time.