Remove more defaults for flag UserOptionsManager::EXCLUDE_DEFAULTS

Use the isValueEqual function from
I572446faa8d40801a9adc3aee4b26d97c18000a1 to remove more bool values,
if the default options and the user option differs in real type.

Bug: T291748
Change-Id: I6e61c11d8aed27b4b559e74849e0056e5eef3638
This commit is contained in:
Umherirrender 2021-09-29 02:10:56 +02:00 committed by Thiemo Kreuz
parent 4954ac1496
commit 6bda73219a
2 changed files with 33 additions and 6 deletions

View file

@ -168,7 +168,14 @@ class UserOptionsManager extends UserOptionsLookup {
}
if ( $flags & self::EXCLUDE_DEFAULTS ) {
$options = array_diff_assoc( $options, $this->defaultOptionsLookup->getDefaultOptions() );
$defaultOptions = $this->defaultOptionsLookup->getDefaultOptions();
foreach ( $options as $option => $value ) {
if ( isset( $defaultOptions[$option] )
&& $this->isValueEqual( $value, $defaultOptions[$option] )
) {
unset( $options[$option] );
}
}
}
return $options;

View file

@ -59,13 +59,33 @@ class UserOptionsManagerTest extends UserOptionsLookupTest {
* @covers MediaWiki\User\UserOptionsManager::getOption
*/
public function testGetOptionsExcludeDefaults() {
$manager = $this->getManager();
$manager->setOption( $this->getAnon( __METHOD__ ), 'new_option', 'new_value' );
$this->assertSame( [
$manager = $this->getManager( [ 'defaults' => [
'null_vs_false' => null,
'null_vs_string' => null,
'false_vs_int' => false,
'false_vs_string' => false,
'int_vs_string' => 0,
'true_vs_int' => true,
'true_vs_string' => true,
] ] );
$user = $this->getAnon( __METHOD__ );
$manager->setOption( $user, 'null_vs_false', false );
$manager->setOption( $user, 'null_vs_string', '' );
$manager->setOption( $user, 'false_vs_int', 0 );
$manager->setOption( $user, 'false_vs_string', '0' );
$manager->setOption( $user, 'int_vs_string', '0' );
$manager->setOption( $user, 'true_vs_int', 1 );
$manager->setOption( $user, 'true_vs_string', '1' );
$manager->setOption( $user, 'new_option', 'new_value' );
$expected = [
// Note that the old, relaxed array_diff-approach considered null equal to false and ""
'null_vs_false' => false,
'null_vs_string' => '',
'language' => 'en',
'variant' => 'en',
'new_option' => 'new_value'
], $manager->getOptions( $this->getAnon( __METHOD__ ), UserOptionsManager::EXCLUDE_DEFAULTS ) );
'new_option' => 'new_value',
];
$this->assertSame( $expected, $manager->getOptions( $user, UserOptionsManager::EXCLUDE_DEFAULTS ) );
}
/**