2019-11-28 14:35:23 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Cite\Tests\Unit;
|
|
|
|
|
2019-12-09 16:08:57 +00:00
|
|
|
use Cite\AnchorFormatter;
|
2019-12-04 17:20:06 +00:00
|
|
|
use Cite\ReferenceMessageLocalizer;
|
|
|
|
use Message;
|
2019-11-29 14:00:39 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
2019-11-28 14:35:23 +00:00
|
|
|
|
|
|
|
/**
|
2019-12-09 16:08:57 +00:00
|
|
|
* @coversDefaultClass \Cite\AnchorFormatter
|
2019-11-29 14:00:39 +00:00
|
|
|
*
|
|
|
|
* @license GPL-2.0-or-later
|
2019-11-28 14:35:23 +00:00
|
|
|
*/
|
2019-12-10 15:24:49 +00:00
|
|
|
class AnchorFormatterTest extends \MediaWikiUnitTestCase {
|
2019-11-28 14:35:23 +00:00
|
|
|
|
2021-07-22 05:44:15 +00:00
|
|
|
public function setUp(): void {
|
2019-11-28 14:35:23 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
global $wgFragmentMode;
|
|
|
|
$wgFragmentMode = [ 'html5' ];
|
|
|
|
}
|
|
|
|
|
2019-12-04 17:20:06 +00:00
|
|
|
/**
|
|
|
|
* @covers ::refKey
|
|
|
|
*/
|
|
|
|
public function testRefKey() {
|
|
|
|
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
|
|
|
|
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
|
|
|
|
function ( ...$args ) {
|
2019-12-09 16:08:57 +00:00
|
|
|
$msg = $this->createMock( Message::class );
|
|
|
|
$msg->method( 'plain' )->willReturn( '(plain:' . implode( '|', $args ) . ')' );
|
|
|
|
$msg->method( 'text' )->willReturn( '(text:' . implode( '|', $args ) . ')' );
|
|
|
|
return $msg;
|
2019-12-04 17:20:06 +00:00
|
|
|
}
|
|
|
|
);
|
2019-12-09 16:08:57 +00:00
|
|
|
$formatter = new AnchorFormatter( $mockMessageLocalizer );
|
2019-12-04 17:20:06 +00:00
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
'(text:cite_reference_link_prefix)key(text:cite_reference_link_suffix)',
|
|
|
|
$formatter->refKey( 'key', null ) );
|
|
|
|
$this->assertSame(
|
|
|
|
'(text:cite_reference_link_prefix)' .
|
|
|
|
'(plain:cite_reference_link_key_with_num|key|2)(text:cite_reference_link_suffix)',
|
|
|
|
$formatter->refKey( 'key', '2' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::getReferencesKey
|
|
|
|
*/
|
|
|
|
public function testGetReferencesKey() {
|
|
|
|
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
|
|
|
|
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
|
|
|
|
function ( ...$args ) {
|
2019-12-09 16:08:57 +00:00
|
|
|
$msg = $this->createMock( Message::class );
|
|
|
|
$msg->method( 'text' )->willReturn( '(' . implode( '|', $args ) . ')' );
|
|
|
|
return $msg;
|
2019-12-04 17:20:06 +00:00
|
|
|
}
|
|
|
|
);
|
2019-12-09 16:08:57 +00:00
|
|
|
$formatter = new AnchorFormatter( $mockMessageLocalizer );
|
2019-12-04 17:20:06 +00:00
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
'(cite_references_link_prefix)key(cite_references_link_suffix)',
|
|
|
|
$formatter->getReferencesKey( 'key' ) );
|
|
|
|
}
|
|
|
|
|
2019-11-28 14:35:23 +00:00
|
|
|
/**
|
|
|
|
* @covers ::normalizeKey
|
2019-12-04 17:20:06 +00:00
|
|
|
* @covers ::__construct
|
2019-11-28 14:35:23 +00:00
|
|
|
* @dataProvider provideKeyNormalizations
|
|
|
|
*/
|
|
|
|
public function testNormalizeKey( $key, $expected ) {
|
2019-12-09 16:08:57 +00:00
|
|
|
/** @var AnchorFormatter $formatter */
|
|
|
|
$formatter = TestingAccessWrapper::newFromObject( new AnchorFormatter(
|
2019-12-04 17:20:06 +00:00
|
|
|
$this->createMock( ReferenceMessageLocalizer::class ) ) );
|
2019-11-29 14:00:39 +00:00
|
|
|
$this->assertSame( $expected, $formatter->normalizeKey( $key ) );
|
2019-11-28 14:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideKeyNormalizations() {
|
|
|
|
return [
|
|
|
|
[ 'a b', 'a_b' ],
|
|
|
|
[ 'a __ b', 'a_b' ],
|
|
|
|
[ ':', ':' ],
|
2020-02-25 12:38:23 +00:00
|
|
|
[ "\t\n", '_' ],
|
2019-11-28 14:35:23 +00:00
|
|
|
[ "'", ''' ],
|
|
|
|
[ "''", '''' ],
|
|
|
|
[ '"%&/<>?[]{|}', '"%&/<>?[]{|}' ],
|
|
|
|
[ 'ISBN', 'ISBN' ],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|