Debian Codegraph @

extensions/AbuseFilter/tests/phpunit/LazyVariableComputerDBTest.php

raw symbols SCIP
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 * @group Test
12 * @group AbuseFilter
13 * @group Database
14 * @covers \MediaWiki\Extension\AbuseFilter\Variables\LazyVariableComputer
15 * @todo Move to LazyVariableComputerTest
16 */
17 class LazyVariableComputerDBTest extends MediaWikiIntegrationTestCase {
18
19 /**
20 * Make different users edit a page, so that we can check their names against
21 * the actual value of a _recent_contributors variable
22 * @param Title $title
23 * @return string[]
24 */
25 private function computeRecentContributors( Title $title ) {
26 // This test uses a custom DB query and it's hard to use mocks
27 $user = $this->getMutableTestUser()->getUser();
28 // Create the page and make a couple of edits from different users
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 }