MediaWiki  1.30.0
DatabaseMysqlBaseTest.php
Go to the documentation of this file.
1 <?php
30 
35  // From Database
36  function __construct() {
37  $this->profiler = new ProfilerStub( [] );
38  $this->trxProfiler = new TransactionProfiler();
39  $this->cliMode = true;
40  $this->connLogger = new \Psr\Log\NullLogger();
41  $this->queryLogger = new \Psr\Log\NullLogger();
42  $this->errorLogger = function ( Exception $e ) {
43  wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
44  };
45  $this->currentDomain = DatabaseDomain::newUnspecified();
46  }
47 
48  protected function closeConnection() {
49  }
50 
51  protected function doQuery( $sql ) {
52  }
53 
54  // From DatabaseMysql
55  protected function mysqlConnect( $realServer ) {
56  }
57 
58  protected function mysqlSetCharset( $charset ) {
59  }
60 
61  protected function mysqlFreeResult( $res ) {
62  }
63 
64  protected function mysqlFetchObject( $res ) {
65  }
66 
67  protected function mysqlFetchArray( $res ) {
68  }
69 
70  protected function mysqlNumRows( $res ) {
71  }
72 
73  protected function mysqlNumFields( $res ) {
74  }
75 
76  protected function mysqlFieldName( $res, $n ) {
77  }
78 
79  protected function mysqlFieldType( $res, $n ) {
80  }
81 
82  protected function mysqlDataSeek( $res, $row ) {
83  }
84 
85  protected function mysqlError( $conn = null ) {
86  }
87 
88  protected function mysqlFetchField( $res, $n ) {
89  }
90 
91  protected function mysqlRealEscapeString( $s ) {
92  }
93 
94  function insertId() {
95  }
96 
97  function lastErrno() {
98  }
99 
100  function affectedRows() {
101  }
102 
103  function getServerVersion() {
104  }
105 }
106 
107 class DatabaseMysqlBaseTest extends PHPUnit_Framework_TestCase {
112  public function testAddIdentifierQuotes( $expected, $in ) {
113  $db = new FakeDatabaseMysqlBase();
114  $quoted = $db->addIdentifierQuotes( $in );
115  $this->assertEquals( $expected, $quoted );
116  }
117 
123  public static function provideDiapers() {
124  return [
125  // Format: expected, input
126  [ '``', '' ],
127 
128  // Yeah I really hate loosely typed PHP idiocies nowadays
129  [ '``', null ],
130 
131  // Dear codereviewer, guess what addIdentifierQuotes()
132  // will return with thoses:
133  [ '``', false ],
134  [ '`1`', true ],
135 
136  // We never know what could happen
137  [ '`0`', 0 ],
138  [ '`1`', 1 ],
139 
140  // Whatchout! Should probably use something more meaningful
141  [ "`'`", "'" ], # single quote
142  [ '`"`', '"' ], # double quote
143  [ '````', '`' ], # backtick
144  [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
145 
146  // sneaky NUL bytes are lurking everywhere
147  [ '``', "\0" ],
148  [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
149 
150  // unicode chars
151  [
152  self::createUnicodeString( '`\u0001a\uFFFFb`' ),
153  self::createUnicodeString( '\u0001a\uFFFFb' )
154  ],
155  [
156  self::createUnicodeString( '`\u0001\uFFFF`' ),
157  self::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
158  ],
159  [ '`☃`', '☃' ],
160  [ '`メインページ`', 'メインページ' ],
161  [ '`Басты_бет`', 'Басты_бет' ],
162 
163  // Real world:
164  [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
165  [ '`Backtick: ```', 'Backtick: `' ],
166  [ '`This is a test`', 'This is a test' ],
167  ];
168  }
169 
170  private static function createUnicodeString( $str ) {
171  return json_decode( '"' . $str . '"' );
172  }
173 
174  private function getMockForViews() {
175  $db = $this->getMockBuilder( 'DatabaseMysqli' )
176  ->disableOriginalConstructor()
177  ->setMethods( [ 'fetchRow', 'query' ] )
178  ->getMock();
179 
180  $db->method( 'query' )
181  ->with( $this->anything() )
182  ->willReturn( new FakeResultWrapper( [
183  (object)[ 'Tables_in_' => 'view1' ],
184  (object)[ 'Tables_in_' => 'view2' ],
185  (object)[ 'Tables_in_' => 'myview' ]
186  ] ) );
187 
188  return $db;
189  }
190 
194  public function testListviews() {
195  $db = $this->getMockForViews();
196 
197  $this->assertEquals( [ 'view1', 'view2', 'myview' ],
198  $db->listViews() );
199 
200  // Prefix filtering
201  $this->assertEquals( [ 'view1', 'view2' ],
202  $db->listViews( 'view' ) );
203  $this->assertEquals( [ 'myview' ],
204  $db->listViews( 'my' ) );
205  $this->assertEquals( [],
206  $db->listViews( 'UNUSED_PREFIX' ) );
207  $this->assertEquals( [ 'view1', 'view2', 'myview' ],
208  $db->listViews( '' ) );
209  }
210 
215  public 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  public static 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 
288  public function testChannelsMatch( MySQLMasterPos $pos1, MySQLMasterPos $pos2, $matches ) {
289  $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
290  $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
291  }
292 
293  public static function provideChannelPositions() {
294  return [
295  [
296  new MySQLMasterPos( 'db1034-bin.000876', '44' ),
297  new MySQLMasterPos( 'db1034-bin.000976', '74' ),
298  true
299  ],
300  [
301  new MySQLMasterPos( 'db1052-bin.000976', '999' ),
302  new MySQLMasterPos( 'db1052-bin.000976', '1000' ),
303  true
304  ],
305  [
306  new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
307  new MySQLMasterPos( 'db1035-bin.000976', '10000' ),
308  false
309  ],
310  [
311  new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
312  new MySQLMasterPos( 'trump2016.000976', '10000' ),
313  false
314  ],
315  ];
316  }
317 
323  public function testPtHeartbeat( $lag ) {
324  $db = $this->getMockBuilder( 'DatabaseMysqli' )
325  ->disableOriginalConstructor()
326  ->setMethods( [
327  'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
328  ->getMock();
329 
330  $db->method( 'getLagDetectionMethod' )
331  ->willReturn( 'pt-heartbeat' );
332 
333  $db->method( 'getMasterServerInfo' )
334  ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
335 
336  // Fake the current time.
337  list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
338  $now = (float)$nowSec + (float)$nowSecFrac;
339  // Fake the heartbeat time.
340  // Work arounds for weak DataTime microseconds support.
341  $ptTime = $now - $lag;
342  $ptSec = (int)$ptTime;
343  $ptSecFrac = ( $ptTime - $ptSec );
344  $ptDateTime = new DateTime( "@$ptSec" );
345  $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
346  $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
347 
348  $db->method( 'getHeartbeatData' )
349  ->with( [ 'server_id' => 172 ] )
350  ->willReturn( [ $ptTimeISO, $now ] );
351 
352  $db->setLBInfo( 'clusterMasterHost', 'db1052' );
353  $lagEst = $db->getLag();
354 
355  $this->assertGreaterThan( $lag - 0.010, $lagEst, "Correct heatbeat lag" );
356  $this->assertLessThan( $lag + 0.010, $lagEst, "Correct heatbeat lag" );
357  }
358 
359  public static function provideLagAmounts() {
360  return [
361  [ 0 ],
362  [ 0.3 ],
363  [ 6.5 ],
364  [ 10.1 ],
365  [ 200.2 ],
366  [ 400.7 ],
367  [ 600.22 ],
368  [ 1000.77 ],
369  ];
370  }
371 }
FakeDatabaseMysqlBase\mysqlDataSeek
mysqlDataSeek( $res, $row)
Move internal result pointer.
Definition: DatabaseMysqlBaseTest.php:82
FakeDatabaseMysqlBase\closeConnection
closeConnection()
Closes underlying database connection.
Definition: DatabaseMysqlBaseTest.php:48
DatabaseMysqlBaseTest\getMockForViews
getMockForViews()
Definition: DatabaseMysqlBaseTest.php:174
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
FakeDatabaseMysqlBase\mysqlFetchObject
mysqlFetchObject( $res)
Fetch a result row as an object.
Definition: DatabaseMysqlBaseTest.php:64
FakeDatabaseMysqlBase\mysqlRealEscapeString
mysqlRealEscapeString( $s)
Definition: DatabaseMysqlBaseTest.php:91
DatabaseMysqlBaseTest\provideLagAmounts
static provideLagAmounts()
Definition: DatabaseMysqlBaseTest.php:359
DatabaseMysqlBaseTest\testChannelsMatch
testChannelsMatch(MySQLMasterPos $pos1, MySQLMasterPos $pos2, $matches)
provideChannelPositions Wikimedia\Rdbms\MySQLMasterPos
Definition: DatabaseMysqlBaseTest.php:288
FakeDatabaseMysqlBase\__construct
__construct()
Definition: DatabaseMysqlBaseTest.php:36
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
FakeDatabaseMysqlBase\affectedRows
affectedRows()
Get the number of rows affected by the last write query.
Definition: DatabaseMysqlBaseTest.php:100
anything
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same so they can t rely on Unix and must forbid reads to even standard directories like tmp lest users read each others files We cannot assume that the user has the ability to install or run any programs not written as web accessible PHP scripts Since anything that works on cheap shared hosting will work if you have shell or root access MediaWiki s design is based around catering to the lowest common denominator Although we support higher end setups as the way many things work by default is tailored toward shared hosting These defaults are unconventional from the point of view of and they certainly aren t ideal for someone who s installing MediaWiki as MediaWiki does not conform to normal Unix filesystem layout Hopefully we ll offer direct support for standard layouts in the but for now *any change to the location of files is unsupported *Moving things and leaving symlinks will *probably *not break anything
Definition: distributors.txt:39
ProfilerStub
Stub profiler that does nothing.
Definition: ProfilerStub.php:29
DatabaseMysqlBaseTest\provideComparePositions
static provideComparePositions()
Definition: DatabaseMysqlBaseTest.php:231
FakeDatabaseMysqlBase\mysqlFetchField
mysqlFetchField( $res, $n)
Get column information from a result.
Definition: DatabaseMysqlBaseTest.php:88
$s
$s
Definition: mergeMessageFileList.php:188
$res
$res
Definition: database.txt:21
DatabaseMysqlBaseTest
Definition: DatabaseMysqlBaseTest.php:107
DatabaseMysqlBaseTest\testPtHeartbeat
testPtHeartbeat( $lag)
provideLagAmounts Wikimedia\Rdbms\DatabaseMysqlBase::getLag Wikimedia\Rdbms\DatabaseMysqlBase::getLag...
Definition: DatabaseMysqlBaseTest.php:323
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
FakeDatabaseMysqlBase\mysqlFieldType
mysqlFieldType( $res, $n)
Get the type of the specified field in a result.
Definition: DatabaseMysqlBaseTest.php:79
FakeDatabaseMysqlBase\mysqlConnect
mysqlConnect( $realServer)
Open a connection to a MySQL server.
Definition: DatabaseMysqlBaseTest.php:55
FakeDatabaseMysqlBase\mysqlNumFields
mysqlNumFields( $res)
Get number of fields in result.
Definition: DatabaseMysqlBaseTest.php:73
Wikimedia\Rdbms\MySQLMasterPos\channelsMatch
channelsMatch(DBMasterPos $pos)
Definition: MySQLMasterPos.php:78
FakeDatabaseMysqlBase\mysqlError
mysqlError( $conn=null)
Returns the text of the error message from previous MySQL operation.
Definition: DatabaseMysqlBaseTest.php:85
Wikimedia\Rdbms\MySQLMasterPos\hasReached
hasReached(DBMasterPos $pos)
Definition: MySQLMasterPos.php:48
$matches
$matches
Definition: NoLocalSettings.php:24
DatabaseMysqlBaseTest\testHasReached
testHasReached(MySQLMasterPos $lowerPos, MySQLMasterPos $higherPos, $match)
provideComparePositions Wikimedia\Rdbms\MySQLMasterPos
Definition: DatabaseMysqlBaseTest.php:215
FakeDatabaseMysqlBase\getServerVersion
getServerVersion()
Definition: DatabaseMysqlBaseTest.php:103
Wikimedia\Rdbms\MySQLMasterPos
DBMasterPos class for MySQL/MariaDB.
Definition: MySQLMasterPos.php:15
FakeDatabaseMysqlBase
Fake class around abstract class so we can call concrete methods.
Definition: DatabaseMysqlBaseTest.php:34
FakeDatabaseMysqlBase\mysqlFieldName
mysqlFieldName( $res, $n)
Get the name of the specified field in a result.
Definition: DatabaseMysqlBaseTest.php:76
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2141
FakeDatabaseMysqlBase\mysqlNumRows
mysqlNumRows( $res)
Get number of rows in result.
Definition: DatabaseMysqlBaseTest.php:70
FakeDatabaseMysqlBase\lastErrno
lastErrno()
Get the last error number.
Definition: DatabaseMysqlBaseTest.php:97
FakeDatabaseMysqlBase\mysqlFreeResult
mysqlFreeResult( $res)
Free result memory.
Definition: DatabaseMysqlBaseTest.php:61
FakeDatabaseMysqlBase\insertId
insertId()
Get the inserted value of an auto-increment row.
Definition: DatabaseMysqlBaseTest.php:94
DatabaseMysqlBaseTest\createUnicodeString
static createUnicodeString( $str)
Definition: DatabaseMysqlBaseTest.php:170
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1965
wfWarn
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
Definition: GlobalFunctions.php:1190
DatabaseMysqlBaseTest\provideChannelPositions
static provideChannelPositions()
Definition: DatabaseMysqlBaseTest.php:293
Wikimedia\Rdbms\DatabaseDomain
Class to handle database/prefix specification for IDatabase domains.
Definition: DatabaseDomain.php:28
Wikimedia\Rdbms\TransactionProfiler
Helper class that detects high-contention DB queries via profiling calls.
Definition: TransactionProfiler.php:38
FakeDatabaseMysqlBase\mysqlSetCharset
mysqlSetCharset( $charset)
Set the character set of the MySQL link.
Definition: DatabaseMysqlBaseTest.php:58
DatabaseMysqlBaseTest\testListviews
testListviews()
Wikimedia\Rdbms\DatabaseMysqlBase::listViews.
Definition: DatabaseMysqlBaseTest.php:194
DatabaseMysqlBaseTest\provideDiapers
static provideDiapers()
Feeds testAddIdentifierQuotes.
Definition: DatabaseMysqlBaseTest.php:123
FakeDatabaseMysqlBase\doQuery
doQuery( $sql)
The DBMS-dependent part of query()
Definition: DatabaseMysqlBaseTest.php:51
DatabaseMysqlBaseTest\testAddIdentifierQuotes
testAddIdentifierQuotes( $expected, $in)
provideDiapers Wikimedia\Rdbms\DatabaseMysqlBase::addIdentifierQuotes
Definition: DatabaseMysqlBaseTest.php:112
Wikimedia\Rdbms\DatabaseMysqlBase
Database abstraction object for MySQL.
Definition: DatabaseMysqlBase.php:40
FakeDatabaseMysqlBase\mysqlFetchArray
mysqlFetchArray( $res)
Fetch a result row as an associative and numeric array.
Definition: DatabaseMysqlBaseTest.php:67