From dc7b8cbb89c92dc46777abcad90fdfb50e704515 Mon Sep 17 00:00:00 2001 From: Rachman Chavik Date: Mon, 13 Oct 2014 19:40:30 +0700 Subject: [PATCH 1/2] Add BC test for #4851 --- .../Test/Case/Model/ModelValidationTest.php | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/Cake/Test/Case/Model/ModelValidationTest.php b/lib/Cake/Test/Case/Model/ModelValidationTest.php index c5f3b22a3..270d61119 100644 --- a/lib/Cake/Test/Case/Model/ModelValidationTest.php +++ b/lib/Cake/Test/Case/Model/ModelValidationTest.php @@ -2449,6 +2449,33 @@ class ModelValidationTest extends BaseModelTest { $this->assertFalse($Article->validates(), 'Should fail, conditions are combined with or'); } +/** + * Test backward compatibility of the isUnique method when used as a validator for multiple fields. + * + * @return void + */ + public function testBackwardCompatIsUniqueValidator() { + $this->loadFixtures('Article'); + $Article = ClassRegistry::init('Article'); + $Article->validate = array( + 'title' => array( + 'duplicate' => array( + 'rule' => 'isUnique', + 'message' => 'Title must be unique', + ), + 'minLength' => array( + 'rule' => array('minLength', 1), + 'message' => 'Title cannot be empty', + ), + ) + ); + $data = array( + 'title' => 'First Article', + ); + $data = $Article->create($data); + $this->assertFalse($Article->validates(), 'Contains a dupe'); + } + } /** From dcb605c700257d90ebb972c85132adece9271ced Mon Sep 17 00:00:00 2001 From: Rachman Chavik Date: Mon, 13 Oct 2014 19:42:46 +0700 Subject: [PATCH 2/2] Possible fix for isUnique backward compatibility --- lib/Cake/Model/Model.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index c9e7ce7e9..3a908f5df 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -3302,9 +3302,12 @@ class Model extends Object implements CakeEventListener { */ public function isUnique($fields, $or = true) { if (is_array($or)) { - $args = func_get_args(); - $fields = $args[1]; - $or = isset($args[2]) ? $args[2] : true; + $isAssociative = count(array_filter(array_keys($or), 'is_string')); + if (!$isAssociative) { + $args = func_get_args(); + $fields = $args[1]; + $or = isset($args[2]) ? $args[2] : true; + } } if (!is_array($fields)) { $fields = func_get_args();