summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/includes/preferences/DefaultPreferencesFactoryTest.php
blob: c10152344b730b500d7d36babd32cac10c9f79fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php

use MediaWiki\Auth\AuthManager;
use MediaWiki\MediaWikiServices;
use MediaWiki\Preferences\DefaultPreferencesFactory;
use Wikimedia\ObjectFactory;
use Wikimedia\TestingAccessWrapper;

/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

/**
 * @group Preferences
 */
class DefaultPreferencesFactoryTest extends MediaWikiTestCase {

	/** @var IContextSource */
	protected $context;

	/** @var Config */
	protected $config;

	public function setUp() {
		parent::setUp();
		global $wgParserConf;
		$this->context = new RequestContext();
		$this->context->setTitle( Title::newFromText( self::class ) );
		$this->setMwGlobals( 'wgParser',
			ObjectFactory::constructClassInstance( $wgParserConf['class'], [ $wgParserConf ] )
		);
		$this->config = MediaWikiServices::getInstance()->getMainConfig();
	}

	/**
	 * Get a basic PreferencesFactory for testing with.
	 * @return DefaultPreferencesFactory
	 */
	protected function getPreferencesFactory() {
		return new DefaultPreferencesFactory(
			$this->config,
			new Language(),
			AuthManager::singleton(),
			MediaWikiServices::getInstance()->getLinkRenderer()
		);
	}

	/**
	 * @covers MediaWiki\Preferences\DefaultPreferencesFactory::getForm()
	 */
	public function testGetForm() {
		$testUser = $this->getTestUser();
		$form = $this->getPreferencesFactory()->getForm( $testUser->getUser(), $this->context );
		$this->assertInstanceOf( PreferencesForm::class, $form );
		$this->assertCount( 5, $form->getPreferenceSections() );
	}

	/**
	 * CSS classes for emailauthentication preference field when there's no email.
	 * @see https://phabricator.wikimedia.org/T36302
	 * @covers MediaWiki\Preferences\DefaultPreferencesFactory::profilePreferences()
	 * @dataProvider emailAuthenticationProvider
	 */
	public function testEmailAuthentication( $user, $cssClass ) {
		$prefs = $this->getPreferencesFactory()->getFormDescriptor( $user, $this->context );
		$this->assertArrayHasKey( 'cssclass', $prefs['emailauthentication'] );
		$this->assertEquals( $cssClass, $prefs['emailauthentication']['cssclass'] );
	}

	public function emailAuthenticationProvider() {
		$userNoEmail = new User;
		$userEmailUnauthed = new User;
		$userEmailUnauthed->setEmail( 'noauth@example.org' );
		$userEmailAuthed = new User;
		$userEmailAuthed->setEmail( 'noauth@example.org' );
		$userEmailAuthed->setEmailAuthenticationTimestamp( wfTimestamp() );
		return [
			[ $userNoEmail, 'mw-email-none' ],
			[ $userEmailUnauthed, 'mw-email-not-authenticated' ],
			[ $userEmailAuthed, 'mw-email-authenticated' ],
		];
	}

	/**
	 * Test that PreferencesFormPreSave hook has correct data:
	 *  - user Object is passed
	 *  - oldUserOptions contains previous user options (before save)
	 *  - formData and User object have set up new properties
	 *
	 * @see https://phabricator.wikimedia.org/T169365
	 * @covers MediaWiki\Preferences\DefaultPreferencesFactory::submitForm()
	 */
	public function testPreferencesFormPreSaveHookHasCorrectData() {
		$oldOptions = [
			'test' => 'abc',
			'option' => 'old'
		];
		$newOptions = [
			'test' => 'abc',
			'option' => 'new'
		];
		$configMock = new HashConfig( [
			'HiddenPrefs' => []
		] );
		$form = $this->getMockBuilder( PreferencesForm::class )
			->disableOriginalConstructor()
			->getMock();

		$userMock = $this->getMockBuilder( User::class )
			->disableOriginalConstructor()
			->getMock();
		$userMock->method( 'getOptions' )
			->willReturn( $oldOptions );
		$userMock->method( 'isAllowedAny' )
			->willReturn( true );
		$userMock->method( 'isAllowed' )
			->willReturn( true );

		$userMock->expects( $this->exactly( 2 ) )
			->method( 'setOption' )
			->withConsecutive(
				[ $this->equalTo( 'test' ), $this->equalTo( $newOptions[ 'test' ] ) ],
				[ $this->equalTo( 'option' ), $this->equalTo( $newOptions[ 'option' ] ) ]
			);

		$form->expects( $this->any() )
			->method( 'getModifiedUser' )
			->willReturn( $userMock );

		$form->expects( $this->any() )
			->method( 'getContext' )
			->willReturn( $this->context );

		$form->expects( $this->any() )
			->method( 'getConfig' )
			->willReturn( $configMock );

		$this->setTemporaryHook( 'PreferencesFormPreSave',
			function ( $formData, $form, $user, &$result, $oldUserOptions )
				use ( $newOptions, $oldOptions, $userMock ) {
					$this->assertSame( $userMock, $user );
					foreach ( $newOptions as $option => $value ) {
						$this->assertSame( $value, $formData[ $option ] );
					}
					foreach ( $oldOptions as $option => $value ) {
						$this->assertSame( $value, $oldUserOptions[ $option ] );
					}
					$this->assertEquals( true, $result );
			}
		);

		$factory = TestingAccessWrapper::newFromObject( $this->getPreferencesFactory() );
		$factory->saveFormData( $newOptions, $form );
	}

	/**
	 * The rclimit preference should accept non-integer input and filter it to become an integer.
	 */
	public function testIntvalFilter() {
		// Test a string with leading zeros (i.e. not octal) and spaces.
		$this->context->getRequest()->setVal( 'wprclimit', ' 0012 ' );
		$user = new User;
		$form = $this->getPreferencesFactory()->getForm( $user, $this->context );
		$form->show();
		$form->trySubmit();
		$this->assertEquals( 12, $user->getOption( 'rclimit' ) );
	}
}