|
|
|
@ -7,8 +7,8 @@ use Yii;
|
|
|
|
|
use yii\base\Exception; |
|
|
|
|
use yii\validators\ExistValidator; |
|
|
|
|
use yiiunit\data\ar\ActiveRecord; |
|
|
|
|
use yiiunit\data\validators\models\ExistValidatorMainModel; |
|
|
|
|
use yiiunit\data\validators\models\ExistValidatorRefModel; |
|
|
|
|
use yiiunit\data\validators\models\ValidatorTestMainModel; |
|
|
|
|
use yiiunit\data\validators\models\ValidatorTestRefModel; |
|
|
|
|
use yiiunit\framework\db\DatabaseTestCase; |
|
|
|
|
|
|
|
|
|
class ExistValidatorTest extends DatabaseTestCase |
|
|
|
@ -39,7 +39,7 @@ class ExistValidatorTest extends DatabaseTestCase
|
|
|
|
|
} |
|
|
|
|
// combine to save the time creating a new db-fixture set (likely ~5 sec) |
|
|
|
|
try { |
|
|
|
|
$val = new ExistValidator(array('className' => ExistValidatorMainModel::className())); |
|
|
|
|
$val = new ExistValidator(array('className' => ValidatorTestMainModel::className())); |
|
|
|
|
$val->validateValue('ref'); |
|
|
|
|
$this->fail('Exception should have been thrown at this time'); |
|
|
|
|
} catch (Exception $e) { |
|
|
|
@ -50,7 +50,7 @@ class ExistValidatorTest extends DatabaseTestCase
|
|
|
|
|
|
|
|
|
|
public function testValidateValue() |
|
|
|
|
{ |
|
|
|
|
$val = new ExistValidator(array('className' => ExistValidatorRefModel::className(), 'attributeName' => 'id')); |
|
|
|
|
$val = new ExistValidator(array('className' => ValidatorTestRefModel::className(), 'attributeName' => 'id')); |
|
|
|
|
$this->assertTrue($val->validateValue(2)); |
|
|
|
|
$this->assertTrue($val->validateValue(5)); |
|
|
|
|
$this->assertFalse($val->validateValue(99)); |
|
|
|
@ -60,39 +60,39 @@ class ExistValidatorTest extends DatabaseTestCase
|
|
|
|
|
public function testValidateAttribute() |
|
|
|
|
{ |
|
|
|
|
// existing value on different table |
|
|
|
|
$val = new ExistValidator(array('className' => ExistValidatorMainModel::className(), 'attributeName' => 'id')); |
|
|
|
|
$m = ExistValidatorRefModel::find(array('id' => 1)); |
|
|
|
|
$val = new ExistValidator(array('className' => ValidatorTestMainModel::className(), 'attributeName' => 'id')); |
|
|
|
|
$m = ValidatorTestRefModel::find(array('id' => 1)); |
|
|
|
|
$val->validateAttribute($m, 'ref'); |
|
|
|
|
$this->assertFalse($m->hasErrors()); |
|
|
|
|
// non-existing value on different table |
|
|
|
|
$val = new ExistValidator(array('className' => ExistValidatorMainModel::className(), 'attributeName' => 'id')); |
|
|
|
|
$m = ExistValidatorRefModel::find(array('id' => 6)); |
|
|
|
|
$val = new ExistValidator(array('className' => ValidatorTestMainModel::className(), 'attributeName' => 'id')); |
|
|
|
|
$m = ValidatorTestRefModel::find(array('id' => 6)); |
|
|
|
|
$val->validateAttribute($m, 'ref'); |
|
|
|
|
$this->assertTrue($m->hasErrors('ref')); |
|
|
|
|
// existing value on same table |
|
|
|
|
$val = new ExistValidator(array('attributeName' => 'ref')); |
|
|
|
|
$m = ExistValidatorRefModel::find(array('id' => 2)); |
|
|
|
|
$m = ValidatorTestRefModel::find(array('id' => 2)); |
|
|
|
|
$val->validateAttribute($m, 'test_val'); |
|
|
|
|
$this->assertFalse($m->hasErrors()); |
|
|
|
|
// non-existing value on same table |
|
|
|
|
$val = new ExistValidator(array('attributeName' => 'ref')); |
|
|
|
|
$m = ExistValidatorRefModel::find(array('id' => 5)); |
|
|
|
|
$m = ValidatorTestRefModel::find(array('id' => 5)); |
|
|
|
|
$val->validateAttribute($m, 'test_val_fail'); |
|
|
|
|
$this->assertTrue($m->hasErrors('test_val_fail')); |
|
|
|
|
// check for given value (true) |
|
|
|
|
$val = new ExistValidator(); |
|
|
|
|
$m = ExistValidatorRefModel::find(array('id' => 3)); |
|
|
|
|
$m = ValidatorTestRefModel::find(array('id' => 3)); |
|
|
|
|
$val->validateAttribute($m, 'ref'); |
|
|
|
|
$this->assertFalse($m->hasErrors()); |
|
|
|
|
// check for given defaults (false) |
|
|
|
|
$val = new ExistValidator(); |
|
|
|
|
$m = ExistValidatorRefModel::find(array('id' => 4)); |
|
|
|
|
$m = ValidatorTestRefModel::find(array('id' => 4)); |
|
|
|
|
$m->a_field = 'some new value'; |
|
|
|
|
$val->validateAttribute($m, 'a_field'); |
|
|
|
|
$this->assertTrue($m->hasErrors('a_field')); |
|
|
|
|
// check array |
|
|
|
|
$val = new ExistValidator(array('attributeName' => 'ref')); |
|
|
|
|
$m = ExistValidatorRefModel::find(array('id' => 2)); |
|
|
|
|
$m = ValidatorTestRefModel::find(array('id' => 2)); |
|
|
|
|
$m->test_val = array(1,2,3); |
|
|
|
|
$val->validateAttribute($m, 'test_val'); |
|
|
|
|
$this->assertTrue($m->hasErrors('test_val')); |
|
|
|
|