wiki.techinc.nl/tests/phpunit/includes/UriTest.php
Daniel Friesen 94f623363b Add a Uri class.
Add a Uri class matching our mw.Uri JS class for handling uris.
This class should be helpful in a bunch of places where we end up doing manual
concatenation of things like the path + '?' + query of a url parsed with wfParseUrl.

[tylerromeo@gmail.com: Removed cat() function, fixed wfWarn() usage for aliases
 and added visibility to all functions. Also added test for aliases.]
Signed-off-by: Tyler Romeo <tylerromeo@gmail.com>

Change-Id: Iefdedb7c80cf1d4aab58050edab3ab44ba868a58
2012-08-06 22:46:25 -04:00

164 lines
4.7 KiB
PHP

<?php
class UriTest extends MediaWikiTestCase {
function setUp() {
AutoLoader::loadClass( 'Uri' );
}
function dataUris() {
return array(
array(
'http://example.com/',
array(
'scheme' => 'http',
'delimiter' => '://',
'user' => null,
'pass' => null,
'host' => 'example.com',
'port' => null,
'path' => '/',
'query' => null,
'fragment' => null,
),
),
array(
'//mediawiki.org/wiki/Main_Page',
array(
'scheme' => null,
'delimiter' => '//',
'user' => null,
'pass' => null,
'host' => 'mediawiki.org',
'port' => null,
'path' => '/wiki/Main_Page',
'query' => null,
'fragment' => null,
),
),
array(
'http://user:pass@example.com/',
array(
'scheme' => 'http',
'delimiter' => '://',
'user' => 'user',
'pass' => 'pass',
'host' => 'example.com',
'port' => null,
'path' => '/',
'query' => null,
'fragment' => null,
),
),
array(
'/?asdf=asdf',
array(
'scheme' => null,
'delimiter' => null,
'user' => null,
'pass' => null,
'host' => null,
'port' => null,
'path' => '/',
'query' => 'asdf=asdf',
'fragment' => null,
),
),
array(
'?asdf=asdf#asdf',
array(
'scheme' => null,
'delimiter' => null,
'user' => null,
'pass' => null,
'host' => null,
'port' => null,
'path' => null,
'query' => 'asdf=asdf',
'fragment' => 'asdf',
),
)
);
}
/**
* Ensure that get* methods properly match the appropriate getComponent( key ) value
* @dataProvider dataUris
*/
function testGetters( $uri ) {
$uri = new Uri( $uri );
$getterMap = array(
'getProtocol' => 'scheme',
'getUser' => 'user',
'getPassword' => 'pass',
'getHost' => 'host',
'getPort' => 'port',
'getPath' => 'path',
'getQueryString' => 'query',
'getFragment' => 'fragment',
);
foreach ( $getterMap as $fn => $c ) {
$this->assertSame( $uri->{$fn}(), $uri->getComponent( $c ), "\$uri->{$fn}(); matches \$uri->getComponent( '$c' );" );
}
}
/**
* Ensure that Uri has the proper components for our example uris
* @dataProvider dataUris
*/
function testComponents( $uri, $components ) {
$uri = new Uri( $uri );
$this->assertSame( $components['scheme'], $uri->getProtocol(), 'Correct scheme' );
$this->assertSame( $components['delimiter'], $uri->getDelimiter(), 'Correct delimiter' );
$this->assertSame( $components['user'], $uri->getUser(), 'Correct user' );
$this->assertSame( $components['pass'], $uri->getPassword(), 'Correct pass' );
$this->assertSame( $components['host'], $uri->getHost(), 'Correct host' );
$this->assertSame( $components['port'], $uri->getPort(), 'Correct port' );
$this->assertSame( $components['path'], $uri->getPath(), 'Correct path' );
$this->assertSame( $components['query'], $uri->getQueryString(), 'Correct query' );
$this->assertSame( $components['fragment'], $uri->getFragment(), 'Correct fragment' );
}
/**
* Ensure that the aliases work for various components.
*/
function testAliases() {
$url = "//myuser@test.com";
$uri = new Uri( $url );
// Set the aliases.
$uri->setComponent( 'protocol', 'https' );
$uri->setComponent( 'password', 'mypass' );
// Now try getting them.
$this->assertSame( 'https', $uri->getComponent( 'protocol' ), 'Correct protocol (alias for scheme)' );
$this->assertSame( 'mypass', $uri->getComponent( 'password' ), 'Correct password (alias for pass)' );
// Finally check their actual names.
$this->assertSame( 'https', $uri->getProtocol(), 'Alias for scheme works' );
$this->assertSame( 'mypass', $uri->getPassword(), 'Alias for pass works' );
}
/**
* Ensure that Uri's helper methods return the correct data
*/
function testHelpers() {
$uri = new Uri( 'http://a:b@example.com:8080/path?query=value' );
$this->assertSame( 'a:b', $uri->getUserInfo(), 'Correct getUserInfo' );
$this->assertSame( 'example.com:8080', $uri->getHostPort(), 'Correct getHostPort' );
$this->assertSame( 'a:b@example.com:8080', $uri->getAuthority(), 'Correct getAuthority' );
$this->assertSame( '/path?query=value', $uri->getRelativePath(), 'Correct getRelativePath' );
$this->assertSame( 'http://a:b@example.com:8080/path?query=value', $uri->toString(), 'Correct toString' );
}
/**
* Ensure that Uri's extend method properly overrides keys
*/
function testExtend() {
$uri = new Uri( 'http://example.org/?a=b&hello=world' );
$uri->extendQuery( 'a=c&foo=bar' );
$this->assertSame( 'a=c&hello=world&foo=bar', $uri->getQueryString() );
}
}