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