3 use MediaWiki\Auth\AuthManager
;
4 use MediaWiki\MediaWikiServices
;
5 use MediaWiki\Preferences\DefaultPreferencesFactory
;
6 use Wikimedia\TestingAccessWrapper
;
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
30 class DefaultPreferencesFactoryTest
extends \MediaWikiTestCase
{
31 use TestAllServiceOptionsUsed
;
33 /** @var IContextSource */
39 public function setUp() {
41 $this->context
= new RequestContext();
42 $this->context
->setTitle( Title
::newFromText( self
::class ) );
44 $services = MediaWikiServices
::getInstance();
46 $this->setMwGlobals( 'wgParser', $services->getParserFactory()->create() );
47 $this->config
= $services->getMainConfig();
51 * Get a basic PreferencesFactory for testing with.
52 * @return DefaultPreferencesFactory
54 protected function getPreferencesFactory() {
55 $mockNsInfo = $this->createMock( NamespaceInfo
::class );
56 $mockNsInfo->method( 'getValidNamespaces' )->willReturn( [
57 NS_MAIN
, NS_TALK
, NS_USER
, NS_USER_TALK
59 $mockNsInfo->expects( $this->never() )
60 ->method( $this->anythingBut( 'getValidNamespaces', '__destruct' ) );
62 return new DefaultPreferencesFactory(
63 new LoggedServiceOptions( self
::$serviceOptionsAccessLog,
64 DefaultPreferencesFactory
::$constructorOptions, $this->config
),
66 AuthManager
::singleton(),
67 MediaWikiServices
::getInstance()->getLinkRenderer(),
73 * @covers MediaWiki\Preferences\DefaultPreferencesFactory::getForm()
75 public function testGetForm() {
76 $this->setTemporaryHook( 'GetPreferences', null );
78 $testUser = $this->getTestUser();
79 $form = $this->getPreferencesFactory()->getForm( $testUser->getUser(), $this->context
);
80 $this->assertInstanceOf( PreferencesFormOOUI
::class, $form );
81 $this->assertCount( 5, $form->getPreferenceSections() );
85 * CSS classes for emailauthentication preference field when there's no email.
86 * @see https://phabricator.wikimedia.org/T36302
87 * @covers MediaWiki\Preferences\DefaultPreferencesFactory::profilePreferences()
88 * @dataProvider emailAuthenticationProvider
90 public function testEmailAuthentication( $user, $cssClass ) {
91 $prefs = $this->getPreferencesFactory()->getFormDescriptor( $user, $this->context
);
92 $this->assertArrayHasKey( 'cssclass', $prefs['emailauthentication'] );
93 $this->assertEquals( $cssClass, $prefs['emailauthentication']['cssclass'] );
97 * @covers MediaWiki\Preferences\DefaultPreferencesFactory::renderingPreferences()
99 public function testShowRollbackConfIsHiddenForUsersWithoutRollbackRights() {
100 $userMock = $this->getMockBuilder( User
::class )
101 ->disableOriginalConstructor()
103 $userMock->method( 'isAllowed' )
104 ->willReturn( false );
105 $userMock->method( 'getEffectiveGroups' )
107 $userMock->method( 'getGroupMemberships' )
109 $userMock->method( 'getOptions' )
110 ->willReturn( [ 'test' => 'yes' ] );
112 $prefs = $this->getPreferencesFactory()->getFormDescriptor( $userMock, $this->context
);
113 $this->assertArrayNotHasKey( 'showrollbackconfirmation', $prefs );
117 * @covers MediaWiki\Preferences\DefaultPreferencesFactory::renderingPreferences()
119 public function testShowRollbackConfIsShownForUsersWithRollbackRights() {
120 $userMock = $this->getMockBuilder( User
::class )
121 ->disableOriginalConstructor()
123 $userMock->method( 'isAllowed' )
124 ->willReturn( true );
125 $userMock->method( 'getEffectiveGroups' )
127 $userMock->method( 'getGroupMemberships' )
129 $userMock->method( 'getOptions' )
130 ->willReturn( [ 'test' => 'yes' ] );
132 $prefs = $this->getPreferencesFactory()->getFormDescriptor( $userMock, $this->context
);
133 $this->assertArrayHasKey( 'showrollbackconfirmation', $prefs );
135 'rendering/advancedrendering',
136 $prefs['showrollbackconfirmation']['section']
140 public function emailAuthenticationProvider() {
141 $userNoEmail = new User
;
142 $userEmailUnauthed = new User
;
143 $userEmailUnauthed->setEmail( 'noauth@example.org' );
144 $userEmailAuthed = new User
;
145 $userEmailAuthed->setEmail( 'noauth@example.org' );
146 $userEmailAuthed->setEmailAuthenticationTimestamp( wfTimestamp() );
148 [ $userNoEmail, 'mw-email-none' ],
149 [ $userEmailUnauthed, 'mw-email-not-authenticated' ],
150 [ $userEmailAuthed, 'mw-email-authenticated' ],
155 * Test that PreferencesFormPreSave hook has correct data:
156 * - user Object is passed
157 * - oldUserOptions contains previous user options (before save)
158 * - formData and User object have set up new properties
160 * @see https://phabricator.wikimedia.org/T169365
161 * @covers MediaWiki\Preferences\DefaultPreferencesFactory::submitForm()
163 public function testPreferencesFormPreSaveHookHasCorrectData() {
172 $configMock = new HashConfig( [
175 $form = $this->getMockBuilder( PreferencesFormOOUI
::class )
176 ->disableOriginalConstructor()
179 $userMock = $this->getMockBuilder( User
::class )
180 ->disableOriginalConstructor()
182 $userMock->method( 'getOptions' )
183 ->willReturn( $oldOptions );
184 $userMock->method( 'isAllowedAny' )
185 ->willReturn( true );
186 $userMock->method( 'isAllowed' )
187 ->willReturn( true );
189 $userMock->expects( $this->exactly( 2 ) )
190 ->method( 'setOption' )
192 [ $this->equalTo( 'test' ), $this->equalTo( $newOptions[ 'test' ] ) ],
193 [ $this->equalTo( 'option' ), $this->equalTo( $newOptions[ 'option' ] ) ]
196 $form->expects( $this->any() )
197 ->method( 'getModifiedUser' )
198 ->willReturn( $userMock );
200 $form->expects( $this->any() )
201 ->method( 'getContext' )
202 ->willReturn( $this->context
);
204 $form->expects( $this->any() )
205 ->method( 'getConfig' )
206 ->willReturn( $configMock );
208 $this->setTemporaryHook( 'PreferencesFormPreSave',
209 function ( $formData, $form, $user, &$result, $oldUserOptions )
210 use ( $newOptions, $oldOptions, $userMock ) {
211 $this->assertSame( $userMock, $user );
212 foreach ( $newOptions as $option => $value ) {
213 $this->assertSame( $value, $formData[ $option ] );
215 foreach ( $oldOptions as $option => $value ) {
216 $this->assertSame( $value, $oldUserOptions[ $option ] );
218 $this->assertEquals( true, $result );
222 /** @var DefaultPreferencesFactory $factory */
223 $factory = TestingAccessWrapper
::newFromObject( $this->getPreferencesFactory() );
224 $factory->saveFormData( $newOptions, $form, [] );
228 * The rclimit preference should accept non-integer input and filter it to become an integer.
230 * @covers \MediaWiki\Preferences\DefaultPreferencesFactory::saveFormData
232 public function testIntvalFilter() {
233 // Test a string with leading zeros (i.e. not octal) and spaces.
234 $this->context
->getRequest()->setVal( 'wprclimit', ' 0012 ' );
236 $form = $this->getPreferencesFactory()->getForm( $user, $this->context
);
239 $this->assertEquals( 12, $user->getOption( 'rclimit' ) );
245 public function testAllServiceOptionsUsed() {
246 $this->assertAllServiceOptionsUsed( [ 'EnotifMinorEdits', 'EnotifRevealEditorAddress' ] );