| 1 |
<?php |
| 2 |
use MediaWiki\Utils\UrlUtils; |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
class UrlUtilsTest extends MediaWikiUnitTestCase { |
| 9 |
|
| 10 |
public function testConstructError(): void { |
| 11 |
$this->expectException( InvalidArgumentException::class ); |
| 12 |
$this->expectExceptionMessage( 'Unrecognized option "unrecognized"' ); |
| 13 |
new UrlUtils( [ 'unrecognized' => true ] ); |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public function testExpandException( |
| 25 |
array $options, $defaultProto, string $expectedClass, string $expectedMsg |
| 26 |
): void { |
| 27 |
$this->expectException( $expectedClass ); |
| 28 |
$this->expectExceptionMessage( $expectedMsg ); |
| 29 |
|
| 30 |
$urlUtils = new UrlUtils( $options ); |
| 31 |
$urlUtils->expand( '/', $defaultProto ); |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public function testExpand( |
| 43 |
string $input, array $options, $defaultProto, ?string $expected |
| 44 |
): void { |
| 45 |
$urlUtils = new UrlUtils( $options ); |
| 46 |
$this->assertSame( $expected, $urlUtils->expand( $input, $defaultProto ) ); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public function testGetServer( array $options, $defaultProto, string $expected ): void { |
| 57 |
$urlUtils = new UrlUtils( $options ); |
| 58 |
$this->assertSame( $expected, $urlUtils->getServer( $defaultProto ) ); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function testAssemble( array $bits, string $expected ): void { |
| 68 |
$this->assertSame( $expected, UrlUtils::assemble( $bits ) ); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function testRemoveDotSegments( string $input, string $expected ): void { |
| 78 |
$this->assertSame( $expected, UrlUtils::removeDotSegments( $input ) ); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public function testValidProtocols( string $method, $validProtocols, string $expected ): void { |
| 91 |
if ( !is_array( $validProtocols ) ) { |
| 92 |
$this->expectDeprecationAndContinue( |
| 93 |
'/Use of \$wgUrlProtocols that is not an array was deprecated in MediaWiki 1\.39/' ); |
| 94 |
} |
| 95 |
$urlUtils = new UrlUtils( [ UrlUtils::VALID_PROTOCOLS => $validProtocols ] ); |
| 96 |
$this->assertSame( $expected, $urlUtils->$method() ); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function testParse( string $url, ?array $expected ): void { |
| 106 |
$urlUtils = new UrlUtils( [ UrlUtils::VALID_PROTOCOLS => [ |
| 107 |
'//', |
| 108 |
'http://', |
| 109 |
'https://', |
| 110 |
'file://', |
| 111 |
'mailto:', |
| 112 |
'news:', |
| 113 |
] ] ); |
| 114 |
$actual = $urlUtils->parse( $url ); |
| 115 |
if ( $expected ) { |
| 116 |
ksort( $expected ); |
| 117 |
} |
| 118 |
if ( $actual ) { |
| 119 |
ksort( $actual ); |
| 120 |
} |
| 121 |
$this->assertSame( $expected, $actual ); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public function testExpandIRI(): void { |
| 128 |
$this->assertSame( "https://te.wikibooks.org/wiki/ఉబుంటు_వాడుకరి_మార్గదర్శని", |
| 129 |
( new UrlUtils )->expandIRI( "https://te.wikibooks.org/wiki/" |
| 130 |
. "%E0%B0%89%E0%B0%AC%E0%B1%81%E0%B0%82%E0%B0%9F%E0%B1%81_" |
| 131 |
. "%E0%B0%B5%E0%B0%BE%E0%B0%A1%E0%B1%81%E0%B0%95%E0%B0%B0%E0%B0%BF_" |
| 132 |
. "%E0%B0%AE%E0%B0%BE%E0%B0%B0%E0%B1%8D%E0%B0%97%E0%B0%A6%E0%B0%B0" |
| 133 |
. "%E0%B1%8D%E0%B0%B6%E0%B0%A8%E0%B0%BF" ) ); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public function testMatchesDomainList( string $url, array $domains, bool $expected ): void { |
| 144 |
$this->assertSame( $expected, ( new UrlUtils )->matchesDomainList( $url, $domains ) ); |
| 145 |
} |
| 146 |
|
| 147 |
} |