MediaWiki  REL1_19
wfExpandUrlTest.php
Go to the documentation of this file.
00001 <?php
00006 class wfExpandUrl extends MediaWikiTestCase {
00008         public function testWfExpandUrl( $fullUrl, $shortUrl, $defaultProto, $server, $canServer, $httpsMode, $message ) {
00009                 // Fake $wgServer and $wgCanonicalServer
00010                 global $wgServer, $wgCanonicalServer;
00011                 $oldServer = $wgServer;
00012                 $oldCanServer = $wgCanonicalServer;
00013                 $wgServer = $server;
00014                 $wgCanonicalServer = $canServer;
00015 
00016                 // Fake $_SERVER['HTTPS'] if needed
00017                 if ( $httpsMode ) {
00018                         $_SERVER['HTTPS'] = 'on';
00019                 } else {
00020                         unset( $_SERVER['HTTPS'] );
00021                 }
00022 
00023                 $this->assertEquals( $fullUrl, wfExpandUrl( $shortUrl, $defaultProto ), $message );
00024 
00025                 // Restore $wgServer and $wgCanonicalServer
00026                 $wgServer = $oldServer;
00027                 $wgCanonicalServer = $oldCanServer;
00028         }
00029 
00035         public function provideExpandableUrls() {
00036                 $modes = array( 'http', 'https' );
00037                 $servers = array( 'http' => 'http://example.com', 'https' => 'https://example.com', 'protocol-relative' => '//example.com' );
00038                 $defaultProtos = array( 'http' => PROTO_HTTP, 'https' => PROTO_HTTPS, 'protocol-relative' => PROTO_RELATIVE, 'current' => PROTO_CURRENT, 'canonical' => PROTO_CANONICAL );
00039 
00040                 $retval = array();
00041                 foreach ( $modes as $mode ) {
00042                         $httpsMode = $mode == 'https';
00043                         foreach ( $servers as $serverDesc => $server ) {
00044                                 foreach ( $modes as $canServerMode  ) {
00045                                         $canServer = "$canServerMode://example2.com";
00046                                         foreach ( $defaultProtos as $protoDesc => $defaultProto ) {
00047                                                 $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 )" );
00048                                                 $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 )" );
00049                                                 # Would be nice to support this, see fixme on wfExpandUrl()
00050                                                 $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 )" );
00051 
00052                                                 // Determine expected protocol
00053                                                 $p = $protoDesc . ':'; // default case
00054                                                 if ( $protoDesc == 'protocol-relative' ) {
00055                                                         $p = '';
00056                                                 } elseif ( $protoDesc == 'current' ) {
00057                                                         $p = "$mode:";
00058                                                 } elseif ( $protoDesc == 'canonical' ) {
00059                                                         $p = "$canServerMode:";
00060                                                 } else {
00061                                                         $p = $protoDesc . ':';
00062                                                 }
00063                                                 // Determine expected server name
00064                                                 if ( $protoDesc == 'canonical' ) {
00065                                                         $srv = $canServer;
00066                                                 } elseif ( $serverDesc == 'protocol-relative' ) {
00067                                                         $srv = $p . $server;
00068                                                 } else {
00069                                                         $srv = $server;
00070                                                 }
00071 
00072                                                 $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 )" );
00073                                                 $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 )" );
00074                                         }
00075                                 }
00076                         }
00077                 }
00078                 return $retval;
00079         }
00080 }