2019-11-28 14:35:23 +00:00
|
|
|
<?php
|
|
|
|
|
2022-07-13 08:09:31 +00:00
|
|
|
namespace Cite\Tests\Integration;
|
2019-11-28 14:35:23 +00:00
|
|
|
|
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
|
|
|
*/
|
2022-07-13 08:09:31 +00:00
|
|
|
class AnchorFormatterTest extends \MediaWikiIntegrationTestCase {
|
2019-11-28 14:35:23 +00:00
|
|
|
|
2022-06-01 08:34:57 +00:00
|
|
|
protected 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' ],
|
2022-01-03 13:10:47 +00:00
|
|
|
|
2022-01-03 15:23:45 +00:00
|
|
|
[ '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' ],
|
2022-01-03 13:10:47 +00:00
|
|
|
[ '9 pm', '9&nbsp;pm' ],
|
2022-01-03 15:23:45 +00:00
|
|
|
[ 'n%25%32%30n', 'n%2525%2532%2530n' ],
|
|
|
|
[ 'a_ %20a', 'a_%2520a' ],
|
2019-11-28 14:35:23 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|