From: Timo Tijhof Date: Thu, 25 Jul 2019 22:23:24 +0000 (+0100) Subject: resourceloader: Improve ResourceLoaderWikiModule test coverage X-Git-Tag: 1.34.0-rc.0~874^2 X-Git-Url: http://git.cyclocoop.org/%7B%24admin_url%7Dmembres/load.php?a=commitdiff_plain;h=e9cc45629e23f0d851711723a4809be3161fae0e;p=lhc%2Fweb%2Fwiklou.git resourceloader: Improve ResourceLoaderWikiModule test coverage * Remove redundant getContent() cases that were all testing the same. The redirect logic should indeed be tested, but exists in getContentObj(), not getContent(). This test was also mocking getContentObj() thus not actually testing what the case claims to test. Fortunately, the right test already exists (testGetContentForRedirects), so this is redundant. * Add actual coverage of successful outcomes for getContent (previously they were all error/null cases), with JS content, and with CSS content. * Fix broken test case for "Bad content model". This was not working because it mocked out getContentObj, thus it wasn't actually testing "bad content model", but rather pointlessly duplicated the previous test case. Fix it by actually making it use a WikitextContent object, which makes it test the branch that handles incompatible content models. Change-Id: I59af5318e536c730755352e9be8f995df1f56a86 --- diff --git a/includes/resourceloader/ResourceLoaderWikiModule.php b/includes/resourceloader/ResourceLoaderWikiModule.php index b1040733a0..a2501c40d0 100644 --- a/includes/resourceloader/ResourceLoaderWikiModule.php +++ b/includes/resourceloader/ResourceLoaderWikiModule.php @@ -412,6 +412,7 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule { return $titleInfo; } + /** @return array */ protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = __METHOD__ ) { $titleInfo = []; $batch = new LinkBatch; diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderWikiModuleTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderWikiModuleTest.php index 59649153e9..089431e952 100644 --- a/tests/phpunit/includes/resourceloader/ResourceLoaderWikiModuleTest.php +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderWikiModuleTest.php @@ -311,37 +311,41 @@ class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase { public static function provideGetContent() { yield 'Bad title' => [ null, '[x]' ]; - yield 'Dead redirect' => [ null, [ - 'text' => 'Dead redirect', - 'title' => 'Dead_redirect', - 'redirect' => 1, - ] ]; - yield 'Bad content model' => [ null, [ - 'text' => 'MediaWiki:Wikitext', - 'ns' => NS_MEDIAWIKI, - 'title' => 'Wikitext', - ] ]; + yield 'No JS content found' => [ null, [ - 'text' => 'MediaWiki:Script.js', + 'text' => 'MediaWiki:Foo.js', 'ns' => NS_MEDIAWIKI, - 'title' => 'Script.js', + 'title' => 'Foo.js', ] ]; - yield 'No CSS content found' => [ null, [ - 'text' => 'MediaWiki:Styles.css', + + yield 'JS content' => [ 'code;', [ + 'text' => 'MediaWiki:Foo.js', 'ns' => NS_MEDIAWIKI, - 'title' => 'Script.css', - ] ]; + 'title' => 'Foo.js', + ], new JavaScriptContent( 'code;' ) ]; + + yield 'CSS content' => [ 'code {}', [ + 'text' => 'MediaWiki:Foo.css', + 'ns' => NS_MEDIAWIKI, + 'title' => 'Foo.css', + ], new CssContent( 'code {}' ) ]; + + yield 'Wikitext content' => [ null, [ + 'text' => 'MediaWiki:Foo', + 'ns' => NS_MEDIAWIKI, + 'title' => 'Foo', + ], new WikitextContent( 'code;' ) ]; } /** * @dataProvider provideGetContent */ - public function testGetContent( $expected, $title ) { + public function testGetContent( $expected, $title, Content $contentObj = null ) { $context = $this->getResourceLoaderContext( [], new EmptyResourceLoader ); $module = $this->getMockBuilder( ResourceLoaderWikiModule::class ) ->setMethods( [ 'getContentObj' ] )->getMock(); $module->method( 'getContentObj' ) - ->willReturn( null ); + ->willReturn( $contentObj ); if ( is_array( $title ) ) { $title += [ 'ns' => NS_MAIN, 'id' => 1, 'len' => 1, 'redirect' => 0 ];