Skip to content

Commit

Permalink
Adding test for saving hasMany and atomic set to false
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Nov 25, 2013
1 parent 18d819f commit 7b3c569
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -2122,4 +2122,44 @@ public function testSaveHasManyWithErrorsAtomic() {
$this->assertEmpty($entity->articles[0]->errors());
$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()
->validator()
->add('title', 'num', ['rule' => 'numeric']);

$this->assertSame($entity, $table->save($entity, ['atomic' => false]));
$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->assertEquals(5, $entity->articles[0]->author_id);
$this->assertEquals(5, $entity->articles[1]->author_id);
}

}

0 comments on commit 7b3c569

Please sign in to comment.