2019-12-05 08:37:17 +00:00
|
|
|
<?php
|
|
|
|
|
2022-07-13 08:09:31 +00:00
|
|
|
namespace Cite\Tests\Integration;
|
2019-12-05 08:37:17 +00:00
|
|
|
|
2019-12-10 15:24:49 +00:00
|
|
|
use Cite\AnchorFormatter;
|
|
|
|
use Cite\ErrorReporter;
|
|
|
|
use Cite\FootnoteMarkFormatter;
|
|
|
|
use Cite\ReferenceMessageLocalizer;
|
2019-12-05 08:37:17 +00:00
|
|
|
use Message;
|
|
|
|
use Parser;
|
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @coversDefaultClass \Cite\FootnoteMarkFormatter
|
|
|
|
*/
|
2022-07-13 08:09:31 +00:00
|
|
|
class FootnoteMarkFormatterTest extends \MediaWikiIntegrationTestCase {
|
2019-12-10 15:24:49 +00:00
|
|
|
|
2019-12-05 08:37:17 +00:00
|
|
|
/**
|
|
|
|
* @covers ::linkRef
|
|
|
|
* @covers ::__construct
|
|
|
|
* @dataProvider provideLinkRef
|
|
|
|
*/
|
|
|
|
public function testLinkRef( string $group, array $ref, string $expectedOutput ) {
|
|
|
|
$fooLabels = 'a b c';
|
|
|
|
|
2019-12-09 16:08:57 +00:00
|
|
|
$mockErrorReporter = $this->createMock( ErrorReporter::class );
|
2019-12-05 08:37:17 +00:00
|
|
|
$mockErrorReporter->method( 'plain' )->willReturnCallback(
|
2021-05-03 07:03:03 +00:00
|
|
|
static function ( $parser, ...$args ) {
|
2019-12-05 08:37:17 +00:00
|
|
|
return implode( '|', $args );
|
|
|
|
}
|
|
|
|
);
|
2019-12-09 16:08:57 +00:00
|
|
|
$anchorFormatter = $this->createMock( AnchorFormatter::class );
|
|
|
|
$anchorFormatter->method( 'getReferencesKey' )->willReturnArgument( 0 );
|
|
|
|
$anchorFormatter->method( 'refKey' )->willReturnCallback(
|
2021-05-03 07:03:03 +00:00
|
|
|
static function ( ...$args ) {
|
2019-12-05 08:37:17 +00:00
|
|
|
return implode( '+', $args );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$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-05 08:37:17 +00:00
|
|
|
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
|
|
|
|
function ( ...$args ) use ( $group, $fooLabels ) {
|
2019-12-09 16:08:57 +00:00
|
|
|
$msg = $this->createMock( Message::class );
|
|
|
|
$msg->method( 'isDisabled' )->willReturn( $group !== 'foo' );
|
|
|
|
$msg->method( 'plain' )->willReturn( $args[0] === 'cite_reference_link'
|
|
|
|
? '(' . implode( '|', $args ) . ')'
|
|
|
|
: $fooLabels );
|
|
|
|
return $msg;
|
2019-12-05 08:37:17 +00:00
|
|
|
}
|
|
|
|
);
|
2023-07-27 09:39:46 +00:00
|
|
|
$mockParser = $this->createNoOpMock( Parser::class, [ 'recursiveTagParse' ] );
|
2019-12-05 08:37:17 +00:00
|
|
|
$mockParser->method( 'recursiveTagParse' )->willReturnArgument( 0 );
|
2019-12-09 14:07:37 +00:00
|
|
|
$formatter = new FootnoteMarkFormatter(
|
2019-12-05 08:37:17 +00:00
|
|
|
$mockErrorReporter,
|
2019-12-09 16:08:57 +00:00
|
|
|
$anchorFormatter,
|
2019-12-09 14:07:37 +00:00
|
|
|
$mockMessageLocalizer
|
|
|
|
);
|
2019-12-05 08:37:17 +00:00
|
|
|
|
2019-12-11 14:03:09 +00:00
|
|
|
$output = $formatter->linkRef( $mockParser, $group, $ref );
|
2019-12-05 08:37:17 +00:00
|
|
|
$this->assertSame( $expectedOutput, $output );
|
|
|
|
}
|
|
|
|
|
2023-05-20 10:03:41 +00:00
|
|
|
public static function provideLinkRef() {
|
2019-12-05 08:37:17 +00:00
|
|
|
return [
|
|
|
|
'Default label' => [
|
|
|
|
'',
|
|
|
|
[
|
|
|
|
'name' => null,
|
|
|
|
'number' => 3,
|
|
|
|
'key' => 4,
|
|
|
|
'count' => -1,
|
|
|
|
],
|
|
|
|
'(cite_reference_link|4+|4|3)'
|
|
|
|
],
|
|
|
|
'Default label, named group' => [
|
|
|
|
'bar',
|
|
|
|
[
|
|
|
|
'name' => null,
|
|
|
|
'number' => 3,
|
|
|
|
'key' => 4,
|
|
|
|
'count' => -1,
|
|
|
|
],
|
|
|
|
'(cite_reference_link|4+|4|bar 3)'
|
|
|
|
],
|
|
|
|
'Custom label' => [
|
|
|
|
'foo',
|
|
|
|
[
|
|
|
|
'name' => null,
|
|
|
|
'number' => 3,
|
|
|
|
'key' => 4,
|
|
|
|
'count' => -1,
|
|
|
|
],
|
|
|
|
'(cite_reference_link|4+|4|c)'
|
|
|
|
],
|
|
|
|
'Custom label overrun' => [
|
|
|
|
'foo',
|
|
|
|
[
|
|
|
|
'name' => null,
|
|
|
|
'number' => 10,
|
|
|
|
'key' => 4,
|
|
|
|
'count' => -1,
|
|
|
|
],
|
|
|
|
'(cite_reference_link|4+|4|' .
|
|
|
|
'cite_error_no_link_label_group|foo|cite_link_label_group-foo)'
|
|
|
|
],
|
|
|
|
'Named ref' => [
|
|
|
|
'',
|
|
|
|
[
|
|
|
|
'name' => 'a',
|
|
|
|
'number' => 3,
|
|
|
|
'key' => 4,
|
|
|
|
'count' => 0,
|
|
|
|
],
|
|
|
|
'(cite_reference_link|a+4-0|a-4|3)'
|
|
|
|
],
|
|
|
|
'Named ref reused' => [
|
|
|
|
'',
|
|
|
|
[
|
|
|
|
'name' => 'a',
|
|
|
|
'number' => 3,
|
|
|
|
'key' => 4,
|
|
|
|
'count' => 2,
|
|
|
|
],
|
|
|
|
'(cite_reference_link|a+4-2|a-4|3)'
|
|
|
|
],
|
|
|
|
'Subreference' => [
|
|
|
|
'',
|
|
|
|
[
|
|
|
|
'name' => null,
|
|
|
|
'number' => 3,
|
|
|
|
'key' => 4,
|
|
|
|
'count' => -1,
|
|
|
|
'extends' => 'b',
|
|
|
|
'extendsIndex' => 2,
|
|
|
|
],
|
|
|
|
'(cite_reference_link|4+|4|3.2)'
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::getLinkLabel
|
|
|
|
*
|
|
|
|
* @dataProvider provideGetLinkLabel
|
|
|
|
*/
|
|
|
|
public function testGetLinkLabel( $expectedLabel, $offset, $group, $labelList ) {
|
|
|
|
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
|
|
|
|
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
|
|
|
|
function ( ...$args ) use ( $labelList ) {
|
2019-12-09 16:08:57 +00:00
|
|
|
$msg = $this->createMock( Message::class );
|
|
|
|
$msg->method( 'isDisabled' )->willReturn( $labelList === null );
|
|
|
|
$msg->method( 'plain' )->willReturn( $labelList );
|
|
|
|
return $msg;
|
2019-12-05 08:37:17 +00:00
|
|
|
}
|
|
|
|
);
|
2019-12-09 16:08:57 +00:00
|
|
|
$mockErrorReporter = $this->createMock( ErrorReporter::class );
|
2019-12-05 08:37:17 +00:00
|
|
|
$mockErrorReporter->method( 'plain' )->willReturnCallback(
|
2021-05-03 07:03:03 +00:00
|
|
|
static function ( $parser, ...$args ) {
|
2019-12-05 08:37:17 +00:00
|
|
|
return implode( '|', $args );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
/** @var FootnoteMarkFormatter $formatter */
|
|
|
|
$formatter = TestingAccessWrapper::newFromObject( new FootnoteMarkFormatter(
|
|
|
|
$mockErrorReporter,
|
2019-12-09 16:08:57 +00:00
|
|
|
$this->createMock( AnchorFormatter::class ),
|
2019-12-10 15:24:49 +00:00
|
|
|
$mockMessageLocalizer
|
|
|
|
) );
|
2019-12-05 08:37:17 +00:00
|
|
|
|
2023-07-27 09:39:46 +00:00
|
|
|
$parser = $this->createNoOpMock( Parser::class );
|
|
|
|
$output = $formatter->getLinkLabel( $parser, $group, $offset );
|
2019-12-05 08:37:17 +00:00
|
|
|
$this->assertSame( $expectedLabel, $output );
|
|
|
|
}
|
|
|
|
|
2023-05-20 10:03:41 +00:00
|
|
|
public static function provideGetLinkLabel() {
|
2019-12-05 08:37:17 +00:00
|
|
|
yield [ null, 1, '', null ];
|
|
|
|
yield [ null, 2, '', null ];
|
|
|
|
yield [ null, 1, 'foo', null ];
|
|
|
|
yield [ null, 2, 'foo', null ];
|
|
|
|
yield [ 'a', 1, 'foo', 'a b c' ];
|
|
|
|
yield [ 'b', 2, 'foo', 'a b c' ];
|
|
|
|
yield [ 'å', 1, 'foo', 'å β' ];
|
|
|
|
yield [ 'cite_error_no_link_label_group|foo|cite_link_label_group-foo', 4, 'foo', 'a b c' ];
|
|
|
|
}
|
2019-12-09 14:07:37 +00:00
|
|
|
|
2019-12-05 08:37:17 +00:00
|
|
|
}
|