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-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 );
|
|
|
|
$container = $doc->documentElement->childNodes[0];
|
|
|
|
|
|
|
|
$parser = self::createParser( $data );
|
|
|
|
$comments = $parser->getComments( $container );
|
|
|
|
$parser->groupThreads( $comments );
|
|
|
|
|
|
|
|
$nodes = [];
|
|
|
|
foreach ( $comments as $comment ) {
|
|
|
|
if ( $comment->type === 'heading' ) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-05-14 22:44:49 +00:00
|
|
|
$node = CommentModifier::addListItem( $comment );
|
2020-05-11 15:54:06 +00:00
|
|
|
$node->textContent = 'Reply to ' . $comment->id;
|
|
|
|
$nodes[] = $node;
|
|
|
|
}
|
|
|
|
|
|
|
|
$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 );
|
|
|
|
$container = $doc->documentElement->childNodes[0];
|
|
|
|
|
|
|
|
$parser = self::createParser( $data );
|
|
|
|
$comments = $parser->getComments( $container );
|
|
|
|
$parser->groupThreads( $comments );
|
|
|
|
|
|
|
|
foreach ( $comments as $comment ) {
|
|
|
|
if ( $comment->type === 'heading' ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$linkNode = $doc->createElement( 'a' );
|
|
|
|
$linkNode->nodeValue = 'Reply';
|
|
|
|
$linkNode->setAttribute( 'href', '#' );
|
2020-05-14 22:44:49 +00:00
|
|
|
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-05-11 15:54:06 +00:00
|
|
|
$doc = self::createDocument( '<div>' . $html . '</div>' );
|
|
|
|
$expectedDoc = self::createDocument( '<div>' . $expected . '</div>' );
|
2020-05-26 20:47:46 +00:00
|
|
|
$container = $doc->documentElement->childNodes[0]->childNodes[0];
|
2020-05-11 15:54:06 +00:00
|
|
|
|
2020-05-26 20:47:46 +00:00
|
|
|
CommentModifier::unwrapList( $container->childNodes[$index] );
|
2020-05-11 15:54:06 +00:00
|
|
|
|
|
|
|
self::assertEquals( $expectedDoc->documentElement, $doc->documentElement );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|