Hoo man has uploaded a new change for review.

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


Change subject: Integration tests for Client's Scribunto integration
......................................................................

Integration tests for Client's Scribunto integration

Change-Id: I026fa93047f9564c997a4005952306c853ff5334
---
M client/WikibaseClient.classes.php
M client/includes/WikibaseClient.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/WikibaseLuaIntegrationTestHelper.php
8 files changed, 386 insertions(+), 37 deletions(-)


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

diff --git a/client/WikibaseClient.classes.php 
b/client/WikibaseClient.classes.php
index 0589530..4b6aed6 100644
--- a/client/WikibaseClient.classes.php
+++ b/client/WikibaseClient.classes.php
@@ -67,7 +67,9 @@
                'Wikibase\Client\Scribunto\WikibaseLuaEntityBindings' => 
'includes/scribunto/WikibaseLuaEntityBindings.php',
 
                // test
-               'Wikibase\Test\MockPageUpdater' => 
'tests/phpunit/MockPageUpdater.php'
+               'Wikibase\Test\MockPageUpdater' => 
'tests/phpunit/MockPageUpdater.php',
+               
'Wikibase\Client\Scribunto\Test\WikibaseLuaIntegrationTestHelper' => 
'tests/phpunit/includes/scribunto/WikibaseLuaIntegrationTestHelper.php',
+               
'Wikibase\Client\Scribunto\Test\Scribunto_LuaWikibaseLibraryTestCase' => 
'tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTestCase.php'
 
        );
 
diff --git a/client/includes/WikibaseClient.php 
b/client/includes/WikibaseClient.php
index f2f72a7..2a052d9 100644
--- a/client/includes/WikibaseClient.php
+++ b/client/includes/WikibaseClient.php
@@ -194,10 +194,6 @@
         * @return EntityLookup
         */
        private function getEntityLookup() {
-               if ( $this->inTestMode ) {
-                       return new MockRepository();
-               }
-
                return $this->getStore()->getEntityLookup();
        }
 
diff --git 
a/client/tests/phpunit/includes/scribunto/LuaWikibaseEntityLibraryTests.lua 
b/client/tests/phpunit/includes/scribunto/LuaWikibaseEntityLibraryTests.lua
index 0b1626b..104da89 100644
--- a/client/tests/phpunit/includes/scribunto/LuaWikibaseEntityLibraryTests.lua
+++ b/client/tests/phpunit/includes/scribunto/LuaWikibaseEntityLibraryTests.lua
@@ -1,12 +1,11 @@
 --[[
-       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 >
 ]]
 
 local testframework = require 'Module:TestFramework'
-local render = mw.wikibase.entity
 
 -- A test item (the structure isn't complete... but good enough for tests)
 local testItem = {
@@ -37,7 +36,7 @@
        return mw.wikibase.entity.create( testItem )
 end
 
--- Tests
+-- Unit Tests
 
 local function testExists()
        return type( mw.wikibase.entity )
@@ -63,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' }
        },
@@ -113,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,-- type='ToString',
+         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 e4796f4..88a0670 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_LuaWikibaseLibrary
@@ -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..ef3c196
--- /dev/null
+++ 
b/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTestCase.php
@@ -0,0 +1,97 @@
+<?php
+
+namespace Wikibase\Client\Scribunto\Test;
+
+if ( !class_exists( 'Scribunto_LuaEngineTestBase' ) ) {
+       // This needs Scribunto
+       class Scribunto_LuaWikibaseLibraryTestCase{}
+       return;
+}
+
+use PHPUnit_Framework_TestSuite;
+use Title;
+use Language;
+use Scribunto_LuaEngineTestSkip;
+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 {
+       /**
+        * We need to overwrite this as Scribunto registers stuff even before 
running setUp
+        */
+       public static function suite( $className ) {
+               if ( !defined( 'WB_VERSION' ) ) {
+                       $suite = new PHPUnit_Framework_TestSuite;
+                       $suite->setName( $className );
+                       $suite->addTest(
+                               new Scribunto_LuaEngineTestSkip(
+                                       $className,
+                                       "Skipping because WikibaseClient 
doesn't have a local site link table."
+                               )
+                       );
+                       return $suite;
+               }
+
+               return parent::suite( $className );
+       }
+
+       protected function setUp() {
+               parent::setUp();
+
+               $wikibaseClient = WikibaseClient::getDefaultInstance();
+
+               // Get the name of the store which will be used and then update 
the
+               // mapping to let it point to our mock.
+               $storeName = Settings::get( 'defaultClientStore' ) ?
+                       Settings::get( 'defaultClientStore' ) : 
'DirectSqlStore';
+
+               $this->setMwGlobals(
+                       'wgWBClientStores',
+                       array( $storeName => 
'\Wikibase\Client\Scribunto\Test\ClientStoreMock' )
+               );
+
+               static $setUp = false;
+               if ( !$setUp ) {
+                       $testHelper = new WikibaseLuaIntegrationTestHelper();
+                       $testHelper->setUp();
+                       $setUp = true;
+               }
+
+               // Reset the store instance to make sure our Mock is really 
being used
+               $wikibaseClient->getStore( false, 'reset' );
+
+               $this->assertInstanceOf(
+                       'Wikibase\Test\MockRepository',
+                       $wikibaseClient->getStore()->getEntityLookup(),
+                       'Mocking the default client EntityLookup failed'
+               );
+
+               $this->setMwGlobals( 'wgContLang', Language::factory( 'de' ) );
+       }
+
+       public function tearDown() {
+               parent::tearDown();
+               // Reset the store instance, to make sure our Mock wont be used 
in other tests
+               WikibaseClient::getDefaultInstance()->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/WikibaseLuaIntegrationTestHelper.php 
b/client/tests/phpunit/includes/scribunto/WikibaseLuaIntegrationTestHelper.php
new file mode 100644
index 0000000..d4111df
--- /dev/null
+++ 
b/client/tests/phpunit/includes/scribunto/WikibaseLuaIntegrationTestHelper.php
@@ -0,0 +1,180 @@
+<?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\Settings;
+use Wikibase\Test\MockRepository;
+use Wikibase\ClientStore;
+
+/**
+ * ClientStore mock for use in Lua integration tests
+ *
+ * @since 0.5
+ *
+ * @license GNU GPL v2+
+ * @author Marius Hoch < h...@online.de >
+ */
+class ClientStoreMock implements ClientStore {
+       public function getItemUsageIndex() {}
+       public function getPropertyLabelResolver() {}
+       public function getTermIndex() {}
+       public function newChangesTable() {}
+       public function getPropertyInfoStore() {}
+       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();
+       }
+}
+
+/**
+ * Helper class for Lua integration tests.
+ *
+ * @since 0.5
+ *
+ * @license GNU GPL v2+
+ * @author Marius Hoch < h...@online.de >
+ */
+class WikibaseLuaIntegrationTestHelper {
+
+       /* @var MockRepository */
+       protected $mockRepository;
+
+       public function __construct() {
+               $clientStore = new ClientStoreMock();
+               $this->mockRepository = $clientStore->getEntityLookup();
+       }
+
+       /**
+        * Sets up the test data.
+        */
+       public function setUp() {
+               $siteLink = new SiteLink(
+                       Settings::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

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I026fa93047f9564c997a4005952306c853ff5334
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Hoo man <h...@online.de>

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

Reply via email to