Merge "Set namespaces for dtp"
[mediawiki.git] / tests / phpunit / mocks / MockBlockTrait.php
blobc837a1a8344181b3bea9f75bb4d3f007b03ed337
1 <?php
2 namespace MediaWiki\Tests\Unit;
4 use MediaWiki\Block\AbstractBlock;
5 use MediaWiki\Block\BlockManager;
6 use MediaWiki\Block\DatabaseBlock;
7 use MediaWiki\User\UserIdentity;
8 use MediaWiki\User\UserIdentityValue;
10 /**
11 * Creates and apply user blocks
12 * @stable to use since 1.42
14 trait MockBlockTrait {
16 /**
17 * @param array $options Options as supported by AbstractBlock and
18 * DatabaseBlock. In addition, the 'target' field may contain a
19 * UserIdentity identifying the blocked user.
21 * @return AbstractBlock
23 private function makeMockBlock( $options = [] ): AbstractBlock {
24 if ( isset( $options['target'] ) ) {
25 /** @var UserIdentity $user */
26 $user = $options['target'];
28 if ( !isset( $options['address'] ) ) {
29 $options['address'] = $user->getName();
31 if ( !isset( $options['wiki'] ) ) {
32 $options['wiki'] = $user->getWikiId();
34 unset( $options['target'] );
37 if ( !isset( $options['by'] ) ) {
38 $options['by'] = UserIdentityValue::newRegistered( 45622, 'Blocker' );
41 $block = new DatabaseBlock( $options );
42 return $block;
45 /**
46 * @param UserIdentity|AbstractBlock|array $block
47 * The Block to apply, or the user to block.
48 * If the block is given as an array,
49 * it supports the fields specified by the constructors
50 * of AbstractBlock and DatabaseBlock.
51 * @param UserIdentity|string|null $match The user to apply the
52 * block for. Will be determined automatically if not given.
54 * @return BlockManager
56 private function makeMockBlockManager( $block, $match = null ) {
57 if ( is_array( $block ) ) {
58 if ( !isset( $block['target'] ) && $match !== null ) {
59 $target = $match instanceof UserIdentity ?
60 new UserIdentityValue( 0, $match ) :
61 $match;
63 $block['target'] = $target;
66 $block = $this->makeMockBlock( $block );
69 if ( $block instanceof UserIdentity ) {
70 $target = $block;
71 $block = $this->makeMockBlock( [ 'target' => $target ] );
74 $match ??= $block->getTargetUserIdentity();
76 $blockManager = $this->getMockBuilder( BlockManager::class )
77 ->disableOriginalConstructor()
78 ->onlyMethods( [
79 'getUserBlock', 'getBlock', 'getCreateAccountBlock', 'getIpBlock', 'clearUserCache'
80 ] )
81 ->getMock();
83 $callback = static function ( $user )
84 use ( $match, $block )
86 if ( is_string( $user ) && $user === $match->getName() ) {
87 return $block;
88 } elseif ( $user->equals( $match ) ) {
89 return $block;
92 return null;
95 $blockManager->method( 'getUserBlock' )
96 ->willReturnCallback( $callback );
98 $blockManager->method( 'getBlock' )
99 ->willReturnCallback( $callback );
101 $blockManager->method( 'getCreateAccountBlock' )
102 ->willReturnCallback( $callback );
104 $blockManager->method( 'getIpBlock' )
105 ->willReturnCallback( $callback );
107 return $blockManager;
111 * @param UserIdentity|AbstractBlock|BlockManager|array $block
112 * The BlockManager to install, the Block to apply, or
113 * the user to block. If the block is given as an array,
114 * it supports the fields specified by the constructors
115 * of AbstractBlock and DatabaseBlock.
116 * @param UserIdentity|string|null $match The user to apply the
117 * block for. Will be determined automatically if not given.
119 * @return BlockManager
121 private function installMockBlockManager( $block, $match = null ): BlockManager {
122 if ( $block instanceof BlockManager ) {
123 $blockManager = $block;
124 } else {
125 $blockManager = $this->makeMockBlockManager( $block, $match );
128 $this->setService( 'BlockManager', $blockManager );
129 return $blockManager;