MediaWiki  REL1_31
SiteExporterTest.php
Go to the documentation of this file.
1 <?php
2 
32 class SiteExporterTest extends PHPUnit\Framework\TestCase {
33 
34  use MediaWikiCoversValidator;
35  use PHPUnit4And6Compat;
36 
37  public function testConstructor_InvalidArgument() {
38  $this->setExpectedException( InvalidArgumentException::class );
39 
40  new SiteExporter( 'Foo' );
41  }
42 
43  public function testExportSites() {
45  $foo->setGlobalId( 'Foo' );
46 
48  $acme->setGlobalId( 'acme.com' );
49  $acme->setGroup( 'Test' );
50  $acme->addLocalId( Site::ID_INTERWIKI, 'acme' );
51  $acme->setPath( Site::PATH_LINK, 'http://acme.com/' );
52 
53  $tmp = tmpfile();
54  $exporter = new SiteExporter( $tmp );
55 
56  $exporter->exportSites( [ $foo, $acme ] );
57 
58  fseek( $tmp, 0 );
59  $xml = fread( $tmp, 16 * 1024 );
60 
61  $this->assertContains( '<sites ', $xml );
62  $this->assertContains( '<site>', $xml );
63  $this->assertContains( '<globalid>Foo</globalid>', $xml );
64  $this->assertContains( '</site>', $xml );
65  $this->assertContains( '<globalid>acme.com</globalid>', $xml );
66  $this->assertContains( '<group>Test</group>', $xml );
67  $this->assertContains( '<localid type="interwiki">acme</localid>', $xml );
68  $this->assertContains( '<path type="link">http://acme.com/</path>', $xml );
69  $this->assertContains( '</sites>', $xml );
70 
71  // NOTE: HHVM (at least on wmf Jenkins) doesn't like file URLs.
72  $xsdFile = __DIR__ . '/../../../../docs/sitelist-1.0.xsd';
73  $xsdData = file_get_contents( $xsdFile );
74 
75  $document = new DOMDocument();
76  $document->loadXML( $xml, LIBXML_NONET );
77  $document->schemaValidateSource( $xsdData );
78  }
79 
80  private function newSiteStore( SiteList $sites ) {
81  $store = $this->getMockBuilder( SiteStore::class )->getMock();
82 
83  $store->expects( $this->once() )
84  ->method( 'saveSites' )
85  ->will( $this->returnCallback( function ( $moreSites ) use ( $sites ) {
86  foreach ( $moreSites as $site ) {
87  $sites->setSite( $site );
88  }
89  } ) );
90 
91  $store->expects( $this->any() )
92  ->method( 'getSites' )
93  ->will( $this->returnValue( new SiteList() ) );
94 
95  return $store;
96  }
97 
98  public function provideRoundTrip() {
100  $foo->setGlobalId( 'Foo' );
101 
103  $acme->setGlobalId( 'acme.com' );
104  $acme->setGroup( 'Test' );
105  $acme->addLocalId( Site::ID_INTERWIKI, 'acme' );
106  $acme->setPath( Site::PATH_LINK, 'http://acme.com/' );
107 
109  $dewiki->setGlobalId( 'dewiki' );
110  $dewiki->setGroup( 'wikipedia' );
111  $dewiki->setForward( true );
112  $dewiki->addLocalId( Site::ID_INTERWIKI, 'wikipedia' );
113  $dewiki->addLocalId( Site::ID_EQUIVALENT, 'de' );
114  $dewiki->setPath( Site::PATH_LINK, 'http://de.wikipedia.org/w/' );
115  $dewiki->setPath( MediaWikiSite::PATH_PAGE, 'http://de.wikipedia.org/wiki/' );
116  $dewiki->setSource( 'meta.wikimedia.org' );
117 
118  return [
119  'empty' => [
120  new SiteList()
121  ],
122 
123  'some' => [
124  new SiteList( [ $foo, $acme, $dewiki ] ),
125  ],
126  ];
127  }
128 
132  public function testRoundTrip( SiteList $sites ) {
133  $tmp = tmpfile();
134  $exporter = new SiteExporter( $tmp );
135 
136  $exporter->exportSites( $sites );
137 
138  fseek( $tmp, 0 );
139  $xml = fread( $tmp, 16 * 1024 );
140 
141  $actualSites = new SiteList();
142  $store = $this->newSiteStore( $actualSites );
143 
144  $importer = new SiteImporter( $store );
145  $importer->importFromXML( $xml );
146 
147  $this->assertEquals( $sites, $actualSites );
148  }
149 
150 }
Site\ID_EQUIVALENT
const ID_EQUIVALENT
Definition: Site.php:36
SiteExporterTest\testRoundTrip
testRoundTrip(SiteList $sites)
provideRoundTrip()
Definition: SiteExporterTest.php:132
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
SiteExporterTest\testExportSites
testExportSites()
Definition: SiteExporterTest.php:43
SiteImporter
Definition: SiteImporter.php:30
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
SiteExporterTest\newSiteStore
newSiteStore(SiteList $sites)
Definition: SiteExporterTest.php:80
SiteExporterTest\testConstructor_InvalidArgument
testConstructor_InvalidArgument()
Definition: SiteExporterTest.php:37
SiteExporterTest\provideRoundTrip
provideRoundTrip()
Definition: SiteExporterTest.php:98
Site\TYPE_UNKNOWN
const TYPE_UNKNOWN
Definition: Site.php:30
SiteList
Definition: SiteList.php:29
SiteExporterTest
Definition: SiteExporterTest.php:32
SiteList\setSite
setSite(Site $site)
Sets a site in the list.
Definition: SiteList.php:263
MediaWikiSite\PATH_PAGE
const PATH_PAGE
Definition: MediaWikiSite.php:40
SiteExporter
Definition: SiteExporter.php:30
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