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