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;
|
2023-12-10 22:18:51 +00:00
|
|
|
use MediaWiki\Parser\Sanitizer;
|
2019-12-04 17:20:06 +00:00
|
|
|
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
|
2023-12-04 07:28:22 +00:00
|
|
|
* @covers ::backLink
|
2019-12-04 17:20:06 +00:00
|
|
|
*/
|
|
|
|
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 );
|
2023-07-20 14:22:46 +00:00
|
|
|
$msg->method( 'plain' )->willReturn( '(' . implode( '|', $args ) . ')' );
|
2019-12-09 16:08:57 +00:00
|
|
|
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(
|
2023-11-27 17:34:55 +00:00
|
|
|
'(cite_reference_link_prefix)key',
|
2023-12-04 07:28:22 +00:00
|
|
|
$formatter->backLink( 'key', null ) );
|
2019-12-04 17:20:06 +00:00
|
|
|
$this->assertSame(
|
2023-11-27 17:34:55 +00:00
|
|
|
'(cite_reference_link_prefix)key_2',
|
2023-12-04 07:28:22 +00:00
|
|
|
$formatter->backLink( 'key', '2' ) );
|
2019-12-04 17:20:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::getReferencesKey
|
2023-12-04 07:28:22 +00:00
|
|
|
* @covers ::jumpLink
|
2019-12-04 17:20:06 +00:00
|
|
|
*/
|
|
|
|
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 );
|
2023-07-20 14:22:46 +00:00
|
|
|
$msg->method( 'plain' )->willReturn( '(' . implode( '|', $args ) . ')' );
|
2019-12-09 16:08:57 +00:00
|
|
|
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(
|
2023-11-27 17:34:55 +00:00
|
|
|
'(cite_references_link_prefix)key',
|
2023-12-04 07:28:22 +00:00
|
|
|
$formatter->jumpLink( 'key' ) );
|
2019-12-04 17:20:06 +00:00
|
|
|
}
|
|
|
|
|
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 ) ) );
|
2023-12-04 07:28:22 +00:00
|
|
|
$normalized = $formatter->normalizeKey( $key );
|
|
|
|
$encoded = Sanitizer::safeEncodeAttribute( Sanitizer::escapeIdForLink( $normalized ) );
|
|
|
|
$this->assertSame( $expected, $encoded );
|
2019-11-28 14:35:23 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 10:03:41 +00:00
|
|
|
public static function provideKeyNormalizations() {
|
2019-11-28 14:35:23 +00:00
|
|
|
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
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|