MediaWiki  master
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  }
25 
26  protected function getTokens() {
27  return $this->getTokenList( self::$users['sysop'] );
28  }
29 
35  private function doBlock( array $extraParams = [], User $blocker = null ) {
36  if ( $blocker === null ) {
37  $blocker = self::$users['sysop']->getUser();
38  }
39 
40  $tokens = $this->getTokens();
41 
42  $this->assertNotNull( $this->mUser, 'Sanity check' );
43  $this->assertNotSame( 0, $this->mUser->getId(), '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->getReason() );
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 
111  public function testBlockOfNonexistentUserId() {
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  ChangeTags::defineTag( 'custom tag' );
123 
124  $this->doBlock( [ 'tags' => 'custom tag' ] );
125 
126  $dbw = wfGetDB( DB_MASTER );
127  $this->assertSame( 1, (int)$dbw->selectField(
128  [ 'change_tag', 'logging', 'change_tag_def' ],
129  'COUNT(*)',
130  [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ],
131  __METHOD__,
132  [],
133  [
134  'change_tag' => [ 'JOIN', 'ct_log_id = log_id' ],
135  'change_tag_def' => [ 'JOIN', 'ctd_id = ct_tag_id' ],
136  ]
137  ) );
138  }
139 
140  public function testBlockWithProhibitedTag() {
141  $this->setExpectedException( ApiUsageException::class,
142  'You do not have permission to apply change tags along with your changes.' );
143 
144  ChangeTags::defineTag( 'custom tag' );
145 
146  $this->setMwGlobals( 'wgRevokePermissions',
147  [ 'user' => [ 'applychangetags' => true ] ] );
148 
149  $this->doBlock( [ 'tags' => 'custom tag' ] );
150  }
151 
152  public function testBlockWithHide() {
153  global $wgGroupPermissions;
154  $newPermissions = $wgGroupPermissions['sysop'];
155  $newPermissions['hideuser'] = true;
156  $this->mergeMwGlobalArrayValue( 'wgGroupPermissions',
157  [ 'sysop' => $newPermissions ] );
158 
159  $res = $this->doBlock( [ 'hidename' => '' ] );
160 
161  $dbw = wfGetDB( DB_MASTER );
162  $this->assertSame( '1', $dbw->selectField(
163  'ipblocks',
164  'ipb_deleted',
165  [ 'ipb_id' => $res[0]['block']['id'] ],
166  __METHOD__
167  ) );
168  }
169 
170  public function testBlockWithProhibitedHide() {
171  $this->setExpectedException( ApiUsageException::class,
172  "You don't have permission to hide user names from the block log." );
173 
174  $this->doBlock( [ 'hidename' => '' ] );
175  }
176 
177  public function testBlockWithEmailBlock() {
178  $res = $this->doBlock( [ 'noemail' => '' ] );
179 
180  $dbw = wfGetDB( DB_MASTER );
181  $this->assertSame( '1', $dbw->selectField(
182  'ipblocks',
183  'ipb_block_email',
184  [ 'ipb_id' => $res[0]['block']['id'] ],
185  __METHOD__
186  ) );
187  }
188 
190  $this->setExpectedException( ApiUsageException::class,
191  "You don't have permission to block users from sending email through the wiki." );
192 
193  $this->setMwGlobals( 'wgRevokePermissions',
194  [ 'sysop' => [ 'blockemail' => true ] ] );
195 
196  $this->doBlock( [ 'noemail' => '' ] );
197  }
198 
199  public function testBlockWithExpiry() {
200  $res = $this->doBlock( [ 'expiry' => '1 day' ] );
201 
202  $dbw = wfGetDB( DB_MASTER );
203  $expiry = $dbw->selectField(
204  'ipblocks',
205  'ipb_expiry',
206  [ 'ipb_id' => $res[0]['block']['id'] ],
207  __METHOD__
208  );
209 
210  // Allow flakiness up to one second
211  $this->assertLessThanOrEqual( 1,
212  abs( wfTimestamp( TS_UNIX, $expiry ) - ( time() + 86400 ) ) );
213  }
214 
215  public function testBlockWithInvalidExpiry() {
216  $this->setExpectedException( ApiUsageException::class, "Expiry time invalid." );
217 
218  $this->doBlock( [ 'expiry' => '' ] );
219  }
220 
221  public function testBlockWithoutRestrictions() {
222  $this->setMwGlobals( [
223  'wgEnablePartialBlocks' => true,
224  ] );
225 
226  $this->doBlock();
227 
228  $block = Block::newFromTarget( $this->mUser->getName() );
229 
230  $this->assertTrue( $block->isSitewide() );
231  $this->assertCount( 0, $block->getRestrictions() );
232  }
233 
234  public function testBlockWithRestrictions() {
235  $this->setMwGlobals( [
236  'wgEnablePartialBlocks' => true,
237  ] );
238 
239  $title = 'Foo';
240  $page = $this->getExistingTestPage( $title );
241  $namespace = NS_TALK;
242 
243  $this->doBlock( [
244  'partial' => true,
245  'pagerestrictions' => $title,
246  'namespacerestrictions' => $namespace,
247  ] );
248 
249  $block = Block::newFromTarget( $this->mUser->getName() );
250 
251  $this->assertFalse( $block->isSitewide() );
252  $this->assertCount( 2, $block->getRestrictions() );
253  $this->assertInstanceOf( PageRestriction::class, $block->getRestrictions()[0] );
254  $this->assertEquals( $title, $block->getRestrictions()[0]->getTitle()->getText() );
255  $this->assertInstanceOf( NamespaceRestriction::class, $block->getRestrictions()[1] );
256  $this->assertEquals( $namespace, $block->getRestrictions()[1]->getValue() );
257  }
258 
263  public function testBlockingActionWithNoToken() {
264  $this->doApiRequest(
265  [
266  'action' => 'block',
267  'user' => $this->mUser->getName(),
268  'reason' => 'Some reason',
269  ],
270  null,
271  false,
272  self::$users['sysop']->getUser()
273  );
274  }
275 
280  public function testBlockWithLargeRange() {
281  $tokens = $this->getTokens();
282 
283  $this->doApiRequest(
284  [
285  'action' => 'block',
286  'user' => '127.0.0.1/64',
287  'reason' => 'Some reason',
288  'token' => $tokens['blocktoken'],
289  ],
290  null,
291  false,
292  self::$users['sysop']->getUser()
293  );
294  }
295 
302  $this->setMwGlobals( [
303  'wgEnablePartialBlocks' => true,
304  ] );
305 
306  $tokens = $this->getTokens();
307 
308  $this->doApiRequest(
309  [
310  'action' => 'block',
311  'user' => $this->mUser->getName(),
312  'reason' => 'Some reason',
313  'partial' => true,
314  'pagerestrictions' => 'One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten|Eleven',
315  'token' => $tokens['blocktoken'],
316  ],
317  null,
318  false,
319  self::$users['sysop']->getUser()
320  );
321  }
322 }
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))
testBlockingToManyPageRestrictions()
ApiUsageException Too many values supplied for parameter "pagerestrictions".
static whoIs( $id)
Get the username corresponding to a given user ID.
Definition: User.php:887
testBlockingActionWithNoToken()
ApiUsageException The "token" parameter must be set.
testBlockWithoutRestrictions()
testBlockOfNonexistentUser()
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:1982
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static newFromTarget( $specificTarget, $vagueTarget=null, $fromMaster=false)
Given a target and the target&#39;s type, get an existing Block object if possible.
Definition: Block.php:1397
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
testBlockWithProhibitedHide()
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
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
testBlockByBlockedUser()
A blocked user can&#39;t block.
insert( $dbw=null)
Insert a block into the block table.
Definition: Block.php:543
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
testBlockWithProhibitedEmailBlock()
const DB_MASTER
Definition: defines.php:26
$wgGroupPermissions
Permission keys given to users in each group.
The User object encapsulates all of the user-specific settings (user_id, name, rights, email address, options, last login time).
Definition: User.php:48
testNormalBlock()
Block by username.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
getTokenList(TestUser $user, $session=null)
doBlock(array $extraParams=[], User $blocker=null)
static getMutableTestUser( $groups=[])
Convenience method for getting a mutable test user.
$res
Definition: database.txt:21
testBlockWithProhibitedTag()
$params
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
$tokens
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown...
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
static defineTag( $tag)
Set ctd_user_defined = 1 in change_tag_def without checking that the tag name is valid.
Definition: ChangeTags.php:877
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
testBlockById()
Block by user ID.
testBlockWithLargeRange()
ApiUsageException Invalid value "127.0.0.1/64" for user parameter "user".
const NS_TALK
Definition: Defines.php:65
API Database medium.
testBlockWithInvalidExpiry()
getExistingTestPage( $title=null)
Returns a WikiPage representing an existing page.
Definition: Block.php:31
testBlockOfNonexistentUserId()
testBlockWithRestrictions()