Fix for BlockTest -- when setup ran multiple times, the block entry wouldn't get...
[lhc/web/wiklou.git] / tests / phpunit / includes / BlockTest.php
1 <?php
2
3 /**
4 * @group Database
5 */
6 class BlockTest extends MediaWikiLangTestCase {
7
8 const REASON = "Some reason";
9
10 private $block, $madeAt;
11
12 /* variable used to save up the blockID we insert in this test suite */
13 private $blockId;
14
15 function setUp() {
16 global $wgContLang;
17 parent::setUp();
18 $wgContLang = Language::factory( 'en' );
19 }
20
21 function addDBData() {
22 //$this->dumpBlocks();
23
24 $user = User::newFromName( 'UTBlockee' );
25 if( $user->getID() == 0 ) {
26 $user->addToDatabase();
27 $user->setPassword( 'UTBlockeePassword' );
28
29 $user->saveSettings();
30 }
31
32 // Delete the last round's block if it's still there
33 $oldBlock = Block::newFromTarget( 'UTBlockee' );
34 if ( $oldBlock ) {
35 // An old block will prevent our new one from saving.
36 $oldBlock->delete();
37 }
38
39 $this->block = new Block( 'UTBlockee', 1, 0,
40 self::REASON
41 );
42 $this->madeAt = wfTimestamp( TS_MW );
43
44 $this->block->insert();
45 // save up ID for use in assertion. Since ID is an autoincrement,
46 // its value might change depending on the order the tests are run.
47 // ApiBlockTest insert its own blocks!
48 $newBlockId = $this->block->getId();
49 if ($newBlockId) {
50 $this->blockId = $newBlockId;
51 } else {
52 throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" );
53 }
54 }
55
56 /**
57 * debug function : dump the ipblocks table
58 */
59 function dumpBlocks() {
60 $v = $this->db->query( 'SELECT * FROM unittest_ipblocks' );
61 print "Got " . $v->numRows() . " rows. Full dump follow:\n";
62 foreach( $v as $row ) {
63 print_r( $row );
64 }
65 }
66
67 function testInitializerFunctionsReturnCorrectBlock() {
68 // $this->dumpBlocks();
69
70 $this->assertTrue( $this->block->equals( Block::newFromTarget('UTBlockee') ), "newFromTarget() returns the same block as the one that was made");
71
72 $this->assertTrue( $this->block->equals( Block::newFromID( $this->blockId ) ), "newFromID() returns the same block as the one that was made");
73
74 }
75
76 /**
77 * per bug 26425
78 */
79 function testBug26425BlockTimestampDefaultsToTime() {
80
81 $this->assertEquals( $this->madeAt, $this->block->mTimestamp, "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 $block = new Block();
95 $block->load( $vagueTarget, 'UTBlockee' );
96 $this->assertTrue( $this->block->equals( Block::newFromTarget('UTBlockee', $vagueTarget) ), "Block->load() returns the same block as the one that was made when given empty ip param " . var_export( $vagueTarget, true ) );
97 }
98
99 /**
100 * CheckUser since being changed to use Block::newFromTarget started failing
101 * because the new function didn't accept empty strings like Block::load()
102 * had. Regression bug 29116.
103 *
104 * @dataProvider dataBug29116
105 */
106 function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
107 $block = Block::newFromTarget('UTBlockee', $vagueTarget);
108 $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 ) );
109 }
110
111 function dataBug29116() {
112 return array(
113 array( null ),
114 array( '' ),
115 array( false )
116 );
117 }
118 }
119