mediawiki-skins-Vector/tests/phpunit/unit/FeatureManagement/Requirements/UserPreferenceRequirementTest.php
Jon Robson 735dad995b Preferences set to "disabled" return false when isEnabled is called
The font size preference sets a preference to disabled rather than 0.

Rather than save this as 0, it would be useful to store this as a string
given in future it will evolve to have values small, medium, large

Change logic in UserPreferenceRequirement to support non-binary options.

Bug: T346987
Change-Id: I399aa1b1be4a45cab0aa3b8addb482e5af6c5bf3
2023-10-04 00:34:16 +00:00

103 lines
2.1 KiB
PHP

<?php
namespace MediaWiki\Skins\Vector\Tests\Unit\FeatureManagement\Requirements;
use MediaWiki\Skins\Vector\FeatureManagement\Requirements\UserPreferenceRequirement;
use MediaWiki\Title\Title;
use MediaWiki\User\UserOptionsLookup;
use User;
/**
* @group Vector
* @group FeatureManagement
* @coversDefaultClass \MediaWiki\Skins\Vector\FeatureManagement\Requirements\UserPreferenceRequirement
*/
final class UserPreferenceRequirementTest extends \MediaWikiUnitTestCase {
public static function providerTestIsMetRequirement() {
return [
[
// Is option enabled?
1,
// Is title present?
true,
// Expected
true,
'If enabled, returns true'
],
[
// Is option enabled?
0,
// Is title present?
true,
// Expected
false,
'If disabled, returns false'
],
[
// Is option enabled?
'enabled',
// Is title present?
false,
// Expected
false,
'If enabled but title null, returns false'
],
[
'disabled',
// Is title present?
true,
// Expected
false,
'If disabled, returns false'
],
[
'0',
// Is title present?
true,
// Expected
false,
'If disabled, returns false'
],
[
'medium',
// Is title present?
true,
// Expected
true,
'If unrecognized string returns true'
],
];
}
/**
* @covers ::isMet
* @dataProvider providerTestIsMetRequirement
* @param bool $isEnabled
* @param bool $isTitlePresent
* @param bool $expected
* @param string $msg
*/
public function testIsMetRequirement(
$isEnabled,
$isTitlePresent,
$expected,
$msg
) {
$user = $this->createMock( User::class );
$title = $isTitlePresent ? $this->createMock( Title::class ) : null;
$userOptionsLookup = $this->createMock( UserOptionsLookup::class );
$userOptionsLookup->method( 'getOption' )->willReturn( $isEnabled );
$requirement = new UserPreferenceRequirement(
$user,
$userOptionsLookup,
'userOption',
'userRequirement',
$title
);
$this->assertSame( $expected, $requirement->isMet(), $msg );
}
}