jenkins-bot has submitted this change and it was merged. Change subject: WikidataPageBanner Add unit tests ......................................................................
WikidataPageBanner Add unit tests This patch introduces unit-testing for the following functions: * addDefaultBanner() * addCustomBanner() Additionally, it changes all static calls done using 'self' to use 'static'. This has been done to allow mocking of the class WikidataPageBanner. See http://php.net/manual/en/language.oop5.late-static-bindings.php Bug: T98832 Change-Id: Ia122bdef53f646d3e35733b80c2c6c688c161dbc --- M WikidataPageBanner.php M includes/WikidataPageBanner.hooks.php A tests/phpunit/BannerTest.php 3 files changed, 168 insertions(+), 5 deletions(-) Approvals: Jdlrobson: Looks good to me, approved jenkins-bot: Verified diff --git a/WikidataPageBanner.php b/WikidataPageBanner.php index be3ad4e..40984a6 100644 --- a/WikidataPageBanner.php +++ b/WikidataPageBanner.php @@ -51,6 +51,7 @@ // Load Banner modules, styles $wgHooks['BeforePageDisplay'][] = 'WikidataPageBanner::loadModules'; $wgHooks['ParserFirstCallInit'][] = 'WikidataPageBanner::onParserFirstCallInit'; +$wgHooks['UnitTestsList'][] = 'WikidataPageBanner::onUnitTestsList'; // include WikidataPageBanner class file require_once __DIR__ . "/includes/WikidataPageBanner.hooks.php"; diff --git a/includes/WikidataPageBanner.hooks.php b/includes/WikidataPageBanner.hooks.php index 6ced653..ae347b9 100644 --- a/includes/WikidataPageBanner.hooks.php +++ b/includes/WikidataPageBanner.hooks.php @@ -25,14 +25,14 @@ && !$title->isMainPage() ) { // if the page uses no 'PAGEBANNER' invocation, insert default banner or // WikidataBanner, first try to obtain bannername from Wikidata - $bannername = self::getWikidataBanner( $title ); + $bannername = static::getWikidataBanner( $title ); if ( $bannername === null ) { // if Wikidata banner not found, set bannername to default banner $bannername = $wgPBImage; } // add title to template parameters $paramsForBannerTemplate = array( 'title' => $title ); - $banner = self::getBannerHtml( $bannername, $paramsForBannerTemplate ); + $banner = static::getBannerHtml( $bannername, $paramsForBannerTemplate ); // add banner only if one found with given banner name if ( $banner !== null ) { // add the banner to output page if a valid one found @@ -99,7 +99,7 @@ $title = $argumentsFromParserFunction['pgname']; } $paramsForBannerTemplate['title'] = $title; - $banner = self::getBannerHtml( $bannername, $paramsForBannerTemplate ); + $banner = static::getBannerHtml( $bannername, $paramsForBannerTemplate ); // if given banner does not exist, return if ( $banner === null ) { return array( '', 'noparse' => true, 'isHTML' => true ); @@ -156,7 +156,7 @@ */ public static function getBannerHtml( $bannername, $options = array() ) { global $wgStandardSizes, $wgArticlePath; - $urls = self::getStandardSizeUrls( $bannername ); + $urls = static::getStandardSizeUrls( $bannername ); $banner = null; /** @var String srcset attribute for <img> element of banner image */ $srcset = ""; @@ -213,7 +213,7 @@ global $wgStandardSizes; $urlSet = array(); foreach ( $wgStandardSizes as $size ) { - $url = self::getBannerUrl( $filename, $size ); + $url = static::getBannerUrl( $filename, $size ); if ( $url != null ) { $urlSet[] = $url; } @@ -281,4 +281,23 @@ } return $results; } + + /* + * UnitTestsList hook handler + * @see https://www.mediawiki.org/wiki/Manual:Hooks/UnitTestsList + * + * @param array $files + * @return bool + */ + public static function onUnitTestsList( &$files ) { + // traverse test/phpunit/ directory and add test files + $it = new RecursiveDirectoryIterator( __DIR__ . '/../tests/phpunit' ); + $it = new RecursiveIteratorIterator( $it ); + foreach ( $it as $path => $file ) { + if ( substr( $path, -8 ) === 'Test.php' ) { + $files[] = $path; + } + } + return true; + } } diff --git a/tests/phpunit/BannerTest.php b/tests/phpunit/BannerTest.php new file mode 100644 index 0000000..2a3832c --- /dev/null +++ b/tests/phpunit/BannerTest.php @@ -0,0 +1,143 @@ +<?php +/** + * @group WikidataPageBanner + */ + +/** + * Mock class for WikidataPageBanner + */ +class MockWikidataPageBanner extends WikidataPageBanner { + public static function getBannerHtml( $bannername, $options = array() ) { + if ( $bannername == 'NoBanner' ) { + return null; + } + return "Banner"; + } + public static function getWikidataBanner( $title ) { + if ( $title == 'NoWikidataBanner' ) { + return null; + } + return "Banner"; + } +} + +class BannerTest extends MediaWikiTestCase { + protected $exceptionFromAddDBData; + /** + * Set of pages. + * array follows the pattern: + * array( 0 => TestPageName, 1 => Namespace, 2 => isCustomBanner, 3 => expected articlebanner + * property + */ + protected $testPagesForDefaultBanner = array( + array( 'PageWithoutCustomBanner', NS_MAIN, false, "Banner" ), + array( 'PageWithCustomBanner', NS_MAIN, true, "Banner" ), + array( 'PageInTalkNamespace', NS_TALK, false, null ), + array( 'NoWikidataBanner', NS_MAIN, false, "Banner" ) + ); + /** + * Set of pages. + * array follows the pattern: + * array( 0 => TestPageName, 1 => Namespace, 2 => expected output of parser function + */ + protected $testPagesForCustomBanner = array( + array( 'PageWithCustomBanner', NS_MAIN, "Banner" ), + array( 'PageInTalkNamespace', NS_TALK, '' ), + array( 'NoBanner', NS_MAIN, '' ) + ); + + /** + * Add test pages to database + * @see MediaWikiTestCase::addDBData() + */ + public function addDBData() { + try { + foreach ( $this->testPagesForDefaultBanner as $page ) { + if ( !Title::newFromText( $page[0], $page[1] )->exists() ) { + $this->insertPage( $page[0], 'Some Text' ); + } + } + + } catch ( Exception $e ) { + $this->exceptionFromAddDBData = $e; + } + + } + protected function setUp() { + parent::setUp(); + $this->addDBData(); + } + + protected function tearDown() { + parent::tearDown(); + } + + /** + * @dataProvider provideTestDefaultBanner + * @covers addDefaultBanner(...) + */ + public function testDefaultBanner( $title, $ns, $isCustomBanner, $expected ) { + $article = $this->createPage( $title, $ns, $isCustomBanner ); + MockWikidataPageBanner::addDefaultBanner( $article ); + $out = $article->getContext()->getOutput(); + $this->assertEquals( $out->getProperty( 'articlebanner' ), $expected, + 'articlebanner property must only be set when a valid banner is added' ); + } + + /** + * @dataProvider provideTestCustomBanner + * @covers addCustomBanner(...) + */ + public function testCustomBanner( $title, $ns, $expected ) { + $parser = $this->createParser( $title, $ns ); + $output = MockWikidataPageBanner::addCustomBanner( $parser, $title ); + $this->assertEquals( $output[0], $expected, + 'articlebanner property must only be set when a valid banner is added' ); + } + + /** + * Helper function for testDefaultBanner + * @return Article Article object representing test pages + */ + protected function createPage( $title, $namespace, $isCustomBanner ) { + $context = new RequestContext(); + $curTitle = Title::newFromText( $title, $namespace ); + $context->setTitle( $curTitle ); + $article = Article::newFromTitle( $curTitle, $context ); + $parserOutput = new ParserOutput(); + $article->mParserOutput = $parserOutput; + if ( $isCustomBanner ) { + $parserOutput->setProperty( 'articlebanner', "Banner" ); + } + return $article; + } + + /** + * Helper function for testCustomBanner + * @return Parser Parser object associated with test pages + */ + protected function createParser( $title, $namespace ) { + $parser = $this->getMock( 'Parser' ); + $parserOutput = new ParserOutput(); + $parser->expects( $this->any() )->method( 'getOutput' ) + ->will( $this->returnValue( $parserOutput ) ); + $curTitle = Title::newFromText( $title, $namespace ); + $parser->expects( $this->any() )->method( 'getTitle' ) + ->will( $this->returnValue( $curTitle ) ); + return $parser; + } + + /** + * Data Provider for testDefaultBanner + */ + public function provideTestDefaultBanner() { + return $this->testPagesForDefaultBanner; + } + + /** + * Data Provider for testDefaultBanner + */ + public function provideTestCustomBanner() { + return $this->testPagesForCustomBanner; + } +} -- To view, visit https://gerrit.wikimedia.org/r/216735 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ia122bdef53f646d3e35733b80c2c6c688c161dbc Gerrit-PatchSet: 10 Gerrit-Project: mediawiki/extensions/WikidataPageBanner Gerrit-Branch: master Gerrit-Owner: Sumit <[email protected]> Gerrit-Reviewer: EBernhardson <[email protected]> Gerrit-Reviewer: Florianschmidtwelzow <[email protected]> Gerrit-Reviewer: Jdlrobson <[email protected]> Gerrit-Reviewer: Sumit <[email protected]> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
