UrlValidatorTest.php 3.71 KB
Newer Older
Suralc committed
1
<?php
AlexGx committed
2

Suralc committed
3
namespace yiiunit\framework\validators;
AlexGx committed
4

5
use yiiunit\data\validators\models\FakedValidationModel;
Suralc committed
6 7 8 9
use yii\validators\UrlValidator;
use yiiunit\TestCase;

/**
10
 * @group validators
Suralc committed
11 12 13
 */
class UrlValidatorTest extends TestCase
{
14 15 16 17 18
    protected function setUp()
    {
        parent::setUp();
        $this->mockApplication();
    }
19

20 21 22 23 24 25 26 27 28 29 30 31 32
    public function testValidateValue()
    {
        $val = new UrlValidator;
        $this->assertFalse($val->validate('google.de'));
        $this->assertTrue($val->validate('http://google.de'));
        $this->assertTrue($val->validate('https://google.de'));
        $this->assertFalse($val->validate('htp://yiiframework.com'));
        $this->assertTrue($val->validate('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->validate('ftp://ftp.ruhr-uni-bochum.de/'));
        $this->assertFalse($val->validate('http://invalid,domain'));
        $this->assertFalse($val->validate('http://äüö?=!"§$%&/()=}][{³²€.edu'));
    }
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    public function testValidateValueWithDefaultScheme()
    {
        $val = new UrlValidator(['defaultScheme' => 'https']);
        $this->assertTrue($val->validate('yiiframework.com'));
        $this->assertTrue($val->validate('http://yiiframework.com'));
    }

    public function testValidateValueWithoutScheme()
    {
        $val = new UrlValidator(['pattern' => '/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)/i']);
        $this->assertTrue($val->validate('yiiframework.com'));
    }

    public function testValidateWithCustomScheme()
    {
        $val = new UrlValidator([
            'validSchemes' => ['http', 'https', 'ftp', 'ftps'],
            'defaultScheme' => 'http',
        ]);
        $this->assertTrue($val->validate('ftp://ftp.ruhr-uni-bochum.de/'));
        $this->assertTrue($val->validate('google.de'));
        $this->assertTrue($val->validate('http://google.de'));
        $this->assertTrue($val->validate('https://google.de'));
        $this->assertFalse($val->validate('htp://yiiframework.com'));
        // relative urls not supported
        $this->assertFalse($val->validate('//yiiframework.com'));
    }

    public function testValidateWithIdn()
    {
        if (!function_exists('idn_to_ascii')) {
            $this->markTestSkipped('intl package required');

            return;
        }
        $val = new UrlValidator([
            'enableIDN' => true,
        ]);
        $this->assertTrue($val->validate('http://äüößìà.de'));
        // converted via http://mct.verisign-grs.com/convertServlet
        $this->assertTrue($val->validate('http://xn--zcack7ayc9a.de'));
    }

    public function testValidateLength()
    {
        $url = 'http://' . str_pad('base', 2000, 'url') . '.de';
        $val = new UrlValidator;
        $this->assertFalse($val->validate($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'));
    }
Suralc committed
103
}