MediaWiki  1.33.0
ApiBlockTest.php
Go to the documentation of this file.
1 <?php
2 
5 
13 class ApiBlockTest extends ApiTestCase {
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 
114  public function testBlockOfNonexistentUserId() {
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 
224  public function testBlockWithoutRestrictions() {
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 
266  public function testBlockingActionWithNoToken() {
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 }
MediaWikiTestCase\mergeMwGlobalArrayValue
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
Definition: MediaWikiTestCase.php:904
ApiBlockTest\testBlockOfNonexistentUser
testBlockOfNonexistentUser()
Definition: ApiBlockTest.php:107
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1912
ApiBlockTest\testBlockWithHide
testBlockWithHide()
Definition: ApiBlockTest.php:155
ApiBlockTest\setUp
setUp()
Definition: ApiBlockTest.php:16
$params
$params
Definition: styleTest.css.php:44
Block\newFromTarget
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
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:585
ApiBlockTest\testVeryLargeRangeBlock
testVeryLargeRangeBlock()
ApiUsageException Range blocks larger than /16 are not allowed.
Definition: ApiBlockTest.php:288
$res
$res
Definition: database.txt:21
ApiBlockTest\testBlockById
testBlockById()
Block by user ID.
Definition: ApiBlockTest.php:83
ApiBlockTest\testBlockWithEmailBlock
testBlockWithEmailBlock()
Definition: ApiBlockTest.php:180
ApiBlockTest\testRangeBlock
testRangeBlock()
Definition: ApiBlockTest.php:279
Block\insert
insert( $dbw=null)
Insert a block into the block table.
Definition: Block.php:545
ApiBlockTest\doBlock
doBlock(array $extraParams=[], User $blocker=null)
Definition: ApiBlockTest.php:39
ApiBlockTest\testNormalBlock
testNormalBlock()
Block by username.
Definition: ApiBlockTest.php:76
php
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:35
ApiBlockTest\$mUser
$mUser
Definition: ApiBlockTest.php:14
ApiBlockTest\testBlockOfNonexistentUserId
testBlockOfNonexistentUserId()
Definition: ApiBlockTest.php:114
ApiTestCase\getTokenList
getTokenList(TestUser $user, $session=null)
Definition: ApiTestCase.php:163
ApiTestCase\doApiRequest
doApiRequest(array $params, array $session=null, $appendModule=false, User $user=null, $tokenType=null)
Does the API request and returns the result.
Definition: ApiTestCase.php:62
ApiBlockTest
API Database medium.
Definition: ApiBlockTest.php:13
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2636
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:709
ApiBlockTest\testBlockWithProhibitedEmailBlock
testBlockWithProhibitedEmailBlock()
Definition: ApiBlockTest.php:192
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
ChangeTags\defineTag
static defineTag( $tag)
Set ctd_user_defined = 1 in change_tag_def without checking that the tag name is valid.
Definition: ChangeTags.php:877
DB_MASTER
const DB_MASTER
Definition: defines.php:26
array
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))
string
This code would result in ircNotify being run twice when an article is and once for brion Hooks can return three possible true was required This is the default since MediaWiki *some string
Definition: hooks.txt:175
ApiBlockTest\testBlockWithoutRestrictions
testBlockWithoutRestrictions()
Definition: ApiBlockTest.php:224
ApiBlockTest\testBlockWithRestrictions
testBlockWithRestrictions()
Definition: ApiBlockTest.php:237
ApiBlockTest\testBlockWithTag
testBlockWithTag()
Definition: ApiBlockTest.php:124
ApiTestCase
Definition: ApiTestCase.php:5
User\whoIs
static whoIs( $id)
Get the username corresponding to a given user ID.
Definition: User.php:885
ApiBlockTest\testBlockWithProhibitedHide
testBlockWithProhibitedHide()
Definition: ApiBlockTest.php:173
MediaWikiTestCase\getMutableTestUser
static getMutableTestUser( $groups=[])
Convenience method for getting a mutable test user.
Definition: MediaWikiTestCase.php:192
$tokens
$tokens
Definition: mwdoc-filter.php:47
ApiBlockTest\getTokens
getTokens()
Definition: ApiBlockTest.php:30
$ret
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:1985
ApiBlockTest\testBlockWithInvalidExpiry
testBlockWithInvalidExpiry()
Definition: ApiBlockTest.php:218
MediaWikiTestCase\getExistingTestPage
getExistingTestPage( $title=null)
Returns a WikiPage representing an existing page.
Definition: MediaWikiTestCase.php:220
MediaWiki\Block\Restriction\NamespaceRestriction
Definition: NamespaceRestriction.php:25
ApiBlockTest\testBlockByBlockedUser
testBlockByBlockedUser()
A blocked user can't block.
Definition: ApiBlockTest.php:90
ApiBlockTest\testBlockingActionWithNoToken
testBlockingActionWithNoToken()
ApiUsageException The "token" parameter must be set.
Definition: ApiBlockTest.php:266
MediaWiki\Block\Restriction\PageRestriction
Definition: PageRestriction.php:25
Block
Definition: Block.php:31
NS_TALK
const NS_TALK
Definition: Defines.php:65
$wgGroupPermissions
$wgGroupPermissions['sysop']['replacetext']
Definition: ReplaceText.php:56
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:48
ApiBlockTest\testBlockWithExpiry
testBlockWithExpiry()
Definition: ApiBlockTest.php:202
ApiBlockTest\testBlockWithProhibitedTag
testBlockWithProhibitedTag()
Definition: ApiBlockTest.php:143