Do not insert page titles into querycache.qc_value
[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 // 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
25 /**
26 * Provider of URL examples for testing wfExpandUrl()
27 *
28 * @return array
29 */
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',
122 PROTO_HTTPS,
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',
132 PROTO_HTTPS,
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',
143 PROTO_HTTPS,
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 }