MediaWiki  REL1_31
SiteImporterTest.php
Go to the documentation of this file.
1 <?php
2 
32 class SiteImporterTest extends PHPUnit\Framework\TestCase {
33 
34  use MediaWikiCoversValidator;
35  use PHPUnit4And6Compat;
36 
37  private function newSiteImporter( array $expectedSites, $errorCount ) {
38  $store = $this->getMockBuilder( SiteStore::class )->getMock();
39 
40  $store->expects( $this->once() )
41  ->method( 'saveSites' )
42  ->will( $this->returnCallback( function ( $sites ) use ( $expectedSites ) {
43  $this->assertSitesEqual( $expectedSites, $sites );
44  } ) );
45 
46  $store->expects( $this->any() )
47  ->method( 'getSites' )
48  ->will( $this->returnValue( new SiteList() ) );
49 
50  $errorHandler = $this->getMockBuilder( Psr\Log\LoggerInterface::class )->getMock();
51  $errorHandler->expects( $this->exactly( $errorCount ) )
52  ->method( 'error' );
53 
54  $importer = new SiteImporter( $store );
55  $importer->setExceptionCallback( [ $errorHandler, 'error' ] );
56 
57  return $importer;
58  }
59 
60  public function assertSitesEqual( $expected, $actual, $message = '' ) {
61  $this->assertEquals(
62  $this->getSerializedSiteList( $expected ),
63  $this->getSerializedSiteList( $actual ),
64  $message
65  );
66  }
67 
68  public function provideImportFromXML() {
70  $foo->setGlobalId( 'Foo' );
71 
73  $acme->setGlobalId( 'acme.com' );
74  $acme->setGroup( 'Test' );
75  $acme->addLocalId( Site::ID_INTERWIKI, 'acme' );
76  $acme->setPath( Site::PATH_LINK, 'http://acme.com/' );
77 
79  $dewiki->setGlobalId( 'dewiki' );
80  $dewiki->setGroup( 'wikipedia' );
81  $dewiki->setForward( true );
82  $dewiki->addLocalId( Site::ID_INTERWIKI, 'wikipedia' );
83  $dewiki->addLocalId( Site::ID_EQUIVALENT, 'de' );
84  $dewiki->setPath( Site::PATH_LINK, 'http://de.wikipedia.org/w/' );
85  $dewiki->setPath( MediaWikiSite::PATH_PAGE, 'http://de.wikipedia.org/wiki/' );
86  $dewiki->setSource( 'meta.wikimedia.org' );
87 
88  return [
89  'empty' => [
90  '<sites></sites>',
91  [],
92  ],
93  'no sites' => [
94  '<sites><Foo><globalid>Foo</globalid></Foo><Bar><quux>Bla</quux></Bar></sites>',
95  [],
96  ],
97  'minimal' => [
98  '<sites>' .
99  '<site><globalid>Foo</globalid></site>' .
100  '</sites>',
101  [ $foo ],
102  ],
103  'full' => [
104  '<sites>' .
105  '<site><globalid>Foo</globalid></site>' .
106  '<site>' .
107  '<globalid>acme.com</globalid>' .
108  '<localid type="interwiki">acme</localid>' .
109  '<group>Test</group>' .
110  '<path type="link">http://acme.com/</path>' .
111  '</site>' .
112  '<site type="mediawiki">' .
113  '<source>meta.wikimedia.org</source>' .
114  '<globalid>dewiki</globalid>' .
115  '<localid type="interwiki">wikipedia</localid>' .
116  '<localid type="equivalent">de</localid>' .
117  '<group>wikipedia</group>' .
118  '<forward/>' .
119  '<path type="link">http://de.wikipedia.org/w/</path>' .
120  '<path type="page_path">http://de.wikipedia.org/wiki/</path>' .
121  '</site>' .
122  '</sites>',
123  [ $foo, $acme, $dewiki ],
124  ],
125  'skip' => [
126  '<sites>' .
127  '<site><globalid>Foo</globalid></site>' .
128  '<site><barf>Foo</barf></site>' .
129  '<site>' .
130  '<globalid>acme.com</globalid>' .
131  '<localid type="interwiki">acme</localid>' .
132  '<silly>boop!</silly>' .
133  '<group>Test</group>' .
134  '<path type="link">http://acme.com/</path>' .
135  '</site>' .
136  '</sites>',
137  [ $foo, $acme ],
138  1
139  ],
140  ];
141  }
142 
146  public function testImportFromXML( $xml, array $expectedSites, $errorCount = 0 ) {
147  $importer = $this->newSiteImporter( $expectedSites, $errorCount );
148  $importer->importFromXML( $xml );
149  }
150 
151  public function testImportFromXML_malformed() {
152  $this->setExpectedException( Exception::class );
153 
154  $store = $this->getMockBuilder( SiteStore::class )->getMock();
155  $importer = new SiteImporter( $store );
156  $importer->importFromXML( 'THIS IS NOT XML' );
157  }
158 
159  public function testImportFromFile() {
161  $foo->setGlobalId( 'Foo' );
162 
164  $acme->setGlobalId( 'acme.com' );
165  $acme->setGroup( 'Test' );
166  $acme->addLocalId( Site::ID_INTERWIKI, 'acme' );
167  $acme->setPath( Site::PATH_LINK, 'http://acme.com/' );
168 
170  $dewiki->setGlobalId( 'dewiki' );
171  $dewiki->setGroup( 'wikipedia' );
172  $dewiki->setForward( true );
173  $dewiki->addLocalId( Site::ID_INTERWIKI, 'wikipedia' );
174  $dewiki->addLocalId( Site::ID_EQUIVALENT, 'de' );
175  $dewiki->setPath( Site::PATH_LINK, 'http://de.wikipedia.org/w/' );
176  $dewiki->setPath( MediaWikiSite::PATH_PAGE, 'http://de.wikipedia.org/wiki/' );
177  $dewiki->setSource( 'meta.wikimedia.org' );
178 
179  $importer = $this->newSiteImporter( [ $foo, $acme, $dewiki ], 0 );
180 
181  $file = __DIR__ . '/SiteImporterTest.xml';
182  $importer->importFromFile( $file );
183  }
184 
190  private function getSerializedSiteList( $sites ) {
191  $serialized = [];
192 
193  foreach ( $sites as $site ) {
194  $key = $site->getGlobalId();
195  $data = unserialize( $site->serialize() );
196 
197  $serialized[$key] = $data;
198  }
199 
200  return $serialized;
201  }
202 }
Site\ID_EQUIVALENT
const ID_EQUIVALENT
Definition: Site.php:36
SiteImporterTest\testImportFromXML_malformed
testImportFromXML_malformed()
Definition: SiteImporterTest.php:151
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
array
the array() calling protocol came about after MediaWiki 1.4rc1.
SiteImporterTest\testImportFromFile
testImportFromFile()
Definition: SiteImporterTest.php:159
$serialized
foreach( $res as $row) $serialized
Definition: testCompression.php:81
unserialize
unserialize( $serialized)
Definition: ApiMessage.php:192
SiteImporterTest\testImportFromXML
testImportFromXML( $xml, array $expectedSites, $errorCount=0)
provideImportFromXML
Definition: SiteImporterTest.php:146
SiteImporter
Definition: SiteImporter.php:30
SiteImporterTest\provideImportFromXML
provideImportFromXML()
Definition: SiteImporterTest.php:68
Site\ID_INTERWIKI
const ID_INTERWIKI
Definition: Site.php:35
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
Site\newForType
static newForType( $siteType)
Definition: Site.php:646
Site\TYPE_UNKNOWN
const TYPE_UNKNOWN
Definition: Site.php:30
SiteList
Definition: SiteList.php:29
SiteImporterTest\getSerializedSiteList
getSerializedSiteList( $sites)
Definition: SiteImporterTest.php:190
SiteImporterTest\assertSitesEqual
assertSitesEqual( $expected, $actual, $message='')
Definition: SiteImporterTest.php:60
SiteImporterTest\newSiteImporter
newSiteImporter(array $expectedSites, $errorCount)
Definition: SiteImporterTest.php:37
SiteImporterTest
Definition: SiteImporterTest.php:32
MediaWikiSite\PATH_PAGE
const PATH_PAGE
Definition: MediaWikiSite.php:40
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:22
Site\PATH_LINK
const PATH_LINK
Definition: Site.php:40
Site\TYPE_MEDIAWIKI
const TYPE_MEDIAWIKI
Definition: Site.php:31
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:56