Compress images
[mediawiki.git] / tests / phpunit / includes / api / ApiBlockTest.php
blob5dfceee8cd870112bbba4678a8c554201b8dbf98
1 <?php
3 /**
4 * @group API
5 * @group Database
6 */
7 class ApiBlockTest extends ApiTestCase {
9 function setUp() {
10 parent::setUp();
11 $this->doLogin();
14 function getTokens() {
15 return $this->getTokenList( self::$users['sysop'] );
18 function addDBData() {
19 $user = User::newFromName( 'UTApiBlockee' );
21 if ( $user->getId() == 0 ) {
22 $user->addToDatabase();
23 $user->setPassword( 'UTApiBlockeePassword' );
25 $user->saveSettings();
29 /**
30 * This test has probably always been broken and use an invalid token
31 * Bug tracking brokenness is https://bugzilla.wikimedia.org/35646
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).
37 function testMakeNormalBlock() {
39 $data = $this->getTokens();
41 $user = User::newFromName( 'UTApiBlockee' );
43 if ( !$user->getId() ) {
44 $this->markTestIncomplete( "The user UTApiBlockee does not exist" );
47 if( !isset( $data[0]['query']['pages'] ) ) {
48 $this->markTestIncomplete( "No block token found" );
51 $keys = array_keys( $data[0]['query']['pages'] );
52 $key = array_pop( $keys );
53 $pageinfo = $data[0]['query']['pages'][$key];
55 $data = $this->doApiRequest( array(
56 'action' => 'block',
57 'user' => 'UTApiBlockee',
58 'reason' => 'Some reason',
59 'token' => $pageinfo['blocktoken'] ), null, false, self::$users['sysop']->user );
61 $block = Block::newFromTarget('UTApiBlockee');
63 $this->assertTrue( !is_null( $block ), 'Block is valid' );
65 $this->assertEquals( 'UTApiBlockee', (string)$block->getTarget() );
66 $this->assertEquals( 'Some reason', $block->mReason );
67 $this->assertEquals( 'infinity', $block->mExpiry );
71 /**
72 * @dataProvider provideBlockUnblockAction
74 function testGetTokenUsingABlockingAction( $action ) {
75 $data = $this->doApiRequest(
76 array(
77 'action' => $action,
78 'user' => 'UTApiBlockee',
79 'gettoken' => '' ),
80 null,
81 false,
82 self::$users['sysop']->user
84 $this->assertEquals( 34, strlen( $data[0][$action]["{$action}token"] ) );
87 /**
88 * Attempting to block without a token should give a UsageException with
89 * error message:
90 * "The token parameter must be set"
92 * @dataProvider provideBlockUnblockAction
93 * @expectedException UsageException
95 function testBlockingActionWithNoToken( $action ) {
96 $this->doApiRequest(
97 array(
98 'action' => $action,
99 'user' => 'UTApiBlockee',
100 'reason' => 'Some reason',
102 null,
103 false,
104 self::$users['sysop']->user
109 * Just provide the 'block' and 'unblock' action to test both API calls
111 function provideBlockUnblockAction() {
112 return array(
113 array( 'block' ),
114 array( 'unblock' ),