|
| 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