Merge "phpcs: Normalize methods declarations to "[final abstract] [visibility]"."
[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( array(
17 'wgLanguageCode' => 'en',
18 'wgContLang' => Language::factory( 'en' )
19 ) );
20 }
21
22 function addDBData() {
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', $user->getID(), 0,
40 'Parce que', 0, false, time() + 100500
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 // 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()" );
83
84 }
85
86 /**
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.
89 *
90 * This stopped working with r84475 and friends: regression being fixed for bug 29116.
91 *
92 * @dataProvider provideBug29116Data
93 */
94 function testBug29116LoadWithEmptyIp( $vagueTarget ) {
95 $this->hideDeprecated( 'Block::load' );
96
97 $uid = User::idFromName( 'UTBlockee' );
98 $this->assertTrue( ( $uid > 0 ), 'Must be able to look up the target user during tests' );
99
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" );
103
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 ) );
105 }
106
107 /**
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.
111 *
112 * @dataProvider provideBug29116Data
113 */
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 ) );
117 }
118
119 public static function provideBug29116Data() {
120 return array(
121 array( null ),
122 array( '' ),
123 array( false )
124 );
125 }
126
127 function testBlockedUserCanNotCreateAccount() {
128 $username = 'BlockedUserToCreateAccountWith';
129 $u = User::newFromName( $username );
130 $u->setPassword( 'NotRandomPass' );
131 $u->addToDatabase();
132 unset( $u );
133
134
135 // Sanity check
136 $this->assertNull(
137 Block::newFromTarget( $username ),
138 "$username should not be blocked"
139 );
140
141 // Reload user
142 $u = User::newFromName( $username );
143 $this->assertFalse(
144 $u->isBlockedFromCreateAccount(),
145 "Our sandbox user should be able to create account before being blocked"
146 );
147
148 // Foreign perspective (blockee not on current wiki)...
149 $block = new Block(
150 /* $address */ $username,
151 /* $user */ 14146,
152 /* $by */ 0,
153 /* $reason */ 'crosswiki block...',
154 /* $timestamp */ wfTimestampNow(),
155 /* $auto */ false,
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'
164 );
165 $block->insert();
166
167 // Reload block from DB
168 $userBlock = Block::newFromTarget( $username );
169 $this->assertTrue(
170 (bool)$block->prevents( 'createaccount' ),
171 "Block object in DB should prevents 'createaccount'"
172 );
173
174 $this->assertInstanceOf(
175 'Block',
176 $userBlock,
177 "'$username' block block object should be existent"
178 );
179
180 // Reload user
181 $u = User::newFromName( $username );
182 $this->assertTrue(
183 (bool)$u->isBlockedFromCreateAccount(),
184 "Our sandbox user '$username' should NOT be able to create account"
185 );
186 }
187
188 function testCrappyCrossWikiBlocks() {
189 // Delete the last round's block if it's still there
190 $oldBlock = Block::newFromTarget( 'UserOnForeignWiki' );
191 if ( $oldBlock ) {
192 // An old block will prevent our new one from saving.
193 $oldBlock->delete();
194 }
195
196 // Foreign perspective (blockee not on current wiki)...
197 $block = new Block(
198 /* $address */ 'UserOnForeignWiki',
199 /* $user */ 14146,
200 /* $by */ 0,
201 /* $reason */ 'crosswiki block...',
202 /* $timestamp */ wfTimestampNow(),
203 /* $auto */ false,
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'
212 );
213
214 $res = $block->insert( $this->db );
215 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
216
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
223
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' );
230 }
231 }