| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\DiscussionTools\Tests; |
| 4 |
|
| 5 |
use MediaWiki\Extension\DiscussionTools\CommentModifier; |
| 6 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 7 |
use Wikimedia\Parsoid\Utils\DOMUtils; |
| 8 |
use Wikimedia\Parsoid\Wt2Html\XMLSerializer; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class CommentModifierTest extends IntegrationTestCase { |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public function testAddListItem( |
| 21 |
string $name, string $title, string $dom, string $expected, string $config, string $data, |
| 22 |
string $replyIndentation = 'invisible' |
| 23 |
): void { |
| 24 |
$origPath = $dom; |
| 25 |
$dom = static::getHtml( $dom ); |
| 26 |
$expectedPath = $expected; |
| 27 |
$expected = static::getHtml( $expected ); |
| 28 |
$config = static::getJson( $config ); |
| 29 |
$data = static::getJson( $data ); |
| 30 |
|
| 31 |
$title = $this->createTitleParser( $config )->parseTitle( $title ); |
| 32 |
|
| 33 |
$doc = static::createDocument( $dom ); |
| 34 |
$container = static::getThreadContainer( $doc ); |
| 35 |
|
| 36 |
$threadItemSet = $this->createParser( $config, $data )->parse( $container, $title ); |
| 37 |
$comments = $threadItemSet->getCommentItems(); |
| 38 |
|
| 39 |
foreach ( $comments as $comment ) { |
| 40 |
if ( $comment->getTranscludedFrom() ) { |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
continue; |
| 45 |
} |
| 46 |
$node = CommentModifier::addListItem( $comment, $replyIndentation ); |
| 47 |
$node->textContent = 'Reply to ' . $comment->getId(); |
| 48 |
} |
| 49 |
|
| 50 |
$expectedDoc = static::createDocument( $expected ); |
| 51 |
|
| 52 |
|
| 53 |
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) { |
| 54 |
static::overwriteHtmlFile( $expectedPath, $container, $origPath ); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
static::assertEquals( $expectedDoc->saveHtml(), $doc->saveHtml(), $name ); |
| 59 |
|
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
public static function provideAddListItem(): array { |
| 64 |
return static::getJson( '../cases/modified.json' ); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public function testAddReplyLink( |
| 71 |
string $name, string $title, string $dom, string $expected, string $config, string $data |
| 72 |
): void { |
| 73 |
$origPath = $dom; |
| 74 |
$dom = static::getHtml( $dom ); |
| 75 |
$expectedPath = $expected; |
| 76 |
$expected = static::getHtml( $expected ); |
| 77 |
$config = static::getJson( $config ); |
| 78 |
$data = static::getJson( $data ); |
| 79 |
|
| 80 |
$title = $this->createTitleParser( $config )->parseTitle( $title ); |
| 81 |
|
| 82 |
$doc = static::createDocument( $dom ); |
| 83 |
$container = static::getThreadContainer( $doc ); |
| 84 |
|
| 85 |
$threadItemSet = $this->createParser( $config, $data )->parse( $container, $title ); |
| 86 |
$comments = $threadItemSet->getCommentItems(); |
| 87 |
|
| 88 |
foreach ( $comments as $comment ) { |
| 89 |
$linkNode = $doc->createElement( 'a' ); |
| 90 |
$linkNode->nodeValue = 'Reply'; |
| 91 |
$linkNode->setAttribute( 'href', '#' ); |
| 92 |
CommentModifier::addReplyLink( $comment, $linkNode ); |
| 93 |
} |
| 94 |
|
| 95 |
$expectedDoc = static::createDocument( $expected ); |
| 96 |
|
| 97 |
|
| 98 |
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) { |
| 99 |
static::overwriteHtmlFile( $expectedPath, $container, $origPath ); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
static::assertEquals( $expectedDoc->saveHtml(), $doc->saveHtml(), $name ); |
| 104 |
} |
| 105 |
|
| 106 |
public static function provideAddReplyLink(): array { |
| 107 |
return static::getJson( '../cases/reply.json' ); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public function testUnwrapList( string $name, string $html, int $index, string $expected ): void { |
| 114 |
$doc = static::createDocument( '' ); |
| 115 |
$container = $doc->createElement( 'div' ); |
| 116 |
|
| 117 |
DOMCompat::setInnerHTML( $container, $html ); |
| 118 |
CommentModifier::unwrapList( $container->childNodes[$index] ); |
| 119 |
|
| 120 |
static::assertEquals( $expected, DOMCompat::getInnerHTML( $container ) ); |
| 121 |
} |
| 122 |
|
| 123 |
public static function provideUnwrapList(): array { |
| 124 |
return static::getJson( '../cases/unwrap.json' ); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public function testUnwrapFragment( string $html, string $expected ): void { |
| 131 |
$doc = static::createDocument( '' ); |
| 132 |
$container = DOMUtils::parseHTMLToFragment( $doc, $html ); |
| 133 |
CommentModifier::unwrapFragment( $container ); |
| 134 |
static::assertEquals( $expected, DOMUtils::getFragmentInnerHTML( $container ) ); |
| 135 |
} |
| 136 |
|
| 137 |
public static function provideUnwrapFragment() { |
| 138 |
yield [ |
| 139 |
"<ul><li>aaa</li></ul>\n<dl><dd>bbb</dd></dl>", |
| 140 |
"<p>aaa</p>\n<p>bbb</p>", |
| 141 |
]; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public function testAppendSignature( |
| 148 |
string $msg, string $html, string $expected |
| 149 |
): void { |
| 150 |
$doc = static::createDocument( '' ); |
| 151 |
$container = DOMUtils::parseHTMLToFragment( $doc, $html ); |
| 152 |
|
| 153 |
CommentModifier::appendSignature( $container, ' ~~~~' ); |
| 154 |
|
| 155 |
static::assertEquals( |
| 156 |
$expected, |
| 157 |
XMLSerializer::serialize( $container, [ 'innerXML' => true, 'smartQuote' => false ] )['html'], |
| 158 |
$msg |
| 159 |
); |
| 160 |
} |
| 161 |
|
| 162 |
public static function provideAppendSignature(): array { |
| 163 |
return static::getJson( '../cases/appendSignature.json' ); |
| 164 |
} |
| 165 |
|
| 166 |
public function testAppendSignatureWikitext(): void { |
| 167 |
static::assertEquals( |
| 168 |
'Foo bar ~~~~', |
| 169 |
CommentModifier::appendSignatureWikitext( 'Foo bar', ' ~~~~' ), |
| 170 |
'Simple message' |
| 171 |
); |
| 172 |
static::assertEquals( |
| 173 |
"Foo bar\n*A\n*B\n~~~~", |
| 174 |
CommentModifier::appendSignatureWikitext( "Foo bar\n*A\n*B", ' ~~~~' ), |
| 175 |
'List' |
| 176 |
); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public function testSanitizeWikitextLinebreaks( string $msg, string $wikitext, string $expected ): void { |
| 183 |
static::assertEquals( |
| 184 |
$expected, |
| 185 |
CommentModifier::sanitizeWikitextLinebreaks( $wikitext ), |
| 186 |
$msg |
| 187 |
); |
| 188 |
} |
| 189 |
|
| 190 |
public static function provideSanitizeWikitextLinebreaks(): array { |
| 191 |
return static::getJson( '../cases/sanitize-wikitext-linebreaks.json' ); |
| 192 |
} |
| 193 |
} |