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