X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Fpreferences%2FDefaultPreferencesFactoryTest.php;h=a45944167a7fb639f45c1825c393ebe01081934d;hb=e81b3603dc2d4e149445ffb190c3b8fe1598b68d;hp=a00eb3fcd0a0699e8b03e7fe57048a6470a1d2f9;hpb=a97bac4084f4aacceaa248907b761a4a234a99a9;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/preferences/DefaultPreferencesFactoryTest.php b/tests/phpunit/includes/preferences/DefaultPreferencesFactoryTest.php index a00eb3fcd0..a45944167a 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 @@ -59,12 +61,16 @@ class DefaultPreferencesFactoryTest extends \MediaWikiTestCase { $mockNsInfo->expects( $this->never() ) ->method( $this->anythingBut( 'getValidNamespaces', '__destruct' ) ); + $mockPermissionManager = $manager ?? $this->createMock( PermissionManager::class ); + 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, + $mockPermissionManager ); } @@ -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' ] ); + } }