wiki.techinc.nl/tests/phpunit/includes/api/ApiBlockTest.php
daniel a8995619c1 Avoid rebuilding database fixtures for every test run
This reduces the runtime of database-bound tests by about 40%
(on my system, from 4:55 to 2:47; results from Jenkins are
inconclusive).

The basic idea is to call addCoreDBData() only once, and have
a addDBDataOnce() that is called once per test class, not for
every test method lie addDBData() is. Most tests could be
trivially be changed to implement addDBDataOnce() instead of
addDBData(). The ones for which this did not work immediately
were left out for now. A closer look at the tests that still
implement addDBData() may reveal additional potential for
improvement.

TODO: Once this is merged, try to change addDBData() to
addDBDataOnce() where possible in extensions.

Change-Id: Iec4ed4c8419fb4ad87e6710de808863ede9998b7
2016-03-10 23:44:34 +00:00

83 lines
2 KiB
PHP

<?php
/**
* @group API
* @group Database
* @group medium
*
* @covers ApiBlock
*/
class ApiBlockTest extends ApiTestCase {
protected function setUp() {
parent::setUp();
$this->doLogin();
}
protected function getTokens() {
return $this->getTokenList( self::$users['sysop'] );
}
function addDBDataOnce() {
$user = User::newFromName( 'UTApiBlockee' );
if ( $user->getId() == 0 ) {
$user->addToDatabase();
TestUser::setPasswordForUser( $user, 'UTApiBlockeePassword' );
$user->saveSettings();
}
}
/**
* This test has probably always been broken and use an invalid token
* Bug tracking brokenness is https://phabricator.wikimedia.org/T37646
*
* Root cause is https://gerrit.wikimedia.org/r/3434
* Which made the Block/Unblock API to actually verify the token
* previously always considered valid (T36212).
*/
public function testMakeNormalBlock() {
$tokens = $this->getTokens();
$user = User::newFromName( 'UTApiBlockee' );
if ( !$user->getId() ) {
$this->markTestIncomplete( "The user UTApiBlockee does not exist" );
}
if ( !array_key_exists( 'blocktoken', $tokens ) ) {
$this->markTestIncomplete( "No block token found" );
}
$this->doApiRequest( [
'action' => 'block',
'user' => 'UTApiBlockee',
'reason' => 'Some reason',
'token' => $tokens['blocktoken'] ], null, false, self::$users['sysop']->getUser() );
$block = Block::newFromTarget( 'UTApiBlockee' );
$this->assertTrue( !is_null( $block ), 'Block is valid' );
$this->assertEquals( 'UTApiBlockee', (string)$block->getTarget() );
$this->assertEquals( 'Some reason', $block->mReason );
$this->assertEquals( 'infinity', $block->mExpiry );
}
/**
* @expectedException UsageException
* @expectedExceptionMessage The token parameter must be set
*/
public function testBlockingActionWithNoToken() {
$this->doApiRequest(
[
'action' => 'block',
'user' => 'UTApiBlockee',
'reason' => 'Some reason',
],
null,
false,
self::$users['sysop']->getUser()
);
}
}