mirror of
https://gerrit.wikimedia.org/r/mediawiki/skins/Vector.git
synced 2024-11-29 01:45:00 +00:00
709772fa12
de76ab5
added the config,
`$wgVectorDefaultSkinVersionForExistingAccounts`. Its usage in
`Hooks::onUserGetDefaultOptions()` was invoked not only for existing
accounts but anonymous users _as well._ This is a bug, due to my own
misconceptions about the hook, that went against both the config's name
and its documentation.
Unfortunately, user sessions are unavailable in
`Hooks::onUserGetDefaultOptions()` so it does not seem to be possible to
determine whether the active user is an anonymous or existing account.
This patch drops the hook and centralizes all version determination
logic in SkinVersionLookup::getVersion(). SkinVersionLookup requires a
the active User object and can make the anonymous / existing account
determination by checking login state.
The issued was identified while responding to review feedback given by
@polishdeveloper / @pmiazga in
I52d80942b4270c008d4e45050589ed9220255a50.
Bug: T251415
Change-Id: I7982b4c34283ba81d0232ee6f501c44cf0a74b98
275 lines
7 KiB
PHP
275 lines
7 KiB
PHP
<?php
|
|
/*
|
|
* @file
|
|
* @ingroup skins
|
|
*/
|
|
|
|
use Vector\Hooks;
|
|
|
|
const SKIN_PREFS_SECTION = 'rendering/skin/skin-prefs';
|
|
|
|
/**
|
|
* Integration tests for Vector Hooks.
|
|
*
|
|
* @group Vector
|
|
* @coversDefaultClass \Vector\Hooks
|
|
*/
|
|
class VectorHooksTest extends \MediaWikiTestCase {
|
|
/**
|
|
* @covers ::onGetPreferences
|
|
*/
|
|
public function testOnGetPreferencesShowPreferencesDisabled() {
|
|
$config = new HashConfig( [
|
|
'VectorShowSkinPreferences' => false,
|
|
] );
|
|
$this->setService( 'Vector.Config', $config );
|
|
|
|
$prefs = [];
|
|
Hooks::onGetPreferences( $this->getTestUser()->getUser(), $prefs );
|
|
$this->assertSame( $prefs, [], 'No preferences are added.' );
|
|
}
|
|
|
|
/**
|
|
* @covers ::onGetPreferences
|
|
*/
|
|
public function testOnGetPreferencesShowPreferencesEnabledSkinSectionFoundLegacy() {
|
|
$config = new HashConfig( [
|
|
'VectorShowSkinPreferences' => true,
|
|
// '1' is Legacy.
|
|
'VectorDefaultSkinVersionForExistingAccounts' => '1',
|
|
] );
|
|
$this->setService( 'Vector.Config', $config );
|
|
|
|
$prefs = [
|
|
'foo' => [],
|
|
'skin' => [],
|
|
'bar' => []
|
|
];
|
|
Hooks::onGetPreferences( $this->getTestUser()->getUser(), $prefs );
|
|
$this->assertSame(
|
|
$prefs,
|
|
[
|
|
'foo' => [],
|
|
'skin' => [],
|
|
'VectorSkinVersion' => [
|
|
'type' => 'toggle',
|
|
'label-message' => 'prefs-vector-enable-vector-1-label',
|
|
'help-message' => 'prefs-vector-enable-vector-1-help',
|
|
'section' => SKIN_PREFS_SECTION,
|
|
// '1' is enabled which means Legacy.
|
|
'default' => '1',
|
|
'hide-if' => [ '!==', 'wpskin', 'vector' ]
|
|
],
|
|
'bar' => []
|
|
],
|
|
'Preferences are inserted directly after skin.'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @covers ::onGetPreferences
|
|
*/
|
|
public function testOnGetPreferencesShowPreferencesEnabledSkinSectionMissingLegacy() {
|
|
$config = new HashConfig( [
|
|
'VectorShowSkinPreferences' => true,
|
|
// '1' is Legacy.
|
|
'VectorDefaultSkinVersionForExistingAccounts' => '1',
|
|
] );
|
|
$this->setService( 'Vector.Config', $config );
|
|
|
|
$prefs = [
|
|
'foo' => [],
|
|
'bar' => []
|
|
];
|
|
Hooks::onGetPreferences( $this->getTestUser()->getUser(), $prefs );
|
|
$this->assertSame(
|
|
$prefs,
|
|
[
|
|
'foo' => [],
|
|
'bar' => [],
|
|
'VectorSkinVersion' => [
|
|
'type' => 'toggle',
|
|
'label-message' => 'prefs-vector-enable-vector-1-label',
|
|
'help-message' => 'prefs-vector-enable-vector-1-help',
|
|
'section' => SKIN_PREFS_SECTION,
|
|
// '1' is enabled which means Legacy.
|
|
'default' => '1',
|
|
'hide-if' => [ '!==', 'wpskin', 'vector' ]
|
|
],
|
|
],
|
|
'Preferences are appended.'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @covers ::onGetPreferences
|
|
*/
|
|
public function testOnGetPreferencesShowPreferencesEnabledSkinSectionMissingLatest() {
|
|
$config = new HashConfig( [
|
|
'VectorShowSkinPreferences' => true,
|
|
// '2' is latest.
|
|
'VectorDefaultSkinVersionForExistingAccounts' => '2',
|
|
] );
|
|
$this->setService( 'Vector.Config', $config );
|
|
|
|
$prefs = [
|
|
'foo' => [],
|
|
'bar' => [],
|
|
];
|
|
Hooks::onGetPreferences( $this->getTestUser()->getUser(), $prefs );
|
|
$this->assertSame(
|
|
$prefs,
|
|
[
|
|
'foo' => [],
|
|
'bar' => [],
|
|
'VectorSkinVersion' => [
|
|
'type' => 'toggle',
|
|
'label-message' => 'prefs-vector-enable-vector-1-label',
|
|
'help-message' => 'prefs-vector-enable-vector-1-help',
|
|
'section' => SKIN_PREFS_SECTION,
|
|
// '0' is disabled (which means latest).
|
|
'default' => '0',
|
|
'hide-if' => [ '!==', 'wpskin', 'vector' ]
|
|
],
|
|
],
|
|
'Legacy skin version is disabled.'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @covers ::onPreferencesFormPreSave
|
|
*/
|
|
public function testOnPreferencesFormPreSaveVectorEnabledLegacyNewPreference() {
|
|
$formData = [
|
|
'skin' => 'vector',
|
|
// True is Legacy.
|
|
'VectorSkinVersion' => true,
|
|
];
|
|
$form = $this->createMock( HTMLForm::class );
|
|
$user = $this->createMock( \User::class );
|
|
$user->expects( $this->once() )
|
|
->method( 'setOption' )
|
|
// '1' is Legacy.
|
|
->with( 'VectorSkinVersion', '1' );
|
|
$result = true;
|
|
$oldPreferences = [];
|
|
|
|
Hooks::onPreferencesFormPreSave( $formData, $form, $user, $result, $oldPreferences );
|
|
}
|
|
|
|
/**
|
|
* @covers ::onPreferencesFormPreSave
|
|
*/
|
|
public function testOnPreferencesFormPreSaveVectorEnabledLatestNewPreference() {
|
|
$formData = [
|
|
'skin' => 'vector',
|
|
// False is latest.
|
|
'VectorSkinVersion' => false,
|
|
];
|
|
$form = $this->createMock( HTMLForm::class );
|
|
$user = $this->createMock( \User::class );
|
|
$user->expects( $this->once() )
|
|
->method( 'setOption' )
|
|
// '2' is latest.
|
|
->with( 'VectorSkinVersion', '2' );
|
|
$result = true;
|
|
$oldPreferences = [];
|
|
|
|
Hooks::onPreferencesFormPreSave( $formData, $form, $user, $result, $oldPreferences );
|
|
}
|
|
|
|
/**
|
|
* @covers ::onPreferencesFormPreSave
|
|
*/
|
|
public function testOnPreferencesFormPreSaveVectorEnabledNoNewPreference() {
|
|
$formData = [
|
|
'skin' => 'vector',
|
|
];
|
|
$form = $this->createMock( HTMLForm::class );
|
|
$user = $this->createMock( \User::class );
|
|
$user->expects( $this->never() )
|
|
->method( 'setOption' );
|
|
$result = true;
|
|
$oldPreferences = [];
|
|
|
|
Hooks::onPreferencesFormPreSave( $formData, $form, $user, $result, $oldPreferences );
|
|
}
|
|
|
|
/**
|
|
* @covers ::onPreferencesFormPreSave
|
|
*/
|
|
public function testOnPreferencesFormPreSaveVectorDisabledNoOldPreference() {
|
|
$formData = [
|
|
// False is latest.
|
|
'VectorSkinVersion' => false,
|
|
];
|
|
$form = $this->createMock( HTMLForm::class );
|
|
$user = $this->createMock( \User::class );
|
|
$user->expects( $this->never() )
|
|
->method( 'setOption' );
|
|
$result = true;
|
|
$oldPreferences = [];
|
|
|
|
Hooks::onPreferencesFormPreSave( $formData, $form, $user, $result, $oldPreferences );
|
|
}
|
|
|
|
/**
|
|
* @covers ::onPreferencesFormPreSave
|
|
*/
|
|
public function testOnPreferencesFormPreSaveVectorDisabledOldPreference() {
|
|
$formData = [
|
|
// False is latest.
|
|
'VectorSkinVersion' => false,
|
|
];
|
|
$form = $this->createMock( HTMLForm::class );
|
|
$user = $this->createMock( \User::class );
|
|
$user->expects( $this->once() )
|
|
->method( 'setOption' )
|
|
->with( 'VectorSkinVersion', 'old' );
|
|
$result = true;
|
|
$oldPreferences = [
|
|
'VectorSkinVersion' => 'old',
|
|
];
|
|
|
|
Hooks::onPreferencesFormPreSave( $formData, $form, $user, $result, $oldPreferences );
|
|
}
|
|
|
|
/**
|
|
* @covers ::onLocalUserCreated
|
|
*/
|
|
public function testOnLocalUserCreatedLegacy() {
|
|
$config = new HashConfig( [
|
|
// '1' is Legacy.
|
|
'VectorDefaultSkinVersionForNewAccounts' => '1',
|
|
] );
|
|
$this->setService( 'Vector.Config', $config );
|
|
|
|
$user = $this->createMock( \User::class );
|
|
$user->expects( $this->once() )
|
|
->method( 'setOption' )
|
|
// '1' is Legacy.
|
|
->with( 'VectorSkinVersion', '1' );
|
|
$isAutoCreated = false;
|
|
Hooks::onLocalUserCreated( $user, $isAutoCreated );
|
|
}
|
|
|
|
/**
|
|
* @covers ::onLocalUserCreated
|
|
*/
|
|
public function testOnLocalUserCreatedLatest() {
|
|
$config = new HashConfig( [
|
|
// '2' is latest.
|
|
'VectorDefaultSkinVersionForNewAccounts' => '2',
|
|
] );
|
|
$this->setService( 'Vector.Config', $config );
|
|
|
|
$user = $this->createMock( \User::class );
|
|
$user->expects( $this->once() )
|
|
->method( 'setOption' )
|
|
// '2' is latest.
|
|
->with( 'VectorSkinVersion', '2' );
|
|
$isAutoCreated = false;
|
|
Hooks::onLocalUserCreated( $user, $isAutoCreated );
|
|
}
|
|
}
|