3 * @group GlobalFunctions
4 * @covers ::wfExpandUrl
6 class WfExpandUrlTest
extends MediaWikiTestCase
{
8 * @dataProvider provideExpandableUrls
10 public function testWfExpandUrl( $fullUrl, $shortUrl, $defaultProto,
11 $server, $canServer, $httpsMode, $httpsPort, $message
13 // Fake $wgServer, $wgCanonicalServer and $wgRequest->getProtocol()
14 // fake edit to fake globals
15 $this->setMwGlobals( [
16 'wgServer' => $server,
17 'wgCanonicalServer' => $canServer,
18 'wgRequest' => new FauxRequest( [], false, null, $httpsMode ?
'https' : 'http' ),
19 'wgHttpsPort' => $httpsPort
22 $this->assertEquals( $fullUrl, wfExpandUrl( $shortUrl, $defaultProto ), $message );
26 * Provider of URL examples for testing wfExpandUrl()
30 public static function provideExpandableUrls() {
31 $modes = [ 'http', 'https' ];
33 'http' => 'http://example.com',
34 'https' => 'https://example.com',
35 'protocol-relative' => '//example.com'
39 'https' => PROTO_HTTPS
,
40 'protocol-relative' => PROTO_RELATIVE
,
41 'current' => PROTO_CURRENT
,
42 'canonical' => PROTO_CANONICAL
46 foreach ( $modes as $mode ) {
47 $httpsMode = $mode == 'https';
48 foreach ( $servers as $serverDesc => $server ) {
49 foreach ( $modes as $canServerMode ) {
50 $canServer = "$canServerMode://example2.com";
51 foreach ( $defaultProtos as $protoDesc => $defaultProto ) {
53 'http://example.com', 'http://example.com',
54 $defaultProto, $server, $canServer, $httpsMode, 443,
55 "Testing fully qualified http URLs (no need to expand) "
56 . "(defaultProto: $protoDesc , wgServer: $server, "
57 . "wgCanonicalServer: $canServer, current request protocol: $mode )"
60 'https://example.com', 'https://example.com',
61 $defaultProto, $server, $canServer, $httpsMode, 443,
62 "Testing fully qualified https URLs (no need to expand) "
63 . "(defaultProto: $protoDesc , wgServer: $server, "
64 . "wgCanonicalServer: $canServer, current request protocol: $mode )"
66 # Would be nice to support this, see fixme on wfExpandUrl()
68 "wiki/FooBar", 'wiki/FooBar',
69 $defaultProto, $server, $canServer, $httpsMode, 443,
70 "Test non-expandable relative URLs (defaultProto: $protoDesc, "
71 . "wgServer: $server, wgCanonicalServer: $canServer, "
72 . "current request protocol: $mode )"
75 // Determine expected protocol
76 if ( $protoDesc == 'protocol-relative' ) {
78 } elseif ( $protoDesc == 'current' ) {
80 } elseif ( $protoDesc == 'canonical' ) {
81 $p = "$canServerMode:";
83 $p = $protoDesc . ':';
85 // Determine expected server name
86 if ( $protoDesc == 'canonical' ) {
88 } elseif ( $serverDesc == 'protocol-relative' ) {
95 "$p//wikipedia.org", '//wikipedia.org',
96 $defaultProto, $server, $canServer, $httpsMode, 443,
97 "Test protocol-relative URL (defaultProto: $protoDesc, "
98 . "wgServer: $server, wgCanonicalServer: $canServer, "
99 . "current request protocol: $mode )"
109 "Testing expanding URL beginning with / (defaultProto: $protoDesc, "
110 . "wgServer: $server, wgCanonicalServer: $canServer, "
111 . "current request protocol: $mode )"
118 // Don't add HTTPS port to foreign URLs
120 'https://foreign.example.com/foo',
121 'https://foreign.example.com/foo',
123 '//wiki.example.com',
124 'http://wiki.example.com',
127 "Don't add HTTPS port to foreign URLs"
130 'https://foreign.example.com:222/foo',
131 'https://foreign.example.com:222/foo',
133 '//wiki.example.com',
134 'http://wiki.example.com',
137 "Don't overwrite HTTPS port of foreign URLs"
139 // Do add HTTPS port to local URLs
141 'https://wiki.example.com:111/foo',
144 '//wiki.example.com',
145 'http://wiki.example.com',
148 "Do add HTTPS port to protocol-relative URLs"