HTMLLegacySkinVersionField: accept string 'default' value

After I628435a4a, we were asserting a boolean was given because we're
extending HTMLFormField which requires a boolean value. This was safe
because GlobalPrefs would provide a boolean, but that changed with
I594f6297.

We could rework GlobalPrefs once again to ensure only a boolean is
passed in, but since HTMLLegacySkinVersionField already has special
handling around the data types, it seems to make sense to contain the
type transformation in this class.

Simply removing the Assertion is enough to prevent T296068, however
depending on when the global preference was saved (such as since MW
1.38.0-wmf.9 but before wmf.10), it's possible either a bool or a string
was saved, hence we check for both to ensure correct display.

Bug: T296068
Change-Id: If10b948617d2bb8346475f207fe425fb768cb987
This commit is contained in:
MusikAnimal 2021-11-23 00:11:22 -05:00
parent 768a07ec6c
commit 5207a15b91
2 changed files with 21 additions and 12 deletions

View File

@ -3,7 +3,6 @@
namespace Vector\HTMLForm\Fields;
use Vector\Constants;
use Wikimedia\Assert\Assert;
/**
* The field on Special:Preferences (and Special:GlobalPreferences) that allows the user to
@ -34,10 +33,13 @@ final class HTMLLegacySkinVersionField extends \HTMLFormField {
* @inheritDoc
*/
public function __construct( $params ) {
Assert::precondition(
is_bool( $params['default'] ),
'The "default" param must be a boolean.'
);
/**
* HTMLCheckField must be given a boolean as the 'default' value.
* Since MW 1.38.0-wmf.9, we could be given a boolean or a string.
* @see T296068
*/
$params['default'] = $params['default'] === true ||
$params['default'] === Constants::SKIN_VERSION_LEGACY;
parent::__construct( $params );

View File

@ -31,20 +31,27 @@ use Vector\HTMLForm\Fields\HTMLLegacySkinVersionField;
class HTMLLegacySkinVersionFieldTest extends \MediaWikiIntegrationTestCase {
public function provideDefault() {
yield [ 'true' ];
yield [ 1 ];
return [
[ false, '0' ],
[ false, false ],
[ true, '1' ],
[ true, true ],
];
}
/**
* @dataProvider provideDefault
* @covers ::__construct
*/
public function testConstructValidatesDefault( $default ) {
$this->expectException( \Wikimedia\Assert\PreconditionException::class );
new HTMLLegacySkinVersionField( [
'default' => $default
public function testConstructValidatesDefault( $expected, $default ) {
$field = new HTMLLegacySkinVersionField( [
'default' => $default,
'fieldname' => 'VectorSkinVersion',
] );
$this->assertSame(
$expected,
$field->getDefault()
);
}
public function provideGetInput() {