Merge "Make JSON styling available on mobile"
[lhc/web/wiklou.git] / tests / phpunit / includes / preferences / DefaultPreferencesFactoryTest.php
1 <?php
2
3 use MediaWiki\Auth\AuthManager;
4 use MediaWiki\Config\ServiceOptions;
5 use MediaWiki\MediaWikiServices;
6 use MediaWiki\Preferences\DefaultPreferencesFactory;
7 use Wikimedia\TestingAccessWrapper;
8
9 /**
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @file
26 */
27
28 /**
29 * @group Preferences
30 */
31 class DefaultPreferencesFactoryTest extends \MediaWikiTestCase {
32
33 /** @var IContextSource */
34 protected $context;
35
36 /** @var Config */
37 protected $config;
38
39 public function setUp() {
40 parent::setUp();
41 $this->context = new RequestContext();
42 $this->context->setTitle( Title::newFromText( self::class ) );
43
44 $services = MediaWikiServices::getInstance();
45
46 $this->setMwGlobals( 'wgParser', $services->getParserFactory()->create() );
47 $this->config = $services->getMainConfig();
48 }
49
50 /**
51 * Get a basic PreferencesFactory for testing with.
52 * @return DefaultPreferencesFactory
53 */
54 protected function getPreferencesFactory() {
55 return new DefaultPreferencesFactory(
56 new ServiceOptions( DefaultPreferencesFactory::$constructorOptions, $this->config ),
57 new Language(),
58 AuthManager::singleton(),
59 MediaWikiServices::getInstance()->getLinkRenderer()
60 );
61 }
62
63 /**
64 * @covers MediaWiki\Preferences\DefaultPreferencesFactory::getForm()
65 */
66 public function testGetForm() {
67 $this->setTemporaryHook( 'GetPreferences', null );
68
69 $testUser = $this->getTestUser();
70 $form = $this->getPreferencesFactory()->getForm( $testUser->getUser(), $this->context );
71 $this->assertInstanceOf( PreferencesFormOOUI::class, $form );
72 $this->assertCount( 5, $form->getPreferenceSections() );
73 }
74
75 /**
76 * CSS classes for emailauthentication preference field when there's no email.
77 * @see https://phabricator.wikimedia.org/T36302
78 * @covers MediaWiki\Preferences\DefaultPreferencesFactory::profilePreferences()
79 * @dataProvider emailAuthenticationProvider
80 */
81 public function testEmailAuthentication( $user, $cssClass ) {
82 $prefs = $this->getPreferencesFactory()->getFormDescriptor( $user, $this->context );
83 $this->assertArrayHasKey( 'cssclass', $prefs['emailauthentication'] );
84 $this->assertEquals( $cssClass, $prefs['emailauthentication']['cssclass'] );
85 }
86
87 /**
88 * @covers MediaWiki\Preferences\DefaultPreferencesFactory::renderingPreferences()
89 */
90 public function testShowRollbackConfIsHiddenForUsersWithoutRollbackRights() {
91 $userMock = $this->getMockBuilder( User::class )
92 ->disableOriginalConstructor()
93 ->getMock();
94 $userMock->method( 'isAllowed' )
95 ->willReturn( false );
96 $userMock->method( 'getEffectiveGroups' )
97 ->willReturn( [] );
98 $userMock->method( 'getGroupMemberships' )
99 ->willReturn( [] );
100 $userMock->method( 'getOptions' )
101 ->willReturn( [ 'test' => 'yes' ] );
102
103 $prefs = $this->getPreferencesFactory()->getFormDescriptor( $userMock, $this->context );
104 $this->assertArrayNotHasKey( 'showrollbackconfirmation', $prefs );
105 }
106
107 /**
108 * @covers MediaWiki\Preferences\DefaultPreferencesFactory::renderingPreferences()
109 */
110 public function testShowRollbackConfIsShownForUsersWithRollbackRights() {
111 $userMock = $this->getMockBuilder( User::class )
112 ->disableOriginalConstructor()
113 ->getMock();
114 $userMock->method( 'isAllowed' )
115 ->willReturn( true );
116 $userMock->method( 'getEffectiveGroups' )
117 ->willReturn( [] );
118 $userMock->method( 'getGroupMemberships' )
119 ->willReturn( [] );
120 $userMock->method( 'getOptions' )
121 ->willReturn( [ 'test' => 'yes' ] );
122
123 $prefs = $this->getPreferencesFactory()->getFormDescriptor( $userMock, $this->context );
124 $this->assertArrayHasKey( 'showrollbackconfirmation', $prefs );
125 $this->assertEquals(
126 'rendering/advancedrendering',
127 $prefs['showrollbackconfirmation']['section']
128 );
129 }
130
131 public function emailAuthenticationProvider() {
132 $userNoEmail = new User;
133 $userEmailUnauthed = new User;
134 $userEmailUnauthed->setEmail( 'noauth@example.org' );
135 $userEmailAuthed = new User;
136 $userEmailAuthed->setEmail( 'noauth@example.org' );
137 $userEmailAuthed->setEmailAuthenticationTimestamp( wfTimestamp() );
138 return [
139 [ $userNoEmail, 'mw-email-none' ],
140 [ $userEmailUnauthed, 'mw-email-not-authenticated' ],
141 [ $userEmailAuthed, 'mw-email-authenticated' ],
142 ];
143 }
144
145 /**
146 * Test that PreferencesFormPreSave hook has correct data:
147 * - user Object is passed
148 * - oldUserOptions contains previous user options (before save)
149 * - formData and User object have set up new properties
150 *
151 * @see https://phabricator.wikimedia.org/T169365
152 * @covers MediaWiki\Preferences\DefaultPreferencesFactory::submitForm()
153 */
154 public function testPreferencesFormPreSaveHookHasCorrectData() {
155 $oldOptions = [
156 'test' => 'abc',
157 'option' => 'old'
158 ];
159 $newOptions = [
160 'test' => 'abc',
161 'option' => 'new'
162 ];
163 $configMock = new HashConfig( [
164 'HiddenPrefs' => []
165 ] );
166 $form = $this->getMockBuilder( PreferencesFormOOUI::class )
167 ->disableOriginalConstructor()
168 ->getMock();
169
170 $userMock = $this->getMockBuilder( User::class )
171 ->disableOriginalConstructor()
172 ->getMock();
173 $userMock->method( 'getOptions' )
174 ->willReturn( $oldOptions );
175 $userMock->method( 'isAllowedAny' )
176 ->willReturn( true );
177 $userMock->method( 'isAllowed' )
178 ->willReturn( true );
179
180 $userMock->expects( $this->exactly( 2 ) )
181 ->method( 'setOption' )
182 ->withConsecutive(
183 [ $this->equalTo( 'test' ), $this->equalTo( $newOptions[ 'test' ] ) ],
184 [ $this->equalTo( 'option' ), $this->equalTo( $newOptions[ 'option' ] ) ]
185 );
186
187 $form->expects( $this->any() )
188 ->method( 'getModifiedUser' )
189 ->willReturn( $userMock );
190
191 $form->expects( $this->any() )
192 ->method( 'getContext' )
193 ->willReturn( $this->context );
194
195 $form->expects( $this->any() )
196 ->method( 'getConfig' )
197 ->willReturn( $configMock );
198
199 $this->setTemporaryHook( 'PreferencesFormPreSave',
200 function ( $formData, $form, $user, &$result, $oldUserOptions )
201 use ( $newOptions, $oldOptions, $userMock ) {
202 $this->assertSame( $userMock, $user );
203 foreach ( $newOptions as $option => $value ) {
204 $this->assertSame( $value, $formData[ $option ] );
205 }
206 foreach ( $oldOptions as $option => $value ) {
207 $this->assertSame( $value, $oldUserOptions[ $option ] );
208 }
209 $this->assertEquals( true, $result );
210 }
211 );
212
213 /** @var DefaultPreferencesFactory $factory */
214 $factory = TestingAccessWrapper::newFromObject( $this->getPreferencesFactory() );
215 $factory->saveFormData( $newOptions, $form, [] );
216 }
217
218 /**
219 * The rclimit preference should accept non-integer input and filter it to become an integer.
220 *
221 * @covers \MediaWiki\Preferences\DefaultPreferencesFactory::saveFormData
222 */
223 public function testIntvalFilter() {
224 // Test a string with leading zeros (i.e. not octal) and spaces.
225 $this->context->getRequest()->setVal( 'wprclimit', ' 0012 ' );
226 $user = new User;
227 $form = $this->getPreferencesFactory()->getForm( $user, $this->context );
228 $form->show();
229 $form->trySubmit();
230 $this->assertEquals( 12, $user->getOption( 'rclimit' ) );
231 }
232 }