Merge "Expand the info in 'resettokens-watchlist-token'"
[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 class ApiBlockTest extends ApiTestCase {
9 protected function setUp() {
10 parent::setUp();
11 $this->doLogin();
12 }
13
14 function getTokens() {
15 return $this->getTokenList( self::$users['sysop'] );
16 }
17
18 function addDBData() {
19 $user = User::newFromName( 'UTApiBlockee' );
20
21 if ( $user->getId() == 0 ) {
22 $user->addToDatabase();
23 $user->setPassword( 'UTApiBlockeePassword' );
24
25 $user->saveSettings();
26 }
27 }
28
29 /**
30 * This test has probably always been broken and use an invalid token
31 * Bug tracking brokenness is https://bugzilla.wikimedia.org/35646
32 *
33 * Root cause is https://gerrit.wikimedia.org/r/3434
34 * Which made the Block/Unblock API to actually verify the token
35 * previously always considered valid (bug 34212).
36 */
37 function testMakeNormalBlock() {
38 $tokens = $this->getTokens();
39
40 $user = User::newFromName( 'UTApiBlockee' );
41
42 if ( !$user->getId() ) {
43 $this->markTestIncomplete( "The user UTApiBlockee does not exist" );
44 }
45
46 if ( !array_key_exists( 'blocktoken', $tokens ) ) {
47 $this->markTestIncomplete( "No block token found" );
48 }
49
50 $this->doApiRequest( array(
51 'action' => 'block',
52 'user' => 'UTApiBlockee',
53 'reason' => 'Some reason',
54 'token' => $tokens['blocktoken'] ), null, false, self::$users['sysop']->user );
55
56 $block = Block::newFromTarget( 'UTApiBlockee' );
57
58 $this->assertTrue( !is_null( $block ), 'Block is valid' );
59
60 $this->assertEquals( 'UTApiBlockee', (string)$block->getTarget() );
61 $this->assertEquals( 'Some reason', $block->mReason );
62 $this->assertEquals( 'infinity', $block->mExpiry );
63 }
64
65 /**
66 * @dataProvider provideBlockUnblockAction
67 */
68 function testGetTokenUsingABlockingAction( $action ) {
69 $data = $this->doApiRequest(
70 array(
71 'action' => $action,
72 'user' => 'UTApiBlockee',
73 'gettoken' => '' ),
74 null,
75 false,
76 self::$users['sysop']->user
77 );
78 $this->assertEquals( 34, strlen( $data[0][$action]["{$action}token"] ) );
79 }
80
81 /**
82 * Attempting to block without a token should give a UsageException with
83 * error message:
84 * "The token parameter must be set"
85 *
86 * @dataProvider provideBlockUnblockAction
87 * @expectedException UsageException
88 */
89 function testBlockingActionWithNoToken( $action ) {
90 $this->doApiRequest(
91 array(
92 'action' => $action,
93 'user' => 'UTApiBlockee',
94 'reason' => 'Some reason',
95 ),
96 null,
97 false,
98 self::$users['sysop']->user
99 );
100 }
101
102 /**
103 * Just provide the 'block' and 'unblock' action to test both API calls
104 */
105 public static function provideBlockUnblockAction() {
106 return array(
107 array( 'block' ),
108 array( 'unblock' ),
109 );
110 }
111 }