Merge "Streamline ReferencePreviewsGadgetsIntegrationTest"

This commit is contained in:
jenkins-bot 2024-04-26 11:01:40 +00:00 committed by Gerrit Code Review
commit a004fbb2f7

View file

@ -5,11 +5,11 @@ namespace Cite\Tests\Integration\ReferencePreviews;
use Cite\ReferencePreviews\ReferencePreviewsGadgetsIntegration; use Cite\ReferencePreviews\ReferencePreviewsGadgetsIntegration;
use InvalidArgumentException; use InvalidArgumentException;
use MediaWiki\Config\Config; use MediaWiki\Config\Config;
use MediaWiki\Config\HashConfig;
use MediaWiki\Extension\Gadgets\Gadget; use MediaWiki\Extension\Gadgets\Gadget;
use MediaWiki\Extension\Gadgets\GadgetRepo; use MediaWiki\Extension\Gadgets\GadgetRepo;
use MediaWiki\User\User; use MediaWiki\User\User;
use MediaWikiIntegrationTestCase; use MediaWikiIntegrationTestCase;
use PHPUnit\Framework\MockObject\MockObject;
/** /**
* @coversDefaultClass \Cite\ReferencePreviews\ReferencePreviewsGadgetsIntegration * @coversDefaultClass \Cite\ReferencePreviews\ReferencePreviewsGadgetsIntegration
@ -31,18 +31,11 @@ class ReferencePreviewsGadgetsIntegrationTest extends MediaWikiIntegrationTestCa
*/ */
private const GADGET_DISABLED = false; private const GADGET_DISABLED = false;
/** private function getConfig( ?string $gadgetName = self::NAV_POPUPS_GADGET_NAME ): Config {
* @return MockObject|Config return new HashConfig( [
*/ ReferencePreviewsGadgetsIntegration::CONFIG_NAVIGATION_POPUPS_NAME => $gadgetName,
private function getConfigMock() { ReferencePreviewsGadgetsIntegration::CONFIG_REFERENCE_TOOLTIPS_NAME => $gadgetName,
$mock = $this->createMock( Config::class ); ] );
$mock->expects( $this->atLeastOnce() )
->method( 'get' )
->willReturnMap( [
[ ReferencePreviewsGadgetsIntegration::CONFIG_NAVIGATION_POPUPS_NAME, self::NAV_POPUPS_GADGET_NAME ],
[ ReferencePreviewsGadgetsIntegration::CONFIG_REFERENCE_TOOLTIPS_NAME, self::NAV_POPUPS_GADGET_NAME ],
] );
return $mock;
} }
/** /**
@ -51,26 +44,28 @@ class ReferencePreviewsGadgetsIntegrationTest extends MediaWikiIntegrationTestCa
* @covers ::sanitizeGadgetName * @covers ::sanitizeGadgetName
*/ */
public function testConflictsWithNavPopupsGadgetIfGadgetsExtensionIsNotLoaded() { public function testConflictsWithNavPopupsGadgetIfGadgetsExtensionIsNotLoaded() {
$user = $this->createMock( User::class ); $integration = new ReferencePreviewsGadgetsIntegration( $this->getConfig() );
$integration = new ReferencePreviewsGadgetsIntegration( $this->getConfigMock() );
$this->assertFalse( $this->assertFalse(
$integration->isNavPopupsGadgetEnabled( $user ), $integration->isNavPopupsGadgetEnabled( $this->createNoOpMock( User::class ) ),
'No conflict is identified.' ); 'No conflict is identified.'
);
} }
/** /**
* @covers ::isNavPopupsGadgetEnabled * @covers ::isNavPopupsGadgetEnabled
*/ */
public function testConflictsWithNavPopupsGadgetIfGadgetNotExists() { public function testConflictsWithNavPopupsGadgetIfGadgetNotExists() {
$user = $this->createMock( User::class );
$gadgetRepoMock = $this->createMock( GadgetRepo::class ); $gadgetRepoMock = $this->createMock( GadgetRepo::class );
$gadgetRepoMock->expects( $this->once() ) $gadgetRepoMock->expects( $this->once() )
->method( 'getGadgetIds' ) ->method( 'getGadgetIds' )
->willReturn( [] ); ->willReturn( [] );
$this->executeConflictsWithNavPopupsGadgetSafeCheck( $user, $this->getConfigMock(), $this->executeIsNavPopupsGadgetEnabled(
$gadgetRepoMock, self::GADGET_DISABLED ); $this->createNoOpMock( User::class ),
$this->getConfig(),
$gadgetRepoMock,
self::GADGET_DISABLED
);
} }
/** /**
@ -94,8 +89,12 @@ class ReferencePreviewsGadgetsIntegrationTest extends MediaWikiIntegrationTestCa
->with( self::NAV_POPUPS_GADGET_NAME ) ->with( self::NAV_POPUPS_GADGET_NAME )
->willReturn( $gadgetMock ); ->willReturn( $gadgetMock );
$this->executeConflictsWithNavPopupsGadgetSafeCheck( $user, $this->getConfigMock(), $this->executeIsNavPopupsGadgetEnabled(
$gadgetRepoMock, self::GADGET_ENABLED ); $user,
$this->getConfig(),
$gadgetRepoMock,
self::GADGET_ENABLED
);
} }
/** /**
@ -103,8 +102,6 @@ class ReferencePreviewsGadgetsIntegrationTest extends MediaWikiIntegrationTestCa
* @covers ::isNavPopupsGadgetEnabled * @covers ::isNavPopupsGadgetEnabled
*/ */
public function testConflictsWithNavPopupsGadgetWhenGadgetNotExists() { public function testConflictsWithNavPopupsGadgetWhenGadgetNotExists() {
$user = $this->createMock( User::class );
$gadgetRepoMock = $this->createMock( GadgetRepo::class ); $gadgetRepoMock = $this->createMock( GadgetRepo::class );
$gadgetRepoMock->expects( $this->once() ) $gadgetRepoMock->expects( $this->once() )
->method( 'getGadgetIds' ) ->method( 'getGadgetIds' )
@ -114,25 +111,19 @@ class ReferencePreviewsGadgetsIntegrationTest extends MediaWikiIntegrationTestCa
->with( self::NAV_POPUPS_GADGET_NAME ) ->with( self::NAV_POPUPS_GADGET_NAME )
->willThrowException( new InvalidArgumentException() ); ->willThrowException( new InvalidArgumentException() );
$this->executeConflictsWithNavPopupsGadgetSafeCheck( $user, $this->getConfigMock(), $this->executeIsNavPopupsGadgetEnabled(
$gadgetRepoMock, self::GADGET_DISABLED ); $this->createNoOpMock( User::class ),
$this->getConfig(),
$gadgetRepoMock,
self::GADGET_DISABLED
);
} }
/** /**
* @covers ::sanitizeGadgetName * @covers ::sanitizeGadgetName
* @dataProvider provideGadgetNamesWithSanitizedVersion * @dataProvider provideGadgetNamesWithSanitizedVersion
*/ */
public function testConflictsWithNavPopupsGadgetNameSanitization( $name, $sanitized ) { public function testConflictsWithNavPopupsGadgetNameSanitization( string $gadgetName, string $sanitized ) {
$user = $this->createMock( User::class );
$configMock = $this->createMock( Config::class );
$configMock->expects( $this->atLeastOnce() )
->method( 'get' )
->willReturnMap( [
[ ReferencePreviewsGadgetsIntegration::CONFIG_NAVIGATION_POPUPS_NAME, $name ],
[ ReferencePreviewsGadgetsIntegration::CONFIG_REFERENCE_TOOLTIPS_NAME, $name ]
] );
$gadgetMock = $this->createMock( Gadget::class ); $gadgetMock = $this->createMock( Gadget::class );
$gadgetMock->expects( $this->once() ) $gadgetMock->expects( $this->once() )
->method( 'isEnabled' ) ->method( 'isEnabled' )
@ -147,37 +138,33 @@ class ReferencePreviewsGadgetsIntegrationTest extends MediaWikiIntegrationTestCa
->with( $sanitized ) ->with( $sanitized )
->willReturn( $gadgetMock ); ->willReturn( $gadgetMock );
$this->executeConflictsWithNavPopupsGadgetSafeCheck( $user, $configMock, $gadgetRepoMock, $this->executeIsNavPopupsGadgetEnabled(
self::GADGET_ENABLED ); $this->createNoOpMock( User::class ),
$this->getConfig( $gadgetName ),
$gadgetRepoMock,
self::GADGET_ENABLED
);
} }
public static function provideGadgetNamesWithSanitizedVersion() { public static function provideGadgetNamesWithSanitizedVersion() {
return [ yield [ ' Popups ', 'Popups' ];
[ ' Popups ', 'Popups' ], yield [ 'Navigation_popups-API', 'Navigation_popups-API' ];
[ 'Navigation_popups-API', 'Navigation_popups-API' ], yield [ 'Navigation popups ', 'Navigation_popups' ];
[ 'Navigation popups ', 'Navigation_popups' ]
];
} }
/** private function executeIsNavPopupsGadgetEnabled(
* Execute test and restore GadgetRepo
*
* @param User $user
* @param Config $config
* @param GadgetRepo $repoMock
* @param bool $expected
*/
private function executeConflictsWithNavPopupsGadgetSafeCheck(
User $user, User $user,
Config $config, Config $config,
GadgetRepo $repoMock, GadgetRepo $repoMock,
$expected bool $expected
) { ): void {
$this->setService( 'GadgetsRepo', $repoMock ); $this->setService( 'GadgetsRepo', $repoMock );
$integration = new ReferencePreviewsGadgetsIntegration( $config ); $integration = new ReferencePreviewsGadgetsIntegration( $config );
$this->assertSame( $expected, $this->assertSame(
$expected,
$integration->isNavPopupsGadgetEnabled( $user ), $integration->isNavPopupsGadgetEnabled( $user ),
( $expected ? 'A' : 'No' ) . ' conflict is identified.' ); ( $expected ? 'A' : 'No' ) . ' conflict is identified.'
);
} }
} }