8 class ApiBlockTest
extends ApiTestCase
{
10 protected function setUp() {
15 function getTokens() {
16 return $this->getTokenList( self
::$users['sysop'] );
19 function addDBData() {
20 $user = User
::newFromName( 'UTApiBlockee' );
22 if ( $user->getId() == 0 ) {
23 $user->addToDatabase();
24 $user->setPassword( 'UTApiBlockeePassword' );
26 $user->saveSettings();
31 * This test has probably always been broken and use an invalid token
32 * Bug tracking brokenness is https://bugzilla.wikimedia.org/35646
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).
38 function testMakeNormalBlock() {
40 $data = $this->getTokens();
42 $user = User
::newFromName( 'UTApiBlockee' );
44 if ( !$user->getId() ) {
45 $this->markTestIncomplete( "The user UTApiBlockee does not exist" );
48 if( !isset( $data[0]['query']['pages'] ) ) {
49 $this->markTestIncomplete( "No block token found" );
52 $keys = array_keys( $data[0]['query']['pages'] );
53 $key = array_pop( $keys );
54 $pageinfo = $data[0]['query']['pages'][$key];
56 $data = $this->doApiRequest( array(
58 'user' => 'UTApiBlockee',
59 'reason' => 'Some reason',
60 'token' => $pageinfo['blocktoken'] ), null, false, self
::$users['sysop']->user
);
62 $block = Block
::newFromTarget('UTApiBlockee');
64 $this->assertTrue( !is_null( $block ), 'Block is valid' );
66 $this->assertEquals( 'UTApiBlockee', (string)$block->getTarget() );
67 $this->assertEquals( 'Some reason', $block->mReason
);
68 $this->assertEquals( 'infinity', $block->mExpiry
);
73 * @dataProvider provideBlockUnblockAction
75 function testGetTokenUsingABlockingAction( $action ) {
76 $data = $this->doApiRequest(
79 'user' => 'UTApiBlockee',
83 self
::$users['sysop']->user
85 $this->assertEquals( 34, strlen( $data[0][$action]["{$action}token"] ) );
89 * Attempting to block without a token should give a UsageException with
91 * "The token parameter must be set"
93 * @dataProvider provideBlockUnblockAction
94 * @expectedException UsageException
96 function testBlockingActionWithNoToken( $action ) {
100 'user' => 'UTApiBlockee',
101 'reason' => 'Some reason',
105 self
::$users['sysop']->user
110 * Just provide the 'block' and 'unblock' action to test both API calls
112 function provideBlockUnblockAction() {