2017-01-04 16:33:40 +00:00
|
|
|
<?php
|
2017-06-20 07:19:34 +00:00
|
|
|
/** This file is part of the MediaWiki extension Popups.
|
2020-01-26 19:22:03 +00:00
|
|
|
*
|
|
|
|
* Popups is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Popups is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Popups. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup extensions
|
|
|
|
*/
|
2017-01-04 16:33:40 +00:00
|
|
|
|
2024-01-05 21:41:45 +00:00
|
|
|
use MediaWiki\Config\Config;
|
2022-03-29 19:37:32 +00:00
|
|
|
use MediaWiki\Extension\Gadgets\Gadget;
|
2022-03-06 01:33:08 +00:00
|
|
|
use MediaWiki\Extension\Gadgets\GadgetRepo;
|
2024-01-05 21:41:45 +00:00
|
|
|
use MediaWiki\User\User;
|
2019-10-06 05:43:20 +00:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-01-04 16:33:40 +00:00
|
|
|
use Popups\PopupsGadgetsIntegration;
|
|
|
|
|
|
|
|
/**
|
2019-02-01 08:40:34 +00:00
|
|
|
* @group Popups
|
|
|
|
* @coversDefaultClass \Popups\PopupsGadgetsIntegration
|
|
|
|
*/
|
2023-08-09 22:05:31 +00:00
|
|
|
class PopupsGadgetsIntegrationTest extends MediaWikiIntegrationTestCase {
|
2019-02-26 10:55:57 +00:00
|
|
|
|
2017-01-11 21:52:07 +00:00
|
|
|
/**
|
|
|
|
* Gadget name for testing
|
|
|
|
*/
|
2019-10-10 14:53:30 +00:00
|
|
|
private const NAV_POPUPS_GADGET_NAME = 'navigation-test';
|
2019-02-26 10:55:57 +00:00
|
|
|
|
2017-01-04 16:33:40 +00:00
|
|
|
/**
|
|
|
|
* Helper constants for easier reading
|
|
|
|
*/
|
2019-10-10 14:53:30 +00:00
|
|
|
private const GADGET_ENABLED = true;
|
2019-02-26 10:55:57 +00:00
|
|
|
|
2017-01-04 16:33:40 +00:00
|
|
|
/**
|
|
|
|
* Helper constants for easier reading
|
|
|
|
*/
|
2019-10-10 14:53:30 +00:00
|
|
|
private const GADGET_DISABLED = false;
|
2017-01-04 16:33:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if Gadgets extension is available
|
|
|
|
*/
|
|
|
|
private function checkRequiredDependencies() {
|
|
|
|
if ( !ExtensionRegistry::getInstance()->isLoaded( 'Gadgets' ) ) {
|
|
|
|
$this->markTestSkipped( 'Skipped as Gadgets extension is not available' );
|
|
|
|
}
|
|
|
|
}
|
2017-01-11 21:52:07 +00:00
|
|
|
|
2017-01-04 16:33:40 +00:00
|
|
|
/**
|
|
|
|
* @param bool $gadgetsEnabled
|
2019-10-06 05:43:20 +00:00
|
|
|
* @return MockObject|ExtensionRegistry
|
2017-01-04 16:33:40 +00:00
|
|
|
*/
|
|
|
|
private function getExtensionRegistryMock( $gadgetsEnabled ) {
|
2019-10-10 15:15:31 +00:00
|
|
|
$mock = $this->createMock( ExtensionRegistry::class );
|
2022-09-29 12:41:35 +00:00
|
|
|
$mock->method( 'isLoaded' )
|
2017-01-04 16:33:40 +00:00
|
|
|
->with( 'Gadgets' )
|
|
|
|
->willReturn( $gadgetsEnabled );
|
|
|
|
return $mock;
|
|
|
|
}
|
2017-01-11 21:52:07 +00:00
|
|
|
|
|
|
|
/**
|
2019-10-06 05:43:20 +00:00
|
|
|
* @return MockObject|Config
|
2017-01-11 21:52:07 +00:00
|
|
|
*/
|
|
|
|
private function getConfigMock() {
|
2019-10-10 15:15:31 +00:00
|
|
|
$mock = $this->createMock( Config::class );
|
2020-11-03 12:28:33 +00:00
|
|
|
$mock->expects( $this->atLeastOnce() )
|
2017-01-11 21:52:07 +00:00
|
|
|
->method( 'get' )
|
2023-11-27 02:13:29 +00:00
|
|
|
->willReturnMap( [
|
|
|
|
[ PopupsGadgetsIntegration::CONFIG_NAVIGATION_POPUPS_NAME, self::NAV_POPUPS_GADGET_NAME ],
|
|
|
|
[ PopupsGadgetsIntegration::CONFIG_REFERENCE_TOOLTIPS_NAME, self::NAV_POPUPS_GADGET_NAME ],
|
|
|
|
] );
|
2017-01-11 21:52:07 +00:00
|
|
|
return $mock;
|
|
|
|
}
|
|
|
|
|
2017-01-04 16:33:40 +00:00
|
|
|
/**
|
|
|
|
* @covers ::conflictsWithNavPopupsGadget
|
|
|
|
* @covers ::isGadgetExtensionEnabled
|
|
|
|
* @covers ::__construct
|
2018-12-18 16:05:33 +00:00
|
|
|
* @covers ::sanitizeGadgetName
|
2017-01-04 16:33:40 +00:00
|
|
|
*/
|
|
|
|
public function testConflictsWithNavPopupsGadgetIfGadgetsExtensionIsNotLoaded() {
|
2019-12-17 15:20:27 +00:00
|
|
|
$user = $this->createMock( User::class );
|
2017-01-11 21:52:07 +00:00
|
|
|
$integration = new PopupsGadgetsIntegration( $this->getConfigMock(),
|
|
|
|
$this->getExtensionRegistryMock( false ) );
|
2019-10-04 08:31:58 +00:00
|
|
|
$this->assertFalse(
|
2018-05-08 19:48:17 +00:00
|
|
|
$integration->conflictsWithNavPopupsGadget( $user ),
|
|
|
|
'No conflict is identified.' );
|
2017-01-04 16:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::conflictsWithNavPopupsGadget
|
|
|
|
*/
|
|
|
|
public function testConflictsWithNavPopupsGadgetIfGadgetNotExists() {
|
|
|
|
$this->checkRequiredDependencies();
|
|
|
|
|
2019-12-17 15:20:27 +00:00
|
|
|
$user = $this->createMock( User::class );
|
2017-01-04 16:33:40 +00:00
|
|
|
|
2019-10-10 15:15:31 +00:00
|
|
|
$gadgetRepoMock = $this->createMock( GadgetRepo::class );
|
2017-01-04 16:33:40 +00:00
|
|
|
$gadgetRepoMock->expects( $this->once() )
|
|
|
|
->method( 'getGadgetIds' )
|
|
|
|
->willReturn( [] );
|
|
|
|
|
2017-04-10 22:27:19 +00:00
|
|
|
$this->executeConflictsWithNavPopupsGadgetSafeCheck( $user, $this->getConfigMock(),
|
|
|
|
$gadgetRepoMock, self::GADGET_DISABLED );
|
2017-01-04 16:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::conflictsWithNavPopupsGadget
|
|
|
|
*/
|
|
|
|
public function testConflictsWithNavPopupsGadgetIfGadgetExists() {
|
|
|
|
$this->checkRequiredDependencies();
|
|
|
|
|
2019-12-17 15:20:27 +00:00
|
|
|
$user = $this->createMock( User::class );
|
2017-01-04 16:33:40 +00:00
|
|
|
|
2019-10-10 15:15:31 +00:00
|
|
|
$gadgetMock = $this->createMock( Gadget::class );
|
2017-01-04 16:33:40 +00:00
|
|
|
$gadgetMock->expects( $this->once() )
|
|
|
|
->method( 'isEnabled' )
|
|
|
|
->with( $user )
|
|
|
|
->willReturn( self::GADGET_ENABLED );
|
|
|
|
|
2019-10-10 15:15:31 +00:00
|
|
|
$gadgetRepoMock = $this->createMock( GadgetRepo::class );
|
2017-01-04 16:33:40 +00:00
|
|
|
$gadgetRepoMock->expects( $this->once() )
|
2017-04-10 22:27:19 +00:00
|
|
|
->method( 'getGadgetIds' )
|
2017-01-11 21:52:07 +00:00
|
|
|
->willReturn( [ self::NAV_POPUPS_GADGET_NAME ] );
|
2017-01-04 16:33:40 +00:00
|
|
|
$gadgetRepoMock->expects( $this->once() )
|
|
|
|
->method( 'getGadget' )
|
2017-01-11 21:52:07 +00:00
|
|
|
->with( self::NAV_POPUPS_GADGET_NAME )
|
2017-01-04 16:33:40 +00:00
|
|
|
->willReturn( $gadgetMock );
|
|
|
|
|
2017-04-10 22:27:19 +00:00
|
|
|
$this->executeConflictsWithNavPopupsGadgetSafeCheck( $user, $this->getConfigMock(),
|
|
|
|
$gadgetRepoMock, self::GADGET_ENABLED );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the edge case when GadgetsRepo::getGadget throws an exception
|
|
|
|
* @covers ::conflictsWithNavPopupsGadget
|
|
|
|
*/
|
|
|
|
public function testConflictsWithNavPopupsGadgetWhenGadgetNotExists() {
|
2019-12-17 15:20:27 +00:00
|
|
|
$this->checkRequiredDependencies();
|
2017-04-10 22:27:19 +00:00
|
|
|
|
2019-12-17 15:20:27 +00:00
|
|
|
$user = $this->createMock( User::class );
|
2017-04-10 22:27:19 +00:00
|
|
|
|
2019-12-17 15:20:27 +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() );
|
2017-04-10 22:27:19 +00:00
|
|
|
|
2019-12-17 15:20:27 +00:00
|
|
|
$this->executeConflictsWithNavPopupsGadgetSafeCheck( $user, $this->getConfigMock(),
|
|
|
|
$gadgetRepoMock, self::GADGET_DISABLED );
|
2017-04-10 22:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::sanitizeGadgetName
|
|
|
|
* @dataProvider provideGadgetNamesWithSanitizedVersion
|
|
|
|
*/
|
|
|
|
public function testConflictsWithNavPopupsGadgetNameSanitization( $name, $sanitized ) {
|
|
|
|
$this->checkRequiredDependencies();
|
|
|
|
|
2019-12-17 15:20:27 +00:00
|
|
|
$user = $this->createMock( User::class );
|
2017-04-10 22:27:19 +00:00
|
|
|
|
2019-10-10 15:15:31 +00:00
|
|
|
$configMock = $this->createMock( Config::class );
|
2020-11-03 12:28:33 +00:00
|
|
|
$configMock->expects( $this->atLeastOnce() )
|
2017-04-10 22:27:19 +00:00
|
|
|
->method( 'get' )
|
2023-11-27 02:13:29 +00:00
|
|
|
->willReturnMap( [
|
|
|
|
[ PopupsGadgetsIntegration::CONFIG_NAVIGATION_POPUPS_NAME, $name ],
|
|
|
|
[ PopupsGadgetsIntegration::CONFIG_REFERENCE_TOOLTIPS_NAME, $name ]
|
|
|
|
] );
|
2017-04-10 22:27:19 +00:00
|
|
|
|
2019-10-10 15:15:31 +00:00
|
|
|
$gadgetMock = $this->createMock( Gadget::class );
|
2017-04-10 22:27:19 +00:00
|
|
|
$gadgetMock->expects( $this->once() )
|
|
|
|
->method( 'isEnabled' )
|
|
|
|
->willReturn( self::GADGET_ENABLED );
|
|
|
|
|
2019-10-10 15:15:31 +00:00
|
|
|
$gadgetRepoMock = $this->createMock( GadgetRepo::class );
|
2017-04-10 22:27:19 +00:00
|
|
|
$gadgetRepoMock->expects( $this->once() )
|
|
|
|
->method( 'getGadgetIds' )
|
|
|
|
->willReturn( [ $sanitized ] );
|
|
|
|
$gadgetRepoMock->expects( $this->once() )
|
|
|
|
->method( 'getGadget' )
|
|
|
|
->with( $sanitized )
|
|
|
|
->willReturn( $gadgetMock );
|
|
|
|
|
|
|
|
$this->executeConflictsWithNavPopupsGadgetSafeCheck( $user, $configMock, $gadgetRepoMock,
|
2017-01-04 16:33:40 +00:00
|
|
|
self::GADGET_ENABLED );
|
|
|
|
}
|
|
|
|
|
2023-05-20 11:56:14 +00:00
|
|
|
public static function provideGadgetNamesWithSanitizedVersion() {
|
2017-04-10 22:27:19 +00:00
|
|
|
return [
|
|
|
|
[ ' Popups ', 'Popups' ],
|
|
|
|
[ 'Navigation_popups-API', 'Navigation_popups-API' ],
|
|
|
|
[ 'Navigation popups ', 'Navigation_popups' ]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute test and restore GadgetRepo
|
2017-01-04 16:33:40 +00:00
|
|
|
*
|
2019-02-01 08:40:34 +00:00
|
|
|
* @param User $user
|
|
|
|
* @param Config $config
|
|
|
|
* @param GadgetRepo $repoMock
|
|
|
|
* @param bool $expected
|
2017-01-04 16:33:40 +00:00
|
|
|
*/
|
2019-02-01 08:40:34 +00:00
|
|
|
private function executeConflictsWithNavPopupsGadgetSafeCheck(
|
|
|
|
User $user,
|
|
|
|
Config $config,
|
|
|
|
GadgetRepo $repoMock,
|
|
|
|
$expected
|
|
|
|
) {
|
2017-01-04 16:33:40 +00:00
|
|
|
$origGadgetsRepo = GadgetRepo::singleton();
|
|
|
|
GadgetRepo::setSingleton( $repoMock );
|
|
|
|
|
2017-04-10 22:27:19 +00:00
|
|
|
$integration = new PopupsGadgetsIntegration( $config,
|
2017-01-11 21:52:07 +00:00
|
|
|
$this->getExtensionRegistryMock( true ) );
|
2019-10-04 08:31:58 +00:00
|
|
|
$this->assertSame( $expected,
|
2018-05-08 19:48:17 +00:00
|
|
|
$integration->conflictsWithNavPopupsGadget( $user ),
|
2018-10-05 22:46:27 +00:00
|
|
|
( $expected ? 'A' : 'No' ) . ' conflict is identified.' );
|
2017-01-04 16:33:40 +00:00
|
|
|
|
|
|
|
GadgetRepo::setSingleton( $origGadgetsRepo );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|