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