mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Popups
synced 2024-11-30 18:34:40 +00:00
fe32c48268
This was motivated by two things: 1. Notice the bad class name in UserPreferencesChangeHandlerTest. This was possible because the default mock builder does not check if a class exists, *and* there was no type hint in place. createMock() *does* check if a class exists. 2. We are free to use createMock() from PHPUnit 5+ now. It's not only shorter but more strict and reliable. Change-Id: I301a171d587026eab0a62575ab2fdbfd7733c661
228 lines
6.6 KiB
PHP
228 lines
6.6 KiB
PHP
<?php
|
|
/** This file is part of the MediaWiki extension Popups.
|
|
*
|
|
* 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
|
|
*/
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
use Popups\PopupsGadgetsIntegration;
|
|
|
|
/**
|
|
* @group Popups
|
|
* @coversDefaultClass \Popups\PopupsGadgetsIntegration
|
|
*/
|
|
class PopupsGadgetsIntegrationTest extends MediaWikiTestCase {
|
|
|
|
/**
|
|
* Gadget name for testing
|
|
*/
|
|
const NAV_POPUPS_GADGET_NAME = 'navigation-test';
|
|
|
|
/**
|
|
* Helper constants for easier reading
|
|
*/
|
|
const GADGET_ENABLED = true;
|
|
|
|
/**
|
|
* Helper constants for easier reading
|
|
*/
|
|
const GADGET_DISABLED = false;
|
|
|
|
/**
|
|
* Checks if Gadgets extension is available
|
|
*/
|
|
private function checkRequiredDependencies() {
|
|
if ( !ExtensionRegistry::getInstance()->isLoaded( 'Gadgets' ) ) {
|
|
$this->markTestSkipped( 'Skipped as Gadgets extension is not available' );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param bool $gadgetsEnabled
|
|
* @return MockObject|ExtensionRegistry
|
|
*/
|
|
private function getExtensionRegistryMock( $gadgetsEnabled ) {
|
|
$mock = $this->createMock( ExtensionRegistry::class );
|
|
$mock->expects( $this->any() )
|
|
->method( 'isLoaded' )
|
|
->with( 'Gadgets' )
|
|
->willReturn( $gadgetsEnabled );
|
|
return $mock;
|
|
}
|
|
|
|
/**
|
|
* @return MockObject|Config
|
|
*/
|
|
private function getConfigMock() {
|
|
$mock = $this->createMock( Config::class );
|
|
$mock->expects( $this->once() )
|
|
->method( 'get' )
|
|
->with( PopupsGadgetsIntegration::CONFIG_NAVIGATION_POPUPS_NAME )
|
|
->willReturn( self::NAV_POPUPS_GADGET_NAME );
|
|
return $mock;
|
|
}
|
|
|
|
/**
|
|
* @covers ::conflictsWithNavPopupsGadget
|
|
* @covers ::isGadgetExtensionEnabled
|
|
* @covers ::__construct
|
|
* @covers ::sanitizeGadgetName
|
|
*/
|
|
public function testConflictsWithNavPopupsGadgetIfGadgetsExtensionIsNotLoaded() {
|
|
$user = $this->getTestUser()->getUser();
|
|
$integration = new PopupsGadgetsIntegration( $this->getConfigMock(),
|
|
$this->getExtensionRegistryMock( false ) );
|
|
$this->assertFalse(
|
|
$integration->conflictsWithNavPopupsGadget( $user ),
|
|
'No conflict is identified.' );
|
|
}
|
|
|
|
/**
|
|
* @covers ::conflictsWithNavPopupsGadget
|
|
*/
|
|
public function testConflictsWithNavPopupsGadgetIfGadgetNotExists() {
|
|
$this->checkRequiredDependencies();
|
|
|
|
$user = $this->getTestUser()->getUser();
|
|
|
|
$gadgetRepoMock = $this->createMock( GadgetRepo::class );
|
|
$gadgetRepoMock->expects( $this->once() )
|
|
->method( 'getGadgetIds' )
|
|
->willReturn( [] );
|
|
|
|
$this->executeConflictsWithNavPopupsGadgetSafeCheck( $user, $this->getConfigMock(),
|
|
$gadgetRepoMock, self::GADGET_DISABLED );
|
|
}
|
|
|
|
/**
|
|
* @covers ::conflictsWithNavPopupsGadget
|
|
*/
|
|
public function testConflictsWithNavPopupsGadgetIfGadgetExists() {
|
|
$this->checkRequiredDependencies();
|
|
|
|
$user = $this->getTestUser()->getUser();
|
|
|
|
$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 );
|
|
|
|
$this->executeConflictsWithNavPopupsGadgetSafeCheck( $user, $this->getConfigMock(),
|
|
$gadgetRepoMock, self::GADGET_ENABLED );
|
|
}
|
|
|
|
/**
|
|
* Test the edge case when GadgetsRepo::getGadget throws an exception
|
|
* @covers ::conflictsWithNavPopupsGadget
|
|
*/
|
|
public function testConflictsWithNavPopupsGadgetWhenGadgetNotExists() {
|
|
$this->checkRequiredDependencies();
|
|
|
|
$user = $this->getTestUser()->getUser();
|
|
|
|
$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() );
|
|
|
|
$this->executeConflictsWithNavPopupsGadgetSafeCheck( $user, $this->getConfigMock(),
|
|
$gadgetRepoMock, self::GADGET_DISABLED );
|
|
}
|
|
|
|
/**
|
|
* @covers ::sanitizeGadgetName
|
|
* @dataProvider provideGadgetNamesWithSanitizedVersion
|
|
*/
|
|
public function testConflictsWithNavPopupsGadgetNameSanitization( $name, $sanitized ) {
|
|
$this->checkRequiredDependencies();
|
|
|
|
$user = $this->getTestUser()->getUser();
|
|
|
|
$configMock = $this->createMock( Config::class );
|
|
$configMock->expects( $this->once() )
|
|
->method( 'get' )
|
|
->with( PopupsGadgetsIntegration::CONFIG_NAVIGATION_POPUPS_NAME )
|
|
->willReturn( $name );
|
|
|
|
$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 );
|
|
|
|
$this->executeConflictsWithNavPopupsGadgetSafeCheck( $user, $configMock, $gadgetRepoMock,
|
|
self::GADGET_ENABLED );
|
|
}
|
|
|
|
public function provideGadgetNamesWithSanitizedVersion() {
|
|
return [
|
|
[ ' Popups ', 'Popups' ],
|
|
[ 'Navigation_popups-API', 'Navigation_popups-API' ],
|
|
[ 'Navigation popups ', 'Navigation_popups' ]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Execute test and restore GadgetRepo
|
|
*
|
|
* @param User $user
|
|
* @param Config $config
|
|
* @param GadgetRepo $repoMock
|
|
* @param bool $expected
|
|
*/
|
|
private function executeConflictsWithNavPopupsGadgetSafeCheck(
|
|
User $user,
|
|
Config $config,
|
|
GadgetRepo $repoMock,
|
|
$expected
|
|
) {
|
|
$origGadgetsRepo = GadgetRepo::singleton();
|
|
GadgetRepo::setSingleton( $repoMock );
|
|
|
|
$integration = new PopupsGadgetsIntegration( $config,
|
|
$this->getExtensionRegistryMock( true ) );
|
|
$this->assertSame( $expected,
|
|
$integration->conflictsWithNavPopupsGadget( $user ),
|
|
( $expected ? 'A' : 'No' ) . ' conflict is identified.' );
|
|
|
|
GadgetRepo::setSingleton( $origGadgetsRepo );
|
|
}
|
|
|
|
}
|