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;
|
2023-12-12 12:07:53 +00:00
|
|
|
use Cite\Cite;
|
2019-12-10 15:24:49 +00:00
|
|
|
use Cite\FootnoteMarkFormatter;
|
|
|
|
use Cite\ReferenceMessageLocalizer;
|
2023-12-22 22:46:54 +00:00
|
|
|
use Cite\Tests\TestUtils;
|
2024-06-08 21:31:42 +00:00
|
|
|
use MediaWiki\Message\Message;
|
|
|
|
use MediaWiki\Parser\Parser;
|
2019-12-05 08:37:17 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
|
|
|
/**
|
2023-12-15 11:12:16 +00:00
|
|
|
* @covers \Cite\FootnoteMarkFormatter
|
|
|
|
* @license GPL-2.0-or-later
|
2019-12-05 08:37:17 +00:00
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* @dataProvider provideLinkRef
|
|
|
|
*/
|
2024-06-21 19:18:25 +00:00
|
|
|
public function testLinkRef( array $ref, string $expectedOutput ) {
|
2019-12-09 16:08:57 +00:00
|
|
|
$anchorFormatter = $this->createMock( AnchorFormatter::class );
|
2023-12-04 07:28:22 +00:00
|
|
|
$anchorFormatter->method( 'jumpLink' )->willReturnArgument( 0 );
|
|
|
|
$anchorFormatter->method( 'backLinkTarget' )->willReturnCallback(
|
2023-12-14 07:58:49 +00:00
|
|
|
static fn ( ...$args ) => implode( '+', $args )
|
2019-12-05 08:37:17 +00:00
|
|
|
);
|
2023-12-15 11:43:49 +00:00
|
|
|
$messageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
|
|
|
|
$messageLocalizer->method( 'localizeSeparators' )->willReturnArgument( 0 );
|
|
|
|
$messageLocalizer->method( 'localizeDigits' )->willReturnArgument( 0 );
|
|
|
|
$messageLocalizer->method( 'msg' )->willReturnCallback(
|
|
|
|
function ( $key, ...$params ) {
|
|
|
|
$customizedGroup = $key === 'cite_link_label_group-foo';
|
2019-12-09 16:08:57 +00:00
|
|
|
$msg = $this->createMock( Message::class );
|
2023-12-15 11:43:49 +00:00
|
|
|
$msg->method( 'isDisabled' )->willReturn( !$customizedGroup );
|
|
|
|
$msg->method( 'plain' )->willReturn( $customizedGroup
|
|
|
|
? 'a b c'
|
|
|
|
: "($key|" . implode( '|', $params ) . ')'
|
|
|
|
);
|
2019-12-09 16:08:57 +00:00
|
|
|
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-09 16:08:57 +00:00
|
|
|
$anchorFormatter,
|
2023-12-15 11:43:49 +00:00
|
|
|
$messageLocalizer
|
2019-12-09 14:07:37 +00:00
|
|
|
);
|
2019-12-05 08:37:17 +00:00
|
|
|
|
2023-12-22 22:46:54 +00:00
|
|
|
$ref = TestUtils::refFromArray( $ref );
|
2024-06-21 19:18:25 +00:00
|
|
|
$output = $formatter->linkRef( $mockParser, $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,
|
2024-06-21 19:18:25 +00:00
|
|
|
'group' => '',
|
2023-07-25 13:34:09 +00:00
|
|
|
'number' => 50003,
|
|
|
|
'key' => 50004,
|
2019-12-05 08:37:17 +00:00
|
|
|
],
|
2023-07-25 13:34:09 +00:00
|
|
|
'(cite_reference_link|50004+|50004|50003)'
|
2019-12-05 08:37:17 +00:00
|
|
|
],
|
|
|
|
'Default label, named group' => [
|
|
|
|
[
|
|
|
|
'name' => null,
|
2024-06-21 19:18:25 +00:00
|
|
|
'group' => 'bar',
|
2019-12-05 08:37:17 +00:00
|
|
|
'number' => 3,
|
|
|
|
'key' => 4,
|
|
|
|
],
|
|
|
|
'(cite_reference_link|4+|4|bar 3)'
|
|
|
|
],
|
|
|
|
'Custom label' => [
|
|
|
|
[
|
|
|
|
'name' => null,
|
2024-06-21 19:18:25 +00:00
|
|
|
'group' => 'foo',
|
2019-12-05 08:37:17 +00:00
|
|
|
'number' => 3,
|
|
|
|
'key' => 4,
|
|
|
|
],
|
|
|
|
'(cite_reference_link|4+|4|c)'
|
|
|
|
],
|
|
|
|
'Custom label overrun' => [
|
|
|
|
[
|
|
|
|
'name' => null,
|
2024-06-21 19:18:25 +00:00
|
|
|
'group' => 'foo',
|
2019-12-05 08:37:17 +00:00
|
|
|
'number' => 10,
|
|
|
|
'key' => 4,
|
|
|
|
],
|
2024-10-23 09:29:14 +00:00
|
|
|
'(cite_reference_link|4+|4|foo 10)'
|
2019-12-05 08:37:17 +00:00
|
|
|
],
|
|
|
|
'Named ref' => [
|
|
|
|
[
|
|
|
|
'name' => 'a',
|
2024-06-21 19:18:25 +00:00
|
|
|
'group' => '',
|
2019-12-05 08:37:17 +00:00
|
|
|
'number' => 3,
|
|
|
|
'key' => 4,
|
2024-01-06 01:18:51 +00:00
|
|
|
'count' => 1,
|
2019-12-05 08:37:17 +00:00
|
|
|
],
|
|
|
|
'(cite_reference_link|a+4-0|a-4|3)'
|
|
|
|
],
|
|
|
|
'Named ref reused' => [
|
|
|
|
[
|
|
|
|
'name' => 'a',
|
2024-06-21 19:18:25 +00:00
|
|
|
'group' => '',
|
2019-12-05 08:37:17 +00:00
|
|
|
'number' => 3,
|
|
|
|
'key' => 4,
|
2023-07-25 13:34:09 +00:00
|
|
|
'count' => 50002,
|
2019-12-05 08:37:17 +00:00
|
|
|
],
|
2024-01-06 01:18:51 +00:00
|
|
|
'(cite_reference_link|a+4-50001|a-4|3)'
|
2019-12-05 08:37:17 +00:00
|
|
|
],
|
|
|
|
'Subreference' => [
|
|
|
|
[
|
|
|
|
'name' => null,
|
2024-06-21 19:18:25 +00:00
|
|
|
'group' => '',
|
2019-12-05 08:37:17 +00:00
|
|
|
'number' => 3,
|
|
|
|
'key' => 4,
|
|
|
|
'extends' => 'b',
|
2023-07-25 13:34:09 +00:00
|
|
|
'extendsIndex' => 50002,
|
2019-12-05 08:37:17 +00:00
|
|
|
],
|
2023-07-25 13:34:09 +00:00
|
|
|
'(cite_reference_link|4+|4|3.50002)'
|
2019-12-05 08:37:17 +00:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-12-12 12:07:53 +00:00
|
|
|
* @dataProvider provideCustomizedLinkLabels
|
2019-12-05 08:37:17 +00:00
|
|
|
*/
|
2023-12-12 12:07:53 +00:00
|
|
|
public function testFetchCustomizedLinkLabel( $expectedLabel, $offset, $group, $labelList ) {
|
2019-12-05 08:37:17 +00:00
|
|
|
$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
|
|
|
}
|
|
|
|
);
|
|
|
|
/** @var FootnoteMarkFormatter $formatter */
|
|
|
|
$formatter = TestingAccessWrapper::newFromObject( new FootnoteMarkFormatter(
|
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
|
|
|
|
2024-10-23 09:29:14 +00:00
|
|
|
$output = $formatter->fetchCustomizedLinkLabel( $group, $offset );
|
2019-12-05 08:37:17 +00:00
|
|
|
$this->assertSame( $expectedLabel, $output );
|
|
|
|
}
|
|
|
|
|
2023-12-12 12:07:53 +00:00
|
|
|
public static function provideCustomizedLinkLabels() {
|
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', 'å β' ];
|
2024-10-23 09:29:14 +00:00
|
|
|
yield [ null, 4, 'foo', 'a b c' ];
|
2019-12-05 08:37:17 +00:00
|
|
|
}
|
2019-12-09 14:07:37 +00:00
|
|
|
|
2023-12-12 12:07:53 +00:00
|
|
|
public function testDefaultGroupCannotHaveCustomLinkLabels() {
|
|
|
|
/** @var FootnoteMarkFormatter $formatter */
|
|
|
|
$formatter = TestingAccessWrapper::newFromObject( new FootnoteMarkFormatter(
|
|
|
|
$this->createNoOpMock( AnchorFormatter::class ),
|
|
|
|
// Assert that ReferenceMessageLocalizer::msg( 'cite_link_label_group-' ) isn't called
|
|
|
|
$this->createNoOpMock( ReferenceMessageLocalizer::class )
|
|
|
|
) );
|
|
|
|
|
2024-10-23 09:29:14 +00:00
|
|
|
$this->assertNull( $formatter->fetchCustomizedLinkLabel( Cite::DEFAULT_GROUP, 1 ) );
|
2023-12-12 12:07:53 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 08:37:17 +00:00
|
|
|
}
|