diff --git a/RELEASE-NOTES-1.43 b/RELEASE-NOTES-1.43 index 183d949fee6..9ae68da6cc7 100644 --- a/RELEASE-NOTES-1.43 +++ b/RELEASE-NOTES-1.43 @@ -211,6 +211,12 @@ because of Phabricator reports. * SpecialBlock::getTargetAndType, deprecated since 1.36, has been removed. * ApiQueryBlockInfoTrait::addBlockInfoToQuery(), deprecated since 1.42, has been removed. +* Linker::makeHeadline(), Linker::generateTOC(), Linker::tocIndent(), + Linker::tocUnindent(), Linker::tocLine(), Linker::tocLineEnd(), and + Linker::tocList(), deprecated in 1.42, have been removed. +* Linker::formatComment(), Linker::formatLinksInComment(), + Linker::commentBlock(), and Linker::revComment(), deprecated in 1.38, + have been removed. * DummyLinker has been removed. The former DummyLinker parameter to the 'ImageBeforeProduceHTML' hook is now null. * … diff --git a/includes/Hook/ImageBeforeProduceHTMLHook.php b/includes/Hook/ImageBeforeProduceHTMLHook.php index 42b7f6e2285..3d232383aba 100644 --- a/includes/Hook/ImageBeforeProduceHTMLHook.php +++ b/includes/Hook/ImageBeforeProduceHTMLHook.php @@ -21,7 +21,7 @@ interface ImageBeforeProduceHTMLHook { * * @since 1.35 * - * @param mixed $linker Unused (always null) + * @param null $unused Will always be null * @param Title &$title Title object of the image * @param File|false &$file File object, or false if it doesn't exist * @param array &$frameParams Various parameters with special meanings; see documentation in @@ -36,7 +36,7 @@ interface ImageBeforeProduceHTMLHook { * @param string &$widthOption Used by the parser to remember the user preference thumbnailsize * @return bool|void True or no return value to continue or false to skip the default logic */ - public function onImageBeforeProduceHTML( $linker, &$title, &$file, + public function onImageBeforeProduceHTML( $unused, &$title, &$file, &$frameParams, &$handlerParams, &$time, &$res, $parser, &$query, &$widthOption ); } diff --git a/includes/linker/Linker.php b/includes/linker/Linker.php index f24bc0e883c..eb08ceef36d 100644 --- a/includes/linker/Linker.php +++ b/includes/linker/Linker.php @@ -24,7 +24,6 @@ namespace MediaWiki\Linker; use File; use HtmlArmor; -use Language; use MediaTransformError; use MediaTransformOutput; use MediaWiki\Context\ContextSource; @@ -48,7 +47,6 @@ use MediaWiki\User\UserIdentityValue; use MessageLocalizer; use Wikimedia\Assert\Assert; use Wikimedia\IPUtils; -use Wikimedia\Parsoid\Core\TOCData; use Wikimedia\Rdbms\SelectQueryBuilder; use Wikimedia\RemexHtml\Serializer\SerializerNode; use Xml; @@ -332,9 +330,8 @@ class Linker { ) { $title = Title::newFromLinkTarget( $title ); $res = null; - $dummy = null; $hookRunner = new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ); - if ( !$hookRunner->onImageBeforeProduceHTML( $dummy, $title, + if ( !$hookRunner->onImageBeforeProduceHTML( null, $title, // @phan-suppress-next-line PhanTypeMismatchArgument Type mismatch on pass-by-ref args $file, $frameParams, $handlerParams, $time, $res, // @phan-suppress-next-line PhanTypeMismatchArgument Type mismatch on pass-by-ref args @@ -1571,61 +1568,6 @@ class Linker { ); } - /** - * This function is called by all recent changes variants, by the page history, - * and by the user contributions list. It is responsible for formatting edit - * summaries. It escapes any HTML in the summary, but adds some CSS to format - * auto-generated comments (from section editing) and formats [[wikilinks]]. - * - * This method produces HTML that can require CSS styles in mediawiki.interface.helpers.styles. - * - * @since 1.16.3. $wikiId added in 1.26 - * @deprecated since 1.38 use CommentFormatter, hard-deprecated in 1.41 - * - * @param string $comment - * @param LinkTarget|null $title LinkTarget object (to generate link to the section in - * autocomment) or null - * @param bool $local Whether section links should refer to local page - * @param string|null $wikiId Id (as used by WikiMap) of the wiki to generate links to. - * For use with external changes. - * - * @return string HTML - */ - public static function formatComment( - $comment, $title = null, $local = false, $wikiId = null - ) { - wfDeprecated( __METHOD__, '1.41' ); - $formatter = MediaWikiServices::getInstance()->getCommentFormatter(); - return $formatter->format( $comment, $title, $local, $wikiId ); - } - - /** - * Formats wiki links and media links in text; all other wiki formatting - * is ignored - * - * @since 1.16.3. $wikiId added in 1.26 - * @deprecated since 1.38 use CommentFormatter, hard-deprecated in 1.41 - * - * @param string $comment Text to format links in. WARNING! Since the output of this - * function is html, $comment must be sanitized for use as html. You probably want - * to pass $comment through Sanitizer::escapeHtmlAllowEntities() before calling - * this function. - * @param LinkTarget|null $title An optional LinkTarget object used to links to sections - * @param bool $local Whether section links should refer to local page - * @param string|null $wikiId Id of the wiki to link to (if not the local wiki), - * as used by WikiMap. - * - * @return string HTML - * @return-taint onlysafefor_html - */ - public static function formatLinksInComment( - $comment, $title = null, $local = false, $wikiId = null - ) { - wfDeprecated( __METHOD__, '1.41' ); - $formatter = MediaWikiServices::getInstance()->getCommentFormatter(); - return $formatter->formatLinksUnsafe( $comment, $title, $local, $wikiId ); - } - /** * @param LinkTarget|null $contextTitle * @param string $target @@ -1710,60 +1652,6 @@ class Linker { return $ret; } - /** - * Wrap a comment in standard punctuation and formatting if - * it's non-empty, otherwise return empty string. - * - * This method produces HTML that requires CSS styles in mediawiki.interface.helpers.styles. - * - * @since 1.16.3. $wikiId added in 1.26 - * @deprecated since 1.38 use CommentFormatter, hard-deprecated in 1.41 - * - * @param string $comment - * @param LinkTarget|null $title LinkTarget object (to generate link to section in autocomment) - * or null - * @param bool $local Whether section links should refer to local page - * @param string|null $wikiId Id (as used by WikiMap) of the wiki to generate links to. - * For use with external changes. - * @param bool $useParentheses Whether the comment is wrapped in parentheses - * - * @return string - */ - public static function commentBlock( - $comment, $title = null, $local = false, $wikiId = null, $useParentheses = true - ) { - wfDeprecated( __METHOD__, '1.41' ); - return MediaWikiServices::getInstance()->getCommentFormatter() - ->formatBlock( $comment, $title, $local, $wikiId, $useParentheses ); - } - - /** - * Wrap and format the given revision's comment block, if the current - * user is allowed to view it. - * - * This method produces HTML that requires CSS styles in mediawiki.interface.helpers.styles. - * - * @since 1.16.3 - * @deprecated since 1.38 use CommentFormatter, hard-deprecated in 1.41 - * @param RevisionRecord $revRecord (Switched from the old Revision class to RevisionRecord - * since 1.35) - * @param bool $local Whether section links should refer to local page - * @param bool $isPublic Show only if all users can see it - * @param bool $useParentheses (optional) Wrap comments in parentheses where needed - * @return string HTML fragment - */ - public static function revComment( - RevisionRecord $revRecord, - $local = false, - $isPublic = false, - $useParentheses = true - ) { - wfDeprecated( __METHOD__, '1.41' ); - $authority = RequestContext::getMain()->getAuthority(); - $formatter = MediaWikiServices::getInstance()->getCommentFormatter(); - return $formatter->formatRevision( $revRecord, $authority, $local, $isPublic, $useParentheses ); - } - /** * @since 1.16.3 * @param int $size @@ -1778,201 +1666,6 @@ class Linker { return "$stxt"; } - /** - * Add another level to the Table of Contents - * - * @deprecated since 1.42 - * @since 1.16.3 - * @return string - */ - public static function tocIndent() { - wfDeprecated( __METHOD__, '1.42' ); - return "\n\n\n", $level > 0 ? $level : 0 ); - } - - /** - * parameter level defines if we are on an indentation level - * - * @deprecated since 1.42 - * @since 1.16.3 - * @param string $linkAnchor Identifier - * @param string $tocline Properly escaped HTML - * @param string $tocnumber Unescaped text - * @param int $level - * @param string|false $sectionIndex - * @return string - */ - public static function tocLine( $linkAnchor, $tocline, $tocnumber, $level, $sectionIndex = false ) { - wfDeprecated( __METHOD__, '1.42' ); - $classes = "toclevel-$level"; - - // Parser.php used to suppress tocLine by setting $sectionindex to false. - // In those circumstances, we can now encounter '' or a "T-" prefixed index - // for when the section comes from templates. - if ( $sectionIndex !== false && $sectionIndex !== '' && !str_starts_with( $sectionIndex, "T-" ) ) { - $classes .= " tocsection-$sectionIndex"; - } - - //
  • - // $tocnumber $tocline - return Html::openElement( 'li', [ 'class' => $classes ] ) - . Html::rawElement( 'a', - [ 'href' => "#$linkAnchor" ], - Html::element( 'span', [ 'class' => 'tocnumber' ], $tocnumber ) - . ' ' - . Html::rawElement( 'span', [ 'class' => 'toctext' ], $tocline ) - ); - } - - /** - * End a Table Of Contents line. - * tocUnindent() will be used instead if we're ending a line below - * the new level. - * @deprecated since 1.42 - * @since 1.16.3 - * @return string - */ - public static function tocLineEnd() { - wfDeprecated( __METHOD__, '1.42' ); - return "
  • \n"; - } - - /** - * Wraps the TOC in a div with ARIA navigation role and provides the hide/collapse JavaScript. - * - * @deprecated since 1.42 - * @since 1.16.3 - * @param string $toc Html of the Table Of Contents - * @param Language|null $lang Language for the toc title, defaults to user language - * @return string Full html of the TOC - */ - public static function tocList( $toc, Language $lang = null ) { - wfDeprecated( __METHOD__, '1.42' ); - $lang ??= RequestContext::getMain()->getLanguage(); - - $title = wfMessage( 'toc' )->inLanguage( $lang )->escaped(); - - return '' - . $toc - . "\n\n"; - } - - /** - * @internal For use by ParserOutput and API modules - * Generate a table of contents from a section tree. - * - * @deprecated since 1.42 - * @since 1.16.3. $lang added in 1.17. Parameters changed in 1.40. - * @param ?TOCData $tocData Return value of ParserOutput::getSections() - * @param Language|null $lang Language for the toc title, defaults to user language - * @param array $options - * - 'maxtoclevel' Max TOC level to generate - * @return string HTML fragment - */ - public static function generateTOC( ?TOCData $tocData, Language $lang = null, array $options = [] ): string { - wfDeprecated( __METHOD__, '1.42' ); - $toc = ''; - $lastLevel = 0; - $maxTocLevel = $options['maxtoclevel'] ?? null; - if ( $maxTocLevel === null ) { - // Use wiki-configured default - $services = MediaWikiServices::getInstance(); - $config = $services->getMainConfig(); - $maxTocLevel = $config->get( MainConfigNames::MaxTocLevel ); - } - foreach ( ( $tocData ? $tocData->getSections() : [] ) as $section ) { - $tocLevel = $section->tocLevel; - if ( $tocLevel < $maxTocLevel ) { - if ( $tocLevel > $lastLevel ) { - $toc .= self::tocIndent(); - } elseif ( $tocLevel < $lastLevel ) { - if ( $lastLevel < $maxTocLevel ) { - $toc .= self::tocUnindent( - $lastLevel - $tocLevel ); - } else { - $toc .= self::tocLineEnd(); - } - } else { - $toc .= self::tocLineEnd(); - } - - $toc .= self::tocLine( $section->linkAnchor, - $section->line, $section->number, - $tocLevel, $section->index ); - $lastLevel = $tocLevel; - } - } - if ( $lastLevel < $maxTocLevel && $lastLevel > 0 ) { - $toc .= self::tocUnindent( $lastLevel - 1 ); - } - return self::tocList( $toc, $lang ); - } - - /** - * Create a headline for content - * - * @deprecated since 1.42 - * @since 1.16.3 - * @param int $level The level of the headline (1-6) - * @param string $attribs Any attributes for the headline, starting with - * a space and ending with '>' - * This *must* be at least '>' for no attribs - * @param string $anchor The anchor to give the headline (the bit after the #) - * @param string $html HTML for the text of the header - * @param string $link HTML to add for the section edit link - * @param string|false $fallbackAnchor A second, optional anchor to give for - * backward compatibility (false to omit) - * - * @return string HTML headline - */ - public static function makeHeadline( $level, $attribs, $anchor, $html, - $link, $fallbackAnchor = false - ) { - wfDeprecated( __METHOD__, '1.42' ); - $anchorEscaped = htmlspecialchars( $anchor, ENT_COMPAT ); - $fallback = ''; - if ( $fallbackAnchor !== false && $fallbackAnchor !== $anchor ) { - $fallbackAnchor = htmlspecialchars( $fallbackAnchor, ENT_COMPAT ); - $fallback = ""; - } - return "$html" - . $link - . ""; - } - /** * Split a link trail, return the "inside" portion and the remainder of the trail * as a two-element array diff --git a/tests/phpunit/includes/linker/LinkerTest.php b/tests/phpunit/includes/linker/LinkerTest.php index 9be27c19b12..3c91b45f4fe 100644 --- a/tests/phpunit/includes/linker/LinkerTest.php +++ b/tests/phpunit/includes/linker/LinkerTest.php @@ -1,11 +1,9 @@ hideDeprecated( 'MediaWiki\Linker\Linker::formatComment' ); - $conf = new SiteConfiguration(); - $conf->settings = [ - 'wgServer' => [ - 'foowiki' => '//foo.example.org', - ], - 'wgArticlePath' => [ - 'foowiki' => '/foo/$1', - ], - ]; - $conf->suffixes = [ 'wiki' ]; - $this->setMwGlobals( 'wgConf', $conf ); - - $this->overrideConfigValues( [ - MainConfigNames::Script => '/w/index.php', - MainConfigNames::ArticlePath => '/wiki/$1', - MainConfigNames::CapitalLinks => true, - // TODO: update tests when the default changes - MainConfigNames::FragmentMode => [ 'legacy' ], - ] ); - - if ( $title === false ) { - // We need a page title that exists - $title = Title::makeTitle( NS_SPECIAL, 'BlankPage' ); - } - - $this->assertEquals( - $expected, - Linker::formatComment( $comment, $title, $local, $wikiId ) - ); - } - - public static function provideCasesForFormatComment() { - return [ - // Linker::formatComment - [ - 'a<script>b', - 'a */" - ], - [ - '→‎autocomment', - "/* autocomment */", - false, true - ], - [ - 'autocomment', - "/* autocomment */", - null - ], - [ - '', - "/* */", - false, true - ], - [ - '', - "/* */", - null - ], - [ - '[[', - "/* [[ */", - false, true - ], - [ - '[[', - "/* [[ */", - null - ], - [ - "foo →‎[[#_\t_]]", - "foo /* [[#_\t_]] */", - false, true - ], - [ - "foo #_\t_", - "foo /* [[#_\t_]] */", - null - ], - [ - '→‎autocomment', - "/* autocomment */", - false, false - ], - [ - '→‎autocomment', - "/* autocomment */", - false, false, 'foowiki' - ], - // Linker::formatLinksInComment - [ - 'abc link def', - "abc [[link]] def", - ], - [ - 'abc text def', - "abc [[link|text]] def", - ], - [ - 'abc Special:BlankPage def', - "abc [[Special:BlankPage|]] def", - ], - [ - 'abc ąśż def', - "abc [[%C4%85%C5%9B%C5%BC]] def", - ], - [ - 'abc #section def', - "abc [[#section]] def", - ], - [ - 'abc /subpage def', - "abc [[/subpage]] def", - ], - [ - 'abc "evil!" def', - "abc [[\"evil!\"]] def", - ], - [ - 'abc [[<script>very evil</script>]] def', - "abc [[]] def", - ], - [ - 'abc [[|]] def', - "abc [[|]] def", - ], - [ - 'abc link def', - "abc [[link]] def", - false, false - ], - [ - 'abc link def', - "abc [[link]] def", - false, false, 'foowiki' - ], - [ - 'Media:LinkerTest.jpg', - '[[Media:LinkerTest.jpg]]' - ], - [ - 'Special:BlankPage', - '[[:Special:BlankPage]]' - ], - [ - 'linktrail...', - '[[link]]trail...' - ] - ]; - // phpcs:enable - } - - /** - * @covers \MediaWiki\Linker\Linker::formatLinksInComment - * @covers \MediaWiki\CommentFormatter\CommentParser - * @covers \MediaWiki\CommentFormatter\CommentFormatter - * @dataProvider provideCasesForFormatLinksInComment - */ - public function testFormatLinksInComment( $expected, $input, $wiki ) { - $this->hideDeprecated( 'MediaWiki\Linker\Linker::formatLinksInComment' ); - $conf = new SiteConfiguration(); - $conf->settings = [ - 'wgServer' => [ - 'foowiki' => '//foo.example.org' - ], - 'wgArticlePath' => [ - 'foowiki' => '/foo/$1', - ], - ]; - $conf->suffixes = [ 'wiki' ]; - $this->setMwGlobals( 'wgConf', $conf ); - $this->overrideConfigValues( [ - MainConfigNames::Script => '/w/index.php', - MainConfigNames::ArticlePath => '/wiki/$1', - MainConfigNames::CapitalLinks => true, - ] ); - - $this->assertEquals( - $expected, - Linker::formatLinksInComment( $input, Title::newFromText( 'Special:BlankPage' ), false, $wiki ) - ); - } - /** * @covers \MediaWiki\Linker\Linker::generateRollback * @dataProvider provideCasesForRollbackGeneration @@ -575,47 +321,6 @@ class LinkerTest extends MediaWikiLangTestCase { ]; } - public static function provideCasesForFormatLinksInComment() { - return [ - [ - 'foo bar Special:BlankPage', - 'foo bar [[Special:BlankPage]]', - null, - ], - [ - 'Special:BlankPage', - '[[ :Special:BlankPage]]', - null, - ], - [ - ':Special:BlankPage', - '[[::Special:BlankPage]]', - null, - ], - [ - '[[FooSpecial:BlankPage', - '[[Foo[[Special:BlankPage]]', - null, - ], - [ - 'Foo\'bar', - "[[Foo'bar]]", - 'foowiki', - ], - [ - 'foo bar Special:BlankPage', - 'foo bar [[Special:BlankPage]]', - 'foowiki', - ], - [ - 'foo bar Image:Example', - 'foo bar [[Image:Example]]', - 'foowiki', - ], - ]; - // phpcs:enable - } - public static function provideTooltipAndAccesskeyAttribs() { return [ 'Watch no expiry' => [ @@ -653,146 +358,6 @@ class LinkerTest extends MediaWikiLangTestCase { $this->assertEquals( $expected, $result ); } - /** - * @covers \MediaWiki\Linker\Linker::commentBlock - * @dataProvider provideCommentBlock - */ - public function testCommentBlock( - $expected, $comment, $title = null, $local = false, $wikiId = null, $useParentheses = true - ) { - $this->hideDeprecated( 'MediaWiki\Linker\Linker::commentBlock' ); - $conf = new SiteConfiguration(); - $conf->settings = [ - 'wgServer' => [ - 'foowiki' => '//foo.example.org' - ], - 'wgArticlePath' => [ - 'foowiki' => '/foo/$1', - ], - ]; - $conf->suffixes = [ 'wiki' ]; - $this->setMwGlobals( 'wgConf', $conf ); - $this->overrideConfigValues( [ - MainConfigNames::Script => '/w/index.php', - MainConfigNames::ArticlePath => '/wiki/$1', - MainConfigNames::CapitalLinks => true, - ] ); - - $this->assertEquals( $expected, Linker::commentBlock( $comment, $title, $local, $wikiId, $useParentheses ) ); - } - - public static function provideCommentBlock() { - return [ - [ - ' (Test)', - 'Test' - ], - 'Empty comment' => [ '', '' ], - 'Backwards compatibility empty comment' => [ '', '*' ], - 'No parenthesis' => [ - ' Test', - 'Test', - null, false, null, - false - ], - 'Page exist link' => [ - ' (Special:BlankPage)', - '[[Special:BlankPage]]' - ], - 'Page does not exist link' => [ - ' (Test)', - '[[Test]]' - ], - 'Link to other page section' => [ - ' (#Test)', - '[[#Test]]', - Title::makeTitle( NS_SPECIAL, 'BlankPage' ) - ], - '$local is true' => [ - ' (#Test)', - '[[#Test]]', - Title::makeTitle( NS_SPECIAL, 'BlankPage' ), - true - ], - 'Given wikiId' => [ - ' (Test)', - '[[Test]]', - null, false, - 'foowiki' - ], - 'Section link to external wiki page' => [ - ' (#Test)', - '[[#Test]]', - Title::makeTitle( NS_SPECIAL, 'BlankPage' ), - false, - 'foowiki' - ], - ]; - } - - /** - * @covers \MediaWiki\Linker\Linker::revComment - * @dataProvider provideRevComment - */ - public function testRevComment( - string $expected, - bool $isSysop = false, - int $visibility = 0, - bool $local = false, - bool $isPublic = false, - bool $useParentheses = true, - ?string $comment = 'Some comment!' - ) { - $this->hideDeprecated( 'MediaWiki\Linker\Linker::revComment' ); - $pageData = $this->insertPage( 'RevCommentTestPage' ); - $revisionRecord = new MutableRevisionRecord( $pageData['title'] ); - if ( $comment ) { - $revisionRecord->setComment( CommentStoreComment::newUnsavedComment( $comment ) ); - } - $revisionRecord->setVisibility( $visibility ); - - $context = RequestContext::getMain(); - $user = $isSysop ? $this->getTestSysop()->getUser() : $this->getTestUser()->getUser(); - $context->setUser( $user ); - - $this->assertEquals( $expected, Linker::revComment( $revisionRecord, $local, $isPublic, $useParentheses ) ); - } - - public static function provideRevComment() { - return [ - 'Should be visible' => [ - ' (Some comment!)' - ], - 'Should not have parenthesis' => [ - ' Some comment!', - false, 0, false, false, - false - ], - 'Should be empty' => [ - '', - false, 0, false, false, true, - null - ], - 'Deleted comment should not be visible to normal users' => [ - ' (edit summary removed)', - false, - RevisionRecord::DELETED_COMMENT - ], - 'Deleted comment should not be visible to normal users even if public' => [ - ' (edit summary removed)', - false, - RevisionRecord::DELETED_COMMENT, - false, - true - ], - 'Deleted comment should be visible to sysops' => [ - ' (Some comment!)', - true, - RevisionRecord::DELETED_COMMENT - ], - ]; - } - /** * @covers \MediaWiki\Linker\Linker::specialLink * @dataProvider provideSpecialLink diff --git a/tests/phpunit/integration/includes/CommentFormatter/CommentParserTest.php b/tests/phpunit/integration/includes/CommentFormatter/CommentParserTest.php index af55850c136..c2a677c711a 100644 --- a/tests/phpunit/integration/includes/CommentFormatter/CommentParserTest.php +++ b/tests/phpunit/integration/includes/CommentFormatter/CommentParserTest.php @@ -4,10 +4,16 @@ namespace MediaWiki\Tests\Integration\CommentFormatter; use LinkCacheTestTrait; use MediaWiki\Cache\LinkBatchFactory; +use MediaWiki\CommentFormatter\CommentFormatter; use MediaWiki\CommentFormatter\CommentParser; +use MediaWiki\CommentFormatter\CommentParserFactory; +use MediaWiki\CommentStore\CommentStoreComment; use MediaWiki\Config\SiteConfiguration; +use MediaWiki\Context\RequestContext; use MediaWiki\Logger\LoggerFactory; use MediaWiki\MainConfigNames; +use MediaWiki\Revision\MutableRevisionRecord; +use MediaWiki\Revision\RevisionRecord; use MediaWiki\Tests\Unit\DummyServicesTrait; use MediaWiki\Title\Title; use RepoGroup; @@ -45,6 +51,14 @@ class CommentParserTest extends \MediaWikiIntegrationTestCase { ); } + private function getFormatter() { + $parserFactory = $this->createNoOpMock( CommentParserFactory::class, [ 'create' ] ); + $parserFactory->method( 'create' )->willReturnCallback( function () { + return $this->getParser(); + } ); + return new CommentFormatter( $parserFactory ); + } + /** * @before */ @@ -274,8 +288,6 @@ class CommentParserTest extends \MediaWikiIntegrationTestCase { } /** - * Adapted from LinkerTest - * * @dataProvider provideFormatComment */ public function testFormatComment( @@ -322,9 +334,6 @@ class CommentParserTest extends \MediaWikiIntegrationTestCase { $this->assertEquals( $expected, $result ); } - /** - * Adapted from LinkerTest - */ public static function provideFormatLinksInComment() { return [ [ @@ -367,7 +376,88 @@ class CommentParserTest extends \MediaWikiIntegrationTestCase { } /** - * Adapted from LinkerTest. Note that we test the new HTML escaping variant. + * @covers \MediaWiki\CommentFormatter\CommentFormatter + * @covers \MediaWiki\CommentFormatter\CommentParser + * @dataProvider provideCommentBlock + */ + public function testCommentBlock( + $expected, $comment, $title = null, $local = false, $wikiId = null, $useParentheses = true + ) { + $conf = new SiteConfiguration(); + $conf->settings = [ + 'wgServer' => [ + 'foowiki' => '//foo.example.org' + ], + 'wgArticlePath' => [ + 'foowiki' => '/foo/$1', + ], + ]; + $conf->suffixes = [ 'wiki' ]; + $this->setMwGlobals( 'wgConf', $conf ); + $this->overrideConfigValues( [ + MainConfigNames::Script => '/w/index.php', + MainConfigNames::ArticlePath => '/wiki/$1', + MainConfigNames::CapitalLinks => true, + ] ); + + $formatter = $this->getFormatter(); + $this->assertEquals( + $expected, + $formatter->formatBlock( $comment, $title, $local, $wikiId, $useParentheses ) + ); + } + + public static function provideCommentBlock() { + return [ + [ + ' (Test)', + 'Test' + ], + 'Empty comment' => [ '', '' ], + 'Backwards compatibility empty comment' => [ '', '*' ], + 'No parenthesis' => [ + ' Test', + 'Test', + null, false, null, + false + ], + 'Page exist link' => [ + ' (Special:BlankPage)', + '[[Special:BlankPage]]' + ], + 'Page does not exist link' => [ + ' (Test)', + '[[Test]]' + ], + 'Link to other page section' => [ + ' (#Test)', + '[[#Test]]', + Title::makeTitle( NS_SPECIAL, 'BlankPage' ) + ], + '$local is true' => [ + ' (#Test)', + '[[#Test]]', + Title::makeTitle( NS_SPECIAL, 'BlankPage' ), + true + ], + 'Given wikiId' => [ + ' (Test)', + '[[Test]]', + null, false, + 'foowiki' + ], + 'Section link to external wiki page' => [ + ' (#Test)', + '[[#Test]]', + Title::makeTitle( NS_SPECIAL, 'BlankPage' ), + false, + 'foowiki' + ], + ]; + } + + /** + * Note that we test the new HTML escaping variant. * * @dataProvider provideFormatLinksInComment */ @@ -474,4 +564,67 @@ class CommentParserTest extends \MediaWikiIntegrationTestCase { ); } + /** + * @dataProvider provideRevComment + */ + public function testRevComment( + string $expected, + bool $isSysop = false, + int $visibility = 0, + bool $local = false, + bool $isPublic = false, + bool $useParentheses = true, + ?string $comment = 'Some comment!' + ) { + $pageData = $this->insertPage( 'RevCommentTestPage' ); + $revisionRecord = new MutableRevisionRecord( $pageData['title'] ); + if ( $comment ) { + $revisionRecord->setComment( CommentStoreComment::newUnsavedComment( $comment ) ); + } + $revisionRecord->setVisibility( $visibility ); + + $context = RequestContext::getMain(); + $user = $isSysop ? $this->getTestSysop()->getUser() : $this->getTestUser()->getUser(); + $context->setUser( $user ); + + $formatter = $this->getFormatter(); + $authority = RequestContext::getMain()->getAuthority(); + $this->assertEquals( $expected, $formatter->formatRevision( $revisionRecord, $authority, $local, $isPublic, $useParentheses ) ); + } + + public static function provideRevComment() { + return [ + 'Should be visible' => [ + ' (Some comment!)' + ], + 'Should not have parenthesis' => [ + ' Some comment!', + false, 0, false, false, + false + ], + 'Should be empty' => [ + '', + false, 0, false, false, true, + null + ], + 'Deleted comment should not be visible to normal users' => [ + ' (edit summary removed)', + false, + RevisionRecord::DELETED_COMMENT + ], + 'Deleted comment should not be visible to normal users even if public' => [ + ' (edit summary removed)', + false, + RevisionRecord::DELETED_COMMENT, + false, + true + ], + 'Deleted comment should be visible to sysops' => [ + ' (Some comment!)', + true, + RevisionRecord::DELETED_COMMENT + ], + ]; + } + }