diff --git a/includes/Variables/LazyVariableComputer.php b/includes/Variables/LazyVariableComputer.php index 7b3b00746..0802fdf11 100644 --- a/includes/Variables/LazyVariableComputer.php +++ b/includes/Variables/LazyVariableComputer.php @@ -395,6 +395,7 @@ class LazyVariableComputer { } /** + * @todo Move to MW core (T272050) * @param Title $title * @return string[] Usernames of the last 10 (unique) authors from $title */ diff --git a/tests/phpunit/LazyVariableComputerDBTest.php b/tests/phpunit/LazyVariableComputerDBTest.php index 3dd2c0306..3420d9aca 100644 --- a/tests/phpunit/LazyVariableComputerDBTest.php +++ b/tests/phpunit/LazyVariableComputerDBTest.php @@ -1,6 +1,7 @@ computeRecentContributors( $title ); - $generator = AbuseFilterServices::getVariableGeneratorFactory()->newGenerator(); - $vars = $generator->addTitleVars( $title, $prefix )->getVariableHolder(); - $manager = AbuseFilterServices::getVariablesManager(); - $actual = $manager->getVar( $vars, $varName )->toNative(); - $this->assertSame( $expected, $actual, "Prefix: $prefix" ); - } - - public function providePrefix() : array { - return [ - 'page' => [ 'page' ], - 'moved_from' => [ 'moved_from' ], - 'moved_to' => [ 'moved_to' ], - ]; - } - - /** - * @param string $prefix - * @covers ::compute - * @dataProvider providePrefix - */ - public function testFirstContributorVar( string $prefix ) { - $varName = "{$prefix}_first_contributor"; - $title = Title::makeTitle( NS_MAIN, "Page to test $varName" ); - $user = $this->getMutableTestUser()->getUser(); - $this->editPage( - $title->getText(), - 'AbuseFilter test for title variables', - '', - $title->getNamespace(), - $user + $computer = AbuseFilterServices::getLazyVariableComputer(); + $var = new LazyLoadedVariable( + 'load-recent-authors', + [ 'title' => $title ] ); - $expected = $user->getName(); - - $generator = AbuseFilterServices::getVariableGeneratorFactory()->newGenerator(); - $vars = $generator->addTitleVars( $title, $prefix )->getVariableHolder(); - $manager = AbuseFilterServices::getVariablesManager(); - $actual = $manager->getVar( $vars, $varName )->toNative(); - $this->assertSame( $expected, $actual, "Prefix: $prefix" ); + $forbidComputeCB = function () { + throw new LogicException( 'Not expected to be called' ); + }; + $actual = $computer->compute( $var, new VariableHolder(), $forbidComputeCB )->toNative(); + $this->assertSame( $expected, $actual ); } } diff --git a/tests/phpunit/unit/Variables/LazyVariableComputerTest.php b/tests/phpunit/unit/Variables/LazyVariableComputerTest.php index 696756fd6..6b75a46dd 100644 --- a/tests/phpunit/unit/Variables/LazyVariableComputerTest.php +++ b/tests/phpunit/unit/Variables/LazyVariableComputerTest.php @@ -15,6 +15,7 @@ use MediaWiki\Extension\AbuseFilter\Variables\VariableHolder; use MediaWiki\Revision\RevisionLookup; use MediaWiki\Revision\RevisionRecord; use MediaWiki\Revision\RevisionStore; +use MediaWiki\User\UserIdentityValue; use MediaWikiUnitTestCase; use MWTimestamp; use Parser; @@ -274,6 +275,28 @@ class LazyVariableComputerTest extends MediaWikiUnitTestCase { ); yield "*_age" => [ $var, $age, $revLookup ]; - // TODO _first_contributor and _recent_contributors are tested in LazyVariableComputerDBTest + $title = $this->createMock( Title::class ); + $firstRev = $this->createMock( RevisionRecord::class ); + $firstUserName = 'First author'; + $firstUser = new UserIdentityValue( 1, $firstUserName, 42 ); + $firstRev->expects( $this->once() )->method( 'getUser' )->willReturn( $firstUser ); + $revLookup = $this->createMock( RevisionLookup::class ); + $revLookup->method( 'getFirstRevision' )->with( $title )->willReturn( $firstRev ); + $var = new LazyLoadedVariable( + 'load-first-author', + [ 'title' => $title ] + ); + yield '*_first_contributor, with rev' => [ $var, $firstUserName, $revLookup ]; + + $title = $this->createMock( Title::class ); + $revLookup = $this->createMock( RevisionLookup::class ); + $revLookup->method( 'getFirstRevision' )->with( $title )->willReturn( null ); + $var = new LazyLoadedVariable( + 'load-first-author', + [ 'title' => $title ] + ); + yield '*_first_contributor, no rev' => [ $var, '', $revLookup ]; + + // TODO _recent_contributors is tested in LazyVariableComputerDBTest } }