MediaWiki REL1_31
SiteImporterTest.php
Go to the documentation of this file.
1<?php
2
32class SiteImporterTest extends PHPUnit\Framework\TestCase {
33
34 use MediaWikiCoversValidator;
35 use PHPUnit4And6Compat;
36
37 private function newSiteImporter( array $expectedSites, $errorCount ) {
38 $store = $this->getMockBuilder( SiteStore::class )->getMock();
39
40 $store->expects( $this->once() )
41 ->method( 'saveSites' )
42 ->will( $this->returnCallback( function ( $sites ) use ( $expectedSites ) {
43 $this->assertSitesEqual( $expectedSites, $sites );
44 } ) );
45
46 $store->expects( $this->any() )
47 ->method( 'getSites' )
48 ->will( $this->returnValue( new SiteList() ) );
49
50 $errorHandler = $this->getMockBuilder( Psr\Log\LoggerInterface::class )->getMock();
51 $errorHandler->expects( $this->exactly( $errorCount ) )
52 ->method( 'error' );
53
54 $importer = new SiteImporter( $store );
55 $importer->setExceptionCallback( [ $errorHandler, 'error' ] );
56
57 return $importer;
58 }
59
60 public function assertSitesEqual( $expected, $actual, $message = '' ) {
61 $this->assertEquals(
62 $this->getSerializedSiteList( $expected ),
63 $this->getSerializedSiteList( $actual ),
64 $message
65 );
66 }
67
68 public function provideImportFromXML() {
70 $foo->setGlobalId( 'Foo' );
71
73 $acme->setGlobalId( 'acme.com' );
74 $acme->setGroup( 'Test' );
75 $acme->addLocalId( Site::ID_INTERWIKI, 'acme' );
76 $acme->setPath( Site::PATH_LINK, 'http://acme.com/' );
77
79 $dewiki->setGlobalId( 'dewiki' );
80 $dewiki->setGroup( 'wikipedia' );
81 $dewiki->setForward( true );
82 $dewiki->addLocalId( Site::ID_INTERWIKI, 'wikipedia' );
83 $dewiki->addLocalId( Site::ID_EQUIVALENT, 'de' );
84 $dewiki->setPath( Site::PATH_LINK, 'http://de.wikipedia.org/w/' );
85 $dewiki->setPath( MediaWikiSite::PATH_PAGE, 'http://de.wikipedia.org/wiki/' );
86 $dewiki->setSource( 'meta.wikimedia.org' );
87
88 return [
89 'empty' => [
90 '<sites></sites>',
91 [],
92 ],
93 'no sites' => [
94 '<sites><Foo><globalid>Foo</globalid></Foo><Bar><quux>Bla</quux></Bar></sites>',
95 [],
96 ],
97 'minimal' => [
98 '<sites>' .
99 '<site><globalid>Foo</globalid></site>' .
100 '</sites>',
101 [ $foo ],
102 ],
103 'full' => [
104 '<sites>' .
105 '<site><globalid>Foo</globalid></site>' .
106 '<site>' .
107 '<globalid>acme.com</globalid>' .
108 '<localid type="interwiki">acme</localid>' .
109 '<group>Test</group>' .
110 '<path type="link">http://acme.com/</path>' .
111 '</site>' .
112 '<site type="mediawiki">' .
113 '<source>meta.wikimedia.org</source>' .
114 '<globalid>dewiki</globalid>' .
115 '<localid type="interwiki">wikipedia</localid>' .
116 '<localid type="equivalent">de</localid>' .
117 '<group>wikipedia</group>' .
118 '<forward/>' .
119 '<path type="link">http://de.wikipedia.org/w/</path>' .
120 '<path type="page_path">http://de.wikipedia.org/wiki/</path>' .
121 '</site>' .
122 '</sites>',
123 [ $foo, $acme, $dewiki ],
124 ],
125 'skip' => [
126 '<sites>' .
127 '<site><globalid>Foo</globalid></site>' .
128 '<site><barf>Foo</barf></site>' .
129 '<site>' .
130 '<globalid>acme.com</globalid>' .
131 '<localid type="interwiki">acme</localid>' .
132 '<silly>boop!</silly>' .
133 '<group>Test</group>' .
134 '<path type="link">http://acme.com/</path>' .
135 '</site>' .
136 '</sites>',
137 [ $foo, $acme ],
138 1
139 ],
140 ];
141 }
142
146 public function testImportFromXML( $xml, array $expectedSites, $errorCount = 0 ) {
147 $importer = $this->newSiteImporter( $expectedSites, $errorCount );
148 $importer->importFromXML( $xml );
149 }
150
151 public function testImportFromXML_malformed() {
152 $this->setExpectedException( Exception::class );
153
154 $store = $this->getMockBuilder( SiteStore::class )->getMock();
155 $importer = new SiteImporter( $store );
156 $importer->importFromXML( 'THIS IS NOT XML' );
157 }
158
159 public function testImportFromFile() {
161 $foo->setGlobalId( 'Foo' );
162
164 $acme->setGlobalId( 'acme.com' );
165 $acme->setGroup( 'Test' );
166 $acme->addLocalId( Site::ID_INTERWIKI, 'acme' );
167 $acme->setPath( Site::PATH_LINK, 'http://acme.com/' );
168
170 $dewiki->setGlobalId( 'dewiki' );
171 $dewiki->setGroup( 'wikipedia' );
172 $dewiki->setForward( true );
173 $dewiki->addLocalId( Site::ID_INTERWIKI, 'wikipedia' );
174 $dewiki->addLocalId( Site::ID_EQUIVALENT, 'de' );
175 $dewiki->setPath( Site::PATH_LINK, 'http://de.wikipedia.org/w/' );
176 $dewiki->setPath( MediaWikiSite::PATH_PAGE, 'http://de.wikipedia.org/wiki/' );
177 $dewiki->setSource( 'meta.wikimedia.org' );
178
179 $importer = $this->newSiteImporter( [ $foo, $acme, $dewiki ], 0 );
180
181 $file = __DIR__ . '/SiteImporterTest.xml';
182 $importer->importFromFile( $file );
183 }
184
190 private function getSerializedSiteList( $sites ) {
191 $serialized = [];
192
193 foreach ( $sites as $site ) {
194 $key = $site->getGlobalId();
195 $data = unserialize( $site->serialize() );
196
197 $serialized[$key] = $data;
198 }
199
200 return $serialized;
201 }
202}
unserialize( $serialized)
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
foreach( $res as $row) $serialized