2020-05-11 15:54:06 +00:00
|
|
|
<?php
|
|
|
|
|
2020-05-14 22:44:49 +00:00
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Tests;
|
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\CommentItem;
|
2020-05-14 22:44:49 +00:00
|
|
|
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-05-11 15:54:06 +00:00
|
|
|
$dom = self::getHtml( $dom );
|
|
|
|
$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
|
|
|
|
|
|
|
$parser = self::createParser( $data );
|
|
|
|
$comments = $parser->getComments( $container );
|
|
|
|
$parser->groupThreads( $comments );
|
|
|
|
|
|
|
|
$nodes = [];
|
|
|
|
foreach ( $comments as $comment ) {
|
2020-05-22 16:26:05 +00:00
|
|
|
if ( $comment instanceof CommentItem ) {
|
|
|
|
$node = CommentModifier::addListItem( $comment );
|
|
|
|
$node->textContent = 'Reply to ' . $comment->getId();
|
|
|
|
$nodes[] = $node;
|
2020-05-11 15:54:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$expectedDoc = self::createDocument( $expected );
|
|
|
|
|
|
|
|
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-05-11 15:54:06 +00:00
|
|
|
$dom = self::getHtml( $dom );
|
|
|
|
$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
|
|
|
|
|
|
|
$parser = self::createParser( $data );
|
|
|
|
$comments = $parser->getComments( $container );
|
|
|
|
$parser->groupThreads( $comments );
|
|
|
|
|
|
|
|
foreach ( $comments as $comment ) {
|
2020-05-22 16:26:05 +00:00
|
|
|
if ( $comment instanceof CommentItem ) {
|
|
|
|
$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 );
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideAutoSignWikitext
|
|
|
|
* @covers ::autoSignWikitext
|
|
|
|
*/
|
|
|
|
public function testAutoSignWikitext(
|
|
|
|
string $msg, string $wikitext, string $expected, ?string $prefix = null
|
|
|
|
) : void {
|
|
|
|
if ( $prefix !== null ) {
|
|
|
|
// TODO: Work out how to mock messages
|
|
|
|
self::assertTrue( true );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::assertEquals(
|
|
|
|
$expected,
|
|
|
|
CommentModifier::autoSignWikitext( $wikitext ),
|
|
|
|
$msg
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideAutoSignWikitext() : array {
|
|
|
|
return self::getJson( '../cases/auto-sign-wikitext.json' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideSanitizeWikitextLinebreaks
|
|
|
|
* @covers ::autoSignWikitext
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|