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-01-08 19:20:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DiscussionTools
|
2022-09-15 12:08:30 +00:00
|
|
|
* @covers \MediaWiki\Extension\DiscussionTools\CommentUtils
|
2021-01-08 19:20:33 +00:00
|
|
|
*/
|
2021-02-02 14:12:51 +00:00
|
|
|
class CommentUtilsTest extends MediaWikiUnitTestCase {
|
|
|
|
|
|
|
|
use TestUtils;
|
2021-01-08 19:20:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideLinearWalk
|
|
|
|
*/
|
|
|
|
public function testLinearWalk( string $name, string $htmlPath, string $expectedPath ) {
|
2022-06-09 13:51:33 +00:00
|
|
|
$html = static::getHtml( $htmlPath );
|
2022-10-12 20:03:23 +00:00
|
|
|
$doc = static::createDocument( $html );
|
2022-06-09 13:51:33 +00:00
|
|
|
$expected = static::getJson( $expectedPath );
|
2021-01-08 19:20:33 +00:00
|
|
|
|
|
|
|
$actual = [];
|
2022-10-12 20:03:23 +00:00
|
|
|
CommentUtils::linearWalk( $doc, 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 = [];
|
2022-10-12 20:03:23 +00:00
|
|
|
CommentUtils::linearWalkBackwards( $doc, 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
|
|
|
}
|
|
|
|
}
|