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