MediaWiki  master
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 static function provideExpandableUrls() {
00036                 $modes = array( 'http', 'https' );
00037                 $servers = array(
00038                         'http' => 'http://example.com',
00039                         'https' => 'https://example.com',
00040                         'protocol-relative' => '//example.com'
00041                 );
00042                 $defaultProtos = array(
00043                         'http' => PROTO_HTTP,
00044                         'https' => PROTO_HTTPS,
00045                         'protocol-relative' => PROTO_RELATIVE,
00046                         'current' => PROTO_CURRENT,
00047                         'canonical' => PROTO_CANONICAL
00048                 );
00049 
00050                 $retval = array();
00051                 foreach ( $modes as $mode ) {
00052                         $httpsMode = $mode == 'https';
00053                         foreach ( $servers as $serverDesc => $server ) {
00054                                 foreach ( $modes as $canServerMode  ) {
00055                                         $canServer = "$canServerMode://example2.com";
00056                                         foreach ( $defaultProtos as $protoDesc => $defaultProto ) {
00057                                                 $retval[] = array(
00058                                                         'http://example.com', 'http://example.com',
00059                                                         $defaultProto, $server, $canServer, $httpsMode,
00060                                                         "Testing fully qualified http URLs (no need to expand) ' .
00061                                                         '(defaultProto: $protoDesc , wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )"
00062                                                 );
00063                                                 $retval[] = array(
00064                                                         'https://example.com', 'https://example.com',
00065                                                         $defaultProto, $server, $canServer, $httpsMode,
00066                                                         "Testing fully qualified https URLs (no need to expand) ' .
00067                                                         '(defaultProto: $protoDesc , wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )"
00068                                                 );
00069                                                 # Would be nice to support this, see fixme on wfExpandUrl()
00070                                                 $retval[] = array(
00071                                                         "wiki/FooBar", 'wiki/FooBar',
00072                                                         $defaultProto, $server, $canServer, $httpsMode,
00073                                                         "Test non-expandable relative URLs ' .
00074                                                         '(defaultProto: $protoDesc , wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )"
00075                                                 );
00076 
00077                                                 // Determine expected protocol
00078                                                 if ( $protoDesc == 'protocol-relative' ) {
00079                                                         $p = '';
00080                                                 } elseif ( $protoDesc == 'current' ) {
00081                                                         $p = "$mode:";
00082                                                 } elseif ( $protoDesc == 'canonical' ) {
00083                                                         $p = "$canServerMode:";
00084                                                 } else {
00085                                                         $p = $protoDesc . ':';
00086                                                 }
00087                                                 // Determine expected server name
00088                                                 if ( $protoDesc == 'canonical' ) {
00089                                                         $srv = $canServer;
00090                                                 } elseif ( $serverDesc == 'protocol-relative' ) {
00091                                                         $srv = $p . $server;
00092                                                 } else {
00093                                                         $srv = $server;
00094                                                 }
00095 
00096                                                 $retval[] = array(
00097                                                         "$p//wikipedia.org", '//wikipedia.org',
00098                                                         $defaultProto, $server, $canServer, $httpsMode,
00099                                                         "Test protocol-relative URL ' .
00100                                                         '(defaultProto: $protoDesc, wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )"
00101                                                 );
00102                                                 $retval[] = array(
00103                                                         "$srv/wiki/FooBar", '/wiki/FooBar',
00104                                                         $defaultProto, $server, $canServer, $httpsMode,
00105                                                         "Testing expanding URL beginning with / ' .
00106                                                         '(defaultProto: $protoDesc , wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )"
00107                                                 );
00108                                         }
00109                                 }
00110                         }
00111                 }
00112                 return $retval;
00113         }
00114 }