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 DOMDocument;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\CommentUtils;
|
2021-02-02 14:12:51 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\Tests\TestUtils;
|
|
|
|
use MediaWikiUnitTestCase;
|
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
|
|
|
|
$fragment = ( new DOMDocument() )->createDocumentFragment();
|
|
|
|
$fragment->appendXML( trim( $html ) );
|
|
|
|
$expected = self::getJson( $expectedPath );
|
|
|
|
|
|
|
|
$actual = [];
|
|
|
|
CommentUtils::linearWalk( $fragment, function ( $event, $node ) use ( &$actual ) {
|
|
|
|
$actual[] = "$event {$node->nodeName}({$node->nodeType})";
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Optionally write updated content to the JSON files
|
|
|
|
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
|
|
|
|
self::overwriteJsonFile( $expectedPath, $actual );
|
|
|
|
}
|
|
|
|
|
|
|
|
self::assertEquals( $expected, $actual, $name );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideLinearWalk() : array {
|
|
|
|
return self::getJson( '../cases/linearWalk.json' );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|