mediawiki.api: Adopt async-await and assert.rejects() in various tests
[mediawiki.git] / tests / phpunit / includes / site / SiteImporterTest.php
blobb04660c7b8a3826878a9b054ef0199895135b10f
1 <?php
3 namespace MediaWiki\Tests\Site;
5 use Exception;
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;
14 /**
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
30 * @file
32 * @ingroup Site
33 * @ingroup Test
35 * @group Site
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 );
50 } );
52 $store->method( 'getSites' )
53 ->willReturn( new SiteList() );
55 $errorHandler = $this->createMock( LoggerInterface::class );
56 $errorHandler->expects( $this->exactly( $errorCount ) )
57 ->method( 'error' );
59 $importer = new SiteImporter( $store );
60 $importer->setExceptionCallback( [ $errorHandler, 'error' ] );
62 return $importer;
65 public function assertSitesEqual( $expected, $actual, $message = '' ) {
66 $this->assertEquals(
67 $this->getSerializedSiteList( $expected ),
68 $this->getSerializedSiteList( $actual ),
69 $message
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' );
93 return [
94 'empty' => [
95 '<sites></sites>',
96 [],
98 'no sites' => [
99 '<sites><Foo><globalid>Foo</globalid></Foo><Bar><quux>Bla</quux></Bar></sites>',
102 'minimal' => [
103 '<sites>' .
104 '<site><globalid>Foo</globalid></site>' .
105 '</sites>',
106 [ $foo ],
108 'full' => [
109 '<sites>' .
110 '<site><globalid>Foo</globalid></site>' .
111 '<site>' .
112 '<globalid>acme.com</globalid>' .
113 '<localid type="interwiki">acme</localid>' .
114 '<group>Test</group>' .
115 '<path type="link">http://acme.com/</path>' .
116 '</site>' .
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>' .
123 '<forward/>' .
124 '<path type="link">http://de.wikipedia.org/w/</path>' .
125 '<path type="page_path">http://de.wikipedia.org/wiki/</path>' .
126 '</site>' .
127 '</sites>',
128 [ $foo, $acme, $dewiki ],
130 'skip' => [
131 '<sites>' .
132 '<site><globalid>Foo</globalid></site>' .
133 '<site><barf>Foo</barf></site>' .
134 '<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>' .
140 '</site>' .
141 '</sites>',
142 [ $foo, $acme ],
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
193 * @return array[]
195 private function getSerializedSiteList( $sites ) {
196 $serialized = [];
198 foreach ( $sites as $site ) {
199 $key = $site->getGlobalId();
200 $data = unserialize( serialize( $site ) );
202 $serialized[$key] = $data;
205 return $serialized;