mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-01 20:19:42 +08:00
Fixes #14254: add an option to specify whether validator is forced to always use master DB for yii\validators\UniqueValidator and yii\validators\ExistValidator
This commit is contained in:
committed by
Alexander Makarov
parent
41cf14e515
commit
63ffae028e
@ -7,6 +7,7 @@
|
||||
|
||||
namespace yiiunit\framework\db;
|
||||
|
||||
use yii\caching\DummyCache;
|
||||
use yii\db\Connection;
|
||||
use yiiunit\TestCase as TestCase;
|
||||
|
||||
@ -129,4 +130,26 @@ abstract class DatabaseTestCase extends TestCase
|
||||
return $sql;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\Connection
|
||||
*/
|
||||
protected function getConnectionWithInvalidSlave()
|
||||
{
|
||||
$config = array_merge($this->database, [
|
||||
'serverStatusCache' => new DummyCache(),
|
||||
'slaves' => [
|
||||
[], // invalid config
|
||||
],
|
||||
]);
|
||||
|
||||
if (isset($config['fixture'])) {
|
||||
$fixture = $config['fixture'];
|
||||
unset($config['fixture']);
|
||||
} else {
|
||||
$fixture = null;
|
||||
}
|
||||
|
||||
return $this->prepareDatabase($config, $fixture, true);
|
||||
}
|
||||
}
|
||||
|
||||
@ -218,4 +218,30 @@ abstract class ExistValidatorTest extends DatabaseTestCase
|
||||
$val->validateAttribute($m, 'id');
|
||||
$this->assertTrue($m->hasErrors('id'));
|
||||
}
|
||||
|
||||
public function testForceMaster()
|
||||
{
|
||||
$connection = $this->getConnectionWithInvalidSlave();
|
||||
ActiveRecord::$db = $connection;
|
||||
|
||||
$model = null;
|
||||
$connection->useMaster(function() use (&$model) {
|
||||
$model = ValidatorTestMainModel::findOne(2);
|
||||
});
|
||||
|
||||
$validator = new ExistValidator([
|
||||
'forceMasterDb' => true,
|
||||
'targetRelation' => 'references',
|
||||
]);
|
||||
$validator->validateAttribute($model, 'id');
|
||||
|
||||
$this->expectException('\yii\base\InvalidConfigException');
|
||||
$validator = new ExistValidator([
|
||||
'forceMasterDb' => false,
|
||||
'targetRelation' => 'references',
|
||||
]);
|
||||
$validator->validateAttribute($model, 'id');
|
||||
|
||||
ActiveRecord::$db = $this->getConnection();
|
||||
}
|
||||
}
|
||||
|
||||
@ -453,6 +453,32 @@ abstract class UniqueValidatorTest extends DatabaseTestCase
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function testForceMaster()
|
||||
{
|
||||
$connection = $this->getConnectionWithInvalidSlave();
|
||||
ActiveRecord::$db = $connection;
|
||||
|
||||
$model = null;
|
||||
$connection->useMaster(function() use (&$model) {
|
||||
$model = WithCustomer::find()->one();
|
||||
});
|
||||
|
||||
$validator = new UniqueValidator([
|
||||
'forceMasterDb' => true,
|
||||
'targetAttribute' => ['status', 'profile_id']
|
||||
]);
|
||||
$validator->validateAttribute($model, 'email');
|
||||
|
||||
$this->expectException('\yii\base\InvalidConfigException');
|
||||
$validator = new UniqueValidator([
|
||||
'forceMasterDb' => false,
|
||||
'targetAttribute' => ['status', 'profile_id']
|
||||
]);
|
||||
$validator->validateAttribute($model, 'email');
|
||||
|
||||
ActiveRecord::$db = $this->getConnection();
|
||||
}
|
||||
}
|
||||
|
||||
class WithCustomer extends Customer {
|
||||
|
||||
Reference in New Issue
Block a user