Sumit has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/216735

Change subject: WikidataPageBanner Add unit tests
......................................................................

WikidataPageBanner Add unit tests

This patch introduces unit-testing for the following functions:
* addDefaultBanner()
* addCustomBanner()
* getBannerUrl()
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
A tests/phpunit/GetBannerUrlTest.php
4 files changed, 203 insertions(+), 7 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikidataPageBanner 
refs/changes/35/216735/1

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 f57344f..4f79504 100644
--- a/includes/WikidataPageBanner.hooks.php
+++ b/includes/WikidataPageBanner.hooks.php
@@ -26,13 +26,13 @@
                                && !$title->isMainPage() ) {
                                // if the page uses no 'PAGEBANNER' invocation, 
insert default banner or
                                // WikidataBanner
-                               // only set articlebanner property on OutputPage
-                               $bannername = self::getWikidataBanner( $title );
+                               $bannername = static::getWikidataBanner( $title 
);
                                if ( $bannername === null ) {
                                        $bannername = $wgPBImage;
                                }
-                               $banner = self::getBannerHtml( $title, 
$bannername );
+                               $banner = static::getBannerHtml( $title, 
$bannername );
                                if ( $banner !== null ) {
+                                       // only set articlebanner property on 
OutputPage
                                        $out->addHtml( $banner );
                                        $out->setProperty( 'articlebanner', 
$banner );
                                }
@@ -76,7 +76,7 @@
                $title = $parser->getTitle();
                $ns = $title->getNamespace();
                if ( in_array( $ns, $wgBannerNamespaces ) && 
!$title->isMainPage() ) {
-                       $banner = self::getBannerHtml( $title, $bannername );
+                       $banner = static::getBannerHtml( $title, $bannername );
                        // if given banner does not exist, return
                        if ( $banner === null ) {
                                return array( '', 'noparse' => true, 'isHTML' 
=> true );
@@ -101,7 +101,7 @@
                $title = Title::makeTitleSafe( NS_IMAGE, $filename );
                $file = wfFindFile( $title );
                $options = array(
-                               'options' => array( 'min_range' => 0, 
'max_range' => 3000 )
+                               'options' => array( 'min_range' => 0, 
'max_range' => 5000 )
                        );
                // if file not found, return null
                if ( $file == null ) {
@@ -133,7 +133,7 @@
         */
        public static function getBannerHtml( $title, $bannername ) {
                global $wgStandardSizes, $wgArticlePath;
-               $urls = self::getStandardSizeUrls( $bannername );
+               $urls = static::getStandardSizeUrls( $bannername );
                $banner = null;
                /** @var String srcset attribute for <img> element of banner 
image */
                $srcset = "";
@@ -192,7 +192,7 @@
                global $wgStandardSizes;
                $urlSet = array();
                foreach ( $wgStandardSizes as $size ) {
-                       $url = self::getBannerUrl( $filename, $size );
+                       $url = static::getBannerUrl( $filename, $size );
                        if ( $url != null ) {
                                $urlSet[] = $url;
                        }
@@ -240,4 +240,16 @@
                }
                return $banner;
        }
+
+       /**
+        * UnitTestsList hook handler
+        * @see https://www.mediawiki.org/wiki/Manual:Hooks/UnitTestsList
+        *
+        * @param array $files
+        * @return bool
+        */
+       public static function onUnitTestsList( &$files ) {
+               $files[] = __DIR__ . './../tests/phpunit';
+               return true;
+       }
 }
diff --git a/tests/phpunit/BannerTest.php b/tests/phpunit/BannerTest.php
new file mode 100644
index 0000000..ebc3878
--- /dev/null
+++ b/tests/phpunit/BannerTest.php
@@ -0,0 +1,141 @@
+<?php
+/**
+ * @group WikidataPageBanner
+ */
+
+/**
+ * Mock class for WikidataPageBanner
+ */
+class MockWikidataPageBanner extends WikidataPageBanner {
+       public static function getBannerHtml( $title, $bannername ) {
+               if ( $title == 'NoBanner' ) {
+                       return null;
+               }
+               return "Banner";
+       }
+       public static function getWikidataBanner( $title ) {
+               if ( $title == 'NoBanner' ) {
+                       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 $testPages = array(
+                       array( 'PageWithoutCustomBanner', NS_MAIN, false, 
"Banner" ),
+                       array( 'PageWithCustomBanner', NS_MAIN, true, "Banner" 
),
+                       array( 'PageInTalkNamespace', NS_TALK, false, null ),
+                       array( 'NoBanner', NS_MAIN, false, null )
+               );
+       /**
+        * 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->testPages 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->method( 'getOutput' )->will( $this->returnValue( 
$parserOutput ) );
+               $curTitle = Title::newFromText( $title, $namespace );
+               $parser->method( 'getTitle' )->will( $this->returnValue( 
$curTitle ) );
+               return $parser;
+       }
+
+       /**
+        * Data Provider for testDefaultBanner
+        */
+       public function provideTestDefaultBanner() {
+               return $this->testPages;
+       }
+
+       /**
+        * Data Provider for testDefaultBanner
+        */
+       public function provideTestCustomBanner() {
+               return $this->testPagesForCustomBanner;
+       }
+}
diff --git a/tests/phpunit/GetBannerUrlTest.php 
b/tests/phpunit/GetBannerUrlTest.php
new file mode 100644
index 0000000..fb9d2ca
--- /dev/null
+++ b/tests/phpunit/GetBannerUrlTest.php
@@ -0,0 +1,42 @@
+<?php
+/**
+ * @group WikidataPageBanner
+ */
+class GetBannerUrlTest extends MediaWikiTestCase {
+       protected function setUp() {
+               parent::setUp();
+               $this->setMWGlobals( array(
+                               'wgUseInstantCommons' => 'true'
+               ) );
+       }
+
+       protected function tearDown() {
+               parent::tearDown();
+       }
+
+       /**
+        * testGetBannerUrl Tests getBannerUrl by using images from wikimedia 
commons
+        * Note: The mentioned images need to be present on commons for tests 
to succeed, if not,
+        * they should be replaced
+        * @covers getBannerUrl(...)
+        */
+       public function testGetBannerUrl() {
+               $file = 'Manhattan4_amk.jpg';
+               $url = WikidataPageBanner::getBannerUrl( $file );
+               $this->assertStringStartsWith( 'http', $url, 'On providing 
valid image, its url should be returned' );
+               $url = WikidataPageBanner::getBannerUrl( $file, 800, 'On 
providing valid image, its url should be returned' );
+               $this->assertStringStartsWith( 'http', $url );
+               $url = WikidataPageBanner::getBannerUrl( $file, 1600, 'On 
providing valid image, its url should be returned' );
+               $this->assertStringStartsWith( 'http', $url );
+
+               $file = 'Sainte-Enimie-Gorges_du_Tarn_France_banner.jpg';
+               $url = WikidataPageBanner::getBannerUrl( $file );
+               $this->assertStringStartsWith( 'http', $url, 'On providing 
valid image, its url should be returned' );
+               $url = WikidataPageBanner::getBannerUrl( $file, 4300 );
+               $this->assertNull( $url, 'On providing valid image with width 
greater than original width, null should be returned' );
+
+               $file = '';
+               $url = WikidataPageBanner::getBannerUrl( $file, 1600 );
+               $this->assertNull( $url, 'invalid image should return null' );
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/216735
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia122bdef53f646d3e35733b80c2c6c688c161dbc
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikidataPageBanner
Gerrit-Branch: master
Gerrit-Owner: Sumit <asthana.sumi...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to