jenkins-bot has submitted this change and it was merged. Change subject: Integration tests for Client's Scribunto integration ......................................................................
Integration tests for Client's Scribunto integration This also removes the broken test mode handling from WikibaseClient as that made it impossible to properly inject a ClientStore mock. Change-Id: I026fa93047f9564c997a4005952306c853ff5334 --- M client/includes/WikibaseClient.php A client/tests/phpunit/MockClientStore.php M client/tests/phpunit/includes/WikibaseClientTest.php M client/tests/phpunit/includes/scribunto/LuaWikibaseEntityLibraryTests.lua A client/tests/phpunit/includes/scribunto/LuaWikibaseLibraryTests.lua M client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseEntityLibraryTest.php M client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTest.php A client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTestCase.php A client/tests/phpunit/includes/scribunto/WikibaseLuaIntegrationTestItemSetUpHelper.php M composer.json 10 files changed, 418 insertions(+), 50 deletions(-) Approvals: WikidataJenkins: Verified Daniel Kinzler: Looks good to me, approved jenkins-bot: Verified diff --git a/client/includes/WikibaseClient.php b/client/includes/WikibaseClient.php index f2f72a7..2b46b27 100644 --- a/client/includes/WikibaseClient.php +++ b/client/includes/WikibaseClient.php @@ -30,7 +30,6 @@ use Wikibase\Settings; use Wikibase\SettingsArray; use Wikibase\StringNormalizer; -use Wikibase\Test\MockRepository; /** * Top level factory for the WikibaseClient extension. @@ -73,11 +72,6 @@ protected $languageFallbackChainFactory = null; /** - * @var boolean - */ - protected $isInTestMode; - - /** * @var ClientStore[] */ private $storeInstances = array(); @@ -117,15 +111,13 @@ * * @param SettingsArray $settings * @param Language $contentLanguage - * @param $inTestMode * @param SiteStore $siteStore */ - public function __construct( SettingsArray $settings, Language $contentLanguage, $inTestMode = false, + public function __construct( SettingsArray $settings, Language $contentLanguage, SiteStore $siteStore = null ) { $this->contentLanguage = $contentLanguage; $this->settings = $settings; - $this->inTestMode = $inTestMode; $this->siteStore = $siteStore; } @@ -194,10 +186,6 @@ * @return EntityLookup */ private function getEntityLookup() { - if ( $this->inTestMode ) { - return new MockRepository(); - } - return $this->getStore()->getEntityLookup(); } @@ -348,10 +336,7 @@ protected static function newInstance() { global $wgContLang; - return new self( - Settings::singleton(), - $wgContLang, - defined( 'MW_PHPUNIT_TEST' ) ); + return new self( Settings::singleton(), $wgContLang ); } /** diff --git a/client/tests/phpunit/MockClientStore.php b/client/tests/phpunit/MockClientStore.php new file mode 100644 index 0000000..dc1b00c --- /dev/null +++ b/client/tests/phpunit/MockClientStore.php @@ -0,0 +1,59 @@ +<?php + +namespace Wikibase\Test; +use Wikibase\Test\MockRepository; +use Wikibase\Test\MockPropertyInfoStore; +use Wikibase\ClientStore; +use Wikibase\SiteLinkLookup; +use Wikibase\PropertyInfoStore; + +/** + * (Incomplete) ClientStore mock + * + * @since 0.5 + * + * @license GNU GPL v2+ + * @author Marius Hoch < h...@online.de > + */ +class MockClientStore implements ClientStore { + public function getItemUsageIndex() {} + public function getPropertyLabelResolver() {} + public function getTermIndex() {} + public function newChangesTable() {} + public function clear() {} + public function rebuild() {} + + private function getMock() { + static $mockRepo = false; + if ( !$mockRepo ) { + $mockRepo = new MockRepository(); + } + + return $mockRepo; + } + + /* + * @return EntityLookup + */ + public function getEntityLookup() { + return $this->getMock(); + } + + /** + * @return SiteLinkLookup + */ + public function getSiteLinkTable() { + return $this->getMock(); + } + + /** + * @return PropertyInfoStore + */ + public function getPropertyInfoStore() { + static $mockPropertyInfoStore = false; + if ( !$mockPropertyInfoStore ) { + $mockPropertyInfoStore = new MockPropertyInfoStore(); + } + return $mockPropertyInfoStore; + } +} \ No newline at end of file diff --git a/client/tests/phpunit/includes/WikibaseClientTest.php b/client/tests/phpunit/includes/WikibaseClientTest.php index f22f54f..b73dc4d 100644 --- a/client/tests/phpunit/includes/WikibaseClientTest.php +++ b/client/tests/phpunit/includes/WikibaseClientTest.php @@ -99,7 +99,7 @@ * @dataProvider getLangLinkSiteGroupProvider */ public function testGetLangLinkSiteGroup( $expected, $settings, $siteStore ) { - $client = new WikibaseClient( $settings, Language::factory( 'en' ), true, $siteStore ); + $client = new WikibaseClient( $settings, Language::factory( 'en' ), $siteStore ); $this->assertEquals( $expected, $client->getLangLinkSiteGroup() ); } @@ -124,7 +124,7 @@ * @dataProvider getSiteGroupProvider */ public function testGetSiteGroup( $expected, SettingsArray $settings, SiteStore $siteStore ) { - $client = new WikibaseClient( $settings, Language::factory( 'en' ), true, $siteStore ); + $client = new WikibaseClient( $settings, Language::factory( 'en' ), $siteStore ); $this->assertEquals( $expected, $client->getSiteGroup() ); } diff --git a/client/tests/phpunit/includes/scribunto/LuaWikibaseEntityLibraryTests.lua b/client/tests/phpunit/includes/scribunto/LuaWikibaseEntityLibraryTests.lua index e7f9d52..a53ea9d 100644 --- a/client/tests/phpunit/includes/scribunto/LuaWikibaseEntityLibraryTests.lua +++ b/client/tests/phpunit/includes/scribunto/LuaWikibaseEntityLibraryTests.lua @@ -1,5 +1,5 @@ --[[ - Unit tests for the mw.wikibase.entity module + Unit and integration tests for the mw.wikibase.entity module @license GNU GPL v2+ @author Marius Hoch < h...@online.de > @@ -36,7 +36,7 @@ return mw.wikibase.entity.create( testItem ) end --- Tests +-- Unit Tests local function testExists() return type( mw.wikibase.entity ) @@ -62,8 +62,30 @@ return getNewTestItem():formatPropertyValues( propertyId ) end --- Tests +-- Integration tests + +local function integrationTestGetPropertiesCount() + return #( mw.wikibase.getEntityObject():getProperties() ) +end + +local function integrationTestGetLabel( langCode ) + return mw.wikibase.getEntityObject():getLabel( langCode ) +end + +local function integrationTestGetSitelink( globalSiteId ) + return mw.wikibase.getEntityObject():getSitelink( globalSiteId ) +end + +local function integrationTestFormatPropertyValues() + local entity = mw.wikibase.getEntityObject() + local propertyId = entity:getProperties()[1] + + return mw.wikibase.getEntityObject():formatPropertyValues( propertyId ) +end + local tests = { + -- Unit Tests + { name = 'mw.wikibase.entity exists', func = testExists, type='ToString', expect = { 'table' } }, @@ -112,7 +134,30 @@ { name = 'mw.wikibase.entity.formatPropertyValues', func = testFormatPropertyValues, args = { function() end }, expect = "bad argument #1 to 'formatPropertyValues' (string expected, got function)" - } + }, + + -- Integration tests + + { name = 'mw.wikibase.entity.getLabel integration 1', func = integrationTestGetLabel, type='ToString', + expect = { 'Lua Test Item' } + }, + { name = 'mw.wikibase.entity.getLabel integration 2', func = integrationTestGetLabel, type='ToString', + args = { 'en' }, + expect = { 'Test all the code paths' } + }, + { name = 'mw.wikibase.entity.getSitelink integration 1', func = integrationTestGetSitelink, type='ToString', + expect = { 'WikibaseClientLuaTest' } + }, + { name = 'mw.wikibase.entity.getSitelink integration 2', func = integrationTestGetSitelink, type='ToString', + args = { 'fooSiteId' }, + expect = { 'FooBarFoo' } + }, + { name = 'mw.wikibase.entity.getProperties integration', func = integrationTestGetPropertiesCount, + expect = { 1 } + }, + { name = 'mw.wikibase.entity.formatPropertyValues integration', func = integrationTestFormatPropertyValues, + expect = { { label = 'LuaTestProperty', value = 'Lua :)' } } + }, } return testframework.getTestProvider( tests ) diff --git a/client/tests/phpunit/includes/scribunto/LuaWikibaseLibraryTests.lua b/client/tests/phpunit/includes/scribunto/LuaWikibaseLibraryTests.lua new file mode 100644 index 0000000..3944b9b --- /dev/null +++ b/client/tests/phpunit/includes/scribunto/LuaWikibaseLibraryTests.lua @@ -0,0 +1,47 @@ +--[[ + Integration tests for the mw.wikibase module + + @license GNU GPL v2+ + @author Marius Hoch < h...@online.de > +]] + +local testframework = require 'Module:TestFramework' + +-- Integration tests + +local function testGetEntityType() + return type( mw.wikibase.getEntity() ) +end + +local function testGetEntityObjectType() + return type( mw.wikibase.getEntityObject() ) +end + +local function testLabel() + local entity = mw.wikibase.getEntityObject() + return mw.wikibase.label( entity.id ) +end + +local function testSitelink() + local entity = mw.wikibase.getEntityObject() + return mw.wikibase.sitelink( entity.id ) +end + +local tests = { + -- Integration tests + + { name = 'mw.wikibase.getEntity (type)', func = testGetEntityType, type='ToString', + expect = { 'table' } + }, + { name = 'mw.wikibase.getEntityObject (type)', func = testGetEntityObjectType, type='ToString', + expect = { 'table' } + }, + { name = 'mw.wikibase.label', func = testLabel, type='ToString', + expect = { 'Lua Test Item' } + }, + { name = 'mw.wikibase.sitelink', func = testSitelink, type='ToString', + expect = { 'WikibaseClientLuaTest' } + } +} + +return testframework.getTestProvider( tests ) diff --git a/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseEntityLibraryTest.php b/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseEntityLibraryTest.php index 0220fd2..f23c3e6 100644 --- a/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseEntityLibraryTest.php +++ b/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseEntityLibraryTest.php @@ -2,11 +2,11 @@ namespace Wikibase\Test; +use Wikibase\Client\Scribunto\Test\Scribunto_LuaWikibaseLibraryTestCase; use Title; use Scribunto_LuaWikibaseEntityLibrary; use Scribunto; use Wikibase\Settings; -use Language; /** * @covers Scribunto_LuaWikibaseEntityLibrary @@ -21,27 +21,13 @@ * @licence GNU GPL v2+ * @author Marius Hoch < h...@online.de > */ -class Scribunto_LuaWikibaseEntityLibraryTest extends \Scribunto_LuaEngineTestBase { +class Scribunto_LuaWikibaseEntityLibraryTest extends Scribunto_LuaWikibaseLibraryTestCase { protected static $moduleName = 'LuaWikibaseEntityLibraryTests'; function getTestModules() { return parent::getTestModules() + array( 'LuaWikibaseEntityLibraryTests' => __DIR__ . '/LuaWikibaseEntityLibraryTests.lua', ); - } - - protected function setUp() { - parent::setUp(); - - if ( !defined( 'WB_VERSION' ) ) { - $this->markTestSkipped( "Skipping because WikibaseClient doesn't have a local site link table." ); - } - - if ( !class_exists( 'Scribunto_LuaStandaloneEngine' ) ) { - $this->markTestSkipped( 'test requires Scribunto' ); - } - - $this->setMwGlobals( 'wgContLang', Language::factory( 'de' ) ); } public function testConstructor() { diff --git a/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTest.php b/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTest.php index aef0c79..25521be 100644 --- a/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTest.php +++ b/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTest.php @@ -2,6 +2,7 @@ namespace Wikibase\Test; +use Wikibase\Client\Scribunto\Test\Scribunto_LuaWikibaseLibraryTestCase; use Title; use Scribunto_LuaWikibaseLibrary; use Scribunto; @@ -19,19 +20,15 @@ * * @licence GNU GPL v2+ * @author Katie Filbert < aude.w...@gmail.com > + * @author Marius Hoch < h...@online.de > */ -class Scribunto_LuaWikibaseLibraryTest extends \MediaWikiTestCase { +class Scribunto_LuaWikibaseLibraryTest extends Scribunto_LuaWikibaseLibraryTestCase { + protected static $moduleName = 'LuaWikibaseLibraryTests'; - protected function setUp() { - parent::setUp(); - - if ( !defined( 'WB_VERSION' ) ) { - $this->markTestSkipped( "Skipping because WikibaseClient doesn't have a local site link table." ); - } - - if ( !class_exists( 'Scribunto_LuaStandaloneEngine' ) ) { - $this->markTestSkipped( 'test requires Scribunto' ); - } + function getTestModules() { + return parent::getTestModules() + array( + 'LuaWikibaseLibraryTests' => __DIR__ . '/LuaWikibaseLibraryTests.lua', + ); } public function testConstructor() { diff --git a/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTestCase.php b/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTestCase.php new file mode 100644 index 0000000..488e2f7 --- /dev/null +++ b/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTestCase.php @@ -0,0 +1,107 @@ +<?php + +namespace Wikibase\Client\Scribunto\Test; + +if ( !class_exists( 'Scribunto_LuaEngineTestBase' ) ) { + // This needs Scribunto + class Scribunto_LuaWikibaseLibraryTestCase{} + return; +} + +use Title; +use Language; +use Wikibase\Settings; +use Wikibase\Client\WikibaseClient; + +/** + * Base class for Wikibase Scribunto Tests + * + * @since 0.5 + * + * @group WikibaseScribunto + * @group WikibaseIntegration + * @group WikibaseClient + * @group Wikibase + * + * @licence GNU GPL v2+ + * @author Marius Hoch < h...@online.de > + */ +class Scribunto_LuaWikibaseLibraryTestCase extends \Scribunto_LuaEngineTestBase { + + /* @var mixed */ + private static $oldDefaultClientStore = null; + + /* @var array */ + private static $oldWgWBClientStores = null; + + /** + * Makes sure WikibaseClient uses our ClientStore mock + */ + private static function doMock() { + global $wgWBClientStores; + + $wikibaseClient = WikibaseClient::getDefaultInstance(); + + self::$oldDefaultClientStore = $wikibaseClient->getSettings()->get( 'defaultClientStore' ); + $wikibaseClient->getSettings()->setSetting( 'defaultClientStore', 'ClientStoreMock' ); + + self::$oldWgWBClientStores = $wgWBClientStores; + $wgWBClientStores = array( + 'ClientStoreMock' => '\Wikibase\Test\MockClientStore' + ); + + // Reset the store instance to make sure our Mock is really being used + $wikibaseClient->getStore( false, 'reset' ); + } + + /** + * Set up stuff we need to have in place even before Scribunto does its stuff + * + * @param string $className + */ + public static function suite( $className ) { + self::doMock(); + + static $setUp = false; + if ( !$setUp ) { + $testHelper = new WikibaseLuaIntegrationTestItemSetUpHelper(); + $testHelper->setUp(); + $setUp = true; + } + + return parent::suite( $className ); + } + + protected function setUp() { + self::doMock(); + + $wikibaseClient = WikibaseClient::getDefaultInstance(); + $this->assertInstanceOf( + 'Wikibase\Test\MockRepository', + $wikibaseClient->getStore()->getEntityLookup(), + 'Mocking the default client EntityLookup failed' + ); + + $this->setMwGlobals( 'wgContLang', Language::factory( 'de' ) ); + parent::setUp(); + } + + public function tearDown() { + global $wgWBClientStores; + parent::tearDown(); + + $wikibaseClient = WikibaseClient::getDefaultInstance(); + + $wikibaseClient->getSettings()->setSetting( 'defaultClientStore', self::$oldDefaultClientStore ); + $wgWBClientStores = self::$oldWgWBClientStores; + // Reset the store instance, to make sure our Mock wont be used in other tests + $wikibaseClient->getStore( false, 'reset' ); + } + + /** + * @return Title + */ + protected function getTestTitle() { + return Title::newFromText( 'WikibaseClientLuaTest' ); + } +} \ No newline at end of file diff --git a/client/tests/phpunit/includes/scribunto/WikibaseLuaIntegrationTestItemSetUpHelper.php b/client/tests/phpunit/includes/scribunto/WikibaseLuaIntegrationTestItemSetUpHelper.php new file mode 100644 index 0000000..2e225a9 --- /dev/null +++ b/client/tests/phpunit/includes/scribunto/WikibaseLuaIntegrationTestItemSetUpHelper.php @@ -0,0 +1,139 @@ +<?php + +namespace Wikibase\Client\Scribunto\Test; + +use TestSites; +use Wikibase\DataModel\Entity\Item; +use Wikibase\DataModel\Claim\Claim; +use Wikibase\DataModel\Snak\Snak; +use Wikibase\DataModel\Entity\PropertyId; +use Wikibase\SnakFactory; +use DataValues\DataValue; +use Wikibase\DataModel\Entity\Property; +use DataValues\StringValue; +use Wikibase\DataModel\SiteLink; +use Wikibase\Lib\V4GuidGenerator; +use Wikibase\Test\MockClientStore; +use Wikibase\Client\WikibaseClient; + +/** + * Helper class for Lua integration tests. + * + * @since 0.5 + * + * @license GNU GPL v2+ + * @author Marius Hoch < h...@online.de > + */ +class WikibaseLuaIntegrationTestItemSetUpHelper { + + /* @var MockRepository */ + protected $mockRepository; + + public function __construct() { + $clientStore = new MockClientStore(); + $this->mockRepository = $clientStore->getEntityLookup(); + } + + /** + * Sets up the test data. + */ + public function setUp() { + $siteLink = new SiteLink( + WikibaseClient::getDefaultInstance()->getSettings()->get( 'siteGlobalID' ), + 'WikibaseClientLuaTest' + ); + + if ( $this->mockRepository->getEntityIdForSiteLink( $siteLink ) ) { + // Already set up for this MockRepository + return; + } + + TestSites::insertIntoDb(); + $property = $this->createTestProperty(); + + $snak = $this->getTestSnak( + $property->getId(), + new StringValue( 'Lua :)' ) + ); + + $testClaim = $this->getTestClaim( $snak ); + + $siteLinks = array( $siteLink ); + $siteLinks[] = new SiteLink( + 'fooSiteId', + 'FooBarFoo' + ); + + $labels = array( + 'de' => 'Lua Test Item', + 'en' => 'Test all the code paths' + ); + + $this->createTestItem( $labels, array( $testClaim ), $siteLinks ); + } + + /** + * @return Property + */ + protected function createTestProperty() { + $property = Property::newEmpty(); + $property->setDataTypeId( 'wikibase-item' ); + $property->setLabel( 'de', 'LuaTestProperty' ); + + $this->mockRepository->putEntity( $property ); + + return $property; + } + + /** + * @param array $label + * @param Claim[]|null $claims + * @param array $siteLinks + * + * @return Item + */ + protected function createTestItem( array $labels, array $claims = null, array $siteLinks = null ) { + $item = Item::newEmpty(); + $item->setLabels( $labels ); + + if ( is_array( $siteLinks ) ) { + foreach( $siteLinks as $siteLink ) { + $item->addSiteLink( $siteLink ); + } + } + + if ( is_array( $claims ) ) { + foreach( $claims as $claim ) { + $item->addClaim( $claim ); + } + } + + $this->mockRepository->putEntity( $item ); + + return $item; + } + + /** + * @param PropertyId $propertyId + * @param DataValue $value + * @return Snak + */ + protected function getTestSnak( PropertyId $propertyId, DataValue $value ) { + $snakFactory = new SnakFactory(); + $snak = $snakFactory->newSnak( $propertyId, 'value', $value ); + + return $snak; + } + + /** + * @param Snak $mainSnak + * @return Claim + */ + protected function getTestClaim( Snak $mainSnak ) { + $claim = new Claim( $mainSnak ); + $guidGen = new V4GuidGenerator(); + $claim->setGuid( $guidGen->newGuid() ); + + return $claim; + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index ee573fa..eef6eeb 100644 --- a/composer.json +++ b/composer.json @@ -46,6 +46,9 @@ "client/includes/", "client/WikibaseClient.hooks.php", "client/tests/phpunit/MockPageUpdater.php", + "client/tests/phpunit/MockClientStore.php", + "client/tests/phpunit/includes/scribunto/WikibaseLuaIntegrationTestItemSetUpHelper.php", + "client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTestCase.php", "lib/includes/", "lib/WikibaseLib.hooks.php", -- To view, visit https://gerrit.wikimedia.org/r/108298 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I026fa93047f9564c997a4005952306c853ff5334 Gerrit-PatchSet: 14 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Hoo man <h...@online.de> Gerrit-Reviewer: Aude <aude.w...@gmail.com> Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: Hoo man <h...@online.de> Gerrit-Reviewer: Jens Ohlig <jens.oh...@wikimedia.de> Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com> Gerrit-Reviewer: WikidataJenkins <wikidata-servi...@wikimedia.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits