MediaWiki REL1_32
DefaultPreferencesFactoryTest.php
Go to the documentation of this file.
1<?php
2
6use Wikimedia\TestingAccessWrapper;
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
86 public function emailAuthenticationProvider() {
87 $userNoEmail = new User;
88 $userEmailUnauthed = new User;
89 $userEmailUnauthed->setEmail( 'noauth@example.org' );
90 $userEmailAuthed = new User;
91 $userEmailAuthed->setEmail( 'noauth@example.org' );
92 $userEmailAuthed->setEmailAuthenticationTimestamp( wfTimestamp() );
93 return [
94 [ $userNoEmail, 'mw-email-none' ],
95 [ $userEmailUnauthed, 'mw-email-not-authenticated' ],
96 [ $userEmailAuthed, 'mw-email-authenticated' ],
97 ];
98 }
99
110 $oldOptions = [
111 'test' => 'abc',
112 'option' => 'old'
113 ];
114 $newOptions = [
115 'test' => 'abc',
116 'option' => 'new'
117 ];
118 $configMock = new HashConfig( [
119 'HiddenPrefs' => []
120 ] );
121 $form = $this->getMockBuilder( PreferencesFormLegacy::class )
122 ->disableOriginalConstructor()
123 ->getMock();
124
125 $userMock = $this->getMockBuilder( User::class )
126 ->disableOriginalConstructor()
127 ->getMock();
128 $userMock->method( 'getOptions' )
129 ->willReturn( $oldOptions );
130 $userMock->method( 'isAllowedAny' )
131 ->willReturn( true );
132 $userMock->method( 'isAllowed' )
133 ->willReturn( true );
134
135 $userMock->expects( $this->exactly( 2 ) )
136 ->method( 'setOption' )
137 ->withConsecutive(
138 [ $this->equalTo( 'test' ), $this->equalTo( $newOptions[ 'test' ] ) ],
139 [ $this->equalTo( 'option' ), $this->equalTo( $newOptions[ 'option' ] ) ]
140 );
141
142 $form->expects( $this->any() )
143 ->method( 'getModifiedUser' )
144 ->willReturn( $userMock );
145
146 $form->expects( $this->any() )
147 ->method( 'getContext' )
148 ->willReturn( $this->context );
149
150 $form->expects( $this->any() )
151 ->method( 'getConfig' )
152 ->willReturn( $configMock );
153
154 $this->setTemporaryHook( 'PreferencesFormPreSave',
155 function ( $formData, $form, $user, &$result, $oldUserOptions )
156 use ( $newOptions, $oldOptions, $userMock ) {
157 $this->assertSame( $userMock, $user );
158 foreach ( $newOptions as $option => $value ) {
159 $this->assertSame( $value, $formData[ $option ] );
160 }
161 foreach ( $oldOptions as $option => $value ) {
162 $this->assertSame( $value, $oldUserOptions[ $option ] );
163 }
164 $this->assertEquals( true, $result );
165 }
166 );
167
169 $factory = TestingAccessWrapper::newFromObject( $this->getPreferencesFactory() );
170 $factory->saveFormData( $newOptions, $form, [] );
171 }
172
178 public function testIntvalFilter() {
179 // Test a string with leading zeros (i.e. not octal) and spaces.
180 $this->context->getRequest()->setVal( 'wprclimit', ' 0012 ' );
181 $user = new User;
182 $form = $this->getPreferencesFactory()->getForm( $user, $this->context );
183 $form->show();
184 $form->trySubmit();
185 $this->assertEquals( 12, $user->getOption( 'rclimit' ) );
186 }
187}
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.
testIntvalFilter()
The rclimit preference should accept non-integer input and filter it to become an integer.
A Config instance which stores all settings as a member variable.
Internationalisation code.
Definition Language.php:35
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:47
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:2335
Interface for configuration instances.
Definition Config.php:28
Interface for objects which can provide a MediaWiki context on request.