jenkins-bot has submitted this change and it was merged.
Change subject: Mock services for SpecialWikibaseRepoPage
......................................................................
Mock services for SpecialWikibaseRepoPage
This provides and injects mock services for testing subclasses of the
SpecialWikibaseRepoPage base class. It also converts the test for
SpecialSetLabelDescriptionAliases to use the new facility.
Bug: T92375
Change-Id: I532ac74f4f007704d45cfa9db81c2f87fdd4421c
---
M repo/includes/WikibaseRepo.php
M repo/includes/specials/SpecialSetLabelDescriptionAliases.php
M repo/includes/specials/SpecialWikibaseRepoPage.php
M repo/tests/phpunit/includes/specials/SpecialSetLabelDescriptionAliasesTest.php
A repo/tests/phpunit/includes/specials/SpecialWikibaseRepoPageTestBase.php
5 files changed, 396 insertions(+), 11 deletions(-)
Approvals:
Thiemo Mättig (WMDE): Looks good to me, approved
jenkins-bot: Verified
Objections:
Aude: There's a problem with this change, please improve
diff --git a/repo/includes/WikibaseRepo.php b/repo/includes/WikibaseRepo.php
index b83a7f8..9f4bacd 100644
--- a/repo/includes/WikibaseRepo.php
+++ b/repo/includes/WikibaseRepo.php
@@ -32,6 +32,7 @@
use Wikibase\Lib\Changes\EntityChangeFactory;
use Wikibase\Lib\ClaimGuidGenerator;
use Wikibase\Lib\ClaimGuidValidator;
+use Wikibase\Lib\ContentLanguages;
use Wikibase\Lib\DispatchingValueFormatter;
use Wikibase\Lib\EntityIdHtmlLinkFormatterFactory;
use Wikibase\Lib\EntityIdLinkFormatter;
diff --git a/repo/includes/specials/SpecialSetLabelDescriptionAliases.php
b/repo/includes/specials/SpecialSetLabelDescriptionAliases.php
index 8c5a058..621d0d4 100644
--- a/repo/includes/specials/SpecialSetLabelDescriptionAliases.php
+++ b/repo/includes/specials/SpecialSetLabelDescriptionAliases.php
@@ -4,13 +4,19 @@
use Html;
use Language;
+use SiteStore;
use Wikibase\ChangeOp\ChangeOpException;
use Wikibase\ChangeOp\FingerprintChangeOpFactory;
use Wikibase\DataModel\Entity\Entity;
use Wikibase\DataModel\Term\FingerprintProvider;
use Wikibase\Lib\ContentLanguages;
+use Wikibase\Lib\Store\EntityRevisionLookup;
+use Wikibase\Lib\Store\EntityStore;
+use Wikibase\Lib\Store\EntityTitleLookup;
+use Wikibase\Repo\Store\EntityPermissionChecker;
use Wikibase\Repo\WikibaseRepo;
use Wikibase\Summary;
+use Wikibase\SummaryFormatter;
/**
* Special page for setting label, description and aliases of a Wikibase
Entity that features a
@@ -56,9 +62,58 @@
parent::__construct( 'SetLabelDescriptionAliases', 'edit' );
$wikibaseRepo = WikibaseRepo::getDefaultInstance();
- $this->changeOpFactory =
$wikibaseRepo->getChangeOpFactoryProvider()
- ->getFingerprintChangeOpFactory();
- $this->termsLanguages = $wikibaseRepo->getTermsLanguages();
+
+ $this->setAdditionalServices(
+ $wikibaseRepo->getChangeOpFactoryProvider()
+ ->getFingerprintChangeOpFactory(),
+ $wikibaseRepo->getTermsLanguages()
+ );
+ }
+
+ /**
+ * Override services (for testing).
+ *
+ * @param SummaryFormatter $summaryFormatter
+ * @param EntityRevisionLookup $entityRevisionLookup
+ * @param EntityTitleLookup $entityTitleLookup
+ * @param EntityStore $entityStore
+ * @param EntityPermissionChecker $permissionChecker
+ * @param SiteStore $siteStore
+ * @param FingerprintChangeOpFactory $changeOpFactory
+ * @param ContentLanguages $termsLanguages
+ */
+ public function setServices(
+ SummaryFormatter $summaryFormatter,
+ EntityRevisionLookup $entityRevisionLookup,
+ EntityTitleLookup $entityTitleLookup,
+ EntityStore $entityStore,
+ EntityPermissionChecker $permissionChecker,
+ SiteStore $siteStore,
+ FingerprintChangeOpFactory $changeOpFactory,
+ ContentLanguages $termsLanguages
+ ) {
+ self::setSpecialWikibaseRepoPageServices(
+ $summaryFormatter,
+ $entityRevisionLookup,
+ $entityTitleLookup,
+ $entityStore,
+ $permissionChecker,
+ $siteStore
+ );
+
+ $this->setAdditionalServices( $changeOpFactory, $termsLanguages
);
+ }
+
+ /**
+ * @param FingerprintChangeOpFactory $changeOpFactory
+ * @param ContentLanguages $termsLanguages
+ */
+ private function setAdditionalServices(
+ FingerprintChangeOpFactory $changeOpFactory,
+ ContentLanguages $termsLanguages
+ ) {
+ $this->changeOpFactory = $changeOpFactory;
+ $this->termsLanguages = $termsLanguages;
}
/**
diff --git a/repo/includes/specials/SpecialWikibaseRepoPage.php
b/repo/includes/specials/SpecialWikibaseRepoPage.php
index 704ab4a..2a103e6 100644
--- a/repo/includes/specials/SpecialWikibaseRepoPage.php
+++ b/repo/includes/specials/SpecialWikibaseRepoPage.php
@@ -73,7 +73,7 @@
parent::__construct( $title, $restriction );
$wikibaseRepo = WikibaseRepo::getDefaultInstance();
- $this->setServices(
+ $this->setSpecialWikibaseRepoPageServices(
$wikibaseRepo->getSummaryFormatter(),
$wikibaseRepo->getEntityRevisionLookup( 'uncached' ),
$wikibaseRepo->getEntityTitleLookup(),
@@ -83,7 +83,17 @@
);
}
- public function setServices(
+ /**
+ * Override services (for testing).
+ *
+ * @param SummaryFormatter $summaryFormatter
+ * @param EntityRevisionLookup $entityRevisionLookup
+ * @param EntityTitleLookup $entityTitleLookup
+ * @param EntityStore $entityStore
+ * @param EntityPermissionChecker $permissionChecker
+ * @param SiteStore $siteStore
+ */
+ public function setSpecialWikibaseRepoPageServices(
SummaryFormatter $summaryFormatter,
EntityRevisionLookup $entityRevisionLookup,
EntityTitleLookup $entityTitleLookup,
diff --git
a/repo/tests/phpunit/includes/specials/SpecialSetLabelDescriptionAliasesTest.php
b/repo/tests/phpunit/includes/specials/SpecialSetLabelDescriptionAliasesTest.php
index 1c43e23..b2ae5c8 100644
---
a/repo/tests/phpunit/includes/specials/SpecialSetLabelDescriptionAliasesTest.php
+++
b/repo/tests/phpunit/includes/specials/SpecialSetLabelDescriptionAliasesTest.php
@@ -2,10 +2,17 @@
namespace Wikibase\Test;
+use ValueValidators\Result;
+use Wikibase\ChangeOp\FingerprintChangeOpFactory;
+use Wikibase\DataModel\Entity\EntityId;
use Wikibase\DataModel\Entity\Item;
-use Wikibase\EntityContent;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\LabelDescriptionDuplicateDetector;
+use Wikibase\Lib\ContentLanguages;
use Wikibase\Repo\Specials\SpecialSetLabelDescriptionAliases;
-use Wikibase\Repo\WikibaseRepo;
+use Wikibase\Repo\Specials\SpecialWikibaseRepoPage;
+use Wikibase\Validators\TermValidatorFactory;
+use Wikibase\Validators\UniquenessViolation;
/**
* @covers Wikibase\Repo\Specials\SpecialSetLabelDescriptionAliases
@@ -21,8 +28,12 @@
* @licence GNU GPL v2+
* @author Bene* < [email protected] >
* @author H. Snater < [email protected] >
+ * @author Daniel Kinzler
*/
-class SpecialSetLabelDescriptionAliasesTest extends SpecialPageTestBase {
+class SpecialSetLabelDescriptionAliasesTest extends
SpecialWikibaseRepoPageTestBase {
+
+ protected $languageCodes = array( 'en', 'de', 'de-ch', 'ii', 'zh' );
+
/**
* @see SpecialPageTestBase::newSpecialPage()
@@ -30,7 +41,123 @@
* @return SpecialSetLabelDescriptionAliases
*/
protected function newSpecialPage() {
- return new SpecialSetLabelDescriptionAliases();
+ $page = new SpecialSetLabelDescriptionAliases();
+
+ $this->setMockServices( $page );
+
+ return $page;
+ }
+
+ /**
+ * @param SpecialSetLabelDescriptionAliases $page
+ */
+ protected function setMockServices( SpecialWikibaseRepoPage $page ) {
+ $page->setServices(
+ $this->getSummaryFormatter(),
+ $this->getEntityRevisionLookup(),
+ $this->getEntityTitleLookup(),
+ $this->getEntityStore(),
+ $this->getEntityPermissionChecker(),
+ $this->getSiteStore(),
+ $this->getFingerprintChangeOpsFactory(),
+ $this->getContentLanguages()
+ );
+ }
+
+ /**
+ * @return FingerprintChangeOpFactory
+ */
+ private function getFingerprintChangeOpsFactory() {
+ $maxLength = 32;
+
+ return new FingerprintChangeOpFactory(
+ new TermValidatorFactory(
+ $maxLength,
+ $this->languageCodes,
+ $this->getIdParser(),
+ $this->getLabelDescriptionDuplicateDetector(),
+ $this->mockRepository
+ )
+ );
+ }
+
+ /**
+ * @return LabelDescriptionDuplicateDetector
+ */
+ private function getLabelDescriptionDuplicateDetector() {
+ $detector = $this->getMockBuilder(
'Wikibase\LabelDescriptionDuplicateDetector' )
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $self = $this; // yay PHP 5.3
+ $detector->expects( $this->any() )
+ ->method( 'detectTermConflicts' )
+ ->will( $this->returnCallback( function(
+ $entityType,
+ array $labels,
+ array $descriptions = null,
+ EntityId $ignoreEntityId = null
+ ) use ( $self ) {
+ $errors = array();
+
+ $errors = array_merge( $errors,
$self->detectDupes( $labels ) );
+ $errors = array_merge( $errors,
$self->detectDupes( $descriptions ) );
+
+ $result = empty( $errors ) ?
Result::newSuccess() : Result::newError( $errors );
+ return $result;
+ } ) );
+
+ return $detector;
+ }
+
+ /**
+ * Mock duplicate detection: the term "DUPE" is considered a duplicate.
+ *
+ * @param string[] $terms
+ *
+ * @return array
+ */
+ public function detectDupes( $terms ) {
+ $errors = array();
+
+ foreach ( $terms as $languageCode => $term ) {
+ if ( $term === 'DUPE' ) {
+ $q666 = new ItemId( 'Q666' );
+
+ $errors[] = new UniquenessViolation(
+ $q666,
+ 'found conflicting terms',
+ 'test-conflict',
+ array(
+ $term,
+ $languageCode,
+ $q666,
+ )
+ );
+ }
+ }
+
+ return $errors;
+ }
+
+ /**
+ * @return ContentLanguages
+ */
+ private function getContentLanguages() {
+ $languages = $this->getMock( 'Wikibase\Lib\ContentLanguages' );
+
+ $languages->expects( $this->any() )
+ ->method( 'getLanguages' )
+ ->will( $this->returnValue( $this->languageCodes ) );
+
+ $languageCodes = $this->languageCodes; // for PHP 5.3
+ $languages->expects( $this->any() )
+ ->method( 'hasLanguage' )
+ ->will( $this->returnCallback( function( $code ) use (
$languageCodes ) {
+ return in_array( $code, $languageCodes );
+ } ) );
+
+ return $languages;
}
/**
@@ -44,8 +171,7 @@
$item->setAliases( 'de', array( 'foo' ) );
// save the item
- $store = WikibaseRepo::getDefaultInstance()->getEntityStore();
- $store->saveEntity( $item, "testing", $GLOBALS['wgUser'],
EDIT_NEW | EntityContent::EDIT_IGNORE_CONSTRAINTS );
+ $this->mockRepository->putEntity( $item );
// return the id
return $item->getId()->getSerialization();
diff --git
a/repo/tests/phpunit/includes/specials/SpecialWikibaseRepoPageTestBase.php
b/repo/tests/phpunit/includes/specials/SpecialWikibaseRepoPageTestBase.php
new file mode 100644
index 0000000..ee7dd8a
--- /dev/null
+++ b/repo/tests/phpunit/includes/specials/SpecialWikibaseRepoPageTestBase.php
@@ -0,0 +1,193 @@
+<?php
+
+namespace Wikibase\Test;
+
+use DataValues\DataValue;
+use Language;
+use SiteStore;
+use Status;
+use TestSites;
+use Title;
+use ValueFormatters\ValueFormatter;
+use Wikibase\DataModel\Entity\BasicEntityIdParser;
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\EntityIdParser;
+use Wikibase\DataModel\Snak\PropertyValueSnak;
+use Wikibase\DataModel\Snak\Snak;
+use Wikibase\Lib\EntityIdFormatter;
+use Wikibase\Lib\PlainEntityIdFormatter;
+use Wikibase\Lib\SnakFormatter;
+use Wikibase\Lib\Store\EntityRevisionLookup;
+use Wikibase\Lib\Store\EntityStore;
+use Wikibase\Lib\Store\EntityTitleLookup;
+use Wikibase\Repo\Specials\SpecialWikibaseRepoPage;
+use Wikibase\Repo\Store\EntityPermissionChecker;
+use Wikibase\SummaryFormatter;
+
+/**
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Daniel Kinzler
+ */
+abstract class SpecialWikibaseRepoPageTestBase extends SpecialPageTestBase {
+
+ /**
+ * @var MockRepository
+ */
+ protected $mockRepository;
+
+ public function setUp() {
+ parent::setUp();
+
+ $this->mockRepository = new MockRepository();
+ }
+
+ /**
+ * Call $page->setSpecialWikibaseRepoPageServices with the default mock
services.
+ *
+ * @param SpecialWikibaseRepoPage $page
+ */
+ protected function setMockServices( SpecialWikibaseRepoPage $page ) {
+ $page->setSpecialWikibaseRepoPageServices(
+ $this->getSummaryFormatter(),
+ $this->getEntityRevisionLookup(),
+ $this->getEntityTitleLookup(),
+ $this->getEntityStore(),
+ $this->getEntityPermissionChecker(),
+ $this->getSiteStore()
+ );
+ }
+
+ protected function getSummaryFormatter() {
+ return new SummaryFormatter(
+ $this->getIdFormatter(),
+ $this->getValueFormatter(),
+ $this->getSnakFormatter(),
+ $this->getLanguage(),
+ $this->getIdParser()
+ );
+ }
+
+ /**
+ * @return EntityRevisionLookup
+ */
+ protected function getEntityRevisionLookup() {
+ return $this->mockRepository;
+ }
+
+ /**
+ * @return EntityTitleLookup
+ */
+ protected function getEntityTitleLookup() {
+ $titleLookup = $this->getMock(
'Wikibase\Lib\Store\EntityTitleLookup' );
+
+ $titleLookup->expects( $this->any() )
+ ->method( 'getTitleForId' )
+ ->will( $this->returnCallback( function( EntityId $id )
{
+ return Title::makeTitle( NS_MAIN,
$id->getEntityType() . ':' . $id->getSerialization() );
+ } ) );
+
+ return $titleLookup;
+ }
+
+ /**
+ * @return EntityStore
+ */
+ protected function getEntityStore() {
+ return $this->mockRepository;
+ }
+
+ /**
+ * @return EntityPermissionChecker
+ */
+ protected function getEntityPermissionChecker() {
+ $permissionChecker = $this->getMock(
'Wikibase\Repo\Store\EntityPermissionChecker' );
+
+ $ok = Status::newGood();
+
+ $permissionChecker->expects( $this->any() )
+ ->method( 'getPermissionStatusForEntity' )
+ ->will( $this->returnValue( $ok ) );
+
+ $permissionChecker->expects( $this->any() )
+ ->method( 'getPermissionStatusForEntityId' )
+ ->will( $this->returnValue( $ok ) );
+
+ $permissionChecker->expects( $this->any() )
+ ->method( 'getPermissionStatusForEntityType' )
+ ->will( $this->returnValue( $ok ) );
+
+ return $permissionChecker;
+ }
+
+ /**
+ * @return SiteStore
+ */
+ protected function getSiteStore() {
+ return new MockSiteStore( TestSites::getSites() );
+ }
+
+ /**
+ * @return EntityIdFormatter
+ */
+ protected function getIdFormatter() {
+ return new PlainEntityIdFormatter();
+ }
+
+ /**
+ * @return ValueFormatter
+ */
+ protected function getValueFormatter() {
+ $formatter = $this->getMock( 'ValueFormatters\ValueFormatter' );
+
+ $formatter->expects( $this->any() )
+ ->method( 'format' )
+ ->will( $this->returnCallback( array( $this,
'formatValueAsText' ) ) );
+
+ return $formatter;
+ }
+
+ /**
+ * @return SnakFormatter
+ */
+ protected function getSnakFormatter() {
+ $formatter = $this->getMock( 'Wikibase\Lib\SnakFormatter' );
+
+ $formatter->expects( $this->any() )
+ ->method( 'formatSnak' )
+ ->will( $this->returnCallback( array( $this,
'formatSnakAsText' ) ) );
+
+ $formatter->expects( $this->any() )
+ ->method( 'getFormat' )
+ ->will( $this->returnValue( 'text/plain' ) );
+
+ return $formatter;
+ }
+
+ public function formatSnakAsText( Snak $snak ) {
+ if ( $snak instanceof PropertyValueSnak ) {
+ return $this->formatValueAsText( $snak->getDataValue()
);
+ } else {
+ return $snak->getType();
+ }
+ }
+
+ public function formatValueAsText( DataValue $value ) {
+ return print_r( $value->getValue(), true );
+ }
+
+ /**
+ * @return Language
+ */
+ protected function getLanguage() {
+ return Language::factory( 'qqx' );
+ }
+
+ /**
+ * @return EntityIdParser
+ */
+ protected function getIdParser() {
+ return new BasicEntityIdParser();
+ }
+}
--
To view, visit https://gerrit.wikimedia.org/r/194516
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I532ac74f4f007704d45cfa9db81c2f87fdd4421c
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Adrian Lang <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits