3 * Tests for wfExpandUrl()
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;
13 $wgCanonicalServer = $canServer;
15 // Fake $_SERVER['HTTPS'] if needed
17 $_SERVER['HTTPS'] = 'on';
19 unset( $_SERVER['HTTPS'] );
22 $this->assertEquals( $fullUrl, wfExpandUrl( $shortUrl, $defaultProto ), $message );
24 // Restore $wgServer and $wgCanonicalServer
25 $wgServer = $oldServer;
26 $wgCanonicalServer = $oldCanServer;
30 * Provider of URL examples for testing wfExpandUrl()
34 public static function provideExpandableUrls() {
35 $modes = array( 'http', 'https' );
37 'http' => 'http://example.com',
38 'https' => 'https://example.com',
39 'protocol-relative' => '//example.com'
41 $defaultProtos = array(
43 'https' => PROTO_HTTPS
,
44 'protocol-relative' => PROTO_RELATIVE
,
45 'current' => PROTO_CURRENT
,
46 'canonical' => PROTO_CANONICAL
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 ) {
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 )"
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 )"
68 # Would be nice to support this, see fixme on wfExpandUrl()
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 )"
76 // Determine expected protocol
77 if ( $protoDesc == 'protocol-relative' ) {
79 } elseif ( $protoDesc == 'current' ) {
81 } elseif ( $protoDesc == 'canonical' ) {
82 $p = "$canServerMode:";
84 $p = $protoDesc . ':';
86 // Determine expected server name
87 if ( $protoDesc == 'canonical' ) {
89 } elseif ( $serverDesc == 'protocol-relative' ) {
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 )"
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 )"