Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / specialpage / FormSpecialPageTestCase.php
blob51ff18b45ac05bbd4e5a4cf0d824651251c25943
1 <?php
3 namespace MediaWiki\Tests\SpecialPage;
5 use MediaWiki\Block\BlockErrorFormatter;
6 use MediaWiki\Block\DatabaseBlock;
7 use MediaWiki\DAO\WikiAwareEntity;
8 use MediaWiki\Permissions\PermissionManager;
9 use MediaWiki\SpecialPage\FormSpecialPage;
10 use MediaWiki\User\User;
11 use MediaWiki\Utils\MWTimestamp;
12 use ReflectionMethod;
13 use SpecialPageTestBase;
14 use UserBlockedError;
15 use Wikimedia\Rdbms\ReadOnlyMode;
17 /**
18 * Factory for handling the special page list and generating SpecialPage objects.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License along
31 * with this program; if not, write to the Free Software Foundation, Inc.,
32 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 * http://www.gnu.org/copyleft/gpl.html
35 * @group SpecialPage
37 abstract class FormSpecialPageTestCase extends SpecialPageTestBase {
38 /**
39 * @return FormSpecialPage
41 abstract protected function newSpecialPage();
43 /**
44 * @covers \MediaWiki\SpecialPage\FormSpecialPage::checkExecutePermissions
46 public function testCheckExecutePermissionsSitewideBlock() {
47 $blockErrorFormatter = $this->createMock( BlockErrorFormatter::class );
48 $blockErrorFormatter->method( 'getMessage' )
49 ->willReturn( $this->getMockMessage( 'test' ) );
50 $this->setService( 'BlockErrorFormatter', $blockErrorFormatter );
52 // Make the permission tests pass so we can check that the user is denied access because of their block.
53 $permissionManager = $this->createMock( PermissionManager::class );
54 $permissionManager->method( 'userHasRight' )->willReturn( true );
55 $this->setService( 'PermissionManager', $permissionManager );
57 $special = $this->newSpecialPage();
58 $checkExecutePermissions = $this->getMethod( $special, 'checkExecutePermissions' );
60 $user = $this->getMockBuilder( User::class )
61 ->onlyMethods( [ 'getBlock', 'getWikiId' ] )
62 ->getMock();
63 $user->method( 'getWikiId' )->willReturn( WikiAwareEntity::LOCAL );
64 $block = $this->createMock( DatabaseBlock::class );
65 $block->method( 'isSitewide' )->willReturn( true );
66 $block->method( 'getTargetUserIdentity' )->willReturn( $user );
67 $block->method( 'getExpiry' )->willReturn( MWTimestamp::convert( TS_MW, 10 ) );
68 $user->method( 'getBlock' )->willReturn( $block );
70 $this->expectException( UserBlockedError::class );
71 $checkExecutePermissions( $user );
74 /**
75 * @covers \MediaWiki\SpecialPage\FormSpecialPage::checkExecutePermissions
77 public function testCheckExecutePermissionsPartialBlock() {
78 $blockErrorFormatter = $this->createMock( BlockErrorFormatter::class );
79 $blockErrorFormatter->method( 'getMessage' )
80 ->willReturn( $this->getMockMessage( 'test' ) );
81 $this->setService( 'BlockErrorFormatter', $blockErrorFormatter );
83 $readOnlyMode = $this->createMock( ReadOnlyMode::class );
84 $readOnlyMode->method( 'isReadOnly' )->willReturn( false );
85 $this->setService( 'ReadOnlyMode', $readOnlyMode );
87 // Make the permission tests pass so we can check that the user is denied access because of their block.
88 $permissionManager = $this->createMock( PermissionManager::class );
89 $permissionManager->method( 'userHasRight' )->willReturn( true );
90 $this->setService( 'PermissionManager', $permissionManager );
92 $special = $this->newSpecialPage();
93 $checkExecutePermissions = $this->getMethod( $special, 'checkExecutePermissions' );
95 $user = $this->getMockBuilder( User::class )
96 ->onlyMethods( [ 'getBlock', 'getWikiId' ] )
97 ->getMock();
98 $user->method( 'getWikiId' )->willReturn( WikiAwareEntity::LOCAL );
99 $block = $this->createMock( DatabaseBlock::class );
100 $block->method( 'isSitewide' )->willReturn( false );
101 $block->method( 'getTargetUserIdentity' )->willReturn( $user );
102 $block->method( 'getExpiry' )->willReturn( MWTimestamp::convert( TS_MW, 10 ) );
103 $user->method( 'getBlock' )->willReturn( $block );
105 $this->assertNull( $checkExecutePermissions( $user ) );
109 * Get a protected/private method.
111 * @param FormSpecialPage $obj
112 * @param string $name
113 * @return callable
115 protected function getMethod( FormSpecialPage $obj, $name ) {
116 $method = new ReflectionMethod( $obj, $name );
117 $method->setAccessible( true );
118 return $method->getClosure( $obj );