Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / GlobalFunctions / wfExpandUrlTest.php
blob1cd320fa00476f1975a88a939880f3481648c929
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, $message
12 ) {
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 ] );
20 $this->assertEquals( $fullUrl, wfExpandUrl( $shortUrl, $defaultProto ), $message );
23 /**
24 * Provider of URL examples for testing wfExpandUrl()
26 * @return array
28 public static function provideExpandableUrls() {
29 $modes = [ 'http', 'https' ];
30 $servers = [
31 'http' => 'http://example.com',
32 'https' => 'https://example.com',
33 'protocol-relative' => '//example.com'
35 $defaultProtos = [
36 'http' => PROTO_HTTP,
37 'https' => PROTO_HTTPS,
38 'protocol-relative' => PROTO_RELATIVE,
39 'current' => PROTO_CURRENT,
40 'canonical' => PROTO_CANONICAL
43 $retval = [];
44 foreach ( $modes as $mode ) {
45 $httpsMode = $mode == 'https';
46 foreach ( $servers as $serverDesc => $server ) {
47 foreach ( $modes as $canServerMode ) {
48 $canServer = "$canServerMode://example2.com";
49 foreach ( $defaultProtos as $protoDesc => $defaultProto ) {
50 $retval[] = [
51 'http://example.com', 'http://example.com',
52 $defaultProto, $server, $canServer, $httpsMode,
53 "Testing fully qualified http URLs (no need to expand) "
54 . "(defaultProto: $protoDesc , wgServer: $server, "
55 . "wgCanonicalServer: $canServer, current request protocol: $mode )"
57 $retval[] = [
58 'https://example.com', 'https://example.com',
59 $defaultProto, $server, $canServer, $httpsMode,
60 "Testing fully qualified https URLs (no need to expand) "
61 . "(defaultProto: $protoDesc , wgServer: $server, "
62 . "wgCanonicalServer: $canServer, current request protocol: $mode )"
64 # Would be nice to support this, see fixme on wfExpandUrl()
65 $retval[] = [
66 "wiki/FooBar", 'wiki/FooBar',
67 $defaultProto, $server, $canServer, $httpsMode,
68 "Test non-expandable relative URLs (defaultProto: $protoDesc, "
69 . "wgServer: $server, wgCanonicalServer: $canServer, "
70 . "current request protocol: $mode )"
73 // Determine expected protocol
74 if ( $protoDesc == 'protocol-relative' ) {
75 $p = '';
76 } elseif ( $protoDesc == 'current' ) {
77 $p = "$mode:";
78 } elseif ( $protoDesc == 'canonical' ) {
79 $p = "$canServerMode:";
80 } else {
81 $p = $protoDesc . ':';
83 // Determine expected server name
84 if ( $protoDesc == 'canonical' ) {
85 $srv = $canServer;
86 } elseif ( $serverDesc == 'protocol-relative' ) {
87 $srv = $p . $server;
88 } else {
89 $srv = $server;
92 $retval[] = [
93 "$p//wikipedia.org", '//wikipedia.org',
94 $defaultProto, $server, $canServer, $httpsMode,
95 "Test protocol-relative URL (defaultProto: $protoDesc, "
96 . "wgServer: $server, wgCanonicalServer: $canServer, "
97 . "current request protocol: $mode )"
99 $retval[] = [
100 "$srv/wiki/FooBar",
101 '/wiki/FooBar',
102 $defaultProto,
103 $server,
104 $canServer,
105 $httpsMode,
106 "Testing expanding URL beginning with / (defaultProto: $protoDesc, "
107 . "wgServer: $server, wgCanonicalServer: $canServer, "
108 . "current request protocol: $mode )"
115 return $retval;