3 * Unit tests for wfExpandUrl()
6 class wfExpandUrl
extends MediaWikiTestCase
{
7 /** @dataProvider provideExpandableUrls */
8 public function testWfExpandUrl( $fullUrl, $shortUrl, $defaultProto, $server, $canServer, $httpsMode, $message ) {
9 // Fake $wgServer and $wgCanonicalServer
10 global $wgServer, $wgCanonicalServer;
11 $oldServer = $wgServer;
12 $oldCanServer = $wgCanonicalServer;
14 $wgCanonicalServer = $canServer;
16 // Fake $_SERVER['HTTPS'] if needed
18 $_SERVER['HTTPS'] = 'on';
20 unset( $_SERVER['HTTPS'] );
23 $this->assertEquals( $fullUrl, wfExpandUrl( $shortUrl, $defaultProto ), $message );
25 // Restore $wgServer and $wgCanonicalServer
26 $wgServer = $oldServer;
27 $wgCanonicalServer = $oldCanServer;
31 * Provider of URL examples for testing wfExpandUrl()
35 public static function provideExpandableUrls() {
36 $modes = array( 'http', 'https' );
38 'http' => 'http://example.com',
39 'https' => 'https://example.com',
40 'protocol-relative' => '//example.com'
42 $defaultProtos = array(
44 'https' => PROTO_HTTPS
,
45 'protocol-relative' => PROTO_RELATIVE
,
46 'current' => PROTO_CURRENT
,
47 'canonical' => PROTO_CANONICAL
51 foreach ( $modes as $mode ) {
52 $httpsMode = $mode == 'https';
53 foreach ( $servers as $serverDesc => $server ) {
54 foreach ( $modes as $canServerMode ) {
55 $canServer = "$canServerMode://example2.com";
56 foreach ( $defaultProtos as $protoDesc => $defaultProto ) {
58 'http://example.com', 'http://example.com',
59 $defaultProto, $server, $canServer, $httpsMode,
60 "Testing fully qualified http URLs (no need to expand) ' .
61 '(defaultProto: $protoDesc , wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )"
64 'https://example.com', 'https://example.com',
65 $defaultProto, $server, $canServer, $httpsMode,
66 "Testing fully qualified https URLs (no need to expand) ' .
67 '(defaultProto: $protoDesc , wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )"
69 # Would be nice to support this, see fixme on wfExpandUrl()
71 "wiki/FooBar", 'wiki/FooBar',
72 $defaultProto, $server, $canServer, $httpsMode,
73 "Test non-expandable relative URLs ' .
74 '(defaultProto: $protoDesc , wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )"
77 // Determine expected protocol
78 if ( $protoDesc == 'protocol-relative' ) {
80 } elseif ( $protoDesc == 'current' ) {
82 } elseif ( $protoDesc == 'canonical' ) {
83 $p = "$canServerMode:";
85 $p = $protoDesc . ':';
87 // Determine expected server name
88 if ( $protoDesc == 'canonical' ) {
90 } elseif ( $serverDesc == 'protocol-relative' ) {
97 "$p//wikipedia.org", '//wikipedia.org',
98 $defaultProto, $server, $canServer, $httpsMode,
99 "Test protocol-relative URL ' .
100 '(defaultProto: $protoDesc, wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )"
103 "$srv/wiki/FooBar", '/wiki/FooBar',
104 $defaultProto, $server, $canServer, $httpsMode,
105 "Testing expanding URL beginning with / ' .
106 '(defaultProto: $protoDesc , wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )"