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