Move DatabaseDomain to Rdbms namespace
[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 use Wikimedia\Rdbms\TransactionProfiler;
29 use Wikimedia\Rdbms\DatabaseDomain;
30
31 /**
32 * Fake class around abstract class so we can call concrete methods.
33 */
34 class FakeDatabaseMysqlBase extends DatabaseMysqlBase {
35 // From Database
36 function __construct() {
37 $this->profiler = new ProfilerStub( [] );
38 $this->trxProfiler = new TransactionProfiler();
39 $this->cliMode = true;
40 $this->connLogger = new \Psr\Log\NullLogger();
41 $this->queryLogger = new \Psr\Log\NullLogger();
42 $this->errorLogger = function ( Exception $e ) {
43 wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
44 };
45 $this->currentDomain = DatabaseDomain::newUnspecified();
46 }
47
48 protected function closeConnection() {
49 }
50
51 protected function doQuery( $sql ) {
52 }
53
54 // From DatabaseMysql
55 protected function mysqlConnect( $realServer ) {
56 }
57
58 protected function mysqlSetCharset( $charset ) {
59 }
60
61 protected function mysqlFreeResult( $res ) {
62 }
63
64 protected function mysqlFetchObject( $res ) {
65 }
66
67 protected function mysqlFetchArray( $res ) {
68 }
69
70 protected function mysqlNumRows( $res ) {
71 }
72
73 protected function mysqlNumFields( $res ) {
74 }
75
76 protected function mysqlFieldName( $res, $n ) {
77 }
78
79 protected function mysqlFieldType( $res, $n ) {
80 }
81
82 protected function mysqlDataSeek( $res, $row ) {
83 }
84
85 protected function mysqlError( $conn = null ) {
86 }
87
88 protected function mysqlFetchField( $res, $n ) {
89 }
90
91 protected function mysqlRealEscapeString( $s ) {
92
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 MediaWikiTestCase {
109 /**
110 * @dataProvider provideDiapers
111 * @covers 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 bug 20281 convention.
123 */
124 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 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 * @covers DatabaseMysqlBase::listViews
193 */
194 function testListviews() {
195 $db = $this->getMockForViews();
196
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 * @dataProvider provideComparePositions
213 */
214 function testHasReached( MySQLMasterPos $lowerPos, MySQLMasterPos $higherPos, $match ) {
215 if ( $match ) {
216 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
217
218 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
219 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
220 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
221 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
222 } else { // channels don't match
223 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
224
225 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
226 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
227 }
228 }
229
230 function provideComparePositions() {
231 return [
232 // Binlog style
233 [
234 new MySQLMasterPos( 'db1034-bin.000976', '843431247' ),
235 new MySQLMasterPos( 'db1034-bin.000976', '843431248' ),
236 true
237 ],
238 [
239 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
240 new MySQLMasterPos( 'db1034-bin.000976', '1000' ),
241 true
242 ],
243 [
244 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
245 new MySQLMasterPos( 'db1035-bin.000976', '1000' ),
246 false
247 ],
248 // MySQL GTID style
249 [
250 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:23' ),
251 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:24' ),
252 true
253 ],
254 [
255 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
256 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
257 true
258 ],
259 [
260 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
261 new MySQLMasterPos( 'db1-bin.2', '2', '1E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
262 false
263 ],
264 // MariaDB GTID style
265 [
266 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-23' ),
267 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-24' ),
268 true
269 ],
270 [
271 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-99' ),
272 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-100' ),
273 true
274 ],
275 [
276 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-999' ),
277 new MySQLMasterPos( 'db1-bin.2', '2', '254-11-1000' ),
278 false
279 ],
280 ];
281 }
282
283 /**
284 * @dataProvider provideChannelPositions
285 */
286 function testChannelsMatch( MySQLMasterPos $pos1, MySQLMasterPos $pos2, $matches ) {
287 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
288 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
289 }
290
291 function provideChannelPositions() {
292 return [
293 [
294 new MySQLMasterPos( 'db1034-bin.000876', '44' ),
295 new MySQLMasterPos( 'db1034-bin.000976', '74' ),
296 true
297 ],
298 [
299 new MySQLMasterPos( 'db1052-bin.000976', '999' ),
300 new MySQLMasterPos( 'db1052-bin.000976', '1000' ),
301 true
302 ],
303 [
304 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
305 new MySQLMasterPos( 'db1035-bin.000976', '10000' ),
306 false
307 ],
308 [
309 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
310 new MySQLMasterPos( 'trump2016.000976', '10000' ),
311 false
312 ],
313 ];
314 }
315
316 /**
317 * @dataProvider provideLagAmounts
318 */
319 function testPtHeartbeat( $lag ) {
320 $db = $this->getMockBuilder( 'DatabaseMysqli' )
321 ->disableOriginalConstructor()
322 ->setMethods( [
323 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
324 ->getMock();
325
326 $db->method( 'getLagDetectionMethod' )
327 ->willReturn( 'pt-heartbeat' );
328
329 $db->method( 'getMasterServerInfo' )
330 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
331
332 // Fake the current time.
333 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
334 $now = (float)$nowSec + (float)$nowSecFrac;
335 // Fake the heartbeat time.
336 // Work arounds for weak DataTime microseconds support.
337 $ptTime = $now - $lag;
338 $ptSec = (int)$ptTime;
339 $ptSecFrac = ( $ptTime - $ptSec );
340 $ptDateTime = new DateTime( "@$ptSec" );
341 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
342 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
343
344 $db->method( 'getHeartbeatData' )
345 ->with( [ 'server_id' => 172 ] )
346 ->willReturn( [ $ptTimeISO, $now ] );
347
348 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
349 $lagEst = $db->getLag();
350
351 $this->assertGreaterThan( $lag - .010, $lagEst, "Correct heatbeat lag" );
352 $this->assertLessThan( $lag + .010, $lagEst, "Correct heatbeat lag" );
353 }
354
355 function provideLagAmounts() {
356 return [
357 [ 0 ],
358 [ 0.3 ],
359 [ 6.5 ],
360 [ 10.1 ],
361 [ 200.2 ],
362 [ 400.7 ],
363 [ 600.22 ],
364 [ 1000.77 ],
365 ];
366 }
367 }