mediawiki-extensions-Discus.../tests/phpunit/ContentThreadItemTest.php
Bartosz Dziewoński 880f9755e0 Separate ContentThreadItem and DatabaseThreadItem etc.
Rename ThreadItem to ContentThreadItem, then create a new ThreadItem
interface containing only the methods that we'll be able to implement
using only the persistently stored data (no parsing), then create a
DatabaseThreadItem. Do the same for CommentItem and HeadingItem.

ThreadItemSet gets a similar treatment, but it's basically only for
Phan's type checking. (This is sad.)

Change-Id: I1633049befe8ec169753b82eb876459af1f63fe8
2022-07-04 23:35:50 +02:00

196 lines
6.1 KiB
PHP

<?php
namespace MediaWiki\Extension\DiscussionTools\Tests;
use DateTimeImmutable;
use MediaWiki\Extension\DiscussionTools\CommentUtils;
use MediaWiki\Extension\DiscussionTools\ImmutableRange;
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentCommentItem;
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentHeadingItem;
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentThreadItem;
use MediaWiki\Extension\DiscussionTools\ThreadItem\ThreadItem;
use MediaWiki\MediaWikiServices;
/**
* @coversDefaultClass \MediaWiki\Extension\DiscussionTools\ThreadItem\ContentThreadItem
*
* @group DiscussionTools
*/
class ContentThreadItemTest extends IntegrationTestCase {
/**
* @dataProvider provideAuthors
* @covers ::getAuthorsBelow
* @covers ::getThreadItemsBelow
*/
public function testGetAuthorsOrThreadItemsBelow(
array $thread, array $expectedAuthors, array $expectedThreadItemIds
): void {
$doc = $this->createDocument( '' );
$node = $doc->createElement( 'div' );
$range = new ImmutableRange( $node, 0, $node, 0 );
$makeThreadItem = static function ( array $arr ) use ( &$makeThreadItem, $range ): ContentThreadItem {
if ( $arr['type'] === 'comment' ) {
$item = new ContentCommentItem( 1, $range, [], new DateTimeImmutable(), $arr['author'] );
} else {
$item = new ContentHeadingItem( $range, 2 );
}
$item->setId( $arr['id'] );
foreach ( $arr['replies'] as $reply ) {
$item->addReply( $makeThreadItem( $reply ) );
}
return $item;
};
$threadItem = $makeThreadItem( $thread );
static::assertEquals( $expectedAuthors, $threadItem->getAuthorsBelow() );
static::assertEquals( $expectedThreadItemIds, array_map( static function ( ThreadItem $threadItem ): string {
return $threadItem->getId();
}, $threadItem->getThreadItemsBelow() ) );
}
public function provideAuthors(): array {
return static::getJson( '../cases/authors.json' );
}
/**
* @dataProvider provideTranscludedFrom
* @covers ::getTranscludedFrom
* @covers \MediaWiki\Extension\DiscussionTools\CommentUtils::unwrapParsoidSections
*/
public function testGetTranscludedFrom(
string $name, string $title, string $dom, string $expected, string $config, string $data
): void {
$dom = static::getHtml( $dom );
$expectedPath = $expected;
$expected = static::getJson( $expected );
$config = static::getJson( $config );
$data = static::getJson( $data );
$this->setupEnv( $config, $data );
$title = MediaWikiServices::getInstance()->getTitleParser()->parseTitle( $title );
$doc = static::createDocument( $dom );
$container = static::getThreadContainer( $doc );
CommentUtils::unwrapParsoidSections( $container );
$threadItemSet = static::createParser( $data )->parse( $container, $title );
$comments = $threadItemSet->getCommentItems();
$transcludedFrom = [];
foreach ( $comments as $comment ) {
$transcludedFrom[ $comment->getId() ] = $comment->getTranscludedFrom();
}
// Optionally write updated content to the JSON files
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
static::overwriteJsonFile( $expectedPath, $transcludedFrom );
}
static::assertEquals(
$expected,
$transcludedFrom,
$name
);
}
public function provideTranscludedFrom(): array {
return static::getJson( '../cases/transcluded.json' );
}
/**
* @dataProvider provideGetText
* @covers ::getText
* @covers \MediaWiki\Extension\DiscussionTools\ThreadItem\ContentCommentItem::getBodyText
* @covers \MediaWiki\Extension\DiscussionTools\ImmutableRange::cloneContents
*/
public function testGetText(
string $name, string $title, string $dom, string $expected, string $config, string $data
): void {
$dom = static::getHtml( $dom );
$expectedPath = $expected;
$expected = static::getJson( $expected );
$config = static::getJson( $config );
$data = static::getJson( $data );
$doc = static::createDocument( $dom );
$container = static::getThreadContainer( $doc );
$this->setupEnv( $config, $data );
$title = MediaWikiServices::getInstance()->getTitleParser()->parseTitle( $title );
$threadItemSet = static::createParser( $data )->parse( $container, $title );
$items = $threadItemSet->getThreadItems();
$output = [];
foreach ( $items as $item ) {
$output[ $item->getId() ] = CommentUtils::htmlTrim(
$item instanceof ContentCommentItem ? $item->getBodyText( true ) : $item->getText()
);
}
// Optionally write updated content to the JSON files
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
static::overwriteJsonFile( $expectedPath, $output );
}
static::assertEquals(
$expected,
$output,
$name
);
}
public function provideGetText(): array {
return static::getJson( '../cases/getText.json' );
}
/**
* @dataProvider provideGetHTML
* @covers ::getHTML
* @covers \MediaWiki\Extension\DiscussionTools\ThreadItem\ContentCommentItem::getBodyHTML
* @covers \MediaWiki\Extension\DiscussionTools\ImmutableRange::cloneContents
*/
public function testGetHTML(
string $name, string $title, string $dom, string $expected, string $config, string $data
): void {
$dom = static::getHtml( $dom );
$expectedPath = $expected;
$expected = static::getJson( $expected );
$config = static::getJson( $config );
$data = static::getJson( $data );
$doc = static::createDocument( $dom );
$container = static::getThreadContainer( $doc );
$this->setupEnv( $config, $data );
$title = MediaWikiServices::getInstance()->getTitleParser()->parseTitle( $title );
$threadItemSet = static::createParser( $data )->parse( $container, $title );
$items = $threadItemSet->getThreadItems();
$output = [];
foreach ( $items as $item ) {
$output[ $item->getId() ] = CommentUtils::htmlTrim(
$item instanceof ContentCommentItem ? $item->getBodyHTML( true ) : $item->getHTML()
);
}
// Optionally write updated content to the JSON files
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
static::overwriteJsonFile( $expectedPath, $output );
}
static::assertEquals(
$expected,
$output,
$name
);
}
public function provideGetHTML(): array {
return static::getJson( '../cases/getHTML.json' );
}
}