ValidatorTest.php 8.55 KB
Newer Older
Suralc committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php

namespace yiiunit\framework\validators;


use yii\validators\BooleanValidator;
use yii\validators\InlineValidator;
use yii\validators\NumberValidator;
use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\data\validators\TestValidator;
use yiiunit\TestCase;

class ValidatorTest extends TestCase
{
15 16 17 18 19
	protected function setUp()
	{
		parent::setUp();
		$this->mockApplication();
	}
Suralc committed
20

Alexander Makarov committed
21
	protected function getTestModel($additionalAttributes = [])
Suralc committed
22 23
	{
		$attributes = array_merge(
Alexander Makarov committed
24
			['attr_runMe1' => true, 'attr_runMe2' => true, 'attr_skip' => true],
Suralc committed
25 26 27 28 29 30 31
			$additionalAttributes
		);
		return FakedValidationModel::createWithAttributes($attributes);
	}

	public function testCreateValidator()
	{
Alexander Makarov committed
32
		$model = FakedValidationModel::createWithAttributes(['attr_test1' => 'abc', 'attr_test2' => '2013']);
Suralc committed
33
		/** @var $numberVal NumberValidator */
Alexander Makarov committed
34
		$numberVal = TestValidator::createValidator('number', $model, ['attr_test1']);
Suralc committed
35
		$this->assertInstanceOf(NumberValidator::className(), $numberVal);
Alexander Makarov committed
36
		$numberVal = TestValidator::createValidator('integer', $model, ['attr_test2']);
Suralc committed
37 38 39 40 41 42
		$this->assertInstanceOf(NumberValidator::className(), $numberVal);
		$this->assertTrue($numberVal->integerOnly);
		$val = TestValidator::createValidator(
			'boolean',
			$model,
			'attr_test1, attr_test2',
Alexander Makarov committed
43
			['on' => ['a', 'b']]
Suralc committed
44 45
		);
		$this->assertInstanceOf(BooleanValidator::className(), $val);
Alexander Makarov committed
46 47
		$this->assertSame(['a', 'b'], $val->on);
		$this->assertSame(['attr_test1', 'attr_test2'], $val->attributes);
Suralc committed
48 49 50 51
		$val = TestValidator::createValidator(
			'boolean',
			$model,
			'attr_test1, attr_test2',
Alexander Makarov committed
52
			['on' => 'a, b', 'except' => 'c,d,e']
Suralc committed
53 54
		);
		$this->assertInstanceOf(BooleanValidator::className(), $val);
Alexander Makarov committed
55 56
		$this->assertSame(['a', 'b'], $val->on);
		$this->assertSame(['c', 'd', 'e'], $val->except);
Suralc committed
57 58 59 60 61 62 63
		$val = TestValidator::createValidator('inlineVal', $model, 'val_attr_a');
		$this->assertInstanceOf(InlineValidator::className(), $val);
		$this->assertSame('inlineVal', $val->method);
	}

	public function testValidate()
	{
Alexander Makarov committed
64
		$val = new TestValidator(['attributes' => ['attr_runMe1', 'attr_runMe2']]);
Suralc committed
65 66 67 68 69 70 71 72 73
		$model = $this->getTestModel();
		$val->validate($model);
		$this->assertTrue($val->isAttributeValidated('attr_runMe1'));
		$this->assertTrue($val->isAttributeValidated('attr_runMe2'));
		$this->assertFalse($val->isAttributeValidated('attr_skip'));
	}

	public function testValidateWithAttributeIntersect()
	{
Alexander Makarov committed
74
		$val = new TestValidator(['attributes' => ['attr_runMe1', 'attr_runMe2']]);
Suralc committed
75
		$model = $this->getTestModel();
Alexander Makarov committed
76
		$val->validate($model, ['attr_runMe1']);
Suralc committed
77 78 79 80 81 82 83 84 85
		$this->assertTrue($val->isAttributeValidated('attr_runMe1'));
		$this->assertFalse($val->isAttributeValidated('attr_runMe2'));
		$this->assertFalse($val->isAttributeValidated('attr_skip'));
	}

	public function testValidateWithEmptyAttributes()
	{
		$val = new TestValidator();
		$model = $this->getTestModel();
Alexander Makarov committed
86
		$val->validate($model, ['attr_runMe1']);
Suralc committed
87 88 89 90 91 92 93 94 95 96 97
		$this->assertFalse($val->isAttributeValidated('attr_runMe1'));
		$this->assertFalse($val->isAttributeValidated('attr_runMe2'));
		$this->assertFalse($val->isAttributeValidated('attr_skip'));
		$val->validate($model);
		$this->assertFalse($val->isAttributeValidated('attr_runMe1'));
		$this->assertFalse($val->isAttributeValidated('attr_runMe2'));
		$this->assertFalse($val->isAttributeValidated('attr_skip'));
	}

	public function testValidateWithError()
	{
Alexander Makarov committed
98
		$val = new TestValidator(['attributes' => ['attr_runMe1', 'attr_runMe2'], 'skipOnError' => false]);
Suralc committed
99 100 101 102 103 104 105
		$model = $this->getTestModel();
		$val->validate($model);
		$this->assertTrue($val->isAttributeValidated('attr_runMe1'));
		$this->assertTrue($val->isAttributeValidated('attr_runMe2'));
		$this->assertFalse($val->isAttributeValidated('attr_skip'));
		$this->assertEquals(1, $val->countAttributeValidations('attr_runMe2'));
		$this->assertEquals(1, $val->countAttributeValidations('attr_runMe1'));
Alexander Makarov committed
106
		$val->validate($model, ['attr_runMe2']);
Suralc committed
107 108 109
		$this->assertEquals(2, $val->countAttributeValidations('attr_runMe2'));
		$this->assertEquals(1, $val->countAttributeValidations('attr_runMe1'));
		$this->assertEquals(0, $val->countAttributeValidations('attr_skip'));
Alexander Makarov committed
110
		$val = new TestValidator(['attributes' => ['attr_runMe1', 'attr_runMe2'], 'skipOnError' => true]);
Suralc committed
111 112 113 114 115 116 117 118 119
		$model = $this->getTestModel();
		$val->enableErrorOnValidateAttribute();
		$val->validate($model);
		$this->assertTrue($val->isAttributeValidated('attr_runMe1'));
		$this->assertTrue($val->isAttributeValidated('attr_runMe2'));
		$this->assertFalse($val->isAttributeValidated('attr_skip'));
		$this->assertEquals(1, $val->countAttributeValidations('attr_runMe1'));
		$this->assertEquals(1, $val->countAttributeValidations('attr_runMe1'));
		$this->assertEquals(0, $val->countAttributeValidations('attr_skip'));
Alexander Makarov committed
120
		$val->validate($model, ['attr_runMe2']);
Suralc committed
121 122 123 124 125 126 127
		$this->assertEquals(1, $val->countAttributeValidations('attr_runMe2'));
		$this->assertEquals(1, $val->countAttributeValidations('attr_runMe1'));
		$this->assertEquals(0, $val->countAttributeValidations('attr_skip'));
	}

	public function testValidateWithEmpty()
	{
Alexander Makarov committed
128 129
		$val = new TestValidator([
			'attributes' => [
Suralc committed
130 131 132 133
				'attr_runMe1',
				'attr_runMe2',
				'attr_empty1',
				'attr_empty2'
Alexander Makarov committed
134
			],
Suralc committed
135
			'skipOnEmpty' => true,
Alexander Makarov committed
136 137
		]);
		$model = $this->getTestModel(['attr_empty1' => '', 'attr_emtpy2' => ' ']);
Suralc committed
138 139 140 141 142 143 144 145 146
		$val->validate($model);
		$this->assertTrue($val->isAttributeValidated('attr_runMe1'));
		$this->assertTrue($val->isAttributeValidated('attr_runMe2'));
		$this->assertFalse($val->isAttributeValidated('attr_empty1'));
		$this->assertFalse($val->isAttributeValidated('attr_empty2'));
		$model->attr_empty1 = 'not empty anymore';
		$val->validate($model);
		$this->assertTrue($val->isAttributeValidated('attr_empty1'));
		$this->assertFalse($val->isAttributeValidated('attr_empty2'));
Alexander Makarov committed
147 148
		$val = new TestValidator([
			'attributes' => [
Suralc committed
149 150 151 152
				'attr_runMe1',
				'attr_runMe2',
				'attr_empty1',
				'attr_empty2'
Alexander Makarov committed
153
			],
Suralc committed
154
			'skipOnEmpty' => false,
Alexander Makarov committed
155 156
		]);
		$model = $this->getTestModel(['attr_empty1' => '', 'attr_emtpy2' => ' ']);
Suralc committed
157 158 159 160 161 162 163 164 165 166 167
		$val->validate($model);
		$this->assertTrue($val->isAttributeValidated('attr_runMe1'));
		$this->assertTrue($val->isAttributeValidated('attr_runMe2'));
		$this->assertTrue($val->isAttributeValidated('attr_empty1'));
		$this->assertTrue($val->isAttributeValidated('attr_empty2'));
	}

	public function testIsEmpty()
	{
		$val = new TestValidator();
		$this->assertTrue($val->isEmpty(null));
Alexander Makarov committed
168
		$this->assertTrue($val->isEmpty([]));
Suralc committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
		$this->assertTrue($val->isEmpty(''));
		$this->assertFalse($val->isEmpty(5));
		$this->assertFalse($val->isEmpty(0));
		$this->assertFalse($val->isEmpty(new \stdClass()));
		$this->assertFalse($val->isEmpty('  '));
		// trim
		$this->assertTrue($val->isEmpty('   ', true));
		$this->assertTrue($val->isEmpty('', true));
		$this->assertTrue($val->isEmpty(" \t\n\r\0\x0B", true));
		$this->assertTrue($val->isEmpty('', true));
		$this->assertFalse($val->isEmpty('0', true));
		$this->assertFalse($val->isEmpty(0, true));
		$this->assertFalse($val->isEmpty('this ain\'t an empty value', true));
	}

	public function testValidateValue()
	{
		$this->setExpectedException(
			'yii\base\NotSupportedException',
			TestValidator::className() . ' does not support validateValue().'
		);
		$val = new TestValidator();
		$val->validateValue('abc');
	}

	public function testClientValidateAttribute()
	{
		$val = new TestValidator();
		$this->assertNull(
Alexander Makarov committed
198
			$val->clientValidateAttribute($this->getTestModel(), 'attr_runMe1', [])
Suralc committed
199 200 201 202 203 204 205 206
		); //todo pass a view instead of array
	}

	public function testIsActive()
	{
		$val = new TestValidator();
		$this->assertTrue($val->isActive('scenA'));
		$this->assertTrue($val->isActive('scenB'));
Alexander Makarov committed
207
		$val->except = ['scenB'];
Suralc committed
208 209
		$this->assertTrue($val->isActive('scenA'));
		$this->assertFalse($val->isActive('scenB'));
Alexander Makarov committed
210
		$val->on = ['scenC'];
Suralc committed
211 212 213 214 215 216 217 218
		$this->assertFalse($val->isActive('scenA'));
		$this->assertFalse($val->isActive('scenB'));
		$this->assertTrue($val->isActive('scenC'));
	}

	public function testAddError()
	{
		$val = new TestValidator();
Alexander Makarov committed
219
		$m = $this->getTestModel(['attr_msg_val' => 'abc']);
Suralc committed
220 221 222
		$val->addError($m, 'attr_msg_val', '{attribute}::{value}');
		$errors = $m->getErrors('attr_msg_val');
		$this->assertEquals('attr_msg_val::abc', $errors[0]);
Alexander Makarov committed
223
		$m = $this->getTestModel(['attr_msg_val' => ['bcc']]);
Suralc committed
224 225
		$val->addError($m, 'attr_msg_val', '{attribute}::{value}');
		$errors = $m->getErrors('attr_msg_val');
Alexander Makarov committed
226
		$this->assertEquals('attr_msg_val::array()', $errors[0]);
Alexander Makarov committed
227
		$m = $this->getTestModel(['attr_msg_val' => 'abc']);
228
		$val->addError($m, 'attr_msg_val', '{attribute}::{value}::{param}', ['param' => 'param_value']);
Suralc committed
229 230 231 232
		$errors = $m->getErrors('attr_msg_val');
		$this->assertEquals('attr_msg_val::abc::param_value', $errors[0]);
	}
}