mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Cite
synced 2024-12-24 04:22:51 +00:00
25e7aa44dd
This is a mistake that exists in this codebase for who knows how long. Cite mis-uses the messaging system a lot for internal things we still want to customize somehow, but are not labels that will ever be shown on the screen. The prefix/suffix messages in this patch are meant to be part of the HTML in id="…" attributes. Prefix/suffix must be a static plain text strings. Using e.g. {{GENDER}} or {{PLURAL}} in these messages is not even possible because there is no $1 parameter to use. Note how all other similar messages already use ->plain(). A few wikis override these messages, but stick to the plain-text convention, as they should: https://global-search.toolforge.org/?q=.®ex=1&namespaces=8&title=Cite.*reference.*fix This will continue to work. This has minor performance implications. Fetching these messages is faster if we can skip transformations. Bug: T321217 Change-Id: I7969c255fe4ce897e904897081da5f52678721aa
101 lines
2.8 KiB
PHP
101 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace Cite\Tests\Integration;
|
|
|
|
use Cite\AnchorFormatter;
|
|
use Cite\ReferenceMessageLocalizer;
|
|
use Message;
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
/**
|
|
* @coversDefaultClass \Cite\AnchorFormatter
|
|
*
|
|
* @license GPL-2.0-or-later
|
|
*/
|
|
class AnchorFormatterTest extends \MediaWikiIntegrationTestCase {
|
|
|
|
protected function setUp(): void {
|
|
parent::setUp();
|
|
|
|
global $wgFragmentMode;
|
|
$wgFragmentMode = [ 'html5' ];
|
|
}
|
|
|
|
/**
|
|
* @covers ::refKey
|
|
*/
|
|
public function testRefKey() {
|
|
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
|
|
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
|
|
function ( ...$args ) {
|
|
$msg = $this->createMock( Message::class );
|
|
$msg->method( 'plain' )->willReturn( '(' . implode( '|', $args ) . ')' );
|
|
return $msg;
|
|
}
|
|
);
|
|
$formatter = new AnchorFormatter( $mockMessageLocalizer );
|
|
|
|
$this->assertSame(
|
|
'(cite_reference_link_prefix)key(cite_reference_link_suffix)',
|
|
$formatter->refKey( 'key', null ) );
|
|
$this->assertSame(
|
|
'(cite_reference_link_prefix)' .
|
|
'(cite_reference_link_key_with_num|key|2)(cite_reference_link_suffix)',
|
|
$formatter->refKey( 'key', '2' ) );
|
|
}
|
|
|
|
/**
|
|
* @covers ::getReferencesKey
|
|
*/
|
|
public function testGetReferencesKey() {
|
|
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
|
|
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
|
|
function ( ...$args ) {
|
|
$msg = $this->createMock( Message::class );
|
|
$msg->method( 'plain' )->willReturn( '(' . implode( '|', $args ) . ')' );
|
|
return $msg;
|
|
}
|
|
);
|
|
$formatter = new AnchorFormatter( $mockMessageLocalizer );
|
|
|
|
$this->assertSame(
|
|
'(cite_references_link_prefix)key(cite_references_link_suffix)',
|
|
$formatter->getReferencesKey( 'key' ) );
|
|
}
|
|
|
|
/**
|
|
* @covers ::normalizeKey
|
|
* @covers ::__construct
|
|
* @dataProvider provideKeyNormalizations
|
|
*/
|
|
public function testNormalizeKey( $key, $expected ) {
|
|
/** @var AnchorFormatter $formatter */
|
|
$formatter = TestingAccessWrapper::newFromObject( new AnchorFormatter(
|
|
$this->createMock( ReferenceMessageLocalizer::class ) ) );
|
|
$this->assertSame( $expected, $formatter->normalizeKey( $key ) );
|
|
}
|
|
|
|
public static function provideKeyNormalizations() {
|
|
return [
|
|
[ 'a b', 'a_b' ],
|
|
[ 'a __ b', 'a_b' ],
|
|
[ ':', ':' ],
|
|
[ "\t\n", '_' ],
|
|
[ "'", ''' ],
|
|
[ "''", '''' ],
|
|
[ '"%&/<>?[]{|}', '"%&/<>?[]{|}' ],
|
|
[ 'ISBN', 'ISBN' ],
|
|
|
|
[ 'nature%20phylo', 'nature%2520phylo' ],
|
|
[ 'Mininova%2E26%2E11%2E2009', 'Mininova%252E26%252E11%252E2009' ],
|
|
[ '%c8%98tiri_2019', '%25c8%2598tiri_2019' ],
|
|
[ 'play%21', 'play%2521' ],
|
|
[ 'Terry+O%26rsquo%3BN…</ref', 'Terry+O%2526rsquo%253BN…</ref' ],
|
|
[ '9 pm', '9&nbsp;pm' ],
|
|
[ 'n%25%32%30n', 'n%2525%2532%2530n' ],
|
|
[ 'a_ %20a', 'a_%2520a' ],
|
|
];
|
|
}
|
|
|
|
}
|