2019-12-10 02:38:17 +00:00
|
|
|
<?php
|
|
|
|
|
2020-05-14 22:44:49 +00:00
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Tests;
|
|
|
|
|
|
|
|
use DateTimeImmutable;
|
2020-06-16 20:13:31 +00:00
|
|
|
use Error;
|
2020-05-22 16:26:05 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\CommentItem;
|
2020-05-14 22:44:49 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\CommentParser;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\CommentUtils;
|
2020-05-22 16:26:05 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\HeadingItem;
|
2020-05-22 13:47:21 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\ImmutableRange;
|
2020-05-22 16:26:05 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem;
|
2020-05-14 23:09:20 +00:00
|
|
|
use stdClass;
|
2021-07-29 02:16:15 +00:00
|
|
|
use Wikimedia\Parsoid\DOM\Element;
|
|
|
|
use Wikimedia\Parsoid\DOM\Node;
|
2021-08-02 13:45:39 +00:00
|
|
|
use Wikimedia\Parsoid\Utils\DOMCompat;
|
2019-12-10 02:38:17 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
|
|
|
/**
|
2020-05-15 21:08:25 +00:00
|
|
|
* @coversDefaultClass \MediaWiki\Extension\DiscussionTools\CommentParser
|
2020-05-14 22:44:49 +00:00
|
|
|
*
|
|
|
|
* @group DiscussionTools
|
2019-12-10 02:38:17 +00:00
|
|
|
*/
|
2021-02-02 14:12:51 +00:00
|
|
|
class CommentParserTest extends IntegrationTestCase {
|
2019-12-10 02:38:17 +00:00
|
|
|
|
2020-05-13 20:24:35 +00:00
|
|
|
/**
|
|
|
|
* Convert UTF-8 byte offsets to UTF-16 code unit offsets.
|
|
|
|
*
|
2021-07-29 02:16:15 +00:00
|
|
|
* @param Element $ancestor
|
|
|
|
* @param Node $node
|
2020-05-13 20:24:35 +00:00
|
|
|
* @param int $nodeOffset
|
2020-05-14 23:09:20 +00:00
|
|
|
* @return string
|
2020-05-13 20:24:35 +00:00
|
|
|
*/
|
2020-05-14 23:09:20 +00:00
|
|
|
private static function getOffsetPath(
|
2021-07-29 02:16:15 +00:00
|
|
|
Element $ancestor, Node $node, int $nodeOffset
|
2021-07-22 07:25:13 +00:00
|
|
|
): string {
|
2020-05-13 20:24:35 +00:00
|
|
|
if ( $node->nodeType === XML_TEXT_NODE ) {
|
2020-08-11 04:22:57 +00:00
|
|
|
$str = substr( $node->nodeValue, 0, $nodeOffset );
|
2020-05-13 20:24:35 +00:00
|
|
|
// Count characters that require two code units to encode in UTF-16
|
|
|
|
$count = preg_match_all( '/[\x{010000}-\x{10FFFF}]/u', $str );
|
|
|
|
$nodeOffset = mb_strlen( $str ) + $count;
|
|
|
|
}
|
|
|
|
|
2020-05-05 13:12:51 +00:00
|
|
|
$path = [ $nodeOffset ];
|
|
|
|
while ( $node !== $ancestor ) {
|
|
|
|
if ( !$node->parentNode ) {
|
|
|
|
throw new Error( 'Not a descendant' );
|
|
|
|
}
|
2020-05-14 22:44:49 +00:00
|
|
|
array_unshift( $path, CommentUtils::childIndexOf( $node ) );
|
2020-05-05 13:12:51 +00:00
|
|
|
$node = $node->parentNode;
|
|
|
|
}
|
|
|
|
return implode( '/', $path );
|
|
|
|
}
|
|
|
|
|
2021-07-29 02:16:15 +00:00
|
|
|
private static function serializeComments( ThreadItem &$threadItem, Element $root ): stdClass {
|
2020-05-22 16:26:05 +00:00
|
|
|
$serialized = new stdClass();
|
|
|
|
|
2020-10-07 14:45:19 +00:00
|
|
|
if ( $threadItem instanceof HeadingItem ) {
|
2020-07-30 23:34:56 +00:00
|
|
|
$serialized->placeholderHeading = $threadItem->isPlaceholderHeading();
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
$serialized->type = $threadItem->getType();
|
2020-07-30 23:34:56 +00:00
|
|
|
|
|
|
|
if ( $threadItem instanceof CommentItem ) {
|
|
|
|
$serialized->timestamp = $threadItem->getTimestamp();
|
|
|
|
$serialized->author = $threadItem->getAuthor();
|
|
|
|
}
|
2020-05-05 13:12:51 +00:00
|
|
|
|
|
|
|
// Can't serialize the DOM nodes involved in the range,
|
|
|
|
// instead use their offsets within their parent nodes
|
2020-05-22 16:26:05 +00:00
|
|
|
$range = $threadItem->getRange();
|
|
|
|
$serialized->range = [
|
|
|
|
self::getOffsetPath( $root, $range->startContainer, $range->startOffset ),
|
|
|
|
self::getOffsetPath( $root, $range->endContainer, $range->endOffset )
|
2020-05-05 13:12:51 +00:00
|
|
|
];
|
2020-05-22 16:26:05 +00:00
|
|
|
|
|
|
|
if ( $threadItem instanceof CommentItem ) {
|
|
|
|
$serialized->signatureRanges = array_map( function ( ImmutableRange $range ) use ( $root ) {
|
2020-05-05 13:12:51 +00:00
|
|
|
return [
|
|
|
|
self::getOffsetPath( $root, $range->startContainer, $range->startOffset ),
|
|
|
|
self::getOffsetPath( $root, $range->endContainer, $range->endOffset )
|
|
|
|
];
|
2020-05-22 16:26:05 +00:00
|
|
|
}, $threadItem->getSignatureRanges() );
|
2020-07-30 23:34:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 19:36:11 +00:00
|
|
|
if ( $threadItem instanceof HeadingItem ) {
|
|
|
|
$serialized->headingLevel = $threadItem->getHeadingLevel();
|
|
|
|
}
|
2020-07-30 23:34:56 +00:00
|
|
|
$serialized->level = $threadItem->getLevel();
|
2021-02-12 19:16:13 +00:00
|
|
|
$serialized->name = $threadItem->getName();
|
2020-07-30 23:34:56 +00:00
|
|
|
$serialized->id = $threadItem->getId();
|
|
|
|
|
2020-11-02 18:35:38 +00:00
|
|
|
$serialized->warnings = $threadItem->getWarnings();
|
2021-03-18 19:41:46 +00:00
|
|
|
// Ignore warnings about legacy IDs (we don't have them in JS)
|
|
|
|
$serialized->warnings = array_values( array_diff( $serialized->warnings, [ 'Duplicate comment legacy ID' ] ) );
|
2020-05-05 13:12:51 +00:00
|
|
|
|
2020-07-30 23:34:56 +00:00
|
|
|
$serialized->replies = [];
|
|
|
|
foreach ( $threadItem->getReplies() as $reply ) {
|
|
|
|
$serialized->replies[] = self::serializeComments( $reply, $root );
|
2020-05-05 13:12:51 +00:00
|
|
|
}
|
2020-05-22 16:26:05 +00:00
|
|
|
|
|
|
|
return $serialized;
|
2020-05-05 13:12:51 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 02:38:17 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideTimestampRegexps
|
|
|
|
* @covers ::getTimestampRegexp
|
|
|
|
*/
|
2020-05-14 23:09:20 +00:00
|
|
|
public function testGetTimestampRegexp(
|
|
|
|
string $format, string $expected, string $message
|
2021-07-22 07:25:13 +00:00
|
|
|
): void {
|
2019-12-10 02:38:17 +00:00
|
|
|
$parser = TestingAccessWrapper::newFromObject(
|
2021-07-29 02:16:15 +00:00
|
|
|
CommentParser::newFromGlobalState( new Element( 'div' ) )
|
2019-12-10 02:38:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// HACK: Fix differences between JS & PHP regexes
|
|
|
|
// TODO: We may just have to have two version in the test data
|
|
|
|
$expected = preg_replace( '/\\\\u([0-9A-F]+)/', '\\\\x{$1}', $expected );
|
|
|
|
$expected = str_replace( ':', '\:', $expected );
|
|
|
|
$expected = '/' . $expected . '/u';
|
|
|
|
|
2020-09-03 20:59:33 +00:00
|
|
|
$result = $parser->getTimestampRegexp( 'en', $format, '\\d', [ 'UTC' => 'UTC' ] );
|
2019-12-10 02:38:17 +00:00
|
|
|
self::assertSame( $expected, $result, $message );
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:25:13 +00:00
|
|
|
public function provideTimestampRegexps(): array {
|
2020-05-18 20:07:00 +00:00
|
|
|
return self::getJson( '../cases/timestamp-regex.json' );
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTimestampParser
|
|
|
|
* @covers ::getTimestampParser
|
|
|
|
*/
|
2020-05-14 23:09:20 +00:00
|
|
|
public function testGetTimestampParser(
|
|
|
|
string $format, array $data, string $expected, string $message
|
2021-07-22 07:25:13 +00:00
|
|
|
): void {
|
2019-12-10 02:38:17 +00:00
|
|
|
$parser = TestingAccessWrapper::newFromObject(
|
2021-07-29 02:16:15 +00:00
|
|
|
CommentParser::newFromGlobalState( new Element( 'div' ) )
|
2019-12-10 02:38:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$expected = new DateTimeImmutable( $expected );
|
|
|
|
|
2020-09-03 20:59:33 +00:00
|
|
|
$tsParser = $parser->getTimestampParser( 'en', $format, null, 'UTC', [ 'UTC' => 'UTC' ] );
|
2019-12-10 02:38:17 +00:00
|
|
|
self::assertEquals( $expected, $tsParser( $data ), $message );
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:25:13 +00:00
|
|
|
public function provideTimestampParser(): array {
|
2020-05-18 20:07:00 +00:00
|
|
|
return self::getJson( '../cases/timestamp-parser.json' );
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTimestampParserDST
|
|
|
|
* @covers ::getTimestampParser
|
|
|
|
*/
|
|
|
|
public function testGetTimestampParserDST(
|
2020-05-14 23:09:20 +00:00
|
|
|
string $sample, string $expected, string $expectedUtc, string $format,
|
|
|
|
string $timezone, array $timezoneAbbrs, string $message
|
2021-07-22 07:25:13 +00:00
|
|
|
): void {
|
2019-12-10 02:38:17 +00:00
|
|
|
$parser = TestingAccessWrapper::newFromObject(
|
2021-07-29 02:16:15 +00:00
|
|
|
CommentParser::newFromGlobalState( new Element( 'div' ) )
|
2019-12-10 02:38:17 +00:00
|
|
|
);
|
|
|
|
|
2020-09-03 20:59:33 +00:00
|
|
|
$regexp = $parser->getTimestampRegexp( 'en', $format, '\\d', $timezoneAbbrs );
|
|
|
|
$tsParser = $parser->getTimestampParser( 'en', $format, null, $timezone, $timezoneAbbrs );
|
2019-12-10 02:38:17 +00:00
|
|
|
|
|
|
|
$expected = new DateTimeImmutable( $expected );
|
|
|
|
$expectedUtc = new DateTimeImmutable( $expectedUtc );
|
|
|
|
|
|
|
|
preg_match( $regexp, $sample, $match, PREG_OFFSET_CAPTURE );
|
|
|
|
$date = $tsParser( $match );
|
|
|
|
|
|
|
|
self::assertEquals( $expected, $date, $message );
|
|
|
|
self::assertEquals( $expectedUtc, $date, $message );
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:25:13 +00:00
|
|
|
public function provideTimestampParserDST(): array {
|
2020-05-18 20:07:00 +00:00
|
|
|
return self::getJson( '../cases/timestamp-parser-dst.json' );
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 13:12:51 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideComments
|
2020-07-20 21:15:03 +00:00
|
|
|
* @covers ::getThreads
|
2020-05-05 13:12:51 +00:00
|
|
|
*/
|
2020-07-20 21:15:03 +00:00
|
|
|
public function testGetThreads(
|
2020-05-14 23:09:20 +00:00
|
|
|
string $name, string $dom, string $expected, string $config, string $data
|
2021-07-22 07:25:13 +00:00
|
|
|
): void {
|
2020-05-11 15:52:06 +00:00
|
|
|
$dom = self::getHtml( $dom );
|
2020-07-30 23:34:56 +00:00
|
|
|
$expectedPath = $expected;
|
2020-05-05 13:12:51 +00:00
|
|
|
$expected = self::getJson( $expected );
|
|
|
|
$config = self::getJson( $config );
|
|
|
|
$data = self::getJson( $data );
|
|
|
|
|
2020-05-11 15:52:06 +00:00
|
|
|
$doc = self::createDocument( $dom );
|
2021-08-02 13:45:39 +00:00
|
|
|
$body = DOMCompat::getBody( $doc );
|
2020-05-05 13:12:51 +00:00
|
|
|
|
2020-07-20 21:15:03 +00:00
|
|
|
$this->setupEnv( $config, $data );
|
|
|
|
$parser = self::createParser( $body, $data );
|
|
|
|
$threads = $parser->getThreads();
|
2020-05-05 13:12:51 +00:00
|
|
|
|
|
|
|
$processedThreads = [];
|
|
|
|
|
|
|
|
foreach ( $threads as $i => $thread ) {
|
2020-06-16 20:13:31 +00:00
|
|
|
$thread = self::serializeComments( $thread, $body );
|
2020-05-05 13:12:51 +00:00
|
|
|
$thread = json_decode( json_encode( $thread ), true );
|
|
|
|
$processedThreads[] = $thread;
|
|
|
|
}
|
2020-07-30 23:34:56 +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, $processedThreads );
|
|
|
|
}
|
2020-08-10 21:31:49 +00:00
|
|
|
|
|
|
|
foreach ( $threads as $i => $thread ) {
|
|
|
|
self::assertEquals( $expected[$i], $processedThreads[$i], $name . ' section ' . $i );
|
|
|
|
}
|
2020-05-05 13:12:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:25:13 +00:00
|
|
|
public function provideComments(): array {
|
2020-05-18 20:07:00 +00:00
|
|
|
return self::getJson( '../cases/comments.json' );
|
2020-05-05 13:12:51 +00:00
|
|
|
}
|
2020-05-15 00:51:36 +00:00
|
|
|
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|