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 ) {
|
2022-06-09 13:51:33 +00:00
|
|
|
$html = static::getHtml( $htmlPath );
|
2021-01-08 19:20:33 +00:00
|
|
|
// 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 ) );
|
2022-06-09 13:51:33 +00:00
|
|
|
$expected = static::getJson( $expectedPath );
|
2021-01-08 19:20:33 +00:00
|
|
|
|
|
|
|
$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' ) ) {
|
2022-06-09 13:51:33 +00:00
|
|
|
static::overwriteJsonFile( $expectedPath, $actual );
|
2021-01-08 19:20:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 13:51:33 +00:00
|
|
|
static::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 ) );
|
2022-06-09 13:51:33 +00:00
|
|
|
static::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 {
|
2022-06-09 13:51:33 +00:00
|
|
|
return static::getJson( '../cases/linearWalk.json' );
|
2021-01-08 19:20:33 +00:00
|
|
|
}
|
|
|
|
}
|