Browse Source

Merge branch 'add-tests' of github.com:suralc/yii2 into add-tests

tags/2.0.0-beta
Suralc 11 years ago
parent
commit
1c57661e1f
  1. 1
      framework/yii/validators/BooleanValidator.php
  2. 1
      framework/yii/validators/CaptchaValidator.php
  3. 1
      framework/yii/validators/CompareValidator.php
  4. 1
      framework/yii/validators/EmailValidator.php
  5. 1
      framework/yii/validators/NumberValidator.php
  6. 1
      framework/yii/validators/RangeValidator.php
  7. 1
      framework/yii/validators/RegularExpressionValidator.php
  8. 1
      framework/yii/validators/RequiredValidator.php
  9. 1
      framework/yii/validators/StringValidator.php
  10. 3
      framework/yii/validators/UrlValidator.php
  11. 54
      tests/unit/framework/validators/BooleanValidatorTest.php
  12. 160
      tests/unit/framework/validators/CompareValidatorTest.php
  13. 61
      tests/unit/framework/validators/DateValidatorTest.php
  14. 32
      tests/unit/framework/validators/DefaultValueValidatorTest.php
  15. 31
      tests/unit/framework/validators/FakedValidationModel.php
  16. 89
      tests/unit/framework/validators/UrlValidatorTest.php

1
framework/yii/validators/BooleanValidator.php

@ -82,6 +82,7 @@ class BooleanValidator extends Validator
* @param \yii\base\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
* @codeCoverageIgnore
*/
public function clientValidateAttribute($object, $attribute, $view)
{

1
framework/yii/validators/CaptchaValidator.php

@ -97,6 +97,7 @@ class CaptchaValidator extends Validator
* @param \yii\base\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
* @codeCoverageIgnore
*/
public function clientValidateAttribute($object, $attribute, $view)
{

1
framework/yii/validators/CompareValidator.php

@ -181,6 +181,7 @@ class CompareValidator extends Validator
* @param \yii\base\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @throws InvalidConfigException if CompareValidator::operator is invalid
* @codeCoverageIgnore
*/
public function clientValidateAttribute($object, $attribute, $view)
{

1
framework/yii/validators/EmailValidator.php

@ -122,6 +122,7 @@ class EmailValidator extends Validator
* @param \yii\base\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
* @codeCoverageIgnore
*/
public function clientValidateAttribute($object, $attribute, $view)
{

1
framework/yii/validators/NumberValidator.php

@ -117,6 +117,7 @@ class NumberValidator extends Validator
* @param \yii\base\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
* @codeCoverageIgnore
*/
public function clientValidateAttribute($object, $attribute, $view)
{

1
framework/yii/validators/RangeValidator.php

@ -84,6 +84,7 @@ class RangeValidator extends Validator
* @param \yii\base\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
* @codeCoverageIgnore
*/
public function clientValidateAttribute($object, $attribute, $view)
{

1
framework/yii/validators/RegularExpressionValidator.php

@ -83,6 +83,7 @@ class RegularExpressionValidator extends Validator
* containing a model form with this validator applied.
* @return string the client-side validation script.
* @throws InvalidConfigException if the "pattern" is not a valid regular expression
* @codeCoverageIgnore
*/
public function clientValidateAttribute($object, $attribute, $view)
{

1
framework/yii/validators/RequiredValidator.php

@ -105,6 +105,7 @@ class RequiredValidator extends Validator
* @param \yii\base\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
* @codeCoverageIgnore
*/
public function clientValidateAttribute($object, $attribute, $view)
{

1
framework/yii/validators/StringValidator.php

@ -145,6 +145,7 @@ class StringValidator extends Validator
* @param \yii\base\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
* @codeCoverageIgnore
*/
public function clientValidateAttribute($object, $attribute, $view)
{

3
framework/yii/validators/UrlValidator.php

@ -54,7 +54,9 @@ class UrlValidator extends Validator
{
parent::init();
if ($this->enableIDN && !function_exists('idn_to_ascii')) {
// @codeCoverageIgnoreStart
throw new InvalidConfigException('In order to use IDN validation intl extension must be installed and enabled.');
// @codeCoverageIgnoreEnd
}
if ($this->message === null) {
$this->message = Yii::t('yii', '{attribute} is not a valid URL.');
@ -119,6 +121,7 @@ class UrlValidator extends Validator
* containing a model form with this validator applied.
* @return string the client-side validation script.
* @see \yii\Web\ActiveForm::enableClientValidation
* @codeCoverageIgnore
*/
public function clientValidateAttribute($object, $attribute, $view)
{

54
tests/unit/framework/validators/BooleanValidatorTest.php

@ -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'));
}
}

160
tests/unit/framework/validators/CompareValidatorTest.php

@ -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');
}
}

61
tests/unit/framework/validators/DateValidatorTest.php

@ -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
);
}
}

32
tests/unit/framework/validators/DefaultValueValidatorTest.php

@ -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);
}
}

31
tests/unit/framework/validators/FakedValidationModel.php

@ -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);
}
}
}

89
tests/unit/framework/validators/UrlValidatorTest.php

@ -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…
Cancel
Save