3 use MediaWiki\Config\HashConfig
;
4 use MediaWiki\MainConfigNames
;
10 * @covers \WikiImporter
12 class ImportFailureTest
extends MediaWikiLangTestCase
{
14 protected function setUp(): void
{
17 $slotRoleRegistry = $this->getServiceContainer()->getSlotRoleRegistry();
19 if ( !$slotRoleRegistry->isDefinedRole( 'ImportFailureTest' ) ) {
20 $slotRoleRegistry->defineRoleWithModel( 'ImportFailureTest', CONTENT_MODEL_WIKITEXT
);
25 * @param ImportSource $source
26 * @return WikiImporter
28 private function getImporter( ImportSource
$source ) {
29 $config = new HashConfig( [
30 MainConfigNames
::MaxArticleSize
=> 2048,
32 $services = $this->getServiceContainer();
33 $importer = new WikiImporter(
35 $this->getTestSysop()->getAuthority(),
37 $services->getHookContainer(),
38 $services->getContentLanguage(),
39 $services->getNamespaceInfo(),
40 $services->getTitleFactory(),
41 $services->getWikiPageFactory(),
42 $services->getWikiRevisionUploadImporter(),
43 $services->getContentHandlerFactory(),
44 $services->getSlotRoleRegistry()
50 * @param string $testName
54 private function getFileToImport( string $testName ) {
55 return __DIR__
. "/../../data/import/$testName.xml";
59 * @param string $prefix
60 * @param string[] $keys
64 private function getUniqueNames( string $prefix, array $keys ) {
67 foreach ( $keys as $k ) {
68 $names[$k] = "$prefix-$k-" . wfRandomString( 6 );
75 * @param string $xmlData
76 * @param string[] $pageTitles
80 private function injectPageTitles( string $xmlData, array $pageTitles ) {
81 $keys = array_map( static function ( $name ) {
82 return "{{{$name}_title}}";
83 }, array_keys( $pageTitles ) );
87 array_values( $pageTitles ),
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/' ];
103 * @dataProvider provideImportFailure
105 public function testImportFailure( $testName, $exceptionName, $exceptionMessage ) {
106 $fileName = $this->getFileToImport( $testName );
108 $pageKeys = [ 'page1', 'page2', 'page3', 'page4', ];
109 $pageTitles = $this->getUniqueNames( $testName, $pageKeys );
111 $xmlData = file_get_contents( $fileName );
112 $xmlData = $this->injectPageTitles( $xmlData, $pageTitles );
114 $source = new ImportStringSource( $xmlData );
115 $importer = $this->getImporter( $source );
116 $this->expectException( $exceptionName );
117 $this->expectExceptionMessageMatches( $exceptionMessage );
118 $importer->doImport();