Debian Codegraph @

extensions/Cite/tests/phpunit/unit/ReferenceListFormatterTest.php

raw symbols SCIP
1 <?php
2
3 namespace Cite\Tests\Unit;
4
5 use Cite\AnchorFormatter;
6 use Cite\ErrorReporter;
7 use Cite\ReferenceListFormatter;
8 use Cite\ReferenceMessageLocalizer;
9 use Cite\Tests\TestUtils;
10 use MediaWiki\Message\Message;
11 use MediaWiki\Parser\Parser;
12 use Wikimedia\TestingAccessWrapper;
13
14 /**
15 * @covers \Cite\ReferenceListFormatter
16 * @license GPL-2.0-or-later
17 */
18 class ReferenceListFormatterTest extends \MediaWikiUnitTestCase {
19
20 /**
21 * @dataProvider provideFormatReferences
22 */
23 public function testFormatReferences( array $refs, string $expectedOutput ) {
24 $mockParser = $this->createNoOpMock( Parser::class, [ 'recursiveTagParse' ] );
25 $mockParser->method( 'recursiveTagParse' )->willReturnArgument( 0 );
26
27 $mockErrorReporter = $this->createMock( ErrorReporter::class );
28 $mockErrorReporter->method( 'plain' )->willReturnCallback(
29 static fn ( $parser, ...$args ) => '(' . implode( '|', $args ) . ')'
30 );
31
32 $mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
33 $mockMessageLocalizer->method( 'msg' )->willReturnCallback(
34 function ( ...$args ) {
35 $msg = $this->createMock( Message::class );
36 $msg->method( 'plain' )->willReturn( '<li>(' . implode( '|', $args ) . ')</li>' );
37 return $msg;
38 }
39 );
40
41 $formatter = new ReferenceListFormatter(
42 $mockErrorReporter,
43 $this->createMock( AnchorFormatter::class ),
44 $mockMessageLocalizer
45 );
46
47 $refs = array_map( [ TestUtils::class, 'refFromArray' ], $refs );
48 $output = $formatter->formatReferences( $mockParser, $refs, true, false );
49 $this->assertSame( $expectedOutput, $output );
50 }
51
52 public static function provideFormatReferences() {
53 return [
54 'Empty' => [
55 'refs' => [],
56 'expectedOutput' => ''
57 ],
58 'Minimal ref' => [
59 'refs' => [
60 0 => [
61 'count' => 1,
62 'key' => 1,
63 'text' => 't',
64 ]
65 ],
66 'expectedOutput' => '<div class="mw-references-wrap"><ol class="references">' . "\n" .
67 '<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
68 "\n|)</li>\n</ol></div>"
69 ],
70 'Ref with extends' => [
71 'refs' => [
72 0 => [
73 'count' => 1,
74 'extends' => 'a',
75 'extendsIndex' => 1,
76 'key' => 2,
77 'number' => 10,
78 'text' => 't2',
79 ],
80 1 => [
81 'count' => 1,
82 'key' => 3,
83 'number' => 11,
84 'text' => 't3',
85 ],
86 'a' => [
87 'count' => 1,
88 'key' => 1,
89 'name' => 'a',
90 'number' => 9,
91 'text' => 't1',
92 ],
93 ],
94 'expectedOutput' => '<div class="mw-references-wrap"><ol class="references">' . "\n" .
95 '<li>(cite_references_link_one|||<span class="reference-text">t1</span>' . "\n" .
96 '|)<ol class="mw-extended-references"><li>(cite_references_link_one|||' .
97 '<span class="reference-text">t2</span>' . "\n|)</li>\n" .
98 "</ol></li>\n" .
99 '<li>(cite_references_link_one|||<span class="reference-text">t3</span>' .
100 "\n|)</li>\n" .
101 '</ol></div>'
102 ],
103 'Subref of subref' => [
104 'refs' => [
105 0 => [
106 'count' => 1,
107 'extends' => 'a',
108 'extendsIndex' => 1,
109 'key' => 1,
110 'number' => 1,
111 'text' => 't1',
112 ],
113 'a' => [
114 'count' => 1,
115 'extends' => 'b',
116 'extendsIndex' => 1,
117 'key' => 2,
118 'name' => 'a',
119 'number' => 1,
120 'text' => 't2',
121 ],
122 'b' => [
123 'count' => 1,
124 'key' => 3,
125 'name' => 'b',
126 'number' => 1,
127 'text' => 't3',
128 ],
129 ],
130 'expectedOutput' => '<div class="mw-references-wrap"><ol class="references">' . "\n" .
131 '<li>(cite_references_link_one|||<span class="reference-text">t3</span>' . "\n" .
132 '|)<ol class="mw-extended-references"><li>(cite_references_link_one|||' .
133 '<span class="reference-text">t1 (cite_error_ref_nested_extends|a|b)</span>' .
134 "\n|)</li>\n" .
135 '<li>(cite_references_link_one|||<span class="reference-text">t2</span>' .
136 "\n|)</li>\n</ol></li>\n" .
137 '</ol></div>'
138 ],
139 'Use columns' => [
140 'refs' => array_map(
141 static fn ( $i ) => [ 'count' => 1, 'key' => $i, 'text' => 't' ],
142 range( 0, 10 )
143 ),
144 'expectedOutput' => '<div class="mw-references-wrap mw-references-columns"><ol class="references">' .
145 "\n" . '<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
146 "\n|)</li>\n" .
147 '<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
148 "\n|)</li>\n" .
149 '<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
150 "\n|)</li>\n" .
151 '<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
152 "\n|)</li>\n" .
153 '<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
154 "\n|)</li>\n" .
155 '<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
156 "\n|)</li>\n" .
157 '<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
158 "\n|)</li>\n" .
159 '<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
160 "\n|)</li>\n" .
161 '<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
162 "\n|)</li>\n" .
163 '<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
164 "\n|)</li>\n" .
165 '<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
166 "\n|)</li>\n</ol></div>"
167 ],
168 ];
169 }
170
171 /**
172 * @dataProvider provideCloseIndention
173 */
174 public function testCloseIndention( $closingLi, $expectedOutput ) {
175 /** @var ReferenceListFormatter $formatter */
176 $formatter = TestingAccessWrapper::newFromObject( new ReferenceListFormatter(
177 $this->createNoOpMock( ErrorReporter::class ),
178 $this->createNoOpMock( AnchorFormatter::class ),
179 $this->createNoOpMock( ReferenceMessageLocalizer::class )
180 ) );
181
182 $output = $formatter->closeIndention( $closingLi );
183 $this->assertSame( $expectedOutput, $output );
184 }
185
186 public static function provideCloseIndention() {
187 return [
188 'No indention' => [ false, '' ],
189 'Indention string' => [ "</li>\n", "</ol></li>\n" ],
190 'Indention without string' => [ true, '</ol>' ],
191 ];
192 }
193
194 /**
195 * @dataProvider provideFormatListItem
196 */
197 public function testFormatListItem(
198 array $ref,
199 string $expectedOutput
200 ) {
201 $mockErrorReporter = $this->createMock( ErrorReporter::class );
202 $mockErrorReporter->method( 'plain' )->willReturnArgument( 1 );
203
204 $anchorFormatter = $this->createMock( AnchorFormatter::class );
205 $anchorFormatter->method( 'backLink' )->willReturnCallback(
206 static fn ( ...$args ) => implode( '+', $args )
207 );
208 $anchorFormatter->method( 'jumpLinkTarget' )->willReturnArgument( 0 );
209
210 $mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
211 $mockMessageLocalizer->method( 'localizeSeparators' )->willReturnArgument( 0 );
212 $mockMessageLocalizer->method( 'localizeDigits' )->willReturnArgument( 0 );
213 $mockMessageLocalizer->method( 'msg' )->willReturnCallback(
214 function ( ...$args ) {
215 $msg = $this->createMock( Message::class );
216 $msg->method( 'plain' )->willReturn( '(' . implode( '|', $args ) . ')' );
217 return $msg;
218 }
219 );
220
221 /** @var ReferenceListFormatter $formatter */
222 $formatter = TestingAccessWrapper::newFromObject( new ReferenceListFormatter(
223 $mockErrorReporter,
224 $anchorFormatter,
225 $mockMessageLocalizer
226 ) );
227
228 $parser = $this->createNoOpMock( Parser::class );
229 $ref = TestUtils::refFromArray( $ref );
230 $output = $formatter->formatListItem( $parser, 1, $ref, false );
231 $this->assertSame( $expectedOutput, $output );
232 }
233
234 public static function provideFormatListItem() {
235 return [
236 'Success' => [
237 'ref' => [
238 'count' => 1,
239 'key' => 1,
240 'text' => 't',
241 ],
242 'expectedOutput' => '(cite_references_link_one|1|1+|<span class="reference-text">t</span>' . "\n|)"
243 ],
244 'With dir' => [
245 'ref' => [
246 'count' => 1,
247 'dir' => 'rtl',
248 'key' => 1,
249 'text' => 't',
250 ],
251 'expectedOutput' => '(cite_references_link_one|1|1+|<span class="reference-text">t</span>' .
252 "\n" . '| class="mw-cite-dir-rtl")'
253 ],
254 'Incomplete follow' => [
255 'ref' => [
256 'follow' => 'f',
257 'key' => 1,
258 'text' => 't',
259 ],
260 'expectedOutput' => "<p id=\"f\"><span class=\"reference-text\">t</span>\n</p>"
261 ],
262 'Count one' => [
263 'ref' => [
264 'count' => 1,
265 'key' => 5,
266 'name' => 'a',
267 'text' => 't',
268 ],
269 'expectedOutput' => '(cite_references_link_one|1-5|1+5-0|<span class="reference-text">t</span>'
270 . "\n|)"
271 ],
272 'Anonymous' => [
273 'ref' => [
274 'count' => 1,
275 'key' => 5,
276 'number' => 3,
277 'text' => 't',
278 ],
279 'expectedOutput' => '(cite_references_link_one|5|5+|<span class="reference-text">t</span>' . "\n|)"
280 ],
281 'Count many' => [
282 'ref' => [
283 'count' => 3,
284 'key' => 5,
285 'name' => 'a',
286 'number' => 3,
287 'text' => 't',
288 ],
289 'expectedOutput' => '(cite_references_link_many|1-5|(cite_references_link_many_format|1+5-0|3.0|' .
290 '(cite_references_link_many_format_backlink_labels))' .
291 '(cite_references_link_many_sep)(cite_references_link_many_format|1+5-1|3.1|' .
292 'cite_error_references_no_backlink_label)(cite_references_link_many_and)' .
293 '(cite_references_link_many_format|1+5-2|3.2|cite_error_references_no_backlink_label' .
294 ')|<span class="reference-text">t</span>' . "\n|)"
295 ],
296 ];
297 }
298
299 /**
300 * @dataProvider provideReferenceText
301 */
302 public function testReferenceText(
303 ?string $text,
304 bool $isSectionPreview,
305 string $expectedOutput
306 ) {
307 $mockErrorReporter = $this->createMock( ErrorReporter::class );
308 $mockErrorReporter->method( 'plain' )->willReturnCallback(
309 static fn ( $parser, ...$args ) => '(' . implode( '|', $args ) . ')'
310 );
311
312 /** @var ReferenceListFormatter $formatter */
313 $formatter = TestingAccessWrapper::newFromObject( new ReferenceListFormatter(
314 $mockErrorReporter,
315 $this->createNoOpMock( AnchorFormatter::class ),
316 $this->createNoOpMock( ReferenceMessageLocalizer::class )
317 ) );
318
319 $parser = $this->createNoOpMock( Parser::class );
320 $ref = TestUtils::refFromArray( [ 'text' => $text ] );
321 $output = $formatter->renderTextAndWarnings( $parser, 1, $ref, $isSectionPreview );
322 $this->assertSame( $expectedOutput, $output );
323 }
324
325 public static function provideReferenceText() {
326 return [
327 'No text, not preview' => [
328 'text' => null,
329 'isSectionPreview' => false,
330 'expectedOutput' => '(cite_error_references_no_text|1)'
331 ],
332 'No text, is preview' => [
333 'text' => null,
334 'isSectionPreview' => true,
335 'expectedOutput' => '(cite_warning_sectionpreview_no_text|1)'
336 ],
337 'Has text' => [
338 'text' => 'text',
339 'isSectionPreview' => true,
340 'expectedOutput' => '<span class="reference-text">text</span>' . "\n"
341 ],
342 'Trims text' => [
343 'text' => "text\n\n",
344 'isSectionPreview' => true,
345 'expectedOutput' => '<span class="reference-text">text</span>' . "\n"
346 ],
347 ];
348 }
349
350 /**
351 * @dataProvider provideReferencesFormatEntryAlternateBacklinkLabel
352 */
353 public function testReferencesFormatEntryAlternateBacklinkLabel(
354 ?string $expectedLabel, string $labelList, int $offset
355 ) {
356 $mockMessage = $this->createNoOpMock( Message::class, [ 'isDisabled', 'plain' ] );
357 $mockMessage->method( 'isDisabled' )->willReturn( !$labelList );
358 $mockMessage->method( 'plain' )->willReturn( $labelList );
359
360 $mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
361 $mockMessageLocalizer->method( 'msg' )
362 ->willReturn( $mockMessage );
363
364 $errorReporter = $this->createMock( ErrorReporter::class );
365 $errorReporter->method( 'plain' )->willReturnArgument( 1 );
366
367 /** @var ReferenceListFormatter $formatter */
368 $formatter = TestingAccessWrapper::newFromObject( new ReferenceListFormatter(
369 $errorReporter,
370 $this->createNoOpMock( AnchorFormatter::class ),
371 $mockMessageLocalizer
372 ) );
373
374 $parser = $this->createNoOpMock( Parser::class );
375 $label = $formatter->referencesFormatEntryAlternateBacklinkLabel( $parser, $offset );
376 $this->assertSame( $expectedLabel, $label );
377 }
378
379 public static function provideReferencesFormatEntryAlternateBacklinkLabel() {
380 yield [ 'aa', 'aa ab ac', 0 ];
381 yield [ 'ab', 'aa ab ac', 1 ];
382 yield [ 'å', 'å b c', 0 ];
383 yield [ 'cite_error_references_no_backlink_label', 'a b c', 10 ];
384 yield [ null, '', 0 ];
385 }
386
387 /**
388 * @dataProvider provideReferencesFormatEntryNumericBacklinkLabel
389 */
390 public function testReferencesFormatEntryNumericBacklinkLabel(
391 string $expectedLabel, string $base, int $offset, int $max
392 ) {
393 $mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
394 $mockMessageLocalizer->method( 'localizeSeparators' )->willReturn( ',' );
395 $mockMessageLocalizer->method( 'localizeDigits' )->willReturnArgument( 0 );
396
397 /** @var ReferenceListFormatter $formatter */
398 $formatter = TestingAccessWrapper::newFromObject( new ReferenceListFormatter(
399 $this->createNoOpMock( ErrorReporter::class ),
400 $this->createNoOpMock( AnchorFormatter::class ),
401 $mockMessageLocalizer
402 ) );
403
404 $label = $formatter->referencesFormatEntryNumericBacklinkLabel( $base, $offset, $max );
405 $this->assertSame( $expectedLabel, $label );
406 }
407
408 public static function provideReferencesFormatEntryNumericBacklinkLabel() {
409 yield [ '1,2', '1', 2, 9 ];
410 yield [ '1,02', '1', 2, 99 ];
411 yield [ '1,002', '1', 2, 100 ];
412 yield [ '1,50005', '1', 50005, 50005 ];
413 yield [ '2,1', '2', 1, 1 ];
414 yield [ '3.2,1', '3.2', 1, 1 ];
415 }
416
417 /**
418 * @dataProvider provideLists
419 */
420 public function testListToText( array $list, $expected ) {
421 $mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
422 $mockMessageLocalizer->method( 'msg' )->willReturnCallback(
423 function ( ...$args ) {
424 $msg = $this->createMock( Message::class );
425 $msg->method( 'plain' )->willReturn( '(' . implode( '|', $args ) . ')' );
426 return $msg;
427 }
428 );
429
430 /** @var ReferenceListFormatter $formatter */
431 $formatter = TestingAccessWrapper::newFromObject( new ReferenceListFormatter(
432 $this->createNoOpMock( ErrorReporter::class ),
433 $this->createNoOpMock( AnchorFormatter::class ),
434 $mockMessageLocalizer
435 ) );
436 $this->assertSame( $expected, $formatter->listToText( $list ) );
437 }
438
439 public static function provideLists() {
440 return [
441 [
442 'list' => [],
443 'expected' => ''
444 ],
445 [
446 // This is intentionally using numbers to test the to-string cast
447 'list' => [ 1 ],
448 'expected' => '1'
449 ],
450 [
451 'list' => [ 1, 2 ],
452 'expected' => '1(cite_references_link_many_and)2'
453 ],
454 [
455 'list' => [ 1, 2, 3 ],
456 'expected' => '1(cite_references_link_many_sep)2(cite_references_link_many_and)3'
457 ],
458 ];
459 }
460
461 }