]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - tests/phpunit/includes/PreferencesTest.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / tests / phpunit / includes / PreferencesTest.php
1 <?php
2
3 /**
4  * @group Database
5  */
6 class PreferencesTest extends MediaWikiTestCase {
7         /**
8          * @var User[]
9          */
10         private $prefUsers;
11         /**
12          * @var RequestContext
13          */
14         private $context;
15
16         public function __construct() {
17                 parent::__construct();
18
19                 $this->prefUsers['noemail'] = new User;
20
21                 $this->prefUsers['notauth'] = new User;
22                 $this->prefUsers['notauth']
23                         ->setEmail( 'noauth@example.org' );
24
25                 $this->prefUsers['auth'] = new User;
26                 $this->prefUsers['auth']
27                         ->setEmail( 'noauth@example.org' );
28                 $this->prefUsers['auth']
29                         ->setEmailAuthenticationTimestamp( 1330946623 );
30
31                 $this->context = new RequestContext;
32                 $this->context->setTitle( Title::newFromText( 'PreferencesTest' ) );
33         }
34
35         protected function setUp() {
36                 parent::setUp();
37
38                 $this->setMwGlobals( [
39                         'wgEnableEmail' => true,
40                         'wgEmailAuthentication' => true,
41                 ] );
42         }
43
44         /**
45          * Placeholder to verify T36302
46          * @covers Preferences::profilePreferences
47          */
48         public function testEmailAuthenticationFieldWhenUserHasNoEmail() {
49                 $prefs = $this->prefsFor( 'noemail' );
50                 $this->assertArrayHasKey( 'cssclass',
51                         $prefs['emailauthentication']
52                 );
53                 $this->assertEquals( 'mw-email-none', $prefs['emailauthentication']['cssclass'] );
54         }
55
56         /**
57          * Placeholder to verify T36302
58          * @covers Preferences::profilePreferences
59          */
60         public function testEmailAuthenticationFieldWhenUserEmailNotAuthenticated() {
61                 $prefs = $this->prefsFor( 'notauth' );
62                 $this->assertArrayHasKey( 'cssclass',
63                         $prefs['emailauthentication']
64                 );
65                 $this->assertEquals( 'mw-email-not-authenticated', $prefs['emailauthentication']['cssclass'] );
66         }
67
68         /**
69          * Placeholder to verify T36302
70          * @covers Preferences::profilePreferences
71          */
72         public function testEmailAuthenticationFieldWhenUserEmailIsAuthenticated() {
73                 $prefs = $this->prefsFor( 'auth' );
74                 $this->assertArrayHasKey( 'cssclass',
75                         $prefs['emailauthentication']
76                 );
77                 $this->assertEquals( 'mw-email-authenticated', $prefs['emailauthentication']['cssclass'] );
78         }
79
80         /**
81          * Test that PreferencesFormPreSave hook has correct data:
82          *  - user Object is passed
83          *  - oldUserOptions contains previous user options (before save)
84          *  - formData and User object have set up new properties
85          *
86          * @see https://phabricator.wikimedia.org/T169365
87          * @covers Preferences::tryFormSubmit
88          */
89         public function testPreferencesFormPreSaveHookHasCorrectData() {
90                 $oldOptions = [
91                         'test' => 'abc',
92                         'option' => 'old'
93                 ];
94                 $newOptions = [
95                         'test' => 'abc',
96                         'option' => 'new'
97                 ];
98                 $configMock = new HashConfig( [
99                         'HiddenPrefs' => []
100                 ] );
101                 $form = $this->getMockBuilder( PreferencesForm::class )
102                         ->disableOriginalConstructor()
103                         ->getMock();
104
105                 $userMock = $this->getMockBuilder( User::class )
106                         ->disableOriginalConstructor()
107                         ->getMock();
108                 $userMock->method( 'getOptions' )
109                         ->willReturn( $oldOptions );
110                 $userMock->method( 'isAllowedAny' )
111                         ->willReturn( true );
112                 $userMock->method( 'isAllowed' )
113                         ->willReturn( true );
114
115                 $userMock->expects( $this->exactly( 2 ) )
116                         ->method( 'setOption' )
117                         ->withConsecutive(
118                                 [ $this->equalTo( 'test' ), $this->equalTo( $newOptions[ 'test' ] ) ],
119                                 [ $this->equalTo( 'option' ), $this->equalTo( $newOptions[ 'option' ] ) ]
120                         );
121
122                 $form->expects( $this->any() )
123                         ->method( 'getModifiedUser' )
124                         ->willReturn( $userMock );
125
126                 $form->expects( $this->any() )
127                         ->method( 'getContext' )
128                         ->willReturn( $this->context );
129
130                 $form->expects( $this->any() )
131                         ->method( 'getConfig' )
132                         ->willReturn( $configMock );
133
134                 $this->setTemporaryHook( 'PreferencesFormPreSave', function (
135                         $formData, $form, $user, &$result, $oldUserOptions )
136                         use ( $newOptions, $oldOptions, $userMock ) {
137                         $this->assertSame( $userMock, $user );
138                         foreach ( $newOptions as $option => $value ) {
139                                 $this->assertSame( $value, $formData[ $option ] );
140                         }
141                         foreach ( $oldOptions as $option => $value ) {
142                                 $this->assertSame( $value, $oldUserOptions[ $option ] );
143                         }
144                         $this->assertEquals( true, $result );
145                 }
146                 );
147
148                 Preferences::tryFormSubmit( $newOptions, $form );
149         }
150
151         /** Helper */
152         protected function prefsFor( $user_key ) {
153                 $preferences = [];
154                 Preferences::profilePreferences(
155                         $this->prefUsers[$user_key],
156                         $this->context,
157                         $preferences
158                 );
159
160                 return $preferences;
161         }
162 }