X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2FPreferencesTest.php;h=289853de57a2efb4c0a6010fb67c3bac21ac882f;hb=3df3b575c6617df64ec98533cc7141bd2314e274;hp=5841bb6f07ff51b28c7bb5cf7e1902fc45d1799a;hpb=76dc1986514cc2da47a0d3872bac8cdcb4ef80e5;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/PreferencesTest.php b/tests/phpunit/includes/PreferencesTest.php index 5841bb6f07..d78c1e7dc0 100644 --- a/tests/phpunit/includes/PreferencesTest.php +++ b/tests/phpunit/includes/PreferencesTest.php @@ -35,51 +35,122 @@ class PreferencesTest extends MediaWikiTestCase { protected function setUp() { parent::setUp(); - $this->setMwGlobals( array( + $this->setMwGlobals( [ 'wgEnableEmail' => true, 'wgEmailAuthentication' => true, - ) ); + ] ); } /** - * Placeholder to verify bug 34302 + * Placeholder to verify T36302 * @covers Preferences::profilePreferences */ - public function testEmailFieldsWhenUserHasNoEmail() { + public function testEmailAuthenticationFieldWhenUserHasNoEmail() { $prefs = $this->prefsFor( 'noemail' ); $this->assertArrayHasKey( 'cssclass', - $prefs['emailaddress'] + $prefs['emailauthentication'] ); - $this->assertEquals( 'mw-email-none', $prefs['emailaddress']['cssclass'] ); + $this->assertEquals( 'mw-email-none', $prefs['emailauthentication']['cssclass'] ); } /** - * Placeholder to verify bug 34302 + * Placeholder to verify T36302 * @covers Preferences::profilePreferences */ - public function testEmailFieldsWhenUserEmailNotAuthenticated() { + public function testEmailAuthenticationFieldWhenUserEmailNotAuthenticated() { $prefs = $this->prefsFor( 'notauth' ); $this->assertArrayHasKey( 'cssclass', - $prefs['emailaddress'] + $prefs['emailauthentication'] ); - $this->assertEquals( 'mw-email-not-authenticated', $prefs['emailaddress']['cssclass'] ); + $this->assertEquals( 'mw-email-not-authenticated', $prefs['emailauthentication']['cssclass'] ); } /** - * Placeholder to verify bug 34302 + * Placeholder to verify T36302 * @covers Preferences::profilePreferences */ - public function testEmailFieldsWhenUserEmailIsAuthenticated() { + public function testEmailAuthenticationFieldWhenUserEmailIsAuthenticated() { $prefs = $this->prefsFor( 'auth' ); $this->assertArrayHasKey( 'cssclass', - $prefs['emailaddress'] + $prefs['emailauthentication'] ); - $this->assertEquals( 'mw-email-authenticated', $prefs['emailaddress']['cssclass'] ); + $this->assertEquals( 'mw-email-authenticated', $prefs['emailauthentication']['cssclass'] ); + } + + /** + * 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 ); + + $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 ); + } + ); + + Preferences::tryFormSubmit( $newOptions, $form ); } /** Helper */ protected function prefsFor( $user_key ) { - $preferences = array(); + $preferences = []; Preferences::profilePreferences( $this->prefUsers[$user_key], $this->context,