$comment, 'selfLinkTarget' => $selfLinkTarget, 'samePage' => $samePage, 'wikiId' => $wikiId, 'enableSectionLinks' => $enableSectionLinks ] ); } public function preprocessUnsafe( $comment, LinkTarget $selfLinkTarget = null, $samePage = false, $wikiId = null, $enableSectionLinks = true ) { return CommentFormatterTestUtils::dumpArray( [ 'comment' => $comment, 'selfLinkTarget' => $selfLinkTarget, 'samePage' => $samePage, 'wikiId' => $wikiId, 'enableSectionLinks' => $enableSectionLinks, 'unsafe' => true ] ); } public function finalize( $comments ) { return $comments; } }; } 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 CommentFormatter( $this->getParserFactory() ); } public function testCreateBatch() { $formatter = $this->newCommentFormatter(); $result = $formatter->createBatch() ->strings( [ 'key' => 'c' ] ) ->useBlock() ->useParentheses() ->samePage() ->execute(); $this->assertSame( [ 'key' => // parentheses have to come after something so I guess it // makes sense that there is a space here ' ' . '(comment=c, samePage, !wikiId, enableSectionLinks)' ], $result ); } public function testFormatItems() { $formatter = $this->newCommentFormatter(); $result = $formatter->formatItems( [ 'key' => new CommentItem( 'c' ) ] ); $this->assertSame( [ 'key' => 'comment=c, !samePage, !wikiId, enableSectionLinks' ], $result ); } public function testFormat() { $formatter = $this->newCommentFormatter(); $result = $formatter->format( 'c', new TitleValue( 0, 'Page' ), true, 'enwiki' ); $this->assertSame( 'comment=c, selfLinkTarget=0:Page, samePage, wikiId=enwiki, enableSectionLinks', $result ); } public function testFormatBlock() { $formatter = $this->newCommentFormatter(); $result = $formatter->formatBlock( 'c', new TitleValue( 0, 'Page' ), true, 'enwiki', true ); $this->assertSame( ' ' . '(comment=c, selfLinkTarget=0:Page, samePage, wikiId=enwiki, enableSectionLinks)' . '', $result ); } public function testFormatLinksUnsafe() { $formatter = $this->newCommentFormatter(); $result = $formatter->formatLinksUnsafe( 'c', new TitleValue( 0, 'Page' ), true, 'enwiki' ); $this->assertSame( 'comment=c, selfLinkTarget=0:Page, samePage, wikiId=enwiki, !enableSectionLinks, unsafe', $result ); } public function testFormatLinks() { $formatter = $this->newCommentFormatter(); $result = $formatter->formatLinks( 'c', new TitleValue( 0, 'Page' ), true, 'enwiki' ); $this->assertSame( 'comment=c, selfLinkTarget=0:Page, samePage, wikiId=enwiki, !enableSectionLinks', $result ); } public function testFormatStrings() { $formatter = $this->newCommentFormatter(); $result = $formatter->formatStrings( [ 'a' => 'A', 'b' => 'B' ], new TitleValue( 0, 'Page' ), true, 'enwiki' ); $this->assertSame( [ 'a' => 'comment=A, selfLinkTarget=0:Page, samePage, wikiId=enwiki, enableSectionLinks', 'b' => 'comment=B, selfLinkTarget=0:Page, samePage, wikiId=enwiki, enableSectionLinks' ], $result ); } public function testFormatStringsAsBlock() { $formatter = $this->newCommentFormatter(); $result = $formatter->formatStringsAsBlock( [ 'a' => 'A', 'b' => 'B' ], new TitleValue( 0, 'Page' ), true, 'enwiki', true ); $this->assertSame( [ 'a' => ' (' . 'comment=A, selfLinkTarget=0:Page, samePage, wikiId=enwiki, enableSectionLinks' . ')', 'b' => ' (' . 'comment=B, selfLinkTarget=0:Page, samePage, wikiId=enwiki, enableSectionLinks' . ')' ], $result ); } public function provideFormatRevision() { $normal = ' (' . 'comment=hello, selfLinkTarget=Page, !samePage, enableSectionLinks' . ')'; $deleted = ' ' . '(edit summary removed)'; $deletedAllowed = ' ' . '(' . 'comment=hello, selfLinkTarget=Page, !samePage, enableSectionLinks' . ')'; return [ 'not deleted' => [ 'hello', false, false, false, true, $normal, ], 'deleted, for public, not allowed' => [ 'hello', true, true, false, true, $deleted ], 'deleted, for public, allowed' => [ 'hello', true, true, true, true, $deleted ], 'deleted, for private, not allowed' => [ 'hello', false, true, false, true, $deleted ], 'deleted, for private, allowed' => [ 'hello', false, true, true, true, $deletedAllowed, ], 'empty' => [ '', false, false, false, true, '' ], 'asterisk' => [ '*', false, false, false, true, '' ] ]; } /** * @param string $text * @param bool $isDeleted * @param bool $isAllowed * @return array{RevisionRecord,Authority} * @throws \MWException */ private function makeRevisionAndAuthority( $text, $isDeleted, $isAllowed ) { $page = new PageIdentityValue( 1, 0, 'Page', false ); $rev = new MutableRevisionRecord( $page ); $comment = new \CommentStoreComment( 1, $text ); $rev->setId( 100 ); $rev->setComment( $comment ); $rev->setVisibility( $isDeleted ? RevisionRecord::DELETED_COMMENT : 0 ); $user = new UserIdentityValue( 1, 'Sysop' ); $rights = $isAllowed ? [ 'deletedhistory' ] : []; $authority = new SimpleAuthority( $user, $rights ); return [ $rev, $authority ]; } /** @dataProvider provideFormatRevision */ public function testFormatRevision( $comment, $isPublic, $isDeleted, $isAllowed, $useParentheses, $expected ) { list( $rev, $authority ) = $this->makeRevisionAndAuthority( $comment, $isDeleted, $isAllowed ); $formatter = $this->newCommentFormatter(); $result = $formatter->formatRevision( $rev, $authority, false, $isPublic ); $this->assertSame( $expected, $result ); } /** @dataProvider provideFormatRevision */ public function testFormatRevisions( $comment, $isPublic, $isDeleted, $isAllowed, $useParentheses, $expected ) { list( $rev, $authority ) = $this->makeRevisionAndAuthority( $comment, $isDeleted, $isAllowed ); $formatter = $this->newCommentFormatter(); $result = $formatter->formatRevisions( [ 'key' => $rev ], $authority, false, $isPublic ); $this->assertSame( [ 'key' => $expected ], $result ); } public function testFormatRevisionsById() { list( $rev, $authority ) = $this->makeRevisionAndAuthority( 'hello', false, false ); $formatter = $this->newCommentFormatter(); $result = $formatter->formatRevisions( [ 'key' => $rev ], $authority, false, false, true, true ); $this->assertSame( [ 100 => ' (' . 'comment=hello, selfLinkTarget=Page, !samePage, enableSectionLinks' . ')' ], $result ); } /** @dataProvider provideFormatRevision */ public function testCreateRevisionBatch( $comment, $isPublic, $isDeleted, $isAllowed, $useParentheses, $expected ) { list( $rev, $authority ) = $this->makeRevisionAndAuthority( $comment, $isDeleted, $isAllowed ); $formatter = $this->newCommentFormatter(); $result = $formatter->createRevisionBatch() ->authority( $authority ) ->hideIfDeleted( $isPublic ) ->useParentheses() ->revisions( [ 'key' => $rev ] ) ->execute(); $this->assertSame( [ 'key' => $expected ], $result ); } public function testCreateRevisionBatchById() { list( $rev, $authority ) = $this->makeRevisionAndAuthority( 'hello', false, false ); $formatter = $this->newCommentFormatter(); $result = $formatter->createRevisionBatch() ->authority( $authority ) ->useParentheses() ->indexById() ->revisions( [ 'key' => $rev ] ) ->execute(); $this->assertSame( [ 100 => ' (' . 'comment=hello, selfLinkTarget=Page, !samePage, enableSectionLinks' . ')' ], $result ); } }