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