MediaWiki  master
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',
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 }
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
const PROTO_CURRENT
Definition: Defines.php:202
GlobalFunctions wfExpandUrl.
const PROTO_HTTPS
Definition: Defines.php:200
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:773
const PROTO_RELATIVE
Definition: Defines.php:201
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
const PROTO_HTTP
Definition: Defines.php:199
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
const PROTO_CANONICAL
Definition: Defines.php:203
static provideExpandableUrls()
Provider of URL examples for testing wfExpandUrl()
testWfExpandUrl( $fullUrl, $shortUrl, $defaultProto, $server, $canServer, $httpsMode, $httpsPort, $message)
provideExpandableUrls