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;
|
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
|
|
|
*/
|
2020-05-14 22:44:49 +00:00
|
|
|
class CommentModifierTest extends CommentTestCase {
|
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
|
|
|
|
) : 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 );
|
2020-07-15 16:21:23 +00:00
|
|
|
$container = $doc->getElementsByTagName( 'body' )->item( 0 );
|
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-07-30 23:34:56 +00:00
|
|
|
// Uncomment this to write updated content to the "modified HTML" files:
|
|
|
|
// self::overwriteHtmlFile( $expectedPath, $doc, $origPath );
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-14 23:09:20 +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
|
|
|
|
) : 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 );
|
2020-07-15 16:21:23 +00:00
|
|
|
$container = $doc->getElementsByTagName( 'body' )->item( 0 );
|
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-07-30 23:34:56 +00:00
|
|
|
// Uncomment this to write updated content to the "reply HTML" files:
|
|
|
|
// self::overwriteHtmlFile( $expectedPath, $doc, $origPath );
|
|
|
|
|
2020-05-11 15:54:06 +00:00
|
|
|
self::assertEquals( $expectedDoc->saveHtml(), $doc->saveHtml(), $name );
|
|
|
|
}
|
|
|
|
|
2020-05-14 23:09:20 +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
|
|
|
|
*/
|
2020-05-26 20:47:46 +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
|
|
|
}
|
|
|
|
|
2020-05-14 23:09:20 +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
|
2020-06-26 22:24:14 +00:00
|
|
|
) : void {
|
2020-07-15 21:43:56 +00:00
|
|
|
self::assertEquals(
|
|
|
|
$expected,
|
|
|
|
CommentModifier::isWikitextSigned( $wikitext ),
|
|
|
|
$msg
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideIsWikitextSigned() : array {
|
|
|
|
return self::getJson( '../cases/isWikitextSigned.json' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideIsHtmlSigned
|
|
|
|
* @covers ::isHtmlSigned
|
|
|
|
*/
|
|
|
|
public function testIsHtmlSigned(
|
|
|
|
string $msg, string $html, bool $expected
|
|
|
|
) : void {
|
|
|
|
$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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-15 21:43:56 +00:00
|
|
|
public function provideIsHtmlSigned() : array {
|
|
|
|
return self::getJson( '../cases/isHtmlSigned.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
|
|
|
*/
|
|
|
|
public function testSanitizeWikitextLinebreaks( string $msg, string $wikitext, string $expected ) : void {
|
|
|
|
self::assertEquals(
|
|
|
|
$expected,
|
|
|
|
CommentModifier::sanitizeWikitextLinebreaks( $wikitext ),
|
|
|
|
$msg
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideSanitizeWikitextLinebreaks() : array {
|
|
|
|
return self::getJson( '../cases/sanitize-wikitext-linebreaks.json' );
|
|
|
|
}
|
2020-05-11 15:54:06 +00:00
|
|
|
}
|