build: Updating mediawiki/mediawiki-codesniffer to 16.0.0
[lhc/web/wiklou.git] / tests / phpunit / includes / site / SiteImporterTest.php
1 <?php
2
3 /**
4 * Tests for the SiteImporter class.
5 *
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.
10 *
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.
15 *
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
20 *
21 * @file
22 *
23 * @ingroup Site
24 * @ingroup Test
25 *
26 * @group Site
27 *
28 * @covers SiteImporter
29 *
30 * @author Daniel Kinzler
31 */
32 class SiteImporterTest extends PHPUnit\Framework\TestCase {
33
34 use MediaWikiCoversValidator;
35
36 private function newSiteImporter( array $expectedSites, $errorCount ) {
37 $store = $this->getMockBuilder( SiteStore::class )->getMock();
38
39 $store->expects( $this->once() )
40 ->method( 'saveSites' )
41 ->will( $this->returnCallback( function ( $sites ) use ( $expectedSites ) {
42 $this->assertSitesEqual( $expectedSites, $sites );
43 } ) );
44
45 $store->expects( $this->any() )
46 ->method( 'getSites' )
47 ->will( $this->returnValue( new SiteList() ) );
48
49 $errorHandler = $this->getMockBuilder( Psr\Log\LoggerInterface::class )->getMock();
50 $errorHandler->expects( $this->exactly( $errorCount ) )
51 ->method( 'error' );
52
53 $importer = new SiteImporter( $store );
54 $importer->setExceptionCallback( [ $errorHandler, 'error' ] );
55
56 return $importer;
57 }
58
59 public function assertSitesEqual( $expected, $actual, $message = '' ) {
60 $this->assertEquals(
61 $this->getSerializedSiteList( $expected ),
62 $this->getSerializedSiteList( $actual ),
63 $message
64 );
65 }
66
67 public function provideImportFromXML() {
68 $foo = Site::newForType( Site::TYPE_UNKNOWN );
69 $foo->setGlobalId( 'Foo' );
70
71 $acme = Site::newForType( Site::TYPE_UNKNOWN );
72 $acme->setGlobalId( 'acme.com' );
73 $acme->setGroup( 'Test' );
74 $acme->addLocalId( Site::ID_INTERWIKI, 'acme' );
75 $acme->setPath( Site::PATH_LINK, 'http://acme.com/' );
76
77 $dewiki = Site::newForType( Site::TYPE_MEDIAWIKI );
78 $dewiki->setGlobalId( 'dewiki' );
79 $dewiki->setGroup( 'wikipedia' );
80 $dewiki->setForward( true );
81 $dewiki->addLocalId( Site::ID_INTERWIKI, 'wikipedia' );
82 $dewiki->addLocalId( Site::ID_EQUIVALENT, 'de' );
83 $dewiki->setPath( Site::PATH_LINK, 'http://de.wikipedia.org/w/' );
84 $dewiki->setPath( MediaWikiSite::PATH_PAGE, 'http://de.wikipedia.org/wiki/' );
85 $dewiki->setSource( 'meta.wikimedia.org' );
86
87 return [
88 'empty' => [
89 '<sites></sites>',
90 [],
91 ],
92 'no sites' => [
93 '<sites><Foo><globalid>Foo</globalid></Foo><Bar><quux>Bla</quux></Bar></sites>',
94 [],
95 ],
96 'minimal' => [
97 '<sites>' .
98 '<site><globalid>Foo</globalid></site>' .
99 '</sites>',
100 [ $foo ],
101 ],
102 'full' => [
103 '<sites>' .
104 '<site><globalid>Foo</globalid></site>' .
105 '<site>' .
106 '<globalid>acme.com</globalid>' .
107 '<localid type="interwiki">acme</localid>' .
108 '<group>Test</group>' .
109 '<path type="link">http://acme.com/</path>' .
110 '</site>' .
111 '<site type="mediawiki">' .
112 '<source>meta.wikimedia.org</source>' .
113 '<globalid>dewiki</globalid>' .
114 '<localid type="interwiki">wikipedia</localid>' .
115 '<localid type="equivalent">de</localid>' .
116 '<group>wikipedia</group>' .
117 '<forward/>' .
118 '<path type="link">http://de.wikipedia.org/w/</path>' .
119 '<path type="page_path">http://de.wikipedia.org/wiki/</path>' .
120 '</site>' .
121 '</sites>',
122 [ $foo, $acme, $dewiki ],
123 ],
124 'skip' => [
125 '<sites>' .
126 '<site><globalid>Foo</globalid></site>' .
127 '<site><barf>Foo</barf></site>' .
128 '<site>' .
129 '<globalid>acme.com</globalid>' .
130 '<localid type="interwiki">acme</localid>' .
131 '<silly>boop!</silly>' .
132 '<group>Test</group>' .
133 '<path type="link">http://acme.com/</path>' .
134 '</site>' .
135 '</sites>',
136 [ $foo, $acme ],
137 1
138 ],
139 ];
140 }
141
142 /**
143 * @dataProvider provideImportFromXML
144 */
145 public function testImportFromXML( $xml, array $expectedSites, $errorCount = 0 ) {
146 $importer = $this->newSiteImporter( $expectedSites, $errorCount );
147 $importer->importFromXML( $xml );
148 }
149
150 public function testImportFromXML_malformed() {
151 $this->setExpectedException( Exception::class );
152
153 $store = $this->getMockBuilder( SiteStore::class )->getMock();
154 $importer = new SiteImporter( $store );
155 $importer->importFromXML( 'THIS IS NOT XML' );
156 }
157
158 public function testImportFromFile() {
159 $foo = Site::newForType( Site::TYPE_UNKNOWN );
160 $foo->setGlobalId( 'Foo' );
161
162 $acme = Site::newForType( Site::TYPE_UNKNOWN );
163 $acme->setGlobalId( 'acme.com' );
164 $acme->setGroup( 'Test' );
165 $acme->addLocalId( Site::ID_INTERWIKI, 'acme' );
166 $acme->setPath( Site::PATH_LINK, 'http://acme.com/' );
167
168 $dewiki = Site::newForType( Site::TYPE_MEDIAWIKI );
169 $dewiki->setGlobalId( 'dewiki' );
170 $dewiki->setGroup( 'wikipedia' );
171 $dewiki->setForward( true );
172 $dewiki->addLocalId( Site::ID_INTERWIKI, 'wikipedia' );
173 $dewiki->addLocalId( Site::ID_EQUIVALENT, 'de' );
174 $dewiki->setPath( Site::PATH_LINK, 'http://de.wikipedia.org/w/' );
175 $dewiki->setPath( MediaWikiSite::PATH_PAGE, 'http://de.wikipedia.org/wiki/' );
176 $dewiki->setSource( 'meta.wikimedia.org' );
177
178 $importer = $this->newSiteImporter( [ $foo, $acme, $dewiki ], 0 );
179
180 $file = __DIR__ . '/SiteImporterTest.xml';
181 $importer->importFromFile( $file );
182 }
183
184 /**
185 * @param Site[] $sites
186 *
187 * @return array[]
188 */
189 private function getSerializedSiteList( $sites ) {
190 $serialized = [];
191
192 foreach ( $sites as $site ) {
193 $key = $site->getGlobalId();
194 $data = unserialize( $site->serialize() );
195
196 $serialized[$key] = $data;
197 }
198
199 return $serialized;
200 }
201 }