| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Site; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use MediaWiki\Site\MediaWikiSite; |
| 7 |
use MediaWiki\Site\Site; |
| 8 |
use MediaWiki\Site\SiteImporter; |
| 9 |
use MediaWiki\Site\SiteList; |
| 10 |
use MediaWiki\Site\SiteStore; |
| 11 |
use MediaWikiIntegrationTestCase; |
| 12 |
use Psr\Log\LoggerInterface; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class SiteImporterTest extends MediaWikiIntegrationTestCase { |
| 42 |
|
| 43 |
private function newSiteImporter( array $expectedSites, $errorCount ) { |
| 44 |
$store = $this->createMock( SiteStore::class ); |
| 45 |
|
| 46 |
$store->expects( $this->once() ) |
| 47 |
->method( 'saveSites' ) |
| 48 |
->willReturnCallback( function ( $sites ) use ( $expectedSites ) { |
| 49 |
$this->assertSitesEqual( $expectedSites, $sites ); |
| 50 |
} ); |
| 51 |
|
| 52 |
$store->method( 'getSites' ) |
| 53 |
->willReturn( new SiteList() ); |
| 54 |
|
| 55 |
$errorHandler = $this->createMock( LoggerInterface::class ); |
| 56 |
$errorHandler->expects( $this->exactly( $errorCount ) ) |
| 57 |
->method( 'error' ); |
| 58 |
|
| 59 |
$importer = new SiteImporter( $store ); |
| 60 |
$importer->setExceptionCallback( [ $errorHandler, 'error' ] ); |
| 61 |
|
| 62 |
return $importer; |
| 63 |
} |
| 64 |
|
| 65 |
public function assertSitesEqual( $expected, $actual, $message = '' ) { |
| 66 |
$this->assertEquals( |
| 67 |
$this->getSerializedSiteList( $expected ), |
| 68 |
$this->getSerializedSiteList( $actual ), |
| 69 |
$message |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
public static function provideImportFromXML() { |
| 74 |
$foo = Site::newForType( Site::TYPE_UNKNOWN ); |
| 75 |
$foo->setGlobalId( 'Foo' ); |
| 76 |
|
| 77 |
$acme = Site::newForType( Site::TYPE_UNKNOWN ); |
| 78 |
$acme->setGlobalId( 'acme.com' ); |
| 79 |
$acme->setGroup( 'Test' ); |
| 80 |
$acme->addLocalId( Site::ID_INTERWIKI, 'acme' ); |
| 81 |
$acme->setPath( Site::PATH_LINK, 'http://acme.com/' ); |
| 82 |
|
| 83 |
$dewiki = Site::newForType( Site::TYPE_MEDIAWIKI ); |
| 84 |
$dewiki->setGlobalId( 'dewiki' ); |
| 85 |
$dewiki->setGroup( 'wikipedia' ); |
| 86 |
$dewiki->setForward( true ); |
| 87 |
$dewiki->addLocalId( Site::ID_INTERWIKI, 'wikipedia' ); |
| 88 |
$dewiki->addLocalId( Site::ID_EQUIVALENT, 'de' ); |
| 89 |
$dewiki->setPath( Site::PATH_LINK, 'http://de.wikipedia.org/w/' ); |
| 90 |
$dewiki->setPath( MediaWikiSite::PATH_PAGE, 'http://de.wikipedia.org/wiki/' ); |
| 91 |
$dewiki->setSource( 'meta.wikimedia.org' ); |
| 92 |
|
| 93 |
return [ |
| 94 |
'empty' => [ |
| 95 |
'<sites></sites>', |
| 96 |
[], |
| 97 |
], |
| 98 |
'no sites' => [ |
| 99 |
'<sites><Foo><globalid>Foo</globalid></Foo><Bar><quux>Bla</quux></Bar></sites>', |
| 100 |
[], |
| 101 |
], |
| 102 |
'minimal' => [ |
| 103 |
'<sites>' . |
| 104 |
'<site><globalid>Foo</globalid></site>' . |
| 105 |
'</sites>', |
| 106 |
[ $foo ], |
| 107 |
], |
| 108 |
'full' => [ |
| 109 |
'<sites>' . |
| 110 |
'<site><globalid>Foo</globalid></site>' . |
| 111 |
'<site>' . |
| 112 |
'<globalid>acme.com</globalid>' . |
| 113 |
'<localid type="interwiki">acme</localid>' . |
| 114 |
'<group>Test</group>' . |
| 115 |
'<path type="link">http://acme.com/</path>' . |
| 116 |
'</site>' . |
| 117 |
'<site type="mediawiki">' . |
| 118 |
'<source>meta.wikimedia.org</source>' . |
| 119 |
'<globalid>dewiki</globalid>' . |
| 120 |
'<localid type="interwiki">wikipedia</localid>' . |
| 121 |
'<localid type="equivalent">de</localid>' . |
| 122 |
'<group>wikipedia</group>' . |
| 123 |
'<forward/>' . |
| 124 |
'<path type="link">http://de.wikipedia.org/w/</path>' . |
| 125 |
'<path type="page_path">http://de.wikipedia.org/wiki/</path>' . |
| 126 |
'</site>' . |
| 127 |
'</sites>', |
| 128 |
[ $foo, $acme, $dewiki ], |
| 129 |
], |
| 130 |
'skip' => [ |
| 131 |
'<sites>' . |
| 132 |
'<site><globalid>Foo</globalid></site>' . |
| 133 |
'<site><barf>Foo</barf></site>' . |
| 134 |
'<site>' . |
| 135 |
'<globalid>acme.com</globalid>' . |
| 136 |
'<localid type="interwiki">acme</localid>' . |
| 137 |
'<silly>boop!</silly>' . |
| 138 |
'<group>Test</group>' . |
| 139 |
'<path type="link">http://acme.com/</path>' . |
| 140 |
'</site>' . |
| 141 |
'</sites>', |
| 142 |
[ $foo, $acme ], |
| 143 |
1 |
| 144 |
], |
| 145 |
]; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public function testImportFromXML( $xml, array $expectedSites, $errorCount = 0 ) { |
| 152 |
$importer = $this->newSiteImporter( $expectedSites, $errorCount ); |
| 153 |
$importer->importFromXML( $xml ); |
| 154 |
} |
| 155 |
|
| 156 |
public function testImportFromXML_malformed() { |
| 157 |
$this->expectException( Exception::class ); |
| 158 |
|
| 159 |
$store = $this->createMock( SiteStore::class ); |
| 160 |
$importer = new SiteImporter( $store ); |
| 161 |
$importer->importFromXML( 'THIS IS NOT XML' ); |
| 162 |
} |
| 163 |
|
| 164 |
public function testImportFromFile() { |
| 165 |
$foo = Site::newForType( Site::TYPE_UNKNOWN ); |
| 166 |
$foo->setGlobalId( 'Foo' ); |
| 167 |
|
| 168 |
$acme = Site::newForType( Site::TYPE_UNKNOWN ); |
| 169 |
$acme->setGlobalId( 'acme.com' ); |
| 170 |
$acme->setGroup( 'Test' ); |
| 171 |
$acme->addLocalId( Site::ID_INTERWIKI, 'acme' ); |
| 172 |
$acme->setPath( Site::PATH_LINK, 'http://acme.com/' ); |
| 173 |
|
| 174 |
$dewiki = Site::newForType( Site::TYPE_MEDIAWIKI ); |
| 175 |
$dewiki->setGlobalId( 'dewiki' ); |
| 176 |
$dewiki->setGroup( 'wikipedia' ); |
| 177 |
$dewiki->setForward( true ); |
| 178 |
$dewiki->addLocalId( Site::ID_INTERWIKI, 'wikipedia' ); |
| 179 |
$dewiki->addLocalId( Site::ID_EQUIVALENT, 'de' ); |
| 180 |
$dewiki->setPath( Site::PATH_LINK, 'http://de.wikipedia.org/w/' ); |
| 181 |
$dewiki->setPath( MediaWikiSite::PATH_PAGE, 'http://de.wikipedia.org/wiki/' ); |
| 182 |
$dewiki->setSource( 'meta.wikimedia.org' ); |
| 183 |
|
| 184 |
$importer = $this->newSiteImporter( [ $foo, $acme, $dewiki ], 0 ); |
| 185 |
|
| 186 |
$file = __DIR__ . '/SiteImporterTest.xml'; |
| 187 |
$importer->importFromFile( $file ); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
private function getSerializedSiteList( $sites ) { |
| 196 |
$serialized = []; |
| 197 |
|
| 198 |
foreach ( $sites as $site ) { |
| 199 |
$key = $site->getGlobalId(); |
| 200 |
$data = unserialize( serialize( $site ) ); |
| 201 |
|
| 202 |
$serialized[$key] = $data; |
| 203 |
} |
| 204 |
|
| 205 |
return $serialized; |
| 206 |
} |
| 207 |
} |