$wgHttpsPort should only be used in very special cases
[lhc/web/wiklou.git] / tests / phpunit / includes / GlobalFunctions / wfExpandUrlTest.php
1 <?php
2 /**
3 * @group GlobalFunctions
4 * @covers ::wfExpandUrl
5 */
6 class WfExpandUrlTest extends MediaWikiTestCase {
7 /**
8 * @dataProvider provideExpandableUrls
9 */
10 public function testWfExpandUrl( $fullUrl, $shortUrl, $defaultProto,
11 $server, $canServer, $httpsMode, $httpsPort, $message
12 ) {
13 // Fake $wgServer, $wgCanonicalServer and $wgRequest->getProtocol()
14 $this->setMwGlobals( [
15 'wgServer' => $server,
16 'wgCanonicalServer' => $canServer,
17 'wgRequest' => new FauxRequest( [], false, null, $httpsMode ? 'https' : 'http' ),
18 'wgHttpsPort' => $httpsPort
19 ] );
20
21 $this->assertEquals( $fullUrl, wfExpandUrl( $shortUrl, $defaultProto ), $message );
22 }
23
24 /**
25 * Provider of URL examples for testing wfExpandUrl()
26 *
27 * @return array
28 */
29 public static function provideExpandableUrls() {
30 $modes = [ 'http', 'https' ];
31 $servers = [
32 'http' => 'http://example.com',
33 'https' => 'https://example.com',
34 'protocol-relative' => '//example.com'
35 ];
36 $defaultProtos = [
37 'http' => PROTO_HTTP,
38 'https' => PROTO_HTTPS,
39 'protocol-relative' => PROTO_RELATIVE,
40 'current' => PROTO_CURRENT,
41 'canonical' => PROTO_CANONICAL
42 ];
43
44 $retval = [];
45 foreach ( $modes as $mode ) {
46 $httpsMode = $mode == 'https';
47 foreach ( $servers as $serverDesc => $server ) {
48 foreach ( $modes as $canServerMode ) {
49 $canServer = "$canServerMode://example2.com";
50 foreach ( $defaultProtos as $protoDesc => $defaultProto ) {
51 $retval[] = [
52 'http://example.com', 'http://example.com',
53 $defaultProto, $server, $canServer, $httpsMode, 443,
54 "Testing fully qualified http URLs (no need to expand) "
55 . "(defaultProto: $protoDesc , wgServer: $server, "
56 . "wgCanonicalServer: $canServer, current request protocol: $mode )"
57 ];
58 $retval[] = [
59 'https://example.com', 'https://example.com',
60 $defaultProto, $server, $canServer, $httpsMode, 443,
61 "Testing fully qualified https URLs (no need to expand) "
62 . "(defaultProto: $protoDesc , wgServer: $server, "
63 . "wgCanonicalServer: $canServer, current request protocol: $mode )"
64 ];
65 # Would be nice to support this, see fixme on wfExpandUrl()
66 $retval[] = [
67 "wiki/FooBar", 'wiki/FooBar',
68 $defaultProto, $server, $canServer, $httpsMode, 443,
69 "Test non-expandable relative URLs (defaultProto: $protoDesc, "
70 . "wgServer: $server, wgCanonicalServer: $canServer, "
71 . "current request protocol: $mode )"
72 ];
73
74 // Determine expected protocol
75 if ( $protoDesc == 'protocol-relative' ) {
76 $p = '';
77 } elseif ( $protoDesc == 'current' ) {
78 $p = "$mode:";
79 } elseif ( $protoDesc == 'canonical' ) {
80 $p = "$canServerMode:";
81 } else {
82 $p = $protoDesc . ':';
83 }
84 // Determine expected server name
85 if ( $protoDesc == 'canonical' ) {
86 $srv = $canServer;
87 } elseif ( $serverDesc == 'protocol-relative' ) {
88 $srv = $p . $server;
89 } else {
90 $srv = $server;
91 }
92
93 $retval[] = [
94 "$p//wikipedia.org", '//wikipedia.org',
95 $defaultProto, $server, $canServer, $httpsMode, 443,
96 "Test protocol-relative URL (defaultProto: $protoDesc, "
97 . "wgServer: $server, wgCanonicalServer: $canServer, "
98 . "current request protocol: $mode )"
99 ];
100 $retval[] = [
101 "$srv/wiki/FooBar",
102 '/wiki/FooBar',
103 $defaultProto,
104 $server,
105 $canServer,
106 $httpsMode,
107 443,
108 "Testing expanding URL beginning with / (defaultProto: $protoDesc, "
109 . "wgServer: $server, wgCanonicalServer: $canServer, "
110 . "current request protocol: $mode )"
111 ];
112 }
113 }
114 }
115 }
116
117 // Don't add HTTPS port to foreign URLs
118 $retval[] = [
119 'https://foreign.example.com/foo',
120 'https://foreign.example.com/foo',
121 PROTO_HTTPS,
122 '//wiki.example.com',
123 'http://wiki.example.com',
124 'https',
125 111,
126 "Don't add HTTPS port to foreign URLs"
127 ];
128 $retval[] = [
129 'https://foreign.example.com:222/foo',
130 'https://foreign.example.com:222/foo',
131 PROTO_HTTPS,
132 '//wiki.example.com',
133 'http://wiki.example.com',
134 'https',
135 111,
136 "Don't overwrite HTTPS port of foreign URLs"
137 ];
138 // Do add HTTPS port to local URLs
139 $retval[] = [
140 'https://wiki.example.com:111/foo',
141 '/foo',
142 PROTO_HTTPS,
143 '//wiki.example.com',
144 'http://wiki.example.com',
145 'https',
146 111,
147 "Do add HTTPS port to protocol-relative URLs"
148 ];
149
150 return $retval;
151 }
152 }