Follow-up to I7654907.
Bug: T225919
Change-Id: I67e72d6c88e3cbfd9515a016b2782d1d9b123775
}
/**
- * Given a list of blocks, return a list blocks where each block either has a
- * unique ID or has ID null.
+ * Given a list of blocks, return a list of unique blocks.
+ *
+ * This usually means that each block has a unique ID. For a block with ID null,
+ * if it's an autoblock, it will be filtered out if the parent block is present;
+ * if not, it is assumed to be a unique system block, and kept.
*
* @param AbstractBlock[] $blocks
* @return AbstractBlock[]
*/
private function getUniqueBlocks( $blocks ) {
- $blockIds = [];
- $uniqueBlocks = [];
+ $systemBlocks = [];
+ $databaseBlocks = [];
+
foreach ( $blocks as $block ) {
- $id = $block->getId();
- if ( $id === null ) {
- $uniqueBlocks[] = $block;
- } elseif ( !isset( $blockIds[$id] ) ) {
- $uniqueBlocks[] = $block;
- $blockIds[$block->getId()] = true;
+ if ( $block instanceof SystemBlock ) {
+ $systemBlocks[] = $block;
+ } elseif ( $block->getType() === DatabaseBlock::TYPE_AUTO ) {
+ if ( !isset( $databaseBlocks[$block->getParentBlockId()] ) ) {
+ $databaseBlocks[$block->getParentBlockId()] = $block;
+ }
+ } else {
+ $databaseBlocks[$block->getId()] = $block;
}
}
- return $uniqueBlocks;
+
+ return array_merge( $systemBlocks, $databaseBlocks );
}
/**
return $this;
}
+ /**
+ * @since 1.34
+ * @return int|null If this is an autoblock, ID of the parent block; otherwise null
+ */
+ public function getParentBlockId() {
+ return $this->mParentBlockId;
+ }
+
/**
* Get/set a flag determining whether the master is used for reads
*
use MediaWiki\Block\BlockManager;
use MediaWiki\Block\DatabaseBlock;
+use MediaWiki\Block\SystemBlock;
/**
* @group Blocking
],
];
}
+
+ /**
+ * @covers ::getUniqueBlocks
+ */
+ public function testGetUniqueBlocks() {
+ $blockId = 100;
+
+ $class = new ReflectionClass( BlockManager::class );
+ $method = $class->getMethod( 'getUniqueBlocks' );
+ $method->setAccessible( true );
+
+ $blockManager = $this->getBlockManager( [] );
+
+ $block = $this->getMockBuilder( DatabaseBlock::class )
+ ->setMethods( [ 'getId' ] )
+ ->getMock();
+ $block->expects( $this->any() )
+ ->method( 'getId' )
+ ->willReturn( $blockId );
+
+ $autoblock = $this->getMockBuilder( DatabaseBlock::class )
+ ->setMethods( [ 'getParentBlockId', 'getType' ] )
+ ->getMock();
+ $autoblock->expects( $this->any() )
+ ->method( 'getParentBlockId' )
+ ->willReturn( $blockId );
+ $autoblock->expects( $this->any() )
+ ->method( 'getType' )
+ ->willReturn( DatabaseBlock::TYPE_AUTO );
+
+ $blocks = [ $block, $block, $autoblock, new SystemBlock() ];
+
+ $this->assertSame( 2, count( $method->invoke( $blockManager, $blocks ) ) );
+ }
}