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