Debian Codegraph @

tests/phpunit/includes/specials/SpecialUnblockTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Block\DatabaseBlock;
4 use MediaWiki\MainConfigNames;
5 use MediaWiki\Request\FauxRequest;
6 use MediaWiki\Specials\SpecialUnblock;
7 use MediaWiki\Title\Title;
8 use Wikimedia\TestingAccessWrapper;
9
10 /**
11 * @group Blocking
12 * @group Database
13 * @coversDefaultClass \MediaWiki\Specials\SpecialUnblock
14 */
15 class SpecialUnblockTest extends SpecialPageTestBase {
16 /**
17 * @inheritDoc
18 */
19 protected function newSpecialPage() {
20 $services = $this->getServiceContainer();
21 return new SpecialUnblock(
22 $services->getUnblockUserFactory(),
23 $services->getBlockUtils(),
24 $services->getDatabaseBlockStore(),
25 $services->getUserNameUtils(),
26 $services->getUserNamePrefixSearch(),
27 $services->getWatchlistManager()
28 );
29 }
30
31 /**
32 * @dataProvider provideGetFields
33 * @covers ::getFields
34 */
35 public function testGetFields( $target, $expected ) {
36 $page = TestingAccessWrapper::newFromObject( $this->newSpecialPage() );
37 $page->target = $target;
38 $page->block = new DatabaseBlock( [
39 'address' => '1.2.3.4',
40 'by' => $this->getTestSysop()->getUser(),
41 ] );
42
43 $fields = $page->getFields();
44 $this->assertIsArray( $fields );
45 foreach ( $expected as $fieldName ) {
46 $this->assertArrayHasKey( $fieldName, $fields );
47 }
48 }
49
50 public static function provideGetFields() {
51 return [
52 'No target specified' => [
53 '',
54 [ 'Target', 'Reason' ],
55 ],
56 'Target is not blocked' => [
57 '1.1.1.1',
58 [ 'Target', 'Reason' ],
59 ],
60 'Target is blocked' => [
61 '1.2.3.4',
62 [ 'Target', 'Reason', 'Name' ],
63 ],
64 ];
65 }
66
67 /**
68 * @dataProvider provideProcessUnblockErrors
69 * @covers ::execute
70 */
71 public function testProcessUnblockErrors( $options, $expected ) {
72 $performer = $this->getTestSysop()->getUser();
73
74 $target = '1.1.1.1';
75 if ( !empty( $options['block'] ) ) {
76 $block = new DatabaseBlock( [
77 'address' => $target,
78 'by' => $performer,
79 'hideName' => true,
80 ] );
81 $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
82 }
83
84 if ( !empty( $options['readOnly'] ) ) {
85 $this->overrideConfigValue( MainConfigNames::ReadOnly, true );
86 $this->expectException( ReadOnlyError::class );
87 }
88
89 if ( isset( $options['permissions'] ) ) {
90 $this->overrideUserPermissions( $performer, $options['permissions'] );
91 }
92
93 $request = new FauxRequest( [
94 'wpTarget' => $target,
95 'wpReason' => '',
96 ], true );
97 [ $html, ] = $this->executeSpecialPage( '', $request, 'qqx', $performer );
98
99 $this->assertStringContainsString( $expected, $html );
100 }
101
102 public static function provideProcessUnblockErrors() {
103 return [
104 'Target is not blocked' => [
105 [
106 'permissions' => [ 'block', 'hideuser' => true ],
107 ],
108 'ipb_cant_unblock',
109 ],
110 'Wrong permissions for unhiding user' => [
111 [
112 'block' => true,
113 'permissions' => [ 'block', 'hideuser' => false ],
114 ],
115 'unblock-hideuser',
116 ],
117 'Delete block failed' => [
118 [
119 'block' => true,
120 'permissions' => [ 'block', 'hideuser' ],
121 'readOnly' => true,
122 ],
123 'ipb_cant_unblock',
124 ],
125 ];
126 }
127
128 /**
129 * @covers ::execute
130 */
131 public function testProcessUnblockErrorsUnblockSelf() {
132 $performer = $this->getTestSysop()->getUser();
133
134 $this->overrideUserPermissions( $performer, [ 'block', 'unblockself' => false ] );
135
136 // Blocker must be different user for unblock self to be disallowed
137 $blocker = $this->getTestUser()->getUser();
138 $block = new DatabaseBlock( [
139 'by' => $blocker,
140 'address' => $performer,
141 ] );
142 $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
143
144 $request = new FauxRequest( [
145 'wpTarget' => $performer->getName(),
146 'wpReason' => '',
147 ], true );
148 [ $html, ] = $this->executeSpecialPage( '', $request, 'qqx', $performer );
149
150 $this->assertStringContainsString( 'ipbnounblockself', $html );
151 }
152
153 /**
154 * @covers ::execute
155 */
156 public function testWatched() {
157 $performer = $this->getTestSysop()->getUser();
158
159 $target = '1.2.3.4';
160 $block = new DatabaseBlock( [
161 'by' => $performer,
162 'address' => $target,
163 ] );
164 $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
165
166 $request = new FauxRequest( [
167 'wpTarget' => $target,
168 'wpReason' => '',
169 'wpWatch' => '1',
170 ], true );
171 $this->executeSpecialPage( '', $request, 'qqx', $performer );
172
173 $userPage = Title::makeTitle( NS_USER, $target );
174 $this->assertTrue( $this->getServiceContainer()->getWatchlistManager()
175 ->isWatched( $performer, $userPage ) );
176 }
177 }