| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Api; |
| 4 |
|
| 5 |
use MediaWiki\Api\ApiUsageException; |
| 6 |
use MediaWiki\Block\DatabaseBlock; |
| 7 |
use MediaWiki\Title\Title; |
| 8 |
use MediaWiki\User\User; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class ApiUnblockTest extends ApiTestCase { |
| 18 |
|
| 19 |
private $blocker; |
| 20 |
|
| 21 |
|
| 22 |
private $blockee; |
| 23 |
|
| 24 |
protected function setUp(): void { |
| 25 |
parent::setUp(); |
| 26 |
|
| 27 |
$this->blocker = $this->getTestSysop()->getUser(); |
| 28 |
$this->blockee = $this->getMutableTestUser()->getUser(); |
| 29 |
|
| 30 |
|
| 31 |
$block = new DatabaseBlock( [ |
| 32 |
'address' => $this->blockee->getName(), |
| 33 |
'by' => $this->blocker, |
| 34 |
] ); |
| 35 |
$blockStore = $this->getServiceContainer()->getDatabaseBlockStore(); |
| 36 |
$result = $blockStore->insertBlock( $block ); |
| 37 |
$this->assertNotFalse( $result, 'Could not insert block' ); |
| 38 |
$blockFromDB = $blockStore->newFromID( $result['id'] ); |
| 39 |
$this->assertInstanceOf( DatabaseBlock::class, $blockFromDB, 'Could not retrieve block' ); |
| 40 |
} |
| 41 |
|
| 42 |
private function getBlockFromParams( array $params ) { |
| 43 |
$blockStore = $this->getServiceContainer()->getDatabaseBlockStore(); |
| 44 |
if ( array_key_exists( 'user', $params ) ) { |
| 45 |
return $blockStore->newFromTarget( $params['user'] ); |
| 46 |
} |
| 47 |
if ( array_key_exists( 'userid', $params ) ) { |
| 48 |
return $blockStore->newFromTarget( |
| 49 |
$this->getServiceContainer()->getUserFactory()->newFromId( $params['userid'] ) |
| 50 |
); |
| 51 |
} |
| 52 |
return $blockStore->newFromID( $params['id'] ); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
private function doUnblock( array $params = [] ) { |
| 61 |
$params += [ 'action' => 'unblock' ]; |
| 62 |
if ( !array_key_exists( 'userid', $params ) && !array_key_exists( 'id', $params ) ) { |
| 63 |
$params += [ 'user' => $this->blockee->getName() ]; |
| 64 |
} |
| 65 |
|
| 66 |
$originalBlock = $this->getBlockFromParams( $params ); |
| 67 |
|
| 68 |
$this->doApiRequestWithToken( $params ); |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
$this->assertInstanceOf( DatabaseBlock::class, $originalBlock, 'Block should initially exist' ); |
| 74 |
$this->assertNull( $this->getBlockFromParams( $params ), 'Block should have been removed' ); |
| 75 |
} |
| 76 |
|
| 77 |
public function testWithNoToken() { |
| 78 |
$this->expectException( ApiUsageException::class ); |
| 79 |
$this->doApiRequest( [ |
| 80 |
'action' => 'unblock', |
| 81 |
'user' => $this->blockee->getName(), |
| 82 |
'reason' => 'Some reason', |
| 83 |
] ); |
| 84 |
} |
| 85 |
|
| 86 |
public function testNormalUnblock() { |
| 87 |
$this->doUnblock(); |
| 88 |
} |
| 89 |
|
| 90 |
public function testUnblockNoPermission() { |
| 91 |
$this->expectApiErrorCode( 'permissiondenied' ); |
| 92 |
|
| 93 |
$this->setGroupPermissions( 'sysop', 'block', false ); |
| 94 |
|
| 95 |
$this->doUnblock(); |
| 96 |
} |
| 97 |
|
| 98 |
public function testUnblockWhenBlocked() { |
| 99 |
$this->expectApiErrorCode( 'ipbblocked' ); |
| 100 |
|
| 101 |
$block = new DatabaseBlock( [ |
| 102 |
'address' => $this->blocker->getName(), |
| 103 |
'by' => $this->getTestUser( 'sysop' )->getUser(), |
| 104 |
] ); |
| 105 |
$this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block ); |
| 106 |
|
| 107 |
$this->doUnblock(); |
| 108 |
} |
| 109 |
|
| 110 |
public function testUnblockSelfWhenBlocked() { |
| 111 |
$block = new DatabaseBlock( [ |
| 112 |
'address' => $this->blocker->getName(), |
| 113 |
'by' => $this->getTestUser( 'sysop' )->getUser(), |
| 114 |
] ); |
| 115 |
$result = $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block ); |
| 116 |
$this->assertNotFalse( $result, 'Could not insert block' ); |
| 117 |
|
| 118 |
$this->doUnblock( [ 'user' => $this->blocker->getName() ] ); |
| 119 |
} |
| 120 |
|
| 121 |
public function testUnblockWithTagNewBackend() { |
| 122 |
$this->getServiceContainer()->getChangeTagsStore()->defineTag( 'custom tag' ); |
| 123 |
|
| 124 |
$this->doUnblock( [ 'tags' => 'custom tag' ] ); |
| 125 |
|
| 126 |
$this->assertSame( 1, (int)$this->getDb()->newSelectQueryBuilder() |
| 127 |
->select( 'COUNT(*)' ) |
| 128 |
->from( 'logging' ) |
| 129 |
->join( 'change_tag', null, 'ct_log_id = log_id' ) |
| 130 |
->join( 'change_tag_def', null, 'ctd_id = ct_tag_id' ) |
| 131 |
->where( [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ] ) |
| 132 |
->caller( __METHOD__ )->fetchField() ); |
| 133 |
} |
| 134 |
|
| 135 |
public function testUnblockWithProhibitedTag() { |
| 136 |
$this->expectApiErrorCode( 'tags-apply-no-permission' ); |
| 137 |
|
| 138 |
$this->getServiceContainer()->getChangeTagsStore()->defineTag( 'custom tag' ); |
| 139 |
|
| 140 |
$this->setGroupPermissions( 'user', 'applychangetags', false ); |
| 141 |
|
| 142 |
$this->doUnblock( [ 'tags' => 'custom tag' ] ); |
| 143 |
} |
| 144 |
|
| 145 |
public function testUnblockById() { |
| 146 |
$this->doUnblock( [ 'userid' => $this->blockee->getId() ] ); |
| 147 |
} |
| 148 |
|
| 149 |
public function testUnblockByInvalidId() { |
| 150 |
$this->expectApiErrorCode( 'nosuchuserid' ); |
| 151 |
|
| 152 |
$this->doUnblock( [ 'userid' => 1234567890 ] ); |
| 153 |
} |
| 154 |
|
| 155 |
public function testUnblockNonexistentBlock() { |
| 156 |
$this->expectApiErrorCode( 'cantunblock' ); |
| 157 |
|
| 158 |
$this->doUnblock( [ 'user' => $this->blocker ] ); |
| 159 |
} |
| 160 |
|
| 161 |
public function testWatched() { |
| 162 |
$userPage = Title::makeTitle( NS_USER, $this->blockee->getName() ); |
| 163 |
$this->doUnblock( [ 'watchuser' => true ] ); |
| 164 |
$this->assertTrue( $this->getServiceContainer()->getWatchlistManager() |
| 165 |
->isWatched( $this->blocker, $userPage ) ); |
| 166 |
} |
| 167 |
} |