mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/DiscussionTools
synced 2024-11-16 20:58:28 +00:00
1c3fada1fb
Documentation: https://www.mediawiki.org/wiki/Manual:PHP_unit_testing/Writing_unit_tests_for_extensions#Two_types_of_tests We can do this because the tested methods do not depend on any globals or on MediaWiki being installed. In addition to being the new hotness, MediaWikiUnitTestCase allows the test classes that use it instead of MediaWikiTestCase to start up much faster. In my testing, running this test case individually now takes 0.35s, compared to 1.1s before. Try: * With new code: time php tests/phpunit/phpunit.php extensions/DiscussionTools/tests/phpunit/unit/CommentUtilsTest.php * With old code: time php tests/phpunit/phpunit.php extensions/DiscussionTools/tests/phpunit/CommentUtilsTest.php Change-Id: I771b1f3d101a394ee869e42547d9ae7839397752
93 lines
2.6 KiB
PHP
93 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Tests;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\CommentItem;
|
|
use MediaWiki\Extension\DiscussionTools\CommentUtils;
|
|
use MediaWiki\Extension\DiscussionTools\HeadingItem;
|
|
use MediaWiki\Extension\DiscussionTools\ImmutableRange;
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem;
|
|
|
|
/**
|
|
* @coversDefaultClass \MediaWiki\Extension\DiscussionTools\ThreadItem
|
|
*
|
|
* @group DiscussionTools
|
|
*/
|
|
class ThreadItemTest extends IntegrationTestCase {
|
|
/**
|
|
* @dataProvider provideAuthors
|
|
* @covers ::getAuthorsBelow
|
|
*/
|
|
public function testGetAuthorsBelow( array $thread, array $expected ) : void {
|
|
$doc = $this->createDocument( '' );
|
|
$node = $doc->createElement( 'div' );
|
|
$range = new ImmutableRange( $node, 0, $node, 0 );
|
|
|
|
$makeThreadItem = function ( array $arr ) use ( &$makeThreadItem, $range ) : ThreadItem {
|
|
if ( $arr['type'] === 'comment' ) {
|
|
$item = new CommentItem( 1, $range, [], 'TIMESTAMP', $arr['author'] );
|
|
} else {
|
|
$item = new HeadingItem( $range, 2 );
|
|
}
|
|
foreach ( $arr['replies'] as $reply ) {
|
|
$item->addReply( $makeThreadItem( $reply ) );
|
|
}
|
|
return $item;
|
|
};
|
|
|
|
$threadItem = $makeThreadItem( $thread );
|
|
|
|
self::assertEquals( $expected, $threadItem->getAuthorsBelow() );
|
|
}
|
|
|
|
public function provideAuthors() : array {
|
|
return self::getJson( '../cases/authors.json' );
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideTranscludedFrom
|
|
* @covers ::getTranscludedFrom
|
|
* @covers \MediaWiki\Extension\DiscussionTools\CommentUtils::unwrapParsoidSections
|
|
*/
|
|
public function testGetTranscludedFrom(
|
|
string $name, string $dom, string $expected, string $config, string $data
|
|
) : void {
|
|
$dom = self::getHtml( $dom );
|
|
$expectedPath = $expected;
|
|
$expected = self::getJson( $expected );
|
|
$config = self::getJson( $config );
|
|
$data = self::getJson( $data );
|
|
|
|
$this->setupEnv( $config, $data );
|
|
|
|
$doc = self::createDocument( $dom );
|
|
$container = $doc->getElementsByTagName( 'body' )->item( 0 );
|
|
|
|
CommentUtils::unwrapParsoidSections( $container );
|
|
|
|
$parser = self::createParser( $container, $data );
|
|
$comments = $parser->getCommentItems();
|
|
|
|
$transcludedFrom = [];
|
|
foreach ( $comments as $comment ) {
|
|
$transcludedFrom[ $comment->getId() ] = $comment->getTranscludedFrom();
|
|
}
|
|
|
|
// Optionally write updated content to the JSON files
|
|
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
|
|
self::overwriteJsonFile( $expectedPath, $transcludedFrom );
|
|
}
|
|
|
|
self::assertEquals(
|
|
$expected,
|
|
$transcludedFrom,
|
|
$name
|
|
);
|
|
}
|
|
|
|
public function provideTranscludedFrom() : array {
|
|
return self::getJson( '../cases/transcluded.json' );
|
|
}
|
|
|
|
}
|