Add some tests covering ThreadItem::getHTML() and related methods

* ThreadItem::getText
* CommentItem::getBodyText (used when generating notifications)
* ThreadItem::getHTML (may soon be used in API)
* CommentItem::getBodyHTML (may soon be used in API)
* ImmutableRange::cloneContents (the common implementation for all
  of the above)

The outputs are only lightly reviewed. This is mostly meant to
document the current behavior rather than the expected behavior,
to avoid making unintentional changes while refactoring.

Change-Id: I14471ee4969aa3d0b5577d9de2a6d4462fab4d09
This commit is contained in:
Bartosz Dziewoński 2021-08-24 07:41:43 +02:00
parent b1eb884966
commit a6a547f2b2
15 changed files with 6970 additions and 0 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

44
tests/cases/getHTML.json Normal file
View file

@ -0,0 +1,44 @@
[
{
"name": "plwiki oldparser",
"dom": "cases/pl-big-oldparser/pl-big-oldparser.html",
"expected": "../cases/pl-big-oldparser/pl-big-oldparser-getHTML.json",
"config": "../data/plwiki-config.json",
"data": "../data/plwiki-data.json"
},
{
"name": "plwiki parsoid",
"dom": "cases/pl-big-parsoid/pl-big-parsoid.html",
"expected": "../cases/pl-big-parsoid/pl-big-parsoid-getHTML.json",
"config": "../data/plwiki-config.json",
"data": "../data/plwiki-data.json"
},
{
"name": "enwiki oldparser",
"dom": "cases/en-big-oldparser/en-big-oldparser.html",
"expected": "../cases/en-big-oldparser/en-big-oldparser-getHTML.json",
"config": "../data/enwiki-config.json",
"data": "../data/enwiki-data.json"
},
{
"name": "enwiki parsoid",
"dom": "cases/en-big-parsoid/en-big-parsoid.html",
"expected": "../cases/en-big-parsoid/en-big-parsoid-getHTML.json",
"config": "../data/enwiki-config.json",
"data": "../data/enwiki-data.json"
},
{
"name": "ckbwiki oldparser",
"dom": "cases/ckb-big-oldparser/ckb-big-oldparser.html",
"expected": "../cases/ckb-big-oldparser/ckb-big-oldparser-getHTML.json",
"config": "../data/ckbwiki-config.json",
"data": "../data/ckbwiki-data.json"
},
{
"name": "ckbwiki parsoid",
"dom": "cases/ckb-big-parsoid/ckb-big-parsoid.html",
"expected": "../cases/ckb-big-parsoid/ckb-big-parsoid-getHTML.json",
"config": "../data/ckbwiki-config.json",
"data": "../data/ckbwiki-data.json"
}
]

44
tests/cases/getText.json Normal file
View file

@ -0,0 +1,44 @@
[
{
"name": "plwiki oldparser",
"dom": "cases/pl-big-oldparser/pl-big-oldparser.html",
"expected": "../cases/pl-big-oldparser/pl-big-oldparser-getText.json",
"config": "../data/plwiki-config.json",
"data": "../data/plwiki-data.json"
},
{
"name": "plwiki parsoid",
"dom": "cases/pl-big-parsoid/pl-big-parsoid.html",
"expected": "../cases/pl-big-parsoid/pl-big-parsoid-getText.json",
"config": "../data/plwiki-config.json",
"data": "../data/plwiki-data.json"
},
{
"name": "enwiki oldparser",
"dom": "cases/en-big-oldparser/en-big-oldparser.html",
"expected": "../cases/en-big-oldparser/en-big-oldparser-getText.json",
"config": "../data/enwiki-config.json",
"data": "../data/enwiki-data.json"
},
{
"name": "enwiki parsoid",
"dom": "cases/en-big-parsoid/en-big-parsoid.html",
"expected": "../cases/en-big-parsoid/en-big-parsoid-getText.json",
"config": "../data/enwiki-config.json",
"data": "../data/enwiki-data.json"
},
{
"name": "ckbwiki oldparser",
"dom": "cases/ckb-big-oldparser/ckb-big-oldparser.html",
"expected": "../cases/ckb-big-oldparser/ckb-big-oldparser-getText.json",
"config": "../data/ckbwiki-config.json",
"data": "../data/ckbwiki-data.json"
},
{
"name": "ckbwiki parsoid",
"dom": "cases/ckb-big-parsoid/ckb-big-parsoid.html",
"expected": "../cases/ckb-big-parsoid/ckb-big-parsoid-getText.json",
"config": "../data/ckbwiki-config.json",
"data": "../data/ckbwiki-data.json"
}
]

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -90,4 +90,94 @@ class ThreadItemTest extends IntegrationTestCase {
return self::getJson( '../cases/transcluded.json' );
}
/**
* @dataProvider provideGetText
* @covers ::getText
* @covers \MediaWiki\Extension\DiscussionTools\CommentItem::getBodyText
* @covers \MediaWiki\Extension\DiscussionTools\ImmutableRange::cloneContents
*/
public function testGetText(
string $name, string $dom, string $expected, string $config, string $data
): void {
$dom = self::getHtml( $dom );
$expectedPath = $expected;
$expected = self::getJson( $expected );
$config = self::getJson( $config );
$data = self::getJson( $data );
$doc = self::createDocument( $dom );
$body = DOMCompat::getBody( $doc );
$this->setupEnv( $config, $data );
$parser = self::createParser( $body, $data );
$items = $parser->getThreadItems();
$output = [];
foreach ( $items as $item ) {
$output[ $item->getId() ] = CommentUtils::htmlTrim(
$item instanceof CommentItem ? $item->getBodyText( true ) : $item->getText()
);
}
// Optionally write updated content to the JSON files
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
self::overwriteJsonFile( $expectedPath, $output );
}
self::assertEquals(
$expected,
$output,
$name
);
}
public function provideGetText(): array {
return self::getJson( '../cases/getText.json' );
}
/**
* @dataProvider provideGetHTML
* @covers ::getHTML
* @covers \MediaWiki\Extension\DiscussionTools\CommentItem::getBodyHTML
* @covers \MediaWiki\Extension\DiscussionTools\ImmutableRange::cloneContents
*/
public function testGetHTML(
string $name, string $dom, string $expected, string $config, string $data
): void {
$dom = self::getHtml( $dom );
$expectedPath = $expected;
$expected = self::getJson( $expected );
$config = self::getJson( $config );
$data = self::getJson( $data );
$doc = self::createDocument( $dom );
$body = DOMCompat::getBody( $doc );
$this->setupEnv( $config, $data );
$parser = self::createParser( $body, $data );
$items = $parser->getThreadItems();
$output = [];
foreach ( $items as $item ) {
$output[ $item->getId() ] = CommentUtils::htmlTrim(
$item instanceof CommentItem ? $item->getBodyHTML( true ) : $item->getHTML()
);
}
// Optionally write updated content to the JSON files
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
self::overwriteJsonFile( $expectedPath, $output );
}
self::assertEquals(
$expected,
$output,
$name
);
}
public function provideGetHTML(): array {
return self::getJson( '../cases/getHTML.json' );
}
}