wiki.techinc.nl/tests/phpunit/includes/GlobalFunctions/wfExpandUrlTest.php
Tim Starling 8af1c9503d Introduce WebRequest::getProtocol()
The use of static server detection outside of its intended use case
(i.e. at the start of DefaultSettings.php), for example in r93258, was
an architectural error. Every other bit of information about the web
request in non-setup code comes from non-static methods of WebRequest,
which allows the request object to be meaningfully replaced or
subclassed. The situation became increasingly ridiculous as more
callers of WebRequest::detectProtocol() were introduced. Two of the
callers were calling it non-statically! I suppose they had the right
idea, in a way.

Using a non-static call allows caching, which is a nice additional
benefit.

WebRequest::detectProtocolAndStdPort() was introduced in r93258 as part of
the introduction of WebRequest::detectProtocol(). It was basically
useless. Grep indicates there was only one caller in core and WMF
deployed extensions, and it is patched here.

Change-Id: Ia0a61e98fbff7a46ceaeebcb02236e5eac3df0e1
2013-11-06 21:13:10 +04:00

105 lines
3.5 KiB
PHP

<?php
/**
* @covers ::wfExpandUrl
*/
class WfExpandUrlTest extends MediaWikiTestCase {
/**
* @dataProvider provideExpandableUrls
*/
public function testWfExpandUrl( $fullUrl, $shortUrl, $defaultProto, $server, $canServer, $httpsMode, $message ) {
// Fake $wgServer, $wgCanonicalServer and $wgRequest->getProtocol()
$this->setMwGlobals( array(
'wgServer' => $server,
'wgCanonicalServer' => $canServer,
'wgRequest' => new FauxRequest( array(), false, null, $httpsMode ? 'https' : 'http' )
) );
$this->assertEquals( $fullUrl, wfExpandUrl( $shortUrl, $defaultProto ), $message );
}
/**
* Provider of URL examples for testing wfExpandUrl()
*
* @return array
*/
public static function provideExpandableUrls() {
$modes = array( 'http', 'https' );
$servers = array(
'http' => 'http://example.com',
'https' => 'https://example.com',
'protocol-relative' => '//example.com'
);
$defaultProtos = array(
'http' => PROTO_HTTP,
'https' => PROTO_HTTPS,
'protocol-relative' => PROTO_RELATIVE,
'current' => PROTO_CURRENT,
'canonical' => PROTO_CANONICAL
);
$retval = array();
foreach ( $modes as $mode ) {
$httpsMode = $mode == 'https';
foreach ( $servers as $serverDesc => $server ) {
foreach ( $modes as $canServerMode ) {
$canServer = "$canServerMode://example2.com";
foreach ( $defaultProtos as $protoDesc => $defaultProto ) {
$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 )"
);
$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 )"
);
# Would be nice to support this, see fixme on wfExpandUrl()
$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 )"
);
// Determine expected protocol
if ( $protoDesc == 'protocol-relative' ) {
$p = '';
} elseif ( $protoDesc == 'current' ) {
$p = "$mode:";
} elseif ( $protoDesc == 'canonical' ) {
$p = "$canServerMode:";
} else {
$p = $protoDesc . ':';
}
// Determine expected server name
if ( $protoDesc == 'canonical' ) {
$srv = $canServer;
} elseif ( $serverDesc == 'protocol-relative' ) {
$srv = $p . $server;
} else {
$srv = $server;
}
$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 )"
);
$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 )"
);
}
}
}
}
return $retval;
}
}