2012-03-05 11:48:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
2012-12-09 09:27:56 +00:00
|
|
|
/**
|
|
|
|
|
* @group Database
|
|
|
|
|
*/
|
2012-03-05 11:48:35 +00:00
|
|
|
class PreferencesTest extends MediaWikiTestCase {
|
2013-10-21 21:09:13 +00:00
|
|
|
/**
|
|
|
|
|
* @var User[]
|
|
|
|
|
*/
|
2012-09-11 00:07:18 +00:00
|
|
|
private $prefUsers;
|
2013-10-21 21:09:13 +00:00
|
|
|
/**
|
|
|
|
|
* @var RequestContext
|
|
|
|
|
*/
|
2012-09-11 00:07:18 +00:00
|
|
|
private $context;
|
2012-03-05 11:48:35 +00:00
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
public function __construct() {
|
2012-03-05 11:48:35 +00:00
|
|
|
parent::__construct();
|
|
|
|
|
|
2012-09-11 00:07:18 +00:00
|
|
|
$this->prefUsers['noemail'] = new User;
|
2012-03-05 11:48:35 +00:00
|
|
|
|
2012-09-11 00:07:18 +00:00
|
|
|
$this->prefUsers['notauth'] = new User;
|
|
|
|
|
$this->prefUsers['notauth']
|
2012-03-05 11:48:35 +00:00
|
|
|
->setEmail( 'noauth@example.org' );
|
|
|
|
|
|
2013-02-14 11:22:13 +00:00
|
|
|
$this->prefUsers['auth'] = new User;
|
2012-09-11 00:07:18 +00:00
|
|
|
$this->prefUsers['auth']
|
2012-03-05 11:48:35 +00:00
|
|
|
->setEmail( 'noauth@example.org' );
|
2012-09-11 00:07:18 +00:00
|
|
|
$this->prefUsers['auth']
|
2012-03-05 11:48:35 +00:00
|
|
|
->setEmailAuthenticationTimestamp( 1330946623 );
|
|
|
|
|
|
|
|
|
|
$this->context = new RequestContext;
|
2013-02-14 11:22:13 +00:00
|
|
|
$this->context->setTitle( Title::newFromText( 'PreferencesTest' ) );
|
2012-10-08 10:56:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function setUp() {
|
|
|
|
|
parent::setUp();
|
2012-04-30 07:29:45 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
2013-05-11 20:14:18 +00:00
|
|
|
'wgEnableEmail' => true,
|
|
|
|
|
'wgEmailAuthentication' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2012-03-05 11:48:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-02-20 23:45:58 +00:00
|
|
|
* Placeholder to verify T36302
|
2012-03-05 11:48:35 +00:00
|
|
|
* @covers Preferences::profilePreferences
|
|
|
|
|
*/
|
2015-12-13 00:56:47 +00:00
|
|
|
public function testEmailAuthenticationFieldWhenUserHasNoEmail() {
|
2012-03-05 11:48:35 +00:00
|
|
|
$prefs = $this->prefsFor( 'noemail' );
|
2012-03-07 09:56:24 +00:00
|
|
|
$this->assertArrayHasKey( 'cssclass',
|
2015-12-13 00:56:47 +00:00
|
|
|
$prefs['emailauthentication']
|
2012-03-05 11:48:35 +00:00
|
|
|
);
|
2015-12-13 00:56:47 +00:00
|
|
|
$this->assertEquals( 'mw-email-none', $prefs['emailauthentication']['cssclass'] );
|
2012-03-05 11:48:35 +00:00
|
|
|
}
|
2013-02-14 11:22:13 +00:00
|
|
|
|
2012-03-05 11:48:35 +00:00
|
|
|
/**
|
2017-02-20 23:45:58 +00:00
|
|
|
* Placeholder to verify T36302
|
2012-03-05 11:48:35 +00:00
|
|
|
* @covers Preferences::profilePreferences
|
|
|
|
|
*/
|
2015-12-13 00:56:47 +00:00
|
|
|
public function testEmailAuthenticationFieldWhenUserEmailNotAuthenticated() {
|
2012-03-05 11:48:35 +00:00
|
|
|
$prefs = $this->prefsFor( 'notauth' );
|
2012-03-07 09:56:24 +00:00
|
|
|
$this->assertArrayHasKey( 'cssclass',
|
2015-12-13 00:56:47 +00:00
|
|
|
$prefs['emailauthentication']
|
2012-03-05 11:48:35 +00:00
|
|
|
);
|
2015-12-13 00:56:47 +00:00
|
|
|
$this->assertEquals( 'mw-email-not-authenticated', $prefs['emailauthentication']['cssclass'] );
|
2012-03-05 11:48:35 +00:00
|
|
|
}
|
2013-02-14 11:22:13 +00:00
|
|
|
|
2012-03-05 11:48:35 +00:00
|
|
|
/**
|
2017-02-20 23:45:58 +00:00
|
|
|
* Placeholder to verify T36302
|
2012-03-05 11:48:35 +00:00
|
|
|
* @covers Preferences::profilePreferences
|
|
|
|
|
*/
|
2015-12-13 00:56:47 +00:00
|
|
|
public function testEmailAuthenticationFieldWhenUserEmailIsAuthenticated() {
|
2012-03-05 11:48:35 +00:00
|
|
|
$prefs = $this->prefsFor( 'auth' );
|
2012-03-07 09:56:24 +00:00
|
|
|
$this->assertArrayHasKey( 'cssclass',
|
2015-12-13 00:56:47 +00:00
|
|
|
$prefs['emailauthentication']
|
2012-03-05 11:48:35 +00:00
|
|
|
);
|
2015-12-13 00:56:47 +00:00
|
|
|
$this->assertEquals( 'mw-email-authenticated', $prefs['emailauthentication']['cssclass'] );
|
2012-03-05 11:48:35 +00:00
|
|
|
}
|
|
|
|
|
|
2017-07-04 18:30:45 +00:00
|
|
|
/**
|
|
|
|
|
* 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 Preferences::tryFormSubmit
|
|
|
|
|
*/
|
|
|
|
|
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 );
|
|
|
|
|
|
2017-07-23 01:24:09 +00:00
|
|
|
$this->setTemporaryHook( 'PreferencesFormPreSave', function (
|
2017-07-04 18:30:45 +00:00
|
|
|
$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 );
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Preferences::tryFormSubmit( $newOptions, $form );
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-05 11:48:35 +00:00
|
|
|
/** Helper */
|
2013-10-21 21:09:13 +00:00
|
|
|
protected function prefsFor( $user_key ) {
|
2017-11-13 16:42:04 +00:00
|
|
|
return Preferences::getPreferences(
|
2013-12-01 19:58:51 +00:00
|
|
|
$this->prefUsers[$user_key],
|
2017-11-13 16:42:04 +00:00
|
|
|
$this->context
|
2012-03-05 11:48:35 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|