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