wiki.techinc.nl/tests/phpunit/includes/WikiReferenceTest.php
Timo Tijhof 29e0183a56 phpunit: Repair GLOBALS reset in MediaWikiUnitTestCase
This code didn't work because the $GLOBALS array is exposed by reference.
Once this reference was broken by unset(), the rest just manipulated a
local array that happens to be called "GLOBALS". It must not be unset or
re-assigned. It can only be changed in-place.

Before this, the execution of a MediaWikiUnitTestCase test stored a
copy of GLOBALS in unitGlobals, then lost the GLOBALS pointer and
created a new variable called "GLOBALS". As such, the tearDown() function
didn't do what it meant to do, either – which then results in odd
failures like T230023

Rewrite it as follows:

* In setup, store the current GLOBALS keys and values, then reduce
  GLOBALS to only the whitelisted keys and values.

* In teardown, restore the original state.

* As optimisation, do this from setUpBeforeClass as well, so that
  there are relatively few globals to reset between tests.
  (Thanks @Simetrical!)

The following tests were previously passing by accident under
MediaWikiUnitTestCase but actually did depend on global config.

* MainSlotRoleHandlerTest (…, ContentHandler, $wgContentHandlers)
* SlotRecordTest (…, ContentHandler, $wgContentHandlers)
* WikiReferenceTest (wfParseUrl, $wgUrlProtocols)
* DifferenceEngineSlotDiffRendererTest (DifferenceEngine, wfDebug, …)
* SlotDiffRendererTest (…, ContentHandler, $wgContentHandlers)
* FileBackendDBRepoWrapperTest (wfWikiID, "Backend domain ID not provided")
* JpegMetadataExtractorTest (…, wfDebug, …, LoggerFactory, …)
* ParserFactoryTest (…, wfDebug, …, LoggerFactory, InvalidArgumentException)
* MediaWikiPageNameNormalizerTest (…, wfDebug, …, LoggerFactory, …)
* SiteExporterTest (SiteImporter, wfLogWarning, …)
* SiteImporterTest (Site::newForType, $wgSiteTypes)
* ZipDirectoryReaderTest (…, wfDebug, …, LoggerFactory, …)

Bug: T230023
Change-Id: Ic22075bb5e81b7c2c4c1b8647547aa55306a10a7
2019-09-02 20:58:34 +01:00

164 lines
3.5 KiB
PHP

<?php
/**
* @covers WikiReference
*/
class WikiReferenceTest extends MediaWikiIntegrationTestCase {
public function provideGetDisplayName() {
return [
'http' => [ 'foo.bar', 'http://foo.bar' ],
'https' => [ 'foo.bar', 'http://foo.bar' ],
// apparently, this is the expected behavior
'invalid' => [ 'purple kittens', 'purple kittens' ],
];
}
/**
* @dataProvider provideGetDisplayName
*/
public function testGetDisplayName( $expected, $canonicalServer ) {
$reference = new WikiReference( $canonicalServer, '/wiki/$1' );
$this->assertEquals( $expected, $reference->getDisplayName() );
}
public function testGetCanonicalServer() {
$reference = new WikiReference( 'https://acme.com', '/wiki/$1', '//acme.com' );
$this->assertEquals( 'https://acme.com', $reference->getCanonicalServer() );
}
public function provideGetCanonicalUrl() {
return [
'no fragment' => [
'https://acme.com/wiki/Foo',
'https://acme.com',
'//acme.com',
'/wiki/$1',
'Foo',
null
],
'empty fragment' => [
'https://acme.com/wiki/Foo',
'https://acme.com',
'//acme.com',
'/wiki/$1',
'Foo',
''
],
'fragment' => [
'https://acme.com/wiki/Foo#Bar',
'https://acme.com',
'//acme.com',
'/wiki/$1',
'Foo',
'Bar'
],
'double fragment' => [
'https://acme.com/wiki/Foo#Bar%23Xus',
'https://acme.com',
'//acme.com',
'/wiki/$1',
'Foo',
'Bar#Xus'
],
'escaped fragment' => [
'https://acme.com/wiki/Foo%23Bar',
'https://acme.com',
'//acme.com',
'/wiki/$1',
'Foo#Bar',
null
],
'empty path' => [
'https://acme.com/Foo',
'https://acme.com',
'//acme.com',
'/$1',
'Foo',
null
],
];
}
/**
* @dataProvider provideGetCanonicalUrl
*/
public function testGetCanonicalUrl(
$expected, $canonicalServer, $server, $path, $page, $fragmentId
) {
$reference = new WikiReference( $canonicalServer, $path, $server );
$this->assertEquals( $expected, $reference->getCanonicalUrl( $page, $fragmentId ) );
}
/**
* @dataProvider provideGetCanonicalUrl
* @note getUrl is an alias for getCanonicalUrl
*/
public function testGetUrl( $expected, $canonicalServer, $server, $path, $page, $fragmentId ) {
$reference = new WikiReference( $canonicalServer, $path, $server );
$this->assertEquals( $expected, $reference->getUrl( $page, $fragmentId ) );
}
public function provideGetFullUrl() {
return [
'no fragment' => [
'//acme.com/wiki/Foo',
'https://acme.com',
'//acme.com',
'/wiki/$1',
'Foo',
null
],
'empty fragment' => [
'//acme.com/wiki/Foo',
'https://acme.com',
'//acme.com',
'/wiki/$1',
'Foo',
''
],
'fragment' => [
'//acme.com/wiki/Foo#Bar',
'https://acme.com',
'//acme.com',
'/wiki/$1',
'Foo',
'Bar'
],
'double fragment' => [
'//acme.com/wiki/Foo#Bar%23Xus',
'https://acme.com',
'//acme.com',
'/wiki/$1',
'Foo',
'Bar#Xus'
],
'escaped fragment' => [
'//acme.com/wiki/Foo%23Bar',
'https://acme.com',
'//acme.com',
'/wiki/$1',
'Foo#Bar',
null
],
'empty path' => [
'//acme.com/Foo',
'https://acme.com',
'//acme.com',
'/$1',
'Foo',
null
],
];
}
/**
* @dataProvider provideGetFullUrl
*/
public function testGetFullUrl( $expected, $canonicalServer, $server, $path, $page, $fragmentId ) {
$reference = new WikiReference( $canonicalServer, $path, $server );
$this->assertEquals( $expected, $reference->getFullUrl( $page, $fragmentId ) );
}
}