2017-11-07 03:10:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
2021-05-29 01:32:24 +00:00
|
|
|
use MediaWiki\Auth\AuthManager;
|
|
|
|
|
use MediaWiki\Config\ServiceOptions;
|
|
|
|
|
use MediaWiki\HookContainer\HookContainer;
|
2021-06-04 23:24:44 +00:00
|
|
|
use MediaWiki\Languages\LanguageConverterFactory;
|
2021-05-29 01:32:24 +00:00
|
|
|
use MediaWiki\Languages\LanguageNameUtils;
|
|
|
|
|
use MediaWiki\Linker\LinkRenderer;
|
2022-08-01 19:14:41 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2017-11-07 03:10:14 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2019-08-22 22:53:05 +00:00
|
|
|
use MediaWiki\Permissions\PermissionManager;
|
2017-11-07 03:10:14 +00:00
|
|
|
use MediaWiki\Preferences\DefaultPreferencesFactory;
|
2022-04-07 23:52:05 +00:00
|
|
|
use MediaWiki\Preferences\SignatureValidatorFactory;
|
2021-12-06 09:33:43 +00:00
|
|
|
use MediaWiki\Session\SessionId;
|
|
|
|
|
use MediaWiki\Session\TestUtils;
|
2021-05-04 23:24:35 +00:00
|
|
|
use MediaWiki\Tests\Unit\DummyServicesTrait;
|
2021-05-29 01:32:24 +00:00
|
|
|
use MediaWiki\User\UserGroupManager;
|
|
|
|
|
use MediaWiki\User\UserIdentity;
|
2021-04-12 14:45:26 +00:00
|
|
|
use MediaWiki\User\UserOptionsLookup;
|
2021-06-05 03:45:14 +00:00
|
|
|
use MediaWiki\User\UserOptionsManager;
|
2021-12-06 09:33:43 +00:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-11-07 03:10:14 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @group Preferences
|
2020-02-04 12:42:03 +00:00
|
|
|
* @coversDefaultClass MediaWiki\Preferences\DefaultPreferencesFactory
|
2017-11-07 03:10:14 +00:00
|
|
|
*/
|
2020-06-30 15:09:24 +00:00
|
|
|
class DefaultPreferencesFactoryTest extends \MediaWikiIntegrationTestCase {
|
2021-05-04 23:24:35 +00:00
|
|
|
use DummyServicesTrait;
|
2019-08-14 12:58:53 +00:00
|
|
|
use TestAllServiceOptionsUsed;
|
2017-11-07 03:10:14 +00:00
|
|
|
|
|
|
|
|
/** @var IContextSource */
|
|
|
|
|
protected $context;
|
|
|
|
|
|
|
|
|
|
/** @var Config */
|
|
|
|
|
protected $config;
|
|
|
|
|
|
2021-07-22 03:11:47 +00:00
|
|
|
protected function setUp(): void {
|
2017-11-07 03:10:14 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
$this->context = new RequestContext();
|
|
|
|
|
$this->context->setTitle( Title::newFromText( self::class ) );
|
2018-08-03 08:43:00 +00:00
|
|
|
|
2022-01-12 20:13:39 +00:00
|
|
|
$services = $this->getServiceContainer();
|
2018-08-03 08:43:00 +00:00
|
|
|
|
2022-08-01 19:14:41 +00:00
|
|
|
$this->overrideConfigValue( MainConfigNames::DisableLangConversion, false );
|
2018-08-03 08:43:00 +00:00
|
|
|
$this->config = $services->getMainConfig();
|
2017-11-07 03:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
2021-05-29 01:32:24 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ::__construct
|
|
|
|
|
*/
|
|
|
|
|
public function testConstruct() {
|
2021-06-04 23:24:44 +00:00
|
|
|
// Make sure if the optional services are not provided, stuff still works, so that
|
2021-05-29 01:32:24 +00:00
|
|
|
// the GlobalPreferences extension isn't broken
|
|
|
|
|
$params = [
|
|
|
|
|
$this->createMock( ServiceOptions::class ),
|
|
|
|
|
$this->createMock( Language::class ),
|
|
|
|
|
$this->createMock( AuthManager::class ),
|
|
|
|
|
$this->createMock( LinkRenderer::class ),
|
|
|
|
|
$this->createMock( NamespaceInfo::class ),
|
|
|
|
|
$this->createMock( PermissionManager::class ),
|
|
|
|
|
$this->createMock( ILanguageConverter::class ),
|
|
|
|
|
$this->createMock( LanguageNameUtils::class ),
|
|
|
|
|
$this->createMock( HookContainer::class ),
|
|
|
|
|
$this->createMock( UserOptionsLookup::class ),
|
|
|
|
|
];
|
|
|
|
|
$preferencesFactory = new DefaultPreferencesFactory( ...$params );
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
|
DefaultPreferencesFactory::class,
|
|
|
|
|
$preferencesFactory,
|
2021-06-04 23:24:44 +00:00
|
|
|
'Created with some services missing'
|
2021-05-29 01:32:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Now, make sure that MediaWikiServices isn't used
|
2021-06-05 03:45:14 +00:00
|
|
|
// Switch the UserOptionsLookup to a UserOptionsManager
|
|
|
|
|
$params[9] = $this->createMock( UserOptionsManager::class );
|
2021-06-04 23:24:44 +00:00
|
|
|
$params[] = $this->createMock( LanguageConverterFactory::class );
|
|
|
|
|
$params[] = $this->createMock( Parser::class );
|
|
|
|
|
$params[] = $this->createMock( SkinFactory::class );
|
2021-05-29 01:32:24 +00:00
|
|
|
$params[] = $this->createMock( UserGroupManager::class );
|
2022-04-07 23:52:05 +00:00
|
|
|
$params[] = $this->createMock( SignatureValidatorFactory::class );
|
2022-10-14 12:37:35 +00:00
|
|
|
$params[] = $this->createMock( Config::class );
|
2021-05-29 01:32:24 +00:00
|
|
|
$oldMwServices = MediaWikiServices::forceGlobalInstance(
|
|
|
|
|
$this->createNoOpMock( MediaWikiServices::class )
|
|
|
|
|
);
|
|
|
|
|
// Wrap in a try-finally block to make sure the real MediaWikiServices is
|
|
|
|
|
// always put back even if something goes wrong
|
|
|
|
|
try {
|
|
|
|
|
$preferencesFactory = new DefaultPreferencesFactory( ...$params );
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
|
DefaultPreferencesFactory::class,
|
|
|
|
|
$preferencesFactory,
|
2021-06-04 23:24:44 +00:00
|
|
|
'Created with all services, MediaWikiServices not used'
|
2021-05-29 01:32:24 +00:00
|
|
|
);
|
|
|
|
|
} finally {
|
|
|
|
|
// Put back the real MediaWikiServices
|
|
|
|
|
MediaWikiServices::forceGlobalInstance( $oldMwServices );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-07 03:10:14 +00:00
|
|
|
/**
|
|
|
|
|
* Get a basic PreferencesFactory for testing with.
|
2021-05-29 01:32:24 +00:00
|
|
|
* @param array $options Supported options are:
|
|
|
|
|
* 'language' - A Language object, falls back to `new Language()`
|
2021-06-05 03:45:14 +00:00
|
|
|
* 'userOptionsManager' - A UserOptionsManager service, falls back to using MediaWikiServices
|
2021-05-29 01:32:24 +00:00
|
|
|
* 'userGroupManager' - A UserGroupManager service, falls back to a mock where no users
|
|
|
|
|
* have any extra groups, just `*` and `user`
|
2017-11-07 03:10:14 +00:00
|
|
|
* @return DefaultPreferencesFactory
|
|
|
|
|
*/
|
2021-05-29 01:32:24 +00:00
|
|
|
protected function getPreferencesFactory( array $options = [] ) {
|
2021-05-04 23:24:35 +00:00
|
|
|
// DummyServicesTrait::getDummyNamespaceInfo
|
|
|
|
|
$nsInfo = $this->getDummyNamespaceInfo();
|
2018-08-05 12:56:23 +00:00
|
|
|
|
2022-01-12 20:13:39 +00:00
|
|
|
$services = $this->getServiceContainer();
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
|
2021-05-26 06:06:00 +00:00
|
|
|
// The PermissionManager should not be used for anything, its only a parameter
|
|
|
|
|
// until we figure out how to remove it without breaking the GlobalPreferences
|
|
|
|
|
// extension (GlobalPreferencesFactory extends DefaultPreferencesFactory)
|
|
|
|
|
$permissionManager = $this->createNoOpMock( PermissionManager::class );
|
|
|
|
|
|
2021-05-29 01:32:24 +00:00
|
|
|
$language = $options['language'] ?? new Language();
|
2021-06-05 03:45:14 +00:00
|
|
|
$userOptionsManager = $options['userOptionsManager'] ?? $services->getUserOptionsManager();
|
2021-05-29 01:32:24 +00:00
|
|
|
|
|
|
|
|
$userGroupManager = $options['userGroupManager'] ?? false;
|
|
|
|
|
if ( !$userGroupManager ) {
|
|
|
|
|
$userGroupManager = $this->createMock( UserGroupManager::class );
|
|
|
|
|
$userGroupManager->method( 'getUserGroupMemberships' )->willReturn( [] );
|
|
|
|
|
$userGroupManager->method( 'getUserEffectiveGroups' )->willReturnCallback(
|
|
|
|
|
static function ( UserIdentity $user ) {
|
|
|
|
|
return $user->isRegistered() ? [ '*', 'user' ] : [ '*' ];
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-07 03:10:14 +00:00
|
|
|
return new DefaultPreferencesFactory(
|
2019-08-14 12:58:53 +00:00
|
|
|
new LoggedServiceOptions( self::$serviceOptionsAccessLog,
|
2019-10-08 18:28:15 +00:00
|
|
|
DefaultPreferencesFactory::CONSTRUCTOR_OPTIONS, $this->config ),
|
2020-02-04 12:42:03 +00:00
|
|
|
$language,
|
2020-03-31 18:51:49 +00:00
|
|
|
$services->getAuthManager(),
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
$services->getLinkRenderer(),
|
2021-05-04 23:24:35 +00:00
|
|
|
$nsInfo,
|
2021-05-26 06:06:00 +00:00
|
|
|
$permissionManager,
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
$services->getLanguageConverterFactory()->getLanguageConverter( $language ),
|
|
|
|
|
$services->getLanguageNameUtils(),
|
2021-03-16 15:16:18 +00:00
|
|
|
$services->getHookContainer(),
|
2021-06-05 03:45:14 +00:00
|
|
|
$userOptionsManager,
|
2021-06-04 23:24:44 +00:00
|
|
|
$services->getLanguageConverterFactory(),
|
|
|
|
|
$services->getParser(),
|
|
|
|
|
$services->getSkinFactory(),
|
2022-04-07 23:52:05 +00:00
|
|
|
$userGroupManager,
|
2022-10-14 12:37:35 +00:00
|
|
|
$services->getSignatureValidatorFactory(),
|
|
|
|
|
$services->getMainConfig()
|
2017-11-07 03:10:14 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-02-04 12:42:03 +00:00
|
|
|
* @covers ::getForm
|
|
|
|
|
* @covers ::searchPreferences
|
2017-11-07 03:10:14 +00:00
|
|
|
*/
|
|
|
|
|
public function testGetForm() {
|
2018-07-19 15:35:59 +00:00
|
|
|
$this->setTemporaryHook( 'GetPreferences', null );
|
|
|
|
|
|
2017-11-07 03:10:14 +00:00
|
|
|
$testUser = $this->getTestUser();
|
2021-05-29 01:32:24 +00:00
|
|
|
$prefFactory = $this->getPreferencesFactory();
|
2020-04-13 01:21:01 +00:00
|
|
|
$form = $prefFactory->getForm( $testUser->getUser(), $this->context );
|
2019-04-14 00:43:33 +00:00
|
|
|
$this->assertInstanceOf( PreferencesFormOOUI::class, $form );
|
2022-02-18 02:39:03 +00:00
|
|
|
$this->assertCount( 6, $form->getPreferenceSections() );
|
2017-11-07 03:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-20 22:54:06 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ::sortSkinNames
|
|
|
|
|
*/
|
|
|
|
|
public function testSortSkinNames() {
|
|
|
|
|
/** @var DefaultPreferencesFactory $factory */
|
|
|
|
|
$factory = TestingAccessWrapper::newFromObject(
|
|
|
|
|
$this->getPreferencesFactory()
|
|
|
|
|
);
|
|
|
|
|
$validSkinNames = [
|
|
|
|
|
'minerva' => 'Minerva Neue',
|
|
|
|
|
'monobook' => 'Monobook',
|
|
|
|
|
'cologne-blue' => 'Cologne Blue',
|
|
|
|
|
'vector' => 'Vector',
|
|
|
|
|
'vector-2022' => 'Vector 2022',
|
|
|
|
|
'timeless' => 'Timeless',
|
|
|
|
|
];
|
|
|
|
|
$currentSkin = 'monobook';
|
|
|
|
|
$preferredSkins = [ 'vector-2022', 'invalid-skin', 'vector' ];
|
|
|
|
|
|
|
|
|
|
uksort( $validSkinNames, static function ( $a, $b ) use ( $factory, $currentSkin, $preferredSkins ) {
|
|
|
|
|
return $factory->sortSkinNames( $a, $b, $currentSkin, $preferredSkins );
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
$this->assertArrayEquals( [
|
|
|
|
|
'monobook' => 'Monobook',
|
|
|
|
|
'vector-2022' => 'Vector 2022',
|
|
|
|
|
'vector' => 'Vector',
|
|
|
|
|
'cologne-blue' => 'Cologne Blue',
|
|
|
|
|
'minerva' => 'Minerva Neue',
|
|
|
|
|
'timeless' => 'Timeless',
|
|
|
|
|
], $validSkinNames );
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-07 03:10:14 +00:00
|
|
|
/**
|
|
|
|
|
* CSS classes for emailauthentication preference field when there's no email.
|
|
|
|
|
* @see https://phabricator.wikimedia.org/T36302
|
2020-02-04 12:42:03 +00:00
|
|
|
*
|
|
|
|
|
* @covers ::profilePreferences
|
2017-11-07 03:10:14 +00:00
|
|
|
* @dataProvider emailAuthenticationProvider
|
|
|
|
|
*/
|
|
|
|
|
public function testEmailAuthentication( $user, $cssClass ) {
|
2021-05-29 01:32:24 +00:00
|
|
|
$prefs = $this->getPreferencesFactory()
|
2020-02-04 12:42:03 +00:00
|
|
|
->getFormDescriptor( $user, $this->context );
|
2017-11-07 03:10:14 +00:00
|
|
|
$this->assertArrayHasKey( 'cssclass', $prefs['emailauthentication'] );
|
|
|
|
|
$this->assertEquals( $cssClass, $prefs['emailauthentication']['cssclass'] );
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 13:37:14 +00:00
|
|
|
/**
|
2020-02-04 12:42:03 +00:00
|
|
|
* @covers ::renderingPreferences
|
2019-02-19 13:37:14 +00:00
|
|
|
*/
|
|
|
|
|
public function testShowRollbackConfIsHiddenForUsersWithoutRollbackRights() {
|
2022-07-14 12:42:07 +00:00
|
|
|
$userMock = $this->createMock( User::class );
|
2021-05-26 06:06:00 +00:00
|
|
|
$userMock->method( 'isAllowed' )->willReturnCallback(
|
|
|
|
|
static function ( $permission ) {
|
|
|
|
|
return $permission === 'editmyoptions';
|
|
|
|
|
}
|
|
|
|
|
);
|
2021-04-12 14:45:26 +00:00
|
|
|
|
2021-06-05 03:45:14 +00:00
|
|
|
$userOptionsManagerMock = $this->createUserOptionsManagerMock( [ 'test' => 'yes' ], true );
|
2021-12-06 09:33:43 +00:00
|
|
|
$userMock = $this->getUserMockWithSession( $userMock );
|
2021-05-29 01:32:24 +00:00
|
|
|
$prefs = $this->getPreferencesFactory( [
|
2021-06-05 03:45:14 +00:00
|
|
|
'userOptionsManager' => $userOptionsManagerMock,
|
2021-05-29 01:32:24 +00:00
|
|
|
] )->getFormDescriptor( $userMock, $this->context );
|
2019-02-19 13:37:14 +00:00
|
|
|
$this->assertArrayNotHasKey( 'showrollbackconfirmation', $prefs );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-02-04 12:42:03 +00:00
|
|
|
* @covers ::renderingPreferences
|
2019-02-19 13:37:14 +00:00
|
|
|
*/
|
|
|
|
|
public function testShowRollbackConfIsShownForUsersWithRollbackRights() {
|
2022-07-14 12:42:07 +00:00
|
|
|
$userMock = $this->createMock( User::class );
|
2021-05-26 06:06:00 +00:00
|
|
|
$userMock->method( 'isAllowed' )->willReturnCallback(
|
|
|
|
|
static function ( $permission ) {
|
|
|
|
|
return $permission === 'editmyoptions' || $permission === 'rollback';
|
|
|
|
|
}
|
|
|
|
|
);
|
2021-12-06 09:33:43 +00:00
|
|
|
$userMock = $this->getUserMockWithSession( $userMock );
|
2021-04-12 14:45:26 +00:00
|
|
|
|
2021-06-05 03:45:14 +00:00
|
|
|
$userOptionsManagerMock = $this->createUserOptionsManagerMock( [ 'test' => 'yes' ], true );
|
2021-05-29 01:32:24 +00:00
|
|
|
$prefs = $this->getPreferencesFactory( [
|
2021-06-05 03:45:14 +00:00
|
|
|
'userOptionsManager' => $userOptionsManagerMock,
|
2021-05-29 01:32:24 +00:00
|
|
|
] )->getFormDescriptor( $userMock, $this->context );
|
2019-02-19 13:37:14 +00:00
|
|
|
$this->assertArrayHasKey( 'showrollbackconfirmation', $prefs );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'rendering/advancedrendering',
|
|
|
|
|
$prefs['showrollbackconfirmation']['section']
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-07 03:10:14 +00:00
|
|
|
public function emailAuthenticationProvider() {
|
|
|
|
|
$userNoEmail = new User;
|
|
|
|
|
$userEmailUnauthed = new User;
|
|
|
|
|
$userEmailUnauthed->setEmail( 'noauth@example.org' );
|
|
|
|
|
$userEmailAuthed = new User;
|
|
|
|
|
$userEmailAuthed->setEmail( 'noauth@example.org' );
|
|
|
|
|
$userEmailAuthed->setEmailAuthenticationTimestamp( wfTimestamp() );
|
|
|
|
|
return [
|
|
|
|
|
[ $userNoEmail, 'mw-email-none' ],
|
|
|
|
|
[ $userEmailUnauthed, 'mw-email-not-authenticated' ],
|
|
|
|
|
[ $userEmailAuthed, 'mw-email-authenticated' ],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that PreferencesFormPreSave hook has correct data:
|
|
|
|
|
* - user Object is passed
|
|
|
|
|
* - oldUserOptions contains previous user options (before save)
|
|
|
|
|
* - formData and User object have set up new properties
|
|
|
|
|
*
|
|
|
|
|
* @see https://phabricator.wikimedia.org/T169365
|
2020-02-04 12:42:03 +00:00
|
|
|
* @covers ::submitForm
|
2017-11-07 03:10:14 +00:00
|
|
|
*/
|
|
|
|
|
public function testPreferencesFormPreSaveHookHasCorrectData() {
|
|
|
|
|
$oldOptions = [
|
|
|
|
|
'test' => 'abc',
|
|
|
|
|
'option' => 'old'
|
|
|
|
|
];
|
|
|
|
|
$newOptions = [
|
|
|
|
|
'test' => 'abc',
|
|
|
|
|
'option' => 'new'
|
|
|
|
|
];
|
|
|
|
|
$configMock = new HashConfig( [
|
|
|
|
|
'HiddenPrefs' => []
|
|
|
|
|
] );
|
2022-07-14 12:42:07 +00:00
|
|
|
$form = $this->createMock( PreferencesFormOOUI::class );
|
2017-11-07 03:10:14 +00:00
|
|
|
|
2022-07-14 12:42:07 +00:00
|
|
|
$userMock = $this->createMock( User::class );
|
2017-11-07 03:10:14 +00:00
|
|
|
|
2021-06-05 03:45:14 +00:00
|
|
|
$userOptionsManagerMock = $this->createUserOptionsManagerMock( $oldOptions );
|
|
|
|
|
$userOptionsManagerMock->expects( $this->exactly( 2 ) )
|
2017-11-07 03:10:14 +00:00
|
|
|
->method( 'setOption' )
|
|
|
|
|
->withConsecutive(
|
2021-06-05 03:45:14 +00:00
|
|
|
[ $userMock, 'test', $newOptions[ 'test' ] ],
|
|
|
|
|
[ $userMock, 'option', $newOptions[ 'option' ] ]
|
2017-11-07 03:10:14 +00:00
|
|
|
);
|
2021-05-26 06:06:00 +00:00
|
|
|
$userMock->method( 'isAllowed' )->willReturnCallback(
|
|
|
|
|
static function ( $permission ) {
|
|
|
|
|
return $permission === 'editmyprivateinfo' || $permission === 'editmyoptions';
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
$userMock->method( 'isAllowedAny' )->willReturnCallback(
|
|
|
|
|
static function ( ...$permissions ) {
|
|
|
|
|
foreach ( $permissions as $perm ) {
|
|
|
|
|
if ( $perm === 'editmyprivateinfo' || $perm === 'editmyoptions' ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
);
|
2017-11-07 03:10:14 +00:00
|
|
|
|
2019-08-22 22:53:05 +00:00
|
|
|
$form->method( 'getModifiedUser' )
|
2017-11-07 03:10:14 +00:00
|
|
|
->willReturn( $userMock );
|
|
|
|
|
|
2019-08-22 22:53:05 +00:00
|
|
|
$form->method( 'getContext' )
|
2017-11-07 03:10:14 +00:00
|
|
|
->willReturn( $this->context );
|
|
|
|
|
|
2019-08-22 22:53:05 +00:00
|
|
|
$form->method( 'getConfig' )
|
2017-11-07 03:10:14 +00:00
|
|
|
->willReturn( $configMock );
|
|
|
|
|
|
|
|
|
|
$this->setTemporaryHook( 'PreferencesFormPreSave',
|
|
|
|
|
function ( $formData, $form, $user, &$result, $oldUserOptions )
|
|
|
|
|
use ( $newOptions, $oldOptions, $userMock ) {
|
|
|
|
|
$this->assertSame( $userMock, $user );
|
|
|
|
|
foreach ( $newOptions as $option => $value ) {
|
|
|
|
|
$this->assertSame( $value, $formData[ $option ] );
|
|
|
|
|
}
|
|
|
|
|
foreach ( $oldOptions as $option => $value ) {
|
|
|
|
|
$this->assertSame( $value, $oldUserOptions[ $option ] );
|
|
|
|
|
}
|
2020-01-09 23:23:19 +00:00
|
|
|
$this->assertTrue( $result );
|
2017-11-07 03:10:14 +00:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2018-07-06 19:21:22 +00:00
|
|
|
/** @var DefaultPreferencesFactory $factory */
|
2020-02-04 12:42:03 +00:00
|
|
|
$factory = TestingAccessWrapper::newFromObject(
|
2021-06-05 03:45:14 +00:00
|
|
|
$this->getPreferencesFactory( [ 'userOptionsManager' => $userOptionsManagerMock ] )
|
2020-02-04 12:42:03 +00:00
|
|
|
);
|
2018-07-06 19:21:22 +00:00
|
|
|
$factory->saveFormData( $newOptions, $form, [] );
|
2017-11-07 03:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The rclimit preference should accept non-integer input and filter it to become an integer.
|
2018-05-07 08:22:40 +00:00
|
|
|
*
|
2020-02-04 12:42:03 +00:00
|
|
|
* @covers ::saveFormData
|
2017-11-07 03:10:14 +00:00
|
|
|
*/
|
|
|
|
|
public function testIntvalFilter() {
|
|
|
|
|
// Test a string with leading zeros (i.e. not octal) and spaces.
|
|
|
|
|
$this->context->getRequest()->setVal( 'wprclimit', ' 0012 ' );
|
|
|
|
|
$user = new User;
|
2021-05-29 01:32:24 +00:00
|
|
|
$prefFactory = $this->getPreferencesFactory();
|
2020-04-13 01:21:01 +00:00
|
|
|
$form = $prefFactory->getForm( $user, $this->context );
|
2017-11-07 03:10:14 +00:00
|
|
|
$form->show();
|
|
|
|
|
$form->trySubmit();
|
2021-12-01 15:21:23 +00:00
|
|
|
$userOptionsLookup = $this->getServiceContainer()->getUserOptionsLookup();
|
|
|
|
|
$this->assertEquals( 12, $userOptionsLookup->getOption( $user, 'rclimit' ) );
|
2017-11-07 03:10:14 +00:00
|
|
|
}
|
2019-08-14 12:58:53 +00:00
|
|
|
|
2020-02-04 12:42:03 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ::profilePreferences
|
|
|
|
|
*/
|
|
|
|
|
public function testVariantsSupport() {
|
2022-07-14 12:42:07 +00:00
|
|
|
$userMock = $this->createMock( User::class );
|
2021-05-26 06:06:00 +00:00
|
|
|
$userMock->method( 'isAllowed' )->willReturn( true );
|
2021-12-06 09:33:43 +00:00
|
|
|
$userMock = $this->getUserMockWithSession( $userMock );
|
2020-02-04 12:42:03 +00:00
|
|
|
|
|
|
|
|
$language = $this->createMock( Language::class );
|
2021-04-22 08:40:46 +00:00
|
|
|
$language->method( 'getCode' )
|
2020-02-04 12:42:03 +00:00
|
|
|
->willReturn( 'sr' );
|
|
|
|
|
|
2021-06-05 03:45:14 +00:00
|
|
|
$userOptionsManagerMock = $this->createUserOptionsManagerMock(
|
2021-04-12 14:45:26 +00:00
|
|
|
[ 'LanguageCode' => 'sr', 'variant' => 'sr' ], true
|
|
|
|
|
);
|
|
|
|
|
|
2021-05-29 01:32:24 +00:00
|
|
|
$prefs = $this->getPreferencesFactory( [
|
|
|
|
|
'language' => $language,
|
2021-06-05 03:45:14 +00:00
|
|
|
'userOptionsManager' => $userOptionsManagerMock,
|
2021-05-29 01:32:24 +00:00
|
|
|
] )->getFormDescriptor( $userMock, $this->context );
|
2020-02-04 12:42:03 +00:00
|
|
|
$this->assertArrayHasKey( 'default', $prefs['variant'] );
|
|
|
|
|
$this->assertEquals( 'sr', $prefs['variant']['default'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::profilePreferences
|
|
|
|
|
*/
|
|
|
|
|
public function testUserGroupMemberships() {
|
2022-07-14 12:42:07 +00:00
|
|
|
$userMock = $this->createMock( User::class );
|
2021-05-26 06:06:00 +00:00
|
|
|
$userMock->method( 'isAllowed' )->willReturn( true );
|
|
|
|
|
$userMock->method( 'isAllowedAny' )->willReturn( true );
|
2021-05-29 01:32:24 +00:00
|
|
|
$userMock->method( 'isRegistered' )->willReturn( true );
|
2021-12-06 09:33:43 +00:00
|
|
|
$userMock = $this->getUserMockWithSession( $userMock );
|
2020-02-04 12:42:03 +00:00
|
|
|
|
|
|
|
|
$language = $this->createMock( Language::class );
|
2021-04-22 08:40:46 +00:00
|
|
|
$language->method( 'getCode' )
|
2020-02-04 12:42:03 +00:00
|
|
|
->willReturn( 'en' );
|
|
|
|
|
|
2021-06-05 03:45:14 +00:00
|
|
|
$userOptionsManagerMock = $this->createUserOptionsManagerMock( [], true );
|
2021-04-12 14:45:26 +00:00
|
|
|
|
2021-05-29 01:32:24 +00:00
|
|
|
$prefs = $this->getPreferencesFactory( [
|
|
|
|
|
'language' => $language,
|
2021-06-05 03:45:14 +00:00
|
|
|
'userOptionsManager' => $userOptionsManagerMock,
|
2021-05-29 01:32:24 +00:00
|
|
|
] )->getFormDescriptor( $userMock, $this->context );
|
2020-02-04 12:42:03 +00:00
|
|
|
$this->assertArrayHasKey( 'default', $prefs['usergroups'] );
|
2021-05-29 01:32:24 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
UserGroupMembership::getLink( 'user', $this->context, 'html' ),
|
|
|
|
|
( $prefs['usergroups']['default'] )()
|
|
|
|
|
);
|
2020-02-04 12:42:03 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-14 12:58:53 +00:00
|
|
|
/**
|
|
|
|
|
* @coversNothing
|
|
|
|
|
*/
|
|
|
|
|
public function testAllServiceOptionsUsed() {
|
preferences: Signature validation (lint errors, user links, nested subst)
Three new checks are now applied to user signatures in preferences:
* Disallow invalid HTML and lint errors (T140606)
Since 15e0e9bb4b we can rely on Parsoid to check the signature for
lint errors. (The old PHP Parser doesn't have this capability.)
Most importantly, this will disallow unclosed HTML tags. Unclosed
formatting tags like `<i>` (and also wikitext markup like `''`)
could affect the entire page with the bad markup.
New configuration variable $wgSignatureAllowedLintErrors is added
to allow ignoring some errors. The default value ignores the
'obsolete-tag' error (caused by HTML tags like `<font>` and `<tt>`.)
* Require a link to user page, talk page or contributions (T237700)
Various tools don't work correctly when such a link is missing. For
example, Echo notifications are not sent, DiscussionTools will not
allow replying to these comments, English Wikipedia's SineBot treats
these comments as unsigned.
Such requirement has been present for a long time in many Wikimedia
wikis' policies, but it was not enforced by software.
* Disallow "nested" substitution in signature (T230652)
Clever abuse of "subst" markup and tildes allows users to save edits
containing wikitext in which substitution occurs again when the page
is next saved. Disallow this in signatures, at least.
New configuration variable $wgSignatureValidation is added to control
what we do about the result of the validation described above. The
options are:
* 'warning':
Only displays a warning near the field on Special:Preferences if
the current signature is invalid. Signatures can still be changed
regardless of validity and will be used when signing comments.
* 'new':
In addition to the above, if a user tries to change their signature,
the new one must be valid. Existing invalid signatures are still
used when signing comments.
* 'disallow':
In addition to the above, existing invalid signatures are no longer
used when signing comments.
Bug: T140606
Bug: T237700
Bug: T230652
Change-Id: I07c575c2d9d2afe7a89c4847d16ac044417297bf
2019-11-09 00:15:51 +00:00
|
|
|
$this->assertAllServiceOptionsUsed( [
|
|
|
|
|
// Only used when $wgEnotifWatchlist or $wgEnotifUserTalk is true
|
|
|
|
|
'EnotifMinorEdits',
|
|
|
|
|
// Only used when $wgEnotifWatchlist or $wgEnotifUserTalk is true
|
|
|
|
|
'EnotifRevealEditorAddress',
|
|
|
|
|
// Only used when 'fancysig' preference is enabled
|
|
|
|
|
'SignatureValidation',
|
|
|
|
|
] );
|
2019-08-14 12:58:53 +00:00
|
|
|
}
|
2021-04-12 14:45:26 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $userOptions
|
|
|
|
|
* @param bool $defaultOptions
|
2021-06-05 03:45:14 +00:00
|
|
|
* @return UserOptionsManager
|
2021-04-12 14:45:26 +00:00
|
|
|
*/
|
2021-06-05 03:45:14 +00:00
|
|
|
private function createUserOptionsManagerMock( array $userOptions, bool $defaultOptions = false ) {
|
2022-03-30 10:32:27 +00:00
|
|
|
$services = $this->getServiceContainer();
|
|
|
|
|
$defaults = $services->getMainConfig()->get( 'DefaultUserOptions' );
|
|
|
|
|
$defaults['language'] = $services->getContentLanguage()->getCode();
|
|
|
|
|
$defaults['skin'] = Skin::normalizeKey( $services->getMainConfig()->get( 'DefaultSkin' ) );
|
|
|
|
|
$userOptions += $defaults;
|
|
|
|
|
|
2021-06-05 03:45:14 +00:00
|
|
|
$mock = $this->createMock( UserOptionsManager::class );
|
2021-04-12 14:45:26 +00:00
|
|
|
$mock->method( 'getOptions' )->willReturn( $userOptions );
|
2022-07-28 04:21:13 +00:00
|
|
|
$mock->method( 'getOption' )->willReturnCallback(
|
|
|
|
|
static function ( $user, $option ) use ( $userOptions ) {
|
|
|
|
|
return $userOptions[$option] ?? null;
|
|
|
|
|
}
|
|
|
|
|
);
|
2021-04-12 14:45:26 +00:00
|
|
|
if ( $defaultOptions ) {
|
|
|
|
|
$mock->method( 'getDefaultOptions' )->willReturn( $defaults );
|
|
|
|
|
}
|
|
|
|
|
return $mock;
|
|
|
|
|
}
|
2021-12-06 09:33:43 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param MockObject $userMock
|
|
|
|
|
* @return MockObject
|
|
|
|
|
*/
|
|
|
|
|
private function getUserMockWithSession( MockObject $userMock ): MockObject {
|
|
|
|
|
// We're mocking a stdClass because the Session class is final, and thus not mockable.
|
|
|
|
|
$mock = $this->getMockBuilder( stdClass::class )
|
|
|
|
|
->addMethods( [ 'getAllowedUserRights', 'deregisterSession', 'getSessionId' ] )
|
|
|
|
|
->getMock();
|
|
|
|
|
$mock->method( 'getSessionId' )->willReturn(
|
|
|
|
|
new SessionId( str_repeat( 'X', 32 ) )
|
|
|
|
|
);
|
|
|
|
|
$session = TestUtils::getDummySession( $mock );
|
|
|
|
|
$mockRequest = $this->getMockBuilder( FauxRequest::class )
|
|
|
|
|
->onlyMethods( [ 'getSession' ] )
|
|
|
|
|
->getMock();
|
|
|
|
|
$mockRequest->method( 'getSession' )->willReturn( $session );
|
|
|
|
|
$userMock->method( 'getRequest' )->willReturn( $mockRequest );
|
2022-07-25 20:56:14 +00:00
|
|
|
$userMock->method( 'getTitleKey' )->willReturn( '' );
|
2021-12-06 09:33:43 +00:00
|
|
|
return $userMock;
|
|
|
|
|
}
|
2017-11-07 03:10:14 +00:00
|
|
|
}
|