3 namespace MediaWiki\Tests\Site
;
6 use MediaWiki\Site\MediaWikiSite
;
7 use MediaWiki\Site\Site
;
8 use MediaWiki\Site\SiteImporter
;
9 use MediaWiki\Site\SiteList
;
10 use MediaWiki\Site\SiteStore
;
11 use MediaWikiIntegrationTestCase
;
12 use Psr\Log\LoggerInterface
;
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 * http://www.gnu.org/copyleft/gpl.html
37 * @covers \MediaWiki\Site\SiteImporter
39 * @author Daniel Kinzler
41 class SiteImporterTest
extends MediaWikiIntegrationTestCase
{
43 private function newSiteImporter( array $expectedSites, $errorCount ) {
44 $store = $this->createMock( SiteStore
::class );
46 $store->expects( $this->once() )
47 ->method( 'saveSites' )
48 ->willReturnCallback( function ( $sites ) use ( $expectedSites ) {
49 $this->assertSitesEqual( $expectedSites, $sites );
52 $store->method( 'getSites' )
53 ->willReturn( new SiteList() );
55 $errorHandler = $this->createMock( LoggerInterface
::class );
56 $errorHandler->expects( $this->exactly( $errorCount ) )
59 $importer = new SiteImporter( $store );
60 $importer->setExceptionCallback( [ $errorHandler, 'error' ] );
65 public function assertSitesEqual( $expected, $actual, $message = '' ) {
67 $this->getSerializedSiteList( $expected ),
68 $this->getSerializedSiteList( $actual ),
73 public static function provideImportFromXML() {
74 $foo = Site
::newForType( Site
::TYPE_UNKNOWN
);
75 $foo->setGlobalId( 'Foo' );
77 $acme = Site
::newForType( Site
::TYPE_UNKNOWN
);
78 $acme->setGlobalId( 'acme.com' );
79 $acme->setGroup( 'Test' );
80 $acme->addLocalId( Site
::ID_INTERWIKI
, 'acme' );
81 $acme->setPath( Site
::PATH_LINK
, 'http://acme.com/' );
83 $dewiki = Site
::newForType( Site
::TYPE_MEDIAWIKI
);
84 $dewiki->setGlobalId( 'dewiki' );
85 $dewiki->setGroup( 'wikipedia' );
86 $dewiki->setForward( true );
87 $dewiki->addLocalId( Site
::ID_INTERWIKI
, 'wikipedia' );
88 $dewiki->addLocalId( Site
::ID_EQUIVALENT
, 'de' );
89 $dewiki->setPath( Site
::PATH_LINK
, 'http://de.wikipedia.org/w/' );
90 $dewiki->setPath( MediaWikiSite
::PATH_PAGE
, 'http://de.wikipedia.org/wiki/' );
91 $dewiki->setSource( 'meta.wikimedia.org' );
99 '<sites><Foo><globalid>Foo</globalid></Foo><Bar><quux>Bla</quux></Bar></sites>',
104 '<site><globalid>Foo</globalid></site>' .
110 '<site><globalid>Foo</globalid></site>' .
112 '<globalid>acme.com</globalid>' .
113 '<localid type="interwiki">acme</localid>' .
114 '<group>Test</group>' .
115 '<path type="link">http://acme.com/</path>' .
117 '<site type="mediawiki">' .
118 '<source>meta.wikimedia.org</source>' .
119 '<globalid>dewiki</globalid>' .
120 '<localid type="interwiki">wikipedia</localid>' .
121 '<localid type="equivalent">de</localid>' .
122 '<group>wikipedia</group>' .
124 '<path type="link">http://de.wikipedia.org/w/</path>' .
125 '<path type="page_path">http://de.wikipedia.org/wiki/</path>' .
128 [ $foo, $acme, $dewiki ],
132 '<site><globalid>Foo</globalid></site>' .
133 '<site><barf>Foo</barf></site>' .
135 '<globalid>acme.com</globalid>' .
136 '<localid type="interwiki">acme</localid>' .
137 '<silly>boop!</silly>' .
138 '<group>Test</group>' .
139 '<path type="link">http://acme.com/</path>' .
149 * @dataProvider provideImportFromXML
151 public function testImportFromXML( $xml, array $expectedSites, $errorCount = 0 ) {
152 $importer = $this->newSiteImporter( $expectedSites, $errorCount );
153 $importer->importFromXML( $xml );
156 public function testImportFromXML_malformed() {
157 $this->expectException( Exception
::class );
159 $store = $this->createMock( SiteStore
::class );
160 $importer = new SiteImporter( $store );
161 $importer->importFromXML( 'THIS IS NOT XML' );
164 public function testImportFromFile() {
165 $foo = Site
::newForType( Site
::TYPE_UNKNOWN
);
166 $foo->setGlobalId( 'Foo' );
168 $acme = Site
::newForType( Site
::TYPE_UNKNOWN
);
169 $acme->setGlobalId( 'acme.com' );
170 $acme->setGroup( 'Test' );
171 $acme->addLocalId( Site
::ID_INTERWIKI
, 'acme' );
172 $acme->setPath( Site
::PATH_LINK
, 'http://acme.com/' );
174 $dewiki = Site
::newForType( Site
::TYPE_MEDIAWIKI
);
175 $dewiki->setGlobalId( 'dewiki' );
176 $dewiki->setGroup( 'wikipedia' );
177 $dewiki->setForward( true );
178 $dewiki->addLocalId( Site
::ID_INTERWIKI
, 'wikipedia' );
179 $dewiki->addLocalId( Site
::ID_EQUIVALENT
, 'de' );
180 $dewiki->setPath( Site
::PATH_LINK
, 'http://de.wikipedia.org/w/' );
181 $dewiki->setPath( MediaWikiSite
::PATH_PAGE
, 'http://de.wikipedia.org/wiki/' );
182 $dewiki->setSource( 'meta.wikimedia.org' );
184 $importer = $this->newSiteImporter( [ $foo, $acme, $dewiki ], 0 );
186 $file = __DIR__
. '/SiteImporterTest.xml';
187 $importer->importFromFile( $file );
191 * @param Site[] $sites
195 private function getSerializedSiteList( $sites ) {
198 foreach ( $sites as $site ) {
199 $key = $site->getGlobalId();
200 $data = unserialize( serialize( $site ) );
202 $serialized[$key] = $data;