4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
26 * @covers SiteExporter
28 * @author Daniel Kinzler
30 class SiteExporterTest
extends MediaWikiIntegrationTestCase
{
32 public function testConstructor_InvalidArgument() {
33 $this->setExpectedException( InvalidArgumentException
::class );
35 new SiteExporter( 'Foo' );
38 public function testExportSites() {
39 $foo = Site
::newForType( Site
::TYPE_UNKNOWN
);
40 $foo->setGlobalId( 'Foo' );
42 $acme = Site
::newForType( Site
::TYPE_UNKNOWN
);
43 $acme->setGlobalId( 'acme.com' );
44 $acme->setGroup( 'Test' );
45 $acme->addLocalId( Site
::ID_INTERWIKI
, 'acme' );
46 $acme->setPath( Site
::PATH_LINK
, 'http://acme.com/' );
49 $exporter = new SiteExporter( $tmp );
51 $exporter->exportSites( [ $foo, $acme ] );
54 $xml = fread( $tmp, 16 * 1024 );
56 $this->assertContains( '<sites ', $xml );
57 $this->assertContains( '<site>', $xml );
58 $this->assertContains( '<globalid>Foo</globalid>', $xml );
59 $this->assertContains( '</site>', $xml );
60 $this->assertContains( '<globalid>acme.com</globalid>', $xml );
61 $this->assertContains( '<group>Test</group>', $xml );
62 $this->assertContains( '<localid type="interwiki">acme</localid>', $xml );
63 $this->assertContains( '<path type="link">http://acme.com/</path>', $xml );
64 $this->assertContains( '</sites>', $xml );
66 // NOTE: HHVM (at least on wmf Jenkins) doesn't like file URLs.
67 $xsdFile = __DIR__
. '/../../../../docs/sitelist-1.0.xsd';
68 $xsdData = file_get_contents( $xsdFile );
70 $document = new DOMDocument();
71 $document->loadXML( $xml, LIBXML_NONET
);
72 $document->schemaValidateSource( $xsdData );
75 private function newSiteStore( SiteList
$sites ) {
76 $store = $this->getMockBuilder( SiteStore
::class )->getMock();
78 $store->expects( $this->once() )
79 ->method( 'saveSites' )
80 ->will( $this->returnCallback( function ( $moreSites ) use ( $sites ) {
81 foreach ( $moreSites as $site ) {
82 $sites->setSite( $site );
86 $store->expects( $this->any() )
87 ->method( 'getSites' )
88 ->will( $this->returnValue( new SiteList() ) );
93 public function provideRoundTrip() {
94 $foo = Site
::newForType( Site
::TYPE_UNKNOWN
);
95 $foo->setGlobalId( 'Foo' );
97 $acme = Site
::newForType( Site
::TYPE_UNKNOWN
);
98 $acme->setGlobalId( 'acme.com' );
99 $acme->setGroup( 'Test' );
100 $acme->addLocalId( Site
::ID_INTERWIKI
, 'acme' );
101 $acme->setPath( Site
::PATH_LINK
, 'http://acme.com/' );
103 $dewiki = Site
::newForType( Site
::TYPE_MEDIAWIKI
);
104 $dewiki->setGlobalId( 'dewiki' );
105 $dewiki->setGroup( 'wikipedia' );
106 $dewiki->setForward( true );
107 $dewiki->addLocalId( Site
::ID_INTERWIKI
, 'wikipedia' );
108 $dewiki->addLocalId( Site
::ID_EQUIVALENT
, 'de' );
109 $dewiki->setPath( Site
::PATH_LINK
, 'http://de.wikipedia.org/w/' );
110 $dewiki->setPath( MediaWikiSite
::PATH_PAGE
, 'http://de.wikipedia.org/wiki/' );
111 $dewiki->setSource( 'meta.wikimedia.org' );
119 new SiteList( [ $foo, $acme, $dewiki ] ),
125 * @dataProvider provideRoundTrip()
127 public function testRoundTrip( SiteList
$sites ) {
129 $exporter = new SiteExporter( $tmp );
131 $exporter->exportSites( $sites );
134 $xml = fread( $tmp, 16 * 1024 );
136 $actualSites = new SiteList();
137 $store = $this->newSiteStore( $actualSites );
139 $importer = new SiteImporter( $store );
140 $importer->importFromXML( $xml );
142 $this->assertEquals( $sites, $actualSites );