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
240 lines
7.2 KiB
PHP
240 lines
7.2 KiB
PHP
<?php
|
|
|
|
use MediaWiki\Linker\LinkRenderer;
|
|
use MediaWiki\Title\Title;
|
|
|
|
/**
|
|
* @covers \RCCacheEntryFactory
|
|
* @group Database
|
|
* @author Katie Filbert <aude.wiki@gmail.com>
|
|
*/
|
|
class RCCacheEntryFactoryTest extends MediaWikiLangTestCase {
|
|
|
|
/**
|
|
* @var TestRecentChangesHelper
|
|
*/
|
|
private $testRecentChangesHelper;
|
|
|
|
/**
|
|
* @var LinkRenderer
|
|
*/
|
|
private $linkRenderer;
|
|
|
|
protected function setUp(): void {
|
|
parent::setUp();
|
|
|
|
$this->linkRenderer = $this->getServiceContainer()->getLinkRenderer();
|
|
$this->testRecentChangesHelper = new TestRecentChangesHelper();
|
|
}
|
|
|
|
public function testNewFromRecentChange() {
|
|
$user = $this->getMutableTestUser()->getUser();
|
|
$recentChange = $this->testRecentChangesHelper->makeEditRecentChange(
|
|
$user,
|
|
'Xyz',
|
|
5, // curid
|
|
191, // thisid
|
|
190, // lastid
|
|
'20131103212153',
|
|
0, // counter
|
|
0 // number of watching users
|
|
);
|
|
$cacheEntryFactory = new RCCacheEntryFactory(
|
|
$this->getContext(),
|
|
$this->getMessages(),
|
|
$this->linkRenderer
|
|
);
|
|
$cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, false );
|
|
|
|
$this->assertInstanceOf( RCCacheEntry::class, $cacheEntry );
|
|
|
|
$this->assertFalse( $cacheEntry->watched, 'watched' );
|
|
$this->assertEquals( '21:21', $cacheEntry->timestamp, 'timestamp' );
|
|
$this->assertSame( 0, $cacheEntry->numberofWatchingusers, 'watching users' );
|
|
$this->assertFalse( $cacheEntry->unpatrolled, 'unpatrolled' );
|
|
|
|
$this->assertUserLinks( $user->getName(), $cacheEntry );
|
|
$this->assertTitleLink( 'Xyz', $cacheEntry );
|
|
|
|
$diff = [ 'curid' => 5, 'diff' => 191, 'oldid' => 190 ];
|
|
$cur = [ 'curid' => 5, 'diff' => 0, 'oldid' => 191 ];
|
|
$this->assertQueryLink( 'cur', $cur, $cacheEntry->curlink );
|
|
$this->assertQueryLink( 'prev', $diff, $cacheEntry->lastlink );
|
|
$this->assertQueryLink( 'diff', $diff, $cacheEntry->difflink );
|
|
}
|
|
|
|
public function testNewForDeleteChange() {
|
|
$user = $this->getMutableTestUser()->getUser();
|
|
$recentChange = $this->testRecentChangesHelper->makeLogRecentChange(
|
|
'delete',
|
|
'delete',
|
|
$user,
|
|
'Abc',
|
|
'20131103212153',
|
|
0, // counter
|
|
0 // number of watching users
|
|
);
|
|
$cacheEntryFactory = new RCCacheEntryFactory(
|
|
$this->getContext(),
|
|
$this->getMessages(),
|
|
$this->linkRenderer
|
|
);
|
|
$cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, false );
|
|
|
|
$this->assertInstanceOf( RCCacheEntry::class, $cacheEntry );
|
|
|
|
$this->assertFalse( $cacheEntry->watched, 'watched' );
|
|
$this->assertEquals( '21:21', $cacheEntry->timestamp, 'timestamp' );
|
|
$this->assertSame( 0, $cacheEntry->numberofWatchingusers, 'watching users' );
|
|
$this->assertFalse( $cacheEntry->unpatrolled, 'unpatrolled' );
|
|
|
|
$this->assertDeleteLogLink( $cacheEntry );
|
|
$this->assertUserLinks( $user->getName(), $cacheEntry );
|
|
|
|
$this->assertEquals( 'cur', $cacheEntry->curlink, 'cur link for delete log or rev' );
|
|
$this->assertEquals( 'diff', $cacheEntry->difflink, 'diff link for delete log or rev' );
|
|
$this->assertEquals( 'prev', $cacheEntry->lastlink, 'pref link for delete log or rev' );
|
|
}
|
|
|
|
public function testNewForRevUserDeleteChange() {
|
|
$user = $this->getMutableTestUser()->getUser();
|
|
$recentChange = $this->testRecentChangesHelper->makeDeletedEditRecentChange(
|
|
$user,
|
|
'Zzz',
|
|
'20131103212153',
|
|
191, // thisid
|
|
190, // lastid
|
|
'20131103212153',
|
|
0, // counter
|
|
0 // number of watching users
|
|
);
|
|
$cacheEntryFactory = new RCCacheEntryFactory(
|
|
$this->getContext(),
|
|
$this->getMessages(),
|
|
$this->linkRenderer
|
|
);
|
|
$cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, false );
|
|
|
|
$this->assertInstanceOf( RCCacheEntry::class, $cacheEntry );
|
|
|
|
$this->assertFalse( $cacheEntry->watched, 'watched' );
|
|
$this->assertEquals( '21:21', $cacheEntry->timestamp, 'timestamp' );
|
|
$this->assertSame( 0, $cacheEntry->numberofWatchingusers, 'watching users' );
|
|
$this->assertFalse( $cacheEntry->unpatrolled, 'unpatrolled' );
|
|
|
|
$this->assertRevDel( $cacheEntry );
|
|
$this->assertTitleLink( 'Zzz', $cacheEntry );
|
|
|
|
$this->assertEquals( 'cur', $cacheEntry->curlink, 'cur link for delete log or rev' );
|
|
$this->assertEquals( 'diff', $cacheEntry->difflink, 'diff link for delete log or rev' );
|
|
$this->assertEquals( 'prev', $cacheEntry->lastlink, 'pref link for delete log or rev' );
|
|
}
|
|
|
|
private function assertValidHTML( $actual ) {
|
|
$this->assertNotSame( '', $actual );
|
|
$document = new DOMDocument;
|
|
|
|
$oldUseInternalErrors = libxml_use_internal_errors( true );
|
|
|
|
try {
|
|
$loaded = $document->loadHTML( $actual );
|
|
$message = '';
|
|
foreach ( libxml_get_errors() as $error ) {
|
|
$message .= "\n" . $error->message;
|
|
}
|
|
|
|
$this->assertNotFalse( $loaded, $message ?: 'Invalid for unknown reason' );
|
|
} finally {
|
|
libxml_use_internal_errors( $oldUseInternalErrors );
|
|
}
|
|
}
|
|
|
|
private function assertUserLinks( $user, $cacheEntry ) {
|
|
$this->assertValidHTML( $cacheEntry->userlink );
|
|
$this->assertMatchesRegularExpression(
|
|
'#^<a .*class="new mw-userlink".*><bdi>' . $user . '</bdi></a>#',
|
|
$cacheEntry->userlink,
|
|
'verify user link'
|
|
);
|
|
|
|
$this->assertValidHTML( $cacheEntry->usertalklink );
|
|
$this->assertMatchesRegularExpression(
|
|
'#^ <span class="mw-usertoollinks mw-changeslist-links">.*<span><a .+>talk</a></span>.*</span>#',
|
|
$cacheEntry->usertalklink,
|
|
'verify user talk link'
|
|
);
|
|
|
|
$this->assertValidHTML( $cacheEntry->usertalklink );
|
|
$this->assertMatchesRegularExpression(
|
|
'#^ <span class="mw-usertoollinks mw-changeslist-links">.*<span><a .+>' .
|
|
'contribs</a></span>.*</span>$#',
|
|
$cacheEntry->usertalklink,
|
|
'verify user tool links'
|
|
);
|
|
}
|
|
|
|
private function assertDeleteLogLink( $cacheEntry ) {
|
|
$this->assertEquals(
|
|
'(<a href="/wiki/Special:Log/delete" title="Special:Log/delete">Deletion log</a>)',
|
|
$cacheEntry->link,
|
|
'verify deletion log link'
|
|
);
|
|
|
|
$this->assertValidHTML( $cacheEntry->link );
|
|
}
|
|
|
|
private function assertRevDel( $cacheEntry ) {
|
|
$this->assertEquals(
|
|
' <span class="history-deleted">(username removed)</span>',
|
|
$cacheEntry->userlink,
|
|
'verify user link for change with deleted revision and user'
|
|
);
|
|
$this->assertValidHTML( $cacheEntry->userlink );
|
|
}
|
|
|
|
private function assertTitleLink( $title, $cacheEntry ) {
|
|
$this->assertEquals(
|
|
'<a href="/wiki/' . $title . '" title="' . $title . '">' . $title . '</a>',
|
|
$cacheEntry->link,
|
|
'verify title link'
|
|
);
|
|
$this->assertValidHTML( $cacheEntry->link );
|
|
}
|
|
|
|
private function assertQueryLink( $content, $params, $link ) {
|
|
$this->assertMatchesRegularExpression(
|
|
"#^<a .+>$content</a>$#",
|
|
$link,
|
|
'verify query link element'
|
|
);
|
|
$this->assertValidHTML( $link );
|
|
|
|
foreach ( $params as $key => $value ) {
|
|
$this->assertMatchesRegularExpression( '/' . $key . '=' . $value . '/', $link, "verify $key link params" );
|
|
}
|
|
}
|
|
|
|
private function getMessages() {
|
|
return [
|
|
'cur' => 'cur',
|
|
'diff' => 'diff',
|
|
'hist' => 'hist',
|
|
'enhancedrc-history' => 'history',
|
|
'last' => 'prev',
|
|
'blocklink' => 'block',
|
|
'history' => 'Page history',
|
|
'semicolon-separator' => '; ',
|
|
'pipe-separator' => ' | '
|
|
];
|
|
}
|
|
|
|
private function getContext() {
|
|
$user = $this->getMutableTestUser()->getUser();
|
|
$context = $this->testRecentChangesHelper->getTestContext( $user );
|
|
|
|
$title = Title::makeTitle( NS_SPECIAL, 'RecentChanges' );
|
|
$context->setTitle( $title );
|
|
|
|
return $context;
|
|
}
|
|
}
|