ValidatorTest.php 8.47 KB
Newer Older
Suralc committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?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
{

Alexander Makarov committed
16
	protected function getTestModel($additionalAttributes = [])
Suralc committed
17 18
	{
		$attributes = array_merge(
Alexander Makarov committed
19
			['attr_runMe1' => true, 'attr_runMe2' => true, 'attr_skip' => true],
Suralc committed
20 21 22 23 24 25 26
			$additionalAttributes
		);
		return FakedValidationModel::createWithAttributes($attributes);
	}

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

	public function testValidate()
	{
Alexander Makarov committed
59
		$val = new TestValidator(['attributes' => ['attr_runMe1', 'attr_runMe2']]);
Suralc committed
60 61 62 63 64 65 66 67 68
		$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
69
		$val = new TestValidator(['attributes' => ['attr_runMe1', 'attr_runMe2']]);
Suralc committed
70
		$model = $this->getTestModel();
Alexander Makarov committed
71
		$val->validate($model, ['attr_runMe1']);
Suralc committed
72 73 74 75 76 77 78 79 80
		$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
81
		$val->validate($model, ['attr_runMe1']);
Suralc committed
82 83 84 85 86 87 88 89 90 91 92
		$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
93
		$val = new TestValidator(['attributes' => ['attr_runMe1', 'attr_runMe2'], 'skipOnError' => false]);
Suralc committed
94 95 96 97 98 99 100
		$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
101
		$val->validate($model, ['attr_runMe2']);
Suralc committed
102 103 104
		$this->assertEquals(2, $val->countAttributeValidations('attr_runMe2'));
		$this->assertEquals(1, $val->countAttributeValidations('attr_runMe1'));
		$this->assertEquals(0, $val->countAttributeValidations('attr_skip'));
Alexander Makarov committed
105
		$val = new TestValidator(['attributes' => ['attr_runMe1', 'attr_runMe2'], 'skipOnError' => true]);
Suralc committed
106 107 108 109 110 111 112 113 114
		$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
115
		$val->validate($model, ['attr_runMe2']);
Suralc committed
116 117 118 119 120 121 122
		$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
123 124
		$val = new TestValidator([
			'attributes' => [
Suralc committed
125 126 127 128
				'attr_runMe1',
				'attr_runMe2',
				'attr_empty1',
				'attr_empty2'
Alexander Makarov committed
129
			],
Suralc committed
130
			'skipOnEmpty' => true,
Alexander Makarov committed
131 132
		]);
		$model = $this->getTestModel(['attr_empty1' => '', 'attr_emtpy2' => ' ']);
Suralc committed
133 134 135 136 137 138 139 140 141
		$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
142 143
		$val = new TestValidator([
			'attributes' => [
Suralc committed
144 145 146 147
				'attr_runMe1',
				'attr_runMe2',
				'attr_empty1',
				'attr_empty2'
Alexander Makarov committed
148
			],
Suralc committed
149
			'skipOnEmpty' => false,
Alexander Makarov committed
150 151
		]);
		$model = $this->getTestModel(['attr_empty1' => '', 'attr_emtpy2' => ' ']);
Suralc committed
152 153 154 155 156 157 158 159 160 161 162
		$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
163
		$this->assertTrue($val->isEmpty([]));
Suralc committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
		$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
193
			$val->clientValidateAttribute($this->getTestModel(), 'attr_runMe1', [])
Suralc committed
194 195 196 197 198 199 200 201
		); //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
202
		$val->except = ['scenB'];
Suralc committed
203 204
		$this->assertTrue($val->isActive('scenA'));
		$this->assertFalse($val->isActive('scenB'));
Alexander Makarov committed
205
		$val->on = ['scenC'];
Suralc committed
206 207 208 209 210 211 212 213
		$this->assertFalse($val->isActive('scenA'));
		$this->assertFalse($val->isActive('scenB'));
		$this->assertTrue($val->isActive('scenC'));
	}

	public function testAddError()
	{
		$val = new TestValidator();
Alexander Makarov committed
214
		$m = $this->getTestModel(['attr_msg_val' => 'abc']);
Suralc committed
215 216 217
		$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
218
		$m = $this->getTestModel(['attr_msg_val' => ['bcc']]);
Suralc committed
219 220
		$val->addError($m, 'attr_msg_val', '{attribute}::{value}');
		$errors = $m->getErrors('attr_msg_val');
Alexander Makarov committed
221
		$this->assertEquals('attr_msg_val::array()', $errors[0]);
Alexander Makarov committed
222
		$m = $this->getTestModel(['attr_msg_val' => 'abc']);
223
		$val->addError($m, 'attr_msg_val', '{attribute}::{value}::{param}', ['param' => 'param_value']);
Suralc committed
224 225 226 227
		$errors = $m->getErrors('attr_msg_val');
		$this->assertEquals('attr_msg_val::abc::param_value', $errors[0]);
	}
}