Aude has uploaded a new change for review.

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

Change subject: Refactor LinkBeginHookHandlerTest
......................................................................

Refactor LinkBeginHookHandlerTest

to use mocks, etc. instead of creating items and
using global state.

Change-Id: I04057d2a63867ff9cecae355fd48049707225a54
---
M repo/tests/phpunit/includes/Hook/LinkBeginHookHandlerTest.php
1 file changed, 93 insertions(+), 90 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/05/176805/1

diff --git a/repo/tests/phpunit/includes/Hook/LinkBeginHookHandlerTest.php 
b/repo/tests/phpunit/includes/Hook/LinkBeginHookHandlerTest.php
index 2df87b1..1e16940 100644
--- a/repo/tests/phpunit/includes/Hook/LinkBeginHookHandlerTest.php
+++ b/repo/tests/phpunit/includes/Hook/LinkBeginHookHandlerTest.php
@@ -3,12 +3,17 @@
 namespace Wikibase\Test;
 
 use Language;
+use OutputPage;
 use RequestContext;
 use Title;
+use Wikibase\DataModel\Entity\EntityId;
 use Wikibase\DataModel\Entity\Item;
 use Wikibase\DataModel\Entity\ItemId;
-use Wikibase\Lib\Store\EntityTitleLookup;
+use Wikibase\LanguageFallbackChain;
+use Wikibase\LanguageWithConversion;
+use Wikibase\Lib\Store\TermLookup;
 use Wikibase\Repo\Hook\LinkBeginHookHandler;
+use Wikibase\Repo\Store\PageEntityIdLookup;
 use Wikibase\Repo\WikibaseRepo;
 
 /**
@@ -18,92 +23,52 @@
  *
  * @group WikibaseRepo
  * @group Wikibase
- * @group Database
  *
  * @licence GNU GPL v2+
  * @author Katie Filbert < [email protected] >
+ * @author Daniel Kinzler
  */
 class LinkBeginHookHandlerTest extends \MediaWikiTestCase {
 
-       /**
-        * @var ItemId
-        */
-       private static $itemId = null;
-
-       /**
-        * @var ItemId
-        */
-       private static $noLabelItemId = null;
-
-       /**
-        * @var EntityContentFactory
-        */
-       private $entityContentFactory = null;
-
-       protected function setUp() {
-               parent::setUp();
-
-               $language = Language::factory( 'en' );
-
-               $this->setMwGlobals( array(
-                       'wgLanguageCode' => 'en',
-                       'wgLang' => $language,
-                       'wgContLang' => $language
-               ) );
-
-               if ( self::$itemId === null ) {
-                       $this->setupItems();
-               }
-       }
-
-       private function setupItems() {
-               $store = WikibaseRepo::getDefaultInstance()->getEntityStore();
-
-               $item = Item::newEmpty();
-               $item->setLabel( 'en', 'linkbegin-label' );
-               $item->setDescription( 'en', 'linkbegin-description' );
-
-               $user = $GLOBALS['wgUser'];
-
-               $entityRevision = $store->saveEntity( $item, 'testing', $user, 
EDIT_NEW );
-               self::$itemId = $entityRevision->getEntity()->getId();
-
-               $entityRevision = $store->saveEntity( Item::newEmpty(), 
'testing', $user, EDIT_NEW );
-               self::$noLabelItemId = $entityRevision->getEntity()->getId();
-       }
+       const ITEM_WITH_LABEL = 'Q1';
+       const ITEM_WITHOUT_LABEL = 'Q11';
+       const ITEM_DELETED = 'Q111';
 
        public function testDoOnLinkBegin() {
                $contextTitle = Title::newFromText( 'Special:Recentchanges' );
-               $linkBeginHookHandler = $this->getLinkBeginHookHandler( 
$contextTitle );
+               $linkBeginHookHandler = $this->getLinkBeginHookHandler();
 
-               $title = $this->getEntityContentFactory()->getTitleForId( 
self::$itemId );
+               $title = Title::makeTitle( NS_MAIN, self::ITEM_WITH_LABEL );
 
                $html = $title->getFullText();
                $customAttribs = array();
 
-               $linkBeginHookHandler->doOnLinkBegin( $title, $html, 
$customAttribs, $this->getOutput( $title ) );
+               $out = $this->getOutputPage( $contextTitle );
+               $linkBeginHookHandler->doOnLinkBegin( $title, $html, 
$customAttribs, $out );
 
                $expectedHtml = '<span class="wb-itemlink">'
                        . '<span class="wb-itemlink-label" lang="en" 
dir="ltr">linkbegin-label</span> '
-                       . '<span class="wb-itemlink-id">(' . 
self::$itemId->getSerialization()
-                       . ')</span></span>';
+                       . '<span class="wb-itemlink-id">(' . 
self::ITEM_WITH_LABEL . ')</span></span>';
 
                $this->assertEquals( $expectedHtml, $html );
 
                $this->assertContains( 'linkbegin-label', 
$customAttribs['title'] );
                $this->assertContains( 'linkbegin-description', 
$customAttribs['title'] );
+
+               $this->assertContains( 'wikibase.common', 
$out->getModuleStyles() );
        }
 
        public function testDoOnLinkBegin_onNonSpecialPage() {
-               $linkBeginHookHandler = $this->getLinkBeginHookHandler( 
Title::newMainPage() );
+               $linkBeginHookHandler = $this->getLinkBeginHookHandler();
 
-               $title = $this->getEntityContentFactory()->getTitleForId( 
self::$itemId );
+               $title = Title::makeTitle( NS_MAIN, self::ITEM_WITH_LABEL );
 
                $titleText = $title->getFullText();
                $html = $titleText;
                $customAttribs = array();
 
-               $linkBeginHookHandler->doOnLinkBegin( $title, $html, 
$customAttribs, $this->getOutput( $title ) );
+               $out = $this->getOutputPage( Title::newMainPage() );
+               $linkBeginHookHandler->doOnLinkBegin( $title, $html, 
$customAttribs, $out );
 
                $this->assertEquals( $titleText, $html );
                $this->assertEquals( array(), $customAttribs );
@@ -111,7 +76,7 @@
 
        public function testDoOnLinkBegin_nonEntityTitleLink() {
                $contextTitle = Title::newFromText( 'Special:Recentchanges' );
-               $linkBeginHookHandler = $this->getLinkBeginHookHandler( 
$contextTitle );
+               $linkBeginHookHandler = $this->getLinkBeginHookHandler();
 
                $title = Title::newMainPage();
 
@@ -119,7 +84,8 @@
                $html = $titleText;
                $customAttribs = array();
 
-               $linkBeginHookHandler->doOnLinkBegin( $title, $html, 
$customAttribs, $this->getOutput( $title ) );
+               $out = $this->getOutputPage( $contextTitle );
+               $linkBeginHookHandler->doOnLinkBegin( $title, $html, 
$customAttribs, $out );
 
                $this->assertEquals( $titleText, $html );
                $this->assertEquals( array(), $customAttribs );
@@ -127,16 +93,16 @@
 
        public function testDoOnLinkBegin_unknownEntityTitle() {
                $contextTitle = Title::newFromText( 'Special:Recentchanges' );
-               $linkBeginHookHandler = $this->getLinkBeginHookHandler( 
$contextTitle );
+               $linkBeginHookHandler = $this->getLinkBeginHookHandler();
 
-               $itemId = ItemId::newFromNumber( mt_rand( 0, 9999999999 ) );
-               $title = $this->getEntityContentFactory()->getTitleForId( 
$itemId );
+               $title = Title::makeTitle( NS_MAIN, self::ITEM_DELETED );
 
                $titleText = $title->getFullText();
                $html = $titleText;
                $customAttribs = array();
 
-               $linkBeginHookHandler->doOnLinkBegin( $title, $html, 
$customAttribs, $this->getOutput( $title ) );
+               $out = $this->getOutputPage( $contextTitle );
+               $linkBeginHookHandler->doOnLinkBegin( $title, $html, 
$customAttribs, $out );
 
                $this->assertEquals( $titleText, $html );
                $this->assertEquals( array(), $customAttribs );
@@ -144,55 +110,92 @@
 
        public function testDoOnLinkBegin_itemHasNoLabel() {
                $contextTitle = Title::newFromText( 'Special:Recentchanges' );
-               $linkBeginHookHandler = $this->getLinkBeginHookHandler( 
$contextTitle );
+               $linkBeginHookHandler = $this->getLinkBeginHookHandler();
 
-               $title = $this->getEntityContentFactory()->getTitleForId( 
self::$noLabelItemId );
+               $title = Title::makeTitle( NS_MAIN, self::ITEM_WITHOUT_LABEL );
 
                $html = $title->getFullText();
                $customAttribs = array();
 
-               $linkBeginHookHandler->doOnLinkBegin( $title, $html, 
$customAttribs, $this->getOutput( $title ) );
+               $out = $this->getOutputPage( $contextTitle );
+               $linkBeginHookHandler->doOnLinkBegin( $title, $html, 
$customAttribs, $out );
 
                $expected = '<span class="wb-itemlink">'
                        . '<span class="wb-itemlink-label" lang="en" 
dir="ltr"></span> '
-                       . '<span class="wb-itemlink-id">('
-                       . self::$noLabelItemId->getSerialization()
-                       . ')</span></span>';
+                       . '<span class="wb-itemlink-id">(' . 
self::ITEM_WITHOUT_LABEL . ')</span></span>';
 
                $this->assertEquals( $expected, $html );
-               $this->assertContains( 
self::$noLabelItemId->getSerialization(), $customAttribs['title'] );
+               $this->assertContains( self::ITEM_WITHOUT_LABEL, 
$customAttribs['title'] );
        }
 
-       private function getEntityContentFactory() {
-               if ( $this->entityContentFactory === null ) {
-                       $wikibaseRepo = WikibaseRepo::getDefaultInstance();
-                       $this->entityContentFactory = 
$wikibaseRepo->getEntityContentFactory();
-               }
-
-               return $this->entityContentFactory;
+       private function getOutputPage( Title $title ) {
+               $context = RequestContext::newExtraneousContext( $title );
+               return $context->getOutput();
        }
 
-       private function getOutput( Title $title ) {
-               return $this->getContext( $title )->getOutput();
+       /**
+        * @return PageEntityIdLookup
+        */
+       private function getPageEntityIdLookup() {
+               $entityIdLookup = $this->getMock( 
'Wikibase\Repo\Store\PageEntityIdLookup' );
+
+               $entityIdLookup->expects( $this->any() )
+                       ->method( 'getPageEntityId' )
+                       ->will( $this->returnCallback( function( Title $title ) 
{
+                               if ( preg_match( '/^Q(\d+)$/', 
$title->getText(), $m ) ) {
+                                       // Q1 .. Q99 are valid
+                                       if ( intval( $m[1] ) < 100 ) {
+                                               return new ItemId( $m[0] );
+                                       }
+                               }
+
+                               return null;
+                       } ) );
+
+               return $entityIdLookup;
        }
 
-       private function getContext( Title $title ) {
-               $context = RequestContext::getMain();
-               $context->setTitle( $title );
+       /**
+        * @return TermLookup
+        */
+       private function getTermLookup() {
+               $termLookup = $this->getMock( 'Wikibase\Lib\Store\TermLookup' );
 
-               return $context;
+               $termLookup->expects( $this->any() )
+                       ->method( 'getLabels' )
+                       ->will( $this->returnCallback( function ( EntityId $id 
) {
+                               if ( $id->getSerialization() == 'Q1' ) {
+                                       return array( 'en' => 'linkbegin-label' 
);
+                               }
+
+                               return array();
+                       } ) );
+
+               $termLookup->expects( $this->any() )
+                       ->method( 'getDescriptions' )
+                       ->will( $this->returnCallback( function ( EntityId $id 
) {
+                               if ( $id->getSerialization() == 'Q1' ) {
+                                       return array( 'en' => 
'linkbegin-description' );
+                               }
+
+                               return array();
+                       } ) );
+
+               return $termLookup;
        }
 
-       private function getLinkBeginHookHandler( Title $title ) {
-               $context = $this->getContext( $title );
-
-               $wikibaseRepo = WikibaseRepo::getDefaultInstance();
+       private function getLinkBeginHookHandler() {
+               $languageFallback = new LanguageFallbackChain( array(
+                       LanguageWithConversion::factory( 'de-ch' ),
+                       LanguageWithConversion::factory( 'de' ),
+                       LanguageWithConversion::factory( 'en' ),
+               ) );
 
                return new LinkBeginHookHandler(
-                       $wikibaseRepo->getEntityContentFactory(),
-                       $wikibaseRepo->getTermLookup(),
-                       
$wikibaseRepo->getLanguageFallbackChainFactory()->newFromContext( $context ),
-                       $context->getLanguage()
+                       $this->getPageEntityIdLookup(),
+                       $this->getTermLookup(),
+                       $languageFallback,
+                       Language::factory( 'en' )
                );
 
        }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I04057d2a63867ff9cecae355fd48049707225a54
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Aude <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to