Move ResultWrapper subclasses to Rdbms
[mediawiki.git] / tests / phpunit / includes / api / ApiBlockTest.php
blob832a113f0e111a1d75a83f1ea72c812c714727c6
1 <?php
3 /**
4 * @group API
5 * @group Database
6 * @group medium
8 * @covers ApiBlock
9 */
10 class ApiBlockTest extends ApiTestCase {
11 protected function setUp() {
12 parent::setUp();
13 $this->doLogin();
16 protected function tearDown() {
17 $block = Block::newFromTarget( 'UTApiBlockee' );
18 if ( !is_null( $block ) ) {
19 $block->delete();
21 parent::tearDown();
24 protected function getTokens() {
25 return $this->getTokenList( self::$users['sysop'] );
28 function addDBDataOnce() {
29 $user = User::newFromName( 'UTApiBlockee' );
31 if ( $user->getId() == 0 ) {
32 $user->addToDatabase();
33 TestUser::setPasswordForUser( $user, 'UTApiBlockeePassword' );
35 $user->saveSettings();
39 /**
40 * This test has probably always been broken and use an invalid token
41 * Bug tracking brokenness is https://phabricator.wikimedia.org/T37646
43 * Root cause is https://gerrit.wikimedia.org/r/3434
44 * Which made the Block/Unblock API to actually verify the token
45 * previously always considered valid (T36212).
47 public function testMakeNormalBlock() {
48 $tokens = $this->getTokens();
50 $user = User::newFromName( 'UTApiBlockee' );
52 if ( !$user->getId() ) {
53 $this->markTestIncomplete( "The user UTApiBlockee does not exist" );
56 if ( !array_key_exists( 'blocktoken', $tokens ) ) {
57 $this->markTestIncomplete( "No block token found" );
60 $this->doApiRequest( [
61 'action' => 'block',
62 'user' => 'UTApiBlockee',
63 'reason' => 'Some reason',
64 'token' => $tokens['blocktoken'] ], null, false, self::$users['sysop']->getUser() );
66 $block = Block::newFromTarget( 'UTApiBlockee' );
68 $this->assertTrue( !is_null( $block ), 'Block is valid' );
70 $this->assertEquals( 'UTApiBlockee', (string)$block->getTarget() );
71 $this->assertEquals( 'Some reason', $block->mReason );
72 $this->assertEquals( 'infinity', $block->mExpiry );
75 /**
76 * Block by user ID
78 public function testMakeNormalBlockId() {
79 $tokens = $this->getTokens();
80 $user = User::newFromName( 'UTApiBlockee' );
82 if ( !$user->getId() ) {
83 $this->markTestIncomplete( "The user UTApiBlockee does not exist." );
86 if ( !array_key_exists( 'blocktoken', $tokens ) ) {
87 $this->markTestIncomplete( "No block token found" );
90 $data = $this->doApiRequest( [
91 'action' => 'block',
92 'userid' => $user->getId(),
93 'reason' => 'Some reason',
94 'token' => $tokens['blocktoken'] ], null, false, self::$users['sysop']->getUser() );
96 $block = Block::newFromTarget( 'UTApiBlockee' );
98 $this->assertTrue( !is_null( $block ), 'Block is valid.' );
99 $this->assertEquals( 'UTApiBlockee', (string)$block->getTarget() );
100 $this->assertEquals( 'Some reason', $block->mReason );
101 $this->assertEquals( 'infinity', $block->mExpiry );
105 * @expectedException ApiUsageException
106 * @expectedExceptionMessage The "token" parameter must be set
108 public function testBlockingActionWithNoToken() {
109 $this->doApiRequest(
111 'action' => 'block',
112 'user' => 'UTApiBlockee',
113 'reason' => 'Some reason',
115 null,
116 false,
117 self::$users['sysop']->getUser()