2021-01-08 19:20:33 +00:00
|
|
|
<?php
|
|
|
|
|
2021-02-02 14:12:51 +00:00
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Tests\Unit;
|
2021-01-08 19:20:33 +00:00
|
|
|
|
|
|
|
use MediaWiki\Extension\DiscussionTools\CommentUtils;
|
2021-02-02 14:12:51 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\Tests\TestUtils;
|
|
|
|
use MediaWikiUnitTestCase;
|
2021-07-29 02:13:09 +00:00
|
|
|
use Wikimedia\Parsoid\Utils\DOMCompat;
|
2021-01-08 19:20:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @coversDefaultClass \MediaWiki\Extension\DiscussionTools\CommentUtils
|
|
|
|
*
|
|
|
|
* @group DiscussionTools
|
|
|
|
*/
|
2021-02-02 14:12:51 +00:00
|
|
|
class CommentUtilsTest extends MediaWikiUnitTestCase {
|
|
|
|
|
|
|
|
use TestUtils;
|
2021-01-08 19:20:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideLinearWalk
|
|
|
|
* @covers ::linearWalk
|
|
|
|
*/
|
|
|
|
public function testLinearWalk( string $name, string $htmlPath, string $expectedPath ) {
|
|
|
|
$html = self::getHtml( $htmlPath );
|
|
|
|
// Slightly awkward to get the same output as in the JS version
|
2021-07-29 02:13:09 +00:00
|
|
|
$fragment = ( DOMCompat::newDocument( true ) )->createDocumentFragment();
|
2021-01-08 19:20:33 +00:00
|
|
|
$fragment->appendXML( trim( $html ) );
|
|
|
|
$expected = self::getJson( $expectedPath );
|
|
|
|
|
|
|
|
$actual = [];
|
2021-05-05 06:59:38 +00:00
|
|
|
CommentUtils::linearWalk( $fragment, static function ( $event, $node ) use ( &$actual ) {
|
2021-01-08 19:20:33 +00:00
|
|
|
$actual[] = "$event {$node->nodeName}({$node->nodeType})";
|
|
|
|
} );
|
|
|
|
|
2021-01-26 18:58:58 +00:00
|
|
|
$actualBackwards = [];
|
2021-05-05 06:59:38 +00:00
|
|
|
CommentUtils::linearWalkBackwards( $fragment, static function ( $event, $node ) use ( &$actualBackwards ) {
|
2021-01-26 18:58:58 +00:00
|
|
|
$actualBackwards[] = "$event {$node->nodeName}({$node->nodeType})";
|
|
|
|
} );
|
|
|
|
|
2021-01-08 19:20:33 +00:00
|
|
|
// Optionally write updated content to the JSON files
|
|
|
|
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
|
|
|
|
self::overwriteJsonFile( $expectedPath, $actual );
|
|
|
|
}
|
|
|
|
|
|
|
|
self::assertEquals( $expected, $actual, $name );
|
2021-01-26 18:58:58 +00:00
|
|
|
|
2021-05-05 06:59:38 +00:00
|
|
|
$expectedBackwards = array_map( static function ( $a ) {
|
2021-01-26 18:58:58 +00:00
|
|
|
return ( substr( $a, 0, 5 ) === 'enter' ? 'leave' : 'enter' ) . substr( $a, 5 );
|
|
|
|
}, array_reverse( $expected ) );
|
|
|
|
self::assertEquals( $expectedBackwards, $actualBackwards, $name . ' (backwards)' );
|
2021-01-08 19:20:33 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:25:13 +00:00
|
|
|
public function provideLinearWalk(): array {
|
2021-01-08 19:20:33 +00:00
|
|
|
return self::getJson( '../cases/linearWalk.json' );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|