Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiUnblockTest.php
1 <?php
2
3 use MediaWiki\Block\DatabaseBlock;
4
5 /**
6 * @group API
7 * @group Database
8 * @group medium
9 *
10 * @covers ApiUnblock
11 */
12 class ApiUnblockTest extends ApiTestCase {
13 /** @var User */
14 private $blocker;
15
16 /** @var User */
17 private $blockee;
18
19 public function setUp() {
20 parent::setUp();
21
22 $this->tablesUsed = array_merge(
23 $this->tablesUsed,
24 [ 'ipblocks', 'change_tag', 'change_tag_def', 'logging' ]
25 );
26
27 $this->blocker = $this->getTestSysop()->getUser();
28 $this->blockee = $this->getMutableTestUser()->getUser();
29
30 // Initialize a blocked user (used by most tests, although not all)
31 $block = new DatabaseBlock( [
32 'address' => $this->blockee->getName(),
33 'by' => $this->blocker->getId(),
34 ] );
35 $result = $block->insert();
36 $this->assertNotFalse( $result, 'Could not insert block' );
37 $blockFromDB = DatabaseBlock::newFromID( $result['id'] );
38 $this->assertTrue( !is_null( $blockFromDB ), 'Could not retrieve block' );
39 }
40
41 private function getBlockFromParams( array $params ) {
42 if ( array_key_exists( 'user', $params ) ) {
43 return DatabaseBlock::newFromTarget( $params['user'] );
44 }
45 if ( array_key_exists( 'userid', $params ) ) {
46 return DatabaseBlock::newFromTarget( User::newFromId( $params['userid'] ) );
47 }
48 return DatabaseBlock::newFromId( $params['id'] );
49 }
50
51 /**
52 * Try to submit the unblock API request and check that the block no longer exists.
53 *
54 * @param array $params API request query parameters
55 */
56 private function doUnblock( array $params = [] ) {
57 $params += [ 'action' => 'unblock' ];
58 if ( !array_key_exists( 'userid', $params ) && !array_key_exists( 'id', $params ) ) {
59 $params += [ 'user' => $this->blockee->getName() ];
60 }
61
62 $originalBlock = $this->getBlockFromParams( $params );
63
64 $this->doApiRequestWithToken( $params );
65
66 // We only check later on whether the block existed to begin with, because maybe the caller
67 // expects doApiRequestWithToken to throw, in which case the block might not be expected to
68 // exist to begin with.
69 $this->assertInstanceOf( DatabaseBlock::class, $originalBlock, 'Block should initially exist' );
70 $this->assertNull( $this->getBlockFromParams( $params ), 'Block should have been removed' );
71 }
72
73 /**
74 * @expectedException ApiUsageException
75 */
76 public function testWithNoToken() {
77 $this->doApiRequest( [
78 'action' => 'unblock',
79 'user' => $this->blockee->getName(),
80 'reason' => 'Some reason',
81 ] );
82 }
83
84 public function testNormalUnblock() {
85 $this->doUnblock();
86 }
87
88 public function testUnblockNoPermission() {
89 $this->setExpectedApiException( 'apierror-permissiondenied-unblock' );
90
91 $this->setGroupPermissions( 'sysop', 'block', false );
92
93 $this->doUnblock();
94 }
95
96 public function testUnblockWhenBlocked() {
97 $this->setExpectedApiException( 'ipbblocked' );
98
99 $block = new DatabaseBlock( [
100 'address' => $this->blocker->getName(),
101 'by' => $this->getTestUser( 'sysop' )->getUser()->getId(),
102 ] );
103 $block->insert();
104
105 $this->doUnblock();
106 }
107
108 public function testUnblockSelfWhenBlocked() {
109 $block = new DatabaseBlock( [
110 'address' => $this->blocker->getName(),
111 'by' => $this->getTestUser( 'sysop' )->getUser()->getId(),
112 ] );
113 $result = $block->insert();
114 $this->assertNotFalse( $result, 'Could not insert block' );
115
116 $this->doUnblock( [ 'user' => $this->blocker->getName() ] );
117 }
118
119 public function testUnblockWithTagNewBackend() {
120 ChangeTags::defineTag( 'custom tag' );
121
122 $this->doUnblock( [ 'tags' => 'custom tag' ] );
123
124 $dbw = wfGetDB( DB_MASTER );
125 $this->assertSame( 1, (int)$dbw->selectField(
126 [ 'change_tag', 'logging', 'change_tag_def' ],
127 'COUNT(*)',
128 [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ],
129 __METHOD__,
130 [],
131 [
132 'change_tag' => [ 'JOIN', 'ct_log_id = log_id' ],
133 'change_tag_def' => [ 'JOIN', 'ctd_id = ct_tag_id' ],
134 ]
135 ) );
136 }
137
138 public function testUnblockWithProhibitedTag() {
139 $this->setExpectedApiException( 'tags-apply-no-permission' );
140
141 ChangeTags::defineTag( 'custom tag' );
142
143 $this->setGroupPermissions( 'user', 'applychangetags', false );
144
145 $this->doUnblock( [ 'tags' => 'custom tag' ] );
146 }
147
148 public function testUnblockById() {
149 $this->doUnblock( [ 'userid' => $this->blockee->getId() ] );
150 }
151
152 public function testUnblockByInvalidId() {
153 $this->setExpectedApiException( [ 'apierror-nosuchuserid', 1234567890 ] );
154
155 $this->doUnblock( [ 'userid' => 1234567890 ] );
156 }
157
158 public function testUnblockNonexistentBlock() {
159 $this->setExpectedAPIException( [ 'ipb_cant_unblock', $this->blocker->getName() ] );
160
161 $this->doUnblock( [ 'user' => $this->blocker ] );
162 }
163 }