MediaWiki REL1_31
DefaultPreferencesFactoryTest.php
Go to the documentation of this file.
1<?php
2
6use Wikimedia\ObjectFactory;
7use Wikimedia\TestingAccessWrapper;
8
32
34 protected $context;
35
37 protected $config;
38
39 public function setUp() {
40 parent::setUp();
41 global $wgParserConf;
42 $this->context = new RequestContext();
43 $this->context->setTitle( Title::newFromText( self::class ) );
44 $this->setMwGlobals( 'wgParser',
45 ObjectFactory::constructClassInstance( $wgParserConf['class'], [ $wgParserConf ] )
46 );
47 $this->config = MediaWikiServices::getInstance()->getMainConfig();
48 }
49
54 protected function getPreferencesFactory() {
56 $this->config,
57 new Language(),
58 AuthManager::singleton(),
59 MediaWikiServices::getInstance()->getLinkRenderer()
60 );
61 }
62
66 public function testGetForm() {
67 $testUser = $this->getTestUser();
68 $form = $this->getPreferencesFactory()->getForm( $testUser->getUser(), $this->context );
69 $this->assertInstanceOf( PreferencesForm::class, $form );
70 $this->assertCount( 5, $form->getPreferenceSections() );
71 }
72
79 public function testEmailAuthentication( $user, $cssClass ) {
80 $prefs = $this->getPreferencesFactory()->getFormDescriptor( $user, $this->context );
81 $this->assertArrayHasKey( 'cssclass', $prefs['emailauthentication'] );
82 $this->assertEquals( $cssClass, $prefs['emailauthentication']['cssclass'] );
83 }
84
85 public function emailAuthenticationProvider() {
86 $userNoEmail = new User;
87 $userEmailUnauthed = new User;
88 $userEmailUnauthed->setEmail( 'noauth@example.org' );
89 $userEmailAuthed = new User;
90 $userEmailAuthed->setEmail( 'noauth@example.org' );
91 $userEmailAuthed->setEmailAuthenticationTimestamp( wfTimestamp() );
92 return [
93 [ $userNoEmail, 'mw-email-none' ],
94 [ $userEmailUnauthed, 'mw-email-not-authenticated' ],
95 [ $userEmailAuthed, 'mw-email-authenticated' ],
96 ];
97 }
98
109 $oldOptions = [
110 'test' => 'abc',
111 'option' => 'old'
112 ];
113 $newOptions = [
114 'test' => 'abc',
115 'option' => 'new'
116 ];
117 $configMock = new HashConfig( [
118 'HiddenPrefs' => []
119 ] );
120 $form = $this->getMockBuilder( PreferencesForm::class )
121 ->disableOriginalConstructor()
122 ->getMock();
123
124 $userMock = $this->getMockBuilder( User::class )
125 ->disableOriginalConstructor()
126 ->getMock();
127 $userMock->method( 'getOptions' )
128 ->willReturn( $oldOptions );
129 $userMock->method( 'isAllowedAny' )
130 ->willReturn( true );
131 $userMock->method( 'isAllowed' )
132 ->willReturn( true );
133
134 $userMock->expects( $this->exactly( 2 ) )
135 ->method( 'setOption' )
136 ->withConsecutive(
137 [ $this->equalTo( 'test' ), $this->equalTo( $newOptions[ 'test' ] ) ],
138 [ $this->equalTo( 'option' ), $this->equalTo( $newOptions[ 'option' ] ) ]
139 );
140
141 $form->expects( $this->any() )
142 ->method( 'getModifiedUser' )
143 ->willReturn( $userMock );
144
145 $form->expects( $this->any() )
146 ->method( 'getContext' )
147 ->willReturn( $this->context );
148
149 $form->expects( $this->any() )
150 ->method( 'getConfig' )
151 ->willReturn( $configMock );
152
153 $this->setTemporaryHook( 'PreferencesFormPreSave',
154 function ( $formData, $form, $user, &$result, $oldUserOptions )
155 use ( $newOptions, $oldOptions, $userMock ) {
156 $this->assertSame( $userMock, $user );
157 foreach ( $newOptions as $option => $value ) {
158 $this->assertSame( $value, $formData[ $option ] );
159 }
160 foreach ( $oldOptions as $option => $value ) {
161 $this->assertSame( $value, $oldUserOptions[ $option ] );
162 }
163 $this->assertEquals( true, $result );
164 }
165 );
166
167 $factory = TestingAccessWrapper::newFromObject( $this->getPreferencesFactory() );
168 $factory->saveFormData( $newOptions, $form );
169 }
170
174 public function testIntvalFilter() {
175 // Test a string with leading zeros (i.e. not octal) and spaces.
176 $this->context->getRequest()->setVal( 'wprclimit', ' 0012 ' );
177 $user = new User;
178 $form = $this->getPreferencesFactory()->getForm( $user, $this->context );
179 $form->show();
180 $form->trySubmit();
181 $this->assertEquals( 12, $user->getOption( 'rclimit' ) );
182 }
183}
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition COPYING.txt:326
$wgParserConf
Parser configuration.
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:53
setEmail( $str)
Set the user's e-mail address.
Definition User.php:3055
Interface for configuration instances.
Definition Config.php:28
Interface for objects which can provide a MediaWiki context on request.