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;
|
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
|
|
|
|
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' => [
|
|
|
|
[],
|
|
|
|
''
|
|
|
|
],
|
|
|
|
'Minimal ref' => [
|
|
|
|
[
|
|
|
|
0 => [
|
|
|
|
'key' => 1,
|
|
|
|
'text' => 't',
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'<div class="mw-references-wrap"><ol class="references">' . "\n" .
|
|
|
|
'<li>(cite_references_link_many|||<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' => [
|
|
|
|
[
|
|
|
|
0 => [
|
|
|
|
'extends' => 'a',
|
|
|
|
'extendsIndex' => 1,
|
|
|
|
'key' => 2,
|
|
|
|
'number' => 10,
|
|
|
|
'text' => 't2',
|
|
|
|
],
|
|
|
|
1 => [
|
|
|
|
'number' => 11,
|
|
|
|
'text' => 't3',
|
|
|
|
],
|
|
|
|
'a' => [
|
|
|
|
'key' => 1,
|
|
|
|
'number' => 9,
|
|
|
|
'text' => 't1',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'<div class="mw-references-wrap"><ol class="references">' . "\n" .
|
|
|
|
'<li>(cite_references_link_many|||<span class="reference-text">t1</span>' . "\n" .
|
2023-07-21 11:57:56 +00:00
|
|
|
'|)<ol class="mw-extended-references"><li>(cite_references_link_many|||' .
|
|
|
|
'<span class="reference-text">t2</span>' . "\n|)</li>\n" .
|
2019-12-04 06:54:04 +00:00
|
|
|
"</ol></li>\n" .
|
|
|
|
'<li>(cite_references_link_many|||<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' => [
|
|
|
|
[
|
|
|
|
0 => [
|
|
|
|
'extends' => 'a',
|
|
|
|
'extendsIndex' => 1,
|
|
|
|
'key' => 1,
|
|
|
|
'number' => 1,
|
|
|
|
'text' => 't1',
|
|
|
|
],
|
|
|
|
'a' => [
|
|
|
|
'extends' => 'b',
|
|
|
|
'extendsIndex' => 1,
|
|
|
|
'key' => 2,
|
|
|
|
'number' => 1,
|
|
|
|
'text' => 't2',
|
|
|
|
],
|
|
|
|
'b' => [
|
|
|
|
'key' => 3,
|
|
|
|
'number' => 1,
|
|
|
|
'text' => 't3',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'<div class="mw-references-wrap"><ol class="references">' . "\n" .
|
|
|
|
'<li>(cite_references_link_many|||<span class="reference-text">t3</span>' . "\n" .
|
2023-07-21 11:57:56 +00:00
|
|
|
'|)<ol class="mw-extended-references"><li>(cite_references_link_many|||' .
|
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" .
|
2019-12-04 10:59:56 +00:00
|
|
|
'<li>(cite_references_link_many|||<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' => [
|
2019-12-04 16:55:52 +00:00
|
|
|
array_map(
|
2023-12-14 07:58:49 +00:00
|
|
|
static fn ( $i ) => [ 'key' => $i, 'text' => 't' ],
|
2019-12-04 16:55:52 +00:00
|
|
|
range( 0, 10 )
|
|
|
|
),
|
2019-12-04 06:54:04 +00:00
|
|
|
'<div class="mw-references-wrap mw-references-columns"><ol class="references">' .
|
|
|
|
"\n" . '<li>(cite_references_link_many|||<span class="reference-text">t</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n" .
|
2019-12-04 06:54:04 +00:00
|
|
|
'<li>(cite_references_link_many|||<span class="reference-text">t</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n" .
|
2019-12-04 06:54:04 +00:00
|
|
|
'<li>(cite_references_link_many|||<span class="reference-text">t</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n" .
|
2019-12-04 06:54:04 +00:00
|
|
|
'<li>(cite_references_link_many|||<span class="reference-text">t</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n" .
|
2019-12-04 06:54:04 +00:00
|
|
|
'<li>(cite_references_link_many|||<span class="reference-text">t</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n" .
|
2019-12-04 06:54:04 +00:00
|
|
|
'<li>(cite_references_link_many|||<span class="reference-text">t</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n" .
|
2019-12-04 06:54:04 +00:00
|
|
|
'<li>(cite_references_link_many|||<span class="reference-text">t</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n" .
|
2019-12-04 06:54:04 +00:00
|
|
|
'<li>(cite_references_link_many|||<span class="reference-text">t</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n" .
|
2019-12-04 06:54:04 +00:00
|
|
|
'<li>(cite_references_link_many|||<span class="reference-text">t</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n" .
|
2019-12-04 06:54:04 +00:00
|
|
|
'<li>(cite_references_link_many|||<span class="reference-text">t</span>' .
|
2023-07-21 11:57:56 +00:00
|
|
|
"\n|)</li>\n" .
|
2019-12-04 06:54:04 +00:00
|
|
|
'<li>(cite_references_link_many|||<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(
|
2019-12-04 06:54:04 +00:00
|
|
|
$key,
|
|
|
|
array $val,
|
|
|
|
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 );
|
|
|
|
$output = $formatter->formatListItem( $parser, $key, $val, 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' => [
|
|
|
|
1,
|
|
|
|
[
|
|
|
|
'text' => 't',
|
|
|
|
],
|
2023-07-21 11:57:56 +00:00
|
|
|
'(cite_references_link_many|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' => [
|
2019-12-04 06:54:04 +00:00
|
|
|
1,
|
|
|
|
[
|
|
|
|
'dir' => 'rtl',
|
|
|
|
'text' => 't',
|
|
|
|
],
|
2023-02-27 16:36:40 +00:00
|
|
|
'(cite_references_link_many|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' => [
|
2020-02-05 10:40:42 +00:00
|
|
|
1,
|
|
|
|
[
|
|
|
|
'follow' => 'f',
|
|
|
|
'text' => 't',
|
|
|
|
],
|
2023-02-27 16:38:07 +00:00
|
|
|
"<p id=\"f\"><span class=\"reference-text\">t</span>\n</p>"
|
2020-02-05 10:40:42 +00:00
|
|
|
],
|
2019-12-04 06:54:04 +00:00
|
|
|
'Count zero' => [
|
|
|
|
1,
|
|
|
|
[
|
|
|
|
'count' => 0,
|
|
|
|
'key' => 5,
|
|
|
|
'text' => 't',
|
|
|
|
],
|
2023-07-21 11:57:56 +00:00
|
|
|
'(cite_references_link_one|1-5|1+5-0|<span class="reference-text">t</span>' . "\n|)"
|
2019-12-04 06:54:04 +00:00
|
|
|
],
|
|
|
|
'Count negative' => [
|
|
|
|
1,
|
|
|
|
[
|
|
|
|
'count' => -1,
|
|
|
|
'key' => 5,
|
|
|
|
'number' => 3,
|
|
|
|
'text' => 't',
|
|
|
|
],
|
2023-07-21 11:57:56 +00:00
|
|
|
'(cite_references_link_one|5|5+|<span class="reference-text">t</span>' . "\n|)"
|
2019-12-04 06:54:04 +00:00
|
|
|
],
|
|
|
|
'Count positive' => [
|
|
|
|
1,
|
|
|
|
[
|
|
|
|
'count' => 2,
|
|
|
|
'key' => 5,
|
|
|
|
'number' => 3,
|
|
|
|
'text' => 't',
|
|
|
|
],
|
2023-02-27 16:36:40 +00:00
|
|
|
'(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(
|
|
|
|
$key,
|
|
|
|
?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-14 12:18:02 +00:00
|
|
|
$output = $formatter->referenceText( $parser, $key, [ 'text' => $text ], $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' => [
|
|
|
|
1,
|
|
|
|
null,
|
|
|
|
false,
|
2019-12-09 16:08:57 +00:00
|
|
|
'(cite_error_references_no_text|1)'
|
2019-12-04 06:54:04 +00:00
|
|
|
],
|
|
|
|
'No text, is preview' => [
|
|
|
|
1,
|
|
|
|
null,
|
|
|
|
true,
|
2019-12-09 16:08:57 +00:00
|
|
|
'(cite_warning_sectionpreview_no_text|1)'
|
2019-12-04 06:54:04 +00:00
|
|
|
],
|
|
|
|
'Has text' => [
|
|
|
|
1,
|
|
|
|
'text',
|
|
|
|
true,
|
|
|
|
'<span class="reference-text">text</span>' . "\n"
|
|
|
|
],
|
|
|
|
'Trims text' => [
|
|
|
|
1,
|
|
|
|
"text\n\n",
|
|
|
|
true,
|
|
|
|
'<span class="reference-text">text</span>' . "\n"
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
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 [
|
|
|
|
[
|
|
|
|
[],
|
|
|
|
''
|
|
|
|
],
|
|
|
|
[
|
|
|
|
// This is intentionally using numbers to test the to-string cast
|
|
|
|
[ 1 ],
|
|
|
|
'1'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 1, 2 ],
|
|
|
|
'1(cite_references_link_many_and)2'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 1, 2, 3 ],
|
|
|
|
'1(cite_references_link_many_sep)2(cite_references_link_many_and)3'
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|