MediaWiki  1.23.2
BlockTest.php
Go to the documentation of this file.
1 <?php
2 
8 
10  private $block;
11  private $madeAt;
12 
13  /* variable used to save up the blockID we insert in this test suite */
14  private $blockId;
15 
16  protected function setUp() {
17  parent::setUp();
18  $this->setMwGlobals( array(
19  'wgLanguageCode' => 'en',
20  'wgContLang' => Language::factory( 'en' )
21  ) );
22  }
23 
24  function addDBData() {
25 
26  $user = User::newFromName( 'UTBlockee' );
27  if ( $user->getID() == 0 ) {
28  $user->addToDatabase();
29  $user->setPassword( 'UTBlockeePassword' );
30 
31  $user->saveSettings();
32  }
33 
34  // Delete the last round's block if it's still there
35  $oldBlock = Block::newFromTarget( 'UTBlockee' );
36  if ( $oldBlock ) {
37  // An old block will prevent our new one from saving.
38  $oldBlock->delete();
39  }
40 
41  $this->block = new Block( 'UTBlockee', $user->getID(), 0,
42  'Parce que', 0, false, time() + 100500
43  );
44  $this->madeAt = wfTimestamp( TS_MW );
45 
46  $this->block->insert();
47  // save up ID for use in assertion. Since ID is an autoincrement,
48  // its value might change depending on the order the tests are run.
49  // ApiBlockTest insert its own blocks!
50  $newBlockId = $this->block->getId();
51  if ( $newBlockId ) {
52  $this->blockId = $newBlockId;
53  } else {
54  throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" );
55  }
56 
57  $this->addXffBlocks();
58  }
59 
63  function dumpBlocks() {
64  $v = $this->db->select( 'ipblocks', '*' );
65  print "Got " . $v->numRows() . " rows. Full dump follow:\n";
66  foreach ( $v as $row ) {
67  print_r( $row );
68  }
69  }
70 
74  public function testINewFromTargetReturnsCorrectBlock() {
75  $this->assertTrue( $this->block->equals( Block::newFromTarget( 'UTBlockee' ) ), "newFromTarget() returns the same block as the one that was made" );
76  }
77 
81  public function testINewFromIDReturnsCorrectBlock() {
82  $this->assertTrue( $this->block->equals( Block::newFromID( $this->blockId ) ), "newFromID() returns the same block as the one that was made" );
83  }
84 
89  // delta to stop one-off errors when things happen to go over a second mark.
90  $delta = abs( $this->madeAt - $this->block->mTimestamp );
91  $this->assertLessThan( 2, $delta, "If no timestamp is specified, the block is recorded as time()" );
92  }
93 
102  public function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
103  $block = Block::newFromTarget( 'UTBlockee', $vagueTarget );
104  $this->assertTrue( $this->block->equals( $block ), "newFromTarget() returns the same block as the one that was made when given empty vagueTarget param " . var_export( $vagueTarget, true ) );
105  }
106 
107  public static function provideBug29116Data() {
108  return array(
109  array( null ),
110  array( '' ),
111  array( false )
112  );
113  }
114 
118  public function testBlockedUserCanNotCreateAccount() {
119  $username = 'BlockedUserToCreateAccountWith';
120  $u = User::newFromName( $username );
121  $u->setPassword( 'NotRandomPass' );
122  $u->addToDatabase();
123  unset( $u );
124 
125  // Sanity check
126  $this->assertNull(
127  Block::newFromTarget( $username ),
128  "$username should not be blocked"
129  );
130 
131  // Reload user
132  $u = User::newFromName( $username );
133  $this->assertFalse(
134  $u->isBlockedFromCreateAccount(),
135  "Our sandbox user should be able to create account before being blocked"
136  );
137 
138  // Foreign perspective (blockee not on current wiki)...
139  $block = new Block(
140  /* $address */ $username,
141  /* $user */ 14146,
142  /* $by */ 0,
143  /* $reason */ 'crosswiki block...',
144  /* $timestamp */ wfTimestampNow(),
145  /* $auto */ false,
146  /* $expiry */ $this->db->getInfinity(),
147  /* anonOnly */ false,
148  /* $createAccount */ true,
149  /* $enableAutoblock */ true,
150  /* $hideName (ipb_deleted) */ true,
151  /* $blockEmail */ true,
152  /* $allowUsertalk */ false,
153  /* $byName */ 'MetaWikiUser'
154  );
155  $block->insert();
156 
157  // Reload block from DB
158  $userBlock = Block::newFromTarget( $username );
159  $this->assertTrue(
160  (bool)$block->prevents( 'createaccount' ),
161  "Block object in DB should prevents 'createaccount'"
162  );
163 
164  $this->assertInstanceOf(
165  'Block',
166  $userBlock,
167  "'$username' block block object should be existent"
168  );
169 
170  // Reload user
171  $u = User::newFromName( $username );
172  $this->assertTrue(
173  (bool)$u->isBlockedFromCreateAccount(),
174  "Our sandbox user '$username' should NOT be able to create account"
175  );
176  }
177 
181  public function testCrappyCrossWikiBlocks() {
182  // Delete the last round's block if it's still there
183  $oldBlock = Block::newFromTarget( 'UserOnForeignWiki' );
184  if ( $oldBlock ) {
185  // An old block will prevent our new one from saving.
186  $oldBlock->delete();
187  }
188 
189  // Foreign perspective (blockee not on current wiki)...
190  $block = new Block(
191  /* $address */ 'UserOnForeignWiki',
192  /* $user */ 14146,
193  /* $by */ 0,
194  /* $reason */ 'crosswiki block...',
195  /* $timestamp */ wfTimestampNow(),
196  /* $auto */ false,
197  /* $expiry */ $this->db->getInfinity(),
198  /* anonOnly */ false,
199  /* $createAccount */ true,
200  /* $enableAutoblock */ true,
201  /* $hideName (ipb_deleted) */ true,
202  /* $blockEmail */ true,
203  /* $allowUsertalk */ false,
204  /* $byName */ 'MetaWikiUser'
205  );
206 
207  $res = $block->insert( $this->db );
208  $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
209 
210  // Local perspective (blockee on current wiki)...
211  $user = User::newFromName( 'UserOnForeignWiki' );
212  $user->addToDatabase();
213  // Set user ID to match the test value
214  $this->db->update( 'user', array( 'user_id' => 14146 ), array( 'user_id' => $user->getId() ) );
215  $user = null; // clear
216 
217  $block = Block::newFromID( $res['id'] );
218  $this->assertEquals( 'UserOnForeignWiki', $block->getTarget()->getName(), 'Correct blockee name' );
219  $this->assertEquals( '14146', $block->getTarget()->getId(), 'Correct blockee id' );
220  $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' );
221  $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
222  $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
223  }
224 
225  protected function addXffBlocks() {
226  static $inited = false;
227 
228  if ( $inited ) {
229  return;
230  }
231 
232  $inited = true;
233 
234  $blockList = array(
235  array( 'target' => '70.2.0.0/16',
236  'type' => Block::TYPE_RANGE,
237  'desc' => 'Range Hardblock',
238  'ACDisable' => false,
239  'isHardblock' => true,
240  'isAutoBlocking' => false,
241  ),
242  array( 'target' => '2001:4860:4001::/48',
243  'type' => Block::TYPE_RANGE,
244  'desc' => 'Range6 Hardblock',
245  'ACDisable' => false,
246  'isHardblock' => true,
247  'isAutoBlocking' => false,
248  ),
249  array( 'target' => '60.2.0.0/16',
250  'type' => Block::TYPE_RANGE,
251  'desc' => 'Range Softblock with AC Disabled',
252  'ACDisable' => true,
253  'isHardblock' => false,
254  'isAutoBlocking' => false,
255  ),
256  array( 'target' => '50.2.0.0/16',
257  'type' => Block::TYPE_RANGE,
258  'desc' => 'Range Softblock',
259  'ACDisable' => false,
260  'isHardblock' => false,
261  'isAutoBlocking' => false,
262  ),
263  array( 'target' => '50.1.1.1',
264  'type' => Block::TYPE_IP,
265  'desc' => 'Exact Softblock',
266  'ACDisable' => false,
267  'isHardblock' => false,
268  'isAutoBlocking' => false,
269  ),
270  );
271 
272  foreach ( $blockList as $insBlock ) {
273  $target = $insBlock['target'];
274 
275  if ( $insBlock['type'] === Block::TYPE_IP ) {
276  $target = User::newFromName( IP::sanitizeIP( $target ), false )->getName();
277  } elseif ( $insBlock['type'] === Block::TYPE_RANGE ) {
278  $target = IP::sanitizeRange( $target );
279  }
280 
281  $block = new Block();
282  $block->setTarget( $target );
283  $block->setBlocker( 'testblocker@global' );
284  $block->mReason = $insBlock['desc'];
285  $block->mExpiry = 'infinity';
286  $block->prevents( 'createaccount', $insBlock['ACDisable'] );
287  $block->isHardblock( $insBlock['isHardblock'] );
288  $block->isAutoblocking( $insBlock['isAutoBlocking'] );
289  $block->insert();
290  }
291  }
292 
293  public static function providerXff() {
294  return array(
295  array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
296  'count' => 2,
297  'result' => 'Range Hardblock'
298  ),
299  array( 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
300  'count' => 2,
301  'result' => 'Range Softblock with AC Disabled'
302  ),
303  array( 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
304  'count' => 2,
305  'result' => 'Exact Softblock'
306  ),
307  array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
308  'count' => 3,
309  'result' => 'Exact Softblock'
310  ),
311  array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
312  'count' => 2,
313  'result' => 'Range Hardblock'
314  ),
315  array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
316  'count' => 2,
317  'result' => 'Range Hardblock'
318  ),
319  array( 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
320  'count' => 2,
321  'result' => 'Range Softblock with AC Disabled'
322  ),
323  array( 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
324  'count' => 2,
325  'result' => 'Exact Softblock'
326  ),
327  array( 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT>, 60.2.1.1, 2.3.4.5',
328  'count' => 1,
329  'result' => 'Range Softblock with AC Disabled'
330  ),
331  array( 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
332  'count' => 2,
333  'result' => 'Range6 Hardblock'
334  ),
335  );
336  }
337 
343  public function testBlocksOnXff( $xff, $exCount, $exResult ) {
344  $list = array_map( 'trim', explode( ',', $xff ) );
345  $xffblocks = Block::getBlocksForIPList( $list, true );
346  $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
347  $block = Block::chooseBlock( $xffblocks, $list );
348  $this->assertEquals( $exResult, $block->mReason, 'Correct block type for XFF header ' . $xff );
349  }
350 }
Block\prevents
prevents( $action, $x=null)
Get/set whether the Block prevents a given action.
Definition: Block.php:886
Block\isHardblock
isHardblock( $x=null)
Get/set whether the Block is a hardblock (affects logged-in users on a given IP/range.
Definition: Block.php:861
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
Block\newFromID
static newFromID( $id)
Load a blocked user from their block id.
Definition: Block.php:104
Block\TYPE_IP
const TYPE_IP
Definition: Block.php:48
Block\TYPE_RANGE
const TYPE_RANGE
Definition: Block.php:49
Block\getBy
getBy()
Get the user id of the blocking sysop.
Definition: Block.php:819
Block\chooseBlock
static chooseBlock(array $blocks, array $ipChain)
From a list of multiple blocks, find the most exact and strongest Block.
Definition: Block.php:1089
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2483
BlockTest\providerXff
static providerXff()
Definition: BlockTest.php:292
BlockTest\addDBData
addDBData()
Stub.
Definition: BlockTest.php:23
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:970
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:388
BlockTest\addXffBlocks
addXffBlocks()
Definition: BlockTest.php:224
Block\insert
insert( $dbw=null)
Insert a block into the block table.
Definition: Block.php:398
BlockTest\testINewFromIDReturnsCorrectBlock
testINewFromIDReturnsCorrectBlock()
@covers Block::newFromID
Definition: BlockTest.php:80
BlockTest\$blockId
$blockId
Definition: BlockTest.php:13
MWException
MediaWiki exception.
Definition: MWException.php:26
BlockTest
@group Database @group Blocking
Definition: BlockTest.php:7
BlockTest\$madeAt
$madeAt
Definition: BlockTest.php:10
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Definition: MediaWikiTestCase.php:302
BlockTest\testBlockedUserCanNotCreateAccount
testBlockedUserCanNotCreateAccount()
@covers Block::prevents
Definition: BlockTest.php:117
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:2514
Block\isAutoblocking
isAutoblocking( $x=null)
Definition: Block.php:870
BlockTest\$block
Block $block
Definition: BlockTest.php:9
BlockTest\testCrappyCrossWikiBlocks
testCrappyCrossWikiBlocks()
@covers Block::insert
Definition: BlockTest.php:180
Block\getBlocksForIPList
static getBlocksForIPList(array $ipChain, $isAnon, $fromMaster=false)
Get all blocks that match any IP from an array of IP addresses.
Definition: Block.php:1010
TS_MW
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition: GlobalFunctions.php:2431
Block\getBlocker
getBlocker()
Get the user who implemented this block.
Definition: Block.php:1298
Block\setBlocker
setBlocker( $user)
Set the user who implemented (or will implement) this block.
Definition: Block.php:1306
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:6
Block\getTarget
getTarget()
Get the target for this particular Block.
Definition: Block.php:1273
block
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LoginAuthenticateAudit' this hook is for auditing only etc block
Definition: hooks.txt:1632
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:237
BlockTest\dumpBlocks
dumpBlocks()
debug function : dump the ipblocks table
Definition: BlockTest.php:62
BlockTest\testINewFromTargetReturnsCorrectBlock
testINewFromTargetReturnsCorrectBlock()
@covers Block::newFromTarget
Definition: BlockTest.php:73
IP\sanitizeIP
static sanitizeIP( $ip)
Convert an IP into a verbose, uppercase, normalized form.
Definition: IP.php:134
BlockTest\testBug26425BlockTimestampDefaultsToTime
testBug26425BlockTimestampDefaultsToTime()
per bug 26425
Definition: BlockTest.php:87
BlockTest\setUp
setUp()
Definition: BlockTest.php:15
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Block
Definition: Block.php:22
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:184
Block\getByName
getByName()
Get the username of the blocking sysop.
Definition: Block.php:831
BlockTest\testBlocksOnXff
testBlocksOnXff( $xff, $exCount, $exResult)
@dataProvider providerXff @covers Block::getBlocksForIPList @covers Block::chooseBlock
Definition: BlockTest.php:342
$res
$res
Definition: database.txt:21
IP\sanitizeRange
static sanitizeRange( $range)
Gets rid of unneeded numbers in quad-dotted/octet IP strings For example, 127.111....
Definition: IP.php:763
Block\setTarget
setTarget( $target)
Set the target for this block, and update $this->type accordingly.
Definition: Block.php:1290
BlockTest\testBug29116NewFromTargetWithEmptyIp
testBug29116NewFromTargetWithEmptyIp( $vagueTarget)
CheckUser since being changed to use Block::newFromTarget started failing because the new function di...
Definition: BlockTest.php:101
BlockTest\provideBug29116Data
static provideBug29116Data()
Definition: BlockTest.php:106