Debian Codegraph @

tests/phpunit/includes/block/CompositeBlockTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Block\BlockRestrictionStore;
4 use MediaWiki\Block\CompositeBlock;
5 use MediaWiki\Block\DatabaseBlock;
6 use MediaWiki\Block\Restriction\NamespaceRestriction;
7 use MediaWiki\Block\Restriction\PageRestriction;
8 use MediaWiki\Block\SystemBlock;
9 use MediaWiki\MainConfigNames;
10
11 /**
12 * @group Database
13 * @group Blocking
14 * @covers \MediaWiki\Block\CompositeBlock
15 */
16 class CompositeBlockTest extends MediaWikiLangTestCase {
17 private function getPartialBlocks() {
18 $sysopUser = $this->getTestSysop()->getUser();
19
20 $userBlock = new DatabaseBlock( [
21 'address' => $this->getTestUser()->getUser(),
22 'by' => $sysopUser,
23 'sitewide' => false,
24 ] );
25 $ipBlock = new DatabaseBlock( [
26 'address' => '127.0.0.1',
27 'by' => $sysopUser,
28 'sitewide' => false,
29 ] );
30
31 $blockStore = $this->getServiceContainer()->getDatabaseBlockStore();
32 $blockStore->insertBlock( $userBlock );
33 $blockStore->insertBlock( $ipBlock );
34
35 return [
36 'user' => $userBlock,
37 'ip' => $ipBlock,
38 ];
39 }
40
41 /**
42 * @dataProvider provideTestStrictestParametersApplied
43 */
44 public function testStrictestParametersApplied( $blocks, $expected ) {
45 $this->overrideConfigValues( [
46 MainConfigNames::BlockDisablesLogin => false,
47 MainConfigNames::BlockAllowsUTEdit => true,
48 ] );
49
50 $block = new CompositeBlock( [
51 'originalBlocks' => $blocks,
52 ] );
53
54 $this->assertSame( $expected[ 'hideName' ], $block->getHideName() );
55 $this->assertSame( $expected[ 'sitewide' ], $block->isSitewide() );
56 $this->assertSame( $expected[ 'blockEmail' ], $block->isEmailBlocked() );
57 $this->assertSame( $expected[ 'allowUsertalk' ], $block->isUsertalkEditAllowed() );
58 }
59
60 public static function provideTestStrictestParametersApplied() {
61 return [
62 'Sitewide block and partial block' => [
63 [
64 new DatabaseBlock( [
65 'sitewide' => false,
66 'blockEmail' => true,
67 'allowUsertalk' => true,
68 ] ),
69 new DatabaseBlock( [
70 'sitewide' => true,
71 'blockEmail' => false,
72 'allowUsertalk' => false,
73 ] ),
74 ],
75 [
76 'hideName' => false,
77 'sitewide' => true,
78 'blockEmail' => true,
79 'allowUsertalk' => false,
80 ],
81 ],
82 'Partial block and system block' => [
83 [
84 new DatabaseBlock( [
85 'sitewide' => false,
86 'blockEmail' => true,
87 'allowUsertalk' => false,
88 ] ),
89 new SystemBlock( [
90 'systemBlock' => 'proxy',
91 ] ),
92 ],
93 [
94 'hideName' => false,
95 'sitewide' => true,
96 'blockEmail' => true,
97 'allowUsertalk' => false,
98 ],
99 ],
100 'System block and user name hiding block' => [
101 [
102 new DatabaseBlock( [
103 'hideName' => true,
104 'sitewide' => true,
105 'blockEmail' => true,
106 'allowUsertalk' => false,
107 ] ),
108 new SystemBlock( [
109 'systemBlock' => 'proxy',
110 ] ),
111 ],
112 [
113 'hideName' => true,
114 'sitewide' => true,
115 'blockEmail' => true,
116 'allowUsertalk' => false,
117 ],
118 ],
119 'Two lenient partial blocks' => [
120 [
121 new DatabaseBlock( [
122 'sitewide' => false,
123 'blockEmail' => false,
124 'allowUsertalk' => true,
125 ] ),
126 new DatabaseBlock( [
127 'sitewide' => false,
128 'blockEmail' => false,
129 'allowUsertalk' => true,
130 ] ),
131 ],
132 [
133 'hideName' => false,
134 'sitewide' => false,
135 'blockEmail' => false,
136 'allowUsertalk' => true,
137 ],
138 ],
139 ];
140 }
141
142 public function testBlockAppliesToTitle() {
143 $this->overrideConfigValue( MainConfigNames::BlockDisablesLogin, false );
144
145 $blocks = $this->getPartialBlocks();
146
147 $block = new CompositeBlock( [
148 'originalBlocks' => $blocks,
149 ] );
150
151 $pageFoo = $this->getExistingTestPage( 'Foo' );
152 $pageBar = $this->getExistingTestPage( 'User:Bar' );
153
154 $this->getBlockRestrictionStore()->insert( [
155 new PageRestriction( $blocks[ 'user' ]->getId(), $pageFoo->getId() ),
156 new NamespaceRestriction( $blocks[ 'ip' ]->getId(), NS_USER ),
157 ] );
158
159 $this->assertTrue( $block->appliesToTitle( $pageFoo->getTitle() ) );
160 $this->assertTrue( $block->appliesToTitle( $pageBar->getTitle() ) );
161 }
162
163 public function testBlockAppliesToUsertalk() {
164 $this->overrideConfigValues( [
165 MainConfigNames::BlockAllowsUTEdit => true,
166 MainConfigNames::BlockDisablesLogin => false,
167 ] );
168
169 $blocks = $this->getPartialBlocks();
170
171 $block = new CompositeBlock( [
172 'originalBlocks' => $blocks,
173 ] );
174
175 $userFactory = $this->getServiceContainer()->getUserFactory();
176 $targetIdentity = $userFactory->newFromUserIdentity( $blocks[ 'user' ]->getTargetUserIdentity() );
177 $title = $targetIdentity->getTalkPage();
178 $page = $this->getExistingTestPage( 'User talk:' . $title->getText() );
179
180 $this->getBlockRestrictionStore()->insert( [
181 new PageRestriction( $blocks[ 'user' ]->getId(), $page->getId() ),
182 new NamespaceRestriction( $blocks[ 'ip' ]->getId(), NS_USER ),
183 ] );
184
185 $this->assertTrue( $block->appliesToUsertalk( $title ) );
186 }
187
188 /**
189 * @dataProvider provideTestBlockAppliesToRight
190 */
191 public function testBlockAppliesToRight( $applies, $expected ) {
192 $this->overrideConfigValue( MainConfigNames::BlockDisablesLogin, false );
193
194 $block = new CompositeBlock( [
195 'originalBlocks' => [
196 $this->getMockBlockForTestAppliesToRight( $applies[ 0 ] ),
197 $this->getMockBlockForTestAppliesToRight( $applies[ 1 ] ),
198 ],
199 ] );
200
201 $this->assertSame( $expected, $block->appliesToRight( 'right' ) );
202 }
203
204 private function getMockBlockForTestAppliesToRight( $applies ) {
205 $mockBlock = $this->getMockBuilder( DatabaseBlock::class )
206 ->onlyMethods( [ 'appliesToRight' ] )
207 ->getMock();
208 $mockBlock->method( 'appliesToRight' )
209 ->willReturn( $applies );
210 return $mockBlock;
211 }
212
213 public static function provideTestBlockAppliesToRight() {
214 return [
215 'Block does not apply if no original blocks apply' => [
216 [ false, false ],
217 false,
218 ],
219 'Block applies if any original block applies (second block doesn\'t apply)' => [
220 [ true, false ],
221 true,
222 ],
223 'Block applies if any original block applies (second block unsure)' => [
224 [ true, null ],
225 true,
226 ],
227 'Block is unsure if all original blocks are unsure' => [
228 [ null, null ],
229 null,
230 ],
231 'Block is unsure if any original block is unsure, and no others apply' => [
232 [ null, false ],
233 null,
234 ],
235 ];
236 }
237
238 public function testTimestamp() {
239 $timestamp = 20000101000000;
240
241 $firstBlock = $this->createMock( DatabaseBlock::class );
242 $firstBlock->method( 'getTimestamp' )
243 ->willReturn( (string)$timestamp );
244
245 $secondBlock = $this->createMock( DatabaseBlock::class );
246 $secondBlock->method( 'getTimestamp' )
247 ->willReturn( (string)( $timestamp + 10 ) );
248
249 $thirdBlock = $this->createMock( DatabaseBlock::class );
250 $thirdBlock->method( 'getTimestamp' )
251 ->willReturn( (string)( $timestamp + 100 ) );
252
253 $block = new CompositeBlock( [
254 'originalBlocks' => [ $thirdBlock, $firstBlock, $secondBlock ],
255 ] );
256 $this->assertSame( (string)$timestamp, $block->getTimestamp() );
257 }
258
259 public function testCreateFromBlocks() {
260 $block1 = new SystemBlock( [
261 'address' => '127.0.0.1',
262 'systemBlock' => 'test1',
263 ] );
264 $block2 = new SystemBlock( [
265 'address' => '127.0.0.1',
266 'systemBlock' => 'test2',
267 ] );
268 $block3 = new SystemBlock( [
269 'address' => '127.0.0.1',
270 'systemBlock' => 'test3',
271 ] );
272
273 $compositeBlock = CompositeBlock::createFromBlocks( $block1, $block2 );
274 $this->assertInstanceOf( CompositeBlock::class, $compositeBlock );
275 $this->assertCount( 2, $compositeBlock->getOriginalBlocks() );
276 [ $actualBlock1, $actualBlock2 ] = $compositeBlock->getOriginalBlocks();
277 $this->assertSame( $block1->getSystemBlockType(), $actualBlock1->getSystemBlockType() );
278 $this->assertSame( $block2->getSystemBlockType(), $actualBlock2->getSystemBlockType() );
279 $this->assertSame( 'blockedtext-composite-reason',
280 $compositeBlock->getReasonComment()->message->getKey() );
281 $this->assertSame( '127.0.0.1', $compositeBlock->getTargetName() );
282
283 $compositeBlock2 = CompositeBlock::createFromBlocks( $compositeBlock, $block3 );
284 $this->assertCount( 3, $compositeBlock2->getOriginalBlocks() );
285 [ $actualBlock1, $actualBlock2, $actualBlock3 ] = $compositeBlock2->getOriginalBlocks();
286 $this->assertSame( $block1->getSystemBlockType(), $actualBlock1->getSystemBlockType() );
287 $this->assertSame( $block2->getSystemBlockType(), $actualBlock2->getSystemBlockType() );
288 $this->assertSame( $block3->getSystemBlockType(), $actualBlock3->getSystemBlockType() );
289 }
290
291 /**
292 * Get an instance of BlockRestrictionStore
293 *
294 * @return BlockRestrictionStore
295 */
296 protected function getBlockRestrictionStore(): BlockRestrictionStore {
297 return $this->getServiceContainer()->getBlockRestrictionStore();
298 }
299 }