Krinkle has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/345953 )
Change subject: resourceloader: Improve code coverage for WikiModuleTest ...................................................................... resourceloader: Improve code coverage for WikiModuleTest preloadTitleInfo: * Add missing case for empty $moduleNames. * Add missing case for invalid page names. getContent: * Add missing case for bad title * Add missing case for dead redirect. * Add missing case for no content found. Change-Id: I44dde13cb0db19d91c4ff15a5abefd17353cad90 --- M includes/resourceloader/ResourceLoaderWikiModule.php M tests/phpunit/includes/resourceloader/ResourceLoaderWikiModuleTest.php 2 files changed, 111 insertions(+), 4 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/53/345953/1 diff --git a/includes/resourceloader/ResourceLoaderWikiModule.php b/includes/resourceloader/ResourceLoaderWikiModule.php index 92095f7..efa2165 100644 --- a/includes/resourceloader/ResourceLoaderWikiModule.php +++ b/includes/resourceloader/ResourceLoaderWikiModule.php @@ -148,7 +148,7 @@ protected function getContent( $titleText ) { $title = Title::newFromText( $titleText ); if ( !$title ) { - return null; + return null; // Bad title } // If the page is a redirect, follow the redirect. @@ -156,7 +156,7 @@ $content = $this->getContentObj( $title ); $title = $content ? $content->getUltimateRedirectTarget() : null; if ( !$title ) { - return null; + return null; // Dead redirect } } @@ -166,12 +166,12 @@ } elseif ( $handler->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT ) ) { $format = CONTENT_FORMAT_JAVASCRIPT; } else { - return null; + return null; // Bad content model } $content = $this->getContentObj( $title ); if ( !$content ) { - return null; + return null; // No content found } return $content->serialize( $format ); diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderWikiModuleTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderWikiModuleTest.php index 5cab8e2..2d0d958 100644 --- a/tests/phpunit/includes/resourceloader/ResourceLoaderWikiModuleTest.php +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderWikiModuleTest.php @@ -219,6 +219,113 @@ } /** + * @covers ResourceLoaderWikiModule::preloadTitleInfo + */ + public function testGetPreloadedBadTitle() { + // Mock values + $pages = [ + // Covers else branch for invalid page name + '[x]' => [ 'type' => 'styles' ], + ]; + $titleInfo = []; + + // Set up objects + $module = $this->getMockBuilder( 'TestResourceLoaderWikiModule' ) + ->setMethods( [ 'getPages' ] ) ->getMock(); + $module->method( 'getPages' )->willReturn( $pages ); + $module::$returnFetchTitleInfo = $titleInfo; + $rl = new EmptyResourceLoader(); + $rl->register( 'testmodule', $module ); + $context = new ResourceLoaderContext( $rl, new FauxRequest() ); + + // Act + TestResourceLoaderWikiModule::preloadTitleInfo( + $context, + wfGetDB( DB_REPLICA ), + [ 'testmodule' ] + ); + + // Assert + $module = TestingAccessWrapper::newFromObject( $module ); + $this->assertEquals( $titleInfo, $module->getTitleInfo( $context ), 'Title info' ); + } + + /** + * @covers ResourceLoaderWikiModule::preloadTitleInfo + */ + public function testGetPreloadedTitleInfoEmpty() { + $context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest() ); + // Covers early return + $this->assertSame( + null, + ResourceLoaderWikiModule::preloadTitleInfo( + $context, + wfGetDB( DB_REPLICA ), + [] + ) + ); + } + + public static function provideGetContent() { + return [ + 'Bad title' => [ null, '[x]' ], + 'Dead redirect' => [ null, [ + 'text' => 'Dead redirect', + 'title' => 'Dead_redirect', + 'redirect' => 1, + ] ], + 'Bad content model' => [ null, [ + 'text' => 'MediaWiki:Wikitext', + 'ns' => NS_MEDIAWIKI, + 'title' => 'Wikitext', + ] ], + 'No JS content found' => [ null, [ + 'text' => 'MediaWiki:Script.js', + 'ns' => NS_MEDIAWIKI, + 'title' => 'Script.js', + ] ], + 'No CSS content found' => [ null, [ + 'text' => 'MediaWiki:Styles.css', + 'ns' => NS_MEDIAWIKI, + 'title' => 'Script.css', + ] ], + ]; + } + + /** + * @covers ResourceLoaderWikiModule::getContent + * @dataProvider provideGetContent + */ + public function testGetContent( $expected, $title ) { + $context = $this->getResourceLoaderContext( [], new EmptyResourceLoader ); + $module = $this->getMockBuilder( 'ResourceLoaderWikiModule' ) + ->setMethods( [ 'getContentObj' ] ) ->getMock(); + $module->expects( $this->any() ) + ->method( 'getContentObj' )->willReturn( null ); + + if ( is_array( $title ) ) { + $title += [ 'ns' => NS_MAIN, 'id' => 1, 'len' => 1, 'redirect' => 0 ]; + $titleText = $title['text']; + // Mock Title db access via LinkCache + MediaWikiServices::getInstance()->getLinkCache()->addGoodLinkObj( + $title['id'], + new TitleValue( $title['ns'], $title['title'] ), + $title['len'], + $title['redirect'] + ); + } else { + $titleText = $title; + } + + $module = TestingAccessWrapper::newFromObject( $module ); + $this->assertEquals( + $expected, + $module->getContent( $titleText ) + ); + + } + + /** * @covers ResourceLoaderWikiModule::getContent */ public function testGetContentForRedirects() { -- To view, visit https://gerrit.wikimedia.org/r/345953 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I44dde13cb0db19d91c4ff15a5abefd17353cad90 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Krinkle <krinklem...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits