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