| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Config\HashConfig; |
| 4 |
use MediaWiki\MainConfigNames; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class ImportFailureTest extends MediaWikiLangTestCase { |
| 13 |
|
| 14 |
protected function setUp(): void { |
| 15 |
parent::setUp(); |
| 16 |
|
| 17 |
$slotRoleRegistry = $this->getServiceContainer()->getSlotRoleRegistry(); |
| 18 |
|
| 19 |
if ( !$slotRoleRegistry->isDefinedRole( 'ImportFailureTest' ) ) { |
| 20 |
$slotRoleRegistry->defineRoleWithModel( 'ImportFailureTest', CONTENT_MODEL_WIKITEXT ); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
private function getImporter( ImportSource $source ) { |
| 29 |
$config = new HashConfig( [ |
| 30 |
MainConfigNames::MaxArticleSize => 2048, |
| 31 |
] ); |
| 32 |
$services = $this->getServiceContainer(); |
| 33 |
$importer = new WikiImporter( |
| 34 |
$source, |
| 35 |
$this->getTestSysop()->getAuthority(), |
| 36 |
$config, |
| 37 |
$services->getHookContainer(), |
| 38 |
$services->getContentLanguage(), |
| 39 |
$services->getNamespaceInfo(), |
| 40 |
$services->getTitleFactory(), |
| 41 |
$services->getWikiPageFactory(), |
| 42 |
$services->getWikiRevisionUploadImporter(), |
| 43 |
$services->getContentHandlerFactory(), |
| 44 |
$services->getSlotRoleRegistry() |
| 45 |
); |
| 46 |
return $importer; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
private function getFileToImport( string $testName ) { |
| 55 |
return __DIR__ . "/../../data/import/$testName.xml"; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
private function getUniqueNames( string $prefix, array $keys ) { |
| 65 |
$names = []; |
| 66 |
|
| 67 |
foreach ( $keys as $k ) { |
| 68 |
$names[$k] = "$prefix-$k-" . wfRandomString( 6 ); |
| 69 |
} |
| 70 |
|
| 71 |
return $names; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
private function injectPageTitles( string $xmlData, array $pageTitles ) { |
| 81 |
$keys = array_map( static function ( $name ) { |
| 82 |
return "{{{$name}_title}}"; |
| 83 |
}, array_keys( $pageTitles ) ); |
| 84 |
|
| 85 |
return str_replace( |
| 86 |
$keys, |
| 87 |
array_values( $pageTitles ), |
| 88 |
$xmlData |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
public static function provideImportFailure() { |
| 93 |
yield [ 'BadXML', RuntimeException::class, '/^XML error at line 3: Opening and ending tag mismatch:.*$/' ]; |
| 94 |
yield [ 'MissingMediaWikiTag', UnexpectedValueException::class, "/^Expected '<mediawiki>' tag, got .*$/" ]; |
| 95 |
yield [ 'MissingMainTextField', InvalidArgumentException::class, '/^Missing text field in import.$/' ]; |
| 96 |
yield [ 'MissingSlotTextField', InvalidArgumentException::class, '/^Missing text field in import.$/' ]; |
| 97 |
yield [ 'MissingSlotRole', RuntimeException::class, '/^Missing role for imported slot.$/' ]; |
| 98 |
yield [ 'UndefinedSlotRole', RuntimeException::class, '/^Undefined slot role .*$/' ]; |
| 99 |
yield [ 'UndefinedContentModel', MWUnknownContentModelException::class, '/not registered on this wiki/' ]; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function testImportFailure( $testName, $exceptionName, $exceptionMessage ) { |
| 106 |
$fileName = $this->getFileToImport( $testName ); |
| 107 |
|
| 108 |
$pageKeys = [ 'page1', 'page2', 'page3', 'page4', ]; |
| 109 |
$pageTitles = $this->getUniqueNames( $testName, $pageKeys ); |
| 110 |
|
| 111 |
$xmlData = file_get_contents( $fileName ); |
| 112 |
$xmlData = $this->injectPageTitles( $xmlData, $pageTitles ); |
| 113 |
|
| 114 |
$source = new ImportStringSource( $xmlData ); |
| 115 |
$importer = $this->getImporter( $source ); |
| 116 |
$this->expectException( $exceptionName ); |
| 117 |
$this->expectExceptionMessageMatches( $exceptionMessage ); |
| 118 |
$importer->doImport(); |
| 119 |
} |
| 120 |
} |