3 use MediaWiki\Block\BlockRestrictionStore
;
4 use MediaWiki\Block\CompositeBlock
;
5 use MediaWiki\Block\DatabaseBlock
;
6 use MediaWiki\Block\Restriction\PageRestriction
;
7 use MediaWiki\Block\Restriction\NamespaceRestriction
;
8 use MediaWiki\Block\SystemBlock
;
9 use MediaWiki\MediaWikiServices
;
14 * @coversDefaultClass \MediaWiki\Block\CompositeBlock
16 class CompositeBlockTest
extends MediaWikiLangTestCase
{
17 private function getPartialBlocks() {
18 $sysopId = $this->getTestSysop()->getUser()->getId();
20 $userBlock = new DatabaseBlock( [
21 'address' => $this->getTestUser()->getUser(),
25 $ipBlock = new DatabaseBlock( [
26 'address' => '127.0.0.1',
40 private function deleteBlocks( $blocks ) {
41 foreach ( $blocks as $block ) {
47 * @covers ::__construct
48 * @dataProvider provideTestStrictestParametersApplied
50 public function testStrictestParametersApplied( $blocks, $expected ) {
51 $this->setMwGlobals( [
52 'wgBlockDisablesLogin' => false,
53 'wgBlockAllowsUTEdit' => true,
56 $block = new CompositeBlock( [
57 'originalBlocks' => $blocks,
60 $this->assertSame( $expected[ 'hideName' ], $block->getHideName() );
61 $this->assertSame( $expected[ 'sitewide' ], $block->isSitewide() );
62 $this->assertSame( $expected[ 'blockEmail' ], $block->isEmailBlocked() );
63 $this->assertSame( $expected[ 'allowUsertalk' ], $block->isUsertalkEditAllowed() );
66 public static function provideTestStrictestParametersApplied() {
68 'Sitewide block and partial block' => [
73 'allowUsertalk' => true,
77 'blockEmail' => false,
78 'allowUsertalk' => false,
85 'allowUsertalk' => false,
88 'Partial block and system block' => [
93 'allowUsertalk' => false,
96 'systemBlock' => 'proxy',
102 'blockEmail' => true,
103 'allowUsertalk' => false,
106 'System block and user name hiding block' => [
111 'blockEmail' => true,
112 'allowUsertalk' => false,
115 'systemBlock' => 'proxy',
121 'blockEmail' => true,
122 'allowUsertalk' => false,
125 'Two lenient partial blocks' => [
129 'blockEmail' => false,
130 'allowUsertalk' => true,
134 'blockEmail' => false,
135 'allowUsertalk' => true,
141 'blockEmail' => false,
142 'allowUsertalk' => true,
149 * @covers ::appliesToTitle
151 public function testBlockAppliesToTitle() {
152 $this->setMwGlobals( [
153 'wgBlockDisablesLogin' => false,
156 $blocks = $this->getPartialBlocks();
158 $block = new CompositeBlock( [
159 'originalBlocks' => $blocks,
162 $pageFoo = $this->getExistingTestPage( 'Foo' );
163 $pageBar = $this->getExistingTestPage( 'User:Bar' );
165 $this->getBlockRestrictionStore()->insert( [
166 new PageRestriction( $blocks[ 'user' ]->getId(), $pageFoo->getId() ),
167 new NamespaceRestriction( $blocks[ 'ip' ]->getId(), NS_USER
),
170 $this->assertTrue( $block->appliesToTitle( $pageFoo->getTitle() ) );
171 $this->assertTrue( $block->appliesToTitle( $pageBar->getTitle() ) );
173 $this->deleteBlocks( $blocks );
177 * @covers ::appliesToUsertalk
178 * @covers ::appliesToPage
179 * @covers ::appliesToNamespace
181 public function testBlockAppliesToUsertalk() {
182 $this->setMwGlobals( [
183 'wgBlockAllowsUTEdit' => true,
184 'wgBlockDisablesLogin' => false,
187 $blocks = $this->getPartialBlocks();
189 $block = new CompositeBlock( [
190 'originalBlocks' => $blocks,
193 $title = $blocks[ 'user' ]->getTarget()->getTalkPage();
194 $page = $this->getExistingTestPage( 'User talk:' . $title->getText() );
196 $this->getBlockRestrictionStore()->insert( [
197 new PageRestriction( $blocks[ 'user' ]->getId(), $page->getId() ),
198 new NamespaceRestriction( $blocks[ 'ip' ]->getId(), NS_USER
),
201 $this->assertTrue( $block->appliesToUsertalk( $blocks[ 'user' ]->getTarget()->getTalkPage() ) );
203 $this->deleteBlocks( $blocks );
207 * @covers ::appliesToRight
208 * @dataProvider provideTestBlockAppliesToRight
210 public function testBlockAppliesToRight( $blocks, $right, $expected ) {
211 $this->setMwGlobals( [
212 'wgBlockDisablesLogin' => false,
215 $block = new CompositeBlock( [
216 'originalBlocks' => $blocks,
219 $this->assertSame( $block->appliesToRight( $right ), $expected );
222 public static function provideTestBlockAppliesToRight() {
224 'Read is not blocked' => [
232 'Email is blocked if blocked by any blocks' => [
235 'blockEmail' => true,
238 'blockEmail' => false,
248 * @covers ::getPermissionsError
249 * @dataProvider provideGetPermissionsError
251 public function testGetPermissionsError( $ids, $expectedIdsMsg ) {
252 // Some block options
255 $byText = 'MediaWiki default';
256 $formattedByText = "\u{202A}{$byText}\u{202C}";
258 $expiry = 'infinite';
260 $block = $this->getMockBuilder( CompositeBlock
::class )
261 ->setMethods( [ 'getIds', 'getBlockErrorParams' ] )
263 $block->method( 'getIds' )
264 ->willReturn( $ids );
265 $block->method( 'getBlockErrorParams' )
278 'blockedtext-composite',
287 ], $block->getPermissionsError( RequestContext
::getMain() ) );
290 public static function provideGetPermissionsError() {
292 'All original blocks are system blocks' => [
294 'Your IP address appears in multiple blacklists',
296 'One original block is a database block' => [
298 'Relevant block IDs: #100 (your IP address may also be blacklisted)',
300 'Several original blocks are database blocks' => [
302 'Relevant block IDs: #100, #101, #102 (your IP address may also be blacklisted)',
308 * Get an instance of BlockRestrictionStore
310 * @return BlockRestrictionStore
312 protected function getBlockRestrictionStore() : BlockRestrictionStore
{
313 return MediaWikiServices
::getInstance()->getBlockRestrictionStore();