wiki.techinc.nl/tests/phpunit/includes/Revision/MainSlotRoleHandlerTest.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.4 KiB
PHP

<?php
namespace MediaWiki\Tests\Revision;
use MediaWiki\Revision\MainSlotRoleHandler;
use PHPUnit\Framework\MockObject\MockObject;
use Title;
/**
* @covers \MediaWiki\Revision\MainSlotRoleHandler
*/
class MainSlotRoleHandlerTest extends \MediaWikiIntegrationTestCase {
private function makeTitleObject( $ns ) {
/** @var Title|MockObject $title */
$title = $this->getMockBuilder( Title::class )
->disableOriginalConstructor()
->getMock();
$title->method( 'getNamespace' )
->willReturn( $ns );
return $title;
}
/**
* @covers \MediaWiki\Revision\MainSlotRoleHandler::__construct
* @covers \MediaWiki\Revision\MainSlotRoleHandler::getRole()
* @covers \MediaWiki\Revision\MainSlotRoleHandler::getNameMessageKey()
* @covers \MediaWiki\Revision\MainSlotRoleHandler::getOutputLayoutHints()
*/
public function testConstruction() {
$handler = new MainSlotRoleHandler( [] );
$this->assertSame( 'main', $handler->getRole() );
$this->assertSame( 'slot-name-main', $handler->getNameMessageKey() );
$hints = $handler->getOutputLayoutHints();
$this->assertArrayHasKey( 'display', $hints );
$this->assertArrayHasKey( 'region', $hints );
$this->assertArrayHasKey( 'placement', $hints );
}
/**
* @covers \MediaWiki\Revision\MainSlotRoleHandler::getDefaultModel()
*/
public function testFetDefaultModel() {
$handler = new MainSlotRoleHandler( [ 100 => CONTENT_MODEL_TEXT ] );
// For the main handler, the namespace determins the default model
$titleMain = $this->makeTitleObject( NS_MAIN );
$this->assertSame( CONTENT_MODEL_WIKITEXT, $handler->getDefaultModel( $titleMain ) );
$title100 = $this->makeTitleObject( 100 );
$this->assertSame( CONTENT_MODEL_TEXT, $handler->getDefaultModel( $title100 ) );
}
/**
* @covers \MediaWiki\Revision\MainSlotRoleHandler::isAllowedModel()
*/
public function testIsAllowedModel() {
$handler = new MainSlotRoleHandler( [] );
// For the main handler, (nearly) all models are allowed
$title = $this->makeTitleObject( NS_MAIN );
$this->assertTrue( $handler->isAllowedModel( CONTENT_MODEL_WIKITEXT, $title ) );
$this->assertTrue( $handler->isAllowedModel( CONTENT_MODEL_TEXT, $title ) );
}
/**
* @covers \MediaWiki\Revision\MainSlotRoleHandler::supportsArticleCount()
*/
public function testSupportsArticleCount() {
$handler = new MainSlotRoleHandler( [] );
$this->assertTrue( $handler->supportsArticleCount() );
}
}