2011-01-02 19:58:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
2012-04-02 13:33:43 +00:00
|
|
|
* @group API
|
2011-01-02 19:58:27 +00:00
|
|
|
* @group Database
|
|
|
|
|
*/
|
2011-07-01 16:34:02 +00:00
|
|
|
class ApiBlockTest extends ApiTestCase {
|
2011-01-02 19:58:27 +00:00
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function setUp() {
|
2011-01-02 19:58:27 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
$this->doLogin();
|
|
|
|
|
}
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2011-01-02 19:58:27 +00:00
|
|
|
function getTokens() {
|
2011-07-01 16:34:02 +00:00
|
|
|
return $this->getTokenList( self::$users['sysop'] );
|
2011-01-02 19:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function addDBData() {
|
2011-09-12 22:18:33 +00:00
|
|
|
$user = User::newFromName( 'UTApiBlockee' );
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2011-01-02 19:58:27 +00:00
|
|
|
if ( $user->getId() == 0 ) {
|
|
|
|
|
$user->addToDatabase();
|
2011-09-12 22:18:33 +00:00
|
|
|
$user->setPassword( 'UTApiBlockeePassword' );
|
2011-01-02 19:58:27 +00:00
|
|
|
|
|
|
|
|
$user->saveSettings();
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-05-28 21:22:29 +00:00
|
|
|
|
2012-04-03 13:05:50 +00:00
|
|
|
/**
|
|
|
|
|
* This test has probably always been broken and use an invalid token
|
|
|
|
|
* Bug tracking brokenness is https://bugzilla.wikimedia.org/35646
|
|
|
|
|
*
|
|
|
|
|
* Root cause is https://gerrit.wikimedia.org/r/3434
|
|
|
|
|
* Which made the Block/Unblock API to actually verify the token
|
|
|
|
|
* previously always considered valid (bug 34212).
|
|
|
|
|
*/
|
2011-01-02 19:58:27 +00:00
|
|
|
function testMakeNormalBlock() {
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2011-01-02 19:58:27 +00:00
|
|
|
$data = $this->getTokens();
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2011-09-12 22:18:33 +00:00
|
|
|
$user = User::newFromName( 'UTApiBlockee' );
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2011-01-02 19:58:27 +00:00
|
|
|
if ( !$user->getId() ) {
|
2011-09-12 22:18:33 +00:00
|
|
|
$this->markTestIncomplete( "The user UTApiBlockee does not exist" );
|
2011-01-02 19:58:27 +00:00
|
|
|
}
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2011-01-02 19:58:27 +00:00
|
|
|
if( !isset( $data[0]['query']['pages'] ) ) {
|
|
|
|
|
$this->markTestIncomplete( "No block token found" );
|
|
|
|
|
}
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2011-01-02 19:58:27 +00:00
|
|
|
$keys = array_keys( $data[0]['query']['pages'] );
|
|
|
|
|
$key = array_pop( $keys );
|
|
|
|
|
$pageinfo = $data[0]['query']['pages'][$key];
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2011-01-02 19:58:27 +00:00
|
|
|
$data = $this->doApiRequest( array(
|
|
|
|
|
'action' => 'block',
|
2011-09-12 22:18:33 +00:00
|
|
|
'user' => 'UTApiBlockee',
|
2011-08-08 16:08:48 +00:00
|
|
|
'reason' => 'Some reason',
|
2012-06-21 20:25:37 +00:00
|
|
|
'token' => $pageinfo['blocktoken'] ), null, false, self::$users['sysop']->user );
|
2011-03-21 19:12:41 +00:00
|
|
|
|
2011-09-12 22:18:33 +00:00
|
|
|
$block = Block::newFromTarget('UTApiBlockee');
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2011-01-02 19:58:27 +00:00
|
|
|
$this->assertTrue( !is_null( $block ), 'Block is valid' );
|
|
|
|
|
|
2011-09-12 22:18:33 +00:00
|
|
|
$this->assertEquals( 'UTApiBlockee', (string)$block->getTarget() );
|
2011-01-02 19:58:27 +00:00
|
|
|
$this->assertEquals( 'Some reason', $block->mReason );
|
2011-11-10 09:36:18 +00:00
|
|
|
$this->assertEquals( 'infinity', $block->mExpiry );
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2011-01-02 19:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-03 20:02:27 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideBlockUnblockAction
|
|
|
|
|
*/
|
|
|
|
|
function testGetTokenUsingABlockingAction( $action ) {
|
|
|
|
|
$data = $this->doApiRequest(
|
|
|
|
|
array(
|
|
|
|
|
'action' => $action,
|
|
|
|
|
'user' => 'UTApiBlockee',
|
|
|
|
|
'gettoken' => '' ),
|
|
|
|
|
null,
|
|
|
|
|
false,
|
|
|
|
|
self::$users['sysop']->user
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals( 34, strlen( $data[0][$action]["{$action}token"] ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Attempting to block without a token should give a UsageException with
|
|
|
|
|
* error message:
|
|
|
|
|
* "The token parameter must be set"
|
|
|
|
|
*
|
|
|
|
|
* @dataProvider provideBlockUnblockAction
|
|
|
|
|
* @expectedException UsageException
|
|
|
|
|
*/
|
|
|
|
|
function testBlockingActionWithNoToken( $action ) {
|
|
|
|
|
$this->doApiRequest(
|
|
|
|
|
array(
|
|
|
|
|
'action' => $action,
|
|
|
|
|
'user' => 'UTApiBlockee',
|
|
|
|
|
'reason' => 'Some reason',
|
|
|
|
|
),
|
|
|
|
|
null,
|
|
|
|
|
false,
|
|
|
|
|
self::$users['sysop']->user
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Just provide the 'block' and 'unblock' action to test both API calls
|
|
|
|
|
*/
|
|
|
|
|
function provideBlockUnblockAction() {
|
|
|
|
|
return array(
|
|
|
|
|
array( 'block' ),
|
|
|
|
|
array( 'unblock' ),
|
|
|
|
|
);
|
|
|
|
|
}
|
2011-01-02 19:58:27 +00:00
|
|
|
}
|