From 0f1de003c6c328ed530eab9606f7c5019c26a539 Mon Sep 17 00:00:00 2001 From: phpnut Date: Fri, 21 Sep 2007 00:42:34 +0000 Subject: [PATCH] Fixes #3215, UserDefined Validation broken. Added test for Validation::userDefined() git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5679 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/validation.php | 13 +++++++------ cake/tests/cases/libs/model/model.test.php | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/cake/libs/validation.php b/cake/libs/validation.php index afaea4e12..b0a3da3d3 100644 --- a/cake/libs/validation.php +++ b/cake/libs/validation.php @@ -708,14 +708,15 @@ class Validation extends Object { /** * Runs an user-defined validation. * - * @param object $object Object that holds validation method - * @param string $method Method name for validation to run - * @param array $args Arguments to send to method - * @return mixed Whatever method returns + * @param mixed $check value that will be validated in user-defined methods. + * @param object $object class that holds validation method + * @param string $method class method name for validation to run + * @param array $args arguments to send to method + * @return mixed user-defined class class method returns * @access public */ - function userDefined($object, $method, $args) { - return call_user_func_array(array(&$object, $method), $args); + function userDefined($check, $object, $method, $args = null) { + return call_user_func_array(array(&$object, $method), array($check,$args)); } /** diff --git a/cake/tests/cases/libs/model/model.test.php b/cake/tests/cases/libs/model/model.test.php index 5e9073191..889a7779e 100644 --- a/cake/tests/cases/libs/model/model.test.php +++ b/cake/tests/cases/libs/model/model.test.php @@ -141,6 +141,13 @@ class Article extends CakeTestModel { 'title' => array('allowEmpty' => false, 'rule' => VALID_NOT_EMPTY), 'body' => VALID_NOT_EMPTY ); + + function titleDuplicate ($title) { + if($title === 'My Article Title') { + return false; + } + return true; + } } /** * Short description for class. @@ -1766,6 +1773,20 @@ class ModelTest extends CakeTestCase { $this->assertTrue($result); $result = $this->model->validates(); $this->assertTrue($result); + + $this->model->validate = array('title' => array('rule' => array('userDefined', 'Article', 'titleDuplicate'))); + + $data = array('TestValidate' => array('title' => 'My Article Title')); + $result = $this->model->create($data); + $this->assertTrue($result); + $result = $this->model->validates(); + $this->assertFalse($result); + + $data = array('TestValidate' => array('title' => 'My Article With a Different Title')); + $result = $this->model->create($data); + $this->assertTrue($result); + $result = $this->model->validates(); + $this->assertTrue($result); } function testSaveField() {