Debian Codegraph @

tests/phpunit/integration/includes/Permissions/PermissionStatusIntegrationTest.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Tests\Integration\Permissions;
22
23 use MediaWiki\Block\Block;
24 use MediaWiki\Block\BlockErrorFormatter;
25 use MediaWiki\CommentStore\CommentStoreComment;
26 use MediaWiki\Language\FormatterFactory;
27 use MediaWiki\Language\RawMessage;
28 use MediaWiki\Permissions\PermissionStatus;
29 use MediaWiki\User\UserIdentityValue;
30 use MediaWikiIntegrationTestCase;
31 use PermissionsError;
32 use ThrottledError;
33 use UserBlockedError;
34
35 /**
36 * @covers \MediaWiki\Permissions\PermissionStatus
37 */
38 class PermissionStatusIntegrationTest extends MediaWikiIntegrationTestCase {
39
40 private function makeBlockedStatus() {
41 $user = new UserIdentityValue( 1, 'Primus' );
42 $blocker = new UserIdentityValue( 2, 'Secundus' );
43
44 $block = $this->createNoOpMock(
45 Block::class,
46 [
47 'getTargetUserIdentity',
48 'getBlocker',
49 'getIdentifier',
50 'getTargetName',
51 'getReasonComment',
52 'getExpiry',
53 'getTimestamp',
54 ]
55 );
56 $block->method( 'getTargetUserIdentity' )->willReturn( $user );
57 $block->method( 'getTargetName' )->willReturn( $user->getName() );
58 $block->method( 'getBlocker' )->willReturn( $blocker );
59 $block->method( 'getIdentifier' )->willReturn( 'TEST' );
60 $block->method( 'getReasonComment' )->willReturn(
61 CommentStoreComment::newUnsavedComment( 'testing' )
62 );
63 $block->method( 'getTimestamp' )->willReturn( '20220102334455' );
64 $block->method( 'getExpiry' )->willReturn( '20250102334455' );
65
66 $status = PermissionStatus::newEmpty();
67 $status->fatal( 'blockednoreason' );
68 $status->setBlock( $block );
69 return $status;
70 }
71
72 private function assertThrowErrorPageError( PermissionStatus $status, $expected ) {
73 $this->expectExceptionMessage( $expected->getMessage() );
74
75 $status->throwErrorPageError();
76 }
77
78 public function testThrowErrorPageError_unknown() {
79 $status = PermissionStatus::newEmpty();
80 $status->fatal( 'permissionserrorstext-withaction', 'move' );
81 $expected = new PermissionsError(
82 null,
83 [ [ 'permissionserrorstext-withaction', 'move' ] ]
84 );
85
86 $this->assertThrowErrorPageError( $status, $expected );
87 }
88
89 public function testThrowErrorPageError_known() {
90 $status = PermissionStatus::newEmpty();
91 $status->setPermission( 'move' );
92 $status->fatal( 'permissionserrorstext-withaction', 'move' );
93 $expected = new PermissionsError(
94 'move',
95 [ [ 'permissionserrorstext-withaction', 'move' ] ]
96 );
97
98 $this->assertThrowErrorPageError( $status, $expected );
99 }
100
101 public function testThrowErrorPageError_blocked() {
102 $blockErrorFormatter = $this->createNoOpMock( BlockErrorFormatter::class, [ 'getMessages' ] );
103 $blockErrorFormatter->method( 'getMessages' )->willReturn( [ new RawMessage( 'testing' ) ] );
104
105 $formatterFactory = $this->createNoOpMock( FormatterFactory::class, [ 'getBlockErrorFormatter' ] );
106 $formatterFactory->method( 'getBlockErrorFormatter' )->willReturn( $blockErrorFormatter );
107
108 $this->setService( 'FormatterFactory', $formatterFactory );
109
110 $status = $this->makeBlockedStatus();
111 $block = $status->getBlock();
112 $expected = new UserBlockedError( $block );
113
114 $this->assertThrowErrorPageError( $status, $expected );
115 }
116
117 public function testThrowErrorPageError_throttled() {
118 $status = PermissionStatus::newEmpty();
119 $status->setRateLimitExceeded();
120 $expected = new ThrottledError();
121
122 $this->assertThrowErrorPageError( $status, $expected );
123 }
124
125 }