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