wiki.techinc.nl/tests/phpunit/includes/changes/OldChangesListTest.php
Timo Tijhof c02513c97e phpunit: Fix tests relying on implicit wgScript/wgArticlePath
A number of tests have hardcoded expections that pass only in WMF CI
where Quibble has LocalSettings.php with $wgScript and $wgArticlePath
set a certain way.

We could fix these by adding setMwGlobals() in their tests, as we
often do, but these are so often forgotten that I'd rather we just
add them to TestSetup.php so that it is simply impossible to write a
test that that passes locally for you (if you have the same config)
but not for someone else.

There is a larger project in there somewhere about expanding this
slowly such that we basically only pluck DB-settings and extension
enablement from LocalSettings and otherwise run the tests with the
default settings in PHPUnit. Pretty much by definition, any (other)
setting you have in LocalSettings is irrelevant because it either:
1. has no effect on the test (majority, harmless either way),
2. has a custom default via TestSetup.php (which has precedence over
   LocalSettings.php),
3. is relevant to the code being tested and the test case correctly
   calls setMwGlobals() to ensure a consistent value during test.
4. is relevant to the tested code but has no override, thus only
   passes if you happen to have the "right" value set for it
   (undesirable).

Case 4 is already categorically impossible for the most common config
settings that influence random code because we give them a value
in TestSetup.php. This patch expands that to include $wgScript
and $wgArticlePath. Perhaps in the future we can think about a way
to do this automatically by either re-applying MainConfigSchema
(sans db settings) or by only selectively applying LocalSettings.php
in the first place.

This patch follows-up I072ddf89562fe, which added a test case in
WikitextContentHandlerIntegrationTest.php that assumed "/index.php"
as the value of $wgScript. This passes in WMF CI since Quibble uses
that value, but the tests failed in most local development installs
since those tend to use "/w" instead.

Rather than one-off fixing that one test with overrideConfigValues(),
switch to a more general fixture, since the precise values don't
matter for this test.

Bug: T349087
Bug: T277470
Change-Id: If4304b7ca4a838bd892d4516a0b5c6dfbc30986e
2024-05-05 00:00:01 +00:00

237 lines
7.4 KiB
PHP

<?php
use MediaWiki\Context\RequestContext;
use MediaWiki\Title\Title;
/**
* @todo add tests to cover article link, timestamp, character difference,
* log entry, user tool links, direction marks, tags, rollback,
* watching users, and date header.
*
* @covers \OldChangesList
* @group Database
* @author Katie Filbert <aude.wiki@gmail.com>
*/
class OldChangesListTest extends MediaWikiLangTestCase {
/**
* @var TestRecentChangesHelper
*/
private $testRecentChangesHelper;
protected function setUp(): void {
parent::setUp();
$this->setUserLang( 'qqx' );
$this->testRecentChangesHelper = new TestRecentChangesHelper();
}
/**
* @dataProvider recentChangesLine_CssForLineNumberProvider
*/
public function testRecentChangesLine_CssForLineNumber( $expected, $linenumber, $message ) {
$oldChangesList = $this->getOldChangesList();
$recentChange = $this->getEditChange();
$line = $oldChangesList->recentChangesLine( $recentChange, false, $linenumber );
$this->assertMatchesRegularExpression( $expected, $line, $message );
}
public static function recentChangesLine_CssForLineNumberProvider() {
return [
[ '/mw-line-odd/', 1, 'odd line number' ],
[ '/mw-line-even/', 2, 'even line number' ]
];
}
public function testRecentChangesLine_NotWatchedCssClass() {
$oldChangesList = $this->getOldChangesList();
$recentChange = $this->getEditChange();
$line = $oldChangesList->recentChangesLine( $recentChange, false, 1 );
$this->assertMatchesRegularExpression( '/mw-changeslist-line-not-watched/', $line );
}
public function testRecentChangesLine_WatchedCssClass() {
$oldChangesList = $this->getOldChangesList();
$recentChange = $this->getEditChange();
$line = $oldChangesList->recentChangesLine( $recentChange, true, 1 );
$this->assertMatchesRegularExpression( '/mw-changeslist-line-watched/', $line );
}
public function testRecentChangesLine_LogTitle() {
$oldChangesList = $this->getOldChangesList();
$recentChange = $this->getLogChange( 'delete', 'delete' );
$line = $oldChangesList->recentChangesLine( $recentChange, false, 1 );
$this->assertMatchesRegularExpression( '/href="\/wiki\/Special:Log\/delete/', $line, 'link has href attribute' );
$this->assertMatchesRegularExpression( '/title="Special:Log\/delete/', $line, 'link has title attribute' );
$this->assertMatchesRegularExpression( "/dellogpage/", $line, 'link text' );
}
public function testRecentChangesLine_DiffHistLinks() {
$oldChangesList = $this->getOldChangesList();
$recentChange = $this->getEditChange();
$line = $oldChangesList->recentChangesLine( $recentChange, false, 1 );
$this->assertMatchesRegularExpression(
'/title=Cat&amp;curid=20131103212153&amp;diff=5&amp;oldid=191/',
$line,
'assert diff link'
);
$this->assertMatchesRegularExpression(
'/title=Cat&amp;curid=20131103212153&amp;action=history"/',
$line,
'assert history link'
);
}
public function testRecentChangesLine_Flags() {
$oldChangesList = $this->getOldChangesList();
$recentChange = $this->getNewBotEditChange();
$line = $oldChangesList->recentChangesLine( $recentChange, false, 1 );
$this->assertStringContainsString(
'<abbr class="newpage" title="(recentchanges-label-newpage)">(newpageletter)</abbr>',
$line,
'new page flag'
);
$this->assertStringContainsString(
'<abbr class="botedit" title="(recentchanges-label-bot)">(boteditletter)</abbr>',
$line,
'bot flag'
);
}
public function testRecentChangesLine_Attribs() {
$recentChange = $this->getEditChange();
$recentChange->mAttribs['ts_tags'] = 'vandalism,newbie';
$this->setTemporaryHook( 'OldChangesListRecentChangesLine', static function (
$oldChangesList, &$html, $rc, $classes, $attribs
) {
$html = $html . '/<div>Additional change line </div>/';
} );
$oldChangesList = $this->getOldChangesList();
$line = $oldChangesList->recentChangesLine( $recentChange, false, 1 );
$this->assertStringContainsString(
'/<div>Additional change line </div>/',
$line
);
$this->assertMatchesRegularExpression(
'/<li data-mw-revid="\d+" data-mw-ts="\d+" class="[\w\s-]*mw-tag-vandalism[\w\s-]*">/',
$line
);
$this->assertMatchesRegularExpression(
'/<li data-mw-revid="\d+" data-mw-ts="\d+" class="[\w\s-]*mw-tag-newbie[\w\s-]*">/',
$line
);
}
public function testRecentChangesLine_numberOfWatchingUsers() {
$oldChangesList = $this->getOldChangesList();
$recentChange = $this->getEditChange();
$recentChange->numberofWatchingusers = 100;
$line = $oldChangesList->recentChangesLine( $recentChange, false, 1 );
$this->assertMatchesRegularExpression( "/(number-of-watching-users-for-recent-changes: 100)/", $line );
}
public function testRecentChangesLine_watchlistCssClass() {
$oldChangesList = $this->getOldChangesList();
$oldChangesList->setWatchlistDivs( true );
$recentChange = $this->getEditChange();
$line = $oldChangesList->recentChangesLine( $recentChange, false, 1 );
$this->assertMatchesRegularExpression( "/watchlist-0-Cat/", $line );
}
public function testRecentChangesLine_dataAttribute() {
$oldChangesList = $this->getOldChangesList();
$oldChangesList->setWatchlistDivs( true );
$recentChange = $this->getEditChange();
$line = $oldChangesList->recentChangesLine( $recentChange, false, 1 );
$this->assertMatchesRegularExpression( '/data-target-page=\"Cat\"/', $line );
$recentChange = $this->getLogChange( 'delete', 'delete' );
$line = $oldChangesList->recentChangesLine( $recentChange, false, 1 );
$this->assertMatchesRegularExpression( '/data-target-page="Abc"/', $line );
}
public function testRecentChangesLine_prefix() {
$mockContext = $this->getMockBuilder( RequestContext::class )
->onlyMethods( [ 'getTitle' ] )
->getMock();
$mockContext->method( 'getTitle' )
->willReturn( Title::makeTitle( NS_MAIN, 'Expected Context Title' ) );
$oldChangesList = $this->getOldChangesList();
$oldChangesList->setContext( $mockContext );
$recentChange = $this->getEditChange();
$oldChangesList->setChangeLinePrefixer( function ( $rc, $changesList ) {
// Make sure RecentChange and ChangesList objects are the same
$this->assertEquals( 'Expected Context Title', $changesList->getContext()->getTitle() );
$this->assertEquals( 'Cat', $rc->getTitle() );
return 'I am a prefix';
} );
$line = $oldChangesList->recentChangesLine( $recentChange );
$this->assertMatchesRegularExpression( "/I am a prefix/", $line );
}
private function getNewBotEditChange() {
$user = $this->getMutableTestUser()->getUser();
$recentChange = $this->testRecentChangesHelper->makeNewBotEditRecentChange(
$user, 'Abc', '20131103212153', 5, 191, 190, 0, 0
);
return $recentChange;
}
private function getLogChange( $logType, $logAction ) {
$user = $this->getMutableTestUser()->getUser();
$recentChange = $this->testRecentChangesHelper->makeLogRecentChange(
$logType, $logAction, $user, 'Abc', '20131103212153', 0, 0
);
return $recentChange;
}
private function getEditChange() {
$user = $this->getMutableTestUser()->getUser();
$recentChange = $this->testRecentChangesHelper->makeEditRecentChange(
$user, 'Cat', '20131103212153', 5, 191, 190, 0, 0
);
return $recentChange;
}
private function getOldChangesList() {
$context = $this->getContext();
return new OldChangesList( $context );
}
private function getContext() {
$user = $this->getMutableTestUser()->getUser();
$context = $this->testRecentChangesHelper->getTestContext( $user );
$context->setLanguage( 'qqx' );
return $context;
}
}