mediawiki-extensions-Cite/tests/phpunit/unit/AnchorFormatterTest.php
Thiemo Kreuz 3f2aeb7e31 Rename two Cite… classes and clean up test setups
* All classes are in a Cite\ namespace now. No need to repeat the word
"Cite" all over the place.

* The "key formatter" is more an ID or anchor formatter. The strings it
returns are all used in id="…" attributes, as well as in href="#…" links
to jump to these IDs.

* This patch also removes quite a bunch of callbacks from tests that
don't need to be callbacks.

* I'm also replacing all json_encode().

* To make the test code more readable, I shorten a bunch of variable
names to e.g. $msg. The fact they are mocks is still relevant, and still
visible because these variable names are only used in very short scopes.

Change-Id: I2bd7c731efd815bcdc5d33bccb0c8e280d55bd06
2019-12-12 08:48:02 +01:00

94 lines
2.6 KiB
PHP

<?php
namespace Cite\Tests\Unit;
use Cite\AnchorFormatter;
use Cite\ReferenceMessageLocalizer;
use MediaWikiUnitTestCase;
use Message;
use Wikimedia\TestingAccessWrapper;
/**
* @coversDefaultClass \Cite\AnchorFormatter
*
* @license GPL-2.0-or-later
*/
class AnchorFormatterTest extends MediaWikiUnitTestCase {
public function setUp() : void {
parent::setUp();
global $wgFragmentMode;
$wgFragmentMode = [ 'html5' ];
}
/**
* @covers ::refKey
*/
public function testRefKey() {
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
function ( ...$args ) {
$msg = $this->createMock( Message::class );
$msg->method( 'plain' )->willReturn( '(plain:' . implode( '|', $args ) . ')' );
$msg->method( 'text' )->willReturn( '(text:' . implode( '|', $args ) . ')' );
return $msg;
}
);
$formatter = new AnchorFormatter( $mockMessageLocalizer );
$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&#124;key&#124;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 ) {
$msg = $this->createMock( Message::class );
$msg->method( 'text' )->willReturn( '(' . implode( '|', $args ) . ')' );
return $msg;
}
);
$formatter = new AnchorFormatter( $mockMessageLocalizer );
$this->assertSame(
'(cite_references_link_prefix)key(cite_references_link_suffix)',
$formatter->getReferencesKey( 'key' ) );
}
/**
* @covers ::normalizeKey
* @covers ::__construct
* @dataProvider provideKeyNormalizations
*/
public function testNormalizeKey( $key, $expected ) {
/** @var AnchorFormatter $formatter */
$formatter = TestingAccessWrapper::newFromObject( new AnchorFormatter(
$this->createMock( ReferenceMessageLocalizer::class ) ) );
$this->assertSame( $expected, $formatter->normalizeKey( $key ) );
}
public function provideKeyNormalizations() {
return [
[ 'a b', 'a_b' ],
[ 'a __ b', 'a_b' ],
[ ':', ':' ],
[ "\t\n", '&#9;&#10;' ],
[ "'", '&#039;' ],
[ "''", '&#039;&#039;' ],
[ '"%&/<>?[]{|}', '&quot;%&amp;/&lt;&gt;?&#91;&#93;&#123;&#124;&#125;' ],
[ 'ISBN', '&#73;SBN' ],
];
}
}