MediaWiki REL1_33
SiteImporterTest.php
Go to the documentation of this file.
1<?php
2
30class 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}
unserialize( $serialized)
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition COPYING.txt:326
assertSitesEqual( $expected, $actual, $message='')
testImportFromXML( $xml, array $expectedSites, $errorCount=0)
provideImportFromXML
newSiteImporter(array $expectedSites, $errorCount)
const TYPE_MEDIAWIKI
Definition Site.php:31
static newForType( $siteType)
Definition Site.php:646
const PATH_LINK
Definition Site.php:40
const TYPE_UNKNOWN
Definition Site.php:30
const ID_EQUIVALENT
Definition Site.php:36
const ID_INTERWIKI
Definition Site.php:35
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
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))
foreach( $res as $row) $serialized