MediaWiki  1.33.0
SiteImporterTest.php
Go to the documentation of this file.
1 <?php
2 
30 class SiteImporterTest extends PHPUnit\Framework\TestCase {
31 
32  use MediaWikiCoversValidator;
33  use PHPUnit4And6Compat;
34 
35  private function newSiteImporter( array $expectedSites, $errorCount ) {
36  $store = $this->getMockBuilder( SiteStore::class )->getMock();
37 
38  $store->expects( $this->once() )
39  ->method( 'saveSites' )
40  ->will( $this->returnCallback( function ( $sites ) use ( $expectedSites ) {
41  $this->assertSitesEqual( $expectedSites, $sites );
42  } ) );
43 
44  $store->expects( $this->any() )
45  ->method( 'getSites' )
46  ->will( $this->returnValue( new SiteList() ) );
47 
48  $errorHandler = $this->getMockBuilder( Psr\Log\LoggerInterface::class )->getMock();
49  $errorHandler->expects( $this->exactly( $errorCount ) )
50  ->method( 'error' );
51 
52  $importer = new SiteImporter( $store );
53  $importer->setExceptionCallback( [ $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() {
68  $foo->setGlobalId( 'Foo' );
69 
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 
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 [
87  'empty' => [
88  '<sites></sites>',
89  [],
90  ],
91  'no sites' => [
92  '<sites><Foo><globalid>Foo</globalid></Foo><Bar><quux>Bla</quux></Bar></sites>',
93  [],
94  ],
95  'minimal' => [
96  '<sites>' .
97  '<site><globalid>Foo</globalid></site>' .
98  '</sites>',
99  [ $foo ],
100  ],
101  'full' => [
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  [ $foo, $acme, $dewiki ],
122  ],
123  'skip' => [
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  [ $foo, $acme ],
136  1
137  ],
138  ];
139  }
140 
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::class );
151 
152  $store = $this->getMockBuilder( SiteStore::class )->getMock();
153  $importer = new SiteImporter( $store );
154  $importer->importFromXML( 'THIS IS NOT XML' );
155  }
156 
157  public function testImportFromFile() {
159  $foo->setGlobalId( 'Foo' );
160 
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 
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( [ $foo, $acme, $dewiki ], 0 );
178 
179  $file = __DIR__ . '/SiteImporterTest.xml';
180  $importer->importFromFile( $file );
181  }
182 
188  private function getSerializedSiteList( $sites ) {
189  $serialized = [];
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 }
Site\ID_EQUIVALENT
const ID_EQUIVALENT
Definition: Site.php:36
SiteImporterTest\testImportFromXML_malformed
testImportFromXML_malformed()
Definition: SiteImporterTest.php:149
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Definition: router.php:42
SiteImporterTest\testImportFromFile
testImportFromFile()
Definition: SiteImporterTest.php:157
$serialized
foreach( $res as $row) $serialized
Definition: testCompression.php:81
SiteImporterTest\testImportFromXML
testImportFromXML( $xml, array $expectedSites, $errorCount=0)
provideImportFromXML
Definition: SiteImporterTest.php:144
SiteImporter
Definition: SiteImporter.php:30
SiteImporterTest\provideImportFromXML
provideImportFromXML()
Definition: SiteImporterTest.php:66
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:35
Site\newForType
static newForType( $siteType)
Definition: Site.php:646
$data
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
Definition: generatePhpCharToUpperMappings.php:13
Site\TYPE_UNKNOWN
const TYPE_UNKNOWN
Definition: Site.php:30
SiteList
Definition: SiteList.php:29
SiteImporterTest\getSerializedSiteList
getSerializedSiteList( $sites)
Definition: SiteImporterTest.php:188
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
SiteImporterTest\assertSitesEqual
assertSitesEqual( $expected, $actual, $message='')
Definition: SiteImporterTest.php:58
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
SiteImporterTest\newSiteImporter
newSiteImporter(array $expectedSites, $errorCount)
Definition: SiteImporterTest.php:35
any
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition: COPYING.txt:326
SiteImporterTest
Definition: SiteImporterTest.php:30
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:142
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:9
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:52