Page MenuHomePhabricator

Replace "db" field usages with new getDb() in MediaWikiIntegrationTestCase
Open, MediumPublic

Description

Many integration tests involve database access. For example, they use:

  • LBFactory::getDatabase()
  • LoadBalancer::getConnection()

All of these methods involve a DBConnRef that properly handles the database domain (e.g. what $db->getDomainId() returns). However, MediaWikiIntegrationTestCase has a member field called "db", which was provided for easy access to a database handles. The problem is that it was created before DBConnRef and just directly points to a Database object (instead of a DBConnRef wrapper). This makes usage of MediaWikiIntegrationTestCase::db idiosyncratic and subject to unexpected database domain changes due to other code getting a connection to a domain that is not the current one.

The field is currently defined using an LB method that is supposed to be internal:

$this->db = $lb->getConnectionInternal( DB_PRIMARY );

This fix for this is to:

  • Fix uses of $this->db that block https://gerrit.wikimedia.org/r/c/mediawiki/core/+/519305
  • Remove use of $this->db from subclasses of MediaWikiIntegrationTestCase that override the field (e.g. << $this->db = ... >>> in setUp()) but can just use MediaWikiIntegrationTestCase::getDB() in each test function. For example, DatabaseIntegrationTest currently has << $this->db = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase() >>.
  • Remove use of $this->db from subclasses of MediaWikiIntegrationTestCase that rely on the parent class to set the field. They can use MediaWikiIntegrationTestCase::getDB() in each test method instead.

When grepping, note that many classes do harmless things like << $this->db = new DatabaseTestHelper(...) >> and are not subclasses of MediaWikiIntegrationTestCase.

Code search: https://codesearch.wmcloud.org/search/?q=-%3Edb-%3E&files=Test%5C.php%24&excludeFiles=&repos=

Event Timeline

Change 828651 had a related patch set uploaded (by Aaron Schulz; author: Aaron Schulz):

[mediawiki/core@master] unittests: add MediaWikiIntegrationTestCase::getDb()

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

Change 828652 had a related patch set uploaded (by Aaron Schulz; author: Aaron Schulz):

[mediawiki/extensions/AbuseFilter@master] Use MediaWikiIntegrationTestCase::getDb() instead of the "db" member

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

Change 828651 merged by jenkins-bot:

[mediawiki/core@master] unittests: add MediaWikiIntegrationTestCase::getDb()

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

Change 828652 merged by jenkins-bot:

[mediawiki/extensions/AbuseFilter@master] Use MediaWikiIntegrationTestCase::getDb() instead of the "db" member

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

aaron removed aaron as the assignee of this task.Sep 28 2022, 9:48 PM
aaron updated the task description. (Show Details)
aaron updated the task description. (Show Details)
aaron added a subscriber: Atieno.
daniel triaged this task as Medium priority.Apr 4 2024, 3:10 PM
daniel added a project: good first task.

Change #1026876 had a related patch set uploaded (by Lgaulia; author: Lgaulia):

[mediawiki/core@master] Test: Replace db filed usages with getDb() in MediaWikiIntegrationTestCase

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

Change #1026876 merged by jenkins-bot:

[mediawiki/core@master] Test: Replace db filed usages with getDb() in MediaWikiIntegrationTestCase

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

Change #1043164 had a related patch set uploaded (by Wandji collins; author: Wandji collins):

[mediawiki/core@master] Replace db with getDb for Tests

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

Change #1043248 had a related patch set uploaded (by Wandji collins; author: Wandji collins):

[mediawiki/extensions/Wikibase@master] Use getDb rather than db for integration Tests

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