2020-07-20 14:13:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Tests;
|
|
|
|
|
2021-11-08 17:47:03 +00:00
|
|
|
use DateTimeImmutable;
|
2020-07-20 14:48:41 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\CommentUtils;
|
2020-07-20 14:13:59 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\ImmutableRange;
|
2022-03-18 03:28:06 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentCommentItem;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentHeadingItem;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentThreadItem;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem\ThreadItem;
|
2022-02-21 22:07:38 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2020-07-20 14:13:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DiscussionTools
|
2022-09-15 12:08:30 +00:00
|
|
|
* @covers \MediaWiki\Extension\DiscussionTools\ThreadItem\ContentThreadItem
|
|
|
|
* @covers \MediaWiki\Extension\DiscussionTools\ThreadItem\ContentCommentItem
|
|
|
|
* @covers \MediaWiki\Extension\DiscussionTools\CommentUtils
|
|
|
|
* @covers \MediaWiki\Extension\DiscussionTools\ImmutableRange
|
2020-07-20 14:13:59 +00:00
|
|
|
*/
|
2022-03-18 03:28:06 +00:00
|
|
|
class ContentThreadItemTest extends IntegrationTestCase {
|
2020-07-20 14:13:59 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideAuthors
|
|
|
|
*/
|
2022-02-21 00:22:39 +00:00
|
|
|
public function testGetAuthorsOrThreadItemsBelow(
|
|
|
|
array $thread, array $expectedAuthors, array $expectedThreadItemIds
|
|
|
|
): void {
|
2020-07-20 14:13:59 +00:00
|
|
|
$doc = $this->createDocument( '' );
|
|
|
|
$node = $doc->createElement( 'div' );
|
|
|
|
$range = new ImmutableRange( $node, 0, $node, 0 );
|
|
|
|
|
2022-03-18 03:28:06 +00:00
|
|
|
$makeThreadItem = static function ( array $arr ) use ( &$makeThreadItem, $range ): ContentThreadItem {
|
2020-07-20 14:13:59 +00:00
|
|
|
if ( $arr['type'] === 'comment' ) {
|
2022-03-18 03:28:06 +00:00
|
|
|
$item = new ContentCommentItem( 1, $range, [], new DateTimeImmutable(), $arr['author'] );
|
2020-07-20 14:13:59 +00:00
|
|
|
} else {
|
2022-03-18 03:28:06 +00:00
|
|
|
$item = new ContentHeadingItem( $range, 2 );
|
2020-07-20 14:13:59 +00:00
|
|
|
}
|
2022-02-21 00:22:39 +00:00
|
|
|
$item->setId( $arr['id'] );
|
2020-07-20 14:13:59 +00:00
|
|
|
foreach ( $arr['replies'] as $reply ) {
|
|
|
|
$item->addReply( $makeThreadItem( $reply ) );
|
|
|
|
}
|
|
|
|
return $item;
|
|
|
|
};
|
|
|
|
|
|
|
|
$threadItem = $makeThreadItem( $thread );
|
|
|
|
|
2022-06-09 13:51:33 +00:00
|
|
|
static::assertEquals( $expectedAuthors, $threadItem->getAuthorsBelow() );
|
|
|
|
static::assertEquals( $expectedThreadItemIds, array_map( static function ( ThreadItem $threadItem ): string {
|
2022-02-21 00:22:39 +00:00
|
|
|
return $threadItem->getId();
|
|
|
|
}, $threadItem->getThreadItemsBelow() ) );
|
2020-07-20 14:13:59 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:25:13 +00:00
|
|
|
public function provideAuthors(): array {
|
2022-06-09 13:51:33 +00:00
|
|
|
return static::getJson( '../cases/authors.json' );
|
2020-07-20 14:13:59 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 14:48:41 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideTranscludedFrom
|
|
|
|
*/
|
|
|
|
public function testGetTranscludedFrom(
|
2022-01-11 15:45:55 +00:00
|
|
|
string $name, string $title, string $dom, string $expected, string $config, string $data
|
2021-07-22 07:25:13 +00:00
|
|
|
): void {
|
2022-06-09 13:51:33 +00:00
|
|
|
$dom = static::getHtml( $dom );
|
2020-07-30 23:34:56 +00:00
|
|
|
$expectedPath = $expected;
|
2022-06-09 13:51:33 +00:00
|
|
|
$expected = static::getJson( $expected );
|
|
|
|
$config = static::getJson( $config );
|
|
|
|
$data = static::getJson( $data );
|
2020-07-20 14:48:41 +00:00
|
|
|
|
|
|
|
$this->setupEnv( $config, $data );
|
2022-02-21 22:07:38 +00:00
|
|
|
$title = MediaWikiServices::getInstance()->getTitleParser()->parseTitle( $title );
|
2020-07-20 14:48:41 +00:00
|
|
|
|
2022-06-09 13:51:33 +00:00
|
|
|
$doc = static::createDocument( $dom );
|
|
|
|
$container = static::getThreadContainer( $doc );
|
2020-07-20 14:48:41 +00:00
|
|
|
|
|
|
|
CommentUtils::unwrapParsoidSections( $container );
|
|
|
|
|
2022-06-09 13:51:33 +00:00
|
|
|
$threadItemSet = static::createParser( $data )->parse( $container, $title );
|
2022-02-19 06:31:34 +00:00
|
|
|
$comments = $threadItemSet->getCommentItems();
|
2020-07-20 14:48:41 +00:00
|
|
|
|
|
|
|
$transcludedFrom = [];
|
|
|
|
foreach ( $comments as $comment ) {
|
2020-07-20 21:15:03 +00:00
|
|
|
$transcludedFrom[ $comment->getId() ] = $comment->getTranscludedFrom();
|
2020-07-20 14:48:41 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 20:51:43 +00:00
|
|
|
// Optionally write updated content to the JSON files
|
|
|
|
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
|
2022-06-09 13:51:33 +00:00
|
|
|
static::overwriteJsonFile( $expectedPath, $transcludedFrom );
|
2020-10-19 20:51:43 +00:00
|
|
|
}
|
2020-08-10 21:31:49 +00:00
|
|
|
|
2022-06-09 13:51:33 +00:00
|
|
|
static::assertEquals(
|
2020-07-20 14:48:41 +00:00
|
|
|
$expected,
|
|
|
|
$transcludedFrom,
|
|
|
|
$name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:25:13 +00:00
|
|
|
public function provideTranscludedFrom(): array {
|
2022-06-09 13:51:33 +00:00
|
|
|
return static::getJson( '../cases/transcluded.json' );
|
2020-07-20 14:48:41 +00:00
|
|
|
}
|
|
|
|
|
2021-08-24 05:41:43 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideGetText
|
|
|
|
*/
|
|
|
|
public function testGetText(
|
2022-01-11 15:45:55 +00:00
|
|
|
string $name, string $title, string $dom, string $expected, string $config, string $data
|
2021-08-24 05:41:43 +00:00
|
|
|
): void {
|
2022-06-09 13:51:33 +00:00
|
|
|
$dom = static::getHtml( $dom );
|
2021-08-24 05:41:43 +00:00
|
|
|
$expectedPath = $expected;
|
2022-06-09 13:51:33 +00:00
|
|
|
$expected = static::getJson( $expected );
|
|
|
|
$config = static::getJson( $config );
|
|
|
|
$data = static::getJson( $data );
|
2021-08-24 05:41:43 +00:00
|
|
|
|
2022-06-09 13:51:33 +00:00
|
|
|
$doc = static::createDocument( $dom );
|
|
|
|
$container = static::getThreadContainer( $doc );
|
2021-08-24 05:41:43 +00:00
|
|
|
|
|
|
|
$this->setupEnv( $config, $data );
|
2022-02-21 22:07:38 +00:00
|
|
|
$title = MediaWikiServices::getInstance()->getTitleParser()->parseTitle( $title );
|
2022-06-09 13:51:33 +00:00
|
|
|
$threadItemSet = static::createParser( $data )->parse( $container, $title );
|
2022-02-19 06:31:34 +00:00
|
|
|
$items = $threadItemSet->getThreadItems();
|
2021-08-24 05:41:43 +00:00
|
|
|
|
|
|
|
$output = [];
|
|
|
|
foreach ( $items as $item ) {
|
|
|
|
$output[ $item->getId() ] = CommentUtils::htmlTrim(
|
2022-03-18 03:28:06 +00:00
|
|
|
$item instanceof ContentCommentItem ? $item->getBodyText( true ) : $item->getText()
|
2021-08-24 05:41:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optionally write updated content to the JSON files
|
|
|
|
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
|
2022-06-09 13:51:33 +00:00
|
|
|
static::overwriteJsonFile( $expectedPath, $output );
|
2021-08-24 05:41:43 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 13:51:33 +00:00
|
|
|
static::assertEquals(
|
2021-08-24 05:41:43 +00:00
|
|
|
$expected,
|
|
|
|
$output,
|
|
|
|
$name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideGetText(): array {
|
2022-06-09 13:51:33 +00:00
|
|
|
return static::getJson( '../cases/getText.json' );
|
2021-08-24 05:41:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideGetHTML
|
|
|
|
*/
|
|
|
|
public function testGetHTML(
|
2022-01-11 15:45:55 +00:00
|
|
|
string $name, string $title, string $dom, string $expected, string $config, string $data
|
2021-08-24 05:41:43 +00:00
|
|
|
): void {
|
2022-06-09 13:51:33 +00:00
|
|
|
$dom = static::getHtml( $dom );
|
2021-08-24 05:41:43 +00:00
|
|
|
$expectedPath = $expected;
|
2022-06-09 13:51:33 +00:00
|
|
|
$expected = static::getJson( $expected );
|
|
|
|
$config = static::getJson( $config );
|
|
|
|
$data = static::getJson( $data );
|
2021-08-24 05:41:43 +00:00
|
|
|
|
2022-06-09 13:51:33 +00:00
|
|
|
$doc = static::createDocument( $dom );
|
|
|
|
$container = static::getThreadContainer( $doc );
|
2021-08-24 05:41:43 +00:00
|
|
|
|
|
|
|
$this->setupEnv( $config, $data );
|
2022-02-21 22:07:38 +00:00
|
|
|
$title = MediaWikiServices::getInstance()->getTitleParser()->parseTitle( $title );
|
2022-06-09 13:51:33 +00:00
|
|
|
$threadItemSet = static::createParser( $data )->parse( $container, $title );
|
2022-02-19 06:31:34 +00:00
|
|
|
$items = $threadItemSet->getThreadItems();
|
2021-08-24 05:41:43 +00:00
|
|
|
|
|
|
|
$output = [];
|
|
|
|
foreach ( $items as $item ) {
|
|
|
|
$output[ $item->getId() ] = CommentUtils::htmlTrim(
|
2022-03-18 03:28:06 +00:00
|
|
|
$item instanceof ContentCommentItem ? $item->getBodyHTML( true ) : $item->getHTML()
|
2021-08-24 05:41:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optionally write updated content to the JSON files
|
|
|
|
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
|
2022-06-09 13:51:33 +00:00
|
|
|
static::overwriteJsonFile( $expectedPath, $output );
|
2021-08-24 05:41:43 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 13:51:33 +00:00
|
|
|
static::assertEquals(
|
2021-08-24 05:41:43 +00:00
|
|
|
$expected,
|
|
|
|
$output,
|
|
|
|
$name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideGetHTML(): array {
|
2022-06-09 13:51:33 +00:00
|
|
|
return static::getJson( '../cases/getHTML.json' );
|
2021-08-24 05:41:43 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 14:13:59 +00:00
|
|
|
}
|