Merge "Exclude redirects from Special:Fewestrevisions"
[lhc/web/wiklou.git] / tests / phpunit / unit / includes / site / SiteImporterTest.php
1 <?php
2
3 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 *
21 * @ingroup Site
22 * @ingroup Test
23 *
24 * @group Site
25 *
26 * @covers SiteImporter
27 *
28 * @author Daniel Kinzler
29 */
30 class SiteImporterTest extends MediaWikiUnitTestCase {
31
32 private function newSiteImporter( array $expectedSites, $errorCount ) {
33 $store = $this->getMockBuilder( SiteStore::class )->getMock();
34
35 $store->expects( $this->once() )
36 ->method( 'saveSites' )
37 ->will( $this->returnCallback( function ( $sites ) use ( $expectedSites ) {
38 $this->assertSitesEqual( $expectedSites, $sites );
39 } ) );
40
41 $store->expects( $this->any() )
42 ->method( 'getSites' )
43 ->will( $this->returnValue( new SiteList() ) );
44
45 $errorHandler = $this->getMockBuilder( Psr\Log\LoggerInterface::class )->getMock();
46 $errorHandler->expects( $this->exactly( $errorCount ) )
47 ->method( 'error' );
48
49 $importer = new SiteImporter( $store );
50 $importer->setExceptionCallback( [ $errorHandler, 'error' ] );
51
52 return $importer;
53 }
54
55 public function assertSitesEqual( $expected, $actual, $message = '' ) {
56 $this->assertEquals(
57 $this->getSerializedSiteList( $expected ),
58 $this->getSerializedSiteList( $actual ),
59 $message
60 );
61 }
62
63 public function provideImportFromXML() {
64 $foo = Site::newForType( Site::TYPE_UNKNOWN );
65 $foo->setGlobalId( 'Foo' );
66
67 $acme = Site::newForType( Site::TYPE_UNKNOWN );
68 $acme->setGlobalId( 'acme.com' );
69 $acme->setGroup( 'Test' );
70 $acme->addLocalId( Site::ID_INTERWIKI, 'acme' );
71 $acme->setPath( Site::PATH_LINK, 'http://acme.com/' );
72
73 $dewiki = Site::newForType( Site::TYPE_MEDIAWIKI );
74 $dewiki->setGlobalId( 'dewiki' );
75 $dewiki->setGroup( 'wikipedia' );
76 $dewiki->setForward( true );
77 $dewiki->addLocalId( Site::ID_INTERWIKI, 'wikipedia' );
78 $dewiki->addLocalId( Site::ID_EQUIVALENT, 'de' );
79 $dewiki->setPath( Site::PATH_LINK, 'http://de.wikipedia.org/w/' );
80 $dewiki->setPath( MediaWikiSite::PATH_PAGE, 'http://de.wikipedia.org/wiki/' );
81 $dewiki->setSource( 'meta.wikimedia.org' );
82
83 return [
84 'empty' => [
85 '<sites></sites>',
86 [],
87 ],
88 'no sites' => [
89 '<sites><Foo><globalid>Foo</globalid></Foo><Bar><quux>Bla</quux></Bar></sites>',
90 [],
91 ],
92 'minimal' => [
93 '<sites>' .
94 '<site><globalid>Foo</globalid></site>' .
95 '</sites>',
96 [ $foo ],
97 ],
98 'full' => [
99 '<sites>' .
100 '<site><globalid>Foo</globalid></site>' .
101 '<site>' .
102 '<globalid>acme.com</globalid>' .
103 '<localid type="interwiki">acme</localid>' .
104 '<group>Test</group>' .
105 '<path type="link">http://acme.com/</path>' .
106 '</site>' .
107 '<site type="mediawiki">' .
108 '<source>meta.wikimedia.org</source>' .
109 '<globalid>dewiki</globalid>' .
110 '<localid type="interwiki">wikipedia</localid>' .
111 '<localid type="equivalent">de</localid>' .
112 '<group>wikipedia</group>' .
113 '<forward/>' .
114 '<path type="link">http://de.wikipedia.org/w/</path>' .
115 '<path type="page_path">http://de.wikipedia.org/wiki/</path>' .
116 '</site>' .
117 '</sites>',
118 [ $foo, $acme, $dewiki ],
119 ],
120 'skip' => [
121 '<sites>' .
122 '<site><globalid>Foo</globalid></site>' .
123 '<site><barf>Foo</barf></site>' .
124 '<site>' .
125 '<globalid>acme.com</globalid>' .
126 '<localid type="interwiki">acme</localid>' .
127 '<silly>boop!</silly>' .
128 '<group>Test</group>' .
129 '<path type="link">http://acme.com/</path>' .
130 '</site>' .
131 '</sites>',
132 [ $foo, $acme ],
133 1
134 ],
135 ];
136 }
137
138 /**
139 * @dataProvider provideImportFromXML
140 */
141 public function testImportFromXML( $xml, array $expectedSites, $errorCount = 0 ) {
142 $importer = $this->newSiteImporter( $expectedSites, $errorCount );
143 $importer->importFromXML( $xml );
144 }
145
146 public function testImportFromXML_malformed() {
147 $this->setExpectedException( Exception::class );
148
149 $store = $this->getMockBuilder( SiteStore::class )->getMock();
150 $importer = new SiteImporter( $store );
151 $importer->importFromXML( 'THIS IS NOT XML' );
152 }
153
154 public function testImportFromFile() {
155 $foo = Site::newForType( Site::TYPE_UNKNOWN );
156 $foo->setGlobalId( 'Foo' );
157
158 $acme = Site::newForType( Site::TYPE_UNKNOWN );
159 $acme->setGlobalId( 'acme.com' );
160 $acme->setGroup( 'Test' );
161 $acme->addLocalId( Site::ID_INTERWIKI, 'acme' );
162 $acme->setPath( Site::PATH_LINK, 'http://acme.com/' );
163
164 $dewiki = Site::newForType( Site::TYPE_MEDIAWIKI );
165 $dewiki->setGlobalId( 'dewiki' );
166 $dewiki->setGroup( 'wikipedia' );
167 $dewiki->setForward( true );
168 $dewiki->addLocalId( Site::ID_INTERWIKI, 'wikipedia' );
169 $dewiki->addLocalId( Site::ID_EQUIVALENT, 'de' );
170 $dewiki->setPath( Site::PATH_LINK, 'http://de.wikipedia.org/w/' );
171 $dewiki->setPath( MediaWikiSite::PATH_PAGE, 'http://de.wikipedia.org/wiki/' );
172 $dewiki->setSource( 'meta.wikimedia.org' );
173
174 $importer = $this->newSiteImporter( [ $foo, $acme, $dewiki ], 0 );
175
176 $file = __DIR__ . '/SiteImporterTest.xml';
177 $importer->importFromFile( $file );
178 }
179
180 /**
181 * @param Site[] $sites
182 *
183 * @return array[]
184 */
185 private function getSerializedSiteList( $sites ) {
186 $serialized = [];
187
188 foreach ( $sites as $site ) {
189 $key = $site->getGlobalId();
190 $data = unserialize( $site->serialize() );
191
192 $serialized[$key] = $data;
193 }
194
195 return $serialized;
196 }
197 }