Skip to content

Commit fcaafd8

Browse files
authored
Merge pull request #20077 from ggh2e3/17191_fix_is_relative_function
17191 fix is relative function
2 parents d43341a + 76f1733 commit fcaafd8

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed

framework/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Yii Framework 2 Change Log
44
2.0.50 under development
55
------------------------
66

7+
- Bug #17191: Fixed `BaseUrl::isRelative($url)` method in `yii\helpers\BaseUrl` (ggh2e3)
78
- Bug #18469: Fixed `Link::serialize(array $links)` method in `yii\web\Link` (ggh2e3)
89
- Bug #20040: Fix type `boolean` in `MSSQL` (terabytesoftw)
910
- Bug #20005: Fix `yii\console\controllers\ServeController` to specify the router script (terabytesoftw)

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: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
/** @dataProvider relativeTrueUrlProvider */
14+
public function testIsRelativeWillReturnTrue($url)
15+
{
16+
$this->assertTrue(BaseUrl::isRelative($url));
17+
}
18+
19+
/** @dataProvider relativeFalseUrlProvider */
20+
public function testIsRelativeWillReturnFalse($url)
21+
{
22+
$this->assertFalse(BaseUrl::isRelative($url));
23+
}
24+
25+
/** @dataProvider ensureSchemeUrlProvider */
26+
public function testEnsureScheme($url, $scheme, $expected)
27+
{
28+
$this->assertEquals($expected, BaseUrl::ensureScheme($url, $scheme));
29+
}
30+
31+
public function ensureSchemeUrlProvider()
32+
{
33+
return [
34+
'relative url and https scheme will return input url' => [
35+
'url' => 'acme.com?name=bugs.bunny',
36+
'scheme' => 'https',
37+
'expected result' => 'acme.com?name=bugs.bunny',
38+
],
39+
'relative url and another url as parameter will return input url' => [
40+
'url' => 'acme.com/test?tnt-link=https://tnt.com/',
41+
'scheme' => 'https',
42+
'expected' => 'acme.com/test?tnt-link=https://tnt.com/',
43+
],
44+
'url with scheme not a string will return input url' => [
45+
'url' => 'acme.com?name=bugs.bunny',
46+
'scheme' => 123,
47+
'expected' => 'acme.com?name=bugs.bunny',
48+
],
49+
'protocol relative url and https scheme will be processed' => [
50+
'url' => '//acme.com?characters/list',
51+
'scheme' => 'https',
52+
'expected' => 'https://acme.com?characters/list',
53+
],
54+
'protocol relative url and empty scheme will be returned' => [
55+
'url' => '//acme.com?characters/list',
56+
'scheme' => '',
57+
'expected' => '//acme.com?characters/list',
58+
],
59+
'absolute url and empty scheme will create protocol relative url' => [
60+
'url' => 'https://acme.com?characters/list',
61+
'scheme' => '',
62+
'expected' => '//acme.com?characters/list',
63+
],
64+
'absolute url and different scheme will be processed' => [
65+
'url' => 'http://acme.com/test?tnt-link=https://tnt.com/',
66+
'scheme' => 'https',
67+
'expected' => 'https://acme.com/test?tnt-link=https://tnt.com/',
68+
]
69+
];
70+
}
71+
72+
public function relativeTrueUrlProvider()
73+
{
74+
return [
75+
'url url without protocol' => [
76+
'url' => 'acme.com/tnt-room=123',
77+
],
78+
'url without protocol and another url in a parameter value' => [
79+
'url' => 'acme.com?tnt-room-link=https://tnt.com',
80+
],
81+
'path only' => [
82+
'url' => '/path',
83+
],
84+
'path with param' => [
85+
'url' => '/path=/home/user',
86+
],
87+
];
88+
}
89+
90+
public function relativeFalseUrlProvider()
91+
{
92+
return [
93+
'url with https protocol' => [
94+
'url' => 'https://acme.com',
95+
],
96+
'url with https protocol and ending slash' => [
97+
'url' => 'https://acme.com/',
98+
],
99+
'url with https protocol and another url as param value' => [
100+
'url' => 'https://acme.com?tnt-link=https://tnt.com',
101+
],
102+
'url starting with two slashes' => [
103+
'url' => '//example.com',
104+
],
105+
'url with ftp protocol' => [
106+
'url' => 'ftp://ftp.acme.com/tnt-suppliers.txt',
107+
],
108+
'url with http protocol' => [
109+
'url' => 'http://no-protection.acme.com',
110+
],
111+
'file url' => [
112+
'url' => 'file:///home/User/2ndFile.html',
113+
]
114+
];
115+
}
116+
}

0 commit comments

Comments
 (0)