3 use MediaWiki\Block\DatabaseBlock
;
4 use MediaWiki\MediaWikiServices
;
5 use Wikimedia\TestingAccessWrapper
;
6 use Wikimedia\Timestamp\ConvertibleTimestamp
;
9 * @covers ApiQueryBlockInfoTrait
11 class ApiQueryBlockInfoTraitTest
extends MediaWikiTestCase
{
13 public function testUsesApiBlockInfoTrait() {
14 $this->assertTrue( method_exists( ApiQueryBlockInfoTrait
::class, 'getBlockDetails' ),
15 'ApiQueryBlockInfoTrait::getBlockDetails exists' );
19 * @dataProvider provideAddBlockInfoToQuery
21 public function testAddBlockInfoToQuery( $args, $expect ) {
22 // Fake timestamp to show up in the queries
23 $reset = ConvertibleTimestamp
::setFakeTime( '20190101000000' );
27 $mock = $this->getMockForTrait( ApiQueryBlockInfoTrait
::class );
28 $mock->method( 'getDB' )->willReturn( wfGetDB( DB_REPLICA
) );
29 $mock->method( 'getPermissionManager' )
30 ->willReturn( MediaWikiServices
::getInstance()->getPermissionManager() );
31 $mock->method( 'getUser' )
32 ->willReturn( $this->getMutableTestUser()->getUser() );
33 $mock->method( 'addTables' )->willReturnCallback( function ( $v ) use ( &$data ) {
34 $data['tables'] = array_merge( $data['tables'] ??
[], (array)$v );
36 $mock->method( 'addFields' )->willReturnCallback( function ( $v ) use ( &$data ) {
37 $data['fields'] = array_merge( $data['fields'] ??
[], (array)$v );
39 $mock->method( 'addWhere' )->willReturnCallback( function ( $v ) use ( &$data ) {
40 $data['where'] = array_merge( $data['where'] ??
[], (array)$v );
42 $mock->method( 'addJoinConds' )->willReturnCallback( function ( $v ) use ( &$data ) {
43 $data['joins'] = array_merge( $data['joins'] ??
[], (array)$v );
46 TestingAccessWrapper
::newFromObject( $mock )->addBlockInfoToQuery( ...$args );
47 $this->assertEquals( $expect, $data );
50 public function provideAddBlockInfoToQuery() {
51 $queryInfo = DatabaseBlock
::getQueryInfo();
53 $db = wfGetDB( DB_REPLICA
);
54 $ts = $db->addQuotes( $db->timestamp( '20190101000000' ) );
58 'tables' => [ 'blk' => [ 'ipblocks' ] ],
59 'fields' => [ 'ipb_deleted' ],
60 'where' => [ 'ipb_deleted = 0 OR ipb_deleted IS NULL' ],
62 'blk' => [ 'LEFT JOIN', [ 'ipb_user=user_id', "ipb_expiry > $ts" ] ]
67 'tables' => [ 'blk' => $queryInfo['tables'] ],
68 'fields' => $queryInfo['fields'],
69 'where' => [ 'ipb_deleted = 0 OR ipb_deleted IS NULL' ],
70 'joins' => $queryInfo['joins'] +
[
71 'blk' => [ 'LEFT JOIN', [ 'ipb_user=user_id', "ipb_expiry > $ts" ] ]