MediaWiki REL1_31
ApiBlockTest.php
Go to the documentation of this file.
1<?php
2
11 protected $mUser = null;
12
13 protected function setUp() {
14 parent::setUp();
15
16 $this->mUser = $this->getMutableTestUser()->getUser();
17 $this->setMwGlobals( 'wgBlockCIDRLimit', [
18 'IPv4' => 16,
19 'IPv6' => 19,
20 ] );
21 }
22
23 protected function tearDown() {
24 $block = Block::newFromTarget( $this->mUser->getName() );
25 if ( !is_null( $block ) ) {
26 $block->delete();
27 }
28 parent::tearDown();
29 }
30
31 protected function getTokens() {
32 return $this->getTokenList( self::$users['sysop'] );
33 }
34
40 private function doBlock( array $extraParams = [], User $blocker = null ) {
41 if ( $blocker === null ) {
42 $blocker = self::$users['sysop']->getUser();
43 }
44
45 $tokens = $this->getTokens();
46
47 $this->assertNotNull( $this->mUser, 'Sanity check' );
48
49 $this->assertArrayHasKey( 'blocktoken', $tokens, 'Sanity check' );
50
51 $params = [
52 'action' => 'block',
53 'user' => $this->mUser->getName(),
54 'reason' => 'Some reason',
55 'token' => $tokens['blocktoken'],
56 ];
57 if ( array_key_exists( 'userid', $extraParams ) ) {
58 // Make sure we don't have both user and userid
59 unset( $params['user'] );
60 }
61 $ret = $this->doApiRequest( array_merge( $params, $extraParams ), null,
62 false, $blocker );
63
64 $block = Block::newFromTarget( $this->mUser->getName() );
65
66 $this->assertTrue( !is_null( $block ), 'Block is valid' );
67
68 $this->assertSame( $this->mUser->getName(), (string)$block->getTarget() );
69 $this->assertSame( 'Some reason', $block->mReason );
70
71 return $ret;
72 }
73
77 public function testNormalBlock() {
78 $this->doBlock();
79 }
80
84 public function testBlockById() {
85 $this->doBlock( [ 'userid' => $this->mUser->getId() ] );
86 }
87
91 public function testBlockByBlockedUser() {
92 $this->setExpectedException( ApiUsageException::class,
93 'You cannot block or unblock other users because you are yourself blocked.' );
94
95 $blocked = $this->getMutableTestUser( [ 'sysop' ] )->getUser();
96 $block = new Block( [
97 'address' => $blocked->getName(),
98 'by' => self::$users['sysop']->getUser()->getId(),
99 'reason' => 'Capriciousness',
100 'timestamp' => '19370101000000',
101 'expiry' => 'infinity',
102 ] );
103 $block->insert();
104
105 $this->doBlock( [], $blocked );
106 }
107
108 public function testBlockOfNonexistentUser() {
109 $this->setExpectedException( ApiUsageException::class,
110 'There is no user by the name "Nonexistent". Check your spelling.' );
111
112 $this->doBlock( [ 'user' => 'Nonexistent' ] );
113 }
114
116 $id = 948206325;
117 $this->setExpectedException( ApiUsageException::class,
118 "There is no user with ID $id." );
119
120 $this->assertFalse( User::whoIs( $id ), 'Sanity check' );
121
122 $this->doBlock( [ 'userid' => $id ] );
123 }
124
125 public function testBlockWithTag() {
126 ChangeTags::defineTag( 'custom tag' );
127
128 $this->doBlock( [ 'tags' => 'custom tag' ] );
129
130 $dbw = wfGetDB( DB_MASTER );
131 $this->assertSame( 'custom tag', $dbw->selectField(
132 [ 'change_tag', 'logging' ],
133 'ct_tag',
134 [ 'log_type' => 'block' ],
135 __METHOD__,
136 [],
137 [ 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ] ]
138 ) );
139 }
140
141 public function testBlockWithProhibitedTag() {
142 $this->setExpectedException( ApiUsageException::class,
143 'You do not have permission to apply change tags along with your changes.' );
144
145 ChangeTags::defineTag( 'custom tag' );
146
147 $this->setMwGlobals( 'wgRevokePermissions',
148 [ 'user' => [ 'applychangetags' => true ] ] );
149
150 $this->doBlock( [ 'tags' => 'custom tag' ] );
151 }
152
153 public function testBlockWithHide() {
154 global $wgGroupPermissions;
155 $newPermissions = $wgGroupPermissions['sysop'];
156 $newPermissions['hideuser'] = true;
157 $this->mergeMwGlobalArrayValue( 'wgGroupPermissions',
158 [ 'sysop' => $newPermissions ] );
159
160 $res = $this->doBlock( [ 'hidename' => '' ] );
161
162 $dbw = wfGetDB( DB_MASTER );
163 $this->assertSame( '1', $dbw->selectField(
164 'ipblocks',
165 'ipb_deleted',
166 [ 'ipb_id' => $res[0]['block']['id'] ],
167 __METHOD__
168 ) );
169 }
170
171 public function testBlockWithProhibitedHide() {
172 $this->setExpectedException( ApiUsageException::class,
173 "You don't have permission to hide user names from the block log." );
174
175 $this->doBlock( [ 'hidename' => '' ] );
176 }
177
178 public function testBlockWithEmailBlock() {
179 $res = $this->doBlock( [ 'noemail' => '' ] );
180
181 $dbw = wfGetDB( DB_MASTER );
182 $this->assertSame( '1', $dbw->selectField(
183 'ipblocks',
184 'ipb_block_email',
185 [ 'ipb_id' => $res[0]['block']['id'] ],
186 __METHOD__
187 ) );
188 }
189
191 $this->setExpectedException( ApiUsageException::class,
192 "You don't have permission to block users from sending email through the wiki." );
193
194 $this->setMwGlobals( 'wgRevokePermissions',
195 [ 'sysop' => [ 'blockemail' => true ] ] );
196
197 $this->doBlock( [ 'noemail' => '' ] );
198 }
199
200 public function testBlockWithExpiry() {
201 $res = $this->doBlock( [ 'expiry' => '1 day' ] );
202
203 $dbw = wfGetDB( DB_MASTER );
204 $expiry = $dbw->selectField(
205 'ipblocks',
206 'ipb_expiry',
207 [ 'ipb_id' => $res[0]['block']['id'] ],
208 __METHOD__
209 );
210
211 // Allow flakiness up to one second
212 $this->assertLessThanOrEqual( 1,
213 abs( wfTimestamp( TS_UNIX, $expiry ) - ( time() + 86400 ) ) );
214 }
215
216 public function testBlockWithInvalidExpiry() {
217 $this->setExpectedException( ApiUsageException::class, "Expiry time invalid." );
218
219 $this->doBlock( [ 'expiry' => '' ] );
220 }
221
227 $this->doApiRequest(
228 [
229 'action' => 'block',
230 'user' => $this->mUser->getName(),
231 'reason' => 'Some reason',
232 ],
233 null,
234 false,
235 self::$users['sysop']->getUser()
236 );
237 }
238
239 public function testRangeBlock() {
240 $this->mUser = User::newFromName( '128.0.0.0/16', false );
241 $this->doBlock();
242 }
243
248 public function testVeryLargeRangeBlock() {
249 $this->mUser = User::newFromName( '128.0.0.0/1', false );
250 $this->doBlock();
251 }
252}
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
$wgGroupPermissions['sysop']['replacetext']
API Database medium.
testBlockByBlockedUser()
A blocked user can't block.
testBlockById()
Block by user ID.
testNormalBlock()
Block by username.
testBlockWithProhibitedHide()
doBlock(array $extraParams=[], User $blocker=null)
testBlockingActionWithNoToken()
ApiUsageException The "token" parameter must be set.
testBlockOfNonexistentUserId()
testVeryLargeRangeBlock()
ApiUsageException Range blocks larger than /16 are not allowed.
testBlockWithProhibitedEmailBlock()
getTokenList(TestUser $user, $session=null)
doApiRequest(array $params, array $session=null, $appendModule=false, User $user=null, $tokenType=null)
Does the API request and returns the result.
static newFromTarget( $specificTarget, $vagueTarget=null, $fromMaster=false)
Given a target and the target's type, get an existing Block object if possible.
Definition Block.php:1173
static defineTag( $tag)
Defines a tag in the valid_tag table, without checking that the tag name is valid.
static getMutableTestUser( $groups=[])
Convenience method for getting a mutable test user.
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:53
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:591
static whoIs( $id)
Get the username corresponding to a given user ID.
Definition User.php:863
$res
Definition database.txt:21
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:2005
$tokens
const DB_MASTER
Definition defines.php:29
$params