| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\DiscussionTools\Tests\Unit; |
| 4 |
|
| 5 |
use MediaWiki\Extension\DiscussionTools\CommentUtils; |
| 6 |
use MediaWiki\Extension\DiscussionTools\Tests\TestUtils; |
| 7 |
use MediaWikiUnitTestCase; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class CommentUtilsTest extends MediaWikiUnitTestCase { |
| 14 |
|
| 15 |
use TestUtils; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public function testLinearWalk( string $name, string $htmlPath, string $expectedPath ) { |
| 21 |
$html = static::getHtml( $htmlPath ); |
| 22 |
$doc = static::createDocument( $html ); |
| 23 |
$expected = static::getJson( $expectedPath ); |
| 24 |
|
| 25 |
$actual = []; |
| 26 |
CommentUtils::linearWalk( $doc, static function ( $event, $node ) use ( &$actual ) { |
| 27 |
|
| 28 |
$actual[] = $event . ' ' . strtolower( $node->nodeName ); |
| 29 |
} ); |
| 30 |
|
| 31 |
$actualBackwards = []; |
| 32 |
CommentUtils::linearWalkBackwards( $doc, static function ( $event, $node ) use ( &$actualBackwards ) { |
| 33 |
$actualBackwards[] = $event . ' ' . strtolower( $node->nodeName ); |
| 34 |
} ); |
| 35 |
|
| 36 |
|
| 37 |
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) { |
| 38 |
static::overwriteJsonFile( $expectedPath, $actual ); |
| 39 |
} |
| 40 |
|
| 41 |
static::assertEquals( $expected, $actual, $name ); |
| 42 |
|
| 43 |
$expectedBackwards = array_map( static function ( $a ) { |
| 44 |
return ( substr( $a, 0, 5 ) === 'enter' ? 'leave' : 'enter' ) . substr( $a, 5 ); |
| 45 |
}, array_reverse( $expected ) ); |
| 46 |
static::assertEquals( $expectedBackwards, $actualBackwards, $name . ' (backwards)' ); |
| 47 |
} |
| 48 |
|
| 49 |
public static function provideLinearWalk(): array { |
| 50 |
return static::getJson( '../cases/linearWalk.json' ); |
| 51 |
} |
| 52 |
} |