2020-02-20 16:37:33 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @since 1.35
|
|
|
|
*/
|
|
|
|
|
2022-05-23 07:32:40 +00:00
|
|
|
namespace MediaWiki\Skins\Vector\Tests\Unit\FeatureManagement;
|
2020-02-20 16:37:33 +00:00
|
|
|
|
2022-05-23 07:32:40 +00:00
|
|
|
use MediaWiki\Skins\Vector\FeatureManagement\FeatureManager;
|
2020-02-20 16:37:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group Vector
|
|
|
|
* @group FeatureManagement
|
2022-05-23 07:32:40 +00:00
|
|
|
* @coversDefaultClass \MediaWiki\Skins\Vector\FeatureManagement\FeatureManager
|
2020-02-20 16:37:33 +00:00
|
|
|
*/
|
|
|
|
class FeatureManagerTest extends \MediaWikiUnitTestCase {
|
|
|
|
|
|
|
|
/**
|
2020-04-29 14:13:27 +00:00
|
|
|
* @covers ::registerSimpleRequirement
|
2020-02-28 15:42:05 +00:00
|
|
|
* @covers ::registerRequirement
|
2020-02-20 16:37:33 +00:00
|
|
|
*/
|
2020-04-29 14:13:27 +00:00
|
|
|
public function testRegisterSimpleRequirementThrowsWhenRequirementIsRegisteredTwice() {
|
2020-02-20 16:37:33 +00:00
|
|
|
$this->expectException( \LogicException::class );
|
|
|
|
|
|
|
|
$featureManager = new FeatureManager();
|
2020-04-29 14:13:27 +00:00
|
|
|
$featureManager->registerSimpleRequirement( 'requirementA', true );
|
|
|
|
$featureManager->registerSimpleRequirement( 'requirementA', true );
|
2020-02-20 16:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function provideInvalidFeatureConfig() {
|
|
|
|
return [
|
|
|
|
|
|
|
|
// ::registerFeature( string, int[] ) will throw an exception.
|
|
|
|
[
|
|
|
|
\Wikimedia\Assert\ParameterAssertionException::class,
|
|
|
|
[ 1 ],
|
|
|
|
],
|
|
|
|
|
2020-02-28 15:42:05 +00:00
|
|
|
// The "bar" requirement hasn't been registered.
|
2020-02-20 16:37:33 +00:00
|
|
|
[
|
|
|
|
\InvalidArgumentException::class,
|
|
|
|
[
|
|
|
|
'bar',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideInvalidFeatureConfig
|
|
|
|
* @covers ::registerFeature
|
|
|
|
*/
|
|
|
|
public function testRegisterFeatureValidatesConfig( $expectedExceptionType, $config ) {
|
|
|
|
$this->expectException( $expectedExceptionType );
|
|
|
|
|
|
|
|
$featureManager = new FeatureManager();
|
2020-04-29 14:13:27 +00:00
|
|
|
$featureManager->registerSimpleRequirement( 'requirement', true );
|
2020-02-20 16:37:33 +00:00
|
|
|
$featureManager->registerFeature( 'feature', $config );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-28 15:42:05 +00:00
|
|
|
* @covers ::isRequirementMet
|
2020-02-20 16:37:33 +00:00
|
|
|
*/
|
2020-02-28 15:42:05 +00:00
|
|
|
public function testIsRequirementMet() {
|
2020-02-20 16:37:33 +00:00
|
|
|
$featureManager = new FeatureManager();
|
2020-04-29 14:13:27 +00:00
|
|
|
$featureManager->registerSimpleRequirement( 'enabled', true );
|
|
|
|
$featureManager->registerSimpleRequirement( 'disabled', false );
|
2020-02-20 16:37:33 +00:00
|
|
|
|
2020-02-28 15:42:05 +00:00
|
|
|
$this->assertTrue( $featureManager->isRequirementMet( 'enabled' ) );
|
|
|
|
$this->assertFalse( $featureManager->isRequirementMet( 'disabled' ) );
|
2020-02-20 16:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-28 15:42:05 +00:00
|
|
|
* @covers ::isRequirementMet
|
2020-02-20 16:37:33 +00:00
|
|
|
*/
|
2020-02-28 15:42:05 +00:00
|
|
|
public function testIsRequirementMetThrowsExceptionWhenRequirementIsntRegistered() {
|
2020-02-20 16:37:33 +00:00
|
|
|
$this->expectException( \InvalidArgumentException::class );
|
|
|
|
|
|
|
|
$featureManager = new FeatureManager();
|
2020-02-28 15:42:05 +00:00
|
|
|
$featureManager->isRequirementMet( 'foo' );
|
2020-02-20 16:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::registerFeature
|
|
|
|
*/
|
|
|
|
public function testRegisterFeatureThrowsExceptionWhenFeatureIsRegisteredTwice() {
|
|
|
|
$this->expectException( \LogicException::class );
|
|
|
|
|
|
|
|
$featureManager = new FeatureManager();
|
|
|
|
$featureManager->registerFeature( 'featureA', [] );
|
|
|
|
$featureManager->registerFeature( 'featureA', [] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::isFeatureEnabled
|
|
|
|
*/
|
|
|
|
public function testIsFeatureEnabled() {
|
|
|
|
$featureManager = new FeatureManager();
|
2020-04-29 14:13:27 +00:00
|
|
|
$featureManager->registerSimpleRequirement( 'foo', false );
|
2020-02-20 16:37:33 +00:00
|
|
|
$featureManager->registerFeature( 'requiresFoo', 'foo' );
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
$featureManager->isFeatureEnabled( 'requiresFoo' ),
|
2020-02-28 15:42:05 +00:00
|
|
|
'A feature is disabled when the requirement that it requires is disabled.'
|
2020-02-20 16:37:33 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2020-04-29 14:13:27 +00:00
|
|
|
$featureManager->registerSimpleRequirement( 'bar', true );
|
|
|
|
$featureManager->registerSimpleRequirement( 'baz', true );
|
2020-02-20 16:37:33 +00:00
|
|
|
|
|
|
|
$featureManager->registerFeature( 'requiresFooBar', [ 'foo', 'bar' ] );
|
|
|
|
$featureManager->registerFeature( 'requiresBarBaz', [ 'bar', 'baz' ] );
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
$featureManager->isFeatureEnabled( 'requiresFooBar' ),
|
2020-02-28 15:42:05 +00:00
|
|
|
'A feature is disabled when at least one requirement that it requires is disabled.'
|
2020-02-20 16:37:33 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertTrue( $featureManager->isFeatureEnabled( 'requiresBarBaz' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::isFeatureEnabled
|
|
|
|
*/
|
|
|
|
public function testIsFeatureEnabledThrowsExceptionWhenFeatureIsntRegistered() {
|
|
|
|
$this->expectException( \InvalidArgumentException::class );
|
|
|
|
|
|
|
|
$featureManager = new FeatureManager();
|
|
|
|
$featureManager->isFeatureEnabled( 'foo' );
|
|
|
|
}
|
|
|
|
}
|