mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/DiscussionTools
synced 2024-11-28 02:00:57 +00:00
5203d30ea6
For compatibility with Parsoid's document abstraction (Parsoid may switch to an alternate DOM library in the future), don't explicitly create a new document object using `new DOMDocument`; instead use the Parsoid wrapper `DOMCompat::newDocument()`. This ensures that the Document object created will be compatible with Parsoid. There are a number of other subtle dependencies on the PHP `dom` extension in DiscussionTools, like explicit `instanceof` tests; those will be tweaked in a follow-up patch (I3c4f41c3819770f85d68157c9f690d650b7266a3) since they do not affect correctness so long as Parsoid is aliasing Document to a subclass of the built-in DOMDocument. Similarly, the Phan warnings we suppress do not cause runtime errors (because of the fixes included in c5265341afd9efde6b54ba56dc009aab88eff83c) but phan will be happier once the follow-up patch lands and aligns all the DOM types. Bug: T287611 Depends-On: If0671255779571a91d3472a9d90d0f2d69dd1f7d Change-Id: Ib98bd5b76de7a0d32a29840d1ce04379c72ef486
58 lines
1.8 KiB
PHP
58 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 = self::getHtml( $htmlPath );
|
|
// Slightly awkward to get the same output as in the JS version
|
|
$fragment = ( DOMCompat::newDocument( true ) )->createDocumentFragment();
|
|
$fragment->appendXML( trim( $html ) );
|
|
$expected = self::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' ) ) {
|
|
self::overwriteJsonFile( $expectedPath, $actual );
|
|
}
|
|
|
|
self::assertEquals( $expected, $actual, $name );
|
|
|
|
$expectedBackwards = array_map( static function ( $a ) {
|
|
return ( substr( $a, 0, 5 ) === 'enter' ? 'leave' : 'enter' ) . substr( $a, 5 );
|
|
}, array_reverse( $expected ) );
|
|
self::assertEquals( $expectedBackwards, $actualBackwards, $name . ' (backwards)' );
|
|
}
|
|
|
|
public function provideLinearWalk(): array {
|
|
return self::getJson( '../cases/linearWalk.json' );
|
|
}
|
|
|
|
}
|