X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Fpreferences%2FDefaultPreferencesFactoryTest.php;h=c7a3fe60453ebb7d7273d2cc705da620e35f1e29;hp=a00eb3fcd0a0699e8b03e7fe57048a6470a1d2f9;hb=af80076034fb734d652eb043c523c1d8df974e51;hpb=956ffb9cd77bb9748cf7bc425d50f0eea7e3abdd diff --git a/tests/phpunit/includes/preferences/DefaultPreferencesFactoryTest.php b/tests/phpunit/includes/preferences/DefaultPreferencesFactoryTest.php index a00eb3fcd0..c7a3fe6045 100644 --- a/tests/phpunit/includes/preferences/DefaultPreferencesFactoryTest.php +++ b/tests/phpunit/includes/preferences/DefaultPreferencesFactoryTest.php @@ -1,8 +1,8 @@ createMock( NamespaceInfo::class ); $mockNsInfo->method( 'getValidNamespaces' )->willReturn( [ NS_MAIN, NS_TALK, NS_USER, NS_USER_TALK @@ -60,11 +62,13 @@ class DefaultPreferencesFactoryTest extends \MediaWikiTestCase { ->method( $this->anythingBut( 'getValidNamespaces', '__destruct' ) ); return new DefaultPreferencesFactory( - new ServiceOptions( DefaultPreferencesFactory::$constructorOptions, $this->config ), + new LoggedServiceOptions( self::$serviceOptionsAccessLog, + DefaultPreferencesFactory::$constructorOptions, $this->config ), new Language(), AuthManager::singleton(), MediaWikiServices::getInstance()->getLinkRenderer(), - $mockNsInfo + $mockNsInfo, + $mockPM ); } @@ -75,7 +79,9 @@ class DefaultPreferencesFactoryTest extends \MediaWikiTestCase { $this->setTemporaryHook( 'GetPreferences', null ); $testUser = $this->getTestUser(); - $form = $this->getPreferencesFactory()->getForm( $testUser->getUser(), $this->context ); + $pm = $this->createMock( PermissionManager::class ); + $pm->method( 'userHasRight' )->willReturn( true ); + $form = $this->getPreferencesFactory( $pm )->getForm( $testUser->getUser(), $this->context ); $this->assertInstanceOf( PreferencesFormOOUI::class, $form ); $this->assertCount( 5, $form->getPreferenceSections() ); } @@ -87,7 +93,9 @@ class DefaultPreferencesFactoryTest extends \MediaWikiTestCase { * @dataProvider emailAuthenticationProvider */ public function testEmailAuthentication( $user, $cssClass ) { - $prefs = $this->getPreferencesFactory()->getFormDescriptor( $user, $this->context ); + $pm = $this->createMock( PermissionManager::class ); + $pm->method( 'userHasRight' )->willReturn( true ); + $prefs = $this->getPreferencesFactory( $pm )->getFormDescriptor( $user, $this->context ); $this->assertArrayHasKey( 'cssclass', $prefs['emailauthentication'] ); $this->assertEquals( $cssClass, $prefs['emailauthentication']['cssclass'] ); } @@ -99,16 +107,18 @@ class DefaultPreferencesFactoryTest extends \MediaWikiTestCase { $userMock = $this->getMockBuilder( User::class ) ->disableOriginalConstructor() ->getMock(); - $userMock->method( 'isAllowed' ) - ->willReturn( false ); $userMock->method( 'getEffectiveGroups' ) ->willReturn( [] ); $userMock->method( 'getGroupMemberships' ) ->willReturn( [] ); $userMock->method( 'getOptions' ) ->willReturn( [ 'test' => 'yes' ] ); - - $prefs = $this->getPreferencesFactory()->getFormDescriptor( $userMock, $this->context ); + $pm = $this->createMock( PermissionManager::class ); + $pm->method( 'userHasRight' ) + ->will( $this->returnValueMap( [ + [ $userMock, 'editmyoptions', true ] + ] ) ); + $prefs = $this->getPreferencesFactory( $pm )->getFormDescriptor( $userMock, $this->context ); $this->assertArrayNotHasKey( 'showrollbackconfirmation', $prefs ); } @@ -119,16 +129,19 @@ class DefaultPreferencesFactoryTest extends \MediaWikiTestCase { $userMock = $this->getMockBuilder( User::class ) ->disableOriginalConstructor() ->getMock(); - $userMock->method( 'isAllowed' ) - ->willReturn( true ); $userMock->method( 'getEffectiveGroups' ) ->willReturn( [] ); $userMock->method( 'getGroupMemberships' ) ->willReturn( [] ); $userMock->method( 'getOptions' ) ->willReturn( [ 'test' => 'yes' ] ); - - $prefs = $this->getPreferencesFactory()->getFormDescriptor( $userMock, $this->context ); + $pm = $this->createMock( PermissionManager::class ); + $pm->method( 'userHasRight' ) + ->will( $this->returnValueMap( [ + [ $userMock, 'editmyoptions', true ], + [ $userMock, 'rollback', true ] + ] ) ); + $prefs = $this->getPreferencesFactory( $pm )->getFormDescriptor( $userMock, $this->context ); $this->assertArrayHasKey( 'showrollbackconfirmation', $prefs ); $this->assertEquals( 'rendering/advancedrendering', @@ -180,10 +193,6 @@ class DefaultPreferencesFactoryTest extends \MediaWikiTestCase { ->getMock(); $userMock->method( 'getOptions' ) ->willReturn( $oldOptions ); - $userMock->method( 'isAllowedAny' ) - ->willReturn( true ); - $userMock->method( 'isAllowed' ) - ->willReturn( true ); $userMock->expects( $this->exactly( 2 ) ) ->method( 'setOption' ) @@ -192,18 +201,25 @@ class DefaultPreferencesFactoryTest extends \MediaWikiTestCase { [ $this->equalTo( 'option' ), $this->equalTo( $newOptions[ 'option' ] ) ] ); - $form->expects( $this->any() ) - ->method( 'getModifiedUser' ) + $form->method( 'getModifiedUser' ) ->willReturn( $userMock ); - $form->expects( $this->any() ) - ->method( 'getContext' ) + $form->method( 'getContext' ) ->willReturn( $this->context ); - $form->expects( $this->any() ) - ->method( 'getConfig' ) + $form->method( 'getConfig' ) ->willReturn( $configMock ); + $pm = $this->createMock( PermissionManager::class ); + $pm->method( 'userHasAnyRight' ) + ->will( $this->returnValueMap( [ + [ $userMock, 'editmyprivateinfo', 'editmyoptions', true ] + ] ) ); + $pm->method( 'userHasRight' ) + ->will( $this->returnValueMap( [ + [ $userMock, 'editmyoptions', true ] + ] ) ); + $this->setTemporaryHook( 'PreferencesFormPreSave', function ( $formData, $form, $user, &$result, $oldUserOptions ) use ( $newOptions, $oldOptions, $userMock ) { @@ -219,7 +235,7 @@ class DefaultPreferencesFactoryTest extends \MediaWikiTestCase { ); /** @var DefaultPreferencesFactory $factory */ - $factory = TestingAccessWrapper::newFromObject( $this->getPreferencesFactory() ); + $factory = TestingAccessWrapper::newFromObject( $this->getPreferencesFactory( $pm ) ); $factory->saveFormData( $newOptions, $form, [] ); } @@ -232,9 +248,23 @@ class DefaultPreferencesFactoryTest extends \MediaWikiTestCase { // 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 ); + $pm = $this->createMock( PermissionManager::class ); + $pm->method( 'userHasAnyRight' ) + ->willReturn( true ); + $pm->method( 'userHasRight' ) + ->will( $this->returnValueMap( [ + [ $user, 'editmyoptions', true ] + ] ) ); + $form = $this->getPreferencesFactory( $pm )->getForm( $user, $this->context ); $form->show(); $form->trySubmit(); $this->assertEquals( 12, $user->getOption( 'rclimit' ) ); } + + /** + * @coversNothing + */ + public function testAllServiceOptionsUsed() { + $this->assertAllServiceOptionsUsed( [ 'EnotifMinorEdits', 'EnotifRevealEditorAddress' ] ); + } }