2019-12-10 02:38:17 +00:00
|
|
|
<?php
|
|
|
|
|
2020-05-14 22:44:49 +00:00
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Tests;
|
|
|
|
|
|
|
|
use DateTimeImmutable;
|
|
|
|
use DOMElement;
|
|
|
|
use DOMNode;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\CommentParser;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\CommentUtils;
|
2020-05-14 23:09:20 +00:00
|
|
|
use stdClass;
|
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
|
|
|
*/
|
2020-05-14 22:44:49 +00:00
|
|
|
class CommentParserTest extends CommentTestCase {
|
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.
|
|
|
|
*
|
|
|
|
* @param DOMElement $ancestor
|
|
|
|
* @param DOMNode $node
|
|
|
|
* @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(
|
|
|
|
DOMElement $ancestor, DOMNode $node, int $nodeOffset
|
|
|
|
) : string {
|
2020-05-13 20:24:35 +00:00
|
|
|
if ( $node->nodeType === XML_TEXT_NODE ) {
|
|
|
|
$startNode = $node;
|
|
|
|
$nodeText = '';
|
|
|
|
|
|
|
|
while ( $node ) {
|
|
|
|
$nodeText .= $node->nodeValue;
|
|
|
|
|
|
|
|
// In Parsoid HTML, entities are represented as a 'mw:Entity' node, rather than normal HTML
|
|
|
|
// entities. On Arabic Wikipedia, the "UTC" timezone name contains some non-breaking spaces,
|
|
|
|
// which apparently are often turned into entities by buggy editing tools. To handle
|
|
|
|
// this, we must piece together the text, so that our regexp can match those timestamps.
|
|
|
|
if (
|
|
|
|
$node->nextSibling &&
|
|
|
|
$node->nextSibling->nodeType === XML_ELEMENT_NODE &&
|
|
|
|
$node->nextSibling->getAttribute( 'typeof' ) === 'mw:Entity'
|
|
|
|
) {
|
|
|
|
$nodeText .= $node->nextSibling->firstChild->nodeValue;
|
|
|
|
|
|
|
|
// If the entity is followed by more text, do this again
|
|
|
|
if (
|
|
|
|
$node->nextSibling->nextSibling &&
|
|
|
|
$node->nextSibling->nextSibling->nodeType === XML_TEXT_NODE
|
|
|
|
) {
|
|
|
|
$node = $node->nextSibling->nextSibling;
|
|
|
|
} else {
|
|
|
|
$node = null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$node = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$str = substr( $nodeText, 0, $nodeOffset );
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
$node = $startNode;
|
|
|
|
}
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2020-05-14 23:09:20 +00:00
|
|
|
private static function serializeComments( stdClass &$parent, DOMElement $root ) : void {
|
2020-05-05 13:12:51 +00:00
|
|
|
unset( $parent->parent );
|
|
|
|
|
|
|
|
// Can't serialize the DOM nodes involved in the range,
|
|
|
|
// instead use their offsets within their parent nodes
|
|
|
|
$parent->range = [
|
|
|
|
self::getOffsetPath( $root, $parent->range->startContainer, $parent->range->startOffset ),
|
|
|
|
self::getOffsetPath( $root, $parent->range->endContainer, $parent->range->endOffset )
|
|
|
|
];
|
|
|
|
if ( isset( $parent->signatureRanges ) ) {
|
|
|
|
$parent->signatureRanges = array_map( function ( $range ) use ( $root ) {
|
|
|
|
return [
|
|
|
|
self::getOffsetPath( $root, $range->startContainer, $range->startOffset ),
|
|
|
|
self::getOffsetPath( $root, $range->endContainer, $range->endOffset )
|
|
|
|
];
|
|
|
|
}, $parent->signatureRanges );
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $parent->replies as $reply ) {
|
|
|
|
self::serializeComments( $reply, $root );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
) : void {
|
2019-12-10 02:38:17 +00:00
|
|
|
$parser = TestingAccessWrapper::newFromObject(
|
2020-05-14 22:44:49 +00:00
|
|
|
CommentParser::newFromGlobalState()
|
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';
|
|
|
|
|
|
|
|
$result = $parser->getTimestampRegexp( $format, '\\d', [ 'UTC' => 'UTC' ] );
|
|
|
|
self::assertSame( $expected, $result, $message );
|
|
|
|
}
|
|
|
|
|
2020-05-14 23:09:20 +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
|
|
|
|
) : void {
|
2019-12-10 02:38:17 +00:00
|
|
|
$parser = TestingAccessWrapper::newFromObject(
|
2020-05-14 22:44:49 +00:00
|
|
|
CommentParser::newFromGlobalState()
|
2019-12-10 02:38:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$expected = new DateTimeImmutable( $expected );
|
|
|
|
|
|
|
|
$tsParser = $parser->getTimestampParser( $format, null, 'UTC', [ 'UTC' => 'UTC' ] );
|
|
|
|
self::assertEquals( $expected, $tsParser( $data ), $message );
|
|
|
|
}
|
|
|
|
|
2020-05-14 23:09:20 +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
|
|
|
|
) : void {
|
2019-12-10 02:38:17 +00:00
|
|
|
$parser = TestingAccessWrapper::newFromObject(
|
2020-05-14 22:44:49 +00:00
|
|
|
CommentParser::newFromGlobalState()
|
2019-12-10 02:38:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$regexp = $parser->getTimestampRegexp( $format, '\\d', $timezoneAbbrs );
|
|
|
|
$tsParser = $parser->getTimestampParser( $format, null, $timezone, $timezoneAbbrs );
|
|
|
|
|
|
|
|
$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 );
|
|
|
|
}
|
|
|
|
|
2020-05-14 23:09:20 +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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideAuthors
|
|
|
|
* @covers ::getAuthors
|
|
|
|
*/
|
2020-05-14 23:09:20 +00:00
|
|
|
public function testGetAuthors( stdClass $thread, array $expected ) : void {
|
2020-05-14 22:44:49 +00:00
|
|
|
$parser = CommentParser::newFromGlobalState();
|
2019-12-10 02:38:17 +00:00
|
|
|
|
|
|
|
self::assertEquals( $expected, $parser->getAuthors( $thread ) );
|
|
|
|
}
|
|
|
|
|
2020-05-14 23:09:20 +00:00
|
|
|
public function provideAuthors() : array {
|
2020-05-19 19:01:03 +00:00
|
|
|
return array_map( function ( $caseItem ) {
|
|
|
|
// PHPUnit requires associative arrays, not stdClass objects
|
|
|
|
return (array)$caseItem;
|
|
|
|
}, self::getJson( '../cases/authors.json', false ) );
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
2020-05-05 13:12:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideComments
|
|
|
|
* @covers ::getComments
|
|
|
|
* @covers ::groupThreads
|
|
|
|
*/
|
2020-05-14 23:09:20 +00:00
|
|
|
public function testGetComments(
|
|
|
|
string $name, string $dom, string $expected, string $config, string $data
|
|
|
|
) : void {
|
2020-05-11 15:52:06 +00:00
|
|
|
$dom = self::getHtml( $dom );
|
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
|
|
|
$this->setupEnv( $config, $data );
|
2020-05-15 00:51:36 +00:00
|
|
|
$parser = self::createParser( $data );
|
2020-05-05 13:12:51 +00:00
|
|
|
|
2020-05-11 15:52:06 +00:00
|
|
|
$doc = self::createDocument( $dom );
|
2020-05-05 13:12:51 +00:00
|
|
|
$container = $doc->documentElement->childNodes[0];
|
|
|
|
|
2020-05-15 00:51:36 +00:00
|
|
|
$comments = $parser->getComments( $container );
|
|
|
|
$threads = $parser->groupThreads( $comments );
|
2020-05-05 13:12:51 +00:00
|
|
|
|
|
|
|
$processedThreads = [];
|
|
|
|
|
|
|
|
foreach ( $threads as $i => $thread ) {
|
|
|
|
self::serializeComments( $thread, $container );
|
|
|
|
$thread = json_decode( json_encode( $thread ), true );
|
|
|
|
$processedThreads[] = $thread;
|
|
|
|
self::assertEquals( $expected[$i], $processedThreads[$i], $name . ' section ' . $i );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 23:09:20 +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
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTranscludedFrom
|
|
|
|
* @covers ::getComments
|
|
|
|
* @covers ::groupThreads
|
2020-05-18 19:33:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\DiscussionTools\CommentUtils::unwrapParsoidSections
|
2020-05-15 00:51:36 +00:00
|
|
|
*/
|
|
|
|
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 );
|
|
|
|
$parser = self::createParser( $data );
|
|
|
|
|
|
|
|
$doc = self::createDocument( $dom );
|
|
|
|
$container = $doc->documentElement->childNodes[0];
|
|
|
|
|
|
|
|
CommentUtils::unwrapParsoidSections( $doc->documentElement );
|
|
|
|
|
|
|
|
$comments = $parser->getComments( $container );
|
|
|
|
$threads = $parser->groupThreads( $comments );
|
|
|
|
|
|
|
|
$transcludedFrom = [];
|
|
|
|
foreach ( $comments as $comment ) {
|
|
|
|
if ( $comment->id ) {
|
|
|
|
$transcludedFrom[ $comment->id ] =
|
|
|
|
$parser->getTranscludedFrom( $comment );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self::assertEquals(
|
|
|
|
$expected,
|
|
|
|
$transcludedFrom,
|
|
|
|
$name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTranscludedFrom() : array {
|
2020-05-18 20:07:00 +00:00
|
|
|
return self::getJson( '../cases/transcluded.json' );
|
2020-05-15 00:51:36 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|