3 use MediaWiki\Block\DatabaseBlock
;
4 use MediaWiki\Block\Restriction\PageRestriction
;
5 use MediaWiki\Block\Restriction\NamespaceRestriction
;
12 * @covers ApiQueryBlocks
14 class ApiQueryBlocksTest
extends ApiTestCase
{
16 protected $tablesUsed = [
18 'ipblocks_restrictions',
21 public function testExecute() {
22 list( $data ) = $this->doApiRequest( [
26 $this->assertEquals( [ 'batchcomplete' => true, 'query' => [ 'blocks' => [] ] ], $data );
29 public function testExecuteBlock() {
30 $badActor = $this->getTestUser()->getUser();
31 $sysop = $this->getTestSysop()->getUser();
33 $block = new DatabaseBlock( [
34 'address' => $badActor->getName(),
35 'user' => $badActor->getId(),
36 'by' => $sysop->getId(),
37 'expiry' => 'infinity',
42 list( $data ) = $this->doApiRequest( [
46 $this->arrayHasKey( 'query', $data );
47 $this->arrayHasKey( 'blocks', $data['query'] );
48 $this->assertCount( 1, $data['query']['blocks'] );
50 'id' => $block->getId(),
51 'user' => $badActor->getName(),
52 'expiry' => $block->getExpiry(),
54 $this->assertArraySubset( $subset, $data['query']['blocks'][0] );
57 public function testExecuteSitewide() {
58 $badActor = $this->getTestUser()->getUser();
59 $sysop = $this->getTestSysop()->getUser();
61 $block = new DatabaseBlock( [
62 'address' => $badActor->getName(),
63 'user' => $badActor->getId(),
64 'by' => $sysop->getId(),
65 'ipb_expiry' => 'infinity',
71 list( $data ) = $this->doApiRequest( [
75 $this->arrayHasKey( 'query', $data );
76 $this->arrayHasKey( 'blocks', $data['query'] );
77 $this->assertCount( 1, $data['query']['blocks'] );
79 'id' => $block->getId(),
80 'user' => $badActor->getName(),
81 'expiry' => $block->getExpiry(),
82 'partial' => !$block->isSitewide(),
84 $this->assertArraySubset( $subset, $data['query']['blocks'][0] );
87 public function testExecuteRestrictions() {
88 $badActor = $this->getTestUser()->getUser();
89 $sysop = $this->getTestSysop()->getUser();
91 $block = new DatabaseBlock( [
92 'address' => $badActor->getName(),
93 'user' => $badActor->getId(),
94 'by' => $sysop->getId(),
95 'expiry' => 'infinity',
102 'id' => $block->getId(),
103 'user' => $badActor->getName(),
104 'expiry' => $block->getExpiry(),
107 $title = 'Lady Macbeth';
108 $pageData = $this->insertPage( $title );
109 $pageId = $pageData['id'];
111 $this->db
->insert( 'ipblocks_restrictions', [
112 'ir_ipb_id' => $block->getId(),
113 'ir_type' => PageRestriction
::TYPE_ID
,
114 'ir_value' => $pageId,
116 // Page that has been deleted.
117 $this->db
->insert( 'ipblocks_restrictions', [
118 'ir_ipb_id' => $block->getId(),
119 'ir_type' => PageRestriction
::TYPE_ID
,
120 'ir_value' => 999999,
122 $this->db
->insert( 'ipblocks_restrictions', [
123 'ir_ipb_id' => $block->getId(),
124 'ir_type' => NamespaceRestriction
::TYPE_ID
,
125 'ir_value' => NS_USER_TALK
,
127 $this->db
->insert( 'ipblocks_restrictions', [
128 'ir_ipb_id' => $block->getId(),
133 // Test without requesting restrictions.
134 list( $data ) = $this->doApiRequest( [
138 $this->arrayHasKey( 'query', $data );
139 $this->arrayHasKey( 'blocks', $data['query'] );
140 $this->assertCount( 1, $data['query']['blocks'] );
141 $flagSubset = array_merge( $subset, [
142 'partial' => !$block->isSitewide(),
144 $this->assertArraySubset( $flagSubset, $data['query']['blocks'][0] );
145 $this->assertArrayNotHasKey( 'restrictions', $data['query']['blocks'][0] );
147 // Test requesting the restrictions.
148 list( $data ) = $this->doApiRequest( [
151 'bkprop' => 'id|user|expiry|restrictions'
153 $this->arrayHasKey( 'query', $data );
154 $this->arrayHasKey( 'blocks', $data['query'] );
155 $this->assertCount( 1, $data['query']['blocks'] );
156 $restrictionsSubset = array_merge( $subset, [
170 $this->assertArraySubset( $restrictionsSubset, $data['query']['blocks'][0] );
171 $this->assertArrayNotHasKey( 'partial', $data['query']['blocks'][0] );