Merge "Remove unused 'XMPGetInfo' and 'XMPGetResults' hooks"
[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 private function newSiteImporter( array $expectedSites, $errorCount ) {
35 $store = $this->getMock( 'SiteStore' );
36
37 $that = $this;
38 $store->expects( $this->once() )
39 ->method( 'saveSites' )
40 ->will( $this->returnCallback( function ( $sites ) use ( $expectedSites, $that ) {
41 $that->assertSitesEqual( $expectedSites, $sites );
42 } ) );
43
44 $store->expects( $this->any() )
45 ->method( 'getSites' )
46 ->will( $this->returnValue( new SiteList() ) );
47
48 $errorHandler = $this->getMock( 'Psr\Log\LoggerInterface' );
49 $errorHandler->expects( $this->exactly( $errorCount ) )
50 ->method( 'error' );
51
52 $importer = new SiteImporter( $store );
53 $importer->setExceptionCallback( array( $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 array(
87 'empty' => array(
88 '<sites></sites>',
89 array(),
90 ),
91 'no sites' => array(
92 '<sites><Foo><globalid>Foo</globalid></Foo><Bar><quux>Bla</quux></Bar></sites>',
93 array(),
94 ),
95 'minimal' => array(
96 '<sites>' .
97 '<site><globalid>Foo</globalid></site>' .
98 '</sites>',
99 array( $foo ),
100 ),
101 'full' => array(
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 array( $foo, $acme, $dewiki ),
122 ),
123 'skip' => array(
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 array( $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' );
151
152 $store = $this->getMock( 'SiteStore' );
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( array( $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 = array();
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 }