Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / tests / phpunit / includes / site / SiteImporterTest.php
blob45241c5941cd88afda135ea80defcfca2ee8e902
1 <?php
3 /**
4 * Tests for the SiteImporter class.
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.
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.
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
21 * @file
23 * @ingroup Site
24 * @ingroup Test
26 * @group Site
28 * @covers SiteImporter
30 * @author Daniel Kinzler
32 class SiteImporterTest extends PHPUnit_Framework_TestCase {
34 private function newSiteImporter( array $expectedSites, $errorCount ) {
35 $store = $this->getMock( 'SiteStore' );
37 $store->expects( $this->once() )
38 ->method( 'saveSites' )
39 ->will( $this->returnCallback( function ( $sites ) use ( $expectedSites ) {
40 $this->assertSitesEqual( $expectedSites, $sites );
41 } ) );
43 $store->expects( $this->any() )
44 ->method( 'getSites' )
45 ->will( $this->returnValue( new SiteList() ) );
47 $errorHandler = $this->getMock( 'Psr\Log\LoggerInterface' );
48 $errorHandler->expects( $this->exactly( $errorCount ) )
49 ->method( 'error' );
51 $importer = new SiteImporter( $store );
52 $importer->setExceptionCallback( [ $errorHandler, 'error' ] );
54 return $importer;
57 public function assertSitesEqual( $expected, $actual, $message = '' ) {
58 $this->assertEquals(
59 $this->getSerializedSiteList( $expected ),
60 $this->getSerializedSiteList( $actual ),
61 $message
65 public function provideImportFromXML() {
66 $foo = Site::newForType( Site::TYPE_UNKNOWN );
67 $foo->setGlobalId( 'Foo' );
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/' );
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' );
85 return [
86 'empty' => [
87 '<sites></sites>',
88 [],
90 'no sites' => [
91 '<sites><Foo><globalid>Foo</globalid></Foo><Bar><quux>Bla</quux></Bar></sites>',
92 [],
94 'minimal' => [
95 '<sites>' .
96 '<site><globalid>Foo</globalid></site>' .
97 '</sites>',
98 [ $foo ],
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 ],
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 ],
141 * @dataProvider provideImportFromXML
143 public function testImportFromXML( $xml, array $expectedSites, $errorCount = 0 ) {
144 $importer = $this->newSiteImporter( $expectedSites, $errorCount );
145 $importer->importFromXML( $xml );
148 public function testImportFromXML_malformed() {
149 $this->setExpectedException( 'Exception' );
151 $store = $this->getMock( 'SiteStore' );
152 $importer = new SiteImporter( $store );
153 $importer->importFromXML( 'THIS IS NOT XML' );
156 public function testImportFromFile() {
157 $foo = Site::newForType( Site::TYPE_UNKNOWN );
158 $foo->setGlobalId( 'Foo' );
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/' );
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' );
176 $importer = $this->newSiteImporter( [ $foo, $acme, $dewiki ], 0 );
178 $file = __DIR__ . '/SiteImporterTest.xml';
179 $importer->importFromFile( $file );
183 * @param Site[] $sites
185 * @return array[]
187 private function getSerializedSiteList( $sites ) {
188 $serialized = [];
190 foreach ( $sites as $site ) {
191 $key = $site->getGlobalId();
192 $data = unserialize( $site->serialize() );
194 $serialized[$key] = $data;
197 return $serialized;