1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
<?php
use MediaWiki\Block\DatabaseBlock;
use MediaWiki\MediaWikiServices;
use Wikimedia\TestingAccessWrapper;
use Wikimedia\Timestamp\ConvertibleTimestamp;
/**
* @covers ApiQueryBlockInfoTrait
* @group Database
*/
class ApiQueryBlockInfoTraitTest extends MediaWikiIntegrationTestCase {
public function testUsesApiBlockInfoTrait() {
$this->assertTrue( method_exists( ApiQueryBlockInfoTrait::class, 'getBlockDetails' ),
'ApiQueryBlockInfoTrait::getBlockDetails exists' );
}
/**
* @dataProvider provideAddBlockInfoToQuery
*/
public function testAddBlockInfoToQuery( $args, $expect ) {
// Fake timestamp to show up in the queries
$reset = ConvertibleTimestamp::setFakeTime( '20190101000000' );
$data = [];
$mock = $this->getMockForTrait( ApiQueryBlockInfoTrait::class );
$mock->method( 'getDB' )->willReturn( $this->getDb() );
$mock->method( 'getAuthority' )
->willReturn( $this->getMutableTestUser()->getUser() );
$mock->method( 'addTables' )->willReturnCallback( static function ( $v ) use ( &$data ) {
$data['tables'] = array_merge( $data['tables'] ?? [], (array)$v );
} );
$mock->method( 'addFields' )->willReturnCallback( static function ( $v ) use ( &$data ) {
$data['fields'] = array_merge( $data['fields'] ?? [], (array)$v );
} );
$mock->method( 'addWhere' )->willReturnCallback( static function ( $v ) use ( &$data ) {
$data['where'] = array_merge( $data['where'] ?? [], (array)$v );
} );
$mock->method( 'addJoinConds' )->willReturnCallback( static function ( $v ) use ( &$data ) {
$data['joins'] = array_merge( $data['joins'] ?? [], (array)$v );
} );
TestingAccessWrapper::newFromObject( $mock )->addBlockInfoToQuery( ...$args );
$this->assertEquals( $expect, $data );
}
public static function provideAddBlockInfoToQuery() {
$queryInfo = DatabaseBlock::getQueryInfo();
$db = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getReplicaDatabase();
$ts = $db->addQuotes( $db->timestamp( '20190101000000' ) );
return [
[ [ false ], [
'tables' => [ 'blk' => [ 'ipblocks' ] ],
'fields' => [ 'ipb_deleted' ],
'where' => [ 'ipb_deleted' => [ 0, null ] ],
'joins' => [
'blk' => [ 'LEFT JOIN', [ 'ipb_user=user_id', "ipb_expiry > $ts" ] ]
],
] ],
[ [ true ], [
'tables' => [ 'blk' => $queryInfo['tables'] ],
'fields' => $queryInfo['fields'],
'where' => [ 'ipb_deleted' => [ 0, null ] ],
'joins' => $queryInfo['joins'] + [
'blk' => [ 'LEFT JOIN', [ 'ipb_user=user_id', "ipb_expiry > $ts" ] ]
],
] ],
];
}
}
|