From c581aab15e99375ad3f28ba5baeebdefed3fc1f6 Mon Sep 17 00:00:00 2001 From: Daimona Eaytoy Date: Thu, 10 Aug 2023 00:05:31 +0200 Subject: [PATCH] Avoid using TestUser in non-Database tests TestUser is expensive and requires DB access because it creates the user in the test database. That is not needed here. For these tests, a mock is enough. Also make PopupsGadgetsIntegrationTest an integration test, to fix a local failure and in view of I0a04c82250582fed7a66c1e10868d9b4f3823a28. If a test is testing the integration of X with Y, guess what, it's an integration test ;-) Change-Id: Ie8b3376ce97b9ddc67746f7754b92628c5ab9470 --- tests/phpunit/PopupsContextTest.php | 20 ++++++++++++------- .../PopupsGadgetsIntegrationTest.php | 2 +- tests/phpunit/PopupsHooksTest.php | 10 +++++----- 3 files changed, 19 insertions(+), 13 deletions(-) rename tests/phpunit/{unit => }/PopupsGadgetsIntegrationTest.php (98%) diff --git a/tests/phpunit/PopupsContextTest.php b/tests/phpunit/PopupsContextTest.php index d3f0f18e4..c6fa0fd73 100644 --- a/tests/phpunit/PopupsContextTest.php +++ b/tests/phpunit/PopupsContextTest.php @@ -19,6 +19,7 @@ * @ingroup extensions */ +use MediaWiki\User\UserOptionsLookup; use PHPUnit\Framework\MockObject\Stub\ConsecutiveCalls; use Popups\PopupsContext; use Popups\PopupsGadgetsIntegration; @@ -94,8 +95,8 @@ class PopupsContextTest extends MediaWikiIntegrationTestCase { * @covers ::shouldSendModuleToUser */ public function testShouldSendToAnonUser() { - $user = $this->getMutableTestUser()->getUser(); - $user->setId( self::ANONYMOUS_USER ); + $user = $this->createMock( User::class ); + $user->method( 'getId' )->willReturn( self::ANONYMOUS_USER ); $context = $this->getContext(); $this->assertTrue( @@ -118,10 +119,15 @@ class PopupsContextTest extends MediaWikiIntegrationTestCase { 'wgPopupsReferencePreviews' => false, ] ); + $user = $this->createMock( User::class ); + $user->method( 'isNamed' )->willReturn( true ); + $userOptLookup = $this->createMock( UserOptionsLookup::class ); + $userOptLookup->method( 'getBoolOption' ) + ->with( $user, PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME ) + ->willReturn( $optIn ); + $this->setService( 'UserOptionsLookup', $userOptLookup ); + $context = $this->getContext(); - $user = $this->getMutableTestUser()->getUser(); - $userOptionsManager = $this->getServiceContainer()->getUserOptionsManager(); - $userOptionsManager->setOption( $user, PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME, $optIn ); $this->assertSame( $expected, $context->shouldSendModuleToUser( $user ), ( $expected ? 'A' : 'No' ) . ' module is sent to the user.' ); @@ -252,7 +258,7 @@ class PopupsContextTest extends MediaWikiIntegrationTestCase { public function testConflictsWithNavPopupsGadget() { $integrationMock = $this->createMock( PopupsGadgetsIntegration::class ); - $user = $this->getTestUser()->getUser(); + $user = $this->createMock( User::class ); $integrationMock->expects( $this->once() ) ->method( 'conflictsWithNavPopupsGadget' ) @@ -301,7 +307,7 @@ class PopupsContextTest extends MediaWikiIntegrationTestCase { $this->assertSame( $expected, - $contextMock->getConfigBitmaskFromUser( $this->getTestUser()->getUser() ) + $contextMock->getConfigBitmaskFromUser( $this->createMock( User::class ) ) ); } diff --git a/tests/phpunit/unit/PopupsGadgetsIntegrationTest.php b/tests/phpunit/PopupsGadgetsIntegrationTest.php similarity index 98% rename from tests/phpunit/unit/PopupsGadgetsIntegrationTest.php rename to tests/phpunit/PopupsGadgetsIntegrationTest.php index 771945734..9d6522689 100644 --- a/tests/phpunit/unit/PopupsGadgetsIntegrationTest.php +++ b/tests/phpunit/PopupsGadgetsIntegrationTest.php @@ -27,7 +27,7 @@ use Popups\PopupsGadgetsIntegration; * @group Popups * @coversDefaultClass \Popups\PopupsGadgetsIntegration */ -class PopupsGadgetsIntegrationTest extends MediaWikiUnitTestCase { +class PopupsGadgetsIntegrationTest extends MediaWikiIntegrationTestCase { /** * Gadget name for testing diff --git a/tests/phpunit/PopupsHooksTest.php b/tests/phpunit/PopupsHooksTest.php index affcc3aba..bd0adde44 100644 --- a/tests/phpunit/PopupsHooksTest.php +++ b/tests/phpunit/PopupsHooksTest.php @@ -46,7 +46,7 @@ class PopupsHooksTest extends MediaWikiIntegrationTestCase { $userOptionsManager = $this->getServiceContainer()->getUserOptionsManager(); ( new PopupsHooks( $userOptionsManager ) ) - ->onGetPreferences( $this->getTestUser()->getUser(), $prefs ); + ->onGetPreferences( $this->createMock( User::class ), $prefs ); $this->assertCount( 1, $prefs, 'No preferences are retrieved.' ); $this->assertSame( 'notEmpty', $prefs[ 'someNotEmptyValue'], @@ -57,7 +57,7 @@ class PopupsHooksTest extends MediaWikiIntegrationTestCase { * @covers ::onGetPreferences */ public function testOnGetPreferencesNavPopupGadgetIsOn() { - $userMock = $this->getTestUser()->getUser(); + $userMock = $this->createMock( User::class ); $contextMock = $this->createMock( PopupsContext::class ); $contextMock->expects( $this->once() ) @@ -73,7 +73,7 @@ class PopupsHooksTest extends MediaWikiIntegrationTestCase { $userOptionsManager = $this->getServiceContainer()->getUserOptionsManager(); ( new PopupsHooks( $userOptionsManager ) ) - ->onGetPreferences( $this->getTestUser()->getUser(), $prefs ); + ->onGetPreferences( $userMock, $prefs ); $this->assertArrayHasKey( PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME, $prefs, 'The opt-in preference is retrieved.' ); @@ -108,7 +108,7 @@ class PopupsHooksTest extends MediaWikiIntegrationTestCase { $userOptionsManager = $this->getServiceContainer()->getUserOptionsManager(); ( new PopupsHooks( $userOptionsManager ) ) - ->onGetPreferences( $this->getTestUser()->getUser(), $prefs ); + ->onGetPreferences( $this->createMock( User::class ), $prefs ); $this->assertGreaterThan( 3, count( $prefs ), 'A preference is retrieved.' ); $this->assertSame( 'notEmpty', $prefs[ 'someNotEmptyValue'], @@ -142,7 +142,7 @@ class PopupsHooksTest extends MediaWikiIntegrationTestCase { $userOptionsManager = $this->getServiceContainer()->getUserOptionsManager(); ( new PopupsHooks( $userOptionsManager ) ) - ->onGetPreferences( $this->getTestUser()->getUser(), $prefs ); + ->onGetPreferences( $this->createMock( User::class ), $prefs ); $this->assertGreaterThan( 2, count( $prefs ), 'A preference is retrieved.' ); $this->assertArrayHasKey( PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME, $prefs,