Suralc
12 years ago
16 changed files with 439 additions and 0 deletions
@ -0,0 +1,54 @@
|
||||
<?php |
||||
namespace yiiunit\framework\validators; |
||||
|
||||
use yiiunit\framework\validators\FakedValidationModel; |
||||
use yii\validators\BooleanValidator; |
||||
use yiiunit\TestCase; |
||||
|
||||
require_once __DIR__ . '/FakedValidationModel.php'; |
||||
/** |
||||
* BooleanValidatorTest |
||||
*/ |
||||
class BooleanValidatorTest extends TestCase |
||||
{ |
||||
public function testValidateValue() |
||||
{ |
||||
$val = new BooleanValidator; |
||||
$this->assertTrue($val->validateValue(true)); |
||||
$this->assertTrue($val->validateValue(false)); |
||||
$this->assertTrue($val->validateValue('0')); |
||||
$this->assertTrue($val->validateValue('1')); |
||||
$this->assertFalse($val->validateValue(null)); |
||||
$this->assertFalse($val->validateValue(array())); |
||||
$val->strict = true; |
||||
$this->assertTrue($val->validateValue('0')); |
||||
$this->assertTrue($val->validateValue('1')); |
||||
$this->assertFalse($val->validateValue(true)); |
||||
$this->assertFalse($val->validateValue(false)); |
||||
$val->trueValue = true; |
||||
$val->falseValue = false; |
||||
$this->assertFalse($val->validateValue('0')); |
||||
$this->assertFalse($val->validateValue(array())); |
||||
$this->assertTrue($val->validateValue(true)); |
||||
$this->assertTrue($val->validateValue(false)); |
||||
} |
||||
|
||||
public function testValidateAttributeAndError() |
||||
{ |
||||
$obj = new FakedValidationModel; |
||||
$obj->attrA = true; |
||||
$obj->attrB = '1'; |
||||
$obj->attrC = '0'; |
||||
$obj->attrD = array(); |
||||
$val = new BooleanValidator; |
||||
$val->validateAttribute($obj, 'attrA'); |
||||
$this->assertFalse($obj->hasErrors('attrA')); |
||||
$val->validateAttribute($obj, 'attrC'); |
||||
$this->assertFalse($obj->hasErrors('attrC')); |
||||
$val->strict = true; |
||||
$val->validateAttribute($obj, 'attrB'); |
||||
$this->assertFalse($obj->hasErrors('attrB')); |
||||
$val->validateAttribute($obj, 'attrD'); |
||||
$this->assertTrue($obj->hasErrors('attrD')); |
||||
} |
||||
} |
@ -0,0 +1,160 @@
|
||||
<?php |
||||
namespace yiiunit\framework\validators; |
||||
|
||||
use yii\base\InvalidConfigException; |
||||
use yii\validators\CompareValidator; |
||||
use yiiunit\TestCase; |
||||
|
||||
require_once __DIR__ . '/FakedValidationModel.php'; |
||||
|
||||
|
||||
class CompareValidatorTest extends TestCase |
||||
{ |
||||
|
||||
public function testValidateValueException() |
||||
{ |
||||
$this->setExpectedException('yii\base\InvalidConfigException'); |
||||
$val = new CompareValidator; |
||||
$val->validateValue('val'); |
||||
} |
||||
|
||||
public function testValidateValue() |
||||
{ |
||||
$value = 18449; |
||||
// default config |
||||
$val = new CompareValidator(array('compareValue' => $value)); |
||||
$this->assertTrue($val->validateValue($value)); |
||||
$this->assertTrue($val->validateValue((string)$value)); |
||||
$this->assertFalse($val->validateValue($value + 1)); |
||||
foreach ($this->getOperationTestData($value) as $op => $tests) { |
||||
$val = new CompareValidator(array('compareValue' => $value)); |
||||
$val->operator = $op; |
||||
foreach ($tests as $test) { |
||||
$this->assertEquals($test[1], $val->validateValue($test[0])); |
||||
} |
||||
} |
||||
} |
||||
|
||||
protected function getOperationTestData($value) |
||||
{ |
||||
return array( |
||||
'===' => array( |
||||
array($value, true), |
||||
array((string)$value, false), |
||||
array((float)$value, false), |
||||
array($value + 1, false), |
||||
), |
||||
'!=' => array( |
||||
array($value, false), |
||||
array((string)$value, false), |
||||
array((float)$value, false), |
||||
array($value + 0.00001, true), |
||||
array(false, true), |
||||
), |
||||
'!==' => array( |
||||
array($value, false), |
||||
array((string)$value, true), |
||||
array((float)$value, true), |
||||
array(false, true), |
||||
), |
||||
'>' => array( |
||||
array($value, false), |
||||
array($value + 1, true), |
||||
array($value - 1, false), |
||||
), |
||||
'>=' => array( |
||||
array($value, true), |
||||
array($value + 1, true), |
||||
array($value - 1, false), |
||||
), |
||||
'<' => array( |
||||
array($value, false), |
||||
array($value + 1, false), |
||||
array($value - 1, true), |
||||
), |
||||
'<=' => array( |
||||
array($value, true), |
||||
array($value + 1, false), |
||||
array($value - 1, true), |
||||
), |
||||
|
||||
); |
||||
} |
||||
|
||||
public function testValidateAttribute() |
||||
{ |
||||
// invalid-array |
||||
$val = new CompareValidator; |
||||
$model = new FakedValidationModel; |
||||
$model->attr = array('test_val'); |
||||
$val->validateAttribute($model, 'attr'); |
||||
$this->assertTrue($model->hasErrors('attr')); |
||||
$val = new CompareValidator(array('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(array('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(array('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')); |
||||
} |
||||
|
||||
public function testValidateAttributeOperators() |
||||
{ |
||||
$value = 55; |
||||
foreach ($this->getOperationTestData($value) as $operator => $tests) { |
||||
$val = new CompareValidator(array('operator' => $operator, 'compareValue' => $value)); |
||||
foreach ($tests as $test) { |
||||
$model = new FakedValidationModel; |
||||
$model->attr_test = $test[0]; |
||||
$val->validateAttribute($model, 'attr_test'); |
||||
$this->assertEquals($test[1], !$model->hasErrors('attr_test')); |
||||
} |
||||
|
||||
} |
||||
} |
||||
|
||||
public function testEnsureMessageSetOnInit() |
||||
{ |
||||
foreach ($this->getOperationTestData(1337) as $operator => $tests) { |
||||
$val = new CompareValidator(array('operator' => $operator)); |
||||
$this->assertTrue(strlen($val->message) > 1); |
||||
} |
||||
try { |
||||
$val = new CompareValidator(array('operator' => '<>')); |
||||
} catch (InvalidConfigException $e) { |
||||
return; |
||||
} |
||||
catch (\Exception $e) { |
||||
$this->fail('InvalidConfigException expected' . get_class($e) . 'received'); |
||||
return; |
||||
} |
||||
$this->fail('InvalidConfigException expected none received'); |
||||
} |
||||
} |
@ -0,0 +1,61 @@
|
||||
<?php |
||||
|
||||
namespace yiiunit\framework\validators; |
||||
|
||||
use DateTime; |
||||
use yii\validators\DateValidator; |
||||
use yiiunit\TestCase; |
||||
|
||||
require_once __DIR__ . '/FakedValidationModel.php'; |
||||
|
||||
class DateValidatorTest extends TestCase |
||||
{ |
||||
public function testEnsureMessageIsSet() |
||||
{ |
||||
$val = new DateValidator; |
||||
$this->assertTrue($val->message !== null && strlen($val->message) > 1); |
||||
} |
||||
|
||||
public function testValidateValue() |
||||
{ |
||||
$val = new DateValidator; |
||||
$this->assertTrue($val->validateValue('2013-09-13')); |
||||
$this->assertFalse($val->validateValue('31.7.2013')); |
||||
$this->assertFalse($val->validateValue('31-7-2013')); |
||||
$this->assertFalse($val->validateValue(time())); |
||||
$val->format = 'U'; |
||||
$this->assertTrue($val->validateValue(time())); |
||||
$val->format = 'd.m.Y'; |
||||
$this->assertTrue($val->validateValue('31.7.2013')); |
||||
$val->format = 'Y-m-!d H:i:s'; |
||||
$this->assertTrue($val->validateValue('2009-02-15 15:16:17')); |
||||
} |
||||
|
||||
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(array('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 |
||||
); |
||||
|
||||
|
||||
} |
||||
} |
@ -0,0 +1,32 @@
|
||||
<?php |
||||
namespace yiiunit\framework\validators; |
||||
use yii\validators\DefaultValueValidator; |
||||
use yiiunit\TestCase; |
||||
|
||||
/** |
||||
* DefaultValueValidatorTest |
||||
*/ |
||||
class DefaultValueValidatorTest extends TestCase |
||||
{ |
||||
public function testValidateAttribute() |
||||
{ |
||||
$val = new DefaultValueValidator; |
||||
$val->value = 'test_value'; |
||||
$obj = new \stdclass; |
||||
$obj->attrA = 'attrA'; |
||||
$obj->attrB = null; |
||||
$obj->attrC = ''; |
||||
// original values to chek which attritubes where modified |
||||
$objB = clone $obj; |
||||
$val->validateAttribute($obj, 'attrB'); |
||||
$this->assertEquals($val->value, $obj->attrB); |
||||
$this->assertEquals($objB->attrA, $obj->attrA); |
||||
$val->value = 'new_test_value'; |
||||
$obj = clone $objB; // get clean object |
||||
$val->validateAttribute($obj, 'attrC'); |
||||
$this->assertEquals('new_test_value', $obj->attrC); |
||||
$this->assertEquals($objB->attrA, $obj->attrA); |
||||
$val->validateAttribute($obj, 'attrA'); |
||||
$this->assertEquals($objB->attrA, $obj->attrA); |
||||
} |
||||
} |
@ -0,0 +1,31 @@
|
||||
<?php |
||||
|
||||
namespace yiiunit\framework\validators; |
||||
|
||||
use yii\base\Model; |
||||
|
||||
/** |
||||
* @codeCoverageIgnore |
||||
*/ |
||||
class FakedValidationModel extends Model |
||||
{ |
||||
private $attr = array(); |
||||
|
||||
public function __get($name) |
||||
{ |
||||
if (stripos($name, 'attr') === 0) { |
||||
return isset($this->attr[$name]) ? $this->attr[$name] : null; |
||||
} |
||||
|
||||
return parent::__get($name); |
||||
} |
||||
|
||||
public function __set($name, $value) |
||||
{ |
||||
if (stripos($name, 'attr') === 0) { |
||||
$this->attr[$name] = $value; |
||||
} else { |
||||
parent::__set($name, $value); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,89 @@
|
||||
<?php |
||||
namespace yiiunit\framework\validators; |
||||
use yiiunit\framework\validators\FakedValidationModel; |
||||
use yii\validators\UrlValidator; |
||||
use yiiunit\TestCase; |
||||
|
||||
require_once __DIR__ . '/FakedValidationModel.php'; |
||||
/** |
||||
* UrlValidatorTest |
||||
*/ |
||||
class UrlValidatorTest extends TestCase |
||||
{ |
||||
public function testValidateValue() |
||||
{ |
||||
$val = new UrlValidator; |
||||
$this->assertFalse($val->validateValue('google.de')); |
||||
$this->assertTrue($val->validateValue('http://google.de')); |
||||
$this->assertTrue($val->validateValue('https://google.de')); |
||||
$this->assertFalse($val->validateValue('htp://yiiframework.com')); |
||||
$this->assertTrue($val->validateValue('https://www.google.de/search?q=yii+framework&ie=utf-8&oe=utf-8' |
||||
.'&rls=org.mozilla:de:official&client=firefox-a&gws_rd=cr')); |
||||
$this->assertFalse($val->validateValue('ftp://ftp.ruhr-uni-bochum.de/')); |
||||
$this->assertFalse($val->validateValue('http://invalid,domain')); |
||||
$this->assertFalse($val->validateValue('http://äüö?=!"§$%&/()=}][{³²€.edu')); |
||||
} |
||||
|
||||
public function testValidateValueWithDefaultScheme() |
||||
{ |
||||
$val = new UrlValidator(array('defaultScheme' => 'https')); |
||||
$this->assertTrue($val->validateValue('yiiframework.com')); |
||||
$this->assertTrue($val->validateValue('http://yiiframework.com')); |
||||
} |
||||
|
||||
public function testValidateWithCustomScheme() |
||||
{ |
||||
$val = new UrlValidator(array( |
||||
'validSchemes' => array('http', 'https', 'ftp', 'ftps'), |
||||
'defaultScheme' => 'http', |
||||
)); |
||||
$this->assertTrue($val->validateValue('ftp://ftp.ruhr-uni-bochum.de/')); |
||||
$this->assertTrue($val->validateValue('google.de')); |
||||
$this->assertTrue($val->validateValue('http://google.de')); |
||||
$this->assertTrue($val->validateValue('https://google.de')); |
||||
$this->assertFalse($val->validateValue('htp://yiiframework.com')); |
||||
// relative urls not supported |
||||
$this->assertFalse($val->validateValue('//yiiframework.com')); |
||||
} |
||||
|
||||
public function testValidateWithIdn() |
||||
{ |
||||
if(!function_exists('idn_to_ascii')) { |
||||
$this->markTestSkipped('intl package required'); |
||||
return; |
||||
} |
||||
$val = new UrlValidator(array( |
||||
'enableIDN' => true, |
||||
)); |
||||
$this->assertTrue($val->validateValue('http://äüößìà.de')); |
||||
// converted via http://mct.verisign-grs.com/convertServlet |
||||
$this->assertTrue($val->validateValue('http://xn--zcack7ayc9a.de')); |
||||
} |
||||
|
||||
public function testValidateLength() |
||||
{ |
||||
$url = 'http://' . str_pad('base', 2000, 'url') . '.de'; |
||||
$val = new UrlValidator; |
||||
$this->assertFalse($val->validateValue($url)); |
||||
} |
||||
|
||||
public function testValidateAttributeAndError() |
||||
{ |
||||
$obj = new FakedValidationModel; |
||||
$obj->attr_url = 'http://google.de'; |
||||
$val = new UrlValidator; |
||||
$val->validateAttribute($obj, 'attr_url'); |
||||
$this->assertFalse($obj->hasErrors('attr_url')); |
||||
$this->assertSame('http://google.de', $obj->attr_url); |
||||
$obj = new FakedValidationModel; |
||||
$val->defaultScheme = 'http'; |
||||
$obj->attr_url = 'google.de'; |
||||
$val->validateAttribute($obj, 'attr_url'); |
||||
$this->assertFalse($obj->hasErrors('attr_url')); |
||||
$this->assertTrue(stripos($obj->attr_url, 'http') !== false); |
||||
$obj = new FakedValidationModel; |
||||
$obj->attr_url = 'gttp;/invalid string'; |
||||
$val->validateAttribute($obj, 'attr_url'); |
||||
$this->assertTrue($obj->hasErrors('attr_url')); |
||||
} |
||||
} |
Loading…
Reference in new issue