| 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 |
|
| 12 |
|
| 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_ref-key', |
| 26 |
$formatter->backLink( 'key', null ) ); |
| 27 |
$this->assertSame( |
| 28 |
'cite_ref-key_2', |
| 29 |
$formatter->backLink( 'key', '2' ) ); |
| 30 |
} |
| 31 |
|
| 32 |
public function testGetReferencesKey() { |
| 33 |
$formatter = new AnchorFormatter(); |
| 34 |
|
| 35 |
$this->assertSame( |
| 36 |
'cite_note-key', |
| 37 |
$formatter->jumpLink( 'key' ) ); |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function testNormalizeKey( $key, $expected ) { |
| 44 |
|
| 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_b' ], |
| 54 |
[ 'a __ b', 'a_b' ], |
| 55 |
[ ':', ':' ], |
| 56 |
[ "\t\n", '_' ], |
| 57 |
[ "'", ''' ], |
| 58 |
[ "''", '''' ], |
| 59 |
[ '"%&/<>?[]{|}', '"%&/<>?[]{|}' ], |
| 60 |
[ 'ISBN', 'ISBN' ], |
| 61 |
|
| 62 |
[ 'nature%20phylo', 'nature%2520phylo' ], |
| 63 |
[ 'Mininova%2E26%2E11%2E2009', 'Mininova%252E26%252E11%252E2009' ], |
| 64 |
[ '%c8%98tiri_2019', '%25c8%2598tiri_2019' ], |
| 65 |
[ 'play%21', 'play%2521' ], |
| 66 |
[ 'Terry+O%26rsquo%3BN…</ref', 'Terry+O%2526rsquo%253BN…</ref' ], |
| 67 |
[ '9 pm', '9&nbsp;pm' ], |
| 68 |
[ 'n%25%32%30n', 'n%2525%2532%2530n' ], |
| 69 |
[ 'a_ %20a', 'a_%2520a' ], |
| 70 |
]; |
| 71 |
} |
| 72 |
|
| 73 |
} |