mediawiki-extensions-Discus.../tests/phpunit/CommentModifierTest.php
Bartosz Dziewoński 4d7c98b97c Revert new reply API (again)
Causes page corruption, in a new way we haven't seen before.

* Revert "Move page updating logic to controller.js"
  This reverts commit 54fdc6de06.
* Revert "ReplyWidget: Move clear methods from #teardown to #clear"
  This reverts commit 9b811a94e0.
* Revert "ApiDiscussionToolsEdit: Do not pass 'basetimestamp'"
  This reverts commit 7de5938a6f.
* Revert "Use DOMCompat::getOuterHTML instead of doc->saveHTML()"
  This reverts commit 7b2448d2f0.
* Revert "CommentController: Remove remains of client-side edit conflict handling"
  This reverts commit 2d038af705.
* Revert "Restore error message for when comment is deleted while replying"
  This reverts commit 655c0526d6.
* Revert "Use transcluded from API to avoid ever fetching Parsoid DOM in client"
  This reverts commit 9d0fc184fe.
* Revert "Create a 'transcludedfrom' API endpoint"
  This reverts commit 5d8f3b9051.
* Revert "Edit API for replies"
  This reverts commit 8829a1a412.

Bug: T259855
Change-Id: I6419408c6194ec0afa6b8ee604b12c1a24c6ac7b
2020-08-13 20:19:29 +02:00

173 lines
4.6 KiB
PHP

<?php
namespace MediaWiki\Extension\DiscussionTools\Tests;
use MediaWiki\Extension\DiscussionTools\CommentModifier;
use Wikimedia\Parsoid\Utils\DOMCompat;
/**
* @coversDefaultClass \MediaWiki\Extension\DiscussionTools\CommentModifier
*
* @group DiscussionTools
*/
class CommentModifierTest extends CommentTestCase {
/**
* @dataProvider provideAddListItem
* @covers ::addListItem
*/
public function testAddListItem(
string $name, string $dom, string $expected, string $config, string $data
) : void {
$origPath = $dom;
$dom = self::getHtml( $dom );
$expectedPath = $expected;
$expected = self::getHtml( $expected );
$config = self::getJson( $config );
$data = self::getJson( $data );
$this->setupEnv( $config, $data );
$doc = self::createDocument( $dom );
$container = $doc->getElementsByTagName( 'body' )->item( 0 );
$parser = self::createParser( $container, $data );
$comments = $parser->getCommentItems();
$nodes = [];
foreach ( $comments as $comment ) {
$node = CommentModifier::addListItem( $comment );
$node->textContent = 'Reply to ' . $comment->getId();
$nodes[] = $node;
}
$expectedDoc = self::createDocument( $expected );
// Uncomment this to write updated content to the "modified HTML" files:
// self::overwriteHtmlFile( $expectedPath, $doc, $origPath );
self::assertEquals( $expectedDoc->saveHtml(), $doc->saveHtml(), $name );
// removeAddedListItem is not implemented on the server
}
public function provideAddListItem() : array {
return self::getJson( '../cases/modified.json' );
}
/**
* @dataProvider provideAddReplyLink
* @covers ::addReplyLink
*/
public function testAddReplyLink(
string $name, string $dom, string $expected, string $config, string $data
) : void {
$origPath = $dom;
$dom = self::getHtml( $dom );
$expectedPath = $expected;
$expected = self::getHtml( $expected );
$config = self::getJson( $config );
$data = self::getJson( $data );
$this->setupEnv( $config, $data );
$doc = self::createDocument( $dom );
$container = $doc->getElementsByTagName( 'body' )->item( 0 );
$parser = self::createParser( $container, $data );
$comments = $parser->getCommentItems();
foreach ( $comments as $comment ) {
$linkNode = $doc->createElement( 'a' );
$linkNode->nodeValue = 'Reply';
$linkNode->setAttribute( 'href', '#' );
CommentModifier::addReplyLink( $comment, $linkNode );
}
$expectedDoc = self::createDocument( $expected );
// Uncomment this to write updated content to the "reply HTML" files:
// self::overwriteHtmlFile( $expectedPath, $doc, $origPath );
self::assertEquals( $expectedDoc->saveHtml(), $doc->saveHtml(), $name );
}
public function provideAddReplyLink() : array {
return self::getJson( '../cases/reply.json' );
}
/**
* @dataProvider provideUnwrapList
* @covers ::unwrapList
*/
public function testUnwrapList( string $name, string $html, int $index, string $expected ) : void {
$doc = self::createDocument( '' );
$container = $doc->createElement( 'div' );
DOMCompat::setInnerHTML( $container, $html );
CommentModifier::unwrapList( $container->childNodes[$index] );
self::assertEquals( $expected, DOMCompat::getInnerHTML( $container ) );
}
public function provideUnwrapList() : array {
return self::getJson( '../cases/unwrap.json' );
}
/**
* @dataProvider provideIsWikitextSigned
* @covers ::isWikitextSigned
*/
public function testIsWikitextSigned(
string $msg, string $wikitext, bool $expected
) : void {
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 );
self::assertEquals(
$expected,
CommentModifier::isHtmlSigned( $container ),
$msg
);
}
public function provideIsHtmlSigned() : array {
return self::getJson( '../cases/isHtmlSigned.json' );
}
/**
* @dataProvider provideSanitizeWikitextLinebreaks
* @covers ::sanitizeWikitextLinebreaks
*/
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' );
}
}