832a113f0e111a1d75a83f1ea72c812c714727c6
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiBlockTest.php
1 <?php
2
3 /**
4 * @group API
5 * @group Database
6 * @group medium
7 *
8 * @covers ApiBlock
9 */
10 class ApiBlockTest extends ApiTestCase {
11 protected function setUp() {
12 parent::setUp();
13 $this->doLogin();
14 }
15
16 protected function tearDown() {
17 $block = Block::newFromTarget( 'UTApiBlockee' );
18 if ( !is_null( $block ) ) {
19 $block->delete();
20 }
21 parent::tearDown();
22 }
23
24 protected function getTokens() {
25 return $this->getTokenList( self::$users['sysop'] );
26 }
27
28 function addDBDataOnce() {
29 $user = User::newFromName( 'UTApiBlockee' );
30
31 if ( $user->getId() == 0 ) {
32 $user->addToDatabase();
33 TestUser::setPasswordForUser( $user, 'UTApiBlockeePassword' );
34
35 $user->saveSettings();
36 }
37 }
38
39 /**
40 * This test has probably always been broken and use an invalid token
41 * Bug tracking brokenness is https://phabricator.wikimedia.org/T37646
42 *
43 * Root cause is https://gerrit.wikimedia.org/r/3434
44 * Which made the Block/Unblock API to actually verify the token
45 * previously always considered valid (T36212).
46 */
47 public function testMakeNormalBlock() {
48 $tokens = $this->getTokens();
49
50 $user = User::newFromName( 'UTApiBlockee' );
51
52 if ( !$user->getId() ) {
53 $this->markTestIncomplete( "The user UTApiBlockee does not exist" );
54 }
55
56 if ( !array_key_exists( 'blocktoken', $tokens ) ) {
57 $this->markTestIncomplete( "No block token found" );
58 }
59
60 $this->doApiRequest( [
61 'action' => 'block',
62 'user' => 'UTApiBlockee',
63 'reason' => 'Some reason',
64 'token' => $tokens['blocktoken'] ], null, false, self::$users['sysop']->getUser() );
65
66 $block = Block::newFromTarget( 'UTApiBlockee' );
67
68 $this->assertTrue( !is_null( $block ), 'Block is valid' );
69
70 $this->assertEquals( 'UTApiBlockee', (string)$block->getTarget() );
71 $this->assertEquals( 'Some reason', $block->mReason );
72 $this->assertEquals( 'infinity', $block->mExpiry );
73 }
74
75 /**
76 * Block by user ID
77 */
78 public function testMakeNormalBlockId() {
79 $tokens = $this->getTokens();
80 $user = User::newFromName( 'UTApiBlockee' );
81
82 if ( !$user->getId() ) {
83 $this->markTestIncomplete( "The user UTApiBlockee does not exist." );
84 }
85
86 if ( !array_key_exists( 'blocktoken', $tokens ) ) {
87 $this->markTestIncomplete( "No block token found" );
88 }
89
90 $data = $this->doApiRequest( [
91 'action' => 'block',
92 'userid' => $user->getId(),
93 'reason' => 'Some reason',
94 'token' => $tokens['blocktoken'] ], null, false, self::$users['sysop']->getUser() );
95
96 $block = Block::newFromTarget( 'UTApiBlockee' );
97
98 $this->assertTrue( !is_null( $block ), 'Block is valid.' );
99 $this->assertEquals( 'UTApiBlockee', (string)$block->getTarget() );
100 $this->assertEquals( 'Some reason', $block->mReason );
101 $this->assertEquals( 'infinity', $block->mExpiry );
102 }
103
104 /**
105 * @expectedException ApiUsageException
106 * @expectedExceptionMessage The "token" parameter must be set
107 */
108 public function testBlockingActionWithNoToken() {
109 $this->doApiRequest(
110 [
111 'action' => 'block',
112 'user' => 'UTApiBlockee',
113 'reason' => 'Some reason',
114 ],
115 null,
116 false,
117 self::$users['sysop']->getUser()
118 );
119 }
120 }