From 394adc9c9245e46dcb933c8583329008e786caa2 Mon Sep 17 00:00:00 2001 From: October CMS Date: Mon, 6 Nov 2023 11:43:00 +1100 Subject: [PATCH] Fixes connection name using unique validation rule --- src/Database/Traits/Validation.php | 29 ++++++++-------- tests/Database/Traits/ValidationTest.php | 42 ++++++++++++++---------- 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/src/Database/Traits/Validation.php b/src/Database/Traits/Validation.php index 46f6abed5..6ffe7791d 100644 --- a/src/Database/Traits/Validation.php +++ b/src/Database/Traits/Validation.php @@ -491,14 +491,15 @@ protected function processRuleFieldNames($rules) */ protected function processValidationUniqueRule($definition, $fieldName) { - [$table, $column, $key, $keyName, $whereColumn, $whereValue] = array_pad(explode(',', $definition, 6), 6, null); + [$ruleName, $ruleDefinition] = array_pad(explode(':', $definition, 2), 2, ''); + [$tableName, $column, $key, $keyName, $whereColumn, $whereValue] = array_pad(explode(',', $ruleDefinition, 6), 6, null); - $table = 'unique:' . $this->getTable(); + $tableName = str_contains($tableName, '.') ? $tableName : $this->getTable(); $column = $column ?: $fieldName; $key = $keyName ? $this->$keyName : $this->getKey(); $keyName = $keyName ?: $this->getKeyName(); - $params = [$table, $column, $key, $keyName]; + $params = [$tableName, $column, $key, $keyName]; if ($whereColumn) { $params[] = $whereColumn; @@ -508,7 +509,7 @@ protected function processValidationUniqueRule($definition, $fieldName) $params[] = $whereValue; } - return implode(',', $params); + return $ruleName . ':' . implode(',', $params); } /** @@ -526,26 +527,26 @@ public function isAttributeRequired($attribute, $checkDependencies = true) return false; } - $ruleset = $this->rules[$attribute]; + $ruleSet = $this->rules[$attribute]; - if (is_array($ruleset)) { - $ruleset = implode('|', $ruleset); + if (is_array($ruleSet)) { + $ruleSet = implode('|', $ruleSet); } - if (strpos($ruleset, 'required:create') !== false && $this->exists) { + if (strpos($ruleSet, 'required:create') !== false && $this->exists) { return false; } - if (strpos($ruleset, 'required:update') !== false && !$this->exists) { + if (strpos($ruleSet, 'required:update') !== false && !$this->exists) { return false; } - if (strpos($ruleset, 'required_with') !== false) { + if (strpos($ruleSet, 'required_with') !== false) { if (!$checkDependencies) { return true; } - $requiredWith = substr($ruleset, strpos($ruleset, 'required_with') + 14); + $requiredWith = substr($ruleSet, strpos($ruleSet, 'required_with') + 14); if (strpos($requiredWith, '|') !== false) { $requiredWith = substr($requiredWith, 0, strpos($requiredWith, '|')); @@ -554,18 +555,18 @@ public function isAttributeRequired($attribute, $checkDependencies = true) return $this->isAttributeRequired($requiredWith, false); } - if (strpos($ruleset, 'required_if') !== false) { + if (strpos($ruleSet, 'required_if') !== false) { if (!$checkDependencies) { return true; } - $requiredIf = substr($ruleset, strpos($ruleset, 'required_if') + 12); + $requiredIf = substr($ruleSet, strpos($ruleSet, 'required_if') + 12); $requiredIf = substr($requiredIf, 0, strpos($requiredIf, ',')); return $this->isAttributeRequired($requiredIf, false); } - return strpos($ruleset, 'required') !== false; + return strpos($ruleSet, 'required') !== false; } /** diff --git a/tests/Database/Traits/ValidationTest.php b/tests/Database/Traits/ValidationTest.php index e6e4c5548..7fb851c0a 100644 --- a/tests/Database/Traits/ValidationTest.php +++ b/tests/Database/Traits/ValidationTest.php @@ -8,11 +8,9 @@ class ValidationTest extends TestCase public $id = 20; - public function testUnique() + public function testUniqueRule() { - /* - * Basic usage of unique rule - */ + // Basic usage of unique rule $rules = ['email' => 'unique:users']; $this->exists = true; @@ -25,9 +23,20 @@ public function testUnique() 'email' => ['unique:users'] ], $this->processValidationRules($rules)); - /* - * Specifying a custom column name - */ + // Custom database connection + $rules = ['email' => 'unique:myconnection.users']; + + $this->exists = true; + $this->assertEquals([ + 'email' => ['unique:myconnection.users,email,7,the_id'] + ], $this->processValidationRules($rules)); + + $this->exists = false; + $this->assertEquals([ + 'email' => ['unique:myconnection.users'] + ], $this->processValidationRules($rules)); + + // Custom table column name $rules = ['email' => 'unique:users,email_address']; $this->exists = true; @@ -40,9 +49,7 @@ public function testUnique() 'email' => ['unique:users,email_address'] ], $this->processValidationRules($rules)); - /* - * Forcing a unique rule to ignore a given ID - */ + // Forcing a unique rule to ignore a given ID $rules = ['email' => 'unique:users,email_address,10']; $this->exists = true; @@ -55,9 +62,7 @@ public function testUnique() 'email' => ['unique:users,email_address,10'] ], $this->processValidationRules($rules)); - /* - * Adding additional where clauses - */ + // Adding additional where clauses $rules = ['email' => 'unique:users,email_address,NULL,id,account_id,1']; $this->exists = true; @@ -70,9 +75,7 @@ public function testUnique() 'email' => ['unique:users,email_address,NULL,id,account_id,1'] ], $this->processValidationRules($rules)); - /* - * Adding multiple additional where clauses - */ + // Adding multiple additional where clauses $rules = ['email' => 'unique:users,email_address,NULL,id,account_id,1,account_name,"Foo",user_id,3']; $this->exists = true; @@ -91,6 +94,11 @@ protected function getTable() return 'users'; } + protected function getConnectionName() + { + return 'mysql'; + } + protected function getKey() { return 7; @@ -103,7 +111,7 @@ protected function getKeyName() public function testArrayFieldNames() { - $mock = $this->getMockForTrait('October\Rain\Database\Traits\Validation'); + $mock = $this->getMockForTrait(\October\Rain\Database\Traits\Validation::class); $rules = [ 'field' => 'required',