Debian Codegraph @

tests/phpunit/integration/includes/CommentFormatter/CommentFormatterTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Integration\CommentFormatter;
4
5 use MediaWiki\CommentFormatter\CommentFormatter;
6 use MediaWiki\CommentFormatter\CommentItem;
7 use MediaWiki\CommentFormatter\CommentParser;
8 use MediaWiki\CommentStore\CommentStoreComment;
9 use MediaWiki\Linker\LinkTarget;
10 use MediaWiki\Page\PageIdentityValue;
11 use MediaWiki\Permissions\Authority;
12 use MediaWiki\Permissions\SimpleAuthority;
13 use MediaWiki\Revision\MutableRevisionRecord;
14 use MediaWiki\Revision\RevisionRecord;
15 use MediaWiki\Tests\Unit\CommentFormatter\CommentFormatterTestUtils;
16 use MediaWiki\Tests\Unit\DummyServicesTrait;
17 use MediaWiki\Title\TitleValue;
18 use MediaWiki\User\UserIdentityValue;
19 use MediaWikiIntegrationTestCase;
20
21 /**
22 * Trivial comment formatting with a mocked parser. Can't be a unit test because
23 * of the wfMessage() calls.
24 *
25 * @covers \MediaWiki\CommentFormatter\CommentFormatter
26 */
27 class CommentFormatterTest extends MediaWikiIntegrationTestCase {
28 use DummyServicesTrait;
29
30 private function getParser() {
31 return new class extends CommentParser {
32 public function __construct() {
33 }
34
35 public function preprocess(
36 string $comment, ?LinkTarget $selfLinkTarget = null, $samePage = false,
37 $wikiId = null, $enableSectionLinks = true
38 ) {
39 if ( $comment === '' || $comment === '*' ) {
40 return $comment; // Hack to make it work more like the real parser
41 }
42 return CommentFormatterTestUtils::dumpArray( [
43 'comment' => $comment,
44 'selfLinkTarget' => $selfLinkTarget,
45 'samePage' => $samePage,
46 'wikiId' => $wikiId,
47 'enableSectionLinks' => $enableSectionLinks
48 ] );
49 }
50
51 public function preprocessUnsafe(
52 $comment, ?LinkTarget $selfLinkTarget = null, $samePage = false, $wikiId = null,
53 $enableSectionLinks = true
54 ) {
55 return CommentFormatterTestUtils::dumpArray( [
56 'comment' => $comment,
57 'selfLinkTarget' => $selfLinkTarget,
58 'samePage' => $samePage,
59 'wikiId' => $wikiId,
60 'enableSectionLinks' => $enableSectionLinks,
61 'unsafe' => true
62 ] );
63 }
64
65 public function finalize( $comments ) {
66 return $comments;
67 }
68 };
69 }
70
71 private function newCommentFormatter() {
72 return new CommentFormatter(
73 $this->getDummyCommentParserFactory( $this->getParser() )
74 );
75 }
76
77 public function testCreateBatch() {
78 $formatter = $this->newCommentFormatter();
79 $result = $formatter->createBatch()
80 ->strings( [ 'key' => 'c' ] )
81 ->useBlock()
82 ->useParentheses()
83 ->samePage()
84 ->execute();
85 $this->assertSame(
86 [
87 'key' =>
88 // parentheses have to come after something so I guess it
89 // makes sense that there is a space here
90 ' ' .
91 '<span class="comment">(comment=c, samePage, !wikiId, enableSectionLinks)</span>'
92 ],
93 $result
94 );
95 }
96
97 public function testFormatItems() {
98 $formatter = $this->newCommentFormatter();
99 $result = $formatter->formatItems( [
100 'key' => new CommentItem( 'c' )
101 ] );
102 $this->assertSame(
103 [ 'key' => 'comment=c, !samePage, !wikiId, enableSectionLinks' ],
104 $result
105 );
106 }
107
108 public function testFormat() {
109 $formatter = $this->newCommentFormatter();
110 $result = $formatter->format(
111 'c',
112 new TitleValue( 0, 'Page' ),
113 true,
114 'enwiki'
115 );
116 $this->assertSame(
117 'comment=c, selfLinkTarget=0:Page, samePage, wikiId=enwiki, enableSectionLinks',
118 $result
119 );
120 }
121
122 public function testFormatBlock() {
123 $formatter = $this->newCommentFormatter();
124 $result = $formatter->formatBlock(
125 'c',
126 new TitleValue( 0, 'Page' ),
127 true,
128 'enwiki',
129 true
130 );
131 $this->assertSame(
132 ' <span class="comment">' .
133 '(comment=c, selfLinkTarget=0:Page, samePage, wikiId=enwiki, enableSectionLinks)' .
134 '</span>',
135 $result
136 );
137 }
138
139 public function testFormatLinksUnsafe() {
140 $formatter = $this->newCommentFormatter();
141 $result = $formatter->formatLinksUnsafe(
142 'c',
143 new TitleValue( 0, 'Page' ),
144 true,
145 'enwiki'
146 );
147 $this->assertSame(
148 'comment=c, selfLinkTarget=0:Page, samePage, wikiId=enwiki, !enableSectionLinks, unsafe',
149 $result
150 );
151 }
152
153 public function testFormatLinks() {
154 $formatter = $this->newCommentFormatter();
155 $result = $formatter->formatLinks(
156 'c',
157 new TitleValue( 0, 'Page' ),
158 true,
159 'enwiki'
160 );
161 $this->assertSame(
162 'comment=c, selfLinkTarget=0:Page, samePage, wikiId=enwiki, !enableSectionLinks',
163 $result
164 );
165 }
166
167 public function testFormatStrings() {
168 $formatter = $this->newCommentFormatter();
169 $result = $formatter->formatStrings(
170 [
171 'a' => 'A',
172 'b' => 'B'
173 ],
174 new TitleValue( 0, 'Page' ),
175 true,
176 'enwiki'
177 );
178 $this->assertSame(
179 [
180 'a' => 'comment=A, selfLinkTarget=0:Page, samePage, wikiId=enwiki, enableSectionLinks',
181 'b' => 'comment=B, selfLinkTarget=0:Page, samePage, wikiId=enwiki, enableSectionLinks'
182 ],
183 $result
184 );
185 }
186
187 public static function provideFormatRevision() {
188 $normal = ' <span class="comment">(' .
189 'comment=hello, selfLinkTarget=Page, !samePage, enableSectionLinks' .
190 ')</span>';
191 $deleted = ' <span class="history-deleted comment"> ' .
192 '<span class="comment">(edit summary removed)</span></span>';
193 $deletedAllowed = ' <span class="history-deleted comment"> ' .
194 '<span class="comment">(' .
195 'comment=hello, selfLinkTarget=Page, !samePage, enableSectionLinks' .
196 ')</span></span>';
197
198 return [
199 'not deleted' => [
200 'hello', false, false, false, true,
201 $normal,
202 ],
203 'deleted, for public, not allowed' => [
204 'hello', true, true, false, true,
205 $deleted
206 ],
207 'deleted, for public, allowed' => [
208 'hello', true, true, true, true,
209 $deleted
210 ],
211 'deleted, for private, not allowed' => [
212 'hello', false, true, false, true,
213 $deleted
214 ],
215 'deleted, for private, allowed' => [
216 'hello', false, true, true, true,
217 $deletedAllowed,
218 ],
219 'empty' => [
220 '', false, false, false, true,
221 ''
222 ],
223 'asterisk' => [
224 '*', false, false, false, true,
225 ''
226 ]
227 ];
228 }
229
230 /**
231 * @param string $text
232 * @param bool $isDeleted
233 * @param bool $isAllowed
234 * @return array<RevisionRecord|Authority>
235 */
236 private function makeRevisionAndAuthority( $text, $isDeleted, $isAllowed ) {
237 $page = new PageIdentityValue( 1, 0, 'Page', false );
238 $rev = new MutableRevisionRecord( $page );
239 $comment = new CommentStoreComment( 1, $text );
240 $rev->setId( 100 );
241 $rev->setComment( $comment );
242 $rev->setVisibility( $isDeleted ? RevisionRecord::DELETED_COMMENT : 0 );
243 $user = new UserIdentityValue( 1, 'Sysop' );
244 $rights = $isAllowed ? [ 'deletedhistory' ] : [];
245 $authority = new SimpleAuthority( $user, $rights );
246 return [ $rev, $authority ];
247 }
248
249 /** @dataProvider provideFormatRevision */
250 public function testFormatRevision( $comment, $isPublic, $isDeleted, $isAllowed, $useParentheses,
251 $expected
252 ) {
253 [ $rev, $authority ] = $this->makeRevisionAndAuthority(
254 $comment, $isDeleted, $isAllowed );
255 $formatter = $this->newCommentFormatter();
256 $result = $formatter->formatRevision(
257 $rev,
258 $authority,
259 false,
260 $isPublic
261 );
262 $this->assertSame(
263 $expected,
264 $result
265 );
266 }
267
268 /** @dataProvider provideFormatRevision */
269 public function testFormatRevisions( $comment, $isPublic, $isDeleted, $isAllowed, $useParentheses,
270 $expected
271 ) {
272 [ $rev, $authority ] = $this->makeRevisionAndAuthority(
273 $comment, $isDeleted, $isAllowed );
274 $formatter = $this->newCommentFormatter();
275 $result = $formatter->formatRevisions(
276 [ 'key' => $rev ],
277 $authority,
278 false,
279 $isPublic
280 );
281 $this->assertSame(
282 [ 'key' => $expected ],
283 $result
284 );
285 }
286
287 public function testFormatRevisionsById() {
288 [ $rev, $authority ] = $this->makeRevisionAndAuthority(
289 'hello', false, false );
290 $formatter = $this->newCommentFormatter();
291 $result = $formatter->formatRevisions(
292 [ 'key' => $rev ],
293 $authority,
294 false,
295 false,
296 true,
297 true
298 );
299 $this->assertSame(
300 [ 100 => ' <span class="comment">(' .
301 'comment=hello, selfLinkTarget=Page, !samePage, enableSectionLinks' .
302 ')</span>'
303 ],
304 $result
305 );
306 }
307
308 /** @dataProvider provideFormatRevision */
309 public function testCreateRevisionBatch( $comment, $isPublic, $isDeleted, $isAllowed, $useParentheses,
310 $expected
311 ) {
312 [ $rev, $authority ] = $this->makeRevisionAndAuthority(
313 $comment, $isDeleted, $isAllowed );
314 $formatter = $this->newCommentFormatter();
315 $result = $formatter->createRevisionBatch()
316 ->authority( $authority )
317 ->hideIfDeleted( $isPublic )
318 ->useParentheses()
319 ->revisions( [ 'key' => $rev ] )
320 ->execute();
321 $this->assertSame(
322 [ 'key' => $expected ],
323 $result
324 );
325 }
326
327 public function testCreateRevisionBatchById() {
328 [ $rev, $authority ] = $this->makeRevisionAndAuthority(
329 'hello', false, false );
330 $formatter = $this->newCommentFormatter();
331 $result = $formatter->createRevisionBatch()
332 ->authority( $authority )
333 ->useParentheses()
334 ->indexById()
335 ->revisions( [ 'key' => $rev ] )
336 ->execute();
337 $this->assertSame(
338 [ 100 => ' <span class="comment">(' .
339 'comment=hello, selfLinkTarget=Page, !samePage, enableSectionLinks' .
340 ')</span>'
341 ],
342 $result
343 );
344 }
345 }