MediaWiki REL1_33
ApiBlockTest.php
Go to the documentation of this file.
1<?php
2
5
14 protected $mUser = null;
15
16 protected function setUp() {
17 parent::setUp();
18 $this->tablesUsed = array_merge(
19 $this->tablesUsed,
20 [ 'ipblocks', 'change_tag', 'change_tag_def', 'logging' ]
21 );
22
23 $this->mUser = $this->getMutableTestUser()->getUser();
24 $this->setMwGlobals( 'wgBlockCIDRLimit', [
25 'IPv4' => 16,
26 'IPv6' => 19,
27 ] );
28 }
29
30 protected function getTokens() {
31 return $this->getTokenList( self::$users['sysop'] );
32 }
33
39 private function doBlock( array $extraParams = [], User $blocker = null ) {
40 if ( $blocker === null ) {
41 $blocker = self::$users['sysop']->getUser();
42 }
43
44 $tokens = $this->getTokens();
45
46 $this->assertNotNull( $this->mUser, 'Sanity check' );
47
48 $this->assertArrayHasKey( 'blocktoken', $tokens, 'Sanity check' );
49
50 $params = [
51 'action' => 'block',
52 'user' => $this->mUser->getName(),
53 'reason' => 'Some reason',
54 'token' => $tokens['blocktoken'],
55 ];
56 if ( array_key_exists( 'userid', $extraParams ) ) {
57 // Make sure we don't have both user and userid
58 unset( $params['user'] );
59 }
60 $ret = $this->doApiRequest( array_merge( $params, $extraParams ), null,
61 false, $blocker );
62
63 $block = Block::newFromTarget( $this->mUser->getName() );
64
65 $this->assertTrue( !is_null( $block ), 'Block is valid' );
66
67 $this->assertSame( $this->mUser->getName(), (string)$block->getTarget() );
68 $this->assertSame( 'Some reason', $block->getReason() );
69
70 return $ret;
71 }
72
76 public function testNormalBlock() {
77 $this->doBlock();
78 }
79
83 public function testBlockById() {
84 $this->doBlock( [ 'userid' => $this->mUser->getId() ] );
85 }
86
90 public function testBlockByBlockedUser() {
91 $this->setExpectedException( ApiUsageException::class,
92 'You cannot block or unblock other users because you are yourself blocked.' );
93
94 $blocked = $this->getMutableTestUser( [ 'sysop' ] )->getUser();
95 $block = new Block( [
96 'address' => $blocked->getName(),
97 'by' => self::$users['sysop']->getUser()->getId(),
98 'reason' => 'Capriciousness',
99 'timestamp' => '19370101000000',
100 'expiry' => 'infinity',
101 ] );
102 $block->insert();
103
104 $this->doBlock( [], $blocked );
105 }
106
107 public function testBlockOfNonexistentUser() {
108 $this->setExpectedException( ApiUsageException::class,
109 'There is no user by the name "Nonexistent". Check your spelling.' );
110
111 $this->doBlock( [ 'user' => 'Nonexistent' ] );
112 }
113
115 $id = 948206325;
116 $this->setExpectedException( ApiUsageException::class,
117 "There is no user with ID $id." );
118
119 $this->assertFalse( User::whoIs( $id ), 'Sanity check' );
120
121 $this->doBlock( [ 'userid' => $id ] );
122 }
123
124 public function testBlockWithTag() {
125 ChangeTags::defineTag( 'custom tag' );
126
127 $this->doBlock( [ 'tags' => 'custom tag' ] );
128
129 $dbw = wfGetDB( DB_MASTER );
130 $this->assertSame( 1, (int)$dbw->selectField(
131 [ 'change_tag', 'logging', 'change_tag_def' ],
132 'COUNT(*)',
133 [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ],
134 __METHOD__,
135 [],
136 [
137 'change_tag' => [ 'JOIN', 'ct_log_id = log_id' ],
138 'change_tag_def' => [ 'JOIN', 'ctd_id = ct_tag_id' ],
139 ]
140 ) );
141 }
142
143 public function testBlockWithProhibitedTag() {
144 $this->setExpectedException( ApiUsageException::class,
145 'You do not have permission to apply change tags along with your changes.' );
146
147 ChangeTags::defineTag( 'custom tag' );
148
149 $this->setMwGlobals( 'wgRevokePermissions',
150 [ 'user' => [ 'applychangetags' => true ] ] );
151
152 $this->doBlock( [ 'tags' => 'custom tag' ] );
153 }
154
155 public function testBlockWithHide() {
156 global $wgGroupPermissions;
157 $newPermissions = $wgGroupPermissions['sysop'];
158 $newPermissions['hideuser'] = true;
159 $this->mergeMwGlobalArrayValue( 'wgGroupPermissions',
160 [ 'sysop' => $newPermissions ] );
161
162 $res = $this->doBlock( [ 'hidename' => '' ] );
163
164 $dbw = wfGetDB( DB_MASTER );
165 $this->assertSame( '1', $dbw->selectField(
166 'ipblocks',
167 'ipb_deleted',
168 [ 'ipb_id' => $res[0]['block']['id'] ],
169 __METHOD__
170 ) );
171 }
172
173 public function testBlockWithProhibitedHide() {
174 $this->setExpectedException( ApiUsageException::class,
175 "You don't have permission to hide user names from the block log." );
176
177 $this->doBlock( [ 'hidename' => '' ] );
178 }
179
180 public function testBlockWithEmailBlock() {
181 $res = $this->doBlock( [ 'noemail' => '' ] );
182
183 $dbw = wfGetDB( DB_MASTER );
184 $this->assertSame( '1', $dbw->selectField(
185 'ipblocks',
186 'ipb_block_email',
187 [ 'ipb_id' => $res[0]['block']['id'] ],
188 __METHOD__
189 ) );
190 }
191
193 $this->setExpectedException( ApiUsageException::class,
194 "You don't have permission to block users from sending email through the wiki." );
195
196 $this->setMwGlobals( 'wgRevokePermissions',
197 [ 'sysop' => [ 'blockemail' => true ] ] );
198
199 $this->doBlock( [ 'noemail' => '' ] );
200 }
201
202 public function testBlockWithExpiry() {
203 $res = $this->doBlock( [ 'expiry' => '1 day' ] );
204
205 $dbw = wfGetDB( DB_MASTER );
206 $expiry = $dbw->selectField(
207 'ipblocks',
208 'ipb_expiry',
209 [ 'ipb_id' => $res[0]['block']['id'] ],
210 __METHOD__
211 );
212
213 // Allow flakiness up to one second
214 $this->assertLessThanOrEqual( 1,
215 abs( wfTimestamp( TS_UNIX, $expiry ) - ( time() + 86400 ) ) );
216 }
217
218 public function testBlockWithInvalidExpiry() {
219 $this->setExpectedException( ApiUsageException::class, "Expiry time invalid." );
220
221 $this->doBlock( [ 'expiry' => '' ] );
222 }
223
225 $this->setMwGlobals( [
226 'wgEnablePartialBlocks' => true,
227 ] );
228
229 $this->doBlock();
230
231 $block = Block::newFromTarget( $this->mUser->getName() );
232
233 $this->assertTrue( $block->isSitewide() );
234 $this->assertCount( 0, $block->getRestrictions() );
235 }
236
237 public function testBlockWithRestrictions() {
238 $this->setMwGlobals( [
239 'wgEnablePartialBlocks' => true,
240 ] );
241
242 $title = 'Foo';
243 $page = $this->getExistingTestPage( $title );
244 $namespace = NS_TALK;
245
246 $this->doBlock( [
247 'partial' => true,
248 'pagerestrictions' => $title,
249 'namespacerestrictions' => $namespace,
250 ] );
251
252 $block = Block::newFromTarget( $this->mUser->getName() );
253
254 $this->assertFalse( $block->isSitewide() );
255 $this->assertCount( 2, $block->getRestrictions() );
256 $this->assertInstanceOf( PageRestriction::class, $block->getRestrictions()[0] );
257 $this->assertEquals( $title, $block->getRestrictions()[0]->getTitle()->getText() );
258 $this->assertInstanceOf( NamespaceRestriction::class, $block->getRestrictions()[1] );
259 $this->assertEquals( $namespace, $block->getRestrictions()[1]->getValue() );
260 }
261
267 $this->doApiRequest(
268 [
269 'action' => 'block',
270 'user' => $this->mUser->getName(),
271 'reason' => 'Some reason',
272 ],
273 null,
274 false,
275 self::$users['sysop']->getUser()
276 );
277 }
278
279 public function testRangeBlock() {
280 $this->mUser = User::newFromName( '128.0.0.0/16', false );
281 $this->doBlock();
282 }
283
288 public function testVeryLargeRangeBlock() {
289 $this->mUser = User::newFromName( '128.0.0.0/1', false );
290 $this->doBlock();
291 }
292}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$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.
testBlockWithoutRestrictions()
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:1403
static defineTag( $tag)
Set ctd_user_defined = 1 in change_tag_def without checking that the tag name is valid.
getExistingTestPage( $title=null)
Returns a WikiPage representing an existing page.
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:48
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:585
static whoIs( $id)
Get the username corresponding to a given user ID.
Definition User.php:885
$res
Definition database.txt:21
const NS_TALK
Definition Defines.php:74
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:955
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:2003
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