MediaWiki REL1_30
SiteImporterTest.php
Go to the documentation of this file.
1<?php
2
32class 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}
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:644
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