mediawiki-extensions-AbuseF.../tests/phpunit/LazyVariableComputerDBTest.php
Timo Tijhof 4743f9d267 tests: Widen @covers tags in phpunit tests
Follows-up I5a5420df13893386.

> We lose useful coverage and waste valuable time on keeping tags
> accurate through refactors (or worse, forget to do so).
>
> Tracking tiny per-method details wastes time in realizing (and
> fixing) when people inevitably don't keep them in sync, and time
> lost in finding uncovered code to write tests to realize it was
> already covered but "not yet claimed".
>
> Given all used methods are de-facto and liberally claimed, and
> that we keep the coverage limited to the subject class, this
> maintains the spirit and intent. PHPUnit offers a more precise
> tool when you need it (i.e. when testing legacy monster classes),
> but for well-written code, the class-wide tag suffices.

Ref https://gerrit.wikimedia.org/r/q/owner:Krinkle+is:merged+message:Widen

Change-Id: If7304d8b5b43ab8a051fbcecced331a787bab960
2024-04-17 01:44:40 +01:00

68 lines
2.1 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\Title\Title;
use MediaWiki\User\UserIdentityValue;
/**
* @group Test
* @group AbuseFilter
* @group Database
* @covers \MediaWiki\Extension\AbuseFilter\Variables\LazyVariableComputer
* @todo Move to LazyVariableComputerTest
*/
class LazyVariableComputerDBTest extends MediaWikiIntegrationTestCase {
/**
* 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;
}
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 );
}
}