wiki.techinc.nl/tests/phpunit/includes/GlobalFunctions/WfExpandUrlTest.php

154 lines
4.5 KiB
PHP
Raw Normal View History

<?php
/**
* @group GlobalFunctions
* @covers ::wfExpandUrl
*/
class WfExpandUrlTest extends MediaWikiIntegrationTestCase {
/**
* @dataProvider provideExpandableUrls
*/
public function testWfExpandUrl( $fullUrl, $shortUrl, $defaultProto,
$server, $canServer, $httpsMode, $httpsPort, $message
) {
// Fake $wgServer, $wgCanonicalServer and $wgRequest->getProtocol()
// fake edit to fake globals
$this->setMwGlobals( [
'wgServer' => $server,
'wgCanonicalServer' => $canServer,
'wgHttpsPort' => $httpsPort
] );
$this->setRequest( new FauxRequest( [], false, null, $httpsMode ? 'https' : 'http' ) );
2011-11-25 00:15:27 +00:00
$this->assertEquals( $fullUrl, wfExpandUrl( $shortUrl, $defaultProto ), $message );
}
/**
* Provider of URL examples for testing wfExpandUrl()
*
* @return array
*/
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
public static function provideExpandableUrls() {
$modes = [ 'http', 'https' ];
$servers = [
'http' => 'http://example.com',
'https' => 'https://example.com',
'protocol-relative' => '//example.com'
];
$defaultProtos = [
'http' => PROTO_HTTP,
'https' => PROTO_HTTPS,
'protocol-relative' => PROTO_RELATIVE,
'current' => PROTO_CURRENT,
'canonical' => PROTO_CANONICAL
];
2011-11-25 00:15:27 +00:00
$retval = [];
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[] = [
'http://example.com', 'http://example.com',
$defaultProto, $server, $canServer, $httpsMode, 443,
"Testing fully qualified http URLs (no need to expand) "
. "(defaultProto: $protoDesc , wgServer: $server, "
. "wgCanonicalServer: $canServer, current request protocol: $mode )"
];
$retval[] = [
'https://example.com', 'https://example.com',
$defaultProto, $server, $canServer, $httpsMode, 443,
"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[] = [
"wiki/FooBar", 'wiki/FooBar',
$defaultProto, $server, $canServer, $httpsMode, 443,
"Test non-expandable relative URLs (defaultProto: $protoDesc, "
. "wgServer: $server, wgCanonicalServer: $canServer, "
. "current request protocol: $mode )"
];
2011-11-25 00:15:27 +00:00
// Determine expected protocol
if ( $protoDesc == 'protocol-relative' ) {
$p = '';
2011-11-25 00:15:27 +00:00
} elseif ( $protoDesc == 'current' ) {
$p = "$mode:";
2011-11-25 00:15:27 +00:00
} elseif ( $protoDesc == 'canonical' ) {
$p = "$canServerMode:";
} else {
$p = $protoDesc . ':';
}
// Determine expected server name
if ( $protoDesc == 'canonical' ) {
$srv = $canServer;
2011-11-25 00:15:27 +00:00
} elseif ( $serverDesc == 'protocol-relative' ) {
$srv = $p . $server;
} else {
$srv = $server;
}
2011-11-25 00:15:27 +00:00
$retval[] = [
"$p//wikipedia.org", '//wikipedia.org',
$defaultProto, $server, $canServer, $httpsMode, 443,
"Test protocol-relative URL (defaultProto: $protoDesc, "
. "wgServer: $server, wgCanonicalServer: $canServer, "
. "current request protocol: $mode )"
];
$retval[] = [
"$srv/wiki/FooBar",
'/wiki/FooBar',
$defaultProto,
$server,
$canServer,
$httpsMode,
443,
"Testing expanding URL beginning with / (defaultProto: $protoDesc, "
. "wgServer: $server, wgCanonicalServer: $canServer, "
. "current request protocol: $mode )"
];
}
}
}
}
// Don't add HTTPS port to foreign URLs
$retval[] = [
'https://foreign.example.com/foo',
'https://foreign.example.com/foo',
PROTO_HTTPS,
'//wiki.example.com',
'http://wiki.example.com',
'https',
111,
"Don't add HTTPS port to foreign URLs"
];
$retval[] = [
'https://foreign.example.com:222/foo',
'https://foreign.example.com:222/foo',
PROTO_HTTPS,
'//wiki.example.com',
'http://wiki.example.com',
'https',
111,
"Don't overwrite HTTPS port of foreign URLs"
];
// Do add HTTPS port to local URLs
$retval[] = [
'https://wiki.example.com:111/foo',
'/foo',
PROTO_HTTPS,
'//wiki.example.com',
'http://wiki.example.com',
'https',
111,
"Do add HTTPS port to protocol-relative URLs"
];
return $retval;
}
}