2011-11-15 17:38:20 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2014-07-18 19:35:43 +00:00
|
|
|
* @group GlobalFunctions
|
2013-10-24 09:53:24 +00:00
|
|
|
* @covers ::wfAssembleUrl
|
2011-11-15 17:38:20 +00:00
|
|
|
*/
|
2019-07-13 20:50:28 +00:00
|
|
|
class WfAssembleUrlTest extends MediaWikiUnitTestCase {
|
2013-10-24 09:53:24 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideURLParts
|
|
|
|
|
*/
|
2011-11-15 17:38:20 +00:00
|
|
|
public function testWfAssembleUrl( $parts, $output ) {
|
|
|
|
|
$partsDump = print_r( $parts, true );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$output,
|
|
|
|
|
wfAssembleUrl( $parts ),
|
|
|
|
|
"Testing $partsDump assembles to $output"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provider of URL parts for testing wfAssembleUrl()
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2012-10-08 10:56:20 +00:00
|
|
|
public static function provideURLParts() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$schemes = [
|
|
|
|
|
'' => [],
|
|
|
|
|
'//' => [
|
2011-11-15 17:38:20 +00:00
|
|
|
'delimiter' => '//',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'http://' => [
|
2011-11-15 17:38:20 +00:00
|
|
|
'scheme' => 'http',
|
|
|
|
|
'delimiter' => '://',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2011-11-15 17:38:20 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$hosts = [
|
|
|
|
|
'' => [],
|
|
|
|
|
'example.com' => [
|
2011-11-15 17:38:20 +00:00
|
|
|
'host' => 'example.com',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'example.com:123' => [
|
2011-11-15 17:38:20 +00:00
|
|
|
'host' => 'example.com',
|
|
|
|
|
'port' => 123,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'id@example.com' => [
|
2011-11-15 17:38:20 +00:00
|
|
|
'user' => 'id',
|
|
|
|
|
'host' => 'example.com',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'id@example.com:123' => [
|
2011-11-15 17:38:20 +00:00
|
|
|
'user' => 'id',
|
|
|
|
|
'host' => 'example.com',
|
|
|
|
|
'port' => 123,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'id:key@example.com' => [
|
2011-11-15 17:38:20 +00:00
|
|
|
'user' => 'id',
|
|
|
|
|
'pass' => 'key',
|
|
|
|
|
'host' => 'example.com',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'id:key@example.com:123' => [
|
2011-11-15 17:38:20 +00:00
|
|
|
'user' => 'id',
|
|
|
|
|
'pass' => 'key',
|
|
|
|
|
'host' => 'example.com',
|
|
|
|
|
'port' => 123,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2011-11-15 17:38:20 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$cases = [];
|
2011-11-15 17:38:20 +00:00
|
|
|
foreach ( $schemes as $scheme => $schemeParts ) {
|
|
|
|
|
foreach ( $hosts as $host => $hostParts ) {
|
2020-11-26 23:45:45 +00:00
|
|
|
foreach ( [ '', '/', '/0', '/path' ] as $path ) {
|
|
|
|
|
foreach ( [ '', '0', 'query' ] as $query ) {
|
|
|
|
|
foreach ( [ '', '0', 'fragment' ] as $fragment ) {
|
2011-11-15 17:38:20 +00:00
|
|
|
$parts = array_merge(
|
|
|
|
|
$schemeParts,
|
|
|
|
|
$hostParts
|
|
|
|
|
);
|
|
|
|
|
$url = $scheme .
|
|
|
|
|
$host .
|
|
|
|
|
$path;
|
|
|
|
|
|
2020-11-26 23:45:45 +00:00
|
|
|
if ( $path !== '' ) {
|
2011-11-15 17:38:20 +00:00
|
|
|
$parts['path'] = $path;
|
|
|
|
|
}
|
2020-11-26 23:45:45 +00:00
|
|
|
if ( $query !== '' ) {
|
2011-11-15 17:38:20 +00:00
|
|
|
$parts['query'] = $query;
|
|
|
|
|
$url .= '?' . $query;
|
|
|
|
|
}
|
2020-11-26 23:45:45 +00:00
|
|
|
if ( $fragment !== '' ) {
|
2011-11-15 17:38:20 +00:00
|
|
|
$parts['fragment'] = $fragment;
|
|
|
|
|
$url .= '#' . $fragment;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$cases[] = [
|
2011-11-15 17:38:20 +00:00
|
|
|
$parts,
|
|
|
|
|
$url,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2011-11-15 17:38:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$complexURL = 'http://id:key@example.org:321' .
|
|
|
|
|
'/over/there?name=ferret&foo=bar#nose';
|
2016-02-17 09:09:32 +00:00
|
|
|
$cases[] = [
|
2011-11-15 17:38:20 +00:00
|
|
|
wfParseUrl( $complexURL ),
|
|
|
|
|
$complexURL,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2011-11-15 17:38:20 +00:00
|
|
|
|
2020-11-26 21:47:25 +00:00
|
|
|
// Account for parse_url() on PHP >= 8 returning an empty query field
|
|
|
|
|
// for URLs ending with '?' such as "http://url.with.empty.query/foo?"
|
|
|
|
|
// (T268852)
|
|
|
|
|
$urlWithEmptyQuery = [
|
|
|
|
|
'scheme' => 'http',
|
|
|
|
|
'delimiter' => '://',
|
|
|
|
|
'host' => 'url.with.empty.query',
|
|
|
|
|
'path' => '/foo',
|
|
|
|
|
'query' => '',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$cases[] = [
|
|
|
|
|
$urlWithEmptyQuery,
|
|
|
|
|
'http://url.with.empty.query/foo'
|
|
|
|
|
];
|
|
|
|
|
|
2011-11-15 17:38:20 +00:00
|
|
|
return $cases;
|
|
|
|
|
}
|
|
|
|
|
}
|