diff --git a/tests/TestCase/ORM/DomainRulesIntegrationTest.php b/tests/TestCase/ORM/DomainRulesIntegrationTest.php index 2ebd07a81ae..02fb967c3fb 100644 --- a/tests/TestCase/ORM/DomainRulesIntegrationTest.php +++ b/tests/TestCase/ORM/DomainRulesIntegrationTest.php @@ -151,4 +151,49 @@ public function testSaveHasManyWithErrorsAtomic() { $this->assertNotEmpty($entity->articles[1]->errors()); } +/** + * Tests that it is possible to continue saving hasMany associations + * even if any of the records fail validation when atomic is set + * to false + * + * @return void + */ + public function testSaveHasManyWithErrorsNonAtomic() { + $entity = new \Cake\ORM\Entity([ + 'name' => 'Jose' + ]); + $entity->articles = [ + new \Cake\ORM\Entity([ + 'title' => 'A title', + 'body' => 'A body' + ]), + new \Cake\ORM\Entity([ + 'title' => '1', + 'body' => 'Another body' + ]) + ]; + + $table = TableRegistry::get('authors'); + $table->hasMany('articles'); + $table->association('articles') + ->target() + ->domainRules() + ->add(function (Entity $article) { + if (!is_numeric($article->title)) { + $article->errors('title', ['an error']); + return false; + } + return true; + }); + + $result = $table->save($entity, ['atomic' => false]); + $this->assertSame($entity, $result); + $this->assertFalse($entity->isNew()); + $this->assertTrue($entity->articles[0]->isNew()); + $this->assertFalse($entity->articles[1]->isNew()); + $this->assertEquals(4, $entity->articles[1]->id); + $this->assertNull($entity->articles[0]->id); + $this->assertNotEmpty($entity->articles[0]->errors('title')); + } + }