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
85 lines
2.3 KiB
PHP
85 lines
2.3 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @covers ZipDirectoryReader
|
|
*/
|
|
class ZipDirectoryReaderTest extends MediaWikiIntegrationTestCase {
|
|
|
|
protected $zipDir;
|
|
protected $entries;
|
|
|
|
protected function setUp() {
|
|
parent::setUp();
|
|
$this->zipDir = __DIR__ . '/../../data/zip';
|
|
}
|
|
|
|
function zipCallback( $entry ) {
|
|
$this->entries[] = $entry;
|
|
}
|
|
|
|
function readZipAssertError( $file, $error, $assertMessage ) {
|
|
$this->entries = [];
|
|
$status = ZipDirectoryReader::read( "{$this->zipDir}/$file", [ $this, 'zipCallback' ] );
|
|
$this->assertTrue( $status->hasMessage( $error ), $assertMessage );
|
|
}
|
|
|
|
function readZipAssertSuccess( $file, $assertMessage ) {
|
|
$this->entries = [];
|
|
$status = ZipDirectoryReader::read( "{$this->zipDir}/$file", [ $this, 'zipCallback' ] );
|
|
$this->assertTrue( $status->isOK(), $assertMessage );
|
|
}
|
|
|
|
public function testEmpty() {
|
|
$this->readZipAssertSuccess( 'empty.zip', 'Empty zip' );
|
|
}
|
|
|
|
public function testMultiDisk0() {
|
|
$this->readZipAssertError( 'split.zip', 'zip-unsupported',
|
|
'Split zip error' );
|
|
}
|
|
|
|
public function testNoSignature() {
|
|
$this->readZipAssertError( 'nosig.zip', 'zip-wrong-format',
|
|
'No signature should give "wrong format" error' );
|
|
}
|
|
|
|
public function testSimple() {
|
|
$this->readZipAssertSuccess( 'class.zip', 'Simple ZIP' );
|
|
$this->assertEquals( $this->entries, [ [
|
|
'name' => 'Class.class',
|
|
'mtime' => '20010115000000',
|
|
'size' => 1,
|
|
] ] );
|
|
}
|
|
|
|
public function testBadCentralEntrySignature() {
|
|
$this->readZipAssertError( 'wrong-central-entry-sig.zip', 'zip-bad',
|
|
'Bad central entry error' );
|
|
}
|
|
|
|
public function testTrailingBytes() {
|
|
// Due to T40432 this is now zip-wrong-format instead of zip-bad
|
|
$this->readZipAssertError( 'trail.zip', 'zip-wrong-format',
|
|
'Trailing bytes error' );
|
|
}
|
|
|
|
public function testWrongCDStart() {
|
|
$this->readZipAssertError( 'wrong-cd-start-disk.zip', 'zip-unsupported',
|
|
'Wrong CD start disk error' );
|
|
}
|
|
|
|
public function testCentralDirectoryGap() {
|
|
$this->readZipAssertError( 'cd-gap.zip', 'zip-bad',
|
|
'CD gap error' );
|
|
}
|
|
|
|
public function testCentralDirectoryTruncated() {
|
|
$this->readZipAssertError( 'cd-truncated.zip', 'zip-bad',
|
|
'CD truncated error (should hit unpack() overrun)' );
|
|
}
|
|
|
|
public function testLooksLikeZip64() {
|
|
$this->readZipAssertError( 'looks-like-zip64.zip', 'zip-unsupported',
|
|
'A file which looks like ZIP64 but isn\'t, should give error' );
|
|
}
|
|
}
|