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
28 * @covers SiteImporter
30 * @licence GNU GPL v2+
31 * @author Daniel Kinzler
33 class SiteImporterTest
extends PHPUnit_Framework_TestCase
{
35 private function newSiteImporter( array $expectedSites, $errorCount ) {
36 $store = $this->getMock( 'SiteStore' );
39 $store->expects( $this->once() )
40 ->method( 'saveSites' )
41 ->will( $this->returnCallback( function ( $sites ) use ( $expectedSites, $self ) {
42 $self->assertSitesEqual( $expectedSites, $sites );
45 $store->expects( $this->any() )
46 ->method( 'getSites' )
47 ->will( $this->returnValue( new SiteList() ) );
49 $errorHandler = $this->getMock( 'Psr\Log\LoggerInterface' );
50 $errorHandler->expects( $this->exactly( $errorCount ) )
53 $importer = new SiteImporter( $store );
54 $importer->setExceptionCallback( array( $errorHandler, 'error' ) );
59 public function assertSitesEqual( $expected, $actual, $message = '' ) {
61 $this->getSerializedSiteList( $expected ),
62 $this->getSerializedSiteList( $actual ),
67 public function provideImportFromXML() {
68 $foo = Site
::newForType( Site
::TYPE_UNKNOWN
);
69 $foo->setGlobalId( 'Foo' );
71 $acme = Site
::newForType( Site
::TYPE_UNKNOWN
);
72 $acme->setGlobalId( 'acme.com' );
73 $acme->setGroup( 'Test' );
74 $acme->addLocalId( Site
::ID_INTERWIKI
, 'acme' );
75 $acme->setPath( Site
::PATH_LINK
, 'http://acme.com/' );
77 $dewiki = Site
::newForType( Site
::TYPE_MEDIAWIKI
);
78 $dewiki->setGlobalId( 'dewiki' );
79 $dewiki->setGroup( 'wikipedia' );
80 $dewiki->setForward( true );
81 $dewiki->addLocalId( Site
::ID_INTERWIKI
, 'wikipedia' );
82 $dewiki->addLocalId( Site
::ID_EQUIVALENT
, 'de' );
83 $dewiki->setPath( Site
::PATH_LINK
, 'http://de.wikipedia.org/w/' );
84 $dewiki->setPath( MediaWikiSite
::PATH_PAGE
, 'http://de.wikipedia.org/wiki/' );
85 $dewiki->setSource( 'meta.wikimedia.org' );
93 '<sites><Foo><globalid>Foo</globalid></Foo><Bar><quux>Bla</quux></Bar></sites>',
98 '<site><globalid>Foo</globalid></site>' .
104 '<site><globalid>Foo</globalid></site>' .
106 '<globalid>acme.com</globalid>' .
107 '<localid type="interwiki">acme</localid>' .
108 '<group>Test</group>' .
109 '<path type="link">http://acme.com/</path>' .
111 '<site type="mediawiki">' .
112 '<source>meta.wikimedia.org</source>' .
113 '<globalid>dewiki</globalid>' .
114 '<localid type="interwiki">wikipedia</localid>' .
115 '<localid type="equivalent">de</localid>' .
116 '<group>wikipedia</group>' .
118 '<path type="link">http://de.wikipedia.org/w/</path>' .
119 '<path type="page_path">http://de.wikipedia.org/wiki/</path>' .
122 array( $foo, $acme, $dewiki ),
126 '<site><globalid>Foo</globalid></site>' .
127 '<site><barf>Foo</barf></site>' .
129 '<globalid>acme.com</globalid>' .
130 '<localid type="interwiki">acme</localid>' .
131 '<silly>boop!</silly>' .
132 '<group>Test</group>' .
133 '<path type="link">http://acme.com/</path>' .
136 array( $foo, $acme ),
143 * @dataProvider provideImportFromXML
145 public function testImportFromXML( $xml, array $expectedSites, $errorCount = 0 ) {
146 $importer = $this->newSiteImporter( $expectedSites, $errorCount );
147 $importer->importFromXML( $xml );
150 public function testImportFromXML_malformed() {
151 $this->setExpectedException( 'Exception' );
153 $store = $this->getMock( 'SiteStore' );
154 $importer = new SiteImporter( $store );
155 $importer->importFromXML( 'THIS IS NOT XML' );
158 public function testImportFromFile() {
159 $foo = Site
::newForType( Site
::TYPE_UNKNOWN
);
160 $foo->setGlobalId( 'Foo' );
162 $acme = Site
::newForType( Site
::TYPE_UNKNOWN
);
163 $acme->setGlobalId( 'acme.com' );
164 $acme->setGroup( 'Test' );
165 $acme->addLocalId( Site
::ID_INTERWIKI
, 'acme' );
166 $acme->setPath( Site
::PATH_LINK
, 'http://acme.com/' );
168 $dewiki = Site
::newForType( Site
::TYPE_MEDIAWIKI
);
169 $dewiki->setGlobalId( 'dewiki' );
170 $dewiki->setGroup( 'wikipedia' );
171 $dewiki->setForward( true );
172 $dewiki->addLocalId( Site
::ID_INTERWIKI
, 'wikipedia' );
173 $dewiki->addLocalId( Site
::ID_EQUIVALENT
, 'de' );
174 $dewiki->setPath( Site
::PATH_LINK
, 'http://de.wikipedia.org/w/' );
175 $dewiki->setPath( MediaWikiSite
::PATH_PAGE
, 'http://de.wikipedia.org/wiki/' );
176 $dewiki->setSource( 'meta.wikimedia.org' );
178 $importer = $this->newSiteImporter( array( $foo, $acme, $dewiki ), 0 );
180 $file = __DIR__
. '/SiteImporterTest.xml';
181 $importer->importFromFile( $file );
185 * @param Site[] $sites
189 private function getSerializedSiteList( $sites ) {
190 $serialized = array();
192 foreach ( $sites as $site ) {
193 $key = $site->getGlobalId();
194 $data = unserialize( $site->serialize() );
196 $serialized[$key] = $data;