wiki.techinc.nl/tests/phpunit/includes/site/SiteExporterTest.php

144 lines
4.2 KiB
PHP
Raw Normal View History

<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*
* @ingroup Site
* @ingroup Test
*
* @group Site
*
* @covers SiteExporter
*
* @author Daniel Kinzler
*/
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 SiteExporterTest extends MediaWikiIntegrationTestCase {
public function testConstructor_InvalidArgument() {
$this->expectException( InvalidArgumentException::class );
new SiteExporter( 'Foo' );
}
public function testExportSites() {
$foo = Site::newForType( Site::TYPE_UNKNOWN );
$foo->setGlobalId( 'Foo' );
$acme = Site::newForType( Site::TYPE_UNKNOWN );
$acme->setGlobalId( 'acme.com' );
$acme->setGroup( 'Test' );
$acme->addLocalId( Site::ID_INTERWIKI, 'acme' );
$acme->setPath( Site::PATH_LINK, 'http://acme.com/' );
$tmp = tmpfile();
$exporter = new SiteExporter( $tmp );
$exporter->exportSites( [ $foo, $acme ] );
fseek( $tmp, 0 );
$xml = fread( $tmp, 16 * 1024 );
$this->assertStringContainsString( '<sites ', $xml );
$this->assertStringContainsString( '<site>', $xml );
$this->assertStringContainsString( '<globalid>Foo</globalid>', $xml );
$this->assertStringContainsString( '</site>', $xml );
$this->assertStringContainsString( '<globalid>acme.com</globalid>', $xml );
$this->assertStringContainsString( '<group>Test</group>', $xml );
$this->assertStringContainsString( '<localid type="interwiki">acme</localid>', $xml );
$this->assertStringContainsString( '<path type="link">http://acme.com/</path>', $xml );
$this->assertStringContainsString( '</sites>', $xml );
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
$xsdFile = __DIR__ . '/../../../../docs/sitelist-1.0.xsd';
$xsdData = file_get_contents( $xsdFile );
$document = new DOMDocument();
$document->loadXML( $xml, LIBXML_NONET );
$document->schemaValidateSource( $xsdData );
}
private function newSiteStore( SiteList $sites ) {
$store = $this->createMock( SiteStore::class );
$store->expects( $this->once() )
->method( 'saveSites' )
->willReturnCallback( static function ( $moreSites ) use ( $sites ) {
foreach ( $moreSites as $site ) {
$sites->setSite( $site );
}
} );
$store->method( 'getSites' )
->willReturn( new SiteList() );
return $store;
}
public static function provideRoundTrip() {
$foo = Site::newForType( Site::TYPE_UNKNOWN );
$foo->setGlobalId( 'Foo' );
$acme = Site::newForType( Site::TYPE_UNKNOWN );
$acme->setGlobalId( 'acme.com' );
$acme->setGroup( 'Test' );
$acme->addLocalId( Site::ID_INTERWIKI, 'acme' );
$acme->setPath( Site::PATH_LINK, 'http://acme.com/' );
$dewiki = Site::newForType( Site::TYPE_MEDIAWIKI );
$dewiki->setGlobalId( 'dewiki' );
$dewiki->setGroup( 'wikipedia' );
$dewiki->setForward( true );
$dewiki->addLocalId( Site::ID_INTERWIKI, 'wikipedia' );
$dewiki->addLocalId( Site::ID_EQUIVALENT, 'de' );
$dewiki->setPath( Site::PATH_LINK, 'http://de.wikipedia.org/w/' );
$dewiki->setPath( MediaWikiSite::PATH_PAGE, 'http://de.wikipedia.org/wiki/' );
$dewiki->setSource( 'meta.wikimedia.org' );
return [
'empty' => [
new SiteList()
],
'some' => [
new SiteList( [ $foo, $acme, $dewiki ] ),
],
];
}
/**
* @dataProvider provideRoundTrip()
*/
public function testRoundTrip( SiteList $sites ) {
$tmp = tmpfile();
$exporter = new SiteExporter( $tmp );
$exporter->exportSites( $sites );
fseek( $tmp, 0 );
$xml = fread( $tmp, 16 * 1024 );
$actualSites = new SiteList();
$store = $this->newSiteStore( $actualSites );
$importer = new SiteImporter( $store );
$importer->importFromXML( $xml );
$this->assertEquals( $sites, $actualSites );
}
}