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 function provideExpandableUrls() {
36 $modes = array( 'http', 'https' );
37 $servers = array( 'http' => 'http://example.com', 'https' => 'https://example.com', 'protocol-relative' => '//example.com' );
38 $defaultProtos = array( 'http' => PROTO_HTTP
, 'https' => PROTO_HTTPS
, 'protocol-relative' => PROTO_RELATIVE
, 'current' => PROTO_CURRENT
, 'canonical' => PROTO_CANONICAL
);
41 foreach ( $modes as $mode ) {
42 $httpsMode = $mode == 'https';
43 foreach ( $servers as $serverDesc => $server ) {
44 foreach ( $modes as $canServerMode ) {
45 $canServer = "$canServerMode://example2.com";
46 foreach ( $defaultProtos as $protoDesc => $defaultProto ) {
47 $retval[] = array( 'http://example.com', 'http://example.com', $defaultProto, $server, $canServer, $httpsMode, "Testing fully qualified http URLs (no need to expand) (defaultProto: $protoDesc , wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )" );
48 $retval[] = array( 'https://example.com', 'https://example.com', $defaultProto, $server, $canServer, $httpsMode, "Testing fully qualified https URLs (no need to expand) (defaultProto: $protoDesc , wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )" );
49 # Would be nice to support this, see fixme on wfExpandUrl()
50 $retval[] = array( "wiki/FooBar", 'wiki/FooBar', $defaultProto, $server, $canServer, $httpsMode, "Test non-expandable relative URLs (defaultProto: $protoDesc , wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )" );
52 // Determine expected protocol
53 $p = $protoDesc . ':'; // default case
54 if ( $protoDesc == 'protocol-relative' ) {
56 } else if ( $protoDesc == 'current' ) {
58 } else if ( $protoDesc == 'canonical' ) {
59 $p = "$canServerMode:";
61 $p = $protoDesc . ':';
63 // Determine expected server name
64 if ( $protoDesc == 'canonical' ) {
66 } else if ( $serverDesc == 'protocol-relative' ) {
72 $retval[] = array( "$p//wikipedia.org", '//wikipedia.org', $defaultProto, $server, $canServer, $httpsMode, "Test protocol-relative URL (defaultProto: $protoDesc, wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )" );
73 $retval[] = array( "$srv/wiki/FooBar", '/wiki/FooBar', $defaultProto, $server, $canServer, $httpsMode, "Testing expanding URL beginning with / (defaultProto: $protoDesc , wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )" );