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 $this->setMwGlobals( [
15 'wgServer' => $server,
16 'wgCanonicalServer' => $canServer,
17 'wgRequest' => new FauxRequest( [], false, null, $httpsMode ?
'https' : 'http' ),
18 'wgHttpsPort' => $httpsPort
21 $this->assertEquals( $fullUrl, wfExpandUrl( $shortUrl, $defaultProto ), $message );
25 * Provider of URL examples for testing wfExpandUrl()
29 public static function provideExpandableUrls() {
30 $modes = [ 'http', 'https' ];
32 'http' => 'http://example.com',
33 'https' => 'https://example.com',
34 'protocol-relative' => '//example.com'
38 'https' => PROTO_HTTPS
,
39 'protocol-relative' => PROTO_RELATIVE
,
40 'current' => PROTO_CURRENT
,
41 'canonical' => PROTO_CANONICAL
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 ) {
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 )"
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 )"
65 # Would be nice to support this, see fixme on wfExpandUrl()
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 )"
74 // Determine expected protocol
75 if ( $protoDesc == 'protocol-relative' ) {
77 } elseif ( $protoDesc == 'current' ) {
79 } elseif ( $protoDesc == 'canonical' ) {
80 $p = "$canServerMode:";
82 $p = $protoDesc . ':';
84 // Determine expected server name
85 if ( $protoDesc == 'canonical' ) {
87 } elseif ( $serverDesc == 'protocol-relative' ) {
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 )"
108 "Testing expanding URL beginning with / (defaultProto: $protoDesc, "
109 . "wgServer: $server, wgCanonicalServer: $canServer, "
110 . "current request protocol: $mode )"
117 // Don't add HTTPS port to foreign URLs
119 'https://foreign.example.com/foo',
120 'https://foreign.example.com/foo',
122 '//wiki.example.com',
123 'http://wiki.example.com',
126 "Don't add HTTPS port to foreign URLs"
129 'https://foreign.example.com:222/foo',
130 'https://foreign.example.com:222/foo',
132 '//wiki.example.com',
133 'http://wiki.example.com',
136 "Don't overwrite HTTPS port of foreign URLs"
138 // Do add HTTPS port to local URLs
140 'https://wiki.example.com:111/foo',
143 '//wiki.example.com',
144 'http://wiki.example.com',
147 "Do add HTTPS port to protocol-relative URLs"