| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\DiscussionTools\Tests; |
| 4 |
|
| 5 |
use DateTimeImmutable; |
| 6 |
use MediaWiki\Extension\DiscussionTools\CommentParser; |
| 7 |
use MediaWiki\Extension\DiscussionTools\CommentUtils; |
| 8 |
use MediaWiki\Extension\DiscussionTools\ImmutableRange; |
| 9 |
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentCommentItem; |
| 10 |
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentHeadingItem; |
| 11 |
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentThreadItem; |
| 12 |
use RuntimeException; |
| 13 |
use stdClass; |
| 14 |
use Wikimedia\Parsoid\DOM\Element; |
| 15 |
use Wikimedia\Parsoid\DOM\Node; |
| 16 |
use Wikimedia\Parsoid\DOM\Text; |
| 17 |
use Wikimedia\TestingAccessWrapper; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class CommentParserTest extends IntegrationTestCase { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
private static function getOffsetPath( |
| 32 |
Element $ancestor, Node $node, int $nodeOffset |
| 33 |
): string { |
| 34 |
if ( $node instanceof Text ) { |
| 35 |
$str = mb_substr( $node->nodeValue, 0, $nodeOffset ); |
| 36 |
|
| 37 |
$count = preg_match_all( '/[\x{010000}-\x{10FFFF}]/u', $str ); |
| 38 |
$nodeOffset += $count; |
| 39 |
} |
| 40 |
|
| 41 |
$path = [ $nodeOffset ]; |
| 42 |
while ( $node !== $ancestor ) { |
| 43 |
if ( !$node->parentNode ) { |
| 44 |
throw new RuntimeException( 'Not a descendant' ); |
| 45 |
} |
| 46 |
array_unshift( $path, CommentUtils::childIndexOf( $node ) ); |
| 47 |
$node = $node->parentNode; |
| 48 |
} |
| 49 |
return implode( '/', $path ); |
| 50 |
} |
| 51 |
|
| 52 |
private static function getPathsFromRange( ImmutableRange $range, Element $root ): array { |
| 53 |
return [ |
| 54 |
static::getOffsetPath( $root, $range->startContainer, $range->startOffset ), |
| 55 |
static::getOffsetPath( $root, $range->endContainer, $range->endOffset ) |
| 56 |
]; |
| 57 |
} |
| 58 |
|
| 59 |
private static function serializeComments( ContentThreadItem $threadItem, Element $root ): stdClass { |
| 60 |
$serialized = new stdClass(); |
| 61 |
|
| 62 |
if ( $threadItem instanceof ContentHeadingItem ) { |
| 63 |
$serialized->placeholderHeading = $threadItem->isPlaceholderHeading(); |
| 64 |
} |
| 65 |
|
| 66 |
$serialized->type = $threadItem->getType(); |
| 67 |
|
| 68 |
if ( $threadItem instanceof ContentCommentItem ) { |
| 69 |
$serialized->timestamp = $threadItem->getTimestampString(); |
| 70 |
$serialized->author = $threadItem->getAuthor(); |
| 71 |
if ( $threadItem->getDisplayName() ) { |
| 72 |
$serialized->displayName = $threadItem->getDisplayName(); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
$range = $threadItem->getRange(); |
| 79 |
$serialized->range = static::getPathsFromRange( $range, $root ); |
| 80 |
|
| 81 |
if ( $threadItem instanceof ContentCommentItem ) { |
| 82 |
$serialized->signatureRanges = array_map( function ( ImmutableRange $range ) use ( $root ) { |
| 83 |
return static::getPathsFromRange( $range, $root ); |
| 84 |
}, $threadItem->getSignatureRanges() ); |
| 85 |
|
| 86 |
$serialized->timestampRanges = array_map( function ( ImmutableRange $range ) use ( $root ) { |
| 87 |
return static::getPathsFromRange( $range, $root ); |
| 88 |
}, $threadItem->getTimestampRanges() ); |
| 89 |
} |
| 90 |
|
| 91 |
if ( $threadItem instanceof ContentHeadingItem ) { |
| 92 |
$serialized->headingLevel = $threadItem->getHeadingLevel(); |
| 93 |
} |
| 94 |
$serialized->level = $threadItem->getLevel(); |
| 95 |
$serialized->name = $threadItem->getName(); |
| 96 |
$serialized->id = $threadItem->getId(); |
| 97 |
|
| 98 |
$serialized->warnings = $threadItem->getWarnings(); |
| 99 |
|
| 100 |
$serialized->replies = []; |
| 101 |
foreach ( $threadItem->getReplies() as $reply ) { |
| 102 |
$serialized->replies[] = static::serializeComments( $reply, $root ); |
| 103 |
} |
| 104 |
|
| 105 |
return $serialized; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function testGetTimestampRegexp( |
| 112 |
string $format, string $expected, string $message |
| 113 |
): void { |
| 114 |
$config = static::getJson( "../data/enwiki-config.json" ); |
| 115 |
$data = static::getJson( "../data/enwiki-data.json" ); |
| 116 |
|
| 117 |
$parser = TestingAccessWrapper::newFromObject( |
| 118 |
$this->createParser( $config, $data ) |
| 119 |
); |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
$expected = preg_replace( '/\\\\u([0-9A-F]+)/', '\\\\x{$1}', $expected ); |
| 124 |
$expected = str_replace( ':', '\:', $expected ); |
| 125 |
$expected = '/' . $expected . '/u'; |
| 126 |
|
| 127 |
$result = $parser->getTimestampRegexp( 'en', $format, '\\d', [ 'UTC' => 'UTC' ] ); |
| 128 |
static::assertSame( $expected, $result, $message ); |
| 129 |
} |
| 130 |
|
| 131 |
public static function provideTimestampRegexps(): array { |
| 132 |
return static::getJson( '../cases/timestamp-regex.json' ); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public function testGetTimestampParser( |
| 139 |
string $format, ?array $digits, array $matchData, string $expected, string $message |
| 140 |
): void { |
| 141 |
$config = static::getJson( "../data/enwiki-config.json" ); |
| 142 |
$data = static::getJson( "../data/enwiki-data.json" ); |
| 143 |
|
| 144 |
$parser = TestingAccessWrapper::newFromObject( |
| 145 |
$this->createParser( $config, $data ) |
| 146 |
); |
| 147 |
|
| 148 |
$expected = new DateTimeImmutable( $expected ); |
| 149 |
|
| 150 |
$tsParser = $parser->getTimestampParser( 'en', $format, $digits, 'UTC', [ 'UTC' => 'UTC' ] ); |
| 151 |
static::assertEquals( $expected, $tsParser( $matchData )['date'], $message ); |
| 152 |
} |
| 153 |
|
| 154 |
public static function provideTimestampParser(): array { |
| 155 |
return static::getJson( '../cases/timestamp-parser.json' ); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public function testGetTimestampParserDST( |
| 162 |
string $sample, string $expected, string $expectedUtc, string $format, |
| 163 |
string $timezone, array $timezoneAbbrs, string $message |
| 164 |
): void { |
| 165 |
$config = static::getJson( "../data/enwiki-config.json" ); |
| 166 |
$data = static::getJson( "../data/enwiki-data.json" ); |
| 167 |
|
| 168 |
$parser = TestingAccessWrapper::newFromObject( |
| 169 |
$this->createParser( $config, $data ) |
| 170 |
); |
| 171 |
|
| 172 |
$regexp = $parser->getTimestampRegexp( 'en', $format, '\\d', $timezoneAbbrs ); |
| 173 |
$tsParser = $parser->getTimestampParser( 'en', $format, null, $timezone, $timezoneAbbrs ); |
| 174 |
|
| 175 |
$expected = new DateTimeImmutable( $expected ); |
| 176 |
$expectedUtc = new DateTimeImmutable( $expectedUtc ); |
| 177 |
|
| 178 |
preg_match( $regexp, $sample, $match, PREG_OFFSET_CAPTURE ); |
| 179 |
$date = $tsParser( $match )['date']; |
| 180 |
|
| 181 |
static::assertEquals( $expected, $date, $message ); |
| 182 |
static::assertEquals( $expectedUtc, $date, $message ); |
| 183 |
} |
| 184 |
|
| 185 |
public static function provideTimestampParserDST(): array { |
| 186 |
return static::getJson( '../cases/timestamp-parser-dst.json' ); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public function testGetThreads( |
| 193 |
string $name, string $title, string $dom, string $expected, string $config, string $data |
| 194 |
): void { |
| 195 |
$dom = static::getHtml( $dom ); |
| 196 |
$expectedPath = $expected; |
| 197 |
$expected = static::getJson( $expected ); |
| 198 |
$config = static::getJson( $config ); |
| 199 |
$data = static::getJson( $data ); |
| 200 |
|
| 201 |
$doc = static::createDocument( $dom ); |
| 202 |
$container = static::getThreadContainer( $doc ); |
| 203 |
|
| 204 |
$title = $this->createTitleParser( $config )->parseTitle( $title ); |
| 205 |
$threadItemSet = $this->createParser( $config, $data )->parse( $container, $title ); |
| 206 |
$threads = $threadItemSet->getThreads(); |
| 207 |
|
| 208 |
$processedThreads = []; |
| 209 |
|
| 210 |
foreach ( $threads as $i => $thread ) { |
| 211 |
$thread = static::serializeComments( $thread, $container ); |
| 212 |
$thread = json_decode( json_encode( $thread ), true ); |
| 213 |
$processedThreads[] = $thread; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) { |
| 218 |
static::overwriteJsonFile( $expectedPath, $processedThreads ); |
| 219 |
} |
| 220 |
|
| 221 |
foreach ( $threads as $i => $thread ) { |
| 222 |
static::assertEquals( $expected[$i], $processedThreads[$i], $name . ' section ' . $i ); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
public static function provideComments(): array { |
| 227 |
return static::getJson( '../cases/comments.json' ); |
| 228 |
} |
| 229 |
|
| 230 |
} |