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.
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.
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
26 * @covers SiteImporter
28 * @author Daniel Kinzler
30 class SiteImporterTest
extends MediaWikiIntegrationTestCase
{
32 private function newSiteImporter( array $expectedSites, $errorCount ) {
33 $store = $this->getMockBuilder( SiteStore
::class )->getMock();
35 $store->expects( $this->once() )
36 ->method( 'saveSites' )
37 ->will( $this->returnCallback( function ( $sites ) use ( $expectedSites ) {
38 $this->assertSitesEqual( $expectedSites, $sites );
41 $store->expects( $this->any() )
42 ->method( 'getSites' )
43 ->will( $this->returnValue( new SiteList() ) );
45 $errorHandler = $this->getMockBuilder( Psr\Log\LoggerInterface
::class )->getMock();
46 $errorHandler->expects( $this->exactly( $errorCount ) )
49 $importer = new SiteImporter( $store );
50 $importer->setExceptionCallback( [ $errorHandler, 'error' ] );
55 public function assertSitesEqual( $expected, $actual, $message = '' ) {
57 $this->getSerializedSiteList( $expected ),
58 $this->getSerializedSiteList( $actual ),
63 public function provideImportFromXML() {
64 $foo = Site
::newForType( Site
::TYPE_UNKNOWN
);
65 $foo->setGlobalId( 'Foo' );
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/' );
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' );
89 '<sites><Foo><globalid>Foo</globalid></Foo><Bar><quux>Bla</quux></Bar></sites>',
94 '<site><globalid>Foo</globalid></site>' .
100 '<site><globalid>Foo</globalid></site>' .
102 '<globalid>acme.com</globalid>' .
103 '<localid type="interwiki">acme</localid>' .
104 '<group>Test</group>' .
105 '<path type="link">http://acme.com/</path>' .
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>' .
114 '<path type="link">http://de.wikipedia.org/w/</path>' .
115 '<path type="page_path">http://de.wikipedia.org/wiki/</path>' .
118 [ $foo, $acme, $dewiki ],
122 '<site><globalid>Foo</globalid></site>' .
123 '<site><barf>Foo</barf></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>' .
139 * @dataProvider provideImportFromXML
141 public function testImportFromXML( $xml, array $expectedSites, $errorCount = 0 ) {
142 $importer = $this->newSiteImporter( $expectedSites, $errorCount );
143 $importer->importFromXML( $xml );
146 public function testImportFromXML_malformed() {
147 $this->setExpectedException( Exception
::class );
149 $store = $this->getMockBuilder( SiteStore
::class )->getMock();
150 $importer = new SiteImporter( $store );
151 $importer->importFromXML( 'THIS IS NOT XML' );
154 public function testImportFromFile() {
155 $foo = Site
::newForType( Site
::TYPE_UNKNOWN
);
156 $foo->setGlobalId( 'Foo' );
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/' );
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' );
174 $importer = $this->newSiteImporter( [ $foo, $acme, $dewiki ], 0 );
176 $file = __DIR__
. '/SiteImporterTest.xml';
177 $importer->importFromFile( $file );
181 * @param Site[] $sites
185 private function getSerializedSiteList( $sites ) {
188 foreach ( $sites as $site ) {
189 $key = $site->getGlobalId();
190 $data = unserialize( $site->serialize() );
192 $serialized[$key] = $data;