mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Cite
synced 2024-12-01 01:56:20 +00:00
4377f0923d
Same arguments as in Iafa2412. The one reason to use more detailled per-method @covers annotations is to avoid "accidental coverage" where code is marked as being covered by tests that don't assert anything that would be meaningful for this code. This is especially a problem with older, bigger classes with lots of side effects. But all the new classes we introduced over the years are small, with predictable, local effects. That's also why we keep the more detailled @covers annotations for the original Cite class. Bug: T353227 Bug: T353269 Change-Id: I69850f4d740d8ad5a7c2368b9068dc91e47cc797
92 lines
2.7 KiB
PHP
92 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace Cite\Tests\Integration;
|
|
|
|
use Cite\AnchorFormatter;
|
|
use Cite\ReferenceMessageLocalizer;
|
|
use MediaWiki\Parser\Sanitizer;
|
|
use Message;
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
/**
|
|
* @covers \Cite\AnchorFormatter
|
|
* @license GPL-2.0-or-later
|
|
*/
|
|
class AnchorFormatterTest extends \MediaWikiIntegrationTestCase {
|
|
|
|
protected function setUp(): void {
|
|
parent::setUp();
|
|
$this->overrideConfigValue( 'FragmentMode', [ 'html5' ] );
|
|
}
|
|
|
|
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',
|
|
$formatter->backLink( 'key', null ) );
|
|
$this->assertSame(
|
|
'(cite_reference_link_prefix)key_2',
|
|
$formatter->backLink( 'key', '2' ) );
|
|
}
|
|
|
|
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',
|
|
$formatter->jumpLink( 'key' ) );
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideKeyNormalizations
|
|
*/
|
|
public function testNormalizeKey( $key, $expected ) {
|
|
/** @var AnchorFormatter $formatter */
|
|
$formatter = TestingAccessWrapper::newFromObject( new AnchorFormatter(
|
|
$this->createMock( ReferenceMessageLocalizer::class ) ) );
|
|
$normalized = $formatter->normalizeKey( $key );
|
|
$encoded = Sanitizer::safeEncodeAttribute( Sanitizer::escapeIdForLink( $normalized ) );
|
|
$this->assertSame( $expected, $encoded );
|
|
}
|
|
|
|
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' ],
|
|
];
|
|
}
|
|
|
|
}
|