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 public function testConstructor_InvalidArgument() {
35 $this->setExpectedException( 'InvalidArgumentException' );
37 new SiteExporter( 'Foo' );
40 public function testExportSites() {
41 $foo = Site
::newForType( Site
::TYPE_UNKNOWN
);
42 $foo->setGlobalId( 'Foo' );
44 $acme = Site
::newForType( Site
::TYPE_UNKNOWN
);
45 $acme->setGlobalId( 'acme.com' );
46 $acme->setGroup( 'Test' );
47 $acme->addLocalId( Site
::ID_INTERWIKI
, 'acme' );
48 $acme->setPath( Site
::PATH_LINK
, 'http://acme.com/' );
51 $exporter = new SiteExporter( $tmp );
53 $exporter->exportSites( [ $foo, $acme ] );
56 $xml = fread( $tmp, 16 * 1024 );
58 $this->assertContains( '<sites ', $xml );
59 $this->assertContains( '<site>', $xml );
60 $this->assertContains( '<globalid>Foo</globalid>', $xml );
61 $this->assertContains( '</site>', $xml );
62 $this->assertContains( '<globalid>acme.com</globalid>', $xml );
63 $this->assertContains( '<group>Test</group>', $xml );
64 $this->assertContains( '<localid type="interwiki">acme</localid>', $xml );
65 $this->assertContains( '<path type="link">http://acme.com/</path>', $xml );
66 $this->assertContains( '</sites>', $xml );
68 // NOTE: HHVM (at least on wmf Jenkins) doesn't like file URLs.
69 $xsdFile = __DIR__
. '/../../../../docs/sitelist-1.0.xsd';
70 $xsdData = file_get_contents( $xsdFile );
72 $document = new DOMDocument();
73 $document->loadXML( $xml, LIBXML_NONET
);
74 $document->schemaValidateSource( $xsdData );
77 private function newSiteStore( SiteList
$sites ) {
78 $store = $this->getMock( 'SiteStore' );
80 $store->expects( $this->once() )
81 ->method( 'saveSites' )
82 ->will( $this->returnCallback( function ( $moreSites ) use ( $sites ) {
83 foreach ( $moreSites as $site ) {
84 $sites->setSite( $site );
88 $store->expects( $this->any() )
89 ->method( 'getSites' )
90 ->will( $this->returnValue( new SiteList() ) );
95 public function provideRoundTrip() {
96 $foo = Site
::newForType( Site
::TYPE_UNKNOWN
);
97 $foo->setGlobalId( 'Foo' );
99 $acme = Site
::newForType( Site
::TYPE_UNKNOWN
);
100 $acme->setGlobalId( 'acme.com' );
101 $acme->setGroup( 'Test' );
102 $acme->addLocalId( Site
::ID_INTERWIKI
, 'acme' );
103 $acme->setPath( Site
::PATH_LINK
, 'http://acme.com/' );
105 $dewiki = Site
::newForType( Site
::TYPE_MEDIAWIKI
);
106 $dewiki->setGlobalId( 'dewiki' );
107 $dewiki->setGroup( 'wikipedia' );
108 $dewiki->setForward( true );
109 $dewiki->addLocalId( Site
::ID_INTERWIKI
, 'wikipedia' );
110 $dewiki->addLocalId( Site
::ID_EQUIVALENT
, 'de' );
111 $dewiki->setPath( Site
::PATH_LINK
, 'http://de.wikipedia.org/w/' );
112 $dewiki->setPath( MediaWikiSite
::PATH_PAGE
, 'http://de.wikipedia.org/wiki/' );
113 $dewiki->setSource( 'meta.wikimedia.org' );
121 new SiteList( [ $foo, $acme, $dewiki ] ),
127 * @dataProvider provideRoundTrip()
129 public function testRoundTrip( SiteList
$sites ) {
131 $exporter = new SiteExporter( $tmp );
133 $exporter->exportSites( $sites );
136 $xml = fread( $tmp, 16 * 1024 );
138 $actualSites = new SiteList();
139 $store = $this->newSiteStore( $actualSites );
141 $importer = new SiteImporter( $store );
142 $importer->importFromXML( $xml );
144 $this->assertEquals( $sites, $actualSites );