Debian Codegraph @

tests/phpunit/includes/specialpage/FormSpecialPageTestCase.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\SpecialPage;
4
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 SpecialPageTestBase;
13 use UserBlockedError;
14 use Wikimedia\Rdbms\ReadOnlyMode;
15 use Wikimedia\TestingAccessWrapper;
16
17 /**
18 * Factory for handling the special page list and generating SpecialPage objects.
19 *
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.
24 *
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.
29 *
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
34 *
35 * @group SpecialPage
36 */
37 abstract class FormSpecialPageTestCase extends SpecialPageTestBase {
38 /**
39 * @return FormSpecialPage
40 */
41 abstract protected function newSpecialPage();
42
43 /**
44 * @covers \MediaWiki\SpecialPage\FormSpecialPage::checkExecutePermissions
45 */
46 public function testCheckExecutePermissionsSitewideBlock() {
47 $blockErrorFormatter = $this->createMock( BlockErrorFormatter::class );
48 $blockErrorFormatter->method( 'getMessage' )
49 ->willReturn( $this->getMockMessage( 'test' ) );
50 $this->setService( 'BlockErrorFormatter', $blockErrorFormatter );
51
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 );
56
57 /** @var FormSpecialPage $special */
58 $special = TestingAccessWrapper::newFromObject( $this->newSpecialPage() );
59
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 );
69
70 $this->expectException( UserBlockedError::class );
71 $special->checkExecutePermissions( $user );
72 }
73
74 /**
75 * @covers \MediaWiki\SpecialPage\FormSpecialPage::checkExecutePermissions
76 */
77 public function testCheckExecutePermissionsPartialBlock() {
78 $blockErrorFormatter = $this->createMock( BlockErrorFormatter::class );
79 $blockErrorFormatter->method( 'getMessage' )
80 ->willReturn( $this->getMockMessage( 'test' ) );
81 $this->setService( 'BlockErrorFormatter', $blockErrorFormatter );
82
83 $readOnlyMode = $this->createMock( ReadOnlyMode::class );
84 $readOnlyMode->method( 'isReadOnly' )->willReturn( false );
85 $this->setService( 'ReadOnlyMode', $readOnlyMode );
86
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 );
91
92 /** @var FormSpecialPage $special */
93 $special = TestingAccessWrapper::newFromObject( $this->newSpecialPage() );
94
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 );
104
105 $this->assertNull( $special->checkExecutePermissions( $user ) );
106 }
107 }