Merge "Rm unused 'remembermypassword' message, doc another"
[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 DatabaseBase
33 function __construct() {
34 $this->profiler = new ProfilerStub( [] );
35 $this->trxProfiler = new TransactionProfiler();
36 }
37
38 protected function closeConnection() {
39 }
40
41 protected function doQuery( $sql ) {
42 }
43
44 // From DatabaseMysql
45 protected function mysqlConnect( $realServer ) {
46 }
47
48 protected function mysqlSetCharset( $charset ) {
49 }
50
51 protected function mysqlFreeResult( $res ) {
52 }
53
54 protected function mysqlFetchObject( $res ) {
55 }
56
57 protected function mysqlFetchArray( $res ) {
58 }
59
60 protected function mysqlNumRows( $res ) {
61 }
62
63 protected function mysqlNumFields( $res ) {
64 }
65
66 protected function mysqlFieldName( $res, $n ) {
67 }
68
69 protected function mysqlFieldType( $res, $n ) {
70 }
71
72 protected function mysqlDataSeek( $res, $row ) {
73 }
74
75 protected function mysqlError( $conn = null ) {
76 }
77
78 protected function mysqlFetchField( $res, $n ) {
79 }
80
81 protected function mysqlRealEscapeString( $s ) {
82
83 }
84
85 // From interface DatabaseType
86 function insertId() {
87 }
88
89 function lastErrno() {
90 }
91
92 function affectedRows() {
93 }
94
95 function getServerVersion() {
96 }
97 }
98
99 class DatabaseMysqlBaseTest extends MediaWikiTestCase {
100 /**
101 * @dataProvider provideDiapers
102 * @covers DatabaseMysqlBase::addIdentifierQuotes
103 */
104 public function testAddIdentifierQuotes( $expected, $in ) {
105 $db = new FakeDatabaseMysqlBase();
106 $quoted = $db->addIdentifierQuotes( $in );
107 $this->assertEquals( $expected, $quoted );
108 }
109
110 /**
111 * Feeds testAddIdentifierQuotes
112 *
113 * Named per bug 20281 convention.
114 */
115 function provideDiapers() {
116 return [
117 // Format: expected, input
118 [ '``', '' ],
119
120 // Yeah I really hate loosely typed PHP idiocies nowadays
121 [ '``', null ],
122
123 // Dear codereviewer, guess what addIdentifierQuotes()
124 // will return with thoses:
125 [ '``', false ],
126 [ '`1`', true ],
127
128 // We never know what could happen
129 [ '`0`', 0 ],
130 [ '`1`', 1 ],
131
132 // Whatchout! Should probably use something more meaningful
133 [ "`'`", "'" ], # single quote
134 [ '`"`', '"' ], # double quote
135 [ '````', '`' ], # backtick
136 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
137
138 // sneaky NUL bytes are lurking everywhere
139 [ '``', "\0" ],
140 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
141
142 // unicode chars
143 [
144 self::createUnicodeString( '`\u0001a\uFFFFb`' ),
145 self::createUnicodeString( '\u0001a\uFFFFb' )
146 ],
147 [
148 self::createUnicodeString( '`\u0001\uFFFF`' ),
149 self::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
150 ],
151 [ '`☃`', '☃' ],
152 [ '`メインページ`', 'メインページ' ],
153 [ '`Басты_бет`', 'Басты_бет' ],
154
155 // Real world:
156 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
157 [ '`Backtick: ```', 'Backtick: `' ],
158 [ '`This is a test`', 'This is a test' ],
159 ];
160 }
161
162 private static function createUnicodeString( $str ) {
163 return json_decode( '"' . $str . '"' );
164 }
165
166 function getMockForViews() {
167 $db = $this->getMockBuilder( 'DatabaseMysql' )
168 ->disableOriginalConstructor()
169 ->setMethods( [ 'fetchRow', 'query' ] )
170 ->getMock();
171
172 $db->expects( $this->any() )
173 ->method( 'query' )
174 ->with( $this->anything() )
175 ->will(
176 $this->returnValue( null )
177 );
178
179 $db->expects( $this->any() )
180 ->method( 'fetchRow' )
181 ->with( $this->anything() )
182 ->will( $this->onConsecutiveCalls(
183 [ 'Tables_in_' => 'view1' ],
184 [ 'Tables_in_' => 'view2' ],
185 [ 'Tables_in_' => 'myview' ],
186 false # no more rows
187 ) );
188 return $db;
189 }
190 /**
191 * @covers DatabaseMysqlBase::listViews
192 */
193 function testListviews() {
194 $db = $this->getMockForViews();
195
196 // The first call populate an internal cache of views
197 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
198 $db->listViews() );
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 * @covers DatabaseMysqlBase::isView
215 * @dataProvider provideViewExistanceChecks
216 */
217 function testIsView( $isView, $viewName ) {
218 $db = $this->getMockForViews();
219
220 switch ( $isView ) {
221 case true:
222 $this->assertTrue( $db->isView( $viewName ),
223 "$viewName should be considered a view" );
224 break;
225
226 case false:
227 $this->assertFalse( $db->isView( $viewName ),
228 "$viewName has not been defined as a view" );
229 break;
230 }
231
232 }
233
234 function provideViewExistanceChecks() {
235 return [
236 // format: whether it is a view, view name
237 [ true, 'view1' ],
238 [ true, 'view2' ],
239 [ true, 'myview' ],
240
241 [ false, 'user' ],
242
243 [ false, 'view10' ],
244 [ false, 'my' ],
245 [ false, 'OH_MY_GOD' ], # they killed kenny!
246 ];
247 }
248
249 /**
250 * @dataProvider provideComparePositions
251 */
252 function testHasReached( MySQLMasterPos $lowerPos, MySQLMasterPos $higherPos, $match ) {
253 if ( $match ) {
254 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
255
256 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
257 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
258 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
259 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
260 } else { // channels don't match
261 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
262
263 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
264 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
265 }
266 }
267
268 function provideComparePositions() {
269 return [
270 // Binlog style
271 [
272 new MySQLMasterPos( 'db1034-bin.000976', '843431247' ),
273 new MySQLMasterPos( 'db1034-bin.000976', '843431248' ),
274 true
275 ],
276 [
277 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
278 new MySQLMasterPos( 'db1034-bin.000976', '1000' ),
279 true
280 ],
281 [
282 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
283 new MySQLMasterPos( 'db1035-bin.000976', '1000' ),
284 false
285 ],
286 // MySQL GTID style
287 [
288 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:23' ),
289 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:24' ),
290 true
291 ],
292 [
293 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
294 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
295 true
296 ],
297 [
298 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
299 new MySQLMasterPos( 'db1-bin.2', '2', '1E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
300 false
301 ],
302 // MariaDB GTID style
303 [
304 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-23' ),
305 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-24' ),
306 true
307 ],
308 [
309 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-99' ),
310 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-100' ),
311 true
312 ],
313 [
314 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-999' ),
315 new MySQLMasterPos( 'db1-bin.2', '2', '254-11-1000' ),
316 false
317 ],
318 ];
319 }
320
321 /**
322 * @dataProvider provideChannelPositions
323 */
324 function testChannelsMatch( MySQLMasterPos $pos1, MySQLMasterPos $pos2, $matches ) {
325 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
326 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
327 }
328
329 function provideChannelPositions() {
330 return [
331 [
332 new MySQLMasterPos( 'db1034-bin.000876', '44' ),
333 new MySQLMasterPos( 'db1034-bin.000976', '74' ),
334 true
335 ],
336 [
337 new MySQLMasterPos( 'db1052-bin.000976', '999' ),
338 new MySQLMasterPos( 'db1052-bin.000976', '1000' ),
339 true
340 ],
341 [
342 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
343 new MySQLMasterPos( 'db1035-bin.000976', '10000' ),
344 false
345 ],
346 [
347 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
348 new MySQLMasterPos( 'trump2016.000976', '10000' ),
349 false
350 ],
351 ];
352 }
353
354 /**
355 * @dataProvider provideLagAmounts
356 */
357 function testPtHeartbeat( $lag ) {
358 $db = $this->getMockBuilder( 'DatabaseMysql' )
359 ->disableOriginalConstructor()
360 ->setMethods( [
361 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
362 ->getMock();
363
364 $db->expects( $this->any() )
365 ->method( 'getLagDetectionMethod' )
366 ->will( $this->returnValue( 'pt-heartbeat' ) );
367
368 $db->expects( $this->any() )
369 ->method( 'getMasterServerInfo' )
370 ->will( $this->returnValue( [ 'serverId' => 172, 'asOf' => time() ] ) );
371
372 // Fake the current time.
373 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
374 $now = (float)$nowSec + (float)$nowSecFrac;
375 // Fake the heartbeat time.
376 // Work arounds for weak DataTime microseconds support.
377 $ptTime = $now - $lag;
378 $ptSec = (int)$ptTime;
379 $ptSecFrac = ( $ptTime - $ptSec );
380 $ptDateTime = new DateTime( "@$ptSec" );
381 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
382 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
383
384 $db->expects( $this->any() )
385 ->method( 'getHeartbeatData' )
386 ->with( [ 'server_id' => 172 ] )
387 ->will( $this->returnValue( [ $ptTimeISO, $now ] ) );
388
389 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
390 $lagEst = $db->getLag();
391
392 $this->assertGreaterThan( $lag - .010, $lagEst, "Correct heatbeat lag" );
393 $this->assertLessThan( $lag + .010, $lagEst, "Correct heatbeat lag" );
394 }
395
396 function provideLagAmounts() {
397 return [
398 [ 0 ],
399 [ 0.3 ],
400 [ 6.5 ],
401 [ 10.1 ],
402 [ 200.2 ],
403 [ 400.7 ],
404 [ 600.22 ],
405 [ 1000.77 ],
406 ];
407 }
408 }