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