Merge "cleanup action=tokens"
[lhc/web/wiklou.git] / tests / phpunit / includes / GlobalFunctions / wfExpandUrlTest.php
1 <?php
2 /**
3 * Tests for wfExpandUrl()
4 */
5 class WfExpandUrlTest extends MediaWikiTestCase {
6 /** @dataProvider provideExpandableUrls */
7 public function testWfExpandUrl( $fullUrl, $shortUrl, $defaultProto, $server, $canServer, $httpsMode, $message ) {
8 // Fake $wgServer and $wgCanonicalServer
9 global $wgServer, $wgCanonicalServer;
10 $oldServer = $wgServer;
11 $oldCanServer = $wgCanonicalServer;
12 $wgServer = $server;
13 $wgCanonicalServer = $canServer;
14
15 // Fake $_SERVER['HTTPS'] if needed
16 if ( $httpsMode ) {
17 $_SERVER['HTTPS'] = 'on';
18 } else {
19 unset( $_SERVER['HTTPS'] );
20 }
21
22 $this->assertEquals( $fullUrl, wfExpandUrl( $shortUrl, $defaultProto ), $message );
23
24 // Restore $wgServer and $wgCanonicalServer
25 $wgServer = $oldServer;
26 $wgCanonicalServer = $oldCanServer;
27 }
28
29 /**
30 * Provider of URL examples for testing wfExpandUrl()
31 *
32 * @return array
33 */
34 public static function provideExpandableUrls() {
35 $modes = array( 'http', 'https' );
36 $servers = array(
37 'http' => 'http://example.com',
38 'https' => 'https://example.com',
39 'protocol-relative' => '//example.com'
40 );
41 $defaultProtos = array(
42 'http' => PROTO_HTTP,
43 'https' => PROTO_HTTPS,
44 'protocol-relative' => PROTO_RELATIVE,
45 'current' => PROTO_CURRENT,
46 'canonical' => PROTO_CANONICAL
47 );
48
49 $retval = array();
50 foreach ( $modes as $mode ) {
51 $httpsMode = $mode == 'https';
52 foreach ( $servers as $serverDesc => $server ) {
53 foreach ( $modes as $canServerMode ) {
54 $canServer = "$canServerMode://example2.com";
55 foreach ( $defaultProtos as $protoDesc => $defaultProto ) {
56 $retval[] = array(
57 'http://example.com', 'http://example.com',
58 $defaultProto, $server, $canServer, $httpsMode,
59 "Testing fully qualified http URLs (no need to expand) ' .
60 '(defaultProto: $protoDesc , wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )"
61 );
62 $retval[] = array(
63 'https://example.com', 'https://example.com',
64 $defaultProto, $server, $canServer, $httpsMode,
65 "Testing fully qualified https URLs (no need to expand) ' .
66 '(defaultProto: $protoDesc , wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )"
67 );
68 # Would be nice to support this, see fixme on wfExpandUrl()
69 $retval[] = array(
70 "wiki/FooBar", 'wiki/FooBar',
71 $defaultProto, $server, $canServer, $httpsMode,
72 "Test non-expandable relative URLs ' .
73 '(defaultProto: $protoDesc , wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )"
74 );
75
76 // Determine expected protocol
77 if ( $protoDesc == 'protocol-relative' ) {
78 $p = '';
79 } elseif ( $protoDesc == 'current' ) {
80 $p = "$mode:";
81 } elseif ( $protoDesc == 'canonical' ) {
82 $p = "$canServerMode:";
83 } else {
84 $p = $protoDesc . ':';
85 }
86 // Determine expected server name
87 if ( $protoDesc == 'canonical' ) {
88 $srv = $canServer;
89 } elseif ( $serverDesc == 'protocol-relative' ) {
90 $srv = $p . $server;
91 } else {
92 $srv = $server;
93 }
94
95 $retval[] = array(
96 "$p//wikipedia.org", '//wikipedia.org',
97 $defaultProto, $server, $canServer, $httpsMode,
98 "Test protocol-relative URL ' .
99 '(defaultProto: $protoDesc, wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )"
100 );
101 $retval[] = array(
102 "$srv/wiki/FooBar", '/wiki/FooBar',
103 $defaultProto, $server, $canServer, $httpsMode,
104 "Testing expanding URL beginning with / ' .
105 '(defaultProto: $protoDesc , wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )"
106 );
107 }
108 }
109 }
110 }
111 return $retval;
112 }
113 }