2020-07-20 14:13:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Tests;
|
|
|
|
|
|
|
|
use MediaWiki\Extension\DiscussionTools\CommentItem;
|
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\HeadingItem;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\ImmutableRange;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem;
|
2021-08-02 13:45:39 +00:00
|
|
|
use Wikimedia\Parsoid\Utils\DOMCompat;
|
2020-07-20 14:13:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @coversDefaultClass \MediaWiki\Extension\DiscussionTools\ThreadItem
|
|
|
|
*
|
|
|
|
* @group DiscussionTools
|
|
|
|
*/
|
2021-02-02 14:12:51 +00:00
|
|
|
class ThreadItemTest extends IntegrationTestCase {
|
2020-07-20 14:13:59 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideAuthors
|
|
|
|
* @covers ::getAuthorsBelow
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function testGetAuthorsBelow( array $thread, array $expected ): void {
|
2020-07-20 14:13:59 +00:00
|
|
|
$doc = $this->createDocument( '' );
|
|
|
|
$node = $doc->createElement( 'div' );
|
|
|
|
$range = new ImmutableRange( $node, 0, $node, 0 );
|
|
|
|
|
2021-07-22 07:25:13 +00:00
|
|
|
$makeThreadItem = static function ( array $arr ) use ( &$makeThreadItem, $range ): ThreadItem {
|
2020-07-20 14:13:59 +00:00
|
|
|
if ( $arr['type'] === 'comment' ) {
|
2021-01-08 19:44:15 +00:00
|
|
|
$item = new CommentItem( 1, $range, [], 'TIMESTAMP', $arr['author'] );
|
2020-07-20 14:13:59 +00:00
|
|
|
} else {
|
2020-10-01 19:36:11 +00:00
|
|
|
$item = new HeadingItem( $range, 2 );
|
2020-07-20 14:13:59 +00:00
|
|
|
}
|
|
|
|
foreach ( $arr['replies'] as $reply ) {
|
|
|
|
$item->addReply( $makeThreadItem( $reply ) );
|
|
|
|
}
|
|
|
|
return $item;
|
|
|
|
};
|
|
|
|
|
|
|
|
$threadItem = $makeThreadItem( $thread );
|
|
|
|
|
|
|
|
self::assertEquals( $expected, $threadItem->getAuthorsBelow() );
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:25:13 +00:00
|
|
|
public function provideAuthors(): array {
|
2020-07-20 14:13:59 +00:00
|
|
|
return self::getJson( '../cases/authors.json' );
|
|
|
|
}
|
|
|
|
|
2020-07-20 14:48:41 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideTranscludedFrom
|
|
|
|
* @covers ::getTranscludedFrom
|
|
|
|
* @covers \MediaWiki\Extension\DiscussionTools\CommentUtils::unwrapParsoidSections
|
|
|
|
*/
|
|
|
|
public function testGetTranscludedFrom(
|
|
|
|
string $name, string $dom, string $expected, string $config, string $data
|
2021-07-22 07:25:13 +00:00
|
|
|
): void {
|
2020-07-20 14:48:41 +00:00
|
|
|
$dom = self::getHtml( $dom );
|
2020-07-30 23:34:56 +00:00
|
|
|
$expectedPath = $expected;
|
2020-07-20 14:48:41 +00:00
|
|
|
$expected = self::getJson( $expected );
|
|
|
|
$config = self::getJson( $config );
|
|
|
|
$data = self::getJson( $data );
|
|
|
|
|
|
|
|
$this->setupEnv( $config, $data );
|
|
|
|
|
|
|
|
$doc = self::createDocument( $dom );
|
2021-08-02 13:45:39 +00:00
|
|
|
$container = DOMCompat::getBody( $doc );
|
2020-07-20 14:48:41 +00:00
|
|
|
|
|
|
|
CommentUtils::unwrapParsoidSections( $container );
|
|
|
|
|
2020-07-20 21:15:03 +00:00
|
|
|
$parser = self::createParser( $container, $data );
|
|
|
|
$comments = $parser->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' ) ) {
|
|
|
|
self::overwriteJsonFile( $expectedPath, $transcludedFrom );
|
|
|
|
}
|
2020-08-10 21:31:49 +00:00
|
|
|
|
2020-07-20 14:48:41 +00:00
|
|
|
self::assertEquals(
|
|
|
|
$expected,
|
|
|
|
$transcludedFrom,
|
|
|
|
$name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:25:13 +00:00
|
|
|
public function provideTranscludedFrom(): array {
|
2020-07-20 14:48:41 +00:00
|
|
|
return self::getJson( '../cases/transcluded.json' );
|
|
|
|
}
|
|
|
|
|
2020-07-20 14:13:59 +00:00
|
|
|
}
|