From dbeee9d5c30947f7d32df99d4dab83cb80a78077 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Sat, 23 Nov 2013 21:37:08 +0100 Subject: [PATCH] Added test to prove that valiadtion in associated save will abort the full save operation --- Cake/Test/TestCase/ORM/TableTest.php | 31 +++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/Cake/Test/TestCase/ORM/TableTest.php b/Cake/Test/TestCase/ORM/TableTest.php index 68b3c6d6437..f9478479642 100644 --- a/Cake/Test/TestCase/ORM/TableTest.php +++ b/Cake/Test/TestCase/ORM/TableTest.php @@ -1953,7 +1953,7 @@ public function testsSaveBelongsTo() { 'body' => 'A body' ]); $entity->author = new \Cake\ORM\Entity([ - 'name' => 'Jose', + 'name' => 'Jose' ]); $table = TableRegistry::get('articles'); @@ -1965,4 +1965,33 @@ public function testsSaveBelongsTo() { $this->assertEquals(5, $entity->get('author_id')); } +/** + * Tests saving belongsTo association and get a validation error + * + * @group save + * @return void + */ + public function testsSaveBelongsToWithValidationError() { + $entity = new \Cake\ORM\Entity([ + 'title' => 'A Title', + 'body' => 'A body' + ]); + $entity->author = new \Cake\ORM\Entity([ + 'name' => 'Jose' + ]); + + $table = TableRegistry::get('articles'); + $table->belongsTo('authors'); + $table->association('authors') + ->target() + ->validator() + ->add('name', 'num', ['rule' => 'numeric']); + + $this->assertFalse($table->save($entity)); + $this->assertTrue($entity->isNew()); + $this->assertTrue($entity->author->isNew()); + $this->assertNull($entity->get('author_id')); + $this->assertNotEmpty($entity->author->errors('name')); + } + }