MediaWiki  1.31.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 
16  $this->mUser = $this->getMutableTestUser()->getUser();
17  }
18 
19  protected function tearDown() {
20  $block = Block::newFromTarget( $this->mUser->getName() );
21  if ( !is_null( $block ) ) {
22  $block->delete();
23  }
24  parent::tearDown();
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  $this->assertNotSame( 0, $this->mUser->getId(), 'Sanity check' );
45 
46  $this->assertArrayHasKey( 'blocktoken', $tokens, 'Sanity check' );
47 
48  $params = [
49  'action' => 'block',
50  'user' => $this->mUser->getName(),
51  'reason' => 'Some reason',
52  'token' => $tokens['blocktoken'],
53  ];
54  if ( array_key_exists( 'userid', $extraParams ) ) {
55  // Make sure we don't have both user and userid
56  unset( $params['user'] );
57  }
58  $ret = $this->doApiRequest( array_merge( $params, $extraParams ), null,
59  false, $blocker );
60 
61  $block = Block::newFromTarget( $this->mUser->getName() );
62 
63  $this->assertTrue( !is_null( $block ), 'Block is valid' );
64 
65  $this->assertSame( $this->mUser->getName(), (string)$block->getTarget() );
66  $this->assertSame( 'Some reason', $block->mReason );
67 
68  return $ret;
69  }
70 
74  public function testNormalBlock() {
75  $this->doBlock();
76  }
77 
81  public function testBlockById() {
82  $this->doBlock( [ 'userid' => $this->mUser->getId() ] );
83  }
84 
88  public function testBlockByBlockedUser() {
89  $this->setExpectedException( ApiUsageException::class,
90  'You cannot block or unblock other users because you are yourself blocked.' );
91 
92  $blocked = $this->getMutableTestUser( [ 'sysop' ] )->getUser();
93  $block = new Block( [
94  'address' => $blocked->getName(),
95  'by' => self::$users['sysop']->getUser()->getId(),
96  'reason' => 'Capriciousness',
97  'timestamp' => '19370101000000',
98  'expiry' => 'infinity',
99  ] );
100  $block->insert();
101 
102  $this->doBlock( [], $blocked );
103  }
104 
105  public function testBlockOfNonexistentUser() {
106  $this->setExpectedException( ApiUsageException::class,
107  'There is no user by the name "Nonexistent". Check your spelling.' );
108 
109  $this->doBlock( [ 'user' => 'Nonexistent' ] );
110  }
111 
112  public function testBlockOfNonexistentUserId() {
113  $id = 948206325;
114  $this->setExpectedException( ApiUsageException::class,
115  "There is no user with ID $id." );
116 
117  $this->assertFalse( User::whoIs( $id ), 'Sanity check' );
118 
119  $this->doBlock( [ 'userid' => $id ] );
120  }
121 
122  public function testBlockWithTag() {
123  ChangeTags::defineTag( 'custom tag' );
124 
125  $this->doBlock( [ 'tags' => 'custom tag' ] );
126 
127  $dbw = wfGetDB( DB_MASTER );
128  $this->assertSame( 'custom tag', $dbw->selectField(
129  [ 'change_tag', 'logging' ],
130  'ct_tag',
131  [ 'log_type' => 'block' ],
132  __METHOD__,
133  [],
134  [ 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ] ]
135  ) );
136  }
137 
138  public function testBlockWithProhibitedTag() {
139  $this->setExpectedException( ApiUsageException::class,
140  'You do not have permission to apply change tags along with your changes.' );
141 
142  ChangeTags::defineTag( 'custom tag' );
143 
144  $this->setMwGlobals( 'wgRevokePermissions',
145  [ 'user' => [ 'applychangetags' => true ] ] );
146 
147  $this->doBlock( [ 'tags' => 'custom tag' ] );
148  }
149 
150  public function testBlockWithHide() {
152  $newPermissions = $wgGroupPermissions['sysop'];
153  $newPermissions['hideuser'] = true;
154  $this->mergeMwGlobalArrayValue( 'wgGroupPermissions',
155  [ 'sysop' => $newPermissions ] );
156 
157  $res = $this->doBlock( [ 'hidename' => '' ] );
158 
159  $dbw = wfGetDB( DB_MASTER );
160  $this->assertSame( '1', $dbw->selectField(
161  'ipblocks',
162  'ipb_deleted',
163  [ 'ipb_id' => $res[0]['block']['id'] ],
164  __METHOD__
165  ) );
166  }
167 
168  public function testBlockWithProhibitedHide() {
169  $this->setExpectedException( ApiUsageException::class,
170  "You don't have permission to hide user names from the block log." );
171 
172  $this->doBlock( [ 'hidename' => '' ] );
173  }
174 
175  public function testBlockWithEmailBlock() {
176  $res = $this->doBlock( [ 'noemail' => '' ] );
177 
178  $dbw = wfGetDB( DB_MASTER );
179  $this->assertSame( '1', $dbw->selectField(
180  'ipblocks',
181  'ipb_block_email',
182  [ 'ipb_id' => $res[0]['block']['id'] ],
183  __METHOD__
184  ) );
185  }
186 
188  $this->setExpectedException( ApiUsageException::class,
189  "You don't have permission to block users from sending email through the wiki." );
190 
191  $this->setMwGlobals( 'wgRevokePermissions',
192  [ 'sysop' => [ 'blockemail' => true ] ] );
193 
194  $this->doBlock( [ 'noemail' => '' ] );
195  }
196 
197  public function testBlockWithExpiry() {
198  $res = $this->doBlock( [ 'expiry' => '1 day' ] );
199 
200  $dbw = wfGetDB( DB_MASTER );
201  $expiry = $dbw->selectField(
202  'ipblocks',
203  'ipb_expiry',
204  [ 'ipb_id' => $res[0]['block']['id'] ],
205  __METHOD__
206  );
207 
208  // Allow flakiness up to one second
209  $this->assertLessThanOrEqual( 1,
210  abs( wfTimestamp( TS_UNIX, $expiry ) - ( time() + 86400 ) ) );
211  }
212 
213  public function testBlockWithInvalidExpiry() {
214  $this->setExpectedException( ApiUsageException::class, "Expiry time invalid." );
215 
216  $this->doBlock( [ 'expiry' => '' ] );
217  }
218 
223  public function testBlockingActionWithNoToken() {
224  $this->doApiRequest(
225  [
226  'action' => 'block',
227  'user' => $this->mUser->getName(),
228  'reason' => 'Some reason',
229  ],
230  null,
231  false,
232  self::$users['sysop']->getUser()
233  );
234  }
235 }
MediaWikiTestCase\mergeMwGlobalArrayValue
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
Definition: MediaWikiTestCase.php:813
ApiBlockTest\testBlockOfNonexistentUser
testBlockOfNonexistentUser()
Definition: ApiBlockTest.php:105
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1968
ApiBlockTest\testBlockWithHide
testBlockWithHide()
Definition: ApiBlockTest.php:150
ApiBlockTest\setUp
setUp()
Definition: ApiBlockTest.php:13
$params
$params
Definition: styleTest.css.php:40
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:1173
$res
$res
Definition: database.txt:21
ApiBlockTest\testBlockById
testBlockById()
Block by user ID.
Definition: ApiBlockTest.php:81
ApiBlockTest\testBlockWithEmailBlock
testBlockWithEmailBlock()
Definition: ApiBlockTest.php:175
Block\insert
insert( $dbw=null)
Insert a block into the block table.
Definition: Block.php:526
ApiBlockTest\doBlock
doBlock(array $extraParams=[], User $blocker=null)
Definition: ApiBlockTest.php:36
ApiBlockTest\testNormalBlock
testNormalBlock()
Block by username.
Definition: ApiBlockTest.php:74
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:112
ApiTestCase\getTokenList
getTokenList(TestUser $user, $session=null)
Definition: ApiTestCase.php:201
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:100
ApiBlockTest
API Database medium.
Definition: ApiBlockTest.php:10
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2800
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:678
ApiBlockTest\tearDown
tearDown()
Definition: ApiBlockTest.php:19
ApiBlockTest\testBlockWithProhibitedEmailBlock
testBlockWithProhibitedEmailBlock()
Definition: ApiBlockTest.php:187
ChangeTags\defineTag
static defineTag( $tag)
Defines a tag in the valid_tag table, without checking that the tag name is valid.
Definition: ChangeTags.php:825
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
DB_MASTER
const DB_MASTER
Definition: defines.php:26
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:122
ApiTestCase
Definition: ApiTestCase.php:5
User\whoIs
static whoIs( $id)
Get the username corresponding to a given user ID.
Definition: User.php:863
ApiBlockTest\testBlockWithProhibitedHide
testBlockWithProhibitedHide()
Definition: ApiBlockTest.php:168
MediaWikiTestCase\getMutableTestUser
static getMutableTestUser( $groups=[])
Convenience method for getting a mutable test user.
Definition: MediaWikiTestCase.php:165
$tokens
$tokens
Definition: mwdoc-filter.php:47
ApiBlockTest\getTokens
getTokens()
Definition: ApiBlockTest.php:27
$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:1987
ApiBlockTest\testBlockWithInvalidExpiry
testBlockWithInvalidExpiry()
Definition: ApiBlockTest.php:213
ApiBlockTest\testBlockByBlockedUser
testBlockByBlockedUser()
A blocked user can't block.
Definition: ApiBlockTest.php:88
ApiBlockTest\testBlockingActionWithNoToken
testBlockingActionWithNoToken()
ApiUsageException The "token" parameter must be set.
Definition: ApiBlockTest.php:223
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:53
ApiBlockTest\testBlockWithExpiry
testBlockWithExpiry()
Definition: ApiBlockTest.php:197
array
the array() calling protocol came about after MediaWiki 1.4rc1.
ApiBlockTest\testBlockWithProhibitedTag
testBlockWithProhibitedTag()
Definition: ApiBlockTest.php:138