Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / import / ImportFailureTest.php
blob1f3d4f5599dd747d88101e893d3dca08b8fa4aca
1 <?php
3 use MediaWiki\Config\HashConfig;
4 use MediaWiki\MainConfigNames;
6 /**
7 * Import failure test.
9 * @group Database
10 * @covers \WikiImporter
12 class ImportFailureTest extends MediaWikiLangTestCase {
14 protected function setUp(): void {
15 parent::setUp();
17 $slotRoleRegistry = $this->getServiceContainer()->getSlotRoleRegistry();
19 if ( !$slotRoleRegistry->isDefinedRole( 'ImportFailureTest' ) ) {
20 $slotRoleRegistry->defineRoleWithModel( 'ImportFailureTest', CONTENT_MODEL_WIKITEXT );
24 /**
25 * @param ImportSource $source
26 * @return WikiImporter
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()
46 return $importer;
49 /**
50 * @param string $testName
52 * @return string
54 private function getFileToImport( string $testName ) {
55 return __DIR__ . "/../../data/import/$testName.xml";
58 /**
59 * @param string $prefix
60 * @param string[] $keys
62 * @return string[]
64 private function getUniqueNames( string $prefix, array $keys ) {
65 $names = [];
67 foreach ( $keys as $k ) {
68 $names[$k] = "$prefix-$k-" . wfRandomString( 6 );
71 return $names;
74 /**
75 * @param string $xmlData
76 * @param string[] $pageTitles
78 * @return string
80 private function injectPageTitles( string $xmlData, array $pageTitles ) {
81 $keys = array_map( static function ( $name ) {
82 return "{{{$name}_title}}";
83 }, array_keys( $pageTitles ) );
85 return str_replace(
86 $keys,
87 array_values( $pageTitles ),
88 $xmlData
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();