Convert Preferences class into PreferencesFactory service
[lhc/web/wiklou.git] / tests / phpunit / includes / preferences / DefaultPreferencesFactoryTest.php
1 <?php
2
3 use MediaWiki\Auth\AuthManager;
4 use MediaWiki\MediaWikiServices;
5 use MediaWiki\Preferences\DefaultPreferencesFactory;
6 use Wikimedia\TestingAccessWrapper;
7
8 /**
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
28 * @group Preferences
29 */
30 class DefaultPreferencesFactoryTest extends MediaWikiTestCase {
31
32 /** @var IContextSource */
33 protected $context;
34
35 /** @var Config */
36 protected $config;
37
38 public function setUp() {
39 parent::setUp();
40 global $wgParserConf;
41 $this->context = new RequestContext();
42 $this->context->setTitle( Title::newFromText( self::class ) );
43 $this->setMwGlobals( 'wgParser',
44 ObjectFactory::constructClassInstance( $wgParserConf['class'], [ $wgParserConf ] )
45 );
46 $this->config = MediaWikiServices::getInstance()->getMainConfig();
47 }
48
49 /**
50 * Get a basic PreferencesFactory for testing with.
51 * @return DefaultPreferencesFactory
52 */
53 protected function getPreferencesFactory() {
54 return new DefaultPreferencesFactory(
55 $this->config,
56 new Language(),
57 AuthManager::singleton(),
58 MediaWikiServices::getInstance()->getLinkRenderer()
59 );
60 }
61
62 /**
63 * @covers MediaWiki\Preferences\DefaultPreferencesFactory::getForm()
64 */
65 public function testGetForm() {
66 $testUser = $this->getTestUser();
67 $form = $this->getPreferencesFactory()->getForm( $testUser->getUser(), $this->context );
68 $this->assertInstanceOf( PreferencesForm::class, $form );
69 $this->assertCount( 5, $form->getPreferenceSections() );
70 }
71
72 /**
73 * CSS classes for emailauthentication preference field when there's no email.
74 * @see https://phabricator.wikimedia.org/T36302
75 * @covers MediaWiki\Preferences\DefaultPreferencesFactory::profilePreferences()
76 * @dataProvider emailAuthenticationProvider
77 */
78 public function testEmailAuthentication( $user, $cssClass ) {
79 $prefs = $this->getPreferencesFactory()->getFormDescriptor( $user, $this->context );
80 $this->assertArrayHasKey( 'cssclass', $prefs['emailauthentication'] );
81 $this->assertEquals( $cssClass, $prefs['emailauthentication']['cssclass'] );
82 }
83
84 public function emailAuthenticationProvider() {
85 $userNoEmail = new User;
86 $userEmailUnauthed = new User;
87 $userEmailUnauthed->setEmail( 'noauth@example.org' );
88 $userEmailAuthed = new User;
89 $userEmailAuthed->setEmail( 'noauth@example.org' );
90 $userEmailAuthed->setEmailAuthenticationTimestamp( wfTimestamp() );
91 return [
92 [ $userNoEmail, 'mw-email-none' ],
93 [ $userEmailUnauthed, 'mw-email-not-authenticated' ],
94 [ $userEmailAuthed, 'mw-email-authenticated' ],
95 ];
96 }
97
98 /**
99 * Test that PreferencesFormPreSave hook has correct data:
100 * - user Object is passed
101 * - oldUserOptions contains previous user options (before save)
102 * - formData and User object have set up new properties
103 *
104 * @see https://phabricator.wikimedia.org/T169365
105 * @covers MediaWiki\Preferences\DefaultPreferencesFactory::submitForm()
106 */
107 public function testPreferencesFormPreSaveHookHasCorrectData() {
108 $oldOptions = [
109 'test' => 'abc',
110 'option' => 'old'
111 ];
112 $newOptions = [
113 'test' => 'abc',
114 'option' => 'new'
115 ];
116 $configMock = new HashConfig( [
117 'HiddenPrefs' => []
118 ] );
119 $form = $this->getMockBuilder( PreferencesForm::class )
120 ->disableOriginalConstructor()
121 ->getMock();
122
123 $userMock = $this->getMockBuilder( User::class )
124 ->disableOriginalConstructor()
125 ->getMock();
126 $userMock->method( 'getOptions' )
127 ->willReturn( $oldOptions );
128 $userMock->method( 'isAllowedAny' )
129 ->willReturn( true );
130 $userMock->method( 'isAllowed' )
131 ->willReturn( true );
132
133 $userMock->expects( $this->exactly( 2 ) )
134 ->method( 'setOption' )
135 ->withConsecutive(
136 [ $this->equalTo( 'test' ), $this->equalTo( $newOptions[ 'test' ] ) ],
137 [ $this->equalTo( 'option' ), $this->equalTo( $newOptions[ 'option' ] ) ]
138 );
139
140 $form->expects( $this->any() )
141 ->method( 'getModifiedUser' )
142 ->willReturn( $userMock );
143
144 $form->expects( $this->any() )
145 ->method( 'getContext' )
146 ->willReturn( $this->context );
147
148 $form->expects( $this->any() )
149 ->method( 'getConfig' )
150 ->willReturn( $configMock );
151
152 $this->setTemporaryHook( 'PreferencesFormPreSave',
153 function ( $formData, $form, $user, &$result, $oldUserOptions )
154 use ( $newOptions, $oldOptions, $userMock ) {
155 $this->assertSame( $userMock, $user );
156 foreach ( $newOptions as $option => $value ) {
157 $this->assertSame( $value, $formData[ $option ] );
158 }
159 foreach ( $oldOptions as $option => $value ) {
160 $this->assertSame( $value, $oldUserOptions[ $option ] );
161 }
162 $this->assertEquals( true, $result );
163 }
164 );
165
166 $factory = TestingAccessWrapper::newFromObject( $this->getPreferencesFactory() );
167 $factory->saveFormData( $newOptions, $form );
168 }
169
170 /**
171 * The rclimit preference should accept non-integer input and filter it to become an integer.
172 */
173 public function testIntvalFilter() {
174 // Test a string with leading zeros (i.e. not octal) and spaces.
175 $this->context->getRequest()->setVal( 'wprclimit', ' 0012 ' );
176 $user = new User;
177 $form = $this->getPreferencesFactory()->getForm( $user, $this->context );
178 $form->show();
179 $form->trySubmit();
180 $this->assertEquals( 12, $user->getOption( 'rclimit' ) );
181 }
182 }