From e609875754103913927f4bab7f67323aa6529165 Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 4 Nov 2009 12:36:17 -0500 Subject: [PATCH] Updating Model::invalidFields, so returning false from beforeValidate() will abort both the validation and saving() of the record. Tests added to check beforeSave, beforeValidate, and beforeDelete return values. Fixes #257 --- cake/libs/model/model.php | 4 +- .../cases/libs/model/model_delete.test.php | 15 ++++++ .../cases/libs/model/model_write.test.php | 34 ++++++++++++ cake/tests/cases/libs/model/models.php | 53 ++++++++++++++++++- 4 files changed, 103 insertions(+), 3 deletions(-) diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index abf93d39c..84b096fbe 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -2359,7 +2359,7 @@ class Model extends Overloadable { ) || $this->beforeValidate($options) === false ) { - return $this->validationErrors; + return false; } if (!isset($this->validate) || empty($this->validate)) { @@ -2793,7 +2793,7 @@ class Model extends Overloadable { function afterDelete() { } /** - * Called during save operations, before validation. Please note that custom + * Called during validation operations, before validation. Please note that custom * validation rules can be defined in $validate. * * @return boolean True if validate operation should continue, false to abort diff --git a/cake/tests/cases/libs/model/model_delete.test.php b/cake/tests/cases/libs/model/model_delete.test.php index 6d30d2bdf..2be205e92 100644 --- a/cake/tests/cases/libs/model/model_delete.test.php +++ b/cake/tests/cases/libs/model/model_delete.test.php @@ -565,7 +565,22 @@ class ModelDeleteTest extends BaseModelTest { )); $this->assertEqual($result['Monkey'], $expected); } +/** + * test that beforeDelete returning false can abort deletion. + * + * @return void + **/ + function testBeforeDeleteDeleteAbortion() { + $this->loadFixtures('Post'); + $Model =& new CallbackPostTestModel(); + $Model->beforeDeleteReturn = false; + $result = $Model->delete(1); + $this->assertFalse($result); + + $exists = $Model->findById(1); + $this->assertTrue(is_array($exists)); + } } ?> \ No newline at end of file diff --git a/cake/tests/cases/libs/model/model_write.test.php b/cake/tests/cases/libs/model/model_write.test.php index 3d023ce11..4d969cca9 100644 --- a/cake/tests/cases/libs/model/model_write.test.php +++ b/cake/tests/cases/libs/model/model_write.test.php @@ -601,6 +601,40 @@ class ModelWriteTest extends BaseModelTest { $result = $TestModel->validates(); $this->assertTrue($result); } +/** + * test that beforeValidate returning false can abort saves. + * + * @return void + **/ + function testBeforeValidateSaveAbortion() { + $Model =& new CallbackPostTestModel(); + $Model->beforeValidateReturn = false; + + $data = array( + 'title' => 'new article', + 'body' => 'this is some text.' + ); + $Model->create(); + $result = $Model->save($data); + $this->assertFalse($result); + } +/** + * test that beforeSave returning false can abort saves. + * + * @return void + **/ + function testBeforeSaveSaveAbortion() { + $Model =& new CallbackPostTestModel(); + $Model->beforeSaveReturn = false; + + $data = array( + 'title' => 'new article', + 'body' => 'this is some text.' + ); + $Model->create(); + $result = $Model->save($data); + $this->assertFalse($result); + } /** * testValidates method * diff --git a/cake/tests/cases/libs/model/models.php b/cake/tests/cases/libs/model/models.php index 64541c4fd..f601b82aa 100644 --- a/cake/tests/cases/libs/model/models.php +++ b/cake/tests/cases/libs/model/models.php @@ -1749,7 +1749,58 @@ class AssociationTest2 extends CakeTestModel { * @subpackage cake.tests.cases.libs.model */ class Callback extends CakeTestModel { - // + +} +/** + * CallbackPostTestModel class + * + * @package cake + * @subpackage cake.tests.cases.libs.model + */ +class CallbackPostTestModel extends CakeTestModel { + var $useTable = 'posts'; +/** + * variable to control return of beforeValidate + * + * @var string + */ + var $beforeValidateReturn = true; +/** + * variable to control return of beforeSave + * + * @var string + */ + var $beforeSaveReturn = true; +/** + * variable to control return of beforeDelete + * + * @var string + */ + var $beforeDeleteReturn = true; +/** + * beforeSave callback + * + * @return void + **/ + function beforeSave($options) { + return $this->beforeSaveReturn; + } +/** + * beforeValidate callback + * + * @return void + **/ + function beforeValidate($options) { + return $this->beforeValidateReturn; + } +/** + * beforeDelete callback + * + * @return void + **/ + function beforeDelete($cascade = true) { + return $this->beforeDeleteReturn; + } } /** * Uuid class