Debian Codegraph @

tests/phpunit/mocks/MockBlockTrait.php

raw symbols SCIP
1 <?php
2 namespace MediaWiki\Tests\Unit;
3
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;
9
10 /**
11 * Creates and apply user blocks
12 * @stable to use since 1.42
13 */
14 trait MockBlockTrait {
15
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.
20 *
21 * @return AbstractBlock
22 */
23 private function makeMockBlock( $options = [] ): AbstractBlock {
24 if ( isset( $options['target'] ) ) {
25 /** @var UserIdentity $user */
26 $user = $options['target'];
27
28 if ( !isset( $options['address'] ) ) {
29 $options['address'] = $user->getName();
30 }
31 if ( !isset( $options['wiki'] ) ) {
32 $options['wiki'] = $user->getWikiId();
33 }
34 unset( $options['target'] );
35 }
36
37 if ( !isset( $options['by'] ) ) {
38 $options['by'] = UserIdentityValue::newRegistered( 45622, 'Blocker' );
39 }
40
41 $block = new DatabaseBlock( $options );
42 return $block;
43 }
44
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.
53 *
54 * @return BlockManager
55 */
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;
62
63 $block['target'] = $target;
64 }
65
66 $block = $this->makeMockBlock( $block );
67 }
68
69 if ( $block instanceof UserIdentity ) {
70 $target = $block;
71 $block = $this->makeMockBlock( [ 'target' => $target ] );
72 }
73
74 $match ??= $block->getTargetUserIdentity();
75
76 $blockManager = $this->getMockBuilder( BlockManager::class )
77 ->disableOriginalConstructor()
78 ->onlyMethods( [
79 'getUserBlock', 'getBlock', 'getCreateAccountBlock', 'getIpBlock', 'clearUserCache'
80 ] )
81 ->getMock();
82
83 $callback = static function ( $user )
84 use ( $match, $block )
85 {
86 if ( is_string( $user ) && $user === $match->getName() ) {
87 return $block;
88 } elseif ( $user->equals( $match ) ) {
89 return $block;
90 }
91
92 return null;
93 };
94
95 $blockManager->method( 'getUserBlock' )
96 ->willReturnCallback( $callback );
97
98 $blockManager->method( 'getBlock' )
99 ->willReturnCallback( $callback );
100
101 $blockManager->method( 'getCreateAccountBlock' )
102 ->willReturnCallback( $callback );
103
104 $blockManager->method( 'getIpBlock' )
105 ->willReturnCallback( $callback );
106
107 return $blockManager;
108 }
109
110 /**
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.
118 *
119 * @return BlockManager
120 */
121 private function installMockBlockManager( $block, $match = null ): BlockManager {
122 if ( $block instanceof BlockManager ) {
123 $blockManager = $block;
124 } else {
125 $blockManager = $this->makeMockBlockManager( $block, $match );
126 }
127
128 $this->setService( 'BlockManager', $blockManager );
129 return $blockManager;
130 }
131
132 }