aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/api/query/ApiQueryBlockInfoTraitTest.php
blob: a982be30f00f720e42777f087f4b355fcda28952 (plain) (blame)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php

namespace MediaWiki\Tests\Api\Query;

use ApiQueryBase;
use ApiQueryBlockInfoTrait;
use MediaWiki\MainConfigNames;
use MediaWiki\Tests\MockDatabase;
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
use MediaWikiIntegrationTestCase;
use Wikimedia\TestingAccessWrapper;
use Wikimedia\Timestamp\ConvertibleTimestamp;

/**
 * @covers \ApiQueryBlockInfoTrait
 */
class ApiQueryBlockInfoTraitTest extends MediaWikiIntegrationTestCase {
	use MockAuthorityTrait;

	public function testUsesApiBlockInfoTrait() {
		$this->assertTrue( method_exists( ApiQueryBlockInfoTrait::class, 'getBlockDetails' ),
			'ApiQueryBlockInfoTrait::getBlockDetails exists' );
	}

	/**
	 * @dataProvider provideAddDeletedUserFilter
	 */
	public function testAddDeletedUserFilter( $schema, $isAllowed, $expect ) {
		$this->overrideConfigValue( MainConfigNames::BlockTargetMigrationStage, $schema );

		// Fake timestamp to show up in the queries
		ConvertibleTimestamp::setFakeTime( '20190101000000' );

		$authority = $this->mockRegisteredAuthorityWithPermissions(
			$isAllowed ? [ 'hideuser' ] : [] );
		$db = new MockDatabase;
		$queryBuilder = $db->newSelectQueryBuilder()
			->from( 'table' );

		$mock = $this->getMockBuilder( ApiQueryBase::class )
			->disableOriginalConstructor()
			->onlyMethods( [
				'getQueryBuilder',
				'getDB',
				'getAuthority'
			] )
			->getMockForAbstractClass();

		$mock->method( 'getQueryBuilder' )->willReturn( $queryBuilder );
		$mock->method( 'getDB' )->willReturn( new MockDatabase );
		$mock->method( 'getAuthority' )->willReturn( $authority );

		TestingAccessWrapper::newFromObject( $mock )->addDeletedUserFilter();
		$data = $queryBuilder->getQueryInfo();
		$this->assertSame( $expect, $data );
	}

	public static function provideAddDeletedUserFilter() {
		return [
			'old unauthorized' => [
				SCHEMA_COMPAT_OLD,
				false,
				[
					'tables' => [ 'table' ],
					'fields' => [ 'hu_deleted' => '1=0' ],
					'conds' => [
						'NOT EXISTS (SELECT  1  FROM "ipblocks" "hu_ipblocks"    ' .
						'WHERE (ipb_user=user_id) AND ipb_deleted = 1  )' ],
					'options' => [],
					'join_conds' => [],
				],
			],
			'old authorized' => [
				SCHEMA_COMPAT_OLD,
				true,
				[
					'tables' => [ 'table' ],
					'fields' => [ 'hu_deleted' => 'EXISTS (SELECT  1  FROM "ipblocks" "hu_ipblocks"    ' .
						'WHERE (ipb_user=user_id) AND ipb_deleted = 1  )' ],
					'conds' => [],
					'options' => [],
					'join_conds' => []
				],
			],
			'new unauthorized' => [
				SCHEMA_COMPAT_NEW,
				false,
				[
					'tables' => [ 'table' ],
					'fields' => [ 'hu_deleted' => '1=0' ],
					'conds' => [ 'NOT EXISTS (SELECT  1  FROM "block_target" "hu_block_target" ' .
						'JOIN "block" ON ((bl_target=hu_block_target.bt_id))   ' .
						'WHERE (hu_block_target.bt_user=user_id) AND bl_deleted = 1  )' ],
					'options' => [],
					'join_conds' => [],
				],
			],
			'new authorized' => [
				SCHEMA_COMPAT_NEW,
				true,
				[
					'tables' => [ 'table' ],
					'fields' => [ 'hu_deleted' => 'EXISTS (SELECT  1  FROM "block_target" "hu_block_target" ' .
						'JOIN "block" ON ((bl_target=hu_block_target.bt_id))   ' .
						'WHERE (hu_block_target.bt_user=user_id) AND bl_deleted = 1  )' ],
					'conds' => [],
					'options' => [],
					'join_conds' => []
				],
			],
		];
	}

}