Merge "(bug 38406) Properly quote table names in DatabaseBase::tableName()"
[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', $user->getID(), 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
126 function testBlockedUserCanNotCreateAccount() {
127 $username = 'BlockedUserToCreateAccountWith';
128 $u = User::newFromName( $username );
129 $u->setPassword( 'NotRandomPass' );
130 $u->addToDatabase();
131 unset( $u );
132
133
134 // Sanity check
135 $this->assertNull(
136 Block::newFromTarget( $username ),
137 "$username should not be blocked"
138 );
139
140 // Reload user
141 $u = User::newFromName( $username );
142 $this->assertFalse(
143 $u->isBlockedFromCreateAccount(),
144 "Our sandbox user should be able to create account before being blocked"
145 );
146
147 // Foreign perspective (blockee not on current wiki)...
148 $block = new Block(
149 /* $address */ $username,
150 /* $user */ 14146,
151 /* $by */ 0,
152 /* $reason */ 'crosswiki block...',
153 /* $timestamp */ wfTimestampNow(),
154 /* $auto */ false,
155 /* $expiry */ $this->db->getInfinity(),
156 /* anonOnly */ false,
157 /* $createAccount */ true,
158 /* $enableAutoblock */ true,
159 /* $hideName (ipb_deleted) */ true,
160 /* $blockEmail */ true,
161 /* $allowUsertalk */ false,
162 /* $byName */ 'MetaWikiUser'
163 );
164 $block->insert();
165
166 // Reload block from DB
167 $userBlock = Block::newFromTarget( $username );
168 $this->assertTrue(
169 (bool) $block->prevents( 'createaccount' ),
170 "Block object in DB should prevents 'createaccount'"
171 );
172
173 $this->assertInstanceOf(
174 'Block',
175 $userBlock,
176 "'$username' block block object should be existent"
177 );
178
179 // Reload user
180 $u = User::newFromName( $username );
181 $this->assertTrue(
182 (bool) $u->isBlockedFromCreateAccount(),
183 "Our sandbox user '$username' should NOT be able to create account"
184 );
185 }
186
187 function testCrappyCrossWikiBlocks() {
188 // Delete the last round's block if it's still there
189 $oldBlock = Block::newFromTarget( 'UserOnForeignWiki' );
190 if ( $oldBlock ) {
191 // An old block will prevent our new one from saving.
192 $oldBlock->delete();
193 }
194
195 // Foreign perspective (blockee not on current wiki)...
196 $block = new Block(
197 /* $address */ 'UserOnForeignWiki',
198 /* $user */ 14146,
199 /* $by */ 0,
200 /* $reason */ 'crosswiki block...',
201 /* $timestamp */ wfTimestampNow(),
202 /* $auto */ false,
203 /* $expiry */ $this->db->getInfinity(),
204 /* anonOnly */ false,
205 /* $createAccount */ true,
206 /* $enableAutoblock */ true,
207 /* $hideName (ipb_deleted) */ true,
208 /* $blockEmail */ true,
209 /* $allowUsertalk */ false,
210 /* $byName */ 'MetaWikiUser'
211 );
212
213 $res = $block->insert( $this->db );
214 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
215
216 // Local perspective (blockee on current wiki)...
217 $user = User::newFromName( 'UserOnForeignWiki' );
218 $user->addToDatabase();
219 // Set user ID to match the test value
220 $this->db->update( 'user', array( 'user_id' => 14146 ), array( 'user_id' => $user->getId() ) );
221 $user = null; // clear
222
223 $block = Block::newFromID( $res['id'] );
224 $this->assertEquals( 'UserOnForeignWiki', $block->getTarget()->getName(), 'Correct blockee name' );
225 $this->assertEquals( '14146', $block->getTarget()->getId(), 'Correct blockee id' );
226 $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' );
227 $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
228 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
229 }
230 }