* may be overridden according to global configs.
*
* @since 1.33
- * @param string $right Right to check
- * @return bool|null null if unrecognized right or unset property
+ * @param string $right
+ * @return bool|null The block applies to the right, or null if
+ * unsure (e.g. unrecognized right or unset property)
*/
public function appliesToRight( $right ) {
$config = RequestContext::getMain()->getConfig();
/**
* @inheritDoc
+ *
+ * Determines whether the CompositeBlock applies to a right by checking
+ * whether the original blocks apply to that right. Each block can report
+ * true (applies), false (does not apply) or null (unsure). Then:
+ * - If any original blocks apply, this block applies
+ * - If no original blocks apply but any are unsure, this block is unsure
+ * - If all blocks do not apply, this block does not apply
*/
public function appliesToRight( $right ) {
- return $this->methodReturnsValue( __FUNCTION__, true, $right );
+ $isUnsure = false;
+
+ foreach ( $this->originalBlocks as $block ) {
+ $appliesToRight = $block->appliesToRight( $right );
+
+ if ( $appliesToRight ) {
+ return true;
+ } elseif ( $appliesToRight === null ) {
+ $isUnsure = true;
+ }
+ }
+
+ return $isUnsure ? null : false;
}
/**
* @covers ::appliesToRight
* @dataProvider provideTestBlockAppliesToRight
*/
- public function testBlockAppliesToRight( $blocks, $right, $expected ) {
+ public function testBlockAppliesToRight( $applies, $expected ) {
$this->setMwGlobals( [
'wgBlockDisablesLogin' => false,
] );
$block = new CompositeBlock( [
- 'originalBlocks' => $blocks,
+ 'originalBlocks' => [
+ $this->getMockBlockForTestAppliesToRight( $applies[ 0 ] ),
+ $this->getMockBlockForTestAppliesToRight( $applies[ 1 ] ),
+ ],
] );
- $this->assertSame( $block->appliesToRight( $right ), $expected );
+ $this->assertSame( $block->appliesToRight( 'right' ), $expected );
+ }
+
+ private function getMockBlockForTestAppliesToRight( $applies ) {
+ $mockBlock = $this->getMockBuilder( DatabaseBlock::class )
+ ->setMethods( [ 'appliesToRight' ] )
+ ->getMock();
+ $mockBlock->method( 'appliesToRight' )
+ ->willReturn( $applies );
+ return $mockBlock;
}
- public static function provideTestBlockAppliesToRight() {
+ public function provideTestBlockAppliesToRight() {
return [
- 'Read is not blocked' => [
- [
- new DatabaseBlock(),
- new DatabaseBlock(),
- ],
- 'read',
+ 'Block does not apply if no original blocks apply' => [
+ [ false, false ],
false,
],
- 'Email is blocked if blocked by any blocks' => [
- [
- new DatabaseBlock( [
- 'blockEmail' => true,
- ] ),
- new DatabaseBlock( [
- 'blockEmail' => false,
- ] ),
- ],
- 'sendemail',
+ '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,
+ ],
];
}