Skip to content

Commit

Permalink
Avoid DB access in non-Database tests
Browse files Browse the repository at this point in the history
When possible, mock the services which access the DB (one notable
example is ChangeTagsStore in many API tests), or provide explicitly the
data that would be looked up in the DB (e.g., Title content model). Add
the test to the Database group otherwise.

Change-Id: Icb3b3ad726bdcfcb64186cf2644d6d2bf3041c78
  • Loading branch information
Daimona committed Aug 6, 2023
1 parent 0cdc972 commit a13f59d
Show file tree
Hide file tree
Showing 24 changed files with 118 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use Wikibase\Lib\SettingsArray;
use Wikibase\Lib\Store\SiteLinkLookup;
use Wikibase\Lib\Tests\MockRepository;
use Wikimedia\TestingAccessWrapper;

/**
* @covers \Wikibase\Client\Hooks\ParserOutputUpdateHookHandler
Expand Down Expand Up @@ -320,6 +321,8 @@ public function testDoContentAlterParserOutput(
array $expectedSisterLinks,
array $expectedBadges = null
) {
$titleWrapper = TestingAccessWrapper::newFromObject( $title );
$titleWrapper->mRedirect = false;
$content = $this->createMock( Content::class );
$parserOutput = $this->newParserOutput( $extensionDataAppend, [] );
$handler = $this->newParserOutputUpdateHookHandler( $this->getTestSiteLinkData() );
Expand Down Expand Up @@ -420,6 +423,8 @@ public function testGivenSitelinkHasStatementWithUnknownEntityType_linkDataIsAdd

$content = $this->createMock( Content::class );
$title = Title::makeTitle( NS_MAIN, 'Foobarium' );
$titleWrapper = TestingAccessWrapper::newFromObject( $title );
$titleWrapper->mRedirect = false;

$parserOutput = $this->newParserOutput( [], [] );

Expand Down
2 changes: 2 additions & 0 deletions lib/tests/phpunit/Formatters/AutoCommentFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public static function provideTestAutoComment() {
* @dataProvider provideTestAutoComment
*/
public function testFormatAutoComment( array $prefixes, $auto, $expected ) {
// If the Translate extension is installed, its handler for ParserBeforeInternalParse can trigger DB access
$this->clearHook( 'ParserBeforeInternalParse' );
$formatter = new AutoCommentFormatter( $this->language, $prefixes );
$value = $formatter->formatAutoComment( $auto );
$this->assertEquals( $expected, $value );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Wikibase\Repo\Tests\RestApi\RouteHandlers;

use MediaWiki\ChangeTags\ChangeTagsStore;
use MediaWiki\Rest\Handler;
use MediaWiki\Rest\Reporter\ErrorReporter;
use MediaWiki\Rest\RequestData;
Expand All @@ -23,6 +24,13 @@ class AddItemStatementRouteHandlerTest extends MediaWikiIntegrationTestCase {

use HandlerTestTrait;

protected function setUp(): void {
parent::setUp();
$changeTagsStore = $this->createMock( ChangeTagsStore::class );
$changeTagsStore->method( 'listExplicitlyDefinedTags' )->willReturn( [] );
$this->setService( 'ChangeTagsStore', $changeTagsStore );
}

public function testHandlesUnexpectedErrors(): void {
$useCase = $this->createStub( AddItemStatement::class );
$useCase->method( 'execute' )->willThrowException( new RuntimeException() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @covers \Wikibase\Repo\RestApi\RouteHandlers\GetItemStatementRouteHandler
*
* @group Wikibase
* @group Database
*
* @license GPL-2.0-or-later
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* @covers \Wikibase\Repo\RestApi\RouteHandlers\GetPropertyStatementRouteHandler
*
* @group Wikibase
* @group Database
*
* @license GPL-2.0-or-later
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Wikibase\Repo\Tests\RestApi\RouteHandlers;

use MediaWiki\ChangeTags\ChangeTagsStore;
use MediaWiki\Rest\Handler;
use MediaWiki\Rest\Reporter\ErrorReporter;
use MediaWiki\Rest\RequestData;
Expand All @@ -23,6 +24,13 @@ class PatchItemLabelsRouteHandlerTest extends MediaWikiIntegrationTestCase {

use HandlerTestTrait;

protected function setUp(): void {
parent::setUp();
$changeTagsStore = $this->createMock( ChangeTagsStore::class );
$changeTagsStore->method( 'listExplicitlyDefinedTags' )->willReturn( [] );
$this->setService( 'ChangeTagsStore', $changeTagsStore );
}

public function testHandlesUnexpectedErrors(): void {
$useCase = $this->createStub( PatchItemLabels::class );
$useCase->method( 'execute' )->willThrowException( new RuntimeException() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Wikibase\Repo\Tests\RestApi\RouteHandlers;

use MediaWiki\ChangeTags\ChangeTagsStore;
use MediaWiki\Rest\Handler;
use MediaWiki\Rest\Reporter\ErrorReporter;
use MediaWiki\Rest\RequestData;
Expand All @@ -23,6 +24,13 @@ class PatchItemStatementRouteHandlerTest extends MediaWikiIntegrationTestCase {

use HandlerTestTrait;

protected function setUp(): void {
parent::setUp();
$changeTagsStore = $this->createMock( ChangeTagsStore::class );
$changeTagsStore->method( 'listExplicitlyDefinedTags' )->willReturn( [] );
$this->setService( 'ChangeTagsStore', $changeTagsStore );
}

public function testHandlesUnexpectedErrors(): void {
$useCase = $this->createStub( PatchItemStatement::class );
$useCase->method( 'execute' )->willThrowException( new RuntimeException() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Wikibase\Repo\Tests\RestApi\RouteHandlers;

use MediaWiki\ChangeTags\ChangeTagsStore;
use MediaWiki\Rest\Handler;
use MediaWiki\Rest\Reporter\ErrorReporter;
use MediaWiki\Rest\RequestData;
Expand All @@ -23,6 +24,13 @@ class PatchStatementRouteHandlerTest extends MediaWikiIntegrationTestCase {

use HandlerTestTrait;

protected function setUp(): void {
parent::setUp();
$changeTagsStore = $this->createMock( ChangeTagsStore::class );
$changeTagsStore->method( 'listExplicitlyDefinedTags' )->willReturn( [] );
$this->setService( 'ChangeTagsStore', $changeTagsStore );
}

public function testHandlesUnexpectedErrors(): void {
$useCase = $this->createStub( PatchItemStatement::class );
$useCase->method( 'execute' )->willThrowException( new RuntimeException() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Wikibase\Repo\Tests\RestApi\RouteHandlers;

use MediaWiki\ChangeTags\ChangeTagsStore;
use MediaWiki\Rest\Handler;
use MediaWiki\Rest\Reporter\ErrorReporter;
use MediaWiki\Rest\RequestData;
Expand All @@ -23,6 +24,13 @@ class RemoveItemStatementRouteHandlerTest extends MediaWikiIntegrationTestCase {

use HandlerTestTrait;

protected function setUp(): void {
parent::setUp();
$changeTagsStore = $this->createMock( ChangeTagsStore::class );
$changeTagsStore->method( 'listExplicitlyDefinedTags' )->willReturn( [] );
$this->setService( 'ChangeTagsStore', $changeTagsStore );
}

public function testHandlesUnexpectedErrors(): void {
$useCase = $this->createStub( RemoveItemStatement::class );
$useCase->method( 'execute' )->willThrowException( new RuntimeException() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Wikibase\Repo\Tests\RestApi\RouteHandlers;

use MediaWiki\ChangeTags\ChangeTagsStore;
use MediaWiki\Rest\Handler;
use MediaWiki\Rest\Reporter\ErrorReporter;
use MediaWiki\Rest\RequestData;
Expand All @@ -23,6 +24,13 @@ class RemoveStatementRouteHandlerTest extends MediaWikiIntegrationTestCase {

use HandlerTestTrait;

protected function setUp(): void {
parent::setUp();
$changeTagsStore = $this->createMock( ChangeTagsStore::class );
$changeTagsStore->method( 'listExplicitlyDefinedTags' )->willReturn( [] );
$this->setService( 'ChangeTagsStore', $changeTagsStore );
}

public function testHandlesUnexpectedErrors(): void {
$useCase = $this->createStub( RemoveItemStatement::class );
$useCase->method( 'execute' )->willThrowException( new RuntimeException() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Wikibase\Repo\Tests\RestApi\RouteHandlers;

use MediaWiki\ChangeTags\ChangeTagsStore;
use MediaWiki\Rest\Handler;
use MediaWiki\Rest\Reporter\ErrorReporter;
use MediaWiki\Rest\RequestData;
Expand All @@ -24,6 +25,13 @@ class ReplaceItemStatementRouteHandlerTest extends MediaWikiIntegrationTestCase

use HandlerTestTrait;

protected function setUp(): void {
parent::setUp();
$changeTagsStore = $this->createMock( ChangeTagsStore::class );
$changeTagsStore->method( 'listExplicitlyDefinedTags' )->willReturn( [] );
$this->setService( 'ChangeTagsStore', $changeTagsStore );
}

public function testHandlesUnexpectedErrors(): void {
$useCase = $this->createStub( ReplaceStatement::class );
$useCase->method( 'execute' )->willThrowException( new RuntimeException() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Wikibase\Repo\Tests\RestApi\RouteHandlers;

use MediaWiki\ChangeTags\ChangeTagsStore;
use MediaWiki\Rest\Handler;
use MediaWiki\Rest\Reporter\ErrorReporter;
use MediaWiki\Rest\RequestData;
Expand All @@ -23,6 +24,13 @@ class ReplaceStatementRouteHandlerTest extends MediaWikiIntegrationTestCase {

use HandlerTestTrait;

protected function setUp(): void {
parent::setUp();
$changeTagsStore = $this->createMock( ChangeTagsStore::class );
$changeTagsStore->method( 'listExplicitlyDefinedTags' )->willReturn( [] );
$this->setService( 'ChangeTagsStore', $changeTagsStore );
}

public function testHandlesUnexpectedErrors(): void {
$useCase = $this->createStub( ReplaceStatement::class );
$useCase->method( 'execute' )->willThrowException( new RuntimeException() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/**
* @covers \Wikibase\Repo\RestApi\RouteHandlers\SetItemDescriptionRouteHandler
* @group Wikibase
* @group Database
* @license GPL-2.0-or-later
*/
class SetItemDescriptionRouteHandlerTest extends \MediaWikiIntegrationTestCase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/**
* @covers \Wikibase\Repo\RestApi\RouteHandlers\SetItemLabelRouteHandler
* @group Wikibase
* @group Database
* @license GPL-2.0-or-later
*/
class SetItemLabelRouteHandlerTest extends \MediaWikiIntegrationTestCase {
Expand Down
3 changes: 3 additions & 0 deletions repo/tests/phpunit/includes/Api/QuerySearchEntitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ApiPageSet;
use ApiQuery;
use MediaWiki\Request\FauxRequest;
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
use MediaWikiIntegrationTestCase;
use RequestContext;
use Status;
Expand All @@ -32,6 +33,7 @@
* @author Bene* < [email protected] >
*/
class QuerySearchEntitiesTest extends MediaWikiIntegrationTestCase {
use MockAuthorityTrait;

/**
* @param array $params
Expand All @@ -42,6 +44,7 @@ private function getApiQuery( array $params ) {
$context = new RequestContext();
$context->setLanguage( 'en-ca' );
$context->setRequest( new FauxRequest( $params, true ) );
$context->setAuthority( $this->mockRegisteredNullAuthority() );
$main = new ApiMain( $context );
return $main->getModuleManager()->getModule( 'query' );
}
Expand Down
10 changes: 10 additions & 0 deletions repo/tests/phpunit/includes/Content/EntityHandlerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use DataValues\Serializers\DataValueSerializer;
use DummySearchIndexFieldDefinition;
use InvalidArgumentException;
use LinkBatch;
use LogicException;
use MediaWiki\Cache\LinkBatchFactory;
use MediaWiki\MediaWikiServices;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Revision\SlotRecord;
Expand All @@ -17,6 +19,7 @@
use RuntimeException;
use SearchEngine;
use Serializers\Serializer;
use Site;
use Title;
use Wikibase\DataAccess\DatabaseEntitySource;
use Wikibase\DataAccess\EntitySourceDefinitions;
Expand Down Expand Up @@ -56,6 +59,7 @@ protected function setUp(): void {
'WikibaseRepo.EntityTypeDefinitions',
$this->getEntityTypeDefinitions()
);
$this->setService( 'WikibaseClient.SiteGroup', Site::GROUP_NONE );
}

abstract public function getModelId();
Expand Down Expand Up @@ -720,7 +724,13 @@ public function providePageProperties() {
* @dataProvider providePageProperties
*/
public function testPageProperties( EntityContent $content, array $expectedProps ) {
$lb = $this->createMock( LinkBatch::class );
$lb->method( 'doQuery' )->willReturn( false );
$lbFactory = $this->createMock( LinkBatchFactory::class );
$lbFactory->method( 'newLinkBatch' )->willReturn( $lb );
$this->setService( 'LinkBatchFactory', $lbFactory );
$title = Title::makeTitle( NS_MAIN, 'Foo' );
$title->setContentModel( CONTENT_MODEL_WIKITEXT );
$contentRenderer = $this->getServiceContainer()->getContentRenderer();
$parserOutput = $contentRenderer->getParserOutput( $content, $title, null, null, false );

Expand Down
4 changes: 4 additions & 0 deletions repo/tests/phpunit/includes/Content/PropertyHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Wikibase\Repo\Tests\Content;

use MediaWiki\Page\WikiPageFactory;
use ParserOutput;
use Title;
use Wikibase\DataModel\Entity\EntityDocument;
Expand All @@ -25,6 +26,7 @@
* @group WikibaseProperty
* @group WikibaseEntity
* @group WikibaseEntityHandler
* @group Database
*
* @license GPL-2.0-or-later
* @author Jeroen De Dauw < [email protected] >
Expand Down Expand Up @@ -196,10 +198,12 @@ public function testDataForSearchIndex() {
}

public function testGetParserOutput() {
$this->setService( 'WikiPageFactory', $this->createMock( WikiPageFactory::class ) );
$content = $this->newEntityContent();
$contentRenderer = $this->getServiceContainer()->getContentRenderer();

$title = Title::makeTitle( NS_MAIN, 'Foo' );
$title->setContentModel( CONTENT_MODEL_WIKITEXT );
$parserOutput = $contentRenderer->getParserOutput( $content, $title );

$expectedUsedOptions = [ 'userlang', 'wb', 'termboxVersion' ];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Wikibase\DataModel\Entity\Property;
use Wikibase\Repo\FederatedProperties\ApiRequestExecutionException;
use Wikibase\Repo\Tests\Hooks\HtmlPageLinkRendererEndHookHandlerTestBase;
use Wikimedia\TestingAccessWrapper;

/**
* @covers \Wikibase\Repo\Hooks\HtmlPageLinkRendererEndHookHandler
Expand All @@ -28,6 +29,8 @@ public function testDoHtmlPageLinkRendererBegin( LinkRenderer $linkRenderer, Req
$handler = $this->newInstance( 'foo', false, true, Property::ENTITY_TYPE );

$title = Title::makeTitle( WB_NS_PROPERTY, self::PROPERTY_WITH_LABEL );
$wrapper = TestingAccessWrapper::newFromObject( $title );
$wrapper->mRedirect = false;
$text = $title->getFullText();
$customAttribs = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
declare( strict_types = 1 );
namespace Wikibase\Repo\Tests\FederatedProperties\ParserOutput;

use LinkBatch;
use MediaWiki\Cache\LinkBatchFactory;
use Psr\SimpleCache\CacheInterface;
use RepoGroup;
use Wikibase\DataModel\Entity\EntityId;
Expand Down Expand Up @@ -34,6 +36,12 @@
class FederatedPropertiesPrefetchingEntityParserOutputGeneratorDecoratorTest extends EntityParserOutputGeneratorTestBase {

public function testShouldPrefetchFederatedProperties() {
$lb = $this->createMock( LinkBatch::class );
$lb->method( 'doQuery' )->willReturn( false );
$lbFactory = $this->createMock( LinkBatchFactory::class );
$lbFactory->method( 'newLinkBatch' )->willReturn( $lb );
$this->setService( 'LinkBatchFactory', $lbFactory );

$labelLanguage = 'en';

$fedPropId1 = new FederatedPropertyId( 'http://wikidata.org/entity/P123', 'P123' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* @covers \Wikibase\Repo\Hooks\MakeGlobalVariablesScriptHookHandler
*
* @group Wikibase
* @group Database
*
* @license GPL-2.0-or-later
*/
Expand Down
1 change: 1 addition & 0 deletions repo/tests/phpunit/includes/Rdf/RdfBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
*
* @group Wikibase
* @group WikibaseRdf
* @group Database
*
* @license GPL-2.0-or-later
* @author Daniel Kinzler
Expand Down
Loading

0 comments on commit a13f59d

Please sign in to comment.