| 1 |
<?php |
| 2 |
|
| 3 |
namespace Cite\Tests\Integration; |
| 4 |
|
| 5 |
use Cite\AnchorFormatter; |
| 6 |
use Cite\Cite; |
| 7 |
use Cite\ErrorReporter; |
| 8 |
use Cite\FootnoteMarkFormatter; |
| 9 |
use Cite\ReferenceMessageLocalizer; |
| 10 |
use Cite\Tests\TestUtils; |
| 11 |
use MediaWiki\Message\Message; |
| 12 |
use MediaWiki\Parser\Parser; |
| 13 |
use Wikimedia\TestingAccessWrapper; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class FootnoteMarkFormatterTest extends \MediaWikiIntegrationTestCase { |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public function testLinkRef( array $ref, string $expectedOutput ) { |
| 25 |
$mockErrorReporter = $this->createMock( ErrorReporter::class ); |
| 26 |
$mockErrorReporter->method( 'plain' )->willReturnCallback( |
| 27 |
static fn ( $parser, ...$args ) => implode( '|', $args ) |
| 28 |
); |
| 29 |
$anchorFormatter = $this->createMock( AnchorFormatter::class ); |
| 30 |
$anchorFormatter->method( 'jumpLink' )->willReturnArgument( 0 ); |
| 31 |
$anchorFormatter->method( 'backLinkTarget' )->willReturnCallback( |
| 32 |
static fn ( ...$args ) => implode( '+', $args ) |
| 33 |
); |
| 34 |
$messageLocalizer = $this->createMock( ReferenceMessageLocalizer::class ); |
| 35 |
$messageLocalizer->method( 'localizeSeparators' )->willReturnArgument( 0 ); |
| 36 |
$messageLocalizer->method( 'localizeDigits' )->willReturnArgument( 0 ); |
| 37 |
$messageLocalizer->method( 'msg' )->willReturnCallback( |
| 38 |
function ( $key, ...$params ) { |
| 39 |
$customizedGroup = $key === 'cite_link_label_group-foo'; |
| 40 |
$msg = $this->createMock( Message::class ); |
| 41 |
$msg->method( 'isDisabled' )->willReturn( !$customizedGroup ); |
| 42 |
$msg->method( 'plain' )->willReturn( $customizedGroup |
| 43 |
? 'a b c' |
| 44 |
: "($key|" . implode( '|', $params ) . ')' |
| 45 |
); |
| 46 |
return $msg; |
| 47 |
} |
| 48 |
); |
| 49 |
$mockParser = $this->createNoOpMock( Parser::class, [ 'recursiveTagParse' ] ); |
| 50 |
$mockParser->method( 'recursiveTagParse' )->willReturnArgument( 0 ); |
| 51 |
$formatter = new FootnoteMarkFormatter( |
| 52 |
$mockErrorReporter, |
| 53 |
$anchorFormatter, |
| 54 |
$messageLocalizer |
| 55 |
); |
| 56 |
|
| 57 |
$ref = TestUtils::refFromArray( $ref ); |
| 58 |
$output = $formatter->linkRef( $mockParser, $ref ); |
| 59 |
$this->assertSame( $expectedOutput, $output ); |
| 60 |
} |
| 61 |
|
| 62 |
public static function provideLinkRef() { |
| 63 |
return [ |
| 64 |
'Default label' => [ |
| 65 |
[ |
| 66 |
'name' => null, |
| 67 |
'group' => '', |
| 68 |
'number' => 50003, |
| 69 |
'key' => 50004, |
| 70 |
], |
| 71 |
'(cite_reference_link|50004+|50004|50003)' |
| 72 |
], |
| 73 |
'Default label, named group' => [ |
| 74 |
[ |
| 75 |
'name' => null, |
| 76 |
'group' => 'bar', |
| 77 |
'number' => 3, |
| 78 |
'key' => 4, |
| 79 |
], |
| 80 |
'(cite_reference_link|4+|4|bar 3)' |
| 81 |
], |
| 82 |
'Custom label' => [ |
| 83 |
[ |
| 84 |
'name' => null, |
| 85 |
'group' => 'foo', |
| 86 |
'number' => 3, |
| 87 |
'key' => 4, |
| 88 |
], |
| 89 |
'(cite_reference_link|4+|4|c)' |
| 90 |
], |
| 91 |
'Custom label overrun' => [ |
| 92 |
[ |
| 93 |
'name' => null, |
| 94 |
'group' => 'foo', |
| 95 |
'number' => 10, |
| 96 |
'key' => 4, |
| 97 |
], |
| 98 |
'(cite_reference_link|4+|4|' . |
| 99 |
|
| 100 |
'cite_error_no_link_label_group|foo|cite_link_label_group-foo)' |
| 101 |
], |
| 102 |
'Named ref' => [ |
| 103 |
[ |
| 104 |
'name' => 'a', |
| 105 |
'group' => '', |
| 106 |
'number' => 3, |
| 107 |
'key' => 4, |
| 108 |
'count' => 1, |
| 109 |
], |
| 110 |
'(cite_reference_link|a+4-0|a-4|3)' |
| 111 |
], |
| 112 |
'Named ref reused' => [ |
| 113 |
[ |
| 114 |
'name' => 'a', |
| 115 |
'group' => '', |
| 116 |
'number' => 3, |
| 117 |
'key' => 4, |
| 118 |
'count' => 50002, |
| 119 |
], |
| 120 |
'(cite_reference_link|a+4-50001|a-4|3)' |
| 121 |
], |
| 122 |
'Subreference' => [ |
| 123 |
[ |
| 124 |
'name' => null, |
| 125 |
'group' => '', |
| 126 |
'number' => 3, |
| 127 |
'key' => 4, |
| 128 |
'extends' => 'b', |
| 129 |
'extendsIndex' => 50002, |
| 130 |
], |
| 131 |
'(cite_reference_link|4+|4|3.50002)' |
| 132 |
], |
| 133 |
]; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function testFetchCustomizedLinkLabel( $expectedLabel, $offset, $group, $labelList ) { |
| 140 |
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class ); |
| 141 |
$mockMessageLocalizer->method( 'msg' )->willReturnCallback( |
| 142 |
function ( ...$args ) use ( $labelList ) { |
| 143 |
$msg = $this->createMock( Message::class ); |
| 144 |
$msg->method( 'isDisabled' )->willReturn( $labelList === null ); |
| 145 |
$msg->method( 'plain' )->willReturn( $labelList ); |
| 146 |
return $msg; |
| 147 |
} |
| 148 |
); |
| 149 |
$mockErrorReporter = $this->createMock( ErrorReporter::class ); |
| 150 |
$mockErrorReporter->method( 'plain' )->willReturnCallback( |
| 151 |
static fn ( $parser, ...$args ) => implode( '|', $args ) |
| 152 |
); |
| 153 |
|
| 154 |
$formatter = TestingAccessWrapper::newFromObject( new FootnoteMarkFormatter( |
| 155 |
$mockErrorReporter, |
| 156 |
$this->createMock( AnchorFormatter::class ), |
| 157 |
$mockMessageLocalizer |
| 158 |
) ); |
| 159 |
|
| 160 |
$parser = $this->createNoOpMock( Parser::class ); |
| 161 |
$output = $formatter->fetchCustomizedLinkLabel( $parser, $group, $offset ); |
| 162 |
$this->assertSame( $expectedLabel, $output ); |
| 163 |
} |
| 164 |
|
| 165 |
public static function provideCustomizedLinkLabels() { |
| 166 |
yield [ null, 1, '', null ]; |
| 167 |
yield [ null, 2, '', null ]; |
| 168 |
yield [ null, 1, 'foo', null ]; |
| 169 |
yield [ null, 2, 'foo', null ]; |
| 170 |
yield [ 'a', 1, 'foo', 'a b c' ]; |
| 171 |
yield [ 'b', 2, 'foo', 'a b c' ]; |
| 172 |
yield [ 'å', 1, 'foo', 'å β' ]; |
| 173 |
yield [ 'cite_error_no_link_label_group|foo|cite_link_label_group-foo', 4, 'foo', 'a b c' ]; |
| 174 |
} |
| 175 |
|
| 176 |
public function testDefaultGroupCannotHaveCustomLinkLabels() { |
| 177 |
|
| 178 |
$formatter = TestingAccessWrapper::newFromObject( new FootnoteMarkFormatter( |
| 179 |
$this->createNoOpMock( ErrorReporter::class ), |
| 180 |
$this->createNoOpMock( AnchorFormatter::class ), |
| 181 |
|
| 182 |
$this->createNoOpMock( ReferenceMessageLocalizer::class ) |
| 183 |
) ); |
| 184 |
|
| 185 |
$parser = $this->createNoOpMock( Parser::class ); |
| 186 |
$this->assertNull( $formatter->fetchCustomizedLinkLabel( $parser, Cite::DEFAULT_GROUP, 1 ) ); |
| 187 |
} |
| 188 |
|
| 189 |
} |