MediaWiki REL1_33
DefaultPreferencesFactoryTest.php
Go to the documentation of this file.
1<?php
2
7
31
33 protected $context;
34
36 protected $config;
37
38 public function setUp() {
39 parent::setUp();
40 $this->context = new RequestContext();
41 $this->context->setTitle( Title::newFromText( self::class ) );
42
43 $services = MediaWikiServices::getInstance();
44
45 $this->setMwGlobals( 'wgParser', $services->getParserFactory()->create() );
46 $this->config = $services->getMainConfig();
47 }
48
53 protected function getPreferencesFactory() {
55 $this->config,
56 new Language(),
57 AuthManager::singleton(),
58 MediaWikiServices::getInstance()->getLinkRenderer()
59 );
60 }
61
65 public function testGetForm() {
66 $this->setTemporaryHook( 'GetPreferences', null );
67
68 $testUser = $this->getTestUser();
69 $form = $this->getPreferencesFactory()->getForm( $testUser->getUser(), $this->context );
70 $this->assertInstanceOf( PreferencesFormLegacy::class, $form );
71 $this->assertCount( 5, $form->getPreferenceSections() );
72 }
73
80 public function testEmailAuthentication( $user, $cssClass ) {
81 $prefs = $this->getPreferencesFactory()->getFormDescriptor( $user, $this->context );
82 $this->assertArrayHasKey( 'cssclass', $prefs['emailauthentication'] );
83 $this->assertEquals( $cssClass, $prefs['emailauthentication']['cssclass'] );
84 }
85
90 $userMock = $this->getMockBuilder( User::class )
91 ->disableOriginalConstructor()
92 ->getMock();
93 $userMock->method( 'isAllowed' )
94 ->willReturn( false );
95 $userMock->method( 'getEffectiveGroups' )
96 ->willReturn( [] );
97 $userMock->method( 'getGroupMemberships' )
98 ->willReturn( [] );
99 $userMock->method( 'getOptions' )
100 ->willReturn( [ 'test' => 'yes' ] );
101
102 $prefs = $this->getPreferencesFactory()->getFormDescriptor( $userMock, $this->context );
103 $this->assertArrayNotHasKey( 'showrollbackconfirmation', $prefs );
104 }
105
110 $userMock = $this->getMockBuilder( User::class )
111 ->disableOriginalConstructor()
112 ->getMock();
113 $userMock->method( 'isAllowed' )
114 ->willReturn( true );
115 $userMock->method( 'getEffectiveGroups' )
116 ->willReturn( [] );
117 $userMock->method( 'getGroupMemberships' )
118 ->willReturn( [] );
119 $userMock->method( 'getOptions' )
120 ->willReturn( [ 'test' => 'yes' ] );
121
122 $prefs = $this->getPreferencesFactory()->getFormDescriptor( $userMock, $this->context );
123 $this->assertArrayHasKey( 'showrollbackconfirmation', $prefs );
124 $this->assertEquals(
125 'rendering/advancedrendering',
126 $prefs['showrollbackconfirmation']['section']
127 );
128 }
129
130 public function emailAuthenticationProvider() {
131 $userNoEmail = new User;
132 $userEmailUnauthed = new User;
133 $userEmailUnauthed->setEmail( 'noauth@example.org' );
134 $userEmailAuthed = new User;
135 $userEmailAuthed->setEmail( 'noauth@example.org' );
136 $userEmailAuthed->setEmailAuthenticationTimestamp( wfTimestamp() );
137 return [
138 [ $userNoEmail, 'mw-email-none' ],
139 [ $userEmailUnauthed, 'mw-email-not-authenticated' ],
140 [ $userEmailAuthed, 'mw-email-authenticated' ],
141 ];
142 }
143
154 $oldOptions = [
155 'test' => 'abc',
156 'option' => 'old'
157 ];
158 $newOptions = [
159 'test' => 'abc',
160 'option' => 'new'
161 ];
162 $configMock = new HashConfig( [
163 'HiddenPrefs' => []
164 ] );
165 $form = $this->getMockBuilder( PreferencesFormLegacy::class )
166 ->disableOriginalConstructor()
167 ->getMock();
168
169 $userMock = $this->getMockBuilder( User::class )
170 ->disableOriginalConstructor()
171 ->getMock();
172 $userMock->method( 'getOptions' )
173 ->willReturn( $oldOptions );
174 $userMock->method( 'isAllowedAny' )
175 ->willReturn( true );
176 $userMock->method( 'isAllowed' )
177 ->willReturn( true );
178
179 $userMock->expects( $this->exactly( 2 ) )
180 ->method( 'setOption' )
181 ->withConsecutive(
182 [ $this->equalTo( 'test' ), $this->equalTo( $newOptions[ 'test' ] ) ],
183 [ $this->equalTo( 'option' ), $this->equalTo( $newOptions[ 'option' ] ) ]
184 );
185
186 $form->expects( $this->any() )
187 ->method( 'getModifiedUser' )
188 ->willReturn( $userMock );
189
190 $form->expects( $this->any() )
191 ->method( 'getContext' )
192 ->willReturn( $this->context );
193
194 $form->expects( $this->any() )
195 ->method( 'getConfig' )
196 ->willReturn( $configMock );
197
198 $this->setTemporaryHook( 'PreferencesFormPreSave',
199 function ( $formData, $form, $user, &$result, $oldUserOptions )
200 use ( $newOptions, $oldOptions, $userMock ) {
201 $this->assertSame( $userMock, $user );
202 foreach ( $newOptions as $option => $value ) {
203 $this->assertSame( $value, $formData[ $option ] );
204 }
205 foreach ( $oldOptions as $option => $value ) {
206 $this->assertSame( $value, $oldUserOptions[ $option ] );
207 }
208 $this->assertEquals( true, $result );
209 }
210 );
211
213 $factory = TestingAccessWrapper::newFromObject( $this->getPreferencesFactory() );
214 $factory->saveFormData( $newOptions, $form, [] );
215 }
216
222 public function testIntvalFilter() {
223 // Test a string with leading zeros (i.e. not octal) and spaces.
224 $this->context->getRequest()->setVal( 'wprclimit', ' 0012 ' );
225 $user = new User;
226 $form = $this->getPreferencesFactory()->getForm( $user, $this->context );
227 $form->show();
228 $form->trySubmit();
229 $this->assertEquals( 12, $user->getOption( 'rclimit' ) );
230 }
231}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition COPYING.txt:326
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
testGetForm()
MediaWiki\Preferences\DefaultPreferencesFactory::getForm()
testPreferencesFormPreSaveHookHasCorrectData()
Test that PreferencesFormPreSave hook has correct data:
getPreferencesFactory()
Get a basic PreferencesFactory for testing with.
testEmailAuthentication( $user, $cssClass)
CSS classes for emailauthentication preference field when there's no email.
testShowRollbackConfIsHiddenForUsersWithoutRollbackRights()
MediaWiki\Preferences\DefaultPreferencesFactory::renderingPreferences()
testIntvalFilter()
The rclimit preference should accept non-integer input and filter it to become an integer.
testShowRollbackConfIsShownForUsersWithRollbackRights()
MediaWiki\Preferences\DefaultPreferencesFactory::renderingPreferences()
A Config instance which stores all settings as a member variable.
Internationalisation code.
Definition Language.php:36
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
static getTestUser( $groups=[])
Convenience method for getting an immutable test user.
setTemporaryHook( $hookName, $handler)
Create a temporary hook handler which will be reset by tearDown.
This serves as the entry point to the authentication system.
MediaWikiServices is the service locator for the application scope of MediaWiki.
This is the default implementation of PreferencesFactory.
Group all the pieces relevant to the context of a request into one instance.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:48
setEmail( $str)
Set the user's e-mail address.
Definition User.php:3072
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place or wrap services the preferred way to define a new service is the $wgServiceWiringFiles array $services
Definition hooks.txt:2290
Interface for configuration instances.
Definition Config.php:28
Interface for objects which can provide a MediaWiki context on request.