Debian Codegraph @

extensions/Cite/tests/phpunit/integration/AnchorFormatterTest.php

raw symbols SCIP
1 <?php
2
3 namespace Cite\Tests\Integration;
4
5 use Cite\AnchorFormatter;
6 use MediaWiki\MainConfigNames;
7 use MediaWiki\Parser\Sanitizer;
8 use Wikimedia\TestingAccessWrapper;
9
10 /**
11 * @covers \Cite\AnchorFormatter
12 * @license GPL-2.0-or-later
13 */
14 class AnchorFormatterTest extends \MediaWikiIntegrationTestCase {
15
16 protected function setUp(): void {
17 parent::setUp();
18 $this->overrideConfigValue( MainConfigNames::FragmentMode, [ 'html5' ] );
19 }
20
21 public function testRefKey() {
22 $formatter = new AnchorFormatter();
23
24 $this->assertSame(
25 'cite&#95;ref-key',
26 $formatter->backLink( 'key', null ) );
27 $this->assertSame(
28 'cite&#95;ref-key&#95;2',
29 $formatter->backLink( 'key', '2' ) );
30 }
31
32 public function testGetReferencesKey() {
33 $formatter = new AnchorFormatter();
34
35 $this->assertSame(
36 'cite&#95;note-key',
37 $formatter->jumpLink( 'key' ) );
38 }
39
40 /**
41 * @dataProvider provideKeyNormalizations
42 */
43 public function testNormalizeKey( $key, $expected ) {
44 /** @var AnchorFormatter $formatter */
45 $formatter = TestingAccessWrapper::newFromObject( new AnchorFormatter() );
46 $normalized = $formatter->normalizeKey( $key );
47 $encoded = Sanitizer::safeEncodeAttribute( Sanitizer::escapeIdForLink( $normalized ) );
48 $this->assertSame( $expected, $encoded );
49 }
50
51 public static function provideKeyNormalizations() {
52 return [
53 [ 'a b', 'a&#95;b' ],
54 [ 'a __ b', 'a&#95;b' ],
55 [ ':', ':' ],
56 [ "\t\n", '&#95;' ],
57 [ "'", '&#039;' ],
58 [ "''", '&#039;&#039;' ],
59 [ '"%&/<>?[]{|}', '&quot;%&amp;/&lt;&gt;?&#91;&#93;&#123;&#124;&#125;' ],
60 [ 'ISBN', '&#73;SBN' ],
61
62 [ 'nature%20phylo', 'nature%2520phylo' ],
63 [ 'Mininova%2E26%2E11%2E2009', 'Mininova%252E26%252E11%252E2009' ],
64 [ '%c8%98tiri_2019', '%25c8%2598tiri&#95;2019' ],
65 [ 'play%21', 'play%2521' ],
66 [ 'Terry+O%26rsquo%3BN…</ref', 'Terry+O%2526rsquo%253BN…&lt;/ref' ],
67 [ '9&nbsp;pm', '9&amp;nbsp;pm' ],
68 [ 'n%25%32%30n', 'n%2525%2532%2530n' ],
69 [ 'a_ %20a', 'a&#95;%2520a' ],
70 ];
71 }
72
73 }