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
This commit is contained in:
Daimona Eaytoy 2023-08-10 00:05:31 +02:00
parent 72121abf87
commit c581aab15e
3 changed files with 19 additions and 13 deletions

View file

@ -19,6 +19,7 @@
* @ingroup extensions * @ingroup extensions
*/ */
use MediaWiki\User\UserOptionsLookup;
use PHPUnit\Framework\MockObject\Stub\ConsecutiveCalls; use PHPUnit\Framework\MockObject\Stub\ConsecutiveCalls;
use Popups\PopupsContext; use Popups\PopupsContext;
use Popups\PopupsGadgetsIntegration; use Popups\PopupsGadgetsIntegration;
@ -94,8 +95,8 @@ class PopupsContextTest extends MediaWikiIntegrationTestCase {
* @covers ::shouldSendModuleToUser * @covers ::shouldSendModuleToUser
*/ */
public function testShouldSendToAnonUser() { public function testShouldSendToAnonUser() {
$user = $this->getMutableTestUser()->getUser(); $user = $this->createMock( User::class );
$user->setId( self::ANONYMOUS_USER ); $user->method( 'getId' )->willReturn( self::ANONYMOUS_USER );
$context = $this->getContext(); $context = $this->getContext();
$this->assertTrue( $this->assertTrue(
@ -118,10 +119,15 @@ class PopupsContextTest extends MediaWikiIntegrationTestCase {
'wgPopupsReferencePreviews' => false, '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(); $context = $this->getContext();
$user = $this->getMutableTestUser()->getUser();
$userOptionsManager = $this->getServiceContainer()->getUserOptionsManager();
$userOptionsManager->setOption( $user, PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME, $optIn );
$this->assertSame( $expected, $this->assertSame( $expected,
$context->shouldSendModuleToUser( $user ), $context->shouldSendModuleToUser( $user ),
( $expected ? 'A' : 'No' ) . ' module is sent to the user.' ); ( $expected ? 'A' : 'No' ) . ' module is sent to the user.' );
@ -252,7 +258,7 @@ class PopupsContextTest extends MediaWikiIntegrationTestCase {
public function testConflictsWithNavPopupsGadget() { public function testConflictsWithNavPopupsGadget() {
$integrationMock = $this->createMock( PopupsGadgetsIntegration::class ); $integrationMock = $this->createMock( PopupsGadgetsIntegration::class );
$user = $this->getTestUser()->getUser(); $user = $this->createMock( User::class );
$integrationMock->expects( $this->once() ) $integrationMock->expects( $this->once() )
->method( 'conflictsWithNavPopupsGadget' ) ->method( 'conflictsWithNavPopupsGadget' )
@ -301,7 +307,7 @@ class PopupsContextTest extends MediaWikiIntegrationTestCase {
$this->assertSame( $this->assertSame(
$expected, $expected,
$contextMock->getConfigBitmaskFromUser( $this->getTestUser()->getUser() ) $contextMock->getConfigBitmaskFromUser( $this->createMock( User::class ) )
); );
} }

View file

@ -27,7 +27,7 @@ use Popups\PopupsGadgetsIntegration;
* @group Popups * @group Popups
* @coversDefaultClass \Popups\PopupsGadgetsIntegration * @coversDefaultClass \Popups\PopupsGadgetsIntegration
*/ */
class PopupsGadgetsIntegrationTest extends MediaWikiUnitTestCase { class PopupsGadgetsIntegrationTest extends MediaWikiIntegrationTestCase {
/** /**
* Gadget name for testing * Gadget name for testing

View file

@ -46,7 +46,7 @@ class PopupsHooksTest extends MediaWikiIntegrationTestCase {
$userOptionsManager = $this->getServiceContainer()->getUserOptionsManager(); $userOptionsManager = $this->getServiceContainer()->getUserOptionsManager();
( new PopupsHooks( $userOptionsManager ) ) ( new PopupsHooks( $userOptionsManager ) )
->onGetPreferences( $this->getTestUser()->getUser(), $prefs ); ->onGetPreferences( $this->createMock( User::class ), $prefs );
$this->assertCount( 1, $prefs, 'No preferences are retrieved.' ); $this->assertCount( 1, $prefs, 'No preferences are retrieved.' );
$this->assertSame( 'notEmpty', $this->assertSame( 'notEmpty',
$prefs[ 'someNotEmptyValue'], $prefs[ 'someNotEmptyValue'],
@ -57,7 +57,7 @@ class PopupsHooksTest extends MediaWikiIntegrationTestCase {
* @covers ::onGetPreferences * @covers ::onGetPreferences
*/ */
public function testOnGetPreferencesNavPopupGadgetIsOn() { public function testOnGetPreferencesNavPopupGadgetIsOn() {
$userMock = $this->getTestUser()->getUser(); $userMock = $this->createMock( User::class );
$contextMock = $this->createMock( PopupsContext::class ); $contextMock = $this->createMock( PopupsContext::class );
$contextMock->expects( $this->once() ) $contextMock->expects( $this->once() )
@ -73,7 +73,7 @@ class PopupsHooksTest extends MediaWikiIntegrationTestCase {
$userOptionsManager = $this->getServiceContainer()->getUserOptionsManager(); $userOptionsManager = $this->getServiceContainer()->getUserOptionsManager();
( new PopupsHooks( $userOptionsManager ) ) ( new PopupsHooks( $userOptionsManager ) )
->onGetPreferences( $this->getTestUser()->getUser(), $prefs ); ->onGetPreferences( $userMock, $prefs );
$this->assertArrayHasKey( PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME, $this->assertArrayHasKey( PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME,
$prefs, $prefs,
'The opt-in preference is retrieved.' ); 'The opt-in preference is retrieved.' );
@ -108,7 +108,7 @@ class PopupsHooksTest extends MediaWikiIntegrationTestCase {
$userOptionsManager = $this->getServiceContainer()->getUserOptionsManager(); $userOptionsManager = $this->getServiceContainer()->getUserOptionsManager();
( new PopupsHooks( $userOptionsManager ) ) ( new PopupsHooks( $userOptionsManager ) )
->onGetPreferences( $this->getTestUser()->getUser(), $prefs ); ->onGetPreferences( $this->createMock( User::class ), $prefs );
$this->assertGreaterThan( 3, count( $prefs ), 'A preference is retrieved.' ); $this->assertGreaterThan( 3, count( $prefs ), 'A preference is retrieved.' );
$this->assertSame( 'notEmpty', $this->assertSame( 'notEmpty',
$prefs[ 'someNotEmptyValue'], $prefs[ 'someNotEmptyValue'],
@ -142,7 +142,7 @@ class PopupsHooksTest extends MediaWikiIntegrationTestCase {
$userOptionsManager = $this->getServiceContainer()->getUserOptionsManager(); $userOptionsManager = $this->getServiceContainer()->getUserOptionsManager();
( new PopupsHooks( $userOptionsManager ) ) ( new PopupsHooks( $userOptionsManager ) )
->onGetPreferences( $this->getTestUser()->getUser(), $prefs ); ->onGetPreferences( $this->createMock( User::class ), $prefs );
$this->assertGreaterThan( 2, count( $prefs ), 'A preference is retrieved.' ); $this->assertGreaterThan( 2, count( $prefs ), 'A preference is retrieved.' );
$this->assertArrayHasKey( PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME, $this->assertArrayHasKey( PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME,
$prefs, $prefs,