Debian Codegraph @

tests/phpunit/includes/site/SiteExporterTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Site;
4
5 use DOMDocument;
6 use InvalidArgumentException;
7 use MediaWiki\Site\MediaWikiSite;
8 use MediaWiki\Site\Site;
9 use MediaWiki\Site\SiteExporter;
10 use MediaWiki\Site\SiteImporter;
11 use MediaWiki\Site\SiteList;
12 use MediaWiki\Site\SiteStore;
13 use MediaWikiIntegrationTestCase;
14
15 /**
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 * http://www.gnu.org/copyleft/gpl.html
30 *
31 * @file
32 *
33 * @ingroup Site
34 * @ingroup Test
35 *
36 * @group Site
37 *
38 * @covers \MediaWiki\Site\SiteExporter
39 *
40 * @author Daniel Kinzler
41 */
42 class SiteExporterTest extends MediaWikiIntegrationTestCase {
43
44 public function testConstructor_InvalidArgument() {
45 $this->expectException( InvalidArgumentException::class );
46
47 new SiteExporter( 'Foo' );
48 }
49
50 public function testExportSites() {
51 $foo = Site::newForType( Site::TYPE_UNKNOWN );
52 $foo->setGlobalId( 'Foo' );
53
54 $acme = Site::newForType( Site::TYPE_UNKNOWN );
55 $acme->setGlobalId( 'acme.com' );
56 $acme->setGroup( 'Test' );
57 $acme->addLocalId( Site::ID_INTERWIKI, 'acme' );
58 $acme->setPath( Site::PATH_LINK, 'http://acme.com/' );
59
60 $tmp = tmpfile();
61 $exporter = new SiteExporter( $tmp );
62
63 $exporter->exportSites( [ $foo, $acme ] );
64
65 fseek( $tmp, 0 );
66 $xml = fread( $tmp, 16 * 1024 );
67
68 $this->assertStringContainsString( '<sites ', $xml );
69 $this->assertStringContainsString( '<site>', $xml );
70 $this->assertStringContainsString( '<globalid>Foo</globalid>', $xml );
71 $this->assertStringContainsString( '</site>', $xml );
72 $this->assertStringContainsString( '<globalid>acme.com</globalid>', $xml );
73 $this->assertStringContainsString( '<group>Test</group>', $xml );
74 $this->assertStringContainsString( '<localid type="interwiki">acme</localid>', $xml );
75 $this->assertStringContainsString( '<path type="link">http://acme.com/</path>', $xml );
76 $this->assertStringContainsString( '</sites>', $xml );
77
78 $xsdFile = __DIR__ . '/../../../../docs/sitelist-1.0.xsd';
79 $xsdData = file_get_contents( $xsdFile );
80
81 $document = new DOMDocument();
82 $document->loadXML( $xml, LIBXML_NONET );
83 $document->schemaValidateSource( $xsdData );
84 }
85
86 private function newSiteStore( SiteList $sites ) {
87 $store = $this->createMock( SiteStore::class );
88
89 $store->expects( $this->once() )
90 ->method( 'saveSites' )
91 ->willReturnCallback( static function ( $moreSites ) use ( $sites ) {
92 foreach ( $moreSites as $site ) {
93 $sites->setSite( $site );
94 }
95 } );
96
97 $store->method( 'getSites' )
98 ->willReturn( new SiteList() );
99
100 return $store;
101 }
102
103 public static function provideRoundTrip() {
104 $foo = Site::newForType( Site::TYPE_UNKNOWN );
105 $foo->setGlobalId( 'Foo' );
106
107 $acme = Site::newForType( Site::TYPE_UNKNOWN );
108 $acme->setGlobalId( 'acme.com' );
109 $acme->setGroup( 'Test' );
110 $acme->addLocalId( Site::ID_INTERWIKI, 'acme' );
111 $acme->setPath( Site::PATH_LINK, 'http://acme.com/' );
112
113 $dewiki = Site::newForType( Site::TYPE_MEDIAWIKI );
114 $dewiki->setGlobalId( 'dewiki' );
115 $dewiki->setGroup( 'wikipedia' );
116 $dewiki->setForward( true );
117 $dewiki->addLocalId( Site::ID_INTERWIKI, 'wikipedia' );
118 $dewiki->addLocalId( Site::ID_EQUIVALENT, 'de' );
119 $dewiki->setPath( Site::PATH_LINK, 'http://de.wikipedia.org/w/' );
120 $dewiki->setPath( MediaWikiSite::PATH_PAGE, 'http://de.wikipedia.org/wiki/' );
121 $dewiki->setSource( 'meta.wikimedia.org' );
122
123 return [
124 'empty' => [
125 new SiteList()
126 ],
127
128 'some' => [
129 new SiteList( [ $foo, $acme, $dewiki ] ),
130 ],
131 ];
132 }
133
134 /**
135 * @dataProvider provideRoundTrip()
136 */
137 public function testRoundTrip( SiteList $sites ) {
138 $tmp = tmpfile();
139 $exporter = new SiteExporter( $tmp );
140
141 $exporter->exportSites( $sites );
142
143 fseek( $tmp, 0 );
144 $xml = fread( $tmp, 16 * 1024 );
145
146 $actualSites = new SiteList();
147 $store = $this->newSiteStore( $actualSites );
148
149 $importer = new SiteImporter( $store );
150 $importer->importFromXML( $xml );
151
152 $this->assertEquals( $sites, $actualSites );
153 }
154
155 }