List Partial Block details in ApiQueryBlocks and ApiQueryUserinfo.
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiQueryBlocksTest.php
1 <?php
2
3 use MediaWiki\Block\Restriction\PageRestriction;
4
5 /**
6 * @group API
7 * @group Database
8 * @group medium
9 *
10 * @covers ApiQueryBlocks
11 */
12 class ApiQueryBlocksTest extends ApiTestCase {
13
14 protected $tablesUsed = [
15 'ipblocks',
16 'ipblocks_restrictions',
17 ];
18
19 public function testExecute() {
20 list( $data ) = $this->doApiRequest( [
21 'action' => 'query',
22 'list' => 'blocks',
23 ] );
24 $this->assertEquals( [ 'batchcomplete' => true, 'query' => [ 'blocks' => [] ] ], $data );
25 }
26
27 public function testExecuteBlock() {
28 $badActor = $this->getTestUser()->getUser();
29 $sysop = $this->getTestSysop()->getUser();
30
31 $block = new Block( [
32 'address' => $badActor->getName(),
33 'user' => $badActor->getId(),
34 'by' => $sysop->getId(),
35 'expiry' => 'infinity',
36 ] );
37
38 $block->insert();
39
40 list( $data ) = $this->doApiRequest( [
41 'action' => 'query',
42 'list' => 'blocks',
43 ] );
44 $this->arrayHasKey( 'query', $data );
45 $this->arrayHasKey( 'blocks', $data['query'] );
46 $this->assertCount( 1, $data['query']['blocks'] );
47 $subset = [
48 'id' => $block->getId(),
49 'user' => $badActor->getName(),
50 'expiry' => $block->getExpiry(),
51 ];
52 $this->assertArraySubset( $subset, $data['query']['blocks'][0] );
53 }
54
55 public function testExecuteSitewide() {
56 $badActor = $this->getTestUser()->getUser();
57 $sysop = $this->getTestSysop()->getUser();
58
59 $block = new Block( [
60 'address' => $badActor->getName(),
61 'user' => $badActor->getId(),
62 'by' => $sysop->getId(),
63 'ipb_expiry' => 'infinity',
64 'ipb_sitewide' => 1,
65 ] );
66
67 $block->insert();
68
69 list( $data ) = $this->doApiRequest( [
70 'action' => 'query',
71 'list' => 'blocks',
72 ] );
73 $this->arrayHasKey( 'query', $data );
74 $this->arrayHasKey( 'blocks', $data['query'] );
75 $this->assertCount( 1, $data['query']['blocks'] );
76 $subset = [
77 'id' => $block->getId(),
78 'user' => $badActor->getName(),
79 'expiry' => $block->getExpiry(),
80 'partial' => !$block->isSitewide(),
81 ];
82 $this->assertArraySubset( $subset, $data['query']['blocks'][0] );
83 }
84
85 public function testExecuteRestrictions() {
86 $badActor = $this->getTestUser()->getUser();
87 $sysop = $this->getTestSysop()->getUser();
88
89 $block = new Block( [
90 'address' => $badActor->getName(),
91 'user' => $badActor->getId(),
92 'by' => $sysop->getId(),
93 'expiry' => 'infinity',
94 'sitewide' => 0,
95 ] );
96
97 $block->insert();
98
99 $subset = [
100 'id' => $block->getId(),
101 'user' => $badActor->getName(),
102 'expiry' => $block->getExpiry(),
103 ];
104
105 $title = 'Lady Macbeth';
106 $pageData = $this->insertPage( $title );
107 $pageId = $pageData['id'];
108
109 $this->db->insert( 'ipblocks_restrictions', [
110 'ir_ipb_id' => $block->getId(),
111 'ir_type' => PageRestriction::TYPE_ID,
112 'ir_value' => $pageId,
113 ] );
114 $this->db->insert( 'ipblocks_restrictions', [
115 'ir_ipb_id' => $block->getId(),
116 'ir_type' => 2,
117 'ir_value' => 3,
118 ] );
119
120 // Test without requesting restrictions.
121 list( $data ) = $this->doApiRequest( [
122 'action' => 'query',
123 'list' => 'blocks',
124 ] );
125 $this->arrayHasKey( 'query', $data );
126 $this->arrayHasKey( 'blocks', $data['query'] );
127 $this->assertCount( 1, $data['query']['blocks'] );
128 $flagSubset = array_merge( $subset, [
129 'partial' => !$block->isSitewide(),
130 ] );
131 $this->assertArraySubset( $flagSubset, $data['query']['blocks'][0] );
132 $this->assertArrayNotHasKey( 'restrictions', $data['query']['blocks'][0] );
133
134 // Test requesting the restrictions.
135 list( $data ) = $this->doApiRequest( [
136 'action' => 'query',
137 'list' => 'blocks',
138 'bkprop' => 'id|user|expiry|restrictions'
139 ] );
140 $this->arrayHasKey( 'query', $data );
141 $this->arrayHasKey( 'blocks', $data['query'] );
142 $this->assertCount( 1, $data['query']['blocks'] );
143 $restrictionsSubset = array_merge( $subset, [
144 'restrictions' => [
145 'pages' => [
146 [
147 'id' => $pageId,
148 'ns' => 0,
149 'title' => $title,
150 ],
151 ],
152 ],
153 ] );
154 $this->assertArraySubset( $restrictionsSubset, $data['query']['blocks'][0] );
155 $this->assertArrayNotHasKey( 'partial', $data['query']['blocks'][0] );
156 }
157 }