diff options
Diffstat (limited to 'MLEB/Babel/tests/phpunit/BabelTest.php')
-rw-r--r-- | MLEB/Babel/tests/phpunit/BabelTest.php | 133 |
1 files changed, 55 insertions, 78 deletions
diff --git a/MLEB/Babel/tests/phpunit/BabelTest.php b/MLEB/Babel/tests/phpunit/BabelTest.php index f1e49a39..e8262ac9 100644 --- a/MLEB/Babel/tests/phpunit/BabelTest.php +++ b/MLEB/Babel/tests/phpunit/BabelTest.php @@ -3,13 +3,13 @@ namespace Babel\Tests; use Babel; +use MediaWiki\MediaWikiServices; use MediaWikiTestCase; use Parser; use ParserOptions; use ParserOutput; use Title; use User; -use WikiPage; /** * @covers Babel @@ -22,61 +22,52 @@ use WikiPage; */ class BabelTest extends MediaWikiTestCase { - protected function setUp() { + public function addDBDataOnce() { + // The '#babel' parser function normally auto-creates category pages via + // a DeferredUpdate. In PHPUnit context, because of wgCommandLineMode + // being true, DeferredUpdates are not actually "deferred". They run + // immediately. This is a problem because this would mean when we parse + // wikitext, BabelAutoCreate would parse and save another wiki page, + // whilst still inside a parser function. This is not allowed in MediaWiki + // and Parser::parse protects against this with an exception. + // + // FIXME: Make BabelAutoCreate less dependent on global state so we can simply + // disable this feature while testing, we don't need these pages for the test. + // + // We cannot simply make DeferredUpdates "deferred" (by disabling wgCommandLineMode), + // because that also means updates from core itself (such as the saving of category + // links) would be deferred, which we do need to observe below. + // + // Workaround this by mocking LinkCache to that BabelAutoCreate/Title::exists() + // perceives these as existing already and will skip auto-creation logic. + $this->setMwGlobals( 'wgCapitalLinks', false ); + MediaWikiServices::getInstance()->resetServiceForTesting( 'NamespaceInfo' ); + $linkCache = new \LinkCache( + MediaWikiServices::getInstance()->getTitleFormatter(), + \WANObjectCache::newEmpty(), + $this->createMock( \NamespaceInfo::class ) + ); + foreach ( [ 'en', 'en-N', 'en-1', 'es', 'es-2', 'de', 'de-N', + 'simple', 'simple-1', 'zh-Hant', 'zh-Hant-3' + ] as $name ) { + $linkCache->addGoodLinkObj( 1, new \TitleValue( NS_CATEGORY, $name ) ); + } + $this->setService( 'LinkCache', $linkCache ); + + $user = User::newFromName( 'User-1' ); + $user->addToDatabase(); + $this->insertPage( 'User:User-1', '{{#babel:en-1|es-2|de|SIMPLE-1|zh-hant-3}}' ); + } + + protected function setUp() : void { parent::setUp(); $this->setContentLang( 'qqx' ); $this->setMwGlobals( [ - // Note that individual tests will change this - 'wgBabelUseDatabase' => true, - 'wgBabelCentralApi' => false, + // Individual tests may change these 'wgBabelCentralDb' => false, 'wgCapitalLinks' => false, ] ); - $user = User::newFromName( 'User-1' ); - $user->addToDatabase(); - - // Avoid auto-creation of categories, since that may cause recursive parser invocation. - $this->createCategoryPage( 'en' ); - $this->createCategoryPage( 'en-1' ); - $this->createCategoryPage( 'es' ); - $this->createCategoryPage( 'es-2' ); - $this->createCategoryPage( 'de' ); - $this->createCategoryPage( 'de-N' ); - $this->createCategoryPage( 'simple' ); - $this->createCategoryPage( 'simple-1' ); - $this->createCategoryPage( 'zh-Hant' ); - $this->createCategoryPage( 'zh-Hant-3' ); - // These are only used if there is a bug in language code normalization, - // but missing categories here would obscure any underlying bug by - // failing with a hard to diagnose recursive parser invocation. - $this->createCategoryPage( 'en-simple' ); - $this->createCategoryPage( 'en-simple-1' ); - - $title = $user->getUserPage(); - $this->insertPage( - $title->getPrefixedText(), '{{#babel:en-1|es-2|de|SIMPLE-1|zh-hant-3}}' - ); - // Test on a category page too ( - $this->insertPage( Title::newFromText( - 'Category:X1', '{{#babel:en-1|es-2|de|simple-1|zh-Hant-3}}' - ) ); - $page = WikiPage::factory( $title ); - // Force a run of LinksUpdate - $updates = $page->getContent()->getSecondaryDataUpdates( $title ); - foreach ( $updates as $update ) { - $update->doUpdate(); - } - } - - /** - * @param string $name - */ - private function createCategoryPage( $name ) { - $category = Title::makeTitle( NS_CATEGORY, $name ); - if ( !$category->exists() ) { - $this->insertPage( $category, 'Test dummy' ); - } } /** @@ -84,29 +75,15 @@ class BabelTest extends MediaWikiTestCase { * @return Parser */ private function getParser( Title $title ) { - $options = new ParserOptions(); + $options = ParserOptions::newFromAnon(); $options->setIsPreview( true ); + $output = new ParserOutput(); - $parser = $this->getMockBuilder( Parser::class ) - ->disableOriginalConstructor() - ->getMock(); - - $parser->expects( $this->any() ) - ->method( 'getOptions' ) - ->will( $this->returnValue( $options ) ); - - $parser->expects( $this->any() ) - ->method( 'getTitle' ) - ->will( $this->returnValue( $title ) ); - - $parser->expects( $this->any() ) - ->method( 'getOutput' ) - ->will( $this->returnValue( new ParserOutput() ) ); - - $parser->expects( $this->any() ) - ->method( 'getDefaultSort' ) - ->will( $this->returnValue( '' ) ); - + $parser = $this->createMock( Parser::class ); + $parser->method( 'getOptions' )->willReturn( $options ); + $parser->method( 'getTitle' )->willReturn( $title ); + $parser->method( 'getOutput' )->willReturn( $output ); + $parser->method( 'getDefaultSort' )->willReturn( '' ); return $parser; } @@ -170,7 +147,7 @@ class BabelTest extends MediaWikiTestCase { $parser = $this->getParser( Title::newFromText( $pageName ) ); $wikiText = Babel::Render( $parser, 'en' ); $this->assertBabelBoxCount( 1, $wikiText ); - $this->assertContains( + $this->assertStringContainsString( '<div class="mw-babel-box mw-babel-box-N" dir="ltr">' . "\n" . '{|' @@ -197,7 +174,7 @@ class BabelTest extends MediaWikiTestCase { $parser = $this->getParser( Title::newFromText( $pageName ) ); $wikiText = Babel::Render( $parser, 'en' ); $this->assertBabelBoxCount( 1, $wikiText ); - $this->assertContains( + $this->assertStringContainsString( '<div class="mw-babel-box mw-babel-box-N" dir="ltr">' . "\n" . '{|' @@ -219,7 +196,7 @@ class BabelTest extends MediaWikiTestCase { $parser = $this->getParser( Title::newFromText( 'User:User-1' ) ); $wikiText = Babel::Render( $parser, 'EN-1', 'zh-Hant' ); $this->assertBabelBoxCount( 2, $wikiText ); - $this->assertContains( + $this->assertStringContainsString( '<div class="mw-babel-box mw-babel-box-1" dir="ltr">' . "\n" . '{|' @@ -236,7 +213,7 @@ class BabelTest extends MediaWikiTestCase { $this->assertHasCategory( $parser, 'en', '1' ); $this->assertHasCategory( $parser, 'en-1', '' ); - $this->assertContains( + $this->assertStringContainsString( '<div class="mw-babel-box mw-babel-box-N" dir="ltr">' . "\n" . '{|' @@ -280,7 +257,7 @@ class BabelTest extends MediaWikiTestCase { $parser = $this->getParser( Title::newFromText( 'User:User-1' ) ); $wikiText = Babel::Render( $parser, 'redLink' ); $this->assertBabelBoxCount( 0, $wikiText ); - $this->assertContains( + $this->assertStringContainsString( '<div class="mw-babel-notabox" dir="ltr">[[(babel-template: redLink)]]</div>', $wikiText ); @@ -290,7 +267,7 @@ class BabelTest extends MediaWikiTestCase { $parser = $this->getParser( Title::newFromText( 'User:User-1' ) ); $wikiText = Babel::Render( $parser, '<invalidTitle>' ); $this->assertBabelBoxCount( 0, $wikiText ); - $this->assertContains( + $this->assertStringContainsString( '<div class="mw-babel-notabox" dir="ltr">(babel-template: <invalidTitle>)</div>', $wikiText ); @@ -307,8 +284,8 @@ class BabelTest extends MediaWikiTestCase { */ public static function provideSettings() { return [ - [ [ 'wgBabelUseDatabase' => true ] ], - [ [ 'wgBabelUseDatabase' => false ] ], + 'lang info from db' => [ [ 'wgBabelUseDatabase' => true ] ], + 'lang info from categories' => [ [ 'wgBabelUseDatabase' => false ] ], ]; } |