Merge "Proper handling of invalid/unknown time zones"
[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 Database
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 function insertId() {
93 }
94
95 function lastErrno() {
96 }
97
98 function affectedRows() {
99 }
100
101 function getServerVersion() {
102 }
103 }
104
105 class DatabaseMysqlBaseTest extends MediaWikiTestCase {
106 /**
107 * @dataProvider provideDiapers
108 * @covers DatabaseMysqlBase::addIdentifierQuotes
109 */
110 public function testAddIdentifierQuotes( $expected, $in ) {
111 $db = new FakeDatabaseMysqlBase();
112 $quoted = $db->addIdentifierQuotes( $in );
113 $this->assertEquals( $expected, $quoted );
114 }
115
116 /**
117 * Feeds testAddIdentifierQuotes
118 *
119 * Named per bug 20281 convention.
120 */
121 function provideDiapers() {
122 return [
123 // Format: expected, input
124 [ '``', '' ],
125
126 // Yeah I really hate loosely typed PHP idiocies nowadays
127 [ '``', null ],
128
129 // Dear codereviewer, guess what addIdentifierQuotes()
130 // will return with thoses:
131 [ '``', false ],
132 [ '`1`', true ],
133
134 // We never know what could happen
135 [ '`0`', 0 ],
136 [ '`1`', 1 ],
137
138 // Whatchout! Should probably use something more meaningful
139 [ "`'`", "'" ], # single quote
140 [ '`"`', '"' ], # double quote
141 [ '````', '`' ], # backtick
142 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
143
144 // sneaky NUL bytes are lurking everywhere
145 [ '``', "\0" ],
146 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
147
148 // unicode chars
149 [
150 self::createUnicodeString( '`\u0001a\uFFFFb`' ),
151 self::createUnicodeString( '\u0001a\uFFFFb' )
152 ],
153 [
154 self::createUnicodeString( '`\u0001\uFFFF`' ),
155 self::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
156 ],
157 [ '`☃`', '☃' ],
158 [ '`メインページ`', 'メインページ' ],
159 [ '`Басты_бет`', 'Басты_бет' ],
160
161 // Real world:
162 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
163 [ '`Backtick: ```', 'Backtick: `' ],
164 [ '`This is a test`', 'This is a test' ],
165 ];
166 }
167
168 private static function createUnicodeString( $str ) {
169 return json_decode( '"' . $str . '"' );
170 }
171
172 function getMockForViews() {
173 $db = $this->getMockBuilder( 'DatabaseMysqli' )
174 ->disableOriginalConstructor()
175 ->setMethods( [ 'fetchRow', 'query' ] )
176 ->getMock();
177
178 $db->method( 'query' )
179 ->with( $this->anything() )
180 ->willReturn( new FakeResultWrapper( [
181 (object)[ 'Tables_in_' => 'view1' ],
182 (object)[ 'Tables_in_' => 'view2' ],
183 (object)[ 'Tables_in_' => 'myview' ]
184 ] ) );
185
186 return $db;
187 }
188 /**
189 * @covers DatabaseMysqlBase::listViews
190 */
191 function testListviews() {
192 $db = $this->getMockForViews();
193
194 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
195 $db->listViews() );
196
197 // Prefix filtering
198 $this->assertEquals( [ 'view1', 'view2' ],
199 $db->listViews( 'view' ) );
200 $this->assertEquals( [ 'myview' ],
201 $db->listViews( 'my' ) );
202 $this->assertEquals( [],
203 $db->listViews( 'UNUSED_PREFIX' ) );
204 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
205 $db->listViews( '' ) );
206 }
207
208 /**
209 * @dataProvider provideComparePositions
210 */
211 function testHasReached( MySQLMasterPos $lowerPos, MySQLMasterPos $higherPos, $match ) {
212 if ( $match ) {
213 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
214
215 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
216 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
217 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
218 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
219 } else { // channels don't match
220 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
221
222 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
223 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
224 }
225 }
226
227 function provideComparePositions() {
228 return [
229 // Binlog style
230 [
231 new MySQLMasterPos( 'db1034-bin.000976', '843431247' ),
232 new MySQLMasterPos( 'db1034-bin.000976', '843431248' ),
233 true
234 ],
235 [
236 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
237 new MySQLMasterPos( 'db1034-bin.000976', '1000' ),
238 true
239 ],
240 [
241 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
242 new MySQLMasterPos( 'db1035-bin.000976', '1000' ),
243 false
244 ],
245 // MySQL GTID style
246 [
247 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:23' ),
248 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:24' ),
249 true
250 ],
251 [
252 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
253 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
254 true
255 ],
256 [
257 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
258 new MySQLMasterPos( 'db1-bin.2', '2', '1E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
259 false
260 ],
261 // MariaDB GTID style
262 [
263 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-23' ),
264 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-24' ),
265 true
266 ],
267 [
268 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-99' ),
269 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-100' ),
270 true
271 ],
272 [
273 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-999' ),
274 new MySQLMasterPos( 'db1-bin.2', '2', '254-11-1000' ),
275 false
276 ],
277 ];
278 }
279
280 /**
281 * @dataProvider provideChannelPositions
282 */
283 function testChannelsMatch( MySQLMasterPos $pos1, MySQLMasterPos $pos2, $matches ) {
284 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
285 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
286 }
287
288 function provideChannelPositions() {
289 return [
290 [
291 new MySQLMasterPos( 'db1034-bin.000876', '44' ),
292 new MySQLMasterPos( 'db1034-bin.000976', '74' ),
293 true
294 ],
295 [
296 new MySQLMasterPos( 'db1052-bin.000976', '999' ),
297 new MySQLMasterPos( 'db1052-bin.000976', '1000' ),
298 true
299 ],
300 [
301 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
302 new MySQLMasterPos( 'db1035-bin.000976', '10000' ),
303 false
304 ],
305 [
306 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
307 new MySQLMasterPos( 'trump2016.000976', '10000' ),
308 false
309 ],
310 ];
311 }
312
313 /**
314 * @dataProvider provideLagAmounts
315 */
316 function testPtHeartbeat( $lag ) {
317 $db = $this->getMockBuilder( 'DatabaseMysqli' )
318 ->disableOriginalConstructor()
319 ->setMethods( [
320 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
321 ->getMock();
322
323 $db->method( 'getLagDetectionMethod' )
324 ->willReturn( 'pt-heartbeat' );
325
326 $db->method( 'getMasterServerInfo' )
327 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
328
329 // Fake the current time.
330 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
331 $now = (float)$nowSec + (float)$nowSecFrac;
332 // Fake the heartbeat time.
333 // Work arounds for weak DataTime microseconds support.
334 $ptTime = $now - $lag;
335 $ptSec = (int)$ptTime;
336 $ptSecFrac = ( $ptTime - $ptSec );
337 $ptDateTime = new DateTime( "@$ptSec" );
338 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
339 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
340
341 $db->method( 'getHeartbeatData' )
342 ->with( [ 'server_id' => 172 ] )
343 ->willReturn( [ $ptTimeISO, $now ] );
344
345 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
346 $lagEst = $db->getLag();
347
348 $this->assertGreaterThan( $lag - .010, $lagEst, "Correct heatbeat lag" );
349 $this->assertLessThan( $lag + .010, $lagEst, "Correct heatbeat lag" );
350 }
351
352 function provideLagAmounts() {
353 return [
354 [ 0 ],
355 [ 0.3 ],
356 [ 6.5 ],
357 [ 10.1 ],
358 [ 200.2 ],
359 [ 400.7 ],
360 [ 600.22 ],
361 [ 1000.77 ],
362 ];
363 }
364 }