2019-12-03 23:53:55 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Cite\Tests\Unit;
|
|
|
|
|
2019-12-09 16:08:57 +00:00
|
|
|
use Cite\AnchorFormatter;
|
|
|
|
use Cite\ErrorReporter;
|
2019-12-03 23:53:55 +00:00
|
|
|
use Cite\ReferenceMessageLocalizer;
|
2019-12-09 15:36:34 +00:00
|
|
|
use Cite\ReferencesFormatter;
|
2023-12-22 22:49:47 +00:00
|
|
|
use Cite\Tests\TestUtils;
|
2019-12-03 23:53:55 +00:00
|
|
|
use Message;
|
|
|
|
use Parser;
|
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
|
|
|
/**
|
2023-12-15 11:12:16 +00:00
|
|
|
* @covers \Cite\ReferencesFormatter
|
2019-12-03 23:53:55 +00:00
|
|
|
* @license GPL-2.0-or-later
|
|
|
|
*/
|
2019-12-10 15:24:49 +00:00
|
|
|
class ReferencesFormatterTest extends \MediaWikiUnitTestCase {
|
|
|
|
|
2019-12-04 06:54:04 +00:00
|
|
|
/**
|
2019-12-09 15:36:34 +00:00
|
|
|
* @dataProvider provideFormatReferences
|
2019-12-04 06:54:04 +00:00
|
|
|
*/
|
2019-12-09 15:36:34 +00:00
|
|
|
public function testFormatReferences( array $refs, string $expectedOutput ) {
|
2023-07-27 09:39:46 +00:00
|
|
|
$mockParser = $this->createNoOpMock( Parser::class, [ 'recursiveTagParse' ] );
|
2019-12-04 06:54:04 +00:00
|
|
|
$mockParser->method( 'recursiveTagParse' )->willReturnArgument( 0 );
|
2019-12-09 14:07:37 +00:00
|
|
|
|
2019-12-09 16:08:57 +00:00
|
|
|
$mockErrorReporter = $this->createMock( ErrorReporter::class );
|
2019-12-04 10:59:56 +00:00
|
|
|
$mockErrorReporter->method( 'plain' )->willReturnCallback(
|
2023-12-14 07:58:49 +00:00
|
|
|
static fn ( $parser, ...$args ) => '(' . implode( '|', $args ) . ')'
|
2019-12-04 10:59:56 +00:00
|
|
|
);
|
2019-12-09 14:07:37 +00:00
|
|
|
|
2019-12-04 06:54:04 +00:00
|
|
|
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
|
|
|
|
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
|
|
|
|
function ( ...$args ) {
|
2019-12-09 16:08:57 +00:00
|
|
|
$msg = $this->createMock( Message::class );
|
|
|
|
$msg->method( 'plain' )->willReturn( '<li>(' . implode( '|', $args ) . ')</li>' );
|
|
|
|
return $msg;
|
2019-12-04 06:54:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-12-09 14:07:37 +00:00
|
|
|
$formatter = new ReferencesFormatter(
|
2019-12-04 10:59:56 +00:00
|
|
|
$mockErrorReporter,
|
2019-12-09 16:08:57 +00:00
|
|
|
$this->createMock( AnchorFormatter::class ),
|
2019-12-09 15:01:13 +00:00
|
|
|
$mockMessageLocalizer
|
2019-12-09 14:07:37 +00:00
|
|
|
);
|
2019-12-04 06:54:04 +00:00
|
|
|
|
2023-12-22 22:49:47 +00:00
|
|
|
$refs = array_map( [ TestUtils::class, 'refFromArray' ], $refs );
|
2019-12-11 15:02:49 +00:00
|
|
|
$output = $formatter->formatReferences( $mockParser, $refs, true, false );
|
2019-12-04 06:54:04 +00:00
|
|
|
$this->assertSame( $expectedOutput, $output );
|
|
|
|
}
|
|
|
|
|
2023-05-20 10:03:41 +00:00
|
|
|
public static function provideFormatReferences() {
|
2019-12-04 06:54:04 +00:00
|
|
|
return [
|
|
|
|
'Empty' => [
|
2023-12-21 19:44:39 +00:00
|
|
|
'refs' => [],
|
|
|
|
'expectedOutput' => ''
|
2019-12-04 06:54:04 +00:00
|
|
|
],
|
|
|
|
'Minimal ref' => [
|
2023-12-21 19:44:39 +00:00
|
|
|
'refs' => [
|
2019-12-04 06:54:04 +00:00
|
|
|
0 => [
|
2024-01-06 01:18:51 +00:00
|
|
|
'count' => 1,
|
2019-12-04 06:54:04 +00:00
|
|
|
'key' => 1,
|
|
|
|
'text' => 't',
|
|
|
|
]
|
|
|
|
],
|
2023-12-21 19:44:39 +00:00
|
|
|
'expectedOutput' => '<div class="mw-references-wrap"><ol class="references">' . "\n" .
|
2023-12-22 18:36:33 +00:00
|
|
|
'<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n</ol></div>"
|
2019-12-04 06:54:04 +00:00
|
|
|
],
|
|
|
|
'Ref with extends' => [
|
2023-12-21 19:44:39 +00:00
|
|
|
'refs' => [
|
2019-12-04 06:54:04 +00:00
|
|
|
0 => [
|
2024-01-06 01:18:51 +00:00
|
|
|
'count' => 1,
|
2019-12-04 06:54:04 +00:00
|
|
|
'extends' => 'a',
|
|
|
|
'extendsIndex' => 1,
|
|
|
|
'key' => 2,
|
|
|
|
'number' => 10,
|
|
|
|
'text' => 't2',
|
|
|
|
],
|
|
|
|
1 => [
|
2024-01-06 01:18:51 +00:00
|
|
|
'count' => 1,
|
2023-12-22 18:36:33 +00:00
|
|
|
'key' => 3,
|
2019-12-04 06:54:04 +00:00
|
|
|
'number' => 11,
|
|
|
|
'text' => 't3',
|
|
|
|
],
|
|
|
|
'a' => [
|
2024-01-06 01:18:51 +00:00
|
|
|
'count' => 1,
|
2019-12-04 06:54:04 +00:00
|
|
|
'key' => 1,
|
2023-12-21 20:14:12 +00:00
|
|
|
'name' => 'a',
|
2019-12-04 06:54:04 +00:00
|
|
|
'number' => 9,
|
|
|
|
'text' => 't1',
|
|
|
|
],
|
|
|
|
],
|
2023-12-21 19:44:39 +00:00
|
|
|
'expectedOutput' => '<div class="mw-references-wrap"><ol class="references">' . "\n" .
|
2023-12-22 18:36:33 +00:00
|
|
|
'<li>(cite_references_link_one|||<span class="reference-text">t1</span>' . "\n" .
|
|
|
|
'|)<ol class="mw-extended-references"><li>(cite_references_link_one|||' .
|
2023-07-21 11:57:56 +00:00
|
|
|
'<span class="reference-text">t2</span>' . "\n|)</li>\n" .
|
2019-12-04 06:54:04 +00:00
|
|
|
"</ol></li>\n" .
|
2023-12-22 18:36:33 +00:00
|
|
|
'<li>(cite_references_link_one|||<span class="reference-text">t3</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n" .
|
2019-12-04 06:54:04 +00:00
|
|
|
'</ol></div>'
|
|
|
|
],
|
2019-12-04 10:59:56 +00:00
|
|
|
'Subref of subref' => [
|
2023-12-21 19:44:39 +00:00
|
|
|
'refs' => [
|
2019-12-04 10:59:56 +00:00
|
|
|
0 => [
|
2024-01-06 01:18:51 +00:00
|
|
|
'count' => 1,
|
2019-12-04 10:59:56 +00:00
|
|
|
'extends' => 'a',
|
|
|
|
'extendsIndex' => 1,
|
|
|
|
'key' => 1,
|
|
|
|
'number' => 1,
|
|
|
|
'text' => 't1',
|
|
|
|
],
|
|
|
|
'a' => [
|
2024-01-06 01:18:51 +00:00
|
|
|
'count' => 1,
|
2019-12-04 10:59:56 +00:00
|
|
|
'extends' => 'b',
|
|
|
|
'extendsIndex' => 1,
|
|
|
|
'key' => 2,
|
2023-12-21 20:14:12 +00:00
|
|
|
'name' => 'a',
|
2019-12-04 10:59:56 +00:00
|
|
|
'number' => 1,
|
|
|
|
'text' => 't2',
|
|
|
|
],
|
|
|
|
'b' => [
|
2024-01-06 01:18:51 +00:00
|
|
|
'count' => 1,
|
2019-12-04 10:59:56 +00:00
|
|
|
'key' => 3,
|
2023-12-21 20:14:12 +00:00
|
|
|
'name' => 'b',
|
2019-12-04 10:59:56 +00:00
|
|
|
'number' => 1,
|
|
|
|
'text' => 't3',
|
|
|
|
],
|
|
|
|
],
|
2023-12-21 19:44:39 +00:00
|
|
|
'expectedOutput' => '<div class="mw-references-wrap"><ol class="references">' . "\n" .
|
2023-12-22 18:36:33 +00:00
|
|
|
'<li>(cite_references_link_one|||<span class="reference-text">t3</span>' . "\n" .
|
|
|
|
'|)<ol class="mw-extended-references"><li>(cite_references_link_one|||' .
|
2020-01-23 14:00:26 +00:00
|
|
|
'<span class="reference-text">t1 (cite_error_ref_nested_extends|a|b)</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n" .
|
2023-12-22 18:36:33 +00:00
|
|
|
'<li>(cite_references_link_one|||<span class="reference-text">t2</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n</ol></li>\n" .
|
2019-12-04 10:59:56 +00:00
|
|
|
'</ol></div>'
|
|
|
|
],
|
2019-12-04 06:54:04 +00:00
|
|
|
'Use columns' => [
|
2023-12-21 19:44:39 +00:00
|
|
|
'refs' => array_map(
|
2024-01-06 01:18:51 +00:00
|
|
|
static fn ( $i ) => [ 'count' => 1, 'key' => $i, 'text' => 't' ],
|
2019-12-04 16:55:52 +00:00
|
|
|
range( 0, 10 )
|
|
|
|
),
|
2023-12-21 19:44:39 +00:00
|
|
|
'expectedOutput' => '<div class="mw-references-wrap mw-references-columns"><ol class="references">' .
|
2023-12-22 18:36:33 +00:00
|
|
|
"\n" . '<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n" .
|
2023-12-22 18:36:33 +00:00
|
|
|
'<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n" .
|
2023-12-22 18:36:33 +00:00
|
|
|
'<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n" .
|
2023-12-22 18:36:33 +00:00
|
|
|
'<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n" .
|
2023-12-22 18:36:33 +00:00
|
|
|
'<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n" .
|
2023-12-22 18:36:33 +00:00
|
|
|
'<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n" .
|
2023-12-22 18:36:33 +00:00
|
|
|
'<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n" .
|
2023-12-22 18:36:33 +00:00
|
|
|
'<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n" .
|
2023-12-22 18:36:33 +00:00
|
|
|
'<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n" .
|
2023-12-22 18:36:33 +00:00
|
|
|
'<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n" .
|
2023-12-22 18:36:33 +00:00
|
|
|
'<li>(cite_references_link_one|||<span class="reference-text">t</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n</ol></div>"
|
2019-12-04 06:54:04 +00:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideCloseIndention
|
|
|
|
*/
|
|
|
|
public function testCloseIndention( $closingLi, $expectedOutput ) {
|
2019-12-09 14:07:37 +00:00
|
|
|
/** @var ReferencesFormatter $formatter */
|
2019-12-09 15:36:34 +00:00
|
|
|
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
2019-12-09 16:08:57 +00:00
|
|
|
$this->createMock( ErrorReporter::class ),
|
|
|
|
$this->createMock( AnchorFormatter::class ),
|
2019-12-09 15:01:13 +00:00
|
|
|
$this->createMock( ReferenceMessageLocalizer::class )
|
|
|
|
) );
|
2019-12-04 06:54:04 +00:00
|
|
|
|
|
|
|
$output = $formatter->closeIndention( $closingLi );
|
|
|
|
$this->assertSame( $expectedOutput, $output );
|
|
|
|
}
|
|
|
|
|
2023-05-20 10:03:41 +00:00
|
|
|
public static function provideCloseIndention() {
|
2019-12-04 06:54:04 +00:00
|
|
|
return [
|
|
|
|
'No indention' => [ false, '' ],
|
|
|
|
'Indention string' => [ "</li>\n", "</ol></li>\n" ],
|
|
|
|
'Indention without string' => [ true, '</ol>' ],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-09 15:36:34 +00:00
|
|
|
* @dataProvider provideFormatListItem
|
2019-12-04 06:54:04 +00:00
|
|
|
*/
|
2019-12-09 15:36:34 +00:00
|
|
|
public function testFormatListItem(
|
2023-12-22 22:49:47 +00:00
|
|
|
array $ref,
|
2019-12-04 06:54:04 +00:00
|
|
|
string $expectedOutput
|
|
|
|
) {
|
2019-12-09 16:08:57 +00:00
|
|
|
$mockErrorReporter = $this->createMock( ErrorReporter::class );
|
2019-12-04 06:54:04 +00:00
|
|
|
$mockErrorReporter->method( 'plain' )->willReturnCallback(
|
2023-12-14 07:58:49 +00:00
|
|
|
static fn ( $parser, ...$args ) => '(' . implode( '|', $args ) . ')'
|
2019-12-04 06:54:04 +00:00
|
|
|
);
|
2019-12-09 16:08:57 +00:00
|
|
|
|
|
|
|
$anchorFormatter = $this->createMock( AnchorFormatter::class );
|
2023-12-04 07:28:22 +00:00
|
|
|
$anchorFormatter->method( 'backLink' )->willReturnCallback(
|
2023-12-14 07:58:49 +00:00
|
|
|
static fn ( ...$args ) => implode( '+', $args )
|
2019-12-04 06:54:04 +00:00
|
|
|
);
|
2023-12-04 07:28:22 +00:00
|
|
|
$anchorFormatter->method( 'jumpLinkTarget' )->willReturnArgument( 0 );
|
2019-12-09 16:08:57 +00:00
|
|
|
|
2019-12-04 06:54:04 +00:00
|
|
|
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
|
2020-09-02 07:40:33 +00:00
|
|
|
$mockMessageLocalizer->method( 'localizeSeparators' )->willReturnArgument( 0 );
|
2019-12-10 15:21:12 +00:00
|
|
|
$mockMessageLocalizer->method( 'localizeDigits' )->willReturnArgument( 0 );
|
2019-12-04 06:54:04 +00:00
|
|
|
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
|
|
|
|
function ( ...$args ) {
|
2019-12-09 16:08:57 +00:00
|
|
|
$msg = $this->createMock( Message::class );
|
|
|
|
$msg->method( 'plain' )->willReturn( '(' . implode( '|', $args ) . ')' );
|
|
|
|
return $msg;
|
2019-12-04 06:54:04 +00:00
|
|
|
}
|
|
|
|
);
|
2019-12-09 15:01:13 +00:00
|
|
|
|
2023-06-05 14:37:03 +00:00
|
|
|
/** @var ReferencesFormatter $formatter */
|
2019-12-09 15:36:34 +00:00
|
|
|
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
2019-12-04 06:54:04 +00:00
|
|
|
$mockErrorReporter,
|
2019-12-09 16:08:57 +00:00
|
|
|
$anchorFormatter,
|
2019-12-09 15:01:13 +00:00
|
|
|
$mockMessageLocalizer
|
|
|
|
) );
|
2019-12-04 06:54:04 +00:00
|
|
|
|
2023-07-27 09:39:46 +00:00
|
|
|
$parser = $this->createNoOpMock( Parser::class );
|
2023-12-22 22:49:47 +00:00
|
|
|
$ref = TestUtils::refFromArray( $ref );
|
|
|
|
$output = $formatter->formatListItem( $parser, 1, $ref, false );
|
2019-12-04 06:54:04 +00:00
|
|
|
$this->assertSame( $expectedOutput, $output );
|
|
|
|
}
|
|
|
|
|
2023-05-20 10:03:41 +00:00
|
|
|
public static function provideFormatListItem() {
|
2019-12-04 06:54:04 +00:00
|
|
|
return [
|
|
|
|
'Success' => [
|
2023-12-21 19:44:39 +00:00
|
|
|
'ref' => [
|
2024-01-06 01:18:51 +00:00
|
|
|
'count' => 1,
|
2023-12-22 18:36:33 +00:00
|
|
|
'key' => 1,
|
2019-12-04 06:54:04 +00:00
|
|
|
'text' => 't',
|
|
|
|
],
|
2023-12-22 18:36:33 +00:00
|
|
|
'expectedOutput' => '(cite_references_link_one|1|1+|<span class="reference-text">t</span>' . "\n|)"
|
2019-12-04 06:54:04 +00:00
|
|
|
],
|
2019-12-16 14:33:55 +00:00
|
|
|
'With dir' => [
|
2023-12-21 19:44:39 +00:00
|
|
|
'ref' => [
|
2024-01-06 01:18:51 +00:00
|
|
|
'count' => 1,
|
2019-12-04 06:54:04 +00:00
|
|
|
'dir' => 'rtl',
|
2023-12-22 18:36:33 +00:00
|
|
|
'key' => 1,
|
2019-12-04 06:54:04 +00:00
|
|
|
'text' => 't',
|
|
|
|
],
|
2023-12-22 18:36:33 +00:00
|
|
|
'expectedOutput' => '(cite_references_link_one|1|1+|<span class="reference-text">t</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n" . '| class="mw-cite-dir-rtl")'
|
2019-12-04 06:54:04 +00:00
|
|
|
],
|
2020-02-05 15:28:45 +00:00
|
|
|
'Incomplete follow' => [
|
2023-12-21 19:44:39 +00:00
|
|
|
'ref' => [
|
2020-02-05 10:40:42 +00:00
|
|
|
'follow' => 'f',
|
2023-12-22 18:36:33 +00:00
|
|
|
'key' => 1,
|
2020-02-05 10:40:42 +00:00
|
|
|
'text' => 't',
|
|
|
|
],
|
2023-12-21 19:44:39 +00:00
|
|
|
'expectedOutput' => "<p id=\"f\"><span class=\"reference-text\">t</span>\n</p>"
|
2020-02-05 10:40:42 +00:00
|
|
|
],
|
2024-01-06 01:18:51 +00:00
|
|
|
'Count one' => [
|
2023-12-21 19:44:39 +00:00
|
|
|
'ref' => [
|
2024-01-06 01:18:51 +00:00
|
|
|
'count' => 1,
|
2019-12-04 06:54:04 +00:00
|
|
|
'key' => 5,
|
2023-12-22 18:36:33 +00:00
|
|
|
'name' => 'a',
|
2019-12-04 06:54:04 +00:00
|
|
|
'text' => 't',
|
|
|
|
],
|
2023-12-22 18:36:33 +00:00
|
|
|
'expectedOutput' => '(cite_references_link_one|1-5|1+5-0|<span class="reference-text">t</span>'
|
|
|
|
. "\n|)"
|
2019-12-04 06:54:04 +00:00
|
|
|
],
|
2024-01-06 15:46:56 +00:00
|
|
|
'Anonymous' => [
|
2023-12-21 19:44:39 +00:00
|
|
|
'ref' => [
|
2024-01-06 01:18:51 +00:00
|
|
|
'count' => 1,
|
2019-12-04 06:54:04 +00:00
|
|
|
'key' => 5,
|
|
|
|
'number' => 3,
|
|
|
|
'text' => 't',
|
|
|
|
],
|
2023-12-21 19:44:39 +00:00
|
|
|
'expectedOutput' => '(cite_references_link_one|5|5+|<span class="reference-text">t</span>' . "\n|)"
|
2019-12-04 06:54:04 +00:00
|
|
|
],
|
2024-01-06 01:18:51 +00:00
|
|
|
'Count many' => [
|
2023-12-21 19:44:39 +00:00
|
|
|
'ref' => [
|
2024-01-06 01:18:51 +00:00
|
|
|
'count' => 3,
|
2019-12-04 06:54:04 +00:00
|
|
|
'key' => 5,
|
2024-01-06 15:46:56 +00:00
|
|
|
'name' => 'a',
|
2019-12-04 06:54:04 +00:00
|
|
|
'number' => 3,
|
|
|
|
'text' => 't',
|
|
|
|
],
|
2023-12-21 19:44:39 +00:00
|
|
|
'expectedOutput' => '(cite_references_link_many|1-5|(cite_references_link_many_format|1+5-0|3.0|' .
|
2019-12-04 06:54:04 +00:00
|
|
|
'(cite_references_link_many_format_backlink_labels))' .
|
|
|
|
'(cite_references_link_many_sep)(cite_references_link_many_format|1+5-1|3.1|' .
|
|
|
|
'(cite_error_references_no_backlink_label))(cite_references_link_many_and)' .
|
|
|
|
'(cite_references_link_many_format|1+5-2|3.2|(cite_error_references_no_backlink_label' .
|
2023-07-21 11:57:56 +00:00
|
|
|
'))|<span class="reference-text">t</span>' . "\n|)"
|
2019-12-04 06:54:04 +00:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideReferenceText
|
|
|
|
*/
|
|
|
|
public function testReferenceText(
|
|
|
|
?string $text,
|
|
|
|
bool $isSectionPreview,
|
|
|
|
string $expectedOutput
|
|
|
|
) {
|
2019-12-09 16:08:57 +00:00
|
|
|
$mockErrorReporter = $this->createMock( ErrorReporter::class );
|
2019-12-04 06:54:04 +00:00
|
|
|
$mockErrorReporter->method( 'plain' )->willReturnCallback(
|
2023-12-14 07:58:49 +00:00
|
|
|
static fn ( $parser, ...$args ) => '(' . implode( '|', $args ) . ')'
|
2019-12-04 06:54:04 +00:00
|
|
|
);
|
2019-12-09 15:01:13 +00:00
|
|
|
|
2019-12-09 15:36:34 +00:00
|
|
|
/** @var ReferencesFormatter $formatter */
|
|
|
|
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
2019-12-04 06:54:04 +00:00
|
|
|
$mockErrorReporter,
|
2019-12-09 16:08:57 +00:00
|
|
|
$this->createMock( AnchorFormatter::class ),
|
2019-12-09 15:01:13 +00:00
|
|
|
$this->createMock( ReferenceMessageLocalizer::class )
|
|
|
|
) );
|
2019-12-04 06:54:04 +00:00
|
|
|
|
2023-07-27 09:39:46 +00:00
|
|
|
$parser = $this->createNoOpMock( Parser::class );
|
2023-12-22 22:49:47 +00:00
|
|
|
$ref = TestUtils::refFromArray( [ 'text' => $text ] );
|
|
|
|
$output = $formatter->referenceText( $parser, 1, $ref, $isSectionPreview );
|
2019-12-04 06:54:04 +00:00
|
|
|
$this->assertSame( $expectedOutput, $output );
|
|
|
|
}
|
|
|
|
|
2023-05-20 10:03:41 +00:00
|
|
|
public static function provideReferenceText() {
|
2019-12-04 06:54:04 +00:00
|
|
|
return [
|
|
|
|
'No text, not preview' => [
|
2023-12-21 19:44:39 +00:00
|
|
|
'text' => null,
|
|
|
|
'isSectionPreview' => false,
|
|
|
|
'expectedOutput' => '(cite_error_references_no_text|1)'
|
2019-12-04 06:54:04 +00:00
|
|
|
],
|
|
|
|
'No text, is preview' => [
|
2023-12-21 19:44:39 +00:00
|
|
|
'text' => null,
|
|
|
|
'isSectionPreview' => true,
|
|
|
|
'expectedOutput' => '(cite_warning_sectionpreview_no_text|1)'
|
2019-12-04 06:54:04 +00:00
|
|
|
],
|
|
|
|
'Has text' => [
|
2023-12-21 19:44:39 +00:00
|
|
|
'text' => 'text',
|
|
|
|
'isSectionPreview' => true,
|
|
|
|
'expectedOutput' => '<span class="reference-text">text</span>' . "\n"
|
2019-12-04 06:54:04 +00:00
|
|
|
],
|
|
|
|
'Trims text' => [
|
2023-12-21 19:44:39 +00:00
|
|
|
'text' => "text\n\n",
|
|
|
|
'isSectionPreview' => true,
|
|
|
|
'expectedOutput' => '<span class="reference-text">text</span>' . "\n"
|
2019-12-04 06:54:04 +00:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-12-03 23:53:55 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideReferencesFormatEntryAlternateBacklinkLabel
|
|
|
|
*/
|
|
|
|
public function testReferencesFormatEntryAlternateBacklinkLabel(
|
|
|
|
?string $expectedLabel, ?string $labelList, int $offset
|
|
|
|
) {
|
|
|
|
$mockMessage = $this->createMock( Message::class );
|
|
|
|
$mockMessage->method( 'exists' )->willReturn( (bool)$labelList );
|
|
|
|
$mockMessage->method( 'plain' )->willReturn( $labelList ?? '<missing-junk>' );
|
2019-12-09 16:08:57 +00:00
|
|
|
|
2019-12-03 23:53:55 +00:00
|
|
|
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
|
|
|
|
$mockMessageLocalizer->method( 'msg' )
|
|
|
|
->willReturn( $mockMessage );
|
2019-12-09 16:08:57 +00:00
|
|
|
|
2023-12-12 09:09:45 +00:00
|
|
|
$errorReporter = $this->createMock( ErrorReporter::class );
|
|
|
|
$errorReporter->expects( $expectedLabel ? $this->never() : $this->once() )
|
|
|
|
->method( 'plain' );
|
2019-12-09 15:01:13 +00:00
|
|
|
|
2019-12-09 15:36:34 +00:00
|
|
|
/** @var ReferencesFormatter $formatter */
|
|
|
|
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
2023-12-12 09:09:45 +00:00
|
|
|
$errorReporter,
|
2019-12-09 16:08:57 +00:00
|
|
|
$this->createMock( AnchorFormatter::class ),
|
2019-12-09 15:01:13 +00:00
|
|
|
$mockMessageLocalizer
|
|
|
|
) );
|
2019-12-03 23:53:55 +00:00
|
|
|
|
2023-07-27 09:39:46 +00:00
|
|
|
$parser = $this->createNoOpMock( Parser::class );
|
|
|
|
$label = $formatter->referencesFormatEntryAlternateBacklinkLabel( $parser, $offset );
|
2019-12-03 23:53:55 +00:00
|
|
|
if ( $expectedLabel !== null ) {
|
|
|
|
$this->assertSame( $expectedLabel, $label );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-20 10:03:41 +00:00
|
|
|
public static function provideReferencesFormatEntryAlternateBacklinkLabel() {
|
2019-12-03 23:53:55 +00:00
|
|
|
yield [ 'aa', 'aa ab ac', 0 ];
|
|
|
|
yield [ 'ab', 'aa ab ac', 1 ];
|
|
|
|
yield [ 'å', 'å b c', 0 ];
|
|
|
|
yield [ null, 'a b c', 10 ];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideReferencesFormatEntryNumericBacklinkLabel
|
|
|
|
*/
|
|
|
|
public function testReferencesFormatEntryNumericBacklinkLabel(
|
|
|
|
string $expectedLabel, int $base, int $offset, int $max
|
|
|
|
) {
|
|
|
|
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
|
2020-09-02 07:40:33 +00:00
|
|
|
$mockMessageLocalizer->method( 'localizeSeparators' )->willReturnArgument( 0 );
|
2019-12-10 15:21:12 +00:00
|
|
|
$mockMessageLocalizer->method( 'localizeDigits' )->willReturnArgument( 0 );
|
2019-12-09 15:01:13 +00:00
|
|
|
|
2019-12-09 15:36:34 +00:00
|
|
|
/** @var ReferencesFormatter $formatter */
|
|
|
|
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
2019-12-09 16:08:57 +00:00
|
|
|
$this->createMock( ErrorReporter::class ),
|
|
|
|
$this->createMock( AnchorFormatter::class ),
|
2019-12-09 15:01:13 +00:00
|
|
|
$mockMessageLocalizer
|
|
|
|
) );
|
2019-12-03 23:53:55 +00:00
|
|
|
|
|
|
|
$label = $formatter->referencesFormatEntryNumericBacklinkLabel( $base, $offset, $max );
|
|
|
|
$this->assertSame( $expectedLabel, $label );
|
|
|
|
}
|
|
|
|
|
2023-05-20 10:03:41 +00:00
|
|
|
public static function provideReferencesFormatEntryNumericBacklinkLabel() {
|
2019-12-03 23:53:55 +00:00
|
|
|
yield [ '1.2', 1, 2, 9 ];
|
|
|
|
yield [ '1.02', 1, 2, 99 ];
|
|
|
|
yield [ '1.002', 1, 2, 100 ];
|
2023-07-25 13:34:09 +00:00
|
|
|
yield [ '1.50005', 1, 50005, 50005 ];
|
2019-12-03 23:53:55 +00:00
|
|
|
yield [ '2.1', 2, 1, 1 ];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideLists
|
|
|
|
*/
|
|
|
|
public function testListToText( array $list, $expected ) {
|
|
|
|
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
|
|
|
|
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
|
2019-12-09 16:08:57 +00:00
|
|
|
function ( ...$args ) {
|
|
|
|
$msg = $this->createMock( Message::class );
|
|
|
|
$msg->method( 'plain' )->willReturn( '(' . implode( '|', $args ) . ')' );
|
|
|
|
return $msg;
|
2019-12-03 23:53:55 +00:00
|
|
|
}
|
|
|
|
);
|
2019-12-09 15:01:13 +00:00
|
|
|
|
2019-12-09 15:36:34 +00:00
|
|
|
/** @var ReferencesFormatter $formatter */
|
|
|
|
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
2019-12-09 16:08:57 +00:00
|
|
|
$this->createMock( ErrorReporter::class ),
|
|
|
|
$this->createMock( AnchorFormatter::class ),
|
2019-12-09 15:01:13 +00:00
|
|
|
$mockMessageLocalizer
|
|
|
|
) );
|
2019-12-03 23:53:55 +00:00
|
|
|
$this->assertSame( $expected, $formatter->listToText( $list ) );
|
|
|
|
}
|
|
|
|
|
2023-05-20 10:03:41 +00:00
|
|
|
public static function provideLists() {
|
2019-12-03 23:53:55 +00:00
|
|
|
return [
|
|
|
|
[
|
2023-12-21 19:44:39 +00:00
|
|
|
'list' => [],
|
|
|
|
'expected' => ''
|
2019-12-03 23:53:55 +00:00
|
|
|
],
|
|
|
|
[
|
|
|
|
// This is intentionally using numbers to test the to-string cast
|
2023-12-21 19:44:39 +00:00
|
|
|
'list' => [ 1 ],
|
|
|
|
'expected' => '1'
|
2019-12-03 23:53:55 +00:00
|
|
|
],
|
|
|
|
[
|
2023-12-21 19:44:39 +00:00
|
|
|
'list' => [ 1, 2 ],
|
|
|
|
'expected' => '1(cite_references_link_many_and)2'
|
2019-12-03 23:53:55 +00:00
|
|
|
],
|
|
|
|
[
|
2023-12-21 19:44:39 +00:00
|
|
|
'list' => [ 1, 2, 3 ],
|
|
|
|
'expected' => '1(cite_references_link_many_sep)2(cite_references_link_many_and)3'
|
2019-12-03 23:53:55 +00:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|