wiki.techinc.nl/tests/phpunit/integration/includes/CommentFormatter/RowCommentFormatterTest.php
Tim Starling f7f84dddb3 Introduce CommentFormatter
CommentParser:

* Move comment formatting backend from Linker to a CommentParser service.
  Allow link existence and file existence to be batched.
* Rename $local to $samePage since I think that is clearer.
* Rename $title to $selfLinkTarget since it was unclear what the title
  was used for.
* Rename the "autocomment" concept to "section link" in public
  interfaces, although the old term remains in CSS classes.
* Keep unsafe HTML pass-through in separate "unsafe" methods, for easier
  static analysis and code review.

CommentFormatter:

* Add CommentFormatter and RowCommentFormatter services as a usable
  frontend for comment batches, and to replace the Linker static methods.
* Provide fluent and parametric interfaces.

Linker:

* Remove Linker::makeCommentLink() without deprecation -- nothing calls
  it and it is obviously an internal helper.
* Soft-deprecate Linker methods formatComment(), formatLinksInComment(),
  commentBlock() and revComment().

Caller migration:

* CommentFormatter single: Linker, RollbackAction, ApiComparePages,
  ApiParse
* CommentFormatter parametric batch: ImageHistoryPseudoPager
* CommentFormatter fluent batch: ApiQueryFilearchive
* RowCommentFormatter sequential: History feed, BlocklistPager,
  ProtectedPagesPager, ApiQueryProtectedTitles
* RowCommentFormatter with index: ChangesFeed, ChangesList,
  ApiQueryDeletedrevs, ApiQueryLogEvents, ApiQueryRecentChanges
* RevisionCommentBatch: HistoryPager, ContribsPager

Bug: T285917
Change-Id: Ia3fd50a4a13138ba5003d884962da24746d562d0
2021-09-28 11:13:03 -07:00

141 lines
3.3 KiB
PHP

<?php
namespace MediaWiki\Tests\Integration\CommentFormatter;
use MediaWiki\CommentFormatter\CommentParser;
use MediaWiki\CommentFormatter\CommentParserFactory;
use MediaWiki\CommentFormatter\RowCommentFormatter;
use MediaWiki\Linker\LinkTarget;
use MediaWiki\Tests\Unit\CommentFormatter\CommentFormatterTestUtils;
/**
* @covers \MediaWiki\CommentFormatter\RowCommentFormatter
* @covers \MediaWiki\CommentFormatter\RowCommentIterator
*/
class RowCommentFormatterTest extends \MediaWikiIntegrationTestCase {
private function getParser() {
return new class extends CommentParser {
public function __construct() {
}
public function preprocess(
string $comment, LinkTarget $selfLinkTarget = null, $samePage = false,
$wikiId = null, $enableSectionLinks = true
) {
if ( $comment === '' || $comment === '*' ) {
return $comment; // Hack to make it work more like the real parser
}
return CommentFormatterTestUtils::dumpArray( [
'comment' => $comment,
'selfLinkTarget' => $selfLinkTarget,
'samePage' => $samePage,
'wikiId' => $wikiId,
'enableSectionLinks' => $enableSectionLinks
] );
}
};
}
private function getParserFactory() {
$parser = $this->getParser();
return new class( $parser ) extends CommentParserFactory {
private $parser;
public function __construct( $parser ) {
$this->parser = $parser;
}
public function create() {
return $this->parser;
}
};
}
private function newCommentFormatter() {
return new RowCommentFormatter(
$this->getParserFactory(),
$this->getServiceContainer()->getCommentStore()
);
}
public function testFormatRows() {
$rows = [
(object)[
'comment_text' => 'hello',
'comment_data' => null,
'namespace' => '0',
'title' => 'Page',
'id' => 1
]
];
$commentFormatter = $this->newCommentFormatter();
$result = $commentFormatter->formatRows(
$rows,
'comment',
'namespace',
'title',
'id'
);
$this->assertSame(
[
1 => 'comment=hello, selfLinkTarget=0:Page, !samePage, !wikiId, enableSectionLinks'
],
$result
);
}
public function testRowsWithFormatItems() {
$rows = [
(object)[
'comment_text' => 'hello',
'comment_data' => null,
'namespace' => '0',
'title' => 'Page',
'id' => 1
]
];
$commentFormatter = $this->newCommentFormatter();
$result = $commentFormatter->formatItems(
$commentFormatter->rows( $rows )
->commentKey( 'comment' )
->namespaceField( 'namespace' )
->titleField( 'title' )
->indexField( 'id' )
);
$this->assertSame(
[
1 => 'comment=hello, selfLinkTarget=0:Page, !samePage, !wikiId, enableSectionLinks'
],
$result
);
}
public function testRowsWithCreateBatch() {
$rows = [
(object)[
'comment_text' => 'hello',
'comment_data' => null,
'namespace' => '0',
'title' => 'Page',
'id' => 1
]
];
$commentFormatter = $this->newCommentFormatter();
$result = $commentFormatter->createBatch()
->comments(
$commentFormatter->rows( $rows )
->commentKey( 'comment' )
->namespaceField( 'namespace' )
->titleField( 'title' )
->indexField( 'id' )
)
->samePage( true )
->execute();
$this->assertSame(
[
1 => 'comment=hello, selfLinkTarget=0:Page, samePage, !wikiId, enableSectionLinks'
],
$result
);
}
}