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