2020-10-19 20:59:55 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Tests;
|
|
|
|
|
2024-06-08 22:02:35 +00:00
|
|
|
use MediaWiki\Cache\GenderCache;
|
2024-06-21 22:16:26 +00:00
|
|
|
use MediaWiki\Context\IContextSource;
|
2024-10-19 21:39:18 +00:00
|
|
|
use MediaWiki\Json\FormatJson;
|
2023-06-02 07:36:59 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2023-09-28 11:00:36 +00:00
|
|
|
use MediaWiki\Output\OutputPage;
|
2024-06-08 22:02:35 +00:00
|
|
|
use MediaWiki\Parser\ParserOutput;
|
2023-08-19 18:16:15 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-10-13 07:17:39 +00:00
|
|
|
use MediaWiki\User\User;
|
2023-09-28 11:00:36 +00:00
|
|
|
use Skin;
|
2021-01-29 18:31:27 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
2020-10-19 20:59:55 +00:00
|
|
|
|
|
|
|
/**
|
2022-09-15 12:08:30 +00:00
|
|
|
* @covers \MediaWiki\Extension\DiscussionTools\CommentFormatter
|
2022-09-16 11:22:43 +00:00
|
|
|
* @covers \MediaWiki\Extension\DiscussionTools\CommentUtils
|
2020-10-19 20:59:55 +00:00
|
|
|
*/
|
2021-02-02 14:12:51 +00:00
|
|
|
class CommentFormatterTest extends IntegrationTestCase {
|
2020-10-19 20:59:55 +00:00
|
|
|
|
2023-12-04 12:45:57 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideIsLanguageRequiringReplyIcon
|
|
|
|
*/
|
|
|
|
public function testIsLanguageRequiringReplyIcon(
|
|
|
|
string $langCode, bool $expected, ?array $config = null
|
|
|
|
): void {
|
2024-07-03 08:44:55 +00:00
|
|
|
$lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( $langCode );
|
2023-12-04 12:45:57 +00:00
|
|
|
if ( $config ) {
|
|
|
|
$this->overrideConfigValues( [
|
|
|
|
'DiscussionTools_visualenhancements_reply_icon_languages' => $config
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
$actual = MockCommentFormatter::isLanguageRequiringReplyIcon( $lang );
|
|
|
|
static::assertEquals( $expected, $actual, $langCode );
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function provideIsLanguageRequiringReplyIcon(): array {
|
|
|
|
return [
|
|
|
|
[ 'zh', true ],
|
|
|
|
[ 'zh-hant', true ],
|
|
|
|
[ 'ar', true ],
|
|
|
|
[ 'arz', true ],
|
|
|
|
[ 'arz', false, [ 'ar' => true, 'arz' => false ] ],
|
|
|
|
[ 'en', false ],
|
|
|
|
[ 'he', false ],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-10-19 20:59:55 +00:00
|
|
|
/**
|
2021-04-19 18:34:55 +00:00
|
|
|
* @dataProvider provideAddDiscussionToolsInternal
|
2020-10-19 20:59:55 +00:00
|
|
|
*/
|
2021-04-19 18:34:55 +00:00
|
|
|
public function testAddDiscussionToolsInternal(
|
2024-02-20 00:57:43 +00:00
|
|
|
string $name, string $titleText, string $dom, string $expected, string $config, string $data,
|
|
|
|
bool $isMobile, bool $useButtons
|
2021-07-22 07:25:13 +00:00
|
|
|
): void {
|
2024-06-21 22:16:26 +00:00
|
|
|
$this->setService( 'GenderCache', $this->createNoOpMock( GenderCache::class ) );
|
2022-06-09 13:51:33 +00:00
|
|
|
$dom = static::getHtml( $dom );
|
2022-11-09 01:43:45 +00:00
|
|
|
$expectedPath = $expected;
|
|
|
|
$expected = static::getText( $expectedPath );
|
2022-06-09 13:51:33 +00:00
|
|
|
$config = static::getJson( $config );
|
|
|
|
$data = static::getJson( $data );
|
2020-10-19 20:59:55 +00:00
|
|
|
|
2023-06-02 07:36:59 +00:00
|
|
|
$this->overrideConfigValues( [
|
2022-03-11 23:42:25 +00:00
|
|
|
// Consistent defaults for generating canonical URLs
|
|
|
|
MainConfigNames::Server => 'https://example.org',
|
|
|
|
MainConfigNames::CanonicalServer => 'https://example.org',
|
2024-02-24 00:15:59 +00:00
|
|
|
MainConfigNames::ArticlePath => '/wiki/$1',
|
2023-06-02 07:36:59 +00:00
|
|
|
MainConfigNames::ScriptPath => '/w',
|
|
|
|
MainConfigNames::Script => '/w/index.php',
|
2022-12-08 16:15:02 +00:00
|
|
|
] );
|
2023-10-09 14:41:28 +00:00
|
|
|
|
2023-08-07 19:46:14 +00:00
|
|
|
$title = Title::newFromText( $titleText );
|
2023-10-09 14:41:28 +00:00
|
|
|
$subscriptionStore = new MockSubscriptionStore();
|
2023-10-13 07:17:39 +00:00
|
|
|
$user = $this->createMock( User::class );
|
2024-07-03 08:44:55 +00:00
|
|
|
$qqxLang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'qqx' );
|
2023-10-09 14:41:28 +00:00
|
|
|
$skin = $this->createMock( Skin::class );
|
2023-10-11 12:03:06 +00:00
|
|
|
$skin->method( 'getSkinName' )->willReturn( 'minerva' );
|
2024-06-21 22:16:26 +00:00
|
|
|
$outputPage = $this->createMock( IContextSource::class );
|
2023-10-09 14:41:28 +00:00
|
|
|
$outputPage->method( 'getTitle' )->willReturn( $title );
|
|
|
|
$outputPage->method( 'getUser' )->willReturn( $user );
|
|
|
|
$outputPage->method( 'getLanguage' )->willReturn( $qqxLang );
|
|
|
|
$outputPage->method( 'getSkin' )->willReturn( $skin );
|
|
|
|
$outputPage->method( 'msg' )->willReturn( 'a label' );
|
|
|
|
|
2022-03-11 23:42:25 +00:00
|
|
|
MockCommentFormatter::$parser = $this->createParser( $config, $data );
|
2021-01-29 18:31:27 +00:00
|
|
|
$commentFormatter = TestingAccessWrapper::newFromClass( MockCommentFormatter::class );
|
|
|
|
|
2022-08-12 17:13:48 +00:00
|
|
|
$pout = new ParserOutput();
|
2023-01-27 12:29:17 +00:00
|
|
|
$preprocessed = $commentFormatter->addDiscussionToolsInternal( $dom, $pout, $title );
|
2022-08-12 17:13:48 +00:00
|
|
|
$preprocessed .= "\n<pre>\n" .
|
2023-02-07 00:42:00 +00:00
|
|
|
"newestComment: " . FormatJson::encode(
|
|
|
|
$pout->getExtensionData( 'DiscussionTools-newestComment' ), "\t", FormatJson::ALL_OK ) . "\n" .
|
|
|
|
( $pout->getExtensionData( 'DiscussionTools-hasLedeContent' ) ?
|
|
|
|
"hasLedeContent\n" : '' ) .
|
|
|
|
( $pout->getExtensionData( 'DiscussionTools-hasCommentsInLedeContent' ) ?
|
|
|
|
"hasCommentsInLedeContent\n" : '' ) .
|
2024-08-22 15:32:32 +00:00
|
|
|
( $pout->getExtensionData( 'DiscussionTools-isEmptyTalkPage' ) ?
|
|
|
|
"isEmptyTalkPage\n" : '' ) .
|
2022-08-12 17:13:48 +00:00
|
|
|
FormatJson::encode( $pout->getJsConfigVars(), "\t", FormatJson::ALL_OK ) .
|
|
|
|
"\n</pre>";
|
2021-01-06 17:06:27 +00:00
|
|
|
|
2023-12-11 15:38:02 +00:00
|
|
|
OutputPage::setupOOUI();
|
2020-10-19 20:59:55 +00:00
|
|
|
|
2022-11-09 01:43:45 +00:00
|
|
|
$actual = $preprocessed;
|
2022-07-27 14:19:31 +00:00
|
|
|
|
2022-11-09 01:43:45 +00:00
|
|
|
$actual = MockCommentFormatter::postprocessTopicSubscription(
|
2024-02-20 00:57:43 +00:00
|
|
|
$actual, $outputPage, $subscriptionStore, $isMobile, $useButtons
|
2022-11-09 01:43:45 +00:00
|
|
|
);
|
2022-07-27 14:19:31 +00:00
|
|
|
|
2022-11-09 01:43:45 +00:00
|
|
|
$actual = MockCommentFormatter::postprocessVisualEnhancements(
|
2023-10-09 14:41:28 +00:00
|
|
|
$actual, $outputPage, $isMobile
|
2022-11-09 01:43:45 +00:00
|
|
|
);
|
2022-07-27 14:19:31 +00:00
|
|
|
|
2022-11-09 01:43:45 +00:00
|
|
|
$actual = MockCommentFormatter::postprocessReplyTool(
|
2024-02-20 00:57:43 +00:00
|
|
|
$actual, $outputPage, $isMobile, $useButtons
|
2022-11-09 01:43:45 +00:00
|
|
|
);
|
2022-07-27 14:19:31 +00:00
|
|
|
|
2022-11-09 01:43:45 +00:00
|
|
|
// OOUI ID's are non-deterministic, so strip them from test output
|
|
|
|
$actual = preg_replace( '/ id=[\'"]ooui-php-[0-9]+[\'"]/', '', $actual );
|
2022-07-27 14:19:31 +00:00
|
|
|
|
2022-11-09 01:43:45 +00:00
|
|
|
// Optionally write updated content to the "reply HTML" files
|
|
|
|
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
|
|
|
|
static::overwriteTextFile( $expectedPath, $actual );
|
2022-07-27 14:19:31 +00:00
|
|
|
}
|
2022-11-09 01:43:45 +00:00
|
|
|
|
|
|
|
static::assertEquals( $expected, $actual, $name );
|
2020-10-19 20:59:55 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 01:43:45 +00:00
|
|
|
/**
|
|
|
|
* @return iterable<array>
|
|
|
|
*/
|
2023-05-20 13:57:13 +00:00
|
|
|
public static function provideAddDiscussionToolsInternal() {
|
2022-11-09 01:43:45 +00:00
|
|
|
foreach ( static::getJson( '../cases/formatted.json' ) as $case ) {
|
|
|
|
// Run each test case twice, for desktop and mobile output
|
2024-02-20 00:57:43 +00:00
|
|
|
yield array_merge( $case,
|
|
|
|
[ 'expected' => $case['expected']['desktop'], 'isMobile' => false, 'useButtons' => true ] );
|
|
|
|
yield array_merge( $case,
|
|
|
|
[ 'expected' => $case['expected']['mobile'], 'isMobile' => true, 'useButtons' => true ] );
|
|
|
|
|
|
|
|
// Test the legacy output without visual enhancements (only available on desktop)
|
|
|
|
yield array_merge( $case,
|
|
|
|
[ 'expected' => $case['expected']['legacy'], 'isMobile' => false, 'useButtons' => false ] );
|
2022-11-09 01:43:45 +00:00
|
|
|
}
|
2020-10-19 20:59:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|