| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Integration\CommentFormatter; |
| 4 |
|
| 5 |
use MediaWiki\CommentFormatter\CommentParser; |
| 6 |
use MediaWiki\CommentFormatter\RowCommentFormatter; |
| 7 |
use MediaWiki\Linker\LinkTarget; |
| 8 |
use MediaWiki\Tests\Unit\CommentFormatter\CommentFormatterTestUtils; |
| 9 |
use MediaWiki\Tests\Unit\DummyServicesTrait; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class RowCommentFormatterTest extends \MediaWikiIntegrationTestCase { |
| 16 |
use DummyServicesTrait; |
| 17 |
|
| 18 |
private function getParser() { |
| 19 |
return new class extends CommentParser { |
| 20 |
public function __construct() { |
| 21 |
} |
| 22 |
|
| 23 |
public function preprocess( |
| 24 |
string $comment, ?LinkTarget $selfLinkTarget = null, $samePage = false, |
| 25 |
$wikiId = null, $enableSectionLinks = true |
| 26 |
) { |
| 27 |
if ( $comment === '' || $comment === '*' ) { |
| 28 |
return $comment; |
| 29 |
} |
| 30 |
return CommentFormatterTestUtils::dumpArray( [ |
| 31 |
'comment' => $comment, |
| 32 |
'selfLinkTarget' => $selfLinkTarget, |
| 33 |
'samePage' => $samePage, |
| 34 |
'wikiId' => $wikiId, |
| 35 |
'enableSectionLinks' => $enableSectionLinks |
| 36 |
] ); |
| 37 |
} |
| 38 |
}; |
| 39 |
} |
| 40 |
|
| 41 |
private function newCommentFormatter() { |
| 42 |
return new RowCommentFormatter( |
| 43 |
$this->getDummyCommentParserFactory( $this->getParser() ), |
| 44 |
$this->getServiceContainer()->getCommentStore() |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
public function testFormatRows() { |
| 49 |
$rows = [ |
| 50 |
(object)[ |
| 51 |
'comment_text' => 'hello', |
| 52 |
'comment_data' => null, |
| 53 |
'namespace' => '0', |
| 54 |
'title' => 'Page', |
| 55 |
'id' => 1 |
| 56 |
] |
| 57 |
]; |
| 58 |
$commentFormatter = $this->newCommentFormatter(); |
| 59 |
$result = $commentFormatter->formatRows( |
| 60 |
$rows, |
| 61 |
'comment', |
| 62 |
'namespace', |
| 63 |
'title', |
| 64 |
'id' |
| 65 |
); |
| 66 |
$this->assertSame( |
| 67 |
[ |
| 68 |
1 => 'comment=hello, selfLinkTarget=0:Page, !samePage, !wikiId, enableSectionLinks' |
| 69 |
], |
| 70 |
$result |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
public function testRowsWithFormatItems() { |
| 75 |
$rows = [ |
| 76 |
(object)[ |
| 77 |
'comment_text' => 'hello', |
| 78 |
'comment_data' => null, |
| 79 |
'namespace' => '0', |
| 80 |
'title' => 'Page', |
| 81 |
'id' => 1 |
| 82 |
] |
| 83 |
]; |
| 84 |
$commentFormatter = $this->newCommentFormatter(); |
| 85 |
$result = $commentFormatter->formatItems( |
| 86 |
$commentFormatter->rows( $rows ) |
| 87 |
->commentKey( 'comment' ) |
| 88 |
->namespaceField( 'namespace' ) |
| 89 |
->titleField( 'title' ) |
| 90 |
->indexField( 'id' ) |
| 91 |
); |
| 92 |
$this->assertSame( |
| 93 |
[ |
| 94 |
1 => 'comment=hello, selfLinkTarget=0:Page, !samePage, !wikiId, enableSectionLinks' |
| 95 |
], |
| 96 |
$result |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
public function testRowsWithCreateBatch() { |
| 101 |
$rows = [ |
| 102 |
(object)[ |
| 103 |
'comment_text' => 'hello', |
| 104 |
'comment_data' => null, |
| 105 |
'namespace' => '0', |
| 106 |
'title' => 'Page', |
| 107 |
'id' => 1 |
| 108 |
] |
| 109 |
]; |
| 110 |
$commentFormatter = $this->newCommentFormatter(); |
| 111 |
$result = $commentFormatter->createBatch() |
| 112 |
->comments( |
| 113 |
$commentFormatter->rows( $rows ) |
| 114 |
->commentKey( 'comment' ) |
| 115 |
->namespaceField( 'namespace' ) |
| 116 |
->titleField( 'title' ) |
| 117 |
->indexField( 'id' ) |
| 118 |
) |
| 119 |
->samePage( true ) |
| 120 |
->execute(); |
| 121 |
$this->assertSame( |
| 122 |
[ |
| 123 |
1 => 'comment=hello, selfLinkTarget=0:Page, samePage, !wikiId, enableSectionLinks' |
| 124 |
], |
| 125 |
$result |
| 126 |
); |
| 127 |
} |
| 128 |
} |