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