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