Simplify r101365, this cruft is not really needed
[mediawiki.git] / tests / phpunit / includes / BlockTest.php
blob022da219ceeb28378a9f25f7093b32da65b833c5
1 <?php
3 /**
4 * @group Database
5 * @group Blocking
6 */
7 class BlockTest extends MediaWikiLangTestCase {
9 private $block, $madeAt;
11 /* variable used to save up the blockID we insert in this test suite */
12 private $blockId;
14 function setUp() {
15 global $wgContLang;
16 parent::setUp();
17 $wgContLang = Language::factory( 'en' );
20 function addDBData() {
21 //$this->dumpBlocks();
23 $user = User::newFromName( 'UTBlockee' );
24 if( $user->getID() == 0 ) {
25 $user->addToDatabase();
26 $user->setPassword( 'UTBlockeePassword' );
28 $user->saveSettings();
31 // Delete the last round's block if it's still there
32 $oldBlock = Block::newFromTarget( 'UTBlockee' );
33 if ( $oldBlock ) {
34 // An old block will prevent our new one from saving.
35 $oldBlock->delete();
38 $this->block = new Block( 'UTBlockee', 1, 0,
39 'Parce que', 0, false, time() + 100500
41 $this->madeAt = wfTimestamp( TS_MW );
43 $this->block->insert();
44 // save up ID for use in assertion. Since ID is an autoincrement,
45 // its value might change depending on the order the tests are run.
46 // ApiBlockTest insert its own blocks!
47 $newBlockId = $this->block->getId();
48 if ($newBlockId) {
49 $this->blockId = $newBlockId;
50 } else {
51 throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" );
55 /**
56 * debug function : dump the ipblocks table
58 function dumpBlocks() {
59 $v = $this->db->query( 'SELECT * FROM unittest_ipblocks' );
60 print "Got " . $v->numRows() . " rows. Full dump follow:\n";
61 foreach( $v as $row ) {
62 print_r( $row );
66 function testInitializerFunctionsReturnCorrectBlock() {
67 // $this->dumpBlocks();
69 $this->assertTrue( $this->block->equals( Block::newFromTarget('UTBlockee') ), "newFromTarget() returns the same block as the one that was made");
71 $this->assertTrue( $this->block->equals( Block::newFromID( $this->blockId ) ), "newFromID() returns the same block as the one that was made");
75 /**
76 * per bug 26425
78 function testBug26425BlockTimestampDefaultsToTime() {
79 // delta to stop one-off errors when things happen to go over a second mark.
80 $delta = abs( $this->madeAt - $this->block->mTimestamp );
81 $this->assertLessThan( 2, $delta, "If no timestamp is specified, the block is recorded as time()");
85 /**
86 * This is the method previously used to load block info in CheckUser etc
87 * passing an empty value (empty string, null, etc) as the ip parameter bypasses IP lookup checks.
89 * This stopped working with r84475 and friends: regression being fixed for bug 29116.
91 * @dataProvider dataBug29116
93 function testBug29116LoadWithEmptyIp( $vagueTarget ) {
94 $this->hideDeprecated( 'Block::load' );
96 $uid = User::idFromName( 'UTBlockee' );
97 $this->assertTrue( ($uid > 0), 'Must be able to look up the target user during tests' );
99 $block = new Block();
100 $ok = $block->load( $vagueTarget, $uid );
101 $this->assertTrue( $ok, "Block->load() with empty IP and user ID '$uid' should return a block" );
103 $this->assertTrue( $this->block->equals( $block ), "Block->load() returns the same block as the one that was made when given empty ip param " . var_export( $vagueTarget, true ) );
107 * CheckUser since being changed to use Block::newFromTarget started failing
108 * because the new function didn't accept empty strings like Block::load()
109 * had. Regression bug 29116.
111 * @dataProvider dataBug29116
113 function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
114 $block = Block::newFromTarget('UTBlockee', $vagueTarget);
115 $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 ) );
118 function dataBug29116() {
119 return array(
120 array( null ),
121 array( '' ),
122 array( false )