2020-05-11 15:54:06 +00:00
|
|
|
<?php
|
|
|
|
|
2020-05-14 22:44:49 +00:00
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Tests;
|
|
|
|
|
|
|
|
use MediaWiki\Extension\DiscussionTools\CommentModifier;
|
2020-06-26 22:09:47 +00:00
|
|
|
use Wikimedia\Parsoid\Utils\DOMCompat;
|
2022-01-29 20:20:51 +00:00
|
|
|
use Wikimedia\Parsoid\Utils\DOMUtils;
|
2021-03-06 18:07:56 +00:00
|
|
|
use Wikimedia\Parsoid\Wt2Html\XMLSerializer;
|
2020-05-14 22:44:49 +00:00
|
|
|
|
2020-05-11 15:54:06 +00:00
|
|
|
/**
|
2020-05-14 22:44:49 +00:00
|
|
|
* @group DiscussionTools
|
2022-09-15 12:08:30 +00:00
|
|
|
* @covers \MediaWiki\Extension\DiscussionTools\CommentModifier
|
2022-09-16 11:22:43 +00:00
|
|
|
* @covers \MediaWiki\Extension\DiscussionTools\CommentUtils
|
2020-05-11 15:54:06 +00:00
|
|
|
*/
|
2021-02-02 14:12:51 +00:00
|
|
|
class CommentModifierTest extends IntegrationTestCase {
|
2020-05-11 15:54:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideAddListItem
|
|
|
|
*/
|
2020-05-14 23:09:20 +00:00
|
|
|
public function testAddListItem(
|
2022-01-12 00:18:27 +00:00
|
|
|
string $name, string $title, string $dom, string $expected, string $config, string $data,
|
|
|
|
string $replyIndentation = 'invisible'
|
2021-07-22 07:25:13 +00:00
|
|
|
): void {
|
2020-07-30 23:34:56 +00:00
|
|
|
$origPath = $dom;
|
2022-06-09 13:51:33 +00:00
|
|
|
$dom = static::getHtml( $dom );
|
2020-07-30 23:34:56 +00:00
|
|
|
$expectedPath = $expected;
|
2022-06-09 13:51:33 +00:00
|
|
|
$expected = static::getHtml( $expected );
|
|
|
|
$config = static::getJson( $config );
|
|
|
|
$data = static::getJson( $data );
|
2020-05-11 15:54:06 +00:00
|
|
|
|
2022-03-11 23:42:25 +00:00
|
|
|
$title = $this->createTitleParser( $config )->parseTitle( $title );
|
2020-05-11 15:54:06 +00:00
|
|
|
|
2022-06-09 13:51:33 +00:00
|
|
|
$doc = static::createDocument( $dom );
|
|
|
|
$container = static::getThreadContainer( $doc );
|
2020-05-11 15:54:06 +00:00
|
|
|
|
2022-03-11 23:42:25 +00:00
|
|
|
$threadItemSet = $this->createParser( $config, $data )->parse( $container, $title );
|
2022-02-19 06:31:34 +00:00
|
|
|
$comments = $threadItemSet->getCommentItems();
|
2020-05-11 15:54:06 +00:00
|
|
|
|
|
|
|
foreach ( $comments as $comment ) {
|
2022-10-08 16:25:39 +00:00
|
|
|
if ( $comment->getTranscludedFrom() ) {
|
|
|
|
// Reply tool wouldn't be available for this comment on this page, because it's transcluded.
|
|
|
|
// Skip this case, because the result of addListItem() would be misleading (we add replies
|
|
|
|
// after the transclusion, which would be the wrong place for most of these comments).
|
|
|
|
continue;
|
|
|
|
}
|
2022-01-12 00:18:27 +00:00
|
|
|
$node = CommentModifier::addListItem( $comment, $replyIndentation );
|
2020-07-20 21:15:03 +00:00
|
|
|
$node->textContent = 'Reply to ' . $comment->getId();
|
2020-05-11 15:54:06 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 13:51:33 +00:00
|
|
|
$expectedDoc = static::createDocument( $expected );
|
2020-05-11 15:54:06 +00:00
|
|
|
|
2020-10-19 20:51:43 +00:00
|
|
|
// Optionally write updated content to the "modified HTML" files
|
|
|
|
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
|
2022-06-09 13:51:33 +00:00
|
|
|
static::overwriteHtmlFile( $expectedPath, $container, $origPath );
|
2020-10-19 20:51:43 +00:00
|
|
|
}
|
2020-07-30 23:34:56 +00:00
|
|
|
|
2020-08-19 20:03:41 +00:00
|
|
|
// saveHtml is not dirty-diff safe, but for testing it is probably faster than DOMCompat::getOuterHTML
|
2022-06-09 13:51:33 +00:00
|
|
|
static::assertEquals( $expectedDoc->saveHtml(), $doc->saveHtml(), $name );
|
2020-05-15 00:23:50 +00:00
|
|
|
|
|
|
|
// removeAddedListItem is not implemented on the server
|
2020-05-11 15:54:06 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 13:57:13 +00:00
|
|
|
public static function provideAddListItem(): array {
|
2022-06-09 13:51:33 +00:00
|
|
|
return static::getJson( '../cases/modified.json' );
|
2020-05-11 15:54:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideAddReplyLink
|
|
|
|
*/
|
2020-05-14 23:09:20 +00:00
|
|
|
public function testAddReplyLink(
|
2022-01-11 15:45:55 +00:00
|
|
|
string $name, string $title, string $dom, string $expected, string $config, string $data
|
2021-07-22 07:25:13 +00:00
|
|
|
): void {
|
2020-07-30 23:34:56 +00:00
|
|
|
$origPath = $dom;
|
2022-06-09 13:51:33 +00:00
|
|
|
$dom = static::getHtml( $dom );
|
2020-07-30 23:34:56 +00:00
|
|
|
$expectedPath = $expected;
|
2022-06-09 13:51:33 +00:00
|
|
|
$expected = static::getHtml( $expected );
|
|
|
|
$config = static::getJson( $config );
|
|
|
|
$data = static::getJson( $data );
|
2020-05-11 15:54:06 +00:00
|
|
|
|
2022-03-11 23:42:25 +00:00
|
|
|
$title = $this->createTitleParser( $config )->parseTitle( $title );
|
2020-05-11 15:54:06 +00:00
|
|
|
|
2022-06-09 13:51:33 +00:00
|
|
|
$doc = static::createDocument( $dom );
|
|
|
|
$container = static::getThreadContainer( $doc );
|
2020-05-11 15:54:06 +00:00
|
|
|
|
2022-03-11 23:42:25 +00:00
|
|
|
$threadItemSet = $this->createParser( $config, $data )->parse( $container, $title );
|
2022-02-19 06:31:34 +00:00
|
|
|
$comments = $threadItemSet->getCommentItems();
|
2020-05-11 15:54:06 +00:00
|
|
|
|
|
|
|
foreach ( $comments as $comment ) {
|
2020-07-20 21:15:03 +00:00
|
|
|
$linkNode = $doc->createElement( 'a' );
|
|
|
|
$linkNode->nodeValue = 'Reply';
|
|
|
|
$linkNode->setAttribute( 'href', '#' );
|
|
|
|
CommentModifier::addReplyLink( $comment, $linkNode );
|
2020-05-11 15:54:06 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 13:51:33 +00:00
|
|
|
$expectedDoc = static::createDocument( $expected );
|
2020-05-11 15:54:06 +00:00
|
|
|
|
2020-10-19 20:51:43 +00:00
|
|
|
// Optionally write updated content to the "reply HTML" files
|
|
|
|
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
|
2022-06-09 13:51:33 +00:00
|
|
|
static::overwriteHtmlFile( $expectedPath, $container, $origPath );
|
2020-10-19 20:51:43 +00:00
|
|
|
}
|
2020-07-30 23:34:56 +00:00
|
|
|
|
2020-08-19 20:03:41 +00:00
|
|
|
// saveHtml is not dirty-diff safe, but for testing it is probably faster than DOMCompat::getOuterHTML
|
2022-06-09 13:51:33 +00:00
|
|
|
static::assertEquals( $expectedDoc->saveHtml(), $doc->saveHtml(), $name );
|
2020-05-11 15:54:06 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 13:57:13 +00:00
|
|
|
public static function provideAddReplyLink(): array {
|
2022-06-09 13:51:33 +00:00
|
|
|
return static::getJson( '../cases/reply.json' );
|
2020-05-11 15:54:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideUnwrapList
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function testUnwrapList( string $name, string $html, int $index, string $expected ): void {
|
2022-06-09 13:51:33 +00:00
|
|
|
$doc = static::createDocument( '' );
|
2020-06-26 22:09:47 +00:00
|
|
|
$container = $doc->createElement( 'div' );
|
2020-05-11 15:54:06 +00:00
|
|
|
|
2020-06-26 22:09:47 +00:00
|
|
|
DOMCompat::setInnerHTML( $container, $html );
|
2020-05-26 20:47:46 +00:00
|
|
|
CommentModifier::unwrapList( $container->childNodes[$index] );
|
2020-05-11 15:54:06 +00:00
|
|
|
|
2022-06-09 13:51:33 +00:00
|
|
|
static::assertEquals( $expected, DOMCompat::getInnerHTML( $container ) );
|
2020-05-11 15:54:06 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 13:57:13 +00:00
|
|
|
public static function provideUnwrapList(): array {
|
2022-06-09 13:51:33 +00:00
|
|
|
return static::getJson( '../cases/unwrap.json' );
|
2020-05-11 15:54:06 +00:00
|
|
|
}
|
2020-06-26 22:24:14 +00:00
|
|
|
|
2024-06-20 15:22:03 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideUnwrapFragment
|
|
|
|
*/
|
|
|
|
public function testUnwrapFragment( string $html, string $expected ): void {
|
|
|
|
$doc = static::createDocument( '' );
|
|
|
|
$container = DOMUtils::parseHTMLToFragment( $doc, $html );
|
|
|
|
CommentModifier::unwrapFragment( $container );
|
|
|
|
static::assertEquals( $expected, DOMUtils::getFragmentInnerHTML( $container ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function provideUnwrapFragment() {
|
|
|
|
yield [
|
|
|
|
"<ul><li>aaa</li></ul>\n<dl><dd>bbb</dd></dl>",
|
|
|
|
"<p>aaa</p>\n<p>bbb</p>",
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-03-06 18:07:56 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideAppendSignature
|
|
|
|
*/
|
|
|
|
public function testAppendSignature(
|
|
|
|
string $msg, string $html, string $expected
|
2021-07-22 07:25:13 +00:00
|
|
|
): void {
|
2022-06-09 13:51:33 +00:00
|
|
|
$doc = static::createDocument( '' );
|
2022-01-29 20:20:51 +00:00
|
|
|
$container = DOMUtils::parseHTMLToFragment( $doc, $html );
|
2021-03-06 18:07:56 +00:00
|
|
|
|
2022-01-29 20:20:51 +00:00
|
|
|
CommentModifier::appendSignature( $container, ' ~~~~' );
|
2021-03-06 18:07:56 +00:00
|
|
|
|
2022-06-09 13:51:33 +00:00
|
|
|
static::assertEquals(
|
2021-03-06 18:07:56 +00:00
|
|
|
$expected,
|
|
|
|
XMLSerializer::serialize( $container, [ 'innerXML' => true, 'smartQuote' => false ] )['html'],
|
|
|
|
$msg
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-20 13:57:13 +00:00
|
|
|
public static function provideAppendSignature(): array {
|
2022-06-09 13:51:33 +00:00
|
|
|
return static::getJson( '../cases/appendSignature.json' );
|
2021-03-06 18:07:56 +00:00
|
|
|
}
|
|
|
|
|
2022-08-06 12:51:02 +00:00
|
|
|
public function testAppendSignatureWikitext(): void {
|
|
|
|
static::assertEquals(
|
|
|
|
'Foo bar ~~~~',
|
|
|
|
CommentModifier::appendSignatureWikitext( 'Foo bar', ' ~~~~' ),
|
|
|
|
'Simple message'
|
|
|
|
);
|
|
|
|
static::assertEquals(
|
|
|
|
"Foo bar\n*A\n*B\n~~~~",
|
|
|
|
CommentModifier::appendSignatureWikitext( "Foo bar\n*A\n*B", ' ~~~~' ),
|
|
|
|
'List'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-26 22:24:14 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideSanitizeWikitextLinebreaks
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function testSanitizeWikitextLinebreaks( string $msg, string $wikitext, string $expected ): void {
|
2022-06-09 13:51:33 +00:00
|
|
|
static::assertEquals(
|
2020-06-26 22:24:14 +00:00
|
|
|
$expected,
|
|
|
|
CommentModifier::sanitizeWikitextLinebreaks( $wikitext ),
|
|
|
|
$msg
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-20 13:57:13 +00:00
|
|
|
public static function provideSanitizeWikitextLinebreaks(): array {
|
2022-06-09 13:51:33 +00:00
|
|
|
return static::getJson( '../cases/sanitize-wikitext-linebreaks.json' );
|
2020-06-26 22:24:14 +00:00
|
|
|
}
|
2020-05-11 15:54:06 +00:00
|
|
|
}
|