2015-02-02 10:58:14 +00:00
|
|
|
<?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 {
|
2017-12-29 23:22:37 +00:00
|
|
|
|
2015-02-02 10:58:14 +00:00
|
|
|
public function testConstructor_InvalidArgument() {
|
2019-10-05 15:39:46 +00:00
|
|
|
$this->expectException( InvalidArgumentException::class );
|
2015-02-02 10:58:14 +00:00
|
|
|
|
|
|
|
|
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 );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$exporter->exportSites( [ $foo, $acme ] );
|
2015-02-02 10:58:14 +00:00
|
|
|
|
|
|
|
|
fseek( $tmp, 0 );
|
2015-06-17 20:01:00 +00:00
|
|
|
$xml = fread( $tmp, 16 * 1024 );
|
2015-02-02 10:58:14 +00:00
|
|
|
|
2019-12-14 10:27:56 +00:00
|
|
|
$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 );
|
2015-02-02 10:58:14 +00:00
|
|
|
|
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';
|
2015-02-02 10:58:14 +00:00
|
|
|
$xsdData = file_get_contents( $xsdFile );
|
|
|
|
|
|
|
|
|
|
$document = new DOMDocument();
|
|
|
|
|
$document->loadXML( $xml, LIBXML_NONET );
|
|
|
|
|
$document->schemaValidateSource( $xsdData );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function newSiteStore( SiteList $sites ) {
|
2022-07-14 12:42:07 +00:00
|
|
|
$store = $this->createMock( SiteStore::class );
|
2015-02-02 10:58:14 +00:00
|
|
|
|
|
|
|
|
$store->expects( $this->once() )
|
|
|
|
|
->method( 'saveSites' )
|
2022-06-05 23:39:02 +00:00
|
|
|
->willReturnCallback( static function ( $moreSites ) use ( $sites ) {
|
2015-02-02 10:58:14 +00:00
|
|
|
foreach ( $moreSites as $site ) {
|
|
|
|
|
$sites->setSite( $site );
|
|
|
|
|
}
|
2022-06-05 23:39:02 +00:00
|
|
|
} );
|
2015-02-02 10:58:14 +00:00
|
|
|
|
2021-04-22 08:28:11 +00:00
|
|
|
$store->method( 'getSites' )
|
|
|
|
|
->willReturn( new SiteList() );
|
2015-02-02 10:58:14 +00:00
|
|
|
|
|
|
|
|
return $store;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:36:19 +00:00
|
|
|
public static function provideRoundTrip() {
|
2015-02-02 10:58:14 +00:00
|
|
|
$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' );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
'empty' => [
|
2015-02-02 10:58:14 +00:00
|
|
|
new SiteList()
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2015-02-02 10:58:14 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
'some' => [
|
|
|
|
|
new SiteList( [ $foo, $acme, $dewiki ] ),
|
|
|
|
|
],
|
|
|
|
|
];
|
2015-02-02 10:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideRoundTrip()
|
|
|
|
|
*/
|
|
|
|
|
public function testRoundTrip( SiteList $sites ) {
|
|
|
|
|
$tmp = tmpfile();
|
|
|
|
|
$exporter = new SiteExporter( $tmp );
|
|
|
|
|
|
|
|
|
|
$exporter->exportSites( $sites );
|
|
|
|
|
|
|
|
|
|
fseek( $tmp, 0 );
|
2015-06-17 20:01:00 +00:00
|
|
|
$xml = fread( $tmp, 16 * 1024 );
|
2015-02-02 10:58:14 +00:00
|
|
|
|
|
|
|
|
$actualSites = new SiteList();
|
|
|
|
|
$store = $this->newSiteStore( $actualSites );
|
|
|
|
|
|
|
|
|
|
$importer = new SiteImporter( $store );
|
|
|
|
|
$importer->importFromXML( $xml );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $sites, $actualSites );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|