2020-01-13 13:07:34 +00:00
|
|
|
<?php
|
|
|
|
|
2018-08-28 17:22:16 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
|
2021-01-02 14:01:00 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Variables\VariableHolder;
|
2020-01-13 13:07:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group Test
|
|
|
|
* @group AbuseFilter
|
|
|
|
* @group AbuseFilterGeneric
|
|
|
|
* @group Database
|
2021-01-08 17:17:41 +00:00
|
|
|
* @coversDefaultClass \MediaWiki\Extension\AbuseFilter\Variables\LazyVariableComputer
|
|
|
|
* @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
|
|
|
protected $tablesUsed = [
|
|
|
|
'page',
|
|
|
|
'text',
|
|
|
|
'page_restrictions',
|
|
|
|
'user',
|
2018-08-28 17:22:16 +00:00
|
|
|
'recentchanges',
|
2020-01-13 13:07:34 +00:00
|
|
|
];
|
|
|
|
|
2018-08-28 17:22:16 +00:00
|
|
|
/**
|
2020-01-13 13:07:34 +00:00
|
|
|
* @param string $oldText The old wikitext of the page
|
|
|
|
* @param string $newText The new wikitext of the page
|
|
|
|
* @param string $summary
|
|
|
|
* @param array $expected Expected edit vars
|
2021-01-08 17:17:41 +00:00
|
|
|
* @covers ::compute
|
|
|
|
* @dataProvider provideEditRelatedVars
|
2020-01-13 13:07:34 +00:00
|
|
|
*/
|
2021-01-08 17:17:41 +00:00
|
|
|
public function testEditRelatedVars( $oldText, $newText, $summary, array $expected ) {
|
2020-01-13 13:07:34 +00:00
|
|
|
$pageName = __METHOD__;
|
|
|
|
$title = Title::makeTitle( 0, $pageName );
|
|
|
|
$page = WikiPage::factory( $title );
|
|
|
|
|
|
|
|
$this->editPage( $pageName, $oldText, 'Creating the test page' );
|
|
|
|
$this->editPage( $pageName, $newText, $summary );
|
|
|
|
|
2021-01-02 14:01:00 +00:00
|
|
|
$baseVars = VariableHolder::newFromArray( [
|
2020-01-13 13:07:34 +00:00
|
|
|
'old_wikitext' => $oldText,
|
|
|
|
'new_wikitext' => $newText,
|
|
|
|
'summary' => $summary
|
|
|
|
] );
|
|
|
|
|
2021-01-03 13:10:20 +00:00
|
|
|
$generator = AbuseFilterServices::getVariableGeneratorFactory()->newGenerator( $baseVars );
|
2021-01-08 17:17:41 +00:00
|
|
|
$actualHolder = $generator->addEditVars( $page, $this->createMock( User::class ) )
|
2020-10-18 22:25:05 +00:00
|
|
|
->getVariableHolder();
|
|
|
|
$actual = AbuseFilterServices::getVariablesManager()->exportAllVars( $actualHolder );
|
2020-01-13 13:07:34 +00:00
|
|
|
|
|
|
|
// Special case for new_html: avoid flaky tests, and only check containment
|
|
|
|
$this->assertStringContainsString( '<div class="mw-parser-output', $actual['new_html'] );
|
|
|
|
$this->assertNotRegExp( "/<!--\s*NewPP limit/", $actual['new_html'] );
|
|
|
|
$this->assertNotRegExp( "/<!--\s*Transclusion/", $actual['new_html'] );
|
|
|
|
foreach ( $expected['new_html'] as $needle ) {
|
|
|
|
$this->assertStringContainsString( $needle, $actual['new_html'], 'Checking new_html' );
|
|
|
|
}
|
|
|
|
unset( $actual['new_html'], $expected['new_html'] );
|
|
|
|
|
|
|
|
$this->assertEquals( $expected, $actual );
|
|
|
|
}
|
|
|
|
|
2021-01-08 17:17:41 +00:00
|
|
|
public function provideEditRelatedVars() : Generator {
|
2020-01-13 13:07:34 +00:00
|
|
|
$summary = __METHOD__;
|
|
|
|
|
|
|
|
// phpcs:disable Generic.Files.LineLength
|
|
|
|
$old = '[https://a.com Test] foo';
|
|
|
|
$new = "'''Random'''.\nSome ''special'' chars: àèìòù 名探偵コナン.\n[[Help:PST|]] test, [//www.b.com link]";
|
|
|
|
$expected = [
|
|
|
|
'old_wikitext' => $old,
|
|
|
|
'new_wikitext' => $new,
|
|
|
|
'summary' => $summary,
|
|
|
|
'new_html' => [ '<p><b>Random</b>', '<i>special</i>', 'PST</a>', 'link</a>' ],
|
|
|
|
'new_pst' => "'''Random'''.\nSome ''special'' chars: àèìòù 名探偵コナン.\n[[Help:PST|PST]] test, [//www.b.com link]",
|
|
|
|
'new_text' => "Random.\nSome special chars: àèìòù 名探偵コナン.\nPST test, link",
|
|
|
|
'edit_diff' => "@@ -1,1 +1,3 @@\n-[https://a.com Test] foo\n+'''Random'''.\n+Some ''special'' chars: àèìòù 名探偵コナン.\n+[[Help:PST|]] test, [//www.b.com link]\n",
|
|
|
|
'edit_diff_pst' => "@@ -1,1 +1,3 @@\n-[https://a.com Test] foo\n+'''Random'''.\n+Some ''special'' chars: àèìòù 名探偵コナン.\n+[[Help:PST|PST]] test, [//www.b.com link]\n",
|
|
|
|
'new_size' => strlen( $new ),
|
|
|
|
'old_size' => strlen( $old ),
|
|
|
|
'edit_delta' => strlen( $new ) - strlen( $old ),
|
|
|
|
'added_lines' => explode( "\n", $new ),
|
|
|
|
'removed_lines' => [ $old ],
|
|
|
|
'added_lines_pst' => [ "'''Random'''.", "Some ''special'' chars: àèìòù 名探偵コナン.", '[[Help:PST|PST]] test, [//www.b.com link]' ],
|
|
|
|
'all_links' => [ '//www.b.com' ],
|
|
|
|
'old_links' => [ 'https://a.com' ],
|
|
|
|
'added_links' => [ '//www.b.com' ],
|
|
|
|
'removed_links' => [ 'https://a.com' ]
|
|
|
|
];
|
|
|
|
|
|
|
|
yield 'PST and special chars' => [ $old, $new, $summary, $expected ];
|
|
|
|
|
|
|
|
$old = "'''Random'''.\nSome ''special'' chars: àèìòù 名探偵コナン.\n[[Help:PST|]] test, [//www.b.com link]";
|
|
|
|
$new = '[https://a.com Test] foo';
|
|
|
|
$expected = [
|
|
|
|
'old_wikitext' => $old,
|
|
|
|
'new_wikitext' => $new,
|
|
|
|
'summary' => $summary,
|
|
|
|
'new_html' => [ 'Test</a>' ],
|
|
|
|
'new_pst' => '[https://a.com Test] foo',
|
|
|
|
'new_text' => 'Test foo',
|
|
|
|
'edit_diff' => "@@ -1,3 +1,1 @@\n-'''Random'''.\n-Some ''special'' chars: àèìòù 名探偵コナン.\n-[[Help:PST|]] test, [//www.b.com link]\n+[https://a.com Test] foo\n",
|
|
|
|
'edit_diff_pst' => "@@ -1,3 +1,1 @@\n-'''Random'''.\n-Some ''special'' chars: àèìòù 名探偵コナン.\n-[[Help:PST|]] test, [//www.b.com link]\n+[https://a.com Test] foo\n",
|
|
|
|
'new_size' => strlen( $new ),
|
|
|
|
'old_size' => strlen( $old ),
|
|
|
|
'edit_delta' => strlen( $new ) - strlen( $old ),
|
|
|
|
'added_lines' => [ $new ],
|
|
|
|
'removed_lines' => explode( "\n", $old ),
|
|
|
|
'added_lines_pst' => [ $new ],
|
|
|
|
'all_links' => [ 'https://a.com' ],
|
|
|
|
'old_links' => [ '//www.b.com' ],
|
|
|
|
'added_links' => [ 'https://a.com' ],
|
|
|
|
'removed_links' => [ '//www.b.com' ]
|
|
|
|
];
|
|
|
|
|
|
|
|
yield 'PST and special chars, reverse' => [ $old, $new, $summary, $expected ];
|
|
|
|
// phpcs:enable Generic.Files.LineLength
|
|
|
|
|
|
|
|
$old = 'This edit will be pretty smal';
|
|
|
|
$new = $old . 'l';
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'old_wikitext' => $old,
|
|
|
|
'new_wikitext' => $new,
|
|
|
|
'summary' => $summary,
|
|
|
|
'new_html' => [ "<p>This edit will be pretty small\n</p>" ],
|
|
|
|
'new_pst' => $new,
|
|
|
|
'new_text' => $new,
|
|
|
|
'edit_diff' => "@@ -1,1 +1,1 @@\n-$old\n+$new\n",
|
|
|
|
'edit_diff_pst' => "@@ -1,1 +1,1 @@\n-$old\n+$new\n",
|
|
|
|
'new_size' => strlen( $new ),
|
|
|
|
'old_size' => strlen( $old ),
|
|
|
|
'edit_delta' => 1,
|
|
|
|
'added_lines' => [ $new ],
|
|
|
|
'removed_lines' => [ $old ],
|
|
|
|
'added_lines_pst' => [ $new ],
|
|
|
|
'all_links' => [],
|
|
|
|
'old_links' => [],
|
|
|
|
'added_links' => [],
|
|
|
|
'removed_links' => []
|
|
|
|
];
|
|
|
|
|
|
|
|
yield 'Small edit' => [ $old, $new, $summary, $expected ];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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->getText(),
|
|
|
|
'AbuseFilter test for title variables',
|
|
|
|
'',
|
|
|
|
$title->getNamespace(),
|
|
|
|
$user
|
|
|
|
);
|
|
|
|
$mockContributors = [ 'X>Alice', 'X>Bob', 'X>Charlie' ];
|
|
|
|
foreach ( $mockContributors as $contributor ) {
|
|
|
|
$this->editPage(
|
|
|
|
$title->getText(),
|
|
|
|
"page revision by $contributor",
|
|
|
|
'',
|
|
|
|
$title->getNamespace(),
|
|
|
|
User::newFromName( $contributor, false )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$contributors = array_reverse( $mockContributors );
|
|
|
|
$contributors[] = $user->getName();
|
|
|
|
return $contributors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-08 14:59:56 +00:00
|
|
|
* @param string $prefix
|
2021-01-08 17:17:41 +00:00
|
|
|
* @covers ::compute
|
|
|
|
* @covers ::getLastPageAuthors
|
2021-01-08 14:59:56 +00:00
|
|
|
* @dataProvider providePrefix
|
2020-01-13 13:07:34 +00:00
|
|
|
*/
|
2021-01-08 14:59:56 +00:00
|
|
|
public function testRecentContributors( string $prefix ) {
|
|
|
|
$varName = "{$prefix}_recent_contributors";
|
|
|
|
$pageName = "Page to test $varName";
|
|
|
|
$title = Title::newFromText( $pageName );
|
2020-01-13 13:07:34 +00:00
|
|
|
|
2021-01-08 14:59:56 +00:00
|
|
|
$expected = $this->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' ],
|
|
|
|
];
|
2020-01-13 13:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-08 14:59:56 +00:00
|
|
|
* @param string $prefix
|
2021-01-08 17:17:41 +00:00
|
|
|
* @covers ::compute
|
2021-01-08 14:59:56 +00:00
|
|
|
* @dataProvider providePrefix
|
2020-01-13 13:07:34 +00:00
|
|
|
*/
|
2021-01-08 14:59:56 +00:00
|
|
|
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
|
|
|
|
);
|
|
|
|
$expected = $user->getName();
|
2020-01-13 13:07:34 +00:00
|
|
|
|
2021-01-08 14:59:56 +00:00
|
|
|
$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" );
|
2020-01-13 13:07:34 +00:00
|
|
|
}
|
|
|
|
}
|