wiki.techinc.nl/tests/phpunit/includes/diff/SlotDiffRendererTest.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

78 lines
2.7 KiB
PHP

<?php
use Wikimedia\Assert\ParameterTypeException;
use Wikimedia\TestingAccessWrapper;
/**
* @covers SlotDiffRenderer
*/
class SlotDiffRendererTest extends \MediaWikiIntegrationTestCase {
/**
* @dataProvider provideNormalizeContents
*/
public function testNormalizeContents(
$oldContent, $newContent, $allowedClasses,
$expectedOldContent, $expectedNewContent, $expectedExceptionClass
) {
$slotDiffRenderer = $this->getMockBuilder( SlotDiffRenderer::class )
->getMock();
try {
// __call needs help deciding which parameter to take by reference
call_user_func_array( [ TestingAccessWrapper::newFromObject( $slotDiffRenderer ),
'normalizeContents' ], [ &$oldContent, &$newContent, $allowedClasses ] );
$this->assertEquals( $expectedOldContent, $oldContent );
$this->assertEquals( $expectedNewContent, $newContent );
} catch ( Exception $e ) {
if ( !$expectedExceptionClass ) {
throw $e;
}
$this->assertInstanceOf( $expectedExceptionClass, $e );
}
}
public function provideNormalizeContents() {
return [
'both null' => [ null, null, null, null, null, InvalidArgumentException::class ],
'left null' => [
null, new WikitextContent( 'abc' ), null,
new WikitextContent( '' ), new WikitextContent( 'abc' ), null,
],
'right null' => [
new WikitextContent( 'def' ), null, null,
new WikitextContent( 'def' ), new WikitextContent( '' ), null,
],
'type filter' => [
new WikitextContent( 'abc' ), new WikitextContent( 'def' ), WikitextContent::class,
new WikitextContent( 'abc' ), new WikitextContent( 'def' ), null,
],
'type filter (subclass)' => [
new WikitextContent( 'abc' ), new WikitextContent( 'def' ), TextContent::class,
new WikitextContent( 'abc' ), new WikitextContent( 'def' ), null,
],
'type filter (null)' => [
new WikitextContent( 'abc' ), null, TextContent::class,
new WikitextContent( 'abc' ), new WikitextContent( '' ), null,
],
'type filter failure (left)' => [
new TextContent( 'abc' ), new WikitextContent( 'def' ), WikitextContent::class,
null, null, ParameterTypeException::class,
],
'type filter failure (right)' => [
new WikitextContent( 'abc' ), new TextContent( 'def' ), WikitextContent::class,
null, null, ParameterTypeException::class,
],
'type filter (array syntax)' => [
new WikitextContent( 'abc' ), new JsonContent( 'def' ),
[ JsonContent::class, WikitextContent::class ],
new WikitextContent( 'abc' ), new JsonContent( 'def' ), null,
],
'type filter failure (array syntax)' => [
new WikitextContent( 'abc' ), new CssContent( 'def' ),
[ JsonContent::class, WikitextContent::class ],
null, null, ParameterTypeException::class,
],
];
}
}