2011-07-13 01:54:40 +00:00
|
|
|
<?php
|
2011-10-11 18:30:50 +00:00
|
|
|
/**
|
2014-07-18 19:35:43 +00:00
|
|
|
* @group GlobalFunctions
|
2013-10-24 09:53:24 +00:00
|
|
|
* @covers ::wfExpandUrl
|
2011-07-13 01:54:40 +00:00
|
|
|
*/
|
2020-06-30 15:09:24 +00:00
|
|
|
class WfExpandUrlTest extends MediaWikiIntegrationTestCase {
|
2013-10-24 09:53:24 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideExpandableUrls
|
|
|
|
|
*/
|
2021-09-21 20:19:50 +00:00
|
|
|
public function testWfExpandUrl( string $input, array $conf,
|
|
|
|
|
string $currentProto, $defaultProto, string $expected
|
2014-04-24 09:47:06 +00:00
|
|
|
) {
|
2021-09-21 20:19:50 +00:00
|
|
|
$this->setMwGlobals( $conf );
|
|
|
|
|
$this->setRequest( new FauxRequest( [], false, null, $currentProto ) );
|
|
|
|
|
$this->assertEquals( $expected, wfExpandUrl( $input, $defaultProto ) );
|
2011-07-13 01:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
public static function provideExpandableUrls() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$modes = [ 'http', 'https' ];
|
|
|
|
|
$servers = [
|
2021-09-21 20:19:50 +00:00
|
|
|
'http://example.com',
|
|
|
|
|
'https://example.com',
|
|
|
|
|
'//example.com',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
|
|
|
|
$defaultProtos = [
|
2012-09-28 23:35:16 +00:00
|
|
|
'http' => PROTO_HTTP,
|
|
|
|
|
'https' => PROTO_HTTPS,
|
|
|
|
|
'protocol-relative' => PROTO_RELATIVE,
|
|
|
|
|
'current' => PROTO_CURRENT,
|
2021-09-21 20:19:50 +00:00
|
|
|
'canonical' => PROTO_CANONICAL,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2011-11-25 00:15:27 +00:00
|
|
|
|
2021-09-21 20:19:50 +00:00
|
|
|
foreach ( $modes as $currentProto ) {
|
|
|
|
|
foreach ( $servers as $server ) {
|
2013-02-14 11:22:13 +00:00
|
|
|
foreach ( $modes as $canServerMode ) {
|
2011-08-19 15:25:50 +00:00
|
|
|
$canServer = "$canServerMode://example2.com";
|
2021-09-21 20:19:50 +00:00
|
|
|
$conf = [
|
|
|
|
|
'wgServer' => $server,
|
|
|
|
|
'wgCanonicalServer' => $canServer,
|
|
|
|
|
'wgHttpsPort' => 443,
|
|
|
|
|
];
|
2011-08-19 15:25:50 +00:00
|
|
|
foreach ( $defaultProtos as $protoDesc => $defaultProto ) {
|
2021-09-21 20:19:50 +00:00
|
|
|
$case = "current: $currentProto, default: $protoDesc, server: $server, canonical: $canServer";
|
|
|
|
|
yield "No-op fully-qualified http URL ($case)" => [
|
|
|
|
|
'http://example.com',
|
|
|
|
|
$conf, $currentProto, $defaultProto,
|
|
|
|
|
'http://example.com',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2021-09-21 20:19:50 +00:00
|
|
|
yield "No-op fully-qualified https URL ($case)" => [
|
|
|
|
|
'https://example.com',
|
|
|
|
|
$conf, $currentProto, $defaultProto,
|
|
|
|
|
'https://example.com',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2021-09-21 20:19:50 +00:00
|
|
|
yield "No-op rootless path-only URL ($case)" => [
|
|
|
|
|
"wiki/FooBar",
|
|
|
|
|
$conf, $currentProto, $defaultProto,
|
|
|
|
|
'wiki/FooBar',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2011-11-25 00:15:27 +00:00
|
|
|
|
2011-08-19 15:25:50 +00:00
|
|
|
// Determine expected protocol
|
2021-09-21 20:19:50 +00:00
|
|
|
if ( $protoDesc === 'protocol-relative' ) {
|
2011-08-19 15:25:50 +00:00
|
|
|
$p = '';
|
2021-09-21 20:19:50 +00:00
|
|
|
} elseif ( $protoDesc === 'current' ) {
|
|
|
|
|
$p = "$currentProto:";
|
|
|
|
|
} elseif ( $protoDesc === 'canonical' ) {
|
2011-08-19 15:25:50 +00:00
|
|
|
$p = "$canServerMode:";
|
|
|
|
|
} else {
|
|
|
|
|
$p = $protoDesc . ':';
|
|
|
|
|
}
|
2021-09-21 20:19:50 +00:00
|
|
|
yield "Expand protocol-relative URL ($case)" => [
|
|
|
|
|
'//wikipedia.org',
|
|
|
|
|
$conf, $currentProto, $defaultProto,
|
|
|
|
|
"$p//wikipedia.org",
|
|
|
|
|
];
|
|
|
|
|
|
2011-08-19 15:25:50 +00:00
|
|
|
// Determine expected server name
|
2021-09-21 20:19:50 +00:00
|
|
|
if ( $protoDesc === 'canonical' ) {
|
2011-08-19 15:25:50 +00:00
|
|
|
$srv = $canServer;
|
2021-09-21 20:19:50 +00:00
|
|
|
} elseif ( $server === '//example.com' ) {
|
2011-08-19 15:25:50 +00:00
|
|
|
$srv = $p . $server;
|
|
|
|
|
} else {
|
|
|
|
|
$srv = $server;
|
|
|
|
|
}
|
2021-09-21 20:19:50 +00:00
|
|
|
yield "Expand path that starts with slash ($case)" => [
|
2014-04-24 09:47:06 +00:00
|
|
|
'/wiki/FooBar',
|
2021-09-21 20:19:50 +00:00
|
|
|
$conf, $currentProto, $defaultProto,
|
|
|
|
|
"$srv/wiki/FooBar",
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2011-07-27 08:21:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2021-09-21 20:19:50 +00:00
|
|
|
$confRel111 = [
|
|
|
|
|
'wgServer' => '//wiki.example.com',
|
|
|
|
|
'wgCanonicalServer' => 'http://wiki.example.com',
|
|
|
|
|
'wgHttpsPort' => 111,
|
|
|
|
|
];
|
|
|
|
|
yield "No-op foreign URL, ignore custom port config" => [
|
2018-07-10 00:05:07 +00:00
|
|
|
'https://foreign.example.com/foo',
|
2021-09-21 20:19:50 +00:00
|
|
|
$confRel111, 'https', PROTO_HTTPS,
|
2018-07-10 00:05:07 +00:00
|
|
|
'https://foreign.example.com/foo',
|
|
|
|
|
];
|
2021-09-21 20:19:50 +00:00
|
|
|
yield "No-op foreign URL, preserve existing port" => [
|
2018-07-10 00:05:07 +00:00
|
|
|
'https://foreign.example.com:222/foo',
|
2021-09-21 20:19:50 +00:00
|
|
|
$confRel111, 'https', PROTO_HTTPS,
|
2018-07-10 00:05:07 +00:00
|
|
|
'https://foreign.example.com:222/foo',
|
|
|
|
|
];
|
2021-09-21 20:19:50 +00:00
|
|
|
yield "Expand path with custom HTTPS port" => [
|
2018-07-10 00:05:07 +00:00
|
|
|
'/foo',
|
2021-09-21 20:19:50 +00:00
|
|
|
$confRel111, 'https', PROTO_HTTPS,
|
|
|
|
|
'https://wiki.example.com:111/foo',
|
2018-07-10 00:05:07 +00:00
|
|
|
];
|
2011-07-13 01:54:40 +00:00
|
|
|
}
|
|
|
|
|
}
|