MediaWiki  1.32.0
ApiBlockTest.php
Go to the documentation of this file.
1 <?php
2 
10 class ApiBlockTest extends ApiTestCase {
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  }
22 
23  protected function getTokens() {
24  return $this->getTokenList( self::$users['sysop'] );
25  }
26 
32  private function doBlock( array $extraParams = [], User $blocker = null ) {
33  if ( $blocker === null ) {
34  $blocker = self::$users['sysop']->getUser();
35  }
36 
37  $tokens = $this->getTokens();
38 
39  $this->assertNotNull( $this->mUser, 'Sanity check' );
40  $this->assertNotSame( 0, $this->mUser->getId(), 'Sanity check' );
41 
42  $this->assertArrayHasKey( 'blocktoken', $tokens, 'Sanity check' );
43 
44  $params = [
45  'action' => 'block',
46  'user' => $this->mUser->getName(),
47  'reason' => 'Some reason',
48  'token' => $tokens['blocktoken'],
49  ];
50  if ( array_key_exists( 'userid', $extraParams ) ) {
51  // Make sure we don't have both user and userid
52  unset( $params['user'] );
53  }
54  $ret = $this->doApiRequest( array_merge( $params, $extraParams ), null,
55  false, $blocker );
56 
57  $block = Block::newFromTarget( $this->mUser->getName() );
58 
59  $this->assertTrue( !is_null( $block ), 'Block is valid' );
60 
61  $this->assertSame( $this->mUser->getName(), (string)$block->getTarget() );
62  $this->assertSame( 'Some reason', $block->mReason );
63 
64  return $ret;
65  }
66 
70  public function testNormalBlock() {
71  $this->doBlock();
72  }
73 
77  public function testBlockById() {
78  $this->doBlock( [ 'userid' => $this->mUser->getId() ] );
79  }
80 
84  public function testBlockByBlockedUser() {
85  $this->setExpectedException( ApiUsageException::class,
86  'You cannot block or unblock other users because you are yourself blocked.' );
87 
88  $blocked = $this->getMutableTestUser( [ 'sysop' ] )->getUser();
89  $block = new Block( [
90  'address' => $blocked->getName(),
91  'by' => self::$users['sysop']->getUser()->getId(),
92  'reason' => 'Capriciousness',
93  'timestamp' => '19370101000000',
94  'expiry' => 'infinity',
95  ] );
96  $block->insert();
97 
98  $this->doBlock( [], $blocked );
99  }
100 
101  public function testBlockOfNonexistentUser() {
102  $this->setExpectedException( ApiUsageException::class,
103  'There is no user by the name "Nonexistent". Check your spelling.' );
104 
105  $this->doBlock( [ 'user' => 'Nonexistent' ] );
106  }
107 
108  public function testBlockOfNonexistentUserId() {
109  $id = 948206325;
110  $this->setExpectedException( ApiUsageException::class,
111  "There is no user with ID $id." );
112 
113  $this->assertFalse( User::whoIs( $id ), 'Sanity check' );
114 
115  $this->doBlock( [ 'userid' => $id ] );
116  }
117 
118  public function testBlockWithTag() {
119  $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_WRITE_BOTH );
120  ChangeTags::defineTag( 'custom tag' );
121 
122  $this->doBlock( [ 'tags' => 'custom tag' ] );
123 
124  $dbw = wfGetDB( DB_MASTER );
125  $this->assertSame( 1, (int)$dbw->selectField(
126  [ 'change_tag', 'logging' ],
127  'COUNT(*)',
128  [ 'log_type' => 'block', 'ct_tag' => 'custom tag' ],
129  __METHOD__,
130  [],
131  [ 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ] ]
132  ) );
133  }
134 
135  public function testBlockWithTagNewBackend() {
136  $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_NEW );
137  ChangeTags::defineTag( 'custom tag' );
138 
139  $this->doBlock( [ 'tags' => 'custom tag' ] );
140 
141  $dbw = wfGetDB( DB_MASTER );
142  $this->assertSame( 1, (int)$dbw->selectField(
143  [ 'change_tag', 'logging', 'change_tag_def' ],
144  'COUNT(*)',
145  [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ],
146  __METHOD__,
147  [],
148  [
149  'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ],
150  'change_tag_def' => [ 'INNER JOIN', 'ctd_id = ct_tag_id' ],
151  ]
152  ) );
153  }
154 
155  public function testBlockWithProhibitedTag() {
156  $this->setExpectedException( ApiUsageException::class,
157  'You do not have permission to apply change tags along with your changes.' );
158 
159  ChangeTags::defineTag( 'custom tag' );
160 
161  $this->setMwGlobals( 'wgRevokePermissions',
162  [ 'user' => [ 'applychangetags' => true ] ] );
163 
164  $this->doBlock( [ 'tags' => 'custom tag' ] );
165  }
166 
167  public function testBlockWithHide() {
168  global $wgGroupPermissions;
169  $newPermissions = $wgGroupPermissions['sysop'];
170  $newPermissions['hideuser'] = true;
171  $this->mergeMwGlobalArrayValue( 'wgGroupPermissions',
172  [ 'sysop' => $newPermissions ] );
173 
174  $res = $this->doBlock( [ 'hidename' => '' ] );
175 
176  $dbw = wfGetDB( DB_MASTER );
177  $this->assertSame( '1', $dbw->selectField(
178  'ipblocks',
179  'ipb_deleted',
180  [ 'ipb_id' => $res[0]['block']['id'] ],
181  __METHOD__
182  ) );
183  }
184 
185  public function testBlockWithProhibitedHide() {
186  $this->setExpectedException( ApiUsageException::class,
187  "You don't have permission to hide user names from the block log." );
188 
189  $this->doBlock( [ 'hidename' => '' ] );
190  }
191 
192  public function testBlockWithEmailBlock() {
193  $res = $this->doBlock( [ 'noemail' => '' ] );
194 
195  $dbw = wfGetDB( DB_MASTER );
196  $this->assertSame( '1', $dbw->selectField(
197  'ipblocks',
198  'ipb_block_email',
199  [ 'ipb_id' => $res[0]['block']['id'] ],
200  __METHOD__
201  ) );
202  }
203 
205  $this->setExpectedException( ApiUsageException::class,
206  "You don't have permission to block users from sending email through the wiki." );
207 
208  $this->setMwGlobals( 'wgRevokePermissions',
209  [ 'sysop' => [ 'blockemail' => true ] ] );
210 
211  $this->doBlock( [ 'noemail' => '' ] );
212  }
213 
214  public function testBlockWithExpiry() {
215  $res = $this->doBlock( [ 'expiry' => '1 day' ] );
216 
217  $dbw = wfGetDB( DB_MASTER );
218  $expiry = $dbw->selectField(
219  'ipblocks',
220  'ipb_expiry',
221  [ 'ipb_id' => $res[0]['block']['id'] ],
222  __METHOD__
223  );
224 
225  // Allow flakiness up to one second
226  $this->assertLessThanOrEqual( 1,
227  abs( wfTimestamp( TS_UNIX, $expiry ) - ( time() + 86400 ) ) );
228  }
229 
230  public function testBlockWithInvalidExpiry() {
231  $this->setExpectedException( ApiUsageException::class, "Expiry time invalid." );
232 
233  $this->doBlock( [ 'expiry' => '' ] );
234  }
235 
240  public function testBlockingActionWithNoToken() {
241  $this->doApiRequest(
242  [
243  'action' => 'block',
244  'user' => $this->mUser->getName(),
245  'reason' => 'Some reason',
246  ],
247  null,
248  false,
249  self::$users['sysop']->getUser()
250  );
251  }
252 }
MediaWikiTestCase\mergeMwGlobalArrayValue
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
Definition: MediaWikiTestCase.php:901
ApiBlockTest\testBlockOfNonexistentUser
testBlockOfNonexistentUser()
Definition: ApiBlockTest.php:101
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1954
ApiBlockTest\testBlockWithHide
testBlockWithHide()
Definition: ApiBlockTest.php:167
MIGRATION_NEW
const MIGRATION_NEW
Definition: Defines.php:318
ApiBlockTest\setUp
setUp()
Definition: ApiBlockTest.php:13
$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:1174
MIGRATION_WRITE_BOTH
const MIGRATION_WRITE_BOTH
Definition: Defines.php:316
$res
$res
Definition: database.txt:21
ApiBlockTest\testBlockById
testBlockById()
Block by user ID.
Definition: ApiBlockTest.php:77
ApiBlockTest\testBlockWithEmailBlock
testBlockWithEmailBlock()
Definition: ApiBlockTest.php:192
Block\insert
insert( $dbw=null)
Insert a block into the block table.
Definition: Block.php:527
ApiBlockTest\doBlock
doBlock(array $extraParams=[], User $blocker=null)
Definition: ApiBlockTest.php:32
ApiBlockTest\testNormalBlock
testNormalBlock()
Block by username.
Definition: ApiBlockTest.php:70
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:11
ApiBlockTest\testBlockOfNonexistentUserId
testBlockOfNonexistentUserId()
Definition: ApiBlockTest.php:108
ApiTestCase\getTokenList
getTokenList(TestUser $user, $session=null)
Definition: ApiTestCase.php:164
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:63
ApiBlockTest
API Database medium.
Definition: ApiBlockTest.php:10
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2693
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:706
ApiBlockTest\testBlockWithProhibitedEmailBlock
testBlockWithProhibitedEmailBlock()
Definition: ApiBlockTest.php:204
ChangeTags\defineTag
static defineTag( $tag)
Defines a tag in the valid_tag table and/or update ctd_user_defined field in change_tag_def,...
Definition: ChangeTags.php:915
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\testBlockWithTag
testBlockWithTag()
Definition: ApiBlockTest.php:118
ApiTestCase
Definition: ApiTestCase.php:5
User\whoIs
static whoIs( $id)
Get the username corresponding to a given user ID.
Definition: User.php:891
ApiBlockTest\testBlockWithProhibitedHide
testBlockWithProhibitedHide()
Definition: ApiBlockTest.php:185
MediaWikiTestCase\getMutableTestUser
static getMutableTestUser( $groups=[])
Convenience method for getting a mutable test user.
Definition: MediaWikiTestCase.php:191
$tokens
$tokens
Definition: mwdoc-filter.php:47
ApiBlockTest\getTokens
getTokens()
Definition: ApiBlockTest.php:23
$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:2036
ApiBlockTest\testBlockWithInvalidExpiry
testBlockWithInvalidExpiry()
Definition: ApiBlockTest.php:230
ApiBlockTest\testBlockWithTagNewBackend
testBlockWithTagNewBackend()
Definition: ApiBlockTest.php:135
ApiBlockTest\testBlockByBlockedUser
testBlockByBlockedUser()
A blocked user can't block.
Definition: ApiBlockTest.php:84
ApiBlockTest\testBlockingActionWithNoToken
testBlockingActionWithNoToken()
ApiUsageException The "token" parameter must be set.
Definition: ApiBlockTest.php:240
Block
Definition: Block.php:27
$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:47
ApiBlockTest\testBlockWithExpiry
testBlockWithExpiry()
Definition: ApiBlockTest.php:214
ApiBlockTest\testBlockWithProhibitedTag
testBlockWithProhibitedTag()
Definition: ApiBlockTest.php:155