Debian Codegraph @

tests/phpunit/unit/includes/utils/UrlUtilsTest.php

raw symbols SCIP
1 <?php
2 use MediaWiki\Utils\UrlUtils;
3
4 /**
5 * @coversDefaultClass \MediaWiki\Utils\UrlUtils
6 * @covers ::__construct
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 * @covers ::expand
18 * @dataProvider UrlUtilsProviders::provideExpandException
19 * @param array $options
20 * @param string|int|null $defaultProto
21 * @param string $expectedClass Expected class of exception
22 * @param string $expectedMsg Expected exception message
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 * @covers ::expand
36 * @dataProvider UrlUtilsProviders::provideExpand
37 * @param string $input
38 * @param array $options
39 * @param string|int|null $defaultProto
40 * @param ?string $expected
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 * @covers ::getServer
51 * @dataProvider UrlUtilsProviders::provideGetServer
52 * @param array $options
53 * @param string|int|null $defaultProto
54 * @param string $expected
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 * @covers ::assemble
63 * @dataProvider UrlUtilsProviders::provideAssemble
64 * @param array $bits
65 * @param string $expected
66 */
67 public function testAssemble( array $bits, string $expected ): void {
68 $this->assertSame( $expected, UrlUtils::assemble( $bits ) );
69 }
70
71 /**
72 * @covers ::removeDotSegments
73 * @dataProvider UrlUtilsProviders::provideRemoveDotSegments
74 * @param string $input
75 * @param string $expected
76 */
77 public function testRemoveDotSegments( string $input, string $expected ): void {
78 $this->assertSame( $expected, UrlUtils::removeDotSegments( $input ) );
79 }
80
81 /**
82 * @covers ::validProtocols
83 * @covers ::validAbsoluteProtocols
84 * @covers ::validProtocolsInternal
85 * @dataProvider UrlUtilsProviders::provideValidProtocols
86 * @param string $method 'validProtocols' or 'validAbsoluteProtocols'
87 * @param array|string $validProtocols Value of option passed to UrlUtils
88 * @param string $expected
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 * @covers ::parse
101 * @dataProvider UrlUtilsProviders::provideParse
102 * @param string $url
103 * @param ?array $expected
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 * @covers ::expandIRI
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 * @covers ::matchesDomainList
138 * @dataProvider UrlUtilsProviders::provideMatchesDomainList
139 * @param string $url
140 * @param array $domains
141 * @param bool $expected
142 */
143 public function testMatchesDomainList( string $url, array $domains, bool $expected ): void {
144 $this->assertSame( $expected, ( new UrlUtils )->matchesDomainList( $url, $domains ) );
145 }
146
147 }