MediaWiki REL1_32
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 $this->tablesUsed = array_merge(
16 $this->tablesUsed,
17 [ 'ipblocks', 'change_tag', 'change_tag_def', 'logging' ]
18 );
19
20 $this->mUser = $this->getMutableTestUser()->getUser();
21 $this->setMwGlobals( 'wgBlockCIDRLimit', [
22 'IPv4' => 16,
23 'IPv6' => 19,
24 ] );
25 }
26
27 protected function getTokens() {
28 return $this->getTokenList( self::$users['sysop'] );
29 }
30
36 private function doBlock( array $extraParams = [], User $blocker = null ) {
37 if ( $blocker === null ) {
38 $blocker = self::$users['sysop']->getUser();
39 }
40
41 $tokens = $this->getTokens();
42
43 $this->assertNotNull( $this->mUser, 'Sanity check' );
44
45 $this->assertArrayHasKey( 'blocktoken', $tokens, 'Sanity check' );
46
47 $params = [
48 'action' => 'block',
49 'user' => $this->mUser->getName(),
50 'reason' => 'Some reason',
51 'token' => $tokens['blocktoken'],
52 ];
53 if ( array_key_exists( 'userid', $extraParams ) ) {
54 // Make sure we don't have both user and userid
55 unset( $params['user'] );
56 }
57 $ret = $this->doApiRequest( array_merge( $params, $extraParams ), null,
58 false, $blocker );
59
60 $block = Block::newFromTarget( $this->mUser->getName() );
61
62 $this->assertTrue( !is_null( $block ), 'Block is valid' );
63
64 $this->assertSame( $this->mUser->getName(), (string)$block->getTarget() );
65 $this->assertSame( 'Some reason', $block->mReason );
66
67 return $ret;
68 }
69
73 public function testNormalBlock() {
74 $this->doBlock();
75 }
76
80 public function testBlockById() {
81 $this->doBlock( [ 'userid' => $this->mUser->getId() ] );
82 }
83
87 public function testBlockByBlockedUser() {
88 $this->setExpectedException( ApiUsageException::class,
89 'You cannot block or unblock other users because you are yourself blocked.' );
90
91 $blocked = $this->getMutableTestUser( [ 'sysop' ] )->getUser();
92 $block = new Block( [
93 'address' => $blocked->getName(),
94 'by' => self::$users['sysop']->getUser()->getId(),
95 'reason' => 'Capriciousness',
96 'timestamp' => '19370101000000',
97 'expiry' => 'infinity',
98 ] );
99 $block->insert();
100
101 $this->doBlock( [], $blocked );
102 }
103
104 public function testBlockOfNonexistentUser() {
105 $this->setExpectedException( ApiUsageException::class,
106 'There is no user by the name "Nonexistent". Check your spelling.' );
107
108 $this->doBlock( [ 'user' => 'Nonexistent' ] );
109 }
110
112 $id = 948206325;
113 $this->setExpectedException( ApiUsageException::class,
114 "There is no user with ID $id." );
115
116 $this->assertFalse( User::whoIs( $id ), 'Sanity check' );
117
118 $this->doBlock( [ 'userid' => $id ] );
119 }
120
121 public function testBlockWithTag() {
122 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_WRITE_BOTH );
123 ChangeTags::defineTag( 'custom tag' );
124
125 $this->doBlock( [ 'tags' => 'custom tag' ] );
126
127 $dbw = wfGetDB( DB_MASTER );
128 $this->assertSame( 1, (int)$dbw->selectField(
129 [ 'change_tag', 'logging' ],
130 'COUNT(*)',
131 [ 'log_type' => 'block', 'ct_tag' => 'custom tag' ],
132 __METHOD__,
133 [],
134 [ 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ] ]
135 ) );
136 }
137
138 public function testBlockWithTagNewBackend() {
139 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_NEW );
140 ChangeTags::defineTag( 'custom tag' );
141
142 $this->doBlock( [ 'tags' => 'custom tag' ] );
143
144 $dbw = wfGetDB( DB_MASTER );
145 $this->assertSame( 1, (int)$dbw->selectField(
146 [ 'change_tag', 'logging', 'change_tag_def' ],
147 'COUNT(*)',
148 [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ],
149 __METHOD__,
150 [],
151 [
152 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ],
153 'change_tag_def' => [ 'INNER JOIN', 'ctd_id = ct_tag_id' ],
154 ]
155 ) );
156 }
157
158 public function testBlockWithProhibitedTag() {
159 $this->setExpectedException( ApiUsageException::class,
160 'You do not have permission to apply change tags along with your changes.' );
161
162 ChangeTags::defineTag( 'custom tag' );
163
164 $this->setMwGlobals( 'wgRevokePermissions',
165 [ 'user' => [ 'applychangetags' => true ] ] );
166
167 $this->doBlock( [ 'tags' => 'custom tag' ] );
168 }
169
170 public function testBlockWithHide() {
171 global $wgGroupPermissions;
172 $newPermissions = $wgGroupPermissions['sysop'];
173 $newPermissions['hideuser'] = true;
174 $this->mergeMwGlobalArrayValue( 'wgGroupPermissions',
175 [ 'sysop' => $newPermissions ] );
176
177 $res = $this->doBlock( [ 'hidename' => '' ] );
178
179 $dbw = wfGetDB( DB_MASTER );
180 $this->assertSame( '1', $dbw->selectField(
181 'ipblocks',
182 'ipb_deleted',
183 [ 'ipb_id' => $res[0]['block']['id'] ],
184 __METHOD__
185 ) );
186 }
187
188 public function testBlockWithProhibitedHide() {
189 $this->setExpectedException( ApiUsageException::class,
190 "You don't have permission to hide user names from the block log." );
191
192 $this->doBlock( [ 'hidename' => '' ] );
193 }
194
195 public function testBlockWithEmailBlock() {
196 $res = $this->doBlock( [ 'noemail' => '' ] );
197
198 $dbw = wfGetDB( DB_MASTER );
199 $this->assertSame( '1', $dbw->selectField(
200 'ipblocks',
201 'ipb_block_email',
202 [ 'ipb_id' => $res[0]['block']['id'] ],
203 __METHOD__
204 ) );
205 }
206
208 $this->setExpectedException( ApiUsageException::class,
209 "You don't have permission to block users from sending email through the wiki." );
210
211 $this->setMwGlobals( 'wgRevokePermissions',
212 [ 'sysop' => [ 'blockemail' => true ] ] );
213
214 $this->doBlock( [ 'noemail' => '' ] );
215 }
216
217 public function testBlockWithExpiry() {
218 $res = $this->doBlock( [ 'expiry' => '1 day' ] );
219
220 $dbw = wfGetDB( DB_MASTER );
221 $expiry = $dbw->selectField(
222 'ipblocks',
223 'ipb_expiry',
224 [ 'ipb_id' => $res[0]['block']['id'] ],
225 __METHOD__
226 );
227
228 // Allow flakiness up to one second
229 $this->assertLessThanOrEqual( 1,
230 abs( wfTimestamp( TS_UNIX, $expiry ) - ( time() + 86400 ) ) );
231 }
232
233 public function testBlockWithInvalidExpiry() {
234 $this->setExpectedException( ApiUsageException::class, "Expiry time invalid." );
235
236 $this->doBlock( [ 'expiry' => '' ] );
237 }
238
244 $this->doApiRequest(
245 [
246 'action' => 'block',
247 'user' => $this->mUser->getName(),
248 'reason' => 'Some reason',
249 ],
250 null,
251 false,
252 self::$users['sysop']->getUser()
253 );
254 }
255
256 public function testRangeBlock() {
257 $this->mUser = User::newFromName( '128.0.0.0/16', false );
258 $this->doBlock();
259 }
260
265 public function testVeryLargeRangeBlock() {
266 $this->mUser = User::newFromName( '128.0.0.0/1', false );
267 $this->doBlock();
268 }
269}
$wgGroupPermissions
Permission keys given to users in each group.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
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:1174
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:47
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:592
static whoIs( $id)
Get the username corresponding to a given user ID.
Definition User.php:891
$res
Definition database.txt:21
const MIGRATION_NEW
Definition Defines.php:318
const MIGRATION_WRITE_BOTH
Definition Defines.php:316
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:2054
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$tokens
const DB_MASTER
Definition defines.php:26
$params