2020-10-19 20:59:55 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Tests;
|
|
|
|
|
2022-08-12 17:13:48 +00:00
|
|
|
use FormatJson;
|
2021-06-02 19:42:45 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2022-08-12 17:13:48 +00:00
|
|
|
use ParserOutput;
|
2022-01-11 15:50:44 +00:00
|
|
|
use Title;
|
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(
|
2022-11-09 01:43:45 +00:00
|
|
|
string $name, string $title, string $dom, string $expected, string $config, string $data, bool $isMobile
|
2021-07-22 07:25:13 +00:00
|
|
|
): void {
|
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 );
|
2022-03-04 17:13:21 +00:00
|
|
|
$title = Title::newFromText( $title );
|
Change CommentParser into a service
Goal:
-----
To have a method like CommentParser::parse(), which just takes a node
to parse and a title and returns plain data, so that we don't need to
keep track of the config to construct a CommentParser object (the
required config like content language is provided by services) and
we don't need to keep that object around after parsing.
Changes:
--------
CommentParser.php:
* …is now a service. Constructor only takes services as arguments.
The node and title are passed to a new parse() method.
* parse() should return plain data, but I split this part to a separate
patch for ease of review: I49bfe019aa460651447fd383f73eafa9d7180a92.
* CommentParser still cheats and accesses global state in a few places,
e.g. calling Title::makeTitleSafe or CommentUtils::getTitleFromUrl,
so we can't turn its tests into true unit tests. This work is left
for future commits.
LanguageData.php:
* …is now a service, instead of a static class.
Parser.js:
* …is not a real service, but it's changed to behave in a similar way.
Constructor takes only the required config as argument,
and node and title are instead passed to a new parse() method.
CommentParserTest.php:
parser.test.js:
* Can be simplified, now that we don't need a useless node and title
to test internal methods that don't use them.
testUtils.js:
* Can be simplified, now that we don't need to override internal
ResourceLoader stuff just to change the parser config.
Change-Id: Iadb7757debe000025e52770ca51ebcf24ca8ee66
2022-02-19 02:43:21 +00:00
|
|
|
MockCommentFormatter::$parser = TestUtils::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();
|
|
|
|
[ 'html' => $preprocessed ] = $commentFormatter->addDiscussionToolsInternal( $dom, $pout, $title );
|
|
|
|
$preprocessed .= "\n<pre>\n" .
|
|
|
|
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(
|
|
|
|
$actual, $qqxLang, $mockSubStore, static::getTestUser()->getUser(), $isMobile
|
|
|
|
);
|
2022-07-27 14:19:31 +00:00
|
|
|
|
2022-11-09 01:43:45 +00:00
|
|
|
$actual = MockCommentFormatter::postprocessVisualEnhancements(
|
|
|
|
$actual, $qqxLang, static::getTestUser()->getUser(), $isMobile
|
|
|
|
);
|
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>
|
|
|
|
*/
|
|
|
|
public function provideAddDiscussionToolsInternal() {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|