7 class BlockTest
extends MediaWikiLangTestCase
{
9 private $block, $madeAt;
11 /* variable used to save up the blockID we insert in this test suite */
14 protected function setUp() {
16 $this->setMwGlobals( array(
17 'wgLanguageCode' => 'en',
18 'wgContLang' => Language
::factory( 'en' )
22 function addDBData() {
24 $user = User
::newFromName( 'UTBlockee' );
25 if ( $user->getID() == 0 ) {
26 $user->addToDatabase();
27 $user->setPassword( 'UTBlockeePassword' );
29 $user->saveSettings();
32 // Delete the last round's block if it's still there
33 $oldBlock = Block
::newFromTarget( 'UTBlockee' );
35 // An old block will prevent our new one from saving.
39 $this->block
= new Block( 'UTBlockee', $user->getID(), 0,
40 'Parce que', 0, false, time() +
100500
42 $this->madeAt
= wfTimestamp( TS_MW
);
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();
50 $this->blockId
= $newBlockId;
52 throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" );
57 * debug function : dump the ipblocks table
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 ) {
67 function testInitializerFunctionsReturnCorrectBlock() {
68 // $this->dumpBlocks();
70 $this->assertTrue( $this->block
->equals( Block
::newFromTarget('UTBlockee') ), "newFromTarget() returns the same block as the one that was made");
72 $this->assertTrue( $this->block
->equals( Block
::newFromID( $this->blockId
) ), "newFromID() returns the same block as the one that was made");
79 function testBug26425BlockTimestampDefaultsToTime() {
80 // delta to stop one-off errors when things happen to go over a second mark.
81 $delta = abs( $this->madeAt
- $this->block
->mTimestamp
);
82 $this->assertLessThan( 2, $delta, "If no timestamp is specified, the block is recorded as time()");
87 * This is the method previously used to load block info in CheckUser etc
88 * passing an empty value (empty string, null, etc) as the ip parameter bypasses IP lookup checks.
90 * This stopped working with r84475 and friends: regression being fixed for bug 29116.
92 * @dataProvider provideBug29116Data
94 function testBug29116LoadWithEmptyIp( $vagueTarget ) {
95 $this->hideDeprecated( 'Block::load' );
97 $uid = User
::idFromName( 'UTBlockee' );
98 $this->assertTrue( ($uid > 0), 'Must be able to look up the target user during tests' );
100 $block = new Block();
101 $ok = $block->load( $vagueTarget, $uid );
102 $this->assertTrue( $ok, "Block->load() with empty IP and user ID '$uid' should return a block" );
104 $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 ) );
108 * CheckUser since being changed to use Block::newFromTarget started failing
109 * because the new function didn't accept empty strings like Block::load()
110 * had. Regression bug 29116.
112 * @dataProvider provideBug29116Data
114 function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
115 $block = Block
::newFromTarget('UTBlockee', $vagueTarget);
116 $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 ) );
119 public static function provideBug29116Data() {
127 function testBlockedUserCanNotCreateAccount() {
128 $username = 'BlockedUserToCreateAccountWith';
129 $u = User
::newFromName( $username );
130 $u->setPassword( 'NotRandomPass' );
137 Block
::newFromTarget( $username ),
138 "$username should not be blocked"
142 $u = User
::newFromName( $username );
144 $u->isBlockedFromCreateAccount(),
145 "Our sandbox user should be able to create account before being blocked"
148 // Foreign perspective (blockee not on current wiki)...
150 /* $address */ $username,
153 /* $reason */ 'crosswiki block...',
154 /* $timestamp */ wfTimestampNow(),
156 /* $expiry */ $this->db
->getInfinity(),
157 /* anonOnly */ false,
158 /* $createAccount */ true,
159 /* $enableAutoblock */ true,
160 /* $hideName (ipb_deleted) */ true,
161 /* $blockEmail */ true,
162 /* $allowUsertalk */ false,
163 /* $byName */ 'MetaWikiUser'
167 // Reload block from DB
168 $userBlock = Block
::newFromTarget( $username );
170 (bool)$block->prevents( 'createaccount' ),
171 "Block object in DB should prevents 'createaccount'"
174 $this->assertInstanceOf(
177 "'$username' block block object should be existent"
181 $u = User
::newFromName( $username );
183 (bool)$u->isBlockedFromCreateAccount(),
184 "Our sandbox user '$username' should NOT be able to create account"
188 function testCrappyCrossWikiBlocks() {
189 // Delete the last round's block if it's still there
190 $oldBlock = Block
::newFromTarget( 'UserOnForeignWiki' );
192 // An old block will prevent our new one from saving.
196 // Foreign perspective (blockee not on current wiki)...
198 /* $address */ 'UserOnForeignWiki',
201 /* $reason */ 'crosswiki block...',
202 /* $timestamp */ wfTimestampNow(),
204 /* $expiry */ $this->db
->getInfinity(),
205 /* anonOnly */ false,
206 /* $createAccount */ true,
207 /* $enableAutoblock */ true,
208 /* $hideName (ipb_deleted) */ true,
209 /* $blockEmail */ true,
210 /* $allowUsertalk */ false,
211 /* $byName */ 'MetaWikiUser'
214 $res = $block->insert( $this->db
);
215 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
217 // Local perspective (blockee on current wiki)...
218 $user = User
::newFromName( 'UserOnForeignWiki' );
219 $user->addToDatabase();
220 // Set user ID to match the test value
221 $this->db
->update( 'user', array( 'user_id' => 14146 ), array( 'user_id' => $user->getId() ) );
222 $user = null; // clear
224 $block = Block
::newFromID( $res['id'] );
225 $this->assertEquals( 'UserOnForeignWiki', $block->getTarget()->getName(), 'Correct blockee name' );
226 $this->assertEquals( '14146', $block->getTarget()->getId(), 'Correct blockee id' );
227 $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' );
228 $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
229 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );