public function testValidateAttribute() { // error-array-add $val = new DateValidator(); $model = new FakedValidationModel(); $model->attr_date = '2013-09-13'; $val->validateAttribute($model, 'attr_date'); $this->assertFalse($model->hasErrors('attr_date')); $model = new FakedValidationModel(); $model->attr_date = '1375293913'; $val->validateAttribute($model, 'attr_date'); $this->assertTrue($model->hasErrors('attr_date')); //// timestamp attribute $val = new DateValidator(['timestampAttribute' => 'attr_timestamp']); $model = new FakedValidationModel(); $model->attr_date = '2013-09-13'; $model->attr_timestamp = true; $val->validateAttribute($model, 'attr_date'); $this->assertFalse($model->hasErrors('attr_date')); $this->assertFalse($model->hasErrors('attr_timestamp')); $this->assertEquals(DateTime::createFromFormat($val->format, '2013-09-13')->getTimestamp(), $model->attr_timestamp); $val = new DateValidator(); $model = FakedValidationModel::createWithAttributes(['attr_date' => []]); $val->validateAttribute($model, 'attr_date'); $this->assertTrue($model->hasErrors('attr_date')); }
public function testValidateAttribute() { $m = FakedValidationModel::createWithAttributes(['attr_one' => ' to be trimmed ', 'attr_two' => 'set this to null', 'attr_empty1' => '', 'attr_empty2' => null, 'attr_array' => ['Maria', 'Anna', 'Elizabeth'], 'attr_array_skipped' => ['John', 'Bill']]); $val = new FilterValidator(['filter' => 'trim']); $val->validateAttribute($m, 'attr_one'); $this->assertSame('to be trimmed', $m->attr_one); $val->filter = function ($value) { return null; }; $val->validateAttribute($m, 'attr_two'); $this->assertNull($m->attr_two); $val->filter = [$this, 'notToBeNull']; $val->validateAttribute($m, 'attr_empty1'); $this->assertSame($this->notToBeNull(''), $m->attr_empty1); $val->skipOnEmpty = true; $val->validateAttribute($m, 'attr_empty2'); $this->assertNotNull($m->attr_empty2); $val->filter = function ($value) { return implode(',', $value); }; $val->skipOnArray = false; $val->validateAttribute($m, 'attr_array'); $this->assertSame('Maria,Anna,Elizabeth', $m->attr_array); $val->skipOnArray = true; $val->validateAttribute($m, 'attr_array_skipped'); $this->assertSame(['John', 'Bill'], $m->attr_array_skipped); }
public function testValidateAttribute() { $val = new StringValidator(); $model = new FakedValidationModel(); $model->attr_string = 'a tet string'; $val->validateAttribute($model, 'attr_string'); $this->assertFalse($model->hasErrors()); $val = new StringValidator(['length' => 20]); $model = new FakedValidationModel(); $model->attr_string = str_repeat('x', 20); $val->validateAttribute($model, 'attr_string'); $this->assertFalse($model->hasErrors()); $model = new FakedValidationModel(); $model->attr_string = 'abc'; $val->validateAttribute($model, 'attr_string'); $this->assertTrue($model->hasErrors('attr_string')); $val = new StringValidator(['max' => 2]); $model = new FakedValidationModel(); $model->attr_string = 'a'; $val->validateAttribute($model, 'attr_string'); $this->assertFalse($model->hasErrors()); $model = new FakedValidationModel(); $model->attr_string = 'abc'; $val->validateAttribute($model, 'attr_string'); $this->assertTrue($model->hasErrors('attr_string')); $val = new StringValidator(['max' => 1]); $model = FakedValidationModel::createWithAttributes(['attr_str' => ['abc']]); $val->validateAttribute($model, 'attr_str'); $this->assertTrue($model->hasErrors('attr_str')); }
public function testValidateAttributeOfNonARModel() { $val = new UniqueValidator(['targetClass' => ValidatorTestRefModel::className(), 'targetAttribute' => 'ref']); $m = FakedValidationModel::createWithAttributes(['attr_1' => 5, 'attr_2' => 1313]); $val->validateAttribute($m, 'attr_1'); $this->assertTrue($m->hasErrors('attr_1')); $val->validateAttribute($m, 'attr_2'); $this->assertFalse($m->hasErrors('attr_2')); }
public function testValidateAttribute() { $val = new RegularExpressionValidator(['pattern' => '/^[a-zA-Z0-9](\\.)?([^\\/]*)$/m']); $m = FakedValidationModel::createWithAttributes(['attr_reg1' => 'b.4']); $val->validateAttribute($m, 'attr_reg1'); $this->assertFalse($m->hasErrors('attr_reg1')); $m->attr_reg1 = 'b./'; $val->validateAttribute($m, 'attr_reg1'); $this->assertTrue($m->hasErrors('attr_reg1')); }
public function testValidateAttribute() { $val = new RangeValidator(['range' => range(1, 10, 1)]); $m = FakedValidationModel::createWithAttributes(['attr_r1' => 5, 'attr_r2' => 999]); $val->validateAttribute($m, 'attr_r1'); $this->assertFalse($m->hasErrors()); $val->validateAttribute($m, 'attr_r2'); $this->assertTrue($m->hasErrors('attr_r2')); $err = $m->getErrors('attr_r2'); $this->assertTrue(stripos($err[0], 'attr_r2') !== false); }
/** * @depends testValidate */ public function testAllowMessageFromRule() { $model = FakedValidationModel::createWithAttributes(['attr_one' => ['text']]); $validator = new EachValidator(['rule' => ['integer']]); $validator->allowMessageFromRule = true; $validator->validateAttribute($model, 'attr_one'); $this->assertContains('integer', $model->getFirstError('attr_one')); $model->clearErrors(); $validator->allowMessageFromRule = false; $validator->validateAttribute($model, 'attr_one'); $this->assertNotContains('integer', $model->getFirstError('attr_one')); }
public function testValidateAttribute() { // empty req-value $val = new RequiredValidator(); $m = FakedValidationModel::createWithAttributes(['attr_val' => null]); $val->validateAttribute($m, 'attr_val'); $this->assertTrue($m->hasErrors('attr_val')); $this->assertTrue(stripos(current($m->getErrors('attr_val')), 'blank') !== false); $val = new RequiredValidator(['requiredValue' => 55]); $m = FakedValidationModel::createWithAttributes(['attr_val' => 56]); $val->validateAttribute($m, 'attr_val'); $this->assertTrue($m->hasErrors('attr_val')); $this->assertTrue(stripos(current($m->getErrors('attr_val')), 'must be') !== false); $val = new RequiredValidator(['requiredValue' => 55]); $m = FakedValidationModel::createWithAttributes(['attr_val' => 55]); $val->validateAttribute($m, 'attr_val'); $this->assertFalse($m->hasErrors('attr_val')); }
public function testCreateValidator() { $model = FakedValidationModel::createWithAttributes(['attr_test1' => 'abc', 'attr_test2' => '2013']); /* @var $numberVal NumberValidator */ $numberVal = TestValidator::createValidator('number', $model, ['attr_test1']); $this->assertInstanceOf(NumberValidator::className(), $numberVal); $numberVal = TestValidator::createValidator('integer', $model, ['attr_test2']); $this->assertInstanceOf(NumberValidator::className(), $numberVal); $this->assertTrue($numberVal->integerOnly); $val = TestValidator::createValidator('boolean', $model, ['attr_test1', 'attr_test2'], ['on' => ['a', 'b']]); $this->assertInstanceOf(BooleanValidator::className(), $val); $this->assertSame(['a', 'b'], $val->on); $this->assertSame(['attr_test1', 'attr_test2'], $val->attributes); $val = TestValidator::createValidator('boolean', $model, ['attr_test1', 'attr_test2'], ['on' => ['a', 'b'], 'except' => ['c', 'd', 'e']]); $this->assertInstanceOf(BooleanValidator::className(), $val); $this->assertSame(['a', 'b'], $val->on); $this->assertSame(['c', 'd', 'e'], $val->except); $val = TestValidator::createValidator('inlineVal', $model, ['val_attr_a']); $this->assertInstanceOf(InlineValidator::className(), $val); $this->assertSame('inlineVal', $val->method); }
public function testValidateAttribute() { // invalid-array $val = new CompareValidator(); $model = new FakedValidationModel(); $model->attr = ['test_val']; $val->validateAttribute($model, 'attr'); $this->assertTrue($model->hasErrors('attr')); $val = new CompareValidator(['compareValue' => 'test-string']); $model = new FakedValidationModel(); $model->attr_test = 'test-string'; $val->validateAttribute($model, 'attr_test'); $this->assertFalse($model->hasErrors('attr_test')); $val = new CompareValidator(['compareAttribute' => 'attr_test_val']); $model = new FakedValidationModel(); $model->attr_test = 'test-string'; $model->attr_test_val = 'test-string'; $val->validateAttribute($model, 'attr_test'); $this->assertFalse($model->hasErrors('attr_test')); $this->assertFalse($model->hasErrors('attr_test_val')); $val = new CompareValidator(['compareAttribute' => 'attr_test_val']); $model = new FakedValidationModel(); $model->attr_test = 'test-string'; $model->attr_test_val = 'test-string-false'; $val->validateAttribute($model, 'attr_test'); $this->assertTrue($model->hasErrors('attr_test')); $this->assertFalse($model->hasErrors('attr_test_val')); // assume: _repeat $val = new CompareValidator(); $model = new FakedValidationModel(); $model->attr_test = 'test-string'; $model->attr_test_repeat = 'test-string'; $val->validateAttribute($model, 'attr_test'); $this->assertFalse($model->hasErrors('attr_test')); $this->assertFalse($model->hasErrors('attr_test_repeat')); $val = new CompareValidator(); $model = new FakedValidationModel(); $model->attr_test = 'test-string'; $model->attr_test_repeat = 'test-string2'; $val->validateAttribute($model, 'attr_test'); $this->assertTrue($model->hasErrors('attr_test')); $this->assertFalse($model->hasErrors('attr_test_repeat')); // not existing op $val = new CompareValidator(); $val->operator = '<>'; $model = FakedValidationModel::createWithAttributes(['attr_o' => 5, 'attr_o_repeat' => 5]); $val->validateAttribute($model, 'attr_o'); $this->assertTrue($model->hasErrors('attr_o')); }
protected function createModelForAttributeTest() { return FakedValidationModel::createWithAttributes(['attr_files' => $this->createTestFiles([['name' => 'abc.jpg', 'size' => 1024, 'type' => 'image/jpeg']]), 'attr_files_empty' => $this->createTestFiles([[]]), 'attr_err_ini' => $this->createTestFiles([['error' => UPLOAD_ERR_INI_SIZE]]), 'attr_err_part' => $this->createTestFiles([['error' => UPLOAD_ERR_PARTIAL]]), 'attr_err_tmp' => $this->createTestFiles([['error' => UPLOAD_ERR_NO_TMP_DIR]]), 'attr_err_write' => $this->createTestFiles([['error' => UPLOAD_ERR_CANT_WRITE]]), 'attr_err_ext' => $this->createTestFiles([['error' => UPLOAD_ERR_EXTENSION]])]); }
public function testValidateAttribute() { $val = new NumberValidator(); $model = new FakedValidationModel(); $model->attr_number = '5.5e1'; $val->validateAttribute($model, 'attr_number'); $this->assertFalse($model->hasErrors('attr_number')); $model->attr_number = '43^32'; //expression $val->validateAttribute($model, 'attr_number'); $this->assertTrue($model->hasErrors('attr_number')); $val = new NumberValidator(['min' => 10]); $model = new FakedValidationModel(); $model->attr_number = 10; $val->validateAttribute($model, 'attr_number'); $this->assertFalse($model->hasErrors('attr_number')); $model->attr_number = 5; $val->validateAttribute($model, 'attr_number'); $this->assertTrue($model->hasErrors('attr_number')); $val = new NumberValidator(['max' => 10]); $model = new FakedValidationModel(); $model->attr_number = 10; $val->validateAttribute($model, 'attr_number'); $this->assertFalse($model->hasErrors('attr_number')); $model->attr_number = 15; $val->validateAttribute($model, 'attr_number'); $this->assertTrue($model->hasErrors('attr_number')); $val = new NumberValidator(['max' => 10, 'integerOnly' => true]); $model = new FakedValidationModel(); $model->attr_number = 10; $val->validateAttribute($model, 'attr_number'); $this->assertFalse($model->hasErrors('attr_number')); $model->attr_number = 3.43; $val->validateAttribute($model, 'attr_number'); $this->assertTrue($model->hasErrors('attr_number')); $val = new NumberValidator(['min' => 1]); $model = FakedValidationModel::createWithAttributes(['attr_num' => [1, 2, 3]]); $val->validateAttribute($model, 'attr_num'); $this->assertTrue($model->hasErrors('attr_num')); }
public function testValidateAttributeICUFormat() { // error-array-add $val = new DateValidator(['format' => 'yyyy-MM-dd']); $model = new FakedValidationModel(); $model->attr_date = '2013-09-13'; $val->validateAttribute($model, 'attr_date'); $this->assertFalse($model->hasErrors('attr_date')); $model = new FakedValidationModel(); $model->attr_date = '1375293913'; $val->validateAttribute($model, 'attr_date'); $this->assertTrue($model->hasErrors('attr_date')); //// timestamp attribute $val = new DateValidator(['format' => 'yyyy-MM-dd', 'timestampAttribute' => 'attr_timestamp']); $model = new FakedValidationModel(); $model->attr_date = '2013-09-13'; $model->attr_timestamp = true; $val->validateAttribute($model, 'attr_date'); $this->assertFalse($model->hasErrors('attr_date')); $this->assertFalse($model->hasErrors('attr_timestamp')); $this->assertEquals(mktime(0, 0, 0, 9, 13, 2013), $model->attr_timestamp); $val = new DateValidator(['format' => 'yyyy-MM-dd']); $model = FakedValidationModel::createWithAttributes(['attr_date' => []]); $val->validateAttribute($model, 'attr_date'); $this->assertTrue($model->hasErrors('attr_date')); }
public function testIntlMultibyteString() { $val = new DateValidator(['format' => 'dd MMM yyyy', 'locale' => 'de_DE']); $model = FakedValidationModel::createWithAttributes(['attr_date' => '12 Mai 2014']); $val->validateAttribute($model, 'attr_date'); $this->assertFalse($model->hasErrors('attr_date')); $val = new DateValidator(['format' => 'dd MMM yyyy', 'locale' => 'ru_RU']); $model = FakedValidationModel::createWithAttributes(['attr_date' => '12 мая 2014']); $val->validateAttribute($model, 'attr_date'); $this->assertFalse($model->hasErrors('attr_date')); }