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;
|
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-15 21:08:25 +00:00
|
|
|
* @coversDefaultClass \MediaWiki\Extension\DiscussionTools\CommentModifier
|
2020-05-14 22:44:49 +00:00
|
|
|
*
|
|
|
|
* @group DiscussionTools
|
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
|
|
|
|
* @covers ::addListItem
|
|
|
|
*/
|
2020-05-14 23:09:20 +00:00
|
|
|
public function testAddListItem(
|
|
|
|
string $name, 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;
|
2020-05-11 15:54:06 +00:00
|
|
|
$dom = self::getHtml( $dom );
|
2020-07-30 23:34:56 +00:00
|
|
|
$expectedPath = $expected;
|
2020-05-11 15:54:06 +00:00
|
|
|
$expected = self::getHtml( $expected );
|
|
|
|
$config = self::getJson( $config );
|
|
|
|
$data = self::getJson( $data );
|
|
|
|
|
|
|
|
$this->setupEnv( $config, $data );
|
|
|
|
|
|
|
|
$doc = self::createDocument( $dom );
|
2021-08-02 13:45:39 +00:00
|
|
|
$container = DOMCompat::getBody( $doc );
|
2020-05-11 15:54:06 +00:00
|
|
|
|
2020-07-20 21:15:03 +00:00
|
|
|
$parser = self::createParser( $container, $data );
|
|
|
|
$comments = $parser->getCommentItems();
|
2020-05-11 15:54:06 +00:00
|
|
|
|
|
|
|
$nodes = [];
|
|
|
|
foreach ( $comments as $comment ) {
|
2020-07-20 21:15:03 +00:00
|
|
|
$node = CommentModifier::addListItem( $comment );
|
|
|
|
$node->textContent = 'Reply to ' . $comment->getId();
|
|
|
|
$nodes[] = $node;
|
2020-05-11 15:54:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$expectedDoc = self::createDocument( $expected );
|
|
|
|
|
2020-10-19 20:51:43 +00:00
|
|
|
// Optionally write updated content to the "modified HTML" files
|
|
|
|
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
|
|
|
|
self::overwriteHtmlFile( $expectedPath, $doc, $origPath );
|
|
|
|
}
|
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
|
2020-05-11 15:54:06 +00:00
|
|
|
self::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
|
|
|
}
|
|
|
|
|
2021-07-22 07:25:13 +00:00
|
|
|
public function provideAddListItem(): array {
|
2020-05-25 18:54:20 +00:00
|
|
|
return self::getJson( '../cases/modified.json' );
|
2020-05-11 15:54:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideAddReplyLink
|
|
|
|
* @covers ::addReplyLink
|
|
|
|
*/
|
2020-05-14 23:09:20 +00:00
|
|
|
public function testAddReplyLink(
|
|
|
|
string $name, 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;
|
2020-05-11 15:54:06 +00:00
|
|
|
$dom = self::getHtml( $dom );
|
2020-07-30 23:34:56 +00:00
|
|
|
$expectedPath = $expected;
|
2020-05-11 15:54:06 +00:00
|
|
|
$expected = self::getHtml( $expected );
|
|
|
|
$config = self::getJson( $config );
|
|
|
|
$data = self::getJson( $data );
|
|
|
|
|
|
|
|
$this->setupEnv( $config, $data );
|
|
|
|
|
|
|
|
$doc = self::createDocument( $dom );
|
2021-08-02 13:45:39 +00:00
|
|
|
$container = DOMCompat::getBody( $doc );
|
2020-05-11 15:54:06 +00:00
|
|
|
|
2020-07-20 21:15:03 +00:00
|
|
|
$parser = self::createParser( $container, $data );
|
|
|
|
$comments = $parser->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
|
|
|
}
|
|
|
|
|
|
|
|
$expectedDoc = self::createDocument( $expected );
|
|
|
|
|
2020-10-19 20:51:43 +00:00
|
|
|
// Optionally write updated content to the "reply HTML" files
|
|
|
|
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
|
|
|
|
self::overwriteHtmlFile( $expectedPath, $doc, $origPath );
|
|
|
|
}
|
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
|
2020-05-11 15:54:06 +00:00
|
|
|
self::assertEquals( $expectedDoc->saveHtml(), $doc->saveHtml(), $name );
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:25:13 +00:00
|
|
|
public function provideAddReplyLink(): array {
|
2020-05-18 20:07:00 +00:00
|
|
|
return self::getJson( '../cases/reply.json' );
|
2020-05-11 15:54:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideUnwrapList
|
|
|
|
* @covers ::unwrapList
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function testUnwrapList( string $name, string $html, int $index, string $expected ): void {
|
2020-06-26 22:09:47 +00:00
|
|
|
$doc = self::createDocument( '' );
|
|
|
|
$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
|
|
|
|
2020-06-26 22:09:47 +00:00
|
|
|
self::assertEquals( $expected, DOMCompat::getInnerHTML( $container ) );
|
2020-05-11 15:54:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:25:13 +00:00
|
|
|
public function provideUnwrapList(): array {
|
2020-05-18 20:07:00 +00:00
|
|
|
return self::getJson( '../cases/unwrap.json' );
|
2020-05-11 15:54:06 +00:00
|
|
|
}
|
2020-06-26 22:24:14 +00:00
|
|
|
|
|
|
|
/**
|
2020-07-15 21:43:56 +00:00
|
|
|
* @dataProvider provideIsWikitextSigned
|
|
|
|
* @covers ::isWikitextSigned
|
2020-06-26 22:24:14 +00:00
|
|
|
*/
|
2020-07-15 21:43:56 +00:00
|
|
|
public function testIsWikitextSigned(
|
|
|
|
string $msg, string $wikitext, bool $expected
|
2021-07-22 07:25:13 +00:00
|
|
|
): void {
|
2020-07-15 21:43:56 +00:00
|
|
|
self::assertEquals(
|
|
|
|
$expected,
|
|
|
|
CommentModifier::isWikitextSigned( $wikitext ),
|
|
|
|
$msg
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:25:13 +00:00
|
|
|
public function provideIsWikitextSigned(): array {
|
2020-07-15 21:43:56 +00:00
|
|
|
return self::getJson( '../cases/isWikitextSigned.json' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideIsHtmlSigned
|
|
|
|
* @covers ::isHtmlSigned
|
|
|
|
*/
|
|
|
|
public function testIsHtmlSigned(
|
|
|
|
string $msg, string $html, bool $expected
|
2021-07-22 07:25:13 +00:00
|
|
|
): void {
|
2020-07-15 21:43:56 +00:00
|
|
|
$doc = self::createDocument( '' );
|
|
|
|
$container = $doc->createElement( 'div' );
|
|
|
|
DOMCompat::setInnerHTML( $container, $html );
|
2020-06-26 22:24:14 +00:00
|
|
|
|
|
|
|
self::assertEquals(
|
|
|
|
$expected,
|
2020-07-15 21:43:56 +00:00
|
|
|
CommentModifier::isHtmlSigned( $container ),
|
2020-06-26 22:24:14 +00:00
|
|
|
$msg
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:25:13 +00:00
|
|
|
public function provideIsHtmlSigned(): array {
|
2020-07-15 21:43:56 +00:00
|
|
|
return self::getJson( '../cases/isHtmlSigned.json' );
|
2020-06-26 22:24:14 +00:00
|
|
|
}
|
|
|
|
|
2021-03-06 18:07:56 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideAppendSignature
|
|
|
|
* @covers ::appendSignature
|
|
|
|
*/
|
|
|
|
public function testAppendSignature(
|
|
|
|
string $msg, string $html, string $expected
|
2021-07-22 07:25:13 +00:00
|
|
|
): void {
|
2021-03-06 18:07:56 +00:00
|
|
|
$doc = self::createDocument( '' );
|
|
|
|
$container = $doc->createElement( 'div' );
|
|
|
|
DOMCompat::setInnerHTML( $container, $html );
|
|
|
|
|
|
|
|
CommentModifier::appendSignature( $container );
|
|
|
|
|
|
|
|
self::assertEquals(
|
|
|
|
$expected,
|
|
|
|
XMLSerializer::serialize( $container, [ 'innerXML' => true, 'smartQuote' => false ] )['html'],
|
|
|
|
$msg
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:25:13 +00:00
|
|
|
public function provideAppendSignature(): array {
|
2021-03-06 18:07:56 +00:00
|
|
|
return self::getJson( '../cases/appendSignature.json' );
|
|
|
|
}
|
|
|
|
|
2020-06-26 22:24:14 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideSanitizeWikitextLinebreaks
|
2020-07-15 21:43:56 +00:00
|
|
|
* @covers ::sanitizeWikitextLinebreaks
|
2020-06-26 22:24:14 +00:00
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function testSanitizeWikitextLinebreaks( string $msg, string $wikitext, string $expected ): void {
|
2020-06-26 22:24:14 +00:00
|
|
|
self::assertEquals(
|
|
|
|
$expected,
|
|
|
|
CommentModifier::sanitizeWikitextLinebreaks( $wikitext ),
|
|
|
|
$msg
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:25:13 +00:00
|
|
|
public function provideSanitizeWikitextLinebreaks(): array {
|
2020-06-26 22:24:14 +00:00
|
|
|
return self::getJson( '../cases/sanitize-wikitext-linebreaks.json' );
|
|
|
|
}
|
2020-05-11 15:54:06 +00:00
|
|
|
}
|