mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-01 11:39:41 +08:00
17191: refacor tests using dataprovider
This commit is contained in:
@ -10,64 +10,103 @@ use yii\helpers\BaseUrl;
|
|||||||
*/
|
*/
|
||||||
class BaseUrlTest extends TestCase
|
class BaseUrlTest extends TestCase
|
||||||
{
|
{
|
||||||
|
/** @dataProvider relativeTrueUrlProvider */
|
||||||
public function testIsRelativeWithAbsoluteUrlWillReturnFalse()
|
public function testIsRelativeWillReturnTrue($url)
|
||||||
{
|
{
|
||||||
$this->assertFalse(BaseUrl::isRelative('https://acme.com/tnt-room=123'));
|
$this->assertTrue(BaseUrl::isRelative($url));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testUrlStartingWithDoubleSlashesWillReturnFalse()
|
/** @dataProvider relativeFalseUrlProvider */
|
||||||
|
public function testIsRelativeWillReturnFalse($url)
|
||||||
{
|
{
|
||||||
$this->assertFalse(BaseUrl::isRelative('//example.com'));
|
$this->assertFalse(BaseUrl::isRelative($url));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIsRelativeWithRelativeUrlWillReturnTrue()
|
public function testEnsureSchemeWithRelativeUrlWillReturnInputUrl()
|
||||||
{
|
{
|
||||||
$this->assertTrue(
|
$url = 'acme.com?name=bugs.bunny';
|
||||||
BaseUrl::isRelative('acme.com/tnt-room=123')
|
$this->assertEquals('acme.com?name=bugs.bunny', BaseUrl::ensureScheme($url, 'https'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testEnsureSchemeWithRelativeUrlWithAnotherUrlAsParamWillReturnInputUrl()
|
||||||
|
{
|
||||||
|
$this->assertEquals('acme.com/test?tnt-link=https://tnt.com/',
|
||||||
|
BaseUrl::ensureScheme('acme.com/test?tnt-link=https://tnt.com/', 'https')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIsRelativeWithRelativeUrlHavingHttpsUrlAsParamValueWillReturnTrue()
|
public function testEnsureSchemeWithSchemeNotAStringWillReturnInputUrl()
|
||||||
{
|
{
|
||||||
$this->assertTrue(BaseUrl::isRelative(
|
$url = 'acme.com?name=bugs.bunny';
|
||||||
'acme.com/tnt-room-link=https://asd.com'
|
$this->assertEquals('acme.com?name=bugs.bunny', BaseUrl::ensureScheme($url, 123));
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIsRelativeWithAbsoluteUrlHavingHttpsUrlAsParamValueWillReturnFalse()
|
public function testEnsureSchemeWithProtocolRelativeUrlAndHttpsSchemeWillBeNormalized()
|
||||||
{
|
{
|
||||||
$this->assertFalse(
|
$url = '//acme.com?characters/list';
|
||||||
BaseUrl::isRelative('https://acme.com/tnt-link=https://tnt.com')
|
$this->assertEquals('https://acme.com?characters/list', BaseUrl::ensureScheme($url, 'https'));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIsRelativeWithA()
|
public function testEnsureSchemeWithProtocolRelativeUrlAndEmptySchemeWillBeReturned()
|
||||||
{
|
{
|
||||||
$this->assertTrue(
|
$url = '//acme.com?characters/list';
|
||||||
BaseUrl::isRelative('/name=bugs.bunny')
|
$this->assertEquals('//acme.com?characters/list', BaseUrl::ensureScheme($url, ''));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIsRelativeWithFtpProtocolUrlWillReturnFalse()
|
public function testAbsoluteUrlProtocolAndEmptySchemeWillCreateProtocolRelativeUrl()
|
||||||
{
|
{
|
||||||
$this->assertFalse(
|
$url = 'https://acme.com?characters/list';
|
||||||
BaseUrl::isRelative('ftp://ftp.acme.com/tnt-suppliers.txt')
|
$this->assertEquals('//acme.com?characters/list', BaseUrl::ensureScheme($url, ''));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIsRelativeWithHttpUrl()
|
public function testEnsureSchemeWithAbsoluteUrlWithAnotherUrlAsParamWillReturnInputUrl()
|
||||||
{
|
{
|
||||||
$this->assertFalse(
|
$url = 'ss://acme.com/test?tnt-link=https://tnt.com/';
|
||||||
BaseUrl::isRelative('http://no-protection.acme.com')
|
$this->assertEquals('https://acme.com/test?tnt-link=https://tnt.com/', BaseUrl::ensureScheme($url, 'https'));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIsRelativeWithFileUrl()
|
public function relativeTrueUrlProvider()
|
||||||
{
|
{
|
||||||
$this->assertFalse(
|
return [
|
||||||
BaseUrl::isRelative('file:///home/User/2ndFile.html')
|
'url url without protocol' => [
|
||||||
);
|
'url' => 'acme.com/tnt-room=123',
|
||||||
|
],
|
||||||
|
'url without protocol and another url in a parameter value' => [
|
||||||
|
'url' => 'acme.com?tnt-room-link=https://tnt.com',
|
||||||
|
],
|
||||||
|
'path only' => [
|
||||||
|
'url' => '/path',
|
||||||
|
],
|
||||||
|
'path with param' => [
|
||||||
|
'url' => '/path=/home/user',
|
||||||
|
],
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function relativeFalseUrlProvider()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'url with https protocol' => [
|
||||||
|
'url' => 'https://acme.com',
|
||||||
|
],
|
||||||
|
'url with https protocol and ending slash' => [
|
||||||
|
'url' => 'https://acme.com/',
|
||||||
|
],
|
||||||
|
'url with https protocol and another url as param value' => [
|
||||||
|
'url' => 'https://acme.com?tnt-link=https://tnt.com',
|
||||||
|
],
|
||||||
|
'url starting with two slashes' => [
|
||||||
|
'url' => '//example.com',
|
||||||
|
],
|
||||||
|
'url with ftp protocol' => [
|
||||||
|
'url' => 'ftp://ftp.acme.com/tnt-suppliers.txt',
|
||||||
|
],
|
||||||
|
'url with http protocol' => [
|
||||||
|
'url' => 'http://no-protection.acme.com',
|
||||||
|
],
|
||||||
|
'file url' => [
|
||||||
|
'url' => 'file:///home/User/2ndFile.html',
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user