aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/logging/ProtectLogFormatterTest.php
diff options
context:
space:
mode:
authorDaimona Eaytoy <daimona.wiki@gmail.com>2023-07-17 17:58:36 +0200
committerDaimona Eaytoy <daimona.wiki@gmail.com>2023-08-01 16:35:20 +0200
commitdb8897dbe7f65bfbd45c52fc3c7378a86bd88154 (patch)
tree1184ea5b11deca4187187b00dc26749fe1da26a2 /tests/phpunit/includes/logging/ProtectLogFormatterTest.php
parent485e47ff102fbf172f6f906e17b6bfa578978fd9 (diff)
downloadmediawikicore-db8897dbe7f65bfbd45c52fc3c7378a86bd88154.tar.gz
mediawikicore-db8897dbe7f65bfbd45c52fc3c7378a86bd88154.zip
LogFormatterTestCase: avoid database access
LogFormatter is another place that uses the global state a lot, with Title, Linker, etc. Some of its dependencies were accessing the database, but most LogFormatter methods are not in the database group. This patch tries to work around the DB dependencies by providing (complex) mocks that do not access the DB. There's a lot of mocking involved, but that's just because of the current state of LogFormatter, and hopefully it'll get better some day. Rights- and ProtectLogFormatter also need a mock DB connection because they use the database to format expiry. Bug: T155147 Change-Id: I4fa9ee4fb246e08cb2b4577454029b5af3276b79
Diffstat (limited to 'tests/phpunit/includes/logging/ProtectLogFormatterTest.php')
-rw-r--r--tests/phpunit/includes/logging/ProtectLogFormatterTest.php22
1 files changed, 19 insertions, 3 deletions
diff --git a/tests/phpunit/includes/logging/ProtectLogFormatterTest.php b/tests/phpunit/includes/logging/ProtectLogFormatterTest.php
index 12c96539a0ee..0cfbd8aa0201 100644
--- a/tests/phpunit/includes/logging/ProtectLogFormatterTest.php
+++ b/tests/phpunit/includes/logging/ProtectLogFormatterTest.php
@@ -1,10 +1,26 @@
<?php
+use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
+use MediaWiki\User\UserIdentityValue;
+use Wikimedia\Rdbms\LBFactory;
+
/**
* @covers ProtectLogFormatter
*/
class ProtectLogFormatterTest extends LogFormatterTestCase {
+ use MockAuthorityTrait;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $db = $this->createNoOpMock( IDatabase::class, [ 'getInfinity' ] );
+ $db->method( 'getInfinity' )->willReturn( 'infinity' );
+ $lbFactory = $this->createMock( LBFactory::class );
+ $lbFactory->method( 'getReplicaDatabase' )->willReturn( $db );
+ $this->setService( 'DBLoadBalancerFactory', $lbFactory );
+ }
+
/**
* Provide different rows from the logging table to test
* for backward compatibility.
@@ -448,8 +464,7 @@ class ProtectLogFormatterTest extends LogFormatterTestCase {
*/
public function testGetActionLinks( array $permissions, $shouldMatch ) {
RequestContext::resetMain();
- $user = $this->getTestUser()->getUser();
- $this->overrideUserPermissions( $user, $permissions );
+ $user = $this->mockUserAuthorityWithPermissions( new UserIdentityValue( 42, __METHOD__ ), $permissions );
$row = $this->expandDatabaseRow( [
'type' => 'protect',
'action' => 'unprotect',
@@ -459,7 +474,8 @@ class ProtectLogFormatterTest extends LogFormatterTestCase {
'params' => [],
], false );
$context = new RequestContext();
- $context->setUser( $user );
+ $context->setAuthority( $user );
+ $context->setLanguage( 'en' );
$formatter = LogFormatter::newFromRow( $row );
$formatter->setContext( $context );
if ( $shouldMatch ) {