Merge "(bug 40154) On action=info show where this page redirects to and whether it...
[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 protected function setUp() {
15 parent::setUp();
16 $this->setMwGlobals( 'wgContLang', Language::factory( 'en' ) );
17 }
18
19 function addDBData() {
20
21 $user = User::newFromName( 'UTBlockee' );
22 if ( $user->getID() == 0 ) {
23 $user->addToDatabase();
24 $user->setPassword( 'UTBlockeePassword' );
25
26 $user->saveSettings();
27 }
28
29 // Delete the last round's block if it's still there
30 $oldBlock = Block::newFromTarget( 'UTBlockee' );
31 if ( $oldBlock ) {
32 // An old block will prevent our new one from saving.
33 $oldBlock->delete();
34 }
35
36 $this->block = new Block( 'UTBlockee', $user->getID(), 0,
37 'Parce que', 0, false, time() + 100500
38 );
39 $this->madeAt = wfTimestamp( TS_MW );
40
41 $this->block->insert();
42 // save up ID for use in assertion. Since ID is an autoincrement,
43 // its value might change depending on the order the tests are run.
44 // ApiBlockTest insert its own blocks!
45 $newBlockId = $this->block->getId();
46 if ( $newBlockId ) {
47 $this->blockId = $newBlockId;
48 } else {
49 throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" );
50 }
51 }
52
53 /**
54 * debug function : dump the ipblocks table
55 */
56 function dumpBlocks() {
57 $v = $this->db->query( 'SELECT * FROM unittest_ipblocks' );
58 print "Got " . $v->numRows() . " rows. Full dump follow:\n";
59 foreach( $v as $row ) {
60 print_r( $row );
61 }
62 }
63
64 function testInitializerFunctionsReturnCorrectBlock() {
65 // $this->dumpBlocks();
66
67 $this->assertTrue( $this->block->equals( Block::newFromTarget('UTBlockee') ), "newFromTarget() returns the same block as the one that was made");
68
69 $this->assertTrue( $this->block->equals( Block::newFromID( $this->blockId ) ), "newFromID() returns the same block as the one that was made");
70
71 }
72
73 /**
74 * per bug 26425
75 */
76 function testBug26425BlockTimestampDefaultsToTime() {
77 // delta to stop one-off errors when things happen to go over a second mark.
78 $delta = abs( $this->madeAt - $this->block->mTimestamp );
79 $this->assertLessThan( 2, $delta, "If no timestamp is specified, the block is recorded as time()");
80
81 }
82
83 /**
84 * This is the method previously used to load block info in CheckUser etc
85 * passing an empty value (empty string, null, etc) as the ip parameter bypasses IP lookup checks.
86 *
87 * This stopped working with r84475 and friends: regression being fixed for bug 29116.
88 *
89 * @dataProvider provideBug29116Data
90 */
91 function testBug29116LoadWithEmptyIp( $vagueTarget ) {
92 $this->hideDeprecated( 'Block::load' );
93
94 $uid = User::idFromName( 'UTBlockee' );
95 $this->assertTrue( ($uid > 0), 'Must be able to look up the target user during tests' );
96
97 $block = new Block();
98 $ok = $block->load( $vagueTarget, $uid );
99 $this->assertTrue( $ok, "Block->load() with empty IP and user ID '$uid' should return a block" );
100
101 $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 ) );
102 }
103
104 /**
105 * CheckUser since being changed to use Block::newFromTarget started failing
106 * because the new function didn't accept empty strings like Block::load()
107 * had. Regression bug 29116.
108 *
109 * @dataProvider provideBug29116Data
110 */
111 function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
112 $block = Block::newFromTarget('UTBlockee', $vagueTarget);
113 $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 ) );
114 }
115
116 public static function provideBug29116Data() {
117 return array(
118 array( null ),
119 array( '' ),
120 array( false )
121 );
122 }
123
124 function testBlockedUserCanNotCreateAccount() {
125 $username = 'BlockedUserToCreateAccountWith';
126 $u = User::newFromName( $username );
127 $u->setPassword( 'NotRandomPass' );
128 $u->addToDatabase();
129 unset( $u );
130
131
132 // Sanity check
133 $this->assertNull(
134 Block::newFromTarget( $username ),
135 "$username should not be blocked"
136 );
137
138 // Reload user
139 $u = User::newFromName( $username );
140 $this->assertFalse(
141 $u->isBlockedFromCreateAccount(),
142 "Our sandbox user should be able to create account before being blocked"
143 );
144
145 // Foreign perspective (blockee not on current wiki)...
146 $block = new Block(
147 /* $address */ $username,
148 /* $user */ 14146,
149 /* $by */ 0,
150 /* $reason */ 'crosswiki block...',
151 /* $timestamp */ wfTimestampNow(),
152 /* $auto */ false,
153 /* $expiry */ $this->db->getInfinity(),
154 /* anonOnly */ false,
155 /* $createAccount */ true,
156 /* $enableAutoblock */ true,
157 /* $hideName (ipb_deleted) */ true,
158 /* $blockEmail */ true,
159 /* $allowUsertalk */ false,
160 /* $byName */ 'MetaWikiUser'
161 );
162 $block->insert();
163
164 // Reload block from DB
165 $userBlock = Block::newFromTarget( $username );
166 $this->assertTrue(
167 (bool) $block->prevents( 'createaccount' ),
168 "Block object in DB should prevents 'createaccount'"
169 );
170
171 $this->assertInstanceOf(
172 'Block',
173 $userBlock,
174 "'$username' block block object should be existent"
175 );
176
177 // Reload user
178 $u = User::newFromName( $username );
179 $this->assertTrue(
180 (bool) $u->isBlockedFromCreateAccount(),
181 "Our sandbox user '$username' should NOT be able to create account"
182 );
183 }
184
185 function testCrappyCrossWikiBlocks() {
186 // Delete the last round's block if it's still there
187 $oldBlock = Block::newFromTarget( 'UserOnForeignWiki' );
188 if ( $oldBlock ) {
189 // An old block will prevent our new one from saving.
190 $oldBlock->delete();
191 }
192
193 // Foreign perspective (blockee not on current wiki)...
194 $block = new Block(
195 /* $address */ 'UserOnForeignWiki',
196 /* $user */ 14146,
197 /* $by */ 0,
198 /* $reason */ 'crosswiki block...',
199 /* $timestamp */ wfTimestampNow(),
200 /* $auto */ false,
201 /* $expiry */ $this->db->getInfinity(),
202 /* anonOnly */ false,
203 /* $createAccount */ true,
204 /* $enableAutoblock */ true,
205 /* $hideName (ipb_deleted) */ true,
206 /* $blockEmail */ true,
207 /* $allowUsertalk */ false,
208 /* $byName */ 'MetaWikiUser'
209 );
210
211 $res = $block->insert( $this->db );
212 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
213
214 // Local perspective (blockee on current wiki)...
215 $user = User::newFromName( 'UserOnForeignWiki' );
216 $user->addToDatabase();
217 // Set user ID to match the test value
218 $this->db->update( 'user', array( 'user_id' => 14146 ), array( 'user_id' => $user->getId() ) );
219 $user = null; // clear
220
221 $block = Block::newFromID( $res['id'] );
222 $this->assertEquals( 'UserOnForeignWiki', $block->getTarget()->getName(), 'Correct blockee name' );
223 $this->assertEquals( '14146', $block->getTarget()->getId(), 'Correct blockee id' );
224 $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' );
225 $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
226 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
227 }
228 }