Skip to content

Commit fc582a2

Browse files
committed
17191: fixed isRelative method
1 parent d43341a commit fc582a2

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

framework/helpers/BaseUrl.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,8 @@ public static function home($scheme = false)
378378
*/
379379
public static function isRelative($url)
380380
{
381-
return strncmp($url, '//', 2) && strpos($url, '://') === false;
381+
$urlComponents = parse_url($url, PHP_URL_SCHEME);
382+
return strncmp($url, '//', 2) && empty($urlComponents);
382383
}
383384

384385
/**
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace yiiunit\framework\helpers;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use yii\helpers\BaseUrl;
7+
8+
/**
9+
* @group helpers
10+
*/
11+
class BaseUrlTest extends TestCase
12+
{
13+
14+
public function testIsRelativeWithAbsoluteUrlWillReturnFalse()
15+
{
16+
$this->assertFalse(BaseUrl::isRelative('https://acme.com/tnt-room=123'));
17+
}
18+
19+
public function testUrlStartingWithDoubleSlashesWillReturnFalse()
20+
{
21+
$this->assertFalse(BaseUrl::isRelative('//example.com'));
22+
}
23+
24+
public function testIsRelativeWithRelativeUrlWillReturnTrue()
25+
{
26+
$this->assertTrue(
27+
BaseUrl::isRelative('acme.com/tnt-room=123')
28+
);
29+
}
30+
31+
public function testIsRelativeWithRelativeUrlHavingHttpsUrlAsParamValueWillReturnTrue()
32+
{
33+
$this->assertTrue(BaseUrl::isRelative(
34+
'acme.com/tnt-room-link=https://asd.com'
35+
));
36+
}
37+
38+
public function testIsRelativeWithAbsoluteUrlHavingHttpsUrlAsParamValueWillReturnFalse()
39+
{
40+
$this->assertFalse(
41+
BaseUrl::isRelative('https://acme.com/tnt-link=https://tnt.com')
42+
);
43+
}
44+
45+
public function testIsRelativeWithA()
46+
{
47+
$this->assertTrue(
48+
BaseUrl::isRelative('/name=bugs.bunny')
49+
);
50+
}
51+
52+
public function testIsRelativeWithFtpProtocolUrlWillReturnFalse()
53+
{
54+
$this->assertFalse(
55+
BaseUrl::isRelative('ftp://ftp.acme.com/tnt-suppliers.txt')
56+
);
57+
}
58+
59+
public function testIsRelativeWithHttpUrl()
60+
{
61+
$this->assertFalse(
62+
BaseUrl::isRelative('http://no-protection.acme.com')
63+
);
64+
}
65+
66+
public function testIsRelativeWithFileUrl()
67+
{
68+
$this->assertFalse(
69+
BaseUrl::isRelative('file:///home/User/2ndFile.html')
70+
);
71+
}
72+
73+
}

0 commit comments

Comments
 (0)