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