mediawiki-extensions-Discus.../tests/phpunit/ThreadItemTest.php
Ed Sanders a2431fe006 Refactor CommentParser
* Pass rootNode to the constructor
* Rename getters to match CommentItem/HeadingItem/ThreadItem
  value classes.
* Always build the thread tree so CommentItem's always have
  and ID and replies/parent.

Change-Id: I508be9534de59016ff806e3d84edcbb1c76cb0c6
2020-07-20 23:38:10 +01:00

88 lines
2.4 KiB
PHP

<?php
namespace MediaWiki\Extension\DiscussionTools\Tests;
use MediaWiki\Extension\DiscussionTools\CommentItem;
use MediaWiki\Extension\DiscussionTools\CommentUtils;
use MediaWiki\Extension\DiscussionTools\HeadingItem;
use MediaWiki\Extension\DiscussionTools\ImmutableRange;
use MediaWiki\Extension\DiscussionTools\ThreadItem;
/**
* @coversDefaultClass \MediaWiki\Extension\DiscussionTools\ThreadItem
*
* @group DiscussionTools
*/
class ThreadItemTest extends CommentTestCase {
/**
* @dataProvider provideAuthors
* @covers ::getAuthorsBelow
*/
public function testGetAuthorsBelow( array $thread, array $expected ) : void {
$doc = $this->createDocument( '' );
$node = $doc->createElement( 'div' );
$range = new ImmutableRange( $node, 0, $node, 0 );
$makeThreadItem = function ( array $arr ) use ( &$makeThreadItem, $range ) : ThreadItem {
if ( $arr['type'] === 'comment' ) {
$item = new CommentItem( 1, $range );
$item->setAuthor( $arr['author'] );
} else {
$item = new HeadingItem( $range );
}
foreach ( $arr['replies'] as $reply ) {
$item->addReply( $makeThreadItem( $reply ) );
}
return $item;
};
$threadItem = $makeThreadItem( $thread );
self::assertEquals( $expected, $threadItem->getAuthorsBelow() );
}
public function provideAuthors() : array {
return self::getJson( '../cases/authors.json' );
}
/**
* @dataProvider provideTranscludedFrom
* @covers ::getTranscludedFrom
* @covers \MediaWiki\Extension\DiscussionTools\CommentUtils::unwrapParsoidSections
*/
public function testGetTranscludedFrom(
string $name, string $dom, string $expected, string $config, string $data
) : void {
$dom = self::getHtml( $dom );
$expected = self::getJson( $expected );
$config = self::getJson( $config );
$data = self::getJson( $data );
$this->setupEnv( $config, $data );
$doc = self::createDocument( $dom );
$container = $doc->getElementsByTagName( 'body' )->item( 0 );
CommentUtils::unwrapParsoidSections( $container );
$parser = self::createParser( $container, $data );
$comments = $parser->getCommentItems();
$transcludedFrom = [];
foreach ( $comments as $comment ) {
$transcludedFrom[ $comment->getId() ] = $comment->getTranscludedFrom();
}
self::assertEquals(
$expected,
$transcludedFrom,
$name
);
}
public function provideTranscludedFrom() : array {
return self::getJson( '../cases/transcluded.json' );
}
}