Simplify r101365, this cruft is not really needed
[lhc/web/wiklou.git] / tests / phpunit / includes / BlockTest.php
1 <?php
2
3 /**
4 * @group Database
5 * @group Blocking
6 */
7 class BlockTest extends MediaWikiLangTestCase {
8
9 private $block, $madeAt;
10
11 /* variable used to save up the blockID we insert in this test suite */
12 private $blockId;
13
14 function setUp() {
15 global $wgContLang;
16 parent::setUp();
17 $wgContLang = Language::factory( 'en' );
18 }
19
20 function addDBData() {
21 //$this->dumpBlocks();
22
23 $user = User::newFromName( 'UTBlockee' );
24 if( $user->getID() == 0 ) {
25 $user->addToDatabase();
26 $user->setPassword( 'UTBlockeePassword' );
27
28 $user->saveSettings();
29 }
30
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();
36 }
37
38 $this->block = new Block( 'UTBlockee', 1, 0,
39 'Parce que', 0, false, time() + 100500
40 );
41 $this->madeAt = wfTimestamp( TS_MW );
42
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?" );
52 }
53 }
54
55 /**
56 * debug function : dump the ipblocks table
57 */
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 );
63 }
64 }
65
66 function testInitializerFunctionsReturnCorrectBlock() {
67 // $this->dumpBlocks();
68
69 $this->assertTrue( $this->block->equals( Block::newFromTarget('UTBlockee') ), "newFromTarget() returns the same block as the one that was made");
70
71 $this->assertTrue( $this->block->equals( Block::newFromID( $this->blockId ) ), "newFromID() returns the same block as the one that was made");
72
73 }
74
75 /**
76 * per bug 26425
77 */
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()");
82
83 }
84
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.
88 *
89 * This stopped working with r84475 and friends: regression being fixed for bug 29116.
90 *
91 * @dataProvider dataBug29116
92 */
93 function testBug29116LoadWithEmptyIp( $vagueTarget ) {
94 $this->hideDeprecated( 'Block::load' );
95
96 $uid = User::idFromName( 'UTBlockee' );
97 $this->assertTrue( ($uid > 0), 'Must be able to look up the target user during tests' );
98
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" );
102
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 ) );
104 }
105
106 /**
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.
110 *
111 * @dataProvider dataBug29116
112 */
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 ) );
116 }
117
118 function dataBug29116() {
119 return array(
120 array( null ),
121 array( '' ),
122 array( false )
123 );
124 }
125 }