Merge "mw.widgets.Complex*: Fix setDisabled"
[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 use Wikimedia\Rdbms\MySQLMasterPos;
31 use Wikimedia\Rdbms\DatabaseMysqlBase;
32
33 /**
34 * Fake class around abstract class so we can call concrete methods.
35 */
36 class FakeDatabaseMysqlBase extends DatabaseMysqlBase {
37 // From Database
38 function __construct() {
39 $this->profiler = new ProfilerStub( [] );
40 $this->trxProfiler = new TransactionProfiler();
41 $this->cliMode = true;
42 $this->connLogger = new \Psr\Log\NullLogger();
43 $this->queryLogger = new \Psr\Log\NullLogger();
44 $this->errorLogger = function ( Exception $e ) {
45 wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
46 };
47 $this->currentDomain = DatabaseDomain::newUnspecified();
48 }
49
50 protected function closeConnection() {
51 }
52
53 protected function doQuery( $sql ) {
54 }
55
56 // From DatabaseMysql
57 protected function mysqlConnect( $realServer ) {
58 }
59
60 protected function mysqlSetCharset( $charset ) {
61 }
62
63 protected function mysqlFreeResult( $res ) {
64 }
65
66 protected function mysqlFetchObject( $res ) {
67 }
68
69 protected function mysqlFetchArray( $res ) {
70 }
71
72 protected function mysqlNumRows( $res ) {
73 }
74
75 protected function mysqlNumFields( $res ) {
76 }
77
78 protected function mysqlFieldName( $res, $n ) {
79 }
80
81 protected function mysqlFieldType( $res, $n ) {
82 }
83
84 protected function mysqlDataSeek( $res, $row ) {
85 }
86
87 protected function mysqlError( $conn = null ) {
88 }
89
90 protected function mysqlFetchField( $res, $n ) {
91 }
92
93 protected function mysqlRealEscapeString( $s ) {
94
95 }
96
97 function insertId() {
98 }
99
100 function lastErrno() {
101 }
102
103 function affectedRows() {
104 }
105
106 function getServerVersion() {
107 }
108 }
109
110 class DatabaseMysqlBaseTest extends MediaWikiTestCase {
111 /**
112 * @dataProvider provideDiapers
113 * @covers DatabaseMysqlBase::addIdentifierQuotes
114 */
115 public function testAddIdentifierQuotes( $expected, $in ) {
116 $db = new FakeDatabaseMysqlBase();
117 $quoted = $db->addIdentifierQuotes( $in );
118 $this->assertEquals( $expected, $quoted );
119 }
120
121 /**
122 * Feeds testAddIdentifierQuotes
123 *
124 * Named per T22281 convention.
125 */
126 function provideDiapers() {
127 return [
128 // Format: expected, input
129 [ '``', '' ],
130
131 // Yeah I really hate loosely typed PHP idiocies nowadays
132 [ '``', null ],
133
134 // Dear codereviewer, guess what addIdentifierQuotes()
135 // will return with thoses:
136 [ '``', false ],
137 [ '`1`', true ],
138
139 // We never know what could happen
140 [ '`0`', 0 ],
141 [ '`1`', 1 ],
142
143 // Whatchout! Should probably use something more meaningful
144 [ "`'`", "'" ], # single quote
145 [ '`"`', '"' ], # double quote
146 [ '````', '`' ], # backtick
147 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
148
149 // sneaky NUL bytes are lurking everywhere
150 [ '``', "\0" ],
151 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
152
153 // unicode chars
154 [
155 self::createUnicodeString( '`\u0001a\uFFFFb`' ),
156 self::createUnicodeString( '\u0001a\uFFFFb' )
157 ],
158 [
159 self::createUnicodeString( '`\u0001\uFFFF`' ),
160 self::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
161 ],
162 [ '`☃`', '☃' ],
163 [ '`メインページ`', 'メインページ' ],
164 [ '`Басты_бет`', 'Басты_бет' ],
165
166 // Real world:
167 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
168 [ '`Backtick: ```', 'Backtick: `' ],
169 [ '`This is a test`', 'This is a test' ],
170 ];
171 }
172
173 private static function createUnicodeString( $str ) {
174 return json_decode( '"' . $str . '"' );
175 }
176
177 function getMockForViews() {
178 $db = $this->getMockBuilder( 'DatabaseMysqli' )
179 ->disableOriginalConstructor()
180 ->setMethods( [ 'fetchRow', 'query' ] )
181 ->getMock();
182
183 $db->method( 'query' )
184 ->with( $this->anything() )
185 ->willReturn( new FakeResultWrapper( [
186 (object)[ 'Tables_in_' => 'view1' ],
187 (object)[ 'Tables_in_' => 'view2' ],
188 (object)[ 'Tables_in_' => 'myview' ]
189 ] ) );
190
191 return $db;
192 }
193 /**
194 * @covers DatabaseMysqlBase::listViews
195 */
196 function testListviews() {
197 $db = $this->getMockForViews();
198
199 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
200 $db->listViews() );
201
202 // Prefix filtering
203 $this->assertEquals( [ 'view1', 'view2' ],
204 $db->listViews( 'view' ) );
205 $this->assertEquals( [ 'myview' ],
206 $db->listViews( 'my' ) );
207 $this->assertEquals( [],
208 $db->listViews( 'UNUSED_PREFIX' ) );
209 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
210 $db->listViews( '' ) );
211 }
212
213 /**
214 * @dataProvider provideComparePositions
215 */
216 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 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 */
288 function testChannelsMatch( MySQLMasterPos $pos1, MySQLMasterPos $pos2, $matches ) {
289 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
290 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
291 }
292
293 function provideChannelPositions() {
294 return [
295 [
296 new MySQLMasterPos( 'db1034-bin.000876', '44' ),
297 new MySQLMasterPos( 'db1034-bin.000976', '74' ),
298 true
299 ],
300 [
301 new MySQLMasterPos( 'db1052-bin.000976', '999' ),
302 new MySQLMasterPos( 'db1052-bin.000976', '1000' ),
303 true
304 ],
305 [
306 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
307 new MySQLMasterPos( 'db1035-bin.000976', '10000' ),
308 false
309 ],
310 [
311 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
312 new MySQLMasterPos( 'trump2016.000976', '10000' ),
313 false
314 ],
315 ];
316 }
317
318 /**
319 * @dataProvider provideLagAmounts
320 */
321 function testPtHeartbeat( $lag ) {
322 $db = $this->getMockBuilder( 'DatabaseMysqli' )
323 ->disableOriginalConstructor()
324 ->setMethods( [
325 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
326 ->getMock();
327
328 $db->method( 'getLagDetectionMethod' )
329 ->willReturn( 'pt-heartbeat' );
330
331 $db->method( 'getMasterServerInfo' )
332 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
333
334 // Fake the current time.
335 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
336 $now = (float)$nowSec + (float)$nowSecFrac;
337 // Fake the heartbeat time.
338 // Work arounds for weak DataTime microseconds support.
339 $ptTime = $now - $lag;
340 $ptSec = (int)$ptTime;
341 $ptSecFrac = ( $ptTime - $ptSec );
342 $ptDateTime = new DateTime( "@$ptSec" );
343 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
344 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
345
346 $db->method( 'getHeartbeatData' )
347 ->with( [ 'server_id' => 172 ] )
348 ->willReturn( [ $ptTimeISO, $now ] );
349
350 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
351 $lagEst = $db->getLag();
352
353 $this->assertGreaterThan( $lag - .010, $lagEst, "Correct heatbeat lag" );
354 $this->assertLessThan( $lag + .010, $lagEst, "Correct heatbeat lag" );
355 }
356
357 function provideLagAmounts() {
358 return [
359 [ 0 ],
360 [ 0.3 ],
361 [ 6.5 ],
362 [ 10.1 ],
363 [ 200.2 ],
364 [ 400.7 ],
365 [ 600.22 ],
366 [ 1000.77 ],
367 ];
368 }
369 }