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