mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/DiscussionTools
synced 2024-11-28 02:00:57 +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
179 lines
4.9 KiB
PHP
179 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Tests;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\CommentModifier;
|
|
use Wikimedia\Parsoid\Utils\DOMCompat;
|
|
|
|
/**
|
|
* @coversDefaultClass \MediaWiki\Extension\DiscussionTools\CommentModifier
|
|
*
|
|
* @group DiscussionTools
|
|
*/
|
|
class CommentModifierTest extends IntegrationTestCase {
|
|
|
|
/**
|
|
* @dataProvider provideAddListItem
|
|
* @covers ::addListItem
|
|
*/
|
|
public function testAddListItem(
|
|
string $name, string $dom, string $expected, string $config, string $data
|
|
) : void {
|
|
$origPath = $dom;
|
|
$dom = self::getHtml( $dom );
|
|
$expectedPath = $expected;
|
|
$expected = self::getHtml( $expected );
|
|
$config = self::getJson( $config );
|
|
$data = self::getJson( $data );
|
|
|
|
$this->setupEnv( $config, $data );
|
|
|
|
$doc = self::createDocument( $dom );
|
|
$container = $doc->getElementsByTagName( 'body' )->item( 0 );
|
|
|
|
$parser = self::createParser( $container, $data );
|
|
$comments = $parser->getCommentItems();
|
|
|
|
$nodes = [];
|
|
foreach ( $comments as $comment ) {
|
|
$node = CommentModifier::addListItem( $comment );
|
|
$node->textContent = 'Reply to ' . $comment->getId();
|
|
$nodes[] = $node;
|
|
}
|
|
|
|
$expectedDoc = self::createDocument( $expected );
|
|
|
|
// Optionally write updated content to the "modified HTML" files
|
|
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
|
|
self::overwriteHtmlFile( $expectedPath, $doc, $origPath );
|
|
}
|
|
|
|
// saveHtml is not dirty-diff safe, but for testing it is probably faster than DOMCompat::getOuterHTML
|
|
self::assertEquals( $expectedDoc->saveHtml(), $doc->saveHtml(), $name );
|
|
|
|
// removeAddedListItem is not implemented on the server
|
|
}
|
|
|
|
public function provideAddListItem() : array {
|
|
return self::getJson( '../cases/modified.json' );
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideAddReplyLink
|
|
* @covers ::addReplyLink
|
|
*/
|
|
public function testAddReplyLink(
|
|
string $name, string $dom, string $expected, string $config, string $data
|
|
) : void {
|
|
$origPath = $dom;
|
|
$dom = self::getHtml( $dom );
|
|
$expectedPath = $expected;
|
|
$expected = self::getHtml( $expected );
|
|
$config = self::getJson( $config );
|
|
$data = self::getJson( $data );
|
|
|
|
$this->setupEnv( $config, $data );
|
|
|
|
$doc = self::createDocument( $dom );
|
|
$container = $doc->getElementsByTagName( 'body' )->item( 0 );
|
|
|
|
$parser = self::createParser( $container, $data );
|
|
$comments = $parser->getCommentItems();
|
|
|
|
foreach ( $comments as $comment ) {
|
|
$linkNode = $doc->createElement( 'a' );
|
|
$linkNode->nodeValue = 'Reply';
|
|
$linkNode->setAttribute( 'href', '#' );
|
|
CommentModifier::addReplyLink( $comment, $linkNode );
|
|
}
|
|
|
|
$expectedDoc = self::createDocument( $expected );
|
|
|
|
// Optionally write updated content to the "reply HTML" files
|
|
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
|
|
self::overwriteHtmlFile( $expectedPath, $doc, $origPath );
|
|
}
|
|
|
|
// saveHtml is not dirty-diff safe, but for testing it is probably faster than DOMCompat::getOuterHTML
|
|
self::assertEquals( $expectedDoc->saveHtml(), $doc->saveHtml(), $name );
|
|
}
|
|
|
|
public function provideAddReplyLink() : array {
|
|
return self::getJson( '../cases/reply.json' );
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideUnwrapList
|
|
* @covers ::unwrapList
|
|
*/
|
|
public function testUnwrapList( string $name, string $html, int $index, string $expected ) : void {
|
|
$doc = self::createDocument( '' );
|
|
$container = $doc->createElement( 'div' );
|
|
|
|
DOMCompat::setInnerHTML( $container, $html );
|
|
CommentModifier::unwrapList( $container->childNodes[$index] );
|
|
|
|
self::assertEquals( $expected, DOMCompat::getInnerHTML( $container ) );
|
|
}
|
|
|
|
public function provideUnwrapList() : array {
|
|
return self::getJson( '../cases/unwrap.json' );
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideIsWikitextSigned
|
|
* @covers ::isWikitextSigned
|
|
*/
|
|
public function testIsWikitextSigned(
|
|
string $msg, string $wikitext, bool $expected
|
|
) : void {
|
|
self::assertEquals(
|
|
$expected,
|
|
CommentModifier::isWikitextSigned( $wikitext ),
|
|
$msg
|
|
);
|
|
}
|
|
|
|
public function provideIsWikitextSigned() : array {
|
|
return self::getJson( '../cases/isWikitextSigned.json' );
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideIsHtmlSigned
|
|
* @covers ::isHtmlSigned
|
|
*/
|
|
public function testIsHtmlSigned(
|
|
string $msg, string $html, bool $expected
|
|
) : void {
|
|
$doc = self::createDocument( '' );
|
|
$container = $doc->createElement( 'div' );
|
|
DOMCompat::setInnerHTML( $container, $html );
|
|
|
|
self::assertEquals(
|
|
$expected,
|
|
CommentModifier::isHtmlSigned( $container ),
|
|
$msg
|
|
);
|
|
}
|
|
|
|
public function provideIsHtmlSigned() : array {
|
|
return self::getJson( '../cases/isHtmlSigned.json' );
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideSanitizeWikitextLinebreaks
|
|
* @covers ::sanitizeWikitextLinebreaks
|
|
*/
|
|
public function testSanitizeWikitextLinebreaks( string $msg, string $wikitext, string $expected ) : void {
|
|
self::assertEquals(
|
|
$expected,
|
|
CommentModifier::sanitizeWikitextLinebreaks( $wikitext ),
|
|
$msg
|
|
);
|
|
}
|
|
|
|
public function provideSanitizeWikitextLinebreaks() : array {
|
|
return self::getJson( '../cases/sanitize-wikitext-linebreaks.json' );
|
|
}
|
|
}
|