mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/DiscussionTools
synced 2024-12-18 11:02:57 +00:00
af54bae2ec
While in many cases the class will never be sub-classed, it's easier just to always use static:: and not worry about predicting which classes might have problems in the future. Change-Id: I23072a1701b5acf62bb3379a877de97627d8fcf3
57 lines
1.8 KiB
PHP
57 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Tests\Unit;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\CommentUtils;
|
|
use MediaWiki\Extension\DiscussionTools\Tests\TestUtils;
|
|
use MediaWikiUnitTestCase;
|
|
use Wikimedia\Parsoid\Utils\DOMCompat;
|
|
|
|
/**
|
|
* @coversDefaultClass \MediaWiki\Extension\DiscussionTools\CommentUtils
|
|
*
|
|
* @group DiscussionTools
|
|
*/
|
|
class CommentUtilsTest extends MediaWikiUnitTestCase {
|
|
|
|
use TestUtils;
|
|
|
|
/**
|
|
* @dataProvider provideLinearWalk
|
|
* @covers ::linearWalk
|
|
*/
|
|
public function testLinearWalk( string $name, string $htmlPath, string $expectedPath ) {
|
|
$html = static::getHtml( $htmlPath );
|
|
// Slightly awkward to get the same output as in the JS version
|
|
$fragment = ( DOMCompat::newDocument( true ) )->createDocumentFragment();
|
|
$fragment->appendXML( trim( $html ) );
|
|
$expected = static::getJson( $expectedPath );
|
|
|
|
$actual = [];
|
|
CommentUtils::linearWalk( $fragment, static function ( $event, $node ) use ( &$actual ) {
|
|
$actual[] = "$event {$node->nodeName}({$node->nodeType})";
|
|
} );
|
|
|
|
$actualBackwards = [];
|
|
CommentUtils::linearWalkBackwards( $fragment, static function ( $event, $node ) use ( &$actualBackwards ) {
|
|
$actualBackwards[] = "$event {$node->nodeName}({$node->nodeType})";
|
|
} );
|
|
|
|
// Optionally write updated content to the JSON files
|
|
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
|
|
static::overwriteJsonFile( $expectedPath, $actual );
|
|
}
|
|
|
|
static::assertEquals( $expected, $actual, $name );
|
|
|
|
$expectedBackwards = array_map( static function ( $a ) {
|
|
return ( substr( $a, 0, 5 ) === 'enter' ? 'leave' : 'enter' ) . substr( $a, 5 );
|
|
}, array_reverse( $expected ) );
|
|
static::assertEquals( $expectedBackwards, $actualBackwards, $name . ' (backwards)' );
|
|
}
|
|
|
|
public function provideLinearWalk(): array {
|
|
return static::getJson( '../cases/linearWalk.json' );
|
|
}
|
|
}
|