2024-04-19 07:42:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Cite\Tests\Integration\ReferencePreviews;
|
|
|
|
|
|
|
|
use Cite\ReferencePreviews\ReferencePreviewsGadgetsIntegration;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use MediaWiki\Config\Config;
|
2024-04-25 15:00:31 +00:00
|
|
|
use MediaWiki\Config\HashConfig;
|
2024-04-19 07:42:41 +00:00
|
|
|
use MediaWiki\Extension\Gadgets\Gadget;
|
|
|
|
use MediaWiki\Extension\Gadgets\GadgetRepo;
|
|
|
|
use MediaWiki\User\User;
|
|
|
|
use MediaWikiIntegrationTestCase;
|
|
|
|
|
|
|
|
/**
|
2024-05-16 12:35:53 +00:00
|
|
|
* @covers \Cite\ReferencePreviews\ReferencePreviewsGadgetsIntegration
|
2024-04-19 07:42:41 +00:00
|
|
|
* @license GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
class ReferencePreviewsGadgetsIntegrationTest extends MediaWikiIntegrationTestCase {
|
|
|
|
/**
|
|
|
|
* Gadget name for testing
|
|
|
|
*/
|
|
|
|
private const NAV_POPUPS_GADGET_NAME = 'navigation-test';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper constants for easier reading
|
|
|
|
*/
|
|
|
|
private const GADGET_ENABLED = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper constants for easier reading
|
|
|
|
*/
|
|
|
|
private const GADGET_DISABLED = false;
|
|
|
|
|
2024-04-25 15:00:31 +00:00
|
|
|
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,
|
|
|
|
] );
|
2024-04-19 07:42:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testConflictsWithNavPopupsGadgetIfGadgetsExtensionIsNotLoaded() {
|
2024-04-25 15:00:31 +00:00
|
|
|
$integration = new ReferencePreviewsGadgetsIntegration( $this->getConfig() );
|
2024-04-19 07:42:41 +00:00
|
|
|
$this->assertFalse(
|
2024-04-25 15:00:31 +00:00
|
|
|
$integration->isNavPopupsGadgetEnabled( $this->createNoOpMock( User::class ) ),
|
|
|
|
'No conflict is identified.'
|
|
|
|
);
|
2024-04-19 07:42:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testConflictsWithNavPopupsGadgetIfGadgetNotExists() {
|
2024-05-16 12:35:53 +00:00
|
|
|
$this->markTestSkippedIfExtensionNotLoaded( 'Gadgets' );
|
|
|
|
|
2024-04-19 07:42:41 +00:00
|
|
|
$gadgetRepoMock = $this->createMock( GadgetRepo::class );
|
|
|
|
$gadgetRepoMock->expects( $this->once() )
|
|
|
|
->method( 'getGadgetIds' )
|
|
|
|
->willReturn( [] );
|
|
|
|
|
2024-04-25 15:00:31 +00:00
|
|
|
$this->executeIsNavPopupsGadgetEnabled(
|
|
|
|
$this->createNoOpMock( User::class ),
|
|
|
|
$this->getConfig(),
|
|
|
|
$gadgetRepoMock,
|
|
|
|
self::GADGET_DISABLED
|
|
|
|
);
|
2024-04-19 07:42:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testConflictsWithNavPopupsGadgetIfGadgetExists() {
|
2024-05-16 12:35:53 +00:00
|
|
|
$this->markTestSkippedIfExtensionNotLoaded( 'Gadgets' );
|
|
|
|
|
2024-04-19 07:42:41 +00:00
|
|
|
$user = $this->createMock( User::class );
|
|
|
|
|
|
|
|
$gadgetMock = $this->createMock( Gadget::class );
|
|
|
|
$gadgetMock->expects( $this->once() )
|
|
|
|
->method( 'isEnabled' )
|
|
|
|
->with( $user )
|
|
|
|
->willReturn( self::GADGET_ENABLED );
|
|
|
|
|
|
|
|
$gadgetRepoMock = $this->createMock( GadgetRepo::class );
|
|
|
|
$gadgetRepoMock->expects( $this->once() )
|
|
|
|
->method( 'getGadgetIds' )
|
|
|
|
->willReturn( [ self::NAV_POPUPS_GADGET_NAME ] );
|
|
|
|
$gadgetRepoMock->expects( $this->once() )
|
|
|
|
->method( 'getGadget' )
|
|
|
|
->with( self::NAV_POPUPS_GADGET_NAME )
|
|
|
|
->willReturn( $gadgetMock );
|
|
|
|
|
2024-04-25 15:00:31 +00:00
|
|
|
$this->executeIsNavPopupsGadgetEnabled(
|
|
|
|
$user,
|
|
|
|
$this->getConfig(),
|
|
|
|
$gadgetRepoMock,
|
|
|
|
self::GADGET_ENABLED
|
|
|
|
);
|
2024-04-19 07:42:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the edge case when GadgetsRepo::getGadget throws an exception
|
|
|
|
*/
|
|
|
|
public function testConflictsWithNavPopupsGadgetWhenGadgetNotExists() {
|
2024-05-16 12:35:53 +00:00
|
|
|
$this->markTestSkippedIfExtensionNotLoaded( 'Gadgets' );
|
|
|
|
|
2024-04-19 07:42:41 +00:00
|
|
|
$gadgetRepoMock = $this->createMock( GadgetRepo::class );
|
|
|
|
$gadgetRepoMock->expects( $this->once() )
|
|
|
|
->method( 'getGadgetIds' )
|
|
|
|
->willReturn( [ self::NAV_POPUPS_GADGET_NAME ] );
|
|
|
|
$gadgetRepoMock->expects( $this->once() )
|
|
|
|
->method( 'getGadget' )
|
|
|
|
->with( self::NAV_POPUPS_GADGET_NAME )
|
|
|
|
->willThrowException( new InvalidArgumentException() );
|
|
|
|
|
2024-04-25 15:00:31 +00:00
|
|
|
$this->executeIsNavPopupsGadgetEnabled(
|
|
|
|
$this->createNoOpMock( User::class ),
|
|
|
|
$this->getConfig(),
|
|
|
|
$gadgetRepoMock,
|
|
|
|
self::GADGET_DISABLED
|
|
|
|
);
|
2024-04-19 07:42:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideGadgetNamesWithSanitizedVersion
|
|
|
|
*/
|
2024-04-25 15:00:31 +00:00
|
|
|
public function testConflictsWithNavPopupsGadgetNameSanitization( string $gadgetName, string $sanitized ) {
|
2024-05-16 12:35:53 +00:00
|
|
|
$this->markTestSkippedIfExtensionNotLoaded( 'Gadgets' );
|
|
|
|
|
2024-04-19 07:42:41 +00:00
|
|
|
$gadgetMock = $this->createMock( Gadget::class );
|
|
|
|
$gadgetMock->expects( $this->once() )
|
|
|
|
->method( 'isEnabled' )
|
|
|
|
->willReturn( self::GADGET_ENABLED );
|
|
|
|
|
|
|
|
$gadgetRepoMock = $this->createMock( GadgetRepo::class );
|
|
|
|
$gadgetRepoMock->expects( $this->once() )
|
|
|
|
->method( 'getGadgetIds' )
|
|
|
|
->willReturn( [ $sanitized ] );
|
|
|
|
$gadgetRepoMock->expects( $this->once() )
|
|
|
|
->method( 'getGadget' )
|
|
|
|
->with( $sanitized )
|
|
|
|
->willReturn( $gadgetMock );
|
|
|
|
|
2024-04-25 15:00:31 +00:00
|
|
|
$this->executeIsNavPopupsGadgetEnabled(
|
|
|
|
$this->createNoOpMock( User::class ),
|
|
|
|
$this->getConfig( $gadgetName ),
|
|
|
|
$gadgetRepoMock,
|
|
|
|
self::GADGET_ENABLED
|
|
|
|
);
|
2024-04-19 07:42:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function provideGadgetNamesWithSanitizedVersion() {
|
2024-04-25 15:00:31 +00:00
|
|
|
yield [ ' Popups ', 'Popups' ];
|
|
|
|
yield [ 'Navigation_popups-API', 'Navigation_popups-API' ];
|
|
|
|
yield [ 'Navigation popups ', 'Navigation_popups' ];
|
2024-04-19 07:42:41 +00:00
|
|
|
}
|
|
|
|
|
2024-04-25 15:00:31 +00:00
|
|
|
private function executeIsNavPopupsGadgetEnabled(
|
2024-04-19 07:42:41 +00:00
|
|
|
User $user,
|
|
|
|
Config $config,
|
|
|
|
GadgetRepo $repoMock,
|
2024-04-25 15:00:31 +00:00
|
|
|
bool $expected
|
|
|
|
): void {
|
2024-04-19 07:42:41 +00:00
|
|
|
$this->setService( 'GadgetsRepo', $repoMock );
|
|
|
|
|
|
|
|
$integration = new ReferencePreviewsGadgetsIntegration( $config );
|
2024-04-25 15:00:31 +00:00
|
|
|
$this->assertSame(
|
|
|
|
$expected,
|
2024-04-19 07:42:41 +00:00
|
|
|
$integration->isNavPopupsGadgetEnabled( $user ),
|
2024-04-25 15:00:31 +00:00
|
|
|
( $expected ? 'A' : 'No' ) . ' conflict is identified.'
|
|
|
|
);
|
2024-04-19 07:42:41 +00:00
|
|
|
}
|
|
|
|
}
|