MediaWiki  1.30.0
SiteImporterTest.php
Go to the documentation of this file.
1 <?php
2 
32 class SiteImporterTest extends PHPUnit_Framework_TestCase {
33 
34  private function newSiteImporter( array $expectedSites, $errorCount ) {
35  $store = $this->getMockBuilder( 'SiteStore' )->getMock();
36 
37  $store->expects( $this->once() )
38  ->method( 'saveSites' )
39  ->will( $this->returnCallback( function ( $sites ) use ( $expectedSites ) {
40  $this->assertSitesEqual( $expectedSites, $sites );
41  } ) );
42 
43  $store->expects( $this->any() )
44  ->method( 'getSites' )
45  ->will( $this->returnValue( new SiteList() ) );
46 
47  $errorHandler = $this->getMockBuilder( 'Psr\Log\LoggerInterface' )->getMock();
48  $errorHandler->expects( $this->exactly( $errorCount ) )
49  ->method( 'error' );
50 
51  $importer = new SiteImporter( $store );
52  $importer->setExceptionCallback( [ $errorHandler, 'error' ] );
53 
54  return $importer;
55  }
56 
57  public function assertSitesEqual( $expected, $actual, $message = '' ) {
58  $this->assertEquals(
59  $this->getSerializedSiteList( $expected ),
60  $this->getSerializedSiteList( $actual ),
61  $message
62  );
63  }
64 
65  public function provideImportFromXML() {
67  $foo->setGlobalId( 'Foo' );
68 
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/' );
74 
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' );
84 
85  return [
86  'empty' => [
87  '<sites></sites>',
88  [],
89  ],
90  'no sites' => [
91  '<sites><Foo><globalid>Foo</globalid></Foo><Bar><quux>Bla</quux></Bar></sites>',
92  [],
93  ],
94  'minimal' => [
95  '<sites>' .
96  '<site><globalid>Foo</globalid></site>' .
97  '</sites>',
98  [ $foo ],
99  ],
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 ],
121  ],
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 ],
135  1
136  ],
137  ];
138  }
139 
143  public function testImportFromXML( $xml, array $expectedSites, $errorCount = 0 ) {
144  $importer = $this->newSiteImporter( $expectedSites, $errorCount );
145  $importer->importFromXML( $xml );
146  }
147 
148  public function testImportFromXML_malformed() {
149  $this->setExpectedException( 'Exception' );
150 
151  $store = $this->getMockBuilder( 'SiteStore' )->getMock();
152  $importer = new SiteImporter( $store );
153  $importer->importFromXML( 'THIS IS NOT XML' );
154  }
155 
156  public function testImportFromFile() {
158  $foo->setGlobalId( 'Foo' );
159 
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/' );
165 
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' );
175 
176  $importer = $this->newSiteImporter( [ $foo, $acme, $dewiki ], 0 );
177 
178  $file = __DIR__ . '/SiteImporterTest.xml';
179  $importer->importFromFile( $file );
180  }
181 
187  private function getSerializedSiteList( $sites ) {
188  $serialized = [];
189 
190  foreach ( $sites as $site ) {
191  $key = $site->getGlobalId();
192  $data = unserialize( $site->serialize() );
193 
194  $serialized[$key] = $data;
195  }
196 
197  return $serialized;
198  }
199 }
Site\ID_EQUIVALENT
const ID_EQUIVALENT
Definition: Site.php:36
SiteImporterTest\testImportFromXML_malformed
testImportFromXML_malformed()
Definition: SiteImporterTest.php:148
SiteImporterTest\testImportFromFile
testImportFromFile()
Definition: SiteImporterTest.php:156
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
$serialized
foreach( $res as $row) $serialized
Definition: testCompression.php:79
unserialize
unserialize( $serialized)
Definition: ApiMessage.php:185
SiteImporterTest\testImportFromXML
testImportFromXML( $xml, array $expectedSites, $errorCount=0)
provideImportFromXML
Definition: SiteImporterTest.php:143
SiteImporter
Definition: SiteImporter.php:30
SiteImporterTest\provideImportFromXML
provideImportFromXML()
Definition: SiteImporterTest.php:65
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:644
Site\TYPE_UNKNOWN
const TYPE_UNKNOWN
Definition: Site.php:30
SiteList
Definition: SiteList.php:29
SiteImporterTest\getSerializedSiteList
getSerializedSiteList( $sites)
Definition: SiteImporterTest.php:187
SiteImporterTest\assertSitesEqual
assertSitesEqual( $expected, $actual, $message='')
Definition: SiteImporterTest.php:57
SiteImporterTest\newSiteImporter
newSiteImporter(array $expectedSites, $errorCount)
Definition: SiteImporterTest.php:34
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: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:9
Site\PATH_LINK
const PATH_LINK
Definition: Site.php:40
Site\TYPE_MEDIAWIKI
const TYPE_MEDIAWIKI
Definition: Site.php:31
array
the array() calling protocol came about after MediaWiki 1.4rc1.