2015-03-10 13:26:14 +00:00
|
|
|
<?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 {
|
2015-03-10 13:26:14 +00:00
|
|
|
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' )
|
2021-04-22 08:28:11 +00:00
|
|
|
->willReturn( $dbReturnValue );
|
2015-03-10 13:26:14 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$newPaths = $wrapperMock->getBackendPaths( [ $originalPath ], $latest );
|
2015-03-10 13:26:14 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$expectedBackendPath,
|
|
|
|
|
$newPaths[0],
|
|
|
|
|
$message );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getBackendPathsProvider() {
|
|
|
|
|
$prefix = 'mwstore://' . $this->backendName . '/' . $this->repoName;
|
|
|
|
|
$mocksForCaching = $this->getMocks();
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[
|
2015-03-10 13:26:14 +00:00
|
|
|
$mocksForCaching,
|
|
|
|
|
false,
|
|
|
|
|
$this->once(),
|
|
|
|
|
'96246614d75ba1703bdfd5d7660bb57407aaf5d9',
|
|
|
|
|
$prefix . '-public/f/o/foobar.jpg',
|
|
|
|
|
$prefix . '-original/9/6/2/96246614d75ba1703bdfd5d7660bb57407aaf5d9',
|
|
|
|
|
'Public path translated correctly',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-03-10 13:26:14 +00:00
|
|
|
$mocksForCaching,
|
|
|
|
|
false,
|
|
|
|
|
$this->never(),
|
|
|
|
|
'96246614d75ba1703bdfd5d7660bb57407aaf5d9',
|
|
|
|
|
$prefix . '-public/f/o/foobar.jpg',
|
|
|
|
|
$prefix . '-original/9/6/2/96246614d75ba1703bdfd5d7660bb57407aaf5d9',
|
|
|
|
|
'LRU cache leveraged',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-03-10 13:26:14 +00:00
|
|
|
$this->getMocks(),
|
|
|
|
|
true,
|
|
|
|
|
$this->once(),
|
|
|
|
|
'96246614d75ba1703bdfd5d7660bb57407aaf5d9',
|
|
|
|
|
$prefix . '-public/f/o/foobar.jpg',
|
|
|
|
|
$prefix . '-original/9/6/2/96246614d75ba1703bdfd5d7660bb57407aaf5d9',
|
|
|
|
|
'Latest obtained',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-03-10 13:26:14 +00:00
|
|
|
$this->getMocks(),
|
|
|
|
|
true,
|
|
|
|
|
$this->never(),
|
|
|
|
|
'96246614d75ba1703bdfd5d7660bb57407aaf5d9',
|
|
|
|
|
$prefix . '-deleted/f/o/foobar.jpg',
|
|
|
|
|
$prefix . '-original/f/o/o/foobar',
|
|
|
|
|
'Deleted path translated correctly',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-03-10 13:26:14 +00:00
|
|
|
$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',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2015-03-10 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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' )
|
2021-04-22 08:28:11 +00:00
|
|
|
->willReturn( '96246614d75ba1703bdfd5d7660bb57407aaf5d9' );
|
2015-03-10 13:26:14 +00:00
|
|
|
|
|
|
|
|
$backendMock->expects( $this->once() )
|
2015-09-26 16:16:19 +00:00
|
|
|
->method( 'getFileContentsMulti' )
|
2021-04-22 08:28:11 +00:00
|
|
|
->willReturn( [ $sha1Path => 'foo' ] );
|
2015-03-10 13:26:14 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$result = $wrapperMock->getFileContentsMulti( [ 'srcs' => [ $filenamePath ] ] );
|
2015-03-10 13:26:14 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ $filenamePath => 'foo' ],
|
2015-03-10 13:26:14 +00:00
|
|
|
$result,
|
|
|
|
|
'File contents paths translated properly'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getMocks() {
|
2019-05-01 15:38:04 +00:00
|
|
|
$dbMock = $this->getMockBuilder( Wikimedia\Rdbms\IDatabase::class )
|
2017-01-03 16:46:29 +00:00
|
|
|
->disableOriginalClone()
|
2015-03-10 13:26:14 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
|
|
|
|
|
2018-01-13 00:02:09 +00:00
|
|
|
$backendMock = $this->getMockBuilder( FSFileBackend::class )
|
2017-04-05 23:39:50 +00:00
|
|
|
->setConstructorArgs( [ [
|
|
|
|
|
'name' => $this->backendName,
|
|
|
|
|
'wikiId' => wfWikiID()
|
|
|
|
|
] ] )
|
|
|
|
|
->getMock();
|
|
|
|
|
|
2018-01-13 00:02:09 +00:00
|
|
|
$wrapperMock = $this->getMockBuilder( FileBackendDBRepoWrapper::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'getDB' ] )
|
2017-04-05 23:39:50 +00:00
|
|
|
->setConstructorArgs( [ [
|
|
|
|
|
'backend' => $backendMock,
|
|
|
|
|
'repoName' => $this->repoName,
|
|
|
|
|
'dbHandleFactory' => null
|
|
|
|
|
] ] )
|
|
|
|
|
->getMock();
|
2015-03-10 13:26:14 +00:00
|
|
|
|
2021-04-22 08:28:11 +00:00
|
|
|
$wrapperMock->method( 'getDB' )->willReturn( $dbMock );
|
2015-03-10 13:26:14 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ $dbMock, $backendMock, $wrapperMock ];
|
2015-03-10 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
}
|