From fc582a2cfda2181b3b433675c63209e3b1f57e25 Mon Sep 17 00:00:00 2001 From: ggh2e3 Date: Sun, 3 Dec 2023 15:07:25 +0100 Subject: [PATCH 1/5] 17191: fixed isRelative method --- framework/helpers/BaseUrl.php | 3 +- tests/framework/helpers/BaseUrlTest.php | 73 +++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 tests/framework/helpers/BaseUrlTest.php diff --git a/framework/helpers/BaseUrl.php b/framework/helpers/BaseUrl.php index a671123c7cd..dcbab192730 100644 --- a/framework/helpers/BaseUrl.php +++ b/framework/helpers/BaseUrl.php @@ -378,7 +378,8 @@ public static function home($scheme = false) */ public static function isRelative($url) { - return strncmp($url, '//', 2) && strpos($url, '://') === false; + $urlComponents = parse_url($url, PHP_URL_SCHEME); + return strncmp($url, '//', 2) && empty($urlComponents); } /** diff --git a/tests/framework/helpers/BaseUrlTest.php b/tests/framework/helpers/BaseUrlTest.php new file mode 100644 index 00000000000..84933fcac96 --- /dev/null +++ b/tests/framework/helpers/BaseUrlTest.php @@ -0,0 +1,73 @@ +assertFalse(BaseUrl::isRelative('https://acme.com/tnt-room=123')); + } + + public function testUrlStartingWithDoubleSlashesWillReturnFalse() + { + $this->assertFalse(BaseUrl::isRelative('//example.com')); + } + + public function testIsRelativeWithRelativeUrlWillReturnTrue() + { + $this->assertTrue( + BaseUrl::isRelative('acme.com/tnt-room=123') + ); + } + + public function testIsRelativeWithRelativeUrlHavingHttpsUrlAsParamValueWillReturnTrue() + { + $this->assertTrue(BaseUrl::isRelative( + 'acme.com/tnt-room-link=https://asd.com' + )); + } + + public function testIsRelativeWithAbsoluteUrlHavingHttpsUrlAsParamValueWillReturnFalse() + { + $this->assertFalse( + BaseUrl::isRelative('https://acme.com/tnt-link=https://tnt.com') + ); + } + + public function testIsRelativeWithA() + { + $this->assertTrue( + BaseUrl::isRelative('/name=bugs.bunny') + ); + } + + public function testIsRelativeWithFtpProtocolUrlWillReturnFalse() + { + $this->assertFalse( + BaseUrl::isRelative('ftp://ftp.acme.com/tnt-suppliers.txt') + ); + } + + public function testIsRelativeWithHttpUrl() + { + $this->assertFalse( + BaseUrl::isRelative('http://no-protection.acme.com') + ); + } + + public function testIsRelativeWithFileUrl() + { + $this->assertFalse( + BaseUrl::isRelative('file:///home/User/2ndFile.html') + ); + } + +} From d3e4df762d51c48c920a0cd8587ac22b1a47d302 Mon Sep 17 00:00:00 2001 From: ggh2e3 Date: Sun, 3 Dec 2023 15:27:56 +0100 Subject: [PATCH 2/5] 17191: refacor tests using dataprovider --- tests/framework/helpers/BaseUrlTest.php | 101 ++++++++++++++++-------- 1 file changed, 70 insertions(+), 31 deletions(-) diff --git a/tests/framework/helpers/BaseUrlTest.php b/tests/framework/helpers/BaseUrlTest.php index 84933fcac96..ecc76fec787 100644 --- a/tests/framework/helpers/BaseUrlTest.php +++ b/tests/framework/helpers/BaseUrlTest.php @@ -10,64 +10,103 @@ */ class BaseUrlTest extends TestCase { + /** @dataProvider relativeTrueUrlProvider */ + public function testIsRelativeWillReturnTrue($url) + { + $this->assertTrue(BaseUrl::isRelative($url)); + } - public function testIsRelativeWithAbsoluteUrlWillReturnFalse() + /** @dataProvider relativeFalseUrlProvider */ + public function testIsRelativeWillReturnFalse($url) { - $this->assertFalse(BaseUrl::isRelative('https://acme.com/tnt-room=123')); + $this->assertFalse(BaseUrl::isRelative($url)); } - public function testUrlStartingWithDoubleSlashesWillReturnFalse() + public function testEnsureSchemeWithRelativeUrlWillReturnInputUrl() { - $this->assertFalse(BaseUrl::isRelative('//example.com')); + $url = 'acme.com?name=bugs.bunny'; + $this->assertEquals('acme.com?name=bugs.bunny', BaseUrl::ensureScheme($url, 'https')); } - public function testIsRelativeWithRelativeUrlWillReturnTrue() + public function testEnsureSchemeWithRelativeUrlWithAnotherUrlAsParamWillReturnInputUrl() { - $this->assertTrue( - BaseUrl::isRelative('acme.com/tnt-room=123') + $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( - 'acme.com/tnt-room-link=https://asd.com' - )); + $url = 'acme.com?name=bugs.bunny'; + $this->assertEquals('acme.com?name=bugs.bunny', BaseUrl::ensureScheme($url, 123)); } - public function testIsRelativeWithAbsoluteUrlHavingHttpsUrlAsParamValueWillReturnFalse() + public function testEnsureSchemeWithProtocolRelativeUrlAndHttpsSchemeWillBeNormalized() { - $this->assertFalse( - BaseUrl::isRelative('https://acme.com/tnt-link=https://tnt.com') - ); + $url = '//acme.com?characters/list'; + $this->assertEquals('https://acme.com?characters/list', BaseUrl::ensureScheme($url, 'https')); } - public function testIsRelativeWithA() + public function testEnsureSchemeWithProtocolRelativeUrlAndEmptySchemeWillBeReturned() { - $this->assertTrue( - BaseUrl::isRelative('/name=bugs.bunny') - ); + $url = '//acme.com?characters/list'; + $this->assertEquals('//acme.com?characters/list', BaseUrl::ensureScheme($url, '')); } - public function testIsRelativeWithFtpProtocolUrlWillReturnFalse() + public function testAbsoluteUrlProtocolAndEmptySchemeWillCreateProtocolRelativeUrl() { - $this->assertFalse( - BaseUrl::isRelative('ftp://ftp.acme.com/tnt-suppliers.txt') - ); + $url = 'https://acme.com?characters/list'; + $this->assertEquals('//acme.com?characters/list', BaseUrl::ensureScheme($url, '')); } - public function testIsRelativeWithHttpUrl() + public function testEnsureSchemeWithAbsoluteUrlWithAnotherUrlAsParamWillReturnInputUrl() { - $this->assertFalse( - BaseUrl::isRelative('http://no-protection.acme.com') - ); + $url = 'ss://acme.com/test?tnt-link=https://tnt.com/'; + $this->assertEquals('https://acme.com/test?tnt-link=https://tnt.com/', BaseUrl::ensureScheme($url, 'https')); } - public function testIsRelativeWithFileUrl() + public function relativeTrueUrlProvider() { - $this->assertFalse( - BaseUrl::isRelative('file:///home/User/2ndFile.html') - ); + return [ + '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', + ] + ]; + } } From d45c1cc6f909df76484360914838ebb90ba72328 Mon Sep 17 00:00:00 2001 From: ggh2e3 Date: Sun, 3 Dec 2023 16:18:04 +0100 Subject: [PATCH 3/5] #17191: improved ensureScheme tests readability --- tests/framework/helpers/BaseUrlTest.php | 76 +++++++++++++------------ 1 file changed, 40 insertions(+), 36 deletions(-) diff --git a/tests/framework/helpers/BaseUrlTest.php b/tests/framework/helpers/BaseUrlTest.php index ecc76fec787..8967d6154c7 100644 --- a/tests/framework/helpers/BaseUrlTest.php +++ b/tests/framework/helpers/BaseUrlTest.php @@ -22,47 +22,51 @@ public function testIsRelativeWillReturnFalse($url) $this->assertFalse(BaseUrl::isRelative($url)); } - public function testEnsureSchemeWithRelativeUrlWillReturnInputUrl() + /** @dataProvider ensureSchemeUrlProvider */ + public function testEnsureScheme() { - $url = 'acme.com?name=bugs.bunny'; - $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 testEnsureSchemeWithSchemeNotAStringWillReturnInputUrl() - { - $url = 'acme.com?name=bugs.bunny'; - $this->assertEquals('acme.com?name=bugs.bunny', BaseUrl::ensureScheme($url, 123)); - } - - public function testEnsureSchemeWithProtocolRelativeUrlAndHttpsSchemeWillBeNormalized() - { - $url = '//acme.com?characters/list'; - $this->assertEquals('https://acme.com?characters/list', BaseUrl::ensureScheme($url, 'https')); - } - public function testEnsureSchemeWithProtocolRelativeUrlAndEmptySchemeWillBeReturned() - { - $url = '//acme.com?characters/list'; - $this->assertEquals('//acme.com?characters/list', BaseUrl::ensureScheme($url, '')); } - public function testAbsoluteUrlProtocolAndEmptySchemeWillCreateProtocolRelativeUrl() + public function ensureSchemeUrlProvider() { - $url = 'https://acme.com?characters/list'; - $this->assertEquals('//acme.com?characters/list', BaseUrl::ensureScheme($url, '')); - } - - public function testEnsureSchemeWithAbsoluteUrlWithAnotherUrlAsParamWillReturnInputUrl() - { - $url = 'ss://acme.com/test?tnt-link=https://tnt.com/'; - $this->assertEquals('https://acme.com/test?tnt-link=https://tnt.com/', BaseUrl::ensureScheme($url, 'https')); + return [ + 'relative url and https scheme will return input url' => [ + 'url' => 'acme.com?name=bugs.bunny', + 'scheme' => 'https', + 'expected result' => 'acme.com?name=bugs.bunny', + ], + 'relative url and another url as parameter will return input url' => [ + 'url' => 'acme.com/test?tnt-link=https://tnt.com/', + 'scheme' => 'https', + 'expected' => 'acme.com/test?tnt-link=https://tnt.com/', + ], + 'url with scheme not a string will return input url' => [ + 'url' => 'acme.com?name=bugs.bunny', + 'scheme' => 123, + 'expected' => 'acme.com?name=bugs.bunny', + ], + 'protocol relative url and https scheme will be processed' => [ + 'url' => '//acme.com?characters/list', + 'scheme' => 'https', + 'expected' => 'https://acme.com?characters/list', + ], + 'protocol relative url and empty scheme will be returned' => [ + 'url' => '//acme.com?characters/list', + 'scheme' => '', + 'expected' => '//acme.com?characters/list', + ], + 'absolute url and empty scheme will create protocol relative url' => [ + 'url' => 'https://acme.com?characters/list', + 'scheme' => '', + 'expected' => '//acme.com?characters/list', + ], + 'absolute url and different scheme will be processed' => [ + 'url' => 'http://acme.com/test?tnt-link=https://tnt.com/', + 'scheme' => 'https', + 'expected' => 'https://acme.com/test?tnt-link=https://tnt.com/', + ] + ]; } public function relativeTrueUrlProvider() From 005a9c72652b721c0139fb77af31838c7b8d727e Mon Sep 17 00:00:00 2001 From: ggh2e3 Date: Sun, 3 Dec 2023 16:21:45 +0100 Subject: [PATCH 4/5] #17191: improved ensureScheme tests readability --- tests/framework/helpers/BaseUrlTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/framework/helpers/BaseUrlTest.php b/tests/framework/helpers/BaseUrlTest.php index 8967d6154c7..9bcd39ef981 100644 --- a/tests/framework/helpers/BaseUrlTest.php +++ b/tests/framework/helpers/BaseUrlTest.php @@ -23,9 +23,9 @@ public function testIsRelativeWillReturnFalse($url) } /** @dataProvider ensureSchemeUrlProvider */ - public function testEnsureScheme() + public function testEnsureScheme($url, $scheme, $expected) { - + $this->assertEquals($expected, BaseUrl::ensureScheme($url, $scheme)); } public function ensureSchemeUrlProvider() From 76f17336fe30ea604e5e9af4f2f9dcd1a8fc8411 Mon Sep 17 00:00:00 2001 From: ggh2e3 Date: Sun, 3 Dec 2023 16:27:39 +0100 Subject: [PATCH 5/5] #17191: updated changelog --- framework/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 6645863bfd7..0a95132681a 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,6 +4,7 @@ Yii Framework 2 Change Log 2.0.50 under development ------------------------ +- Bug #17191: Fixed `BaseUrl::isRelative($url)` method in `yii\helpers\BaseUrl` (ggh2e3) - Bug #18469: Fixed `Link::serialize(array $links)` method in `yii\web\Link` (ggh2e3) - Bug #20040: Fix type `boolean` in `MSSQL` (terabytesoftw) - Bug #20005: Fix `yii\console\controllers\ServeController` to specify the router script (terabytesoftw)