| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Api\ApiMain; |
| 4 |
use MediaWiki\Block\DatabaseBlock; |
| 5 |
use MediaWiki\Extension\Thanks\Api\ApiCoreThank; |
| 6 |
use MediaWiki\Tests\Api\ApiTestCase; |
| 7 |
use MediaWiki\User\User; |
| 8 |
use MediaWiki\User\UserIdentityValue; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class ApiCoreThankUnitTest extends ApiTestCase { |
| 20 |
|
| 21 |
protected function getModule() { |
| 22 |
$services = $this->getServiceContainer(); |
| 23 |
return new ApiCoreThank( |
| 24 |
new ApiMain(), |
| 25 |
'thank', |
| 26 |
$services->getPermissionManager(), |
| 27 |
$services->getRevisionStore(), |
| 28 |
$services->getUserFactory(), |
| 29 |
$services->getService( 'ThanksLogStore' ) |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
private static function createBlock( $options ) { |
| 34 |
$options = array_merge( [ |
| 35 |
'address' => 'Test user', |
| 36 |
'by' => new UserIdentityValue( 1, 'TestUser' ), |
| 37 |
'reason' => __METHOD__, |
| 38 |
'timestamp' => wfTimestamp( TS_MW ), |
| 39 |
'expiry' => 'infinity', |
| 40 |
], $options ); |
| 41 |
return new DatabaseBlock( $options ); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public function testDieOnBadUser( |
| 50 |
$mockisNamed, |
| 51 |
$mockPingLimited, |
| 52 |
$mockBlock, |
| 53 |
$dieMethod, |
| 54 |
$expectedError |
| 55 |
) { |
| 56 |
$user = $this->createMock( User::class ); |
| 57 |
if ( $mockisNamed !== null ) { |
| 58 |
$user->expects( $this->once() ) |
| 59 |
->method( 'isNamed' ) |
| 60 |
->willReturn( $mockisNamed ); |
| 61 |
} |
| 62 |
if ( $mockPingLimited !== null ) { |
| 63 |
$user->expects( $this->once() ) |
| 64 |
->method( 'pingLimiter' ) |
| 65 |
->willReturn( $mockPingLimited ); |
| 66 |
} |
| 67 |
if ( $mockBlock !== null ) { |
| 68 |
$user->expects( $this->once() ) |
| 69 |
->method( 'getBlock' ) |
| 70 |
->willReturn( $mockBlock ); |
| 71 |
} |
| 72 |
|
| 73 |
$module = $this->getModule(); |
| 74 |
$method = new ReflectionMethod( $module, $dieMethod ); |
| 75 |
|
| 76 |
if ( $expectedError ) { |
| 77 |
$this->expectApiErrorCode( $expectedError ); |
| 78 |
} |
| 79 |
|
| 80 |
$method->invoke( $module, $user ); |
| 81 |
|
| 82 |
$this->assertTrue( true ); |
| 83 |
} |
| 84 |
|
| 85 |
public static function provideDieOnBadUser() { |
| 86 |
return [ |
| 87 |
'anon' => [ |
| 88 |
false, |
| 89 |
null, |
| 90 |
null, |
| 91 |
'dieOnBadUser', |
| 92 |
'notloggedin' |
| 93 |
], |
| 94 |
'ping' => [ |
| 95 |
true, |
| 96 |
true, |
| 97 |
null, |
| 98 |
'dieOnBadUser', |
| 99 |
'ratelimited' |
| 100 |
], |
| 101 |
'sitewide blocked' => [ |
| 102 |
null, |
| 103 |
null, |
| 104 |
self::createBlock( [] ), |
| 105 |
'dieOnUserBlockedFromThanks', |
| 106 |
'blocked' |
| 107 |
], |
| 108 |
'partial blocked' => [ |
| 109 |
null, |
| 110 |
null, |
| 111 |
self::createBlock( [ 'sitewide' => false ] ), |
| 112 |
'dieOnUserBlockedFromThanks', |
| 113 |
false |
| 114 |
], |
| 115 |
]; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
} |