MediaWiki REL1_32
WfExpandUrlTest.php
Go to the documentation of this file.
1<?php
10 public function testWfExpandUrl( $fullUrl, $shortUrl, $defaultProto,
11 $server, $canServer, $httpsMode, $httpsPort, $message
12 ) {
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
20 ] );
21
22 $this->assertEquals( $fullUrl, wfExpandUrl( $shortUrl, $defaultProto ), $message );
23 }
24
30 public static function provideExpandableUrls() {
31 $modes = [ 'http', 'https' ];
32 $servers = [
33 'http' => 'http://example.com',
34 'https' => 'https://example.com',
35 'protocol-relative' => '//example.com'
36 ];
37 $defaultProtos = [
38 'http' => PROTO_HTTP,
39 'https' => PROTO_HTTPS,
40 'protocol-relative' => PROTO_RELATIVE,
41 'current' => PROTO_CURRENT,
42 'canonical' => PROTO_CANONICAL
43 ];
44
45 $retval = [];
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 ) {
52 $retval[] = [
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 )"
58 ];
59 $retval[] = [
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 )"
65 ];
66 # Would be nice to support this, see fixme on wfExpandUrl()
67 $retval[] = [
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 )"
73 ];
74
75 // Determine expected protocol
76 if ( $protoDesc == 'protocol-relative' ) {
77 $p = '';
78 } elseif ( $protoDesc == 'current' ) {
79 $p = "$mode:";
80 } elseif ( $protoDesc == 'canonical' ) {
81 $p = "$canServerMode:";
82 } else {
83 $p = $protoDesc . ':';
84 }
85 // Determine expected server name
86 if ( $protoDesc == 'canonical' ) {
87 $srv = $canServer;
88 } elseif ( $serverDesc == 'protocol-relative' ) {
89 $srv = $p . $server;
90 } else {
91 $srv = $server;
92 }
93
94 $retval[] = [
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 )"
100 ];
101 $retval[] = [
102 "$srv/wiki/FooBar",
103 '/wiki/FooBar',
104 $defaultProto,
105 $server,
106 $canServer,
107 $httpsMode,
108 443,
109 "Testing expanding URL beginning with / (defaultProto: $protoDesc, "
110 . "wgServer: $server, wgCanonicalServer: $canServer, "
111 . "current request protocol: $mode )"
112 ];
113 }
114 }
115 }
116 }
117
118 // Don't add HTTPS port to foreign URLs
119 $retval[] = [
120 'https://foreign.example.com/foo',
121 'https://foreign.example.com/foo',
123 '//wiki.example.com',
124 'http://wiki.example.com',
125 'https',
126 111,
127 "Don't add HTTPS port to foreign URLs"
128 ];
129 $retval[] = [
130 'https://foreign.example.com:222/foo',
131 'https://foreign.example.com:222/foo',
133 '//wiki.example.com',
134 'http://wiki.example.com',
135 'https',
136 111,
137 "Don't overwrite HTTPS port of foreign URLs"
138 ];
139 // Do add HTTPS port to local URLs
140 $retval[] = [
141 'https://wiki.example.com:111/foo',
142 '/foo',
144 '//wiki.example.com',
145 'http://wiki.example.com',
146 'https',
147 111,
148 "Do add HTTPS port to protocol-relative URLs"
149 ];
150
151 return $retval;
152 }
153}
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
WebRequest clone which takes values from a provided array.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
GlobalFunctions wfExpandUrl.
testWfExpandUrl( $fullUrl, $shortUrl, $defaultProto, $server, $canServer, $httpsMode, $httpsPort, $message)
provideExpandableUrls
static provideExpandableUrls()
Provider of URL examples for testing wfExpandUrl()
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account incomplete not yet checked for validity & $retval
Definition hooks.txt:266
const PROTO_CANONICAL
Definition Defines.php:223
const PROTO_HTTPS
Definition Defines.php:220
const PROTO_CURRENT
Definition Defines.php:222
const PROTO_HTTP
Definition Defines.php:219
const PROTO_RELATIVE
Definition Defines.php:221