API: Use ApiBlockInfoTrait in ApiQueryUsers and AllUsers
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiQueryBlockInfoTraitTest.php
1 <?php
2
3 use MediaWiki\Block\DatabaseBlock;
4 use MediaWiki\MediaWikiServices;
5 use Wikimedia\TestingAccessWrapper;
6 use Wikimedia\Timestamp\ConvertibleTimestamp;
7
8 /**
9 * @covers ApiQueryBlockInfoTrait
10 */
11 class ApiQueryBlockInfoTraitTest extends MediaWikiTestCase {
12
13 public function testUsesApiBlockInfoTrait() {
14 $this->assertTrue( method_exists( ApiQueryBlockInfoTrait::class, 'getBlockDetails' ),
15 'ApiQueryBlockInfoTrait::getBlockDetails exists' );
16 }
17
18 /**
19 * @dataProvider provideAddBlockInfoToQuery
20 */
21 public function testAddBlockInfoToQuery( $args, $expect ) {
22 // Fake timestamp to show up in the queries
23 $reset = ConvertibleTimestamp::setFakeTime( '20190101000000' );
24
25 $data = [];
26
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 );
35 } );
36 $mock->method( 'addFields' )->willReturnCallback( function ( $v ) use ( &$data ) {
37 $data['fields'] = array_merge( $data['fields'] ?? [], (array)$v );
38 } );
39 $mock->method( 'addWhere' )->willReturnCallback( function ( $v ) use ( &$data ) {
40 $data['where'] = array_merge( $data['where'] ?? [], (array)$v );
41 } );
42 $mock->method( 'addJoinConds' )->willReturnCallback( function ( $v ) use ( &$data ) {
43 $data['joins'] = array_merge( $data['joins'] ?? [], (array)$v );
44 } );
45
46 TestingAccessWrapper::newFromObject( $mock )->addBlockInfoToQuery( ...$args );
47 $this->assertEquals( $expect, $data );
48 }
49
50 public function provideAddBlockInfoToQuery() {
51 $queryInfo = DatabaseBlock::getQueryInfo();
52
53 $db = wfGetDB( DB_REPLICA );
54 $ts = $db->addQuotes( $db->timestamp( '20190101000000' ) );
55
56 return [
57 [ [ false ], [
58 'tables' => [ 'blk' => [ 'ipblocks' ] ],
59 'fields' => [ 'ipb_deleted' ],
60 'where' => [ 'ipb_deleted = 0 OR ipb_deleted IS NULL' ],
61 'joins' => [
62 'blk' => [ 'LEFT JOIN', [ 'ipb_user=user_id', "ipb_expiry > $ts" ] ]
63 ],
64 ] ],
65
66 [ [ true ], [
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" ] ]
72 ],
73 ] ],
74 ];
75 }
76
77 }