MediaWiki  1.29.2
ApiBlockTest.php
Go to the documentation of this file.
1 <?php
2 
10 class ApiBlockTest extends ApiTestCase {
11  protected function setUp() {
12  parent::setUp();
13  $this->doLogin();
14  }
15 
16  protected function tearDown() {
17  $block = Block::newFromTarget( 'UTApiBlockee' );
18  if ( !is_null( $block ) ) {
19  $block->delete();
20  }
21  parent::tearDown();
22  }
23 
24  protected function getTokens() {
25  return $this->getTokenList( self::$users['sysop'] );
26  }
27 
28  function addDBDataOnce() {
29  $user = User::newFromName( 'UTApiBlockee' );
30 
31  if ( $user->getId() == 0 ) {
32  $user->addToDatabase();
33  TestUser::setPasswordForUser( $user, 'UTApiBlockeePassword' );
34 
35  $user->saveSettings();
36  }
37  }
38 
47  public function testMakeNormalBlock() {
48  $tokens = $this->getTokens();
49 
50  $user = User::newFromName( 'UTApiBlockee' );
51 
52  if ( !$user->getId() ) {
53  $this->markTestIncomplete( "The user UTApiBlockee does not exist" );
54  }
55 
56  if ( !array_key_exists( 'blocktoken', $tokens ) ) {
57  $this->markTestIncomplete( "No block token found" );
58  }
59 
60  $this->doApiRequest( [
61  'action' => 'block',
62  'user' => 'UTApiBlockee',
63  'reason' => 'Some reason',
64  'token' => $tokens['blocktoken'] ], null, false, self::$users['sysop']->getUser() );
65 
66  $block = Block::newFromTarget( 'UTApiBlockee' );
67 
68  $this->assertTrue( !is_null( $block ), 'Block is valid' );
69 
70  $this->assertEquals( 'UTApiBlockee', (string)$block->getTarget() );
71  $this->assertEquals( 'Some reason', $block->mReason );
72  $this->assertEquals( 'infinity', $block->mExpiry );
73  }
74 
78  public function testMakeNormalBlockId() {
79  $tokens = $this->getTokens();
80  $user = User::newFromName( 'UTApiBlockee' );
81 
82  if ( !$user->getId() ) {
83  $this->markTestIncomplete( "The user UTApiBlockee does not exist." );
84  }
85 
86  if ( !array_key_exists( 'blocktoken', $tokens ) ) {
87  $this->markTestIncomplete( "No block token found" );
88  }
89 
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() );
95 
96  $block = Block::newFromTarget( 'UTApiBlockee' );
97 
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 );
102  }
103 
108  public function testBlockingActionWithNoToken() {
109  $this->doApiRequest(
110  [
111  'action' => 'block',
112  'user' => 'UTApiBlockee',
113  'reason' => 'Some reason',
114  ],
115  null,
116  false,
117  self::$users['sysop']->getUser()
118  );
119  }
120 }
ApiBlockTest\testMakeNormalBlock
testMakeNormalBlock()
This test has probably always been broken and use an invalid token Bug tracking brokenness is https:/...
Definition: ApiBlockTest.php:47
ApiBlockTest\testMakeNormalBlockId
testMakeNormalBlockId()
Block by user ID.
Definition: ApiBlockTest.php:78
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:246
ApiBlockTest\setUp
setUp()
Definition: ApiBlockTest.php:11
Block\newFromTarget
static newFromTarget( $specificTarget, $vagueTarget=null, $fromMaster=false)
Given a target and the target's type, get an existing Block object if possible.
Definition: Block.php:1113
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:556
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ApiTestCase\getTokenList
getTokenList(TestUser $user, $session=null)
Definition: ApiTestCase.php:188
ApiBlockTest
API Database medium.
Definition: ApiBlockTest.php:10
ApiBlockTest\addDBDataOnce
addDBDataOnce()
Stub.
Definition: ApiBlockTest.php:28
ApiBlockTest\tearDown
tearDown()
Definition: ApiBlockTest.php:16
ApiTestCase\doLogin
doLogin( $testUser='sysop')
Definition: ApiTestCase.php:152
ApiTestCase
Definition: ApiTestCase.php:3
TestUser\setPasswordForUser
static setPasswordForUser(User $user, $password)
Set the password on a testing user.
Definition: TestUser.php:127
$tokens
$tokens
Definition: mwdoc-filter.php:46
ApiBlockTest\getTokens
getTokens()
Definition: ApiBlockTest.php:24
ApiTestCase\doApiRequest
doApiRequest(array $params, array $session=null, $appendModule=false, User $user=null)
Does the API request and returns the result.
Definition: ApiTestCase.php:73
ApiBlockTest\testBlockingActionWithNoToken
testBlockingActionWithNoToken()
ApiUsageException The "token" parameter must be set.
Definition: ApiBlockTest.php:108