2020-01-13 13:07:34 +00:00
|
|
|
<?php
|
|
|
|
|
2018-08-28 17:22:16 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
|
2021-01-14 16:18:13 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Variables\LazyLoadedVariable;
|
2021-01-02 14:01:00 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Variables\VariableHolder;
|
2022-07-02 12:45:19 +00:00
|
|
|
use MediaWiki\Permissions\UltimateAuthority;
|
2023-12-10 19:03:19 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2022-07-02 12:45:19 +00:00
|
|
|
use MediaWiki\User\UserIdentityValue;
|
2020-01-13 13:07:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group Test
|
|
|
|
* @group AbuseFilter
|
|
|
|
* @group Database
|
2024-04-17 00:44:40 +00:00
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\Variables\LazyVariableComputer
|
2021-01-08 17:17:41 +00:00
|
|
|
* @todo Move to LazyVariableComputerTest
|
2020-01-13 13:07:34 +00:00
|
|
|
*/
|
2021-01-08 17:17:41 +00:00
|
|
|
class LazyVariableComputerDBTest extends MediaWikiIntegrationTestCase {
|
2020-09-16 09:15:38 +00:00
|
|
|
|
2020-01-13 13:07:34 +00:00
|
|
|
/**
|
|
|
|
* 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(
|
2022-06-28 20:46:45 +00:00
|
|
|
$title,
|
2020-01-13 13:07:34 +00:00
|
|
|
'AbuseFilter test for title variables',
|
|
|
|
'',
|
2022-06-28 20:46:45 +00:00
|
|
|
NS_MAIN,
|
2020-01-13 13:07:34 +00:00
|
|
|
$user
|
|
|
|
);
|
|
|
|
$mockContributors = [ 'X>Alice', 'X>Bob', 'X>Charlie' ];
|
|
|
|
foreach ( $mockContributors as $contributor ) {
|
|
|
|
$this->editPage(
|
2022-06-28 20:46:45 +00:00
|
|
|
$title,
|
2020-01-13 13:07:34 +00:00
|
|
|
"page revision by $contributor",
|
|
|
|
'',
|
2022-06-28 20:46:45 +00:00
|
|
|
NS_MAIN,
|
2022-07-02 12:45:19 +00:00
|
|
|
new UltimateAuthority( UserIdentityValue::newAnonymous( $contributor ) )
|
2020-01-13 13:07:34 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
$contributors = array_reverse( $mockContributors );
|
|
|
|
$contributors[] = $user->getName();
|
|
|
|
return $contributors;
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:18:13 +00:00
|
|
|
public function testRecentContributors() {
|
|
|
|
$varName = "page_recent_contributors";
|
2022-06-28 20:46:45 +00:00
|
|
|
$title = Title::makeTitle( NS_MAIN, "Page to test $varName" );
|
2020-01-13 13:07:34 +00:00
|
|
|
|
2021-01-08 14:59:56 +00:00
|
|
|
$expected = $this->computeRecentContributors( $title );
|
2021-01-14 16:18:13 +00:00
|
|
|
$computer = AbuseFilterServices::getLazyVariableComputer();
|
|
|
|
$var = new LazyLoadedVariable(
|
|
|
|
'load-recent-authors',
|
|
|
|
[ 'title' => $title ]
|
2021-01-08 14:59:56 +00:00
|
|
|
);
|
2021-04-30 18:54:17 +00:00
|
|
|
$forbidComputeCB = static function () {
|
2021-01-14 16:18:13 +00:00
|
|
|
throw new LogicException( 'Not expected to be called' );
|
|
|
|
};
|
|
|
|
$actual = $computer->compute( $var, new VariableHolder(), $forbidComputeCB )->toNative();
|
|
|
|
$this->assertSame( $expected, $actual );
|
2020-01-13 13:07:34 +00:00
|
|
|
}
|
|
|
|
}
|