wiki.techinc.nl/tests/phpunit/includes/filerepo/FileBackendDBRepoWrapperTest.php

141 lines
3.7 KiB
PHP
Raw Normal View History

<?php
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-08-07 13:40:55 +00:00
class FileBackendDBRepoWrapperTest extends MediaWikiIntegrationTestCase {
protected $backendName = 'foo-backend';
protected $repoName = 'pureTestRepo';
/**
* @dataProvider getBackendPathsProvider
* @covers FileBackendDBRepoWrapper::getBackendPaths
*/
public function testGetBackendPaths(
$mocks,
$latest,
$dbReadsExpected,
$dbReturnValue,
$originalPath,
$expectedBackendPath,
$message ) {
list( $dbMock, $backendMock, $wrapperMock ) = $mocks;
$dbMock->expects( $dbReadsExpected )
->method( 'selectField' )
->willReturn( $dbReturnValue );
$newPaths = $wrapperMock->getBackendPaths( [ $originalPath ], $latest );
$this->assertEquals(
$expectedBackendPath,
$newPaths[0],
$message );
}
public function getBackendPathsProvider() {
$prefix = 'mwstore://' . $this->backendName . '/' . $this->repoName;
$mocksForCaching = $this->getMocks();
return [
[
$mocksForCaching,
false,
$this->once(),
'96246614d75ba1703bdfd5d7660bb57407aaf5d9',
$prefix . '-public/f/o/foobar.jpg',
$prefix . '-original/9/6/2/96246614d75ba1703bdfd5d7660bb57407aaf5d9',
'Public path translated correctly',
],
[
$mocksForCaching,
false,
$this->never(),
'96246614d75ba1703bdfd5d7660bb57407aaf5d9',
$prefix . '-public/f/o/foobar.jpg',
$prefix . '-original/9/6/2/96246614d75ba1703bdfd5d7660bb57407aaf5d9',
'LRU cache leveraged',
],
[
$this->getMocks(),
true,
$this->once(),
'96246614d75ba1703bdfd5d7660bb57407aaf5d9',
$prefix . '-public/f/o/foobar.jpg',
$prefix . '-original/9/6/2/96246614d75ba1703bdfd5d7660bb57407aaf5d9',
'Latest obtained',
],
[
$this->getMocks(),
true,
$this->never(),
'96246614d75ba1703bdfd5d7660bb57407aaf5d9',
$prefix . '-deleted/f/o/foobar.jpg',
$prefix . '-original/f/o/o/foobar',
'Deleted path translated correctly',
],
[
$this->getMocks(),
true,
$this->once(),
null,
$prefix . '-public/b/a/baz.jpg',
$prefix . '-public/b/a/baz.jpg',
'Path left untouched if no sha1 can be found',
],
];
}
/**
* @covers FileBackendDBRepoWrapper::getFileContentsMulti
*/
public function testGetFileContentsMulti() {
list( $dbMock, $backendMock, $wrapperMock ) = $this->getMocks();
$sha1Path = 'mwstore://' . $this->backendName . '/' . $this->repoName
. '-original/9/6/2/96246614d75ba1703bdfd5d7660bb57407aaf5d9';
$filenamePath = 'mwstore://' . $this->backendName . '/' . $this->repoName
. '-public/f/o/foobar.jpg';
$dbMock->expects( $this->once() )
->method( 'selectField' )
->willReturn( '96246614d75ba1703bdfd5d7660bb57407aaf5d9' );
$backendMock->expects( $this->once() )
->method( 'getFileContentsMulti' )
->willReturn( [ $sha1Path => 'foo' ] );
$result = $wrapperMock->getFileContentsMulti( [ 'srcs' => [ $filenamePath ] ] );
$this->assertEquals(
[ $filenamePath => 'foo' ],
$result,
'File contents paths translated properly'
);
}
protected function getMocks() {
$dbMock = $this->getMockBuilder( Wikimedia\Rdbms\IDatabase::class )
->disableOriginalClone()
->disableOriginalConstructor()
->getMock();
$backendMock = $this->getMockBuilder( FSFileBackend::class )
->setConstructorArgs( [ [
'name' => $this->backendName,
'wikiId' => wfWikiID()
] ] )
->getMock();
$wrapperMock = $this->getMockBuilder( FileBackendDBRepoWrapper::class )
->onlyMethods( [ 'getDB' ] )
->setConstructorArgs( [ [
'backend' => $backendMock,
'repoName' => $this->repoName,
'dbHandleFactory' => null
] ] )
->getMock();
$wrapperMock->method( 'getDB' )->willReturn( $dbMock );
return [ $dbMock, $backendMock, $wrapperMock ];
}
}