| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices; |
| 4 |
use MediaWiki\Extension\AbuseFilter\Variables\LazyLoadedVariable; |
| 5 |
use MediaWiki\Extension\AbuseFilter\Variables\VariableHolder; |
| 6 |
use MediaWiki\Permissions\UltimateAuthority; |
| 7 |
use MediaWiki\Title\Title; |
| 8 |
use MediaWiki\User\UserIdentityValue; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class LazyVariableComputerDBTest extends MediaWikiIntegrationTestCase { |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
private function computeRecentContributors( Title $title ) { |
| 26 |
|
| 27 |
$user = $this->getMutableTestUser()->getUser(); |
| 28 |
|
| 29 |
$this->editPage( |
| 30 |
$title, |
| 31 |
'AbuseFilter test for title variables', |
| 32 |
'', |
| 33 |
NS_MAIN, |
| 34 |
$user |
| 35 |
); |
| 36 |
$mockContributors = [ 'X>Alice', 'X>Bob', 'X>Charlie' ]; |
| 37 |
foreach ( $mockContributors as $contributor ) { |
| 38 |
$this->editPage( |
| 39 |
$title, |
| 40 |
"page revision by $contributor", |
| 41 |
'', |
| 42 |
NS_MAIN, |
| 43 |
new UltimateAuthority( UserIdentityValue::newAnonymous( $contributor ) ) |
| 44 |
); |
| 45 |
} |
| 46 |
$contributors = array_reverse( $mockContributors ); |
| 47 |
$contributors[] = $user->getName(); |
| 48 |
return $contributors; |
| 49 |
} |
| 50 |
|
| 51 |
public function testRecentContributors() { |
| 52 |
$varName = "page_recent_contributors"; |
| 53 |
$title = Title::makeTitle( NS_MAIN, "Page to test $varName" ); |
| 54 |
|
| 55 |
$expected = $this->computeRecentContributors( $title ); |
| 56 |
$computer = AbuseFilterServices::getLazyVariableComputer(); |
| 57 |
$var = new LazyLoadedVariable( |
| 58 |
'load-recent-authors', |
| 59 |
[ 'title' => $title ] |
| 60 |
); |
| 61 |
$forbidComputeCB = static function () { |
| 62 |
throw new LogicException( 'Not expected to be called' ); |
| 63 |
}; |
| 64 |
$actual = $computer->compute( $var, new VariableHolder(), $forbidComputeCB )->toNative(); |
| 65 |
$this->assertSame( $expected, $actual ); |
| 66 |
} |
| 67 |
} |