2020-10-19 20:59:55 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Tests;
|
|
|
|
|
2022-08-12 17:13:48 +00:00
|
|
|
use FormatJson;
|
2023-08-07 19:46:14 +00:00
|
|
|
use GenderCache;
|
2023-06-02 07:36:59 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2021-06-02 19:42:45 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2023-08-19 18:16:15 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-08-07 19:46:14 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2022-08-12 17:13:48 +00:00
|
|
|
use ParserOutput;
|
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
|
|
|
|
|
|
|
/**
|
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(
|
2023-08-07 19:46:14 +00:00
|
|
|
string $name, string $titleText, string $dom, string $expected, string $config, string $data, bool $isMobile
|
2021-07-22 07:25:13 +00:00
|
|
|
): void {
|
2023-08-07 19:46:14 +00:00
|
|
|
$this->setService( 'GenderCache', $this->createMock( 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
|
|
|
|
|
|
|
$this->setupEnv( $config, $data );
|
2023-06-02 07:36:59 +00:00
|
|
|
$this->overrideConfigValues( [
|
|
|
|
MainConfigNames::ScriptPath => '/w',
|
|
|
|
MainConfigNames::Script => '/w/index.php',
|
2022-12-08 16:15:02 +00:00
|
|
|
] );
|
2023-08-07 19:46:14 +00:00
|
|
|
$title = Title::newFromText( $titleText );
|
2022-11-10 22:03:06 +00:00
|
|
|
MockCommentFormatter::$parser = static::createParser( $data );
|
2020-10-19 20:59:55 +00:00
|
|
|
|
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" : '' ) .
|
|
|
|
( $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
|
|
|
|
2021-04-19 19:42:53 +00:00
|
|
|
$mockSubStore = new MockSubscriptionStore();
|
2021-06-02 19:42:45 +00:00
|
|
|
$qqxLang = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'qqx' );
|
|
|
|
|
2021-01-28 17:19:52 +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(
|
2023-08-07 19:46:14 +00:00
|
|
|
$actual, $qqxLang, $title, $mockSubStore, $this->createMock( UserIdentity::class ), $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::postprocessVisualEnhancements(
|
2023-08-07 19:46:14 +00:00
|
|
|
$actual, $qqxLang, $this->createMock( UserIdentity::class ), $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(
|
|
|
|
$actual, $qqxLang, $isMobile
|
|
|
|
);
|
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
|
|
|
|
yield array_merge( $case, [ 'expected' => $case['expected']['desktop'], 'isMobile' => false ] );
|
|
|
|
yield array_merge( $case, [ 'expected' => $case['expected']['mobile'], 'isMobile' => true ] );
|
|
|
|
}
|
2020-10-19 20:59:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|