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