mediawiki-extensions-AbuseF.../tests/phpunit/LazyVariableComputerDBTest.php
Matěj Suchánek 702d77e3ce Create real integration test for variables
For fixing bugs like T65632, T105325, or T264104, we will need
to update code in more than one place at once. To prevent
regressions, create an integration test which tests the whole
pipeline, from the request submission to variable evaluation.
Edits are simulated using action=edit API call because the hook
AbuseFilter uses is run from EditPage.

To increase confidence in test coverage, remove some annotations
from AbuseFilterConsequencesTest or make them less greedy.
Ideally, it would only test consequences.

This patch includes refactoring of AbuseFilterCreateAccountTestTrait
which now only inserts the user into the database if it really
should be created.
It also restores test coverage of some other classes.

Change-Id: I661f4e0e2bcac4770e499708fca4e4e153f31fed
2022-11-26 18:51:38 +01:00

79 lines
2.2 KiB
PHP

<?php
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
use MediaWiki\Extension\AbuseFilter\Variables\LazyLoadedVariable;
use MediaWiki\Extension\AbuseFilter\Variables\VariableHolder;
use MediaWiki\Permissions\UltimateAuthority;
use MediaWiki\User\UserIdentityValue;
/**
* @group Test
* @group AbuseFilter
* @group Database
* @coversDefaultClass \MediaWiki\Extension\AbuseFilter\Variables\LazyVariableComputer
* @todo Move to LazyVariableComputerTest
*/
class LazyVariableComputerDBTest extends MediaWikiIntegrationTestCase {
/** @inheritDoc */
protected $tablesUsed = [
'page',
'text',
'user',
'recentchanges',
];
/**
* Make different users edit a page, so that we can check their names against
* the actual value of a _recent_contributors variable
* @param Title $title
* @return string[]
*/
private function computeRecentContributors( Title $title ) {
// This test uses a custom DB query and it's hard to use mocks
$user = $this->getMutableTestUser()->getUser();
// Create the page and make a couple of edits from different users
$this->editPage(
$title,
'AbuseFilter test for title variables',
'',
NS_MAIN,
$user
);
$mockContributors = [ 'X>Alice', 'X>Bob', 'X>Charlie' ];
foreach ( $mockContributors as $contributor ) {
$this->editPage(
$title,
"page revision by $contributor",
'',
NS_MAIN,
new UltimateAuthority( UserIdentityValue::newAnonymous( $contributor ) )
);
}
$contributors = array_reverse( $mockContributors );
$contributors[] = $user->getName();
return $contributors;
}
/**
* @covers ::compute
* @covers ::getLastPageAuthors
*/
public function testRecentContributors() {
$varName = "page_recent_contributors";
$title = Title::makeTitle( NS_MAIN, "Page to test $varName" );
$expected = $this->computeRecentContributors( $title );
$computer = AbuseFilterServices::getLazyVariableComputer();
$var = new LazyLoadedVariable(
'load-recent-authors',
[ 'title' => $title ]
);
$forbidComputeCB = static function () {
throw new LogicException( 'Not expected to be called' );
};
$actual = $computer->compute( $var, new VariableHolder(), $forbidComputeCB )->toNative();
$this->assertSame( $expected, $actual );
}
}