Set the DatabaseDomain in some tests classes for sanity
[lhc/web/wiklou.git] / tests / phpunit / includes / db / DatabaseMysqlBaseTest.php
1 <?php
2 /**
3 * Holds tests for DatabaseMysqlBase MediaWiki class.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @author Antoine Musso
22 * @author Bryan Davis
23 * @copyright © 2013 Antoine Musso
24 * @copyright © 2013 Bryan Davis
25 * @copyright © 2013 Wikimedia Foundation Inc.
26 */
27
28 /**
29 * Fake class around abstract class so we can call concrete methods.
30 */
31 class FakeDatabaseMysqlBase extends DatabaseMysqlBase {
32 // From DatabaseBase
33 function __construct() {
34 $this->profiler = new ProfilerStub( [] );
35 $this->trxProfiler = new TransactionProfiler();
36 $this->currentDomain = DatabaseDomain::newUnspecified();
37 }
38
39 protected function closeConnection() {
40 }
41
42 protected function doQuery( $sql ) {
43 }
44
45 // From DatabaseMysql
46 protected function mysqlConnect( $realServer ) {
47 }
48
49 protected function mysqlSetCharset( $charset ) {
50 }
51
52 protected function mysqlFreeResult( $res ) {
53 }
54
55 protected function mysqlFetchObject( $res ) {
56 }
57
58 protected function mysqlFetchArray( $res ) {
59 }
60
61 protected function mysqlNumRows( $res ) {
62 }
63
64 protected function mysqlNumFields( $res ) {
65 }
66
67 protected function mysqlFieldName( $res, $n ) {
68 }
69
70 protected function mysqlFieldType( $res, $n ) {
71 }
72
73 protected function mysqlDataSeek( $res, $row ) {
74 }
75
76 protected function mysqlError( $conn = null ) {
77 }
78
79 protected function mysqlFetchField( $res, $n ) {
80 }
81
82 protected function mysqlRealEscapeString( $s ) {
83
84 }
85
86 // From interface DatabaseType
87 function insertId() {
88 }
89
90 function lastErrno() {
91 }
92
93 function affectedRows() {
94 }
95
96 function getServerVersion() {
97 }
98 }
99
100 class DatabaseMysqlBaseTest extends MediaWikiTestCase {
101 /**
102 * @dataProvider provideDiapers
103 * @covers DatabaseMysqlBase::addIdentifierQuotes
104 */
105 public function testAddIdentifierQuotes( $expected, $in ) {
106 $db = new FakeDatabaseMysqlBase();
107 $quoted = $db->addIdentifierQuotes( $in );
108 $this->assertEquals( $expected, $quoted );
109 }
110
111 /**
112 * Feeds testAddIdentifierQuotes
113 *
114 * Named per bug 20281 convention.
115 */
116 function provideDiapers() {
117 return [
118 // Format: expected, input
119 [ '``', '' ],
120
121 // Yeah I really hate loosely typed PHP idiocies nowadays
122 [ '``', null ],
123
124 // Dear codereviewer, guess what addIdentifierQuotes()
125 // will return with thoses:
126 [ '``', false ],
127 [ '`1`', true ],
128
129 // We never know what could happen
130 [ '`0`', 0 ],
131 [ '`1`', 1 ],
132
133 // Whatchout! Should probably use something more meaningful
134 [ "`'`", "'" ], # single quote
135 [ '`"`', '"' ], # double quote
136 [ '````', '`' ], # backtick
137 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
138
139 // sneaky NUL bytes are lurking everywhere
140 [ '``', "\0" ],
141 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
142
143 // unicode chars
144 [
145 self::createUnicodeString( '`\u0001a\uFFFFb`' ),
146 self::createUnicodeString( '\u0001a\uFFFFb' )
147 ],
148 [
149 self::createUnicodeString( '`\u0001\uFFFF`' ),
150 self::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
151 ],
152 [ '`☃`', '☃' ],
153 [ '`メインページ`', 'メインページ' ],
154 [ '`Басты_бет`', 'Басты_бет' ],
155
156 // Real world:
157 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
158 [ '`Backtick: ```', 'Backtick: `' ],
159 [ '`This is a test`', 'This is a test' ],
160 ];
161 }
162
163 private static function createUnicodeString( $str ) {
164 return json_decode( '"' . $str . '"' );
165 }
166
167 function getMockForViews() {
168 $db = $this->getMockBuilder( 'DatabaseMysql' )
169 ->disableOriginalConstructor()
170 ->setMethods( [ 'fetchRow', 'query' ] )
171 ->getMock();
172
173 $db->method( 'query' )
174 ->with( $this->anything() )
175 ->willReturn( null );
176
177 $db->method( 'fetchRow' )
178 ->with( $this->anything() )
179 ->will( $this->onConsecutiveCalls(
180 [ 'Tables_in_' => 'view1' ],
181 [ 'Tables_in_' => 'view2' ],
182 [ 'Tables_in_' => 'myview' ],
183 false # no more rows
184 ) );
185 return $db;
186 }
187 /**
188 * @covers DatabaseMysqlBase::listViews
189 */
190 function testListviews() {
191 $db = $this->getMockForViews();
192
193 // The first call populate an internal cache of views
194 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
195 $db->listViews() );
196 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
197 $db->listViews() );
198
199 // Prefix filtering
200 $this->assertEquals( [ 'view1', 'view2' ],
201 $db->listViews( 'view' ) );
202 $this->assertEquals( [ 'myview' ],
203 $db->listViews( 'my' ) );
204 $this->assertEquals( [],
205 $db->listViews( 'UNUSED_PREFIX' ) );
206 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
207 $db->listViews( '' ) );
208 }
209
210 /**
211 * @covers DatabaseMysqlBase::isView
212 * @dataProvider provideViewExistanceChecks
213 */
214 function testIsView( $isView, $viewName ) {
215 $db = $this->getMockForViews();
216
217 switch ( $isView ) {
218 case true:
219 $this->assertTrue( $db->isView( $viewName ),
220 "$viewName should be considered a view" );
221 break;
222
223 case false:
224 $this->assertFalse( $db->isView( $viewName ),
225 "$viewName has not been defined as a view" );
226 break;
227 }
228
229 }
230
231 function provideViewExistanceChecks() {
232 return [
233 // format: whether it is a view, view name
234 [ true, 'view1' ],
235 [ true, 'view2' ],
236 [ true, 'myview' ],
237
238 [ false, 'user' ],
239
240 [ false, 'view10' ],
241 [ false, 'my' ],
242 [ false, 'OH_MY_GOD' ], # they killed kenny!
243 ];
244 }
245
246 /**
247 * @dataProvider provideComparePositions
248 */
249 function testHasReached( MySQLMasterPos $lowerPos, MySQLMasterPos $higherPos, $match ) {
250 if ( $match ) {
251 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
252
253 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
254 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
255 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
256 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
257 } else { // channels don't match
258 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
259
260 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
261 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
262 }
263 }
264
265 function provideComparePositions() {
266 return [
267 // Binlog style
268 [
269 new MySQLMasterPos( 'db1034-bin.000976', '843431247' ),
270 new MySQLMasterPos( 'db1034-bin.000976', '843431248' ),
271 true
272 ],
273 [
274 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
275 new MySQLMasterPos( 'db1034-bin.000976', '1000' ),
276 true
277 ],
278 [
279 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
280 new MySQLMasterPos( 'db1035-bin.000976', '1000' ),
281 false
282 ],
283 // MySQL GTID style
284 [
285 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:23' ),
286 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:24' ),
287 true
288 ],
289 [
290 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
291 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
292 true
293 ],
294 [
295 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
296 new MySQLMasterPos( 'db1-bin.2', '2', '1E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
297 false
298 ],
299 // MariaDB GTID style
300 [
301 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-23' ),
302 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-24' ),
303 true
304 ],
305 [
306 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-99' ),
307 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-100' ),
308 true
309 ],
310 [
311 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-999' ),
312 new MySQLMasterPos( 'db1-bin.2', '2', '254-11-1000' ),
313 false
314 ],
315 ];
316 }
317
318 /**
319 * @dataProvider provideChannelPositions
320 */
321 function testChannelsMatch( MySQLMasterPos $pos1, MySQLMasterPos $pos2, $matches ) {
322 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
323 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
324 }
325
326 function provideChannelPositions() {
327 return [
328 [
329 new MySQLMasterPos( 'db1034-bin.000876', '44' ),
330 new MySQLMasterPos( 'db1034-bin.000976', '74' ),
331 true
332 ],
333 [
334 new MySQLMasterPos( 'db1052-bin.000976', '999' ),
335 new MySQLMasterPos( 'db1052-bin.000976', '1000' ),
336 true
337 ],
338 [
339 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
340 new MySQLMasterPos( 'db1035-bin.000976', '10000' ),
341 false
342 ],
343 [
344 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
345 new MySQLMasterPos( 'trump2016.000976', '10000' ),
346 false
347 ],
348 ];
349 }
350
351 /**
352 * @dataProvider provideLagAmounts
353 */
354 function testPtHeartbeat( $lag ) {
355 $db = $this->getMockBuilder( 'DatabaseMysql' )
356 ->disableOriginalConstructor()
357 ->setMethods( [
358 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
359 ->getMock();
360
361 $db->method( 'getLagDetectionMethod' )
362 ->willReturn( 'pt-heartbeat' );
363
364 $db->method( 'getMasterServerInfo' )
365 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
366
367 // Fake the current time.
368 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
369 $now = (float)$nowSec + (float)$nowSecFrac;
370 // Fake the heartbeat time.
371 // Work arounds for weak DataTime microseconds support.
372 $ptTime = $now - $lag;
373 $ptSec = (int)$ptTime;
374 $ptSecFrac = ( $ptTime - $ptSec );
375 $ptDateTime = new DateTime( "@$ptSec" );
376 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
377 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
378
379 $db->method( 'getHeartbeatData' )
380 ->with( [ 'server_id' => 172 ] )
381 ->willReturn( [ $ptTimeISO, $now ] );
382
383 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
384 $lagEst = $db->getLag();
385
386 $this->assertGreaterThan( $lag - .010, $lagEst, "Correct heatbeat lag" );
387 $this->assertLessThan( $lag + .010, $lagEst, "Correct heatbeat lag" );
388 }
389
390 function provideLagAmounts() {
391 return [
392 [ 0 ],
393 [ 0.3 ],
394 [ 6.5 ],
395 [ 10.1 ],
396 [ 200.2 ],
397 [ 400.7 ],
398 [ 600.22 ],
399 [ 1000.77 ],
400 ];
401 }
402 }