test: coverage recording now needs to be explicit
[mediawiki.git] / tests / phpunit / includes / api / ApiBlockTest.php
bloba3d39dd712fef2dc3c41b6c407091ac37fea637f
1 <?php
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();
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() {
38 $data = $this->getTokens();
40 $user = User::newFromName( 'UTApiBlockee' );
42 if ( !$user->getId() ) {
43 $this->markTestIncomplete( "The user UTApiBlockee does not exist" );
46 if ( !isset( $data[0]['query']['pages'] ) ) {
47 $this->markTestIncomplete( "No block token found" );
50 $keys = array_keys( $data[0]['query']['pages'] );
51 $key = array_pop( $keys );
52 $pageinfo = $data[0]['query']['pages'][$key];
54 $this->doApiRequest( array(
55 'action' => 'block',
56 'user' => 'UTApiBlockee',
57 'reason' => 'Some reason',
58 'token' => $pageinfo['blocktoken'] ), null, false, self::$users['sysop']->user );
60 $block = Block::newFromTarget( 'UTApiBlockee' );
62 $this->assertTrue( !is_null( $block ), 'Block is valid' );
64 $this->assertEquals( 'UTApiBlockee', (string)$block->getTarget() );
65 $this->assertEquals( 'Some reason', $block->mReason );
66 $this->assertEquals( 'infinity', $block->mExpiry );
69 /**
70 * @dataProvider provideBlockUnblockAction
72 function testGetTokenUsingABlockingAction( $action ) {
73 $data = $this->doApiRequest(
74 array(
75 'action' => $action,
76 'user' => 'UTApiBlockee',
77 'gettoken' => '' ),
78 null,
79 false,
80 self::$users['sysop']->user
82 $this->assertEquals( 34, strlen( $data[0][$action]["{$action}token"] ) );
85 /**
86 * Attempting to block without a token should give a UsageException with
87 * error message:
88 * "The token parameter must be set"
90 * @dataProvider provideBlockUnblockAction
91 * @expectedException UsageException
93 function testBlockingActionWithNoToken( $action ) {
94 $this->doApiRequest(
95 array(
96 'action' => $action,
97 'user' => 'UTApiBlockee',
98 'reason' => 'Some reason',
100 null,
101 false,
102 self::$users['sysop']->user
107 * Just provide the 'block' and 'unblock' action to test both API calls
109 public static function provideBlockUnblockAction() {
110 return array(
111 array( 'block' ),
112 array( 'unblock' ),