Merge "Fix \n handling for HTMLUsersMultiselectField"
[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 * @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
31 /**
32 * Fake class around abstract class so we can call concrete methods.
33 */
34 class FakeDatabaseMysqlBase extends DatabaseMysqlBase {
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 MediaWikiTestCase {
108 /**
109 * @dataProvider provideDiapers
110 * @covers DatabaseMysqlBase::addIdentifierQuotes
111 */
112 public function testAddIdentifierQuotes( $expected, $in ) {
113 $db = new FakeDatabaseMysqlBase();
114 $quoted = $db->addIdentifierQuotes( $in );
115 $this->assertEquals( $expected, $quoted );
116 }
117
118 /**
119 * Feeds testAddIdentifierQuotes
120 *
121 * Named per T22281 convention.
122 */
123 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 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 /**
191 * @covers DatabaseMysqlBase::listViews
192 */
193 function testListviews() {
194 $db = $this->getMockForViews();
195
196 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
197 $db->listViews() );
198
199 // Prefix filtering
200 $this->assertEquals( [ 'view1', 'view2' ],
201 $db->listViews( 'view' ) );
202 $this->assertEquals( [ 'myview' ],
203 $db->listViews( 'my' ) );
204 $this->assertEquals( [],
205 $db->listViews( 'UNUSED_PREFIX' ) );
206 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
207 $db->listViews( '' ) );
208 }
209
210 /**
211 * @dataProvider provideComparePositions
212 */
213 function testHasReached( MySQLMasterPos $lowerPos, MySQLMasterPos $higherPos, $match ) {
214 if ( $match ) {
215 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
216
217 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
218 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
219 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
220 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
221 } else { // channels don't match
222 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
223
224 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
225 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
226 }
227 }
228
229 function provideComparePositions() {
230 return [
231 // Binlog style
232 [
233 new MySQLMasterPos( 'db1034-bin.000976', '843431247' ),
234 new MySQLMasterPos( 'db1034-bin.000976', '843431248' ),
235 true
236 ],
237 [
238 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
239 new MySQLMasterPos( 'db1034-bin.000976', '1000' ),
240 true
241 ],
242 [
243 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
244 new MySQLMasterPos( 'db1035-bin.000976', '1000' ),
245 false
246 ],
247 // MySQL GTID style
248 [
249 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:23' ),
250 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:24' ),
251 true
252 ],
253 [
254 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
255 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
256 true
257 ],
258 [
259 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
260 new MySQLMasterPos( 'db1-bin.2', '2', '1E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
261 false
262 ],
263 // MariaDB GTID style
264 [
265 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-23' ),
266 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-24' ),
267 true
268 ],
269 [
270 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-99' ),
271 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-100' ),
272 true
273 ],
274 [
275 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-999' ),
276 new MySQLMasterPos( 'db1-bin.2', '2', '254-11-1000' ),
277 false
278 ],
279 ];
280 }
281
282 /**
283 * @dataProvider provideChannelPositions
284 */
285 function testChannelsMatch( MySQLMasterPos $pos1, MySQLMasterPos $pos2, $matches ) {
286 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
287 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
288 }
289
290 function provideChannelPositions() {
291 return [
292 [
293 new MySQLMasterPos( 'db1034-bin.000876', '44' ),
294 new MySQLMasterPos( 'db1034-bin.000976', '74' ),
295 true
296 ],
297 [
298 new MySQLMasterPos( 'db1052-bin.000976', '999' ),
299 new MySQLMasterPos( 'db1052-bin.000976', '1000' ),
300 true
301 ],
302 [
303 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
304 new MySQLMasterPos( 'db1035-bin.000976', '10000' ),
305 false
306 ],
307 [
308 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
309 new MySQLMasterPos( 'trump2016.000976', '10000' ),
310 false
311 ],
312 ];
313 }
314
315 /**
316 * @dataProvider provideLagAmounts
317 */
318 function testPtHeartbeat( $lag ) {
319 $db = $this->getMockBuilder( 'DatabaseMysqli' )
320 ->disableOriginalConstructor()
321 ->setMethods( [
322 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
323 ->getMock();
324
325 $db->method( 'getLagDetectionMethod' )
326 ->willReturn( 'pt-heartbeat' );
327
328 $db->method( 'getMasterServerInfo' )
329 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
330
331 // Fake the current time.
332 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
333 $now = (float)$nowSec + (float)$nowSecFrac;
334 // Fake the heartbeat time.
335 // Work arounds for weak DataTime microseconds support.
336 $ptTime = $now - $lag;
337 $ptSec = (int)$ptTime;
338 $ptSecFrac = ( $ptTime - $ptSec );
339 $ptDateTime = new DateTime( "@$ptSec" );
340 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
341 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
342
343 $db->method( 'getHeartbeatData' )
344 ->with( [ 'server_id' => 172 ] )
345 ->willReturn( [ $ptTimeISO, $now ] );
346
347 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
348 $lagEst = $db->getLag();
349
350 $this->assertGreaterThan( $lag - .010, $lagEst, "Correct heatbeat lag" );
351 $this->assertLessThan( $lag + .010, $lagEst, "Correct heatbeat lag" );
352 }
353
354 function provideLagAmounts() {
355 return [
356 [ 0 ],
357 [ 0.3 ],
358 [ 6.5 ],
359 [ 10.1 ],
360 [ 200.2 ],
361 [ 400.7 ],
362 [ 600.22 ],
363 [ 1000.77 ],
364 ];
365 }
366 }