mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Cite
synced 2024-11-12 09:10:32 +00:00
Tests for guardedRef
This is a mess of a function, and the tests show it. There are lots of side-effects and context-sensitivity, which can be addressed in later work. The interface with ReferenceStack is too wide. Change-Id: I00cab2a555b2a9efd32d937979cd722d43ac1005
This commit is contained in:
parent
31bda4777b
commit
3d049159c2
|
@ -4,6 +4,7 @@ namespace Cite\Tests\Unit;
|
|||
|
||||
use Cite\Cite;
|
||||
use Cite\CiteErrorReporter;
|
||||
use Cite\FootnoteMarkFormatter;
|
||||
use Cite\ReferenceStack;
|
||||
use Parser;
|
||||
use ParserOutput;
|
||||
|
@ -163,6 +164,184 @@ class CiteUnitTest extends \MediaWikiUnitTestCase {
|
|||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::guardedRef
|
||||
* @dataProvider provideGuardedRef
|
||||
*/
|
||||
public function testGuardedRef(
|
||||
string $text,
|
||||
array $argv,
|
||||
?string $inReferencesGroup,
|
||||
array $initialRefs,
|
||||
string $expectOutput,
|
||||
array $expectedErrors,
|
||||
array $expectedRefs
|
||||
) {
|
||||
/** @var (array|false)[] $pushedRefs Jumble of raw arguments, to roughly emulate
|
||||
* ReferenceStack.
|
||||
*/
|
||||
$pushedRefs = [];
|
||||
|
||||
$mockParser = $this->createMock( Parser::class );
|
||||
$mockParser->method( 'getStripState' )
|
||||
->willReturn( $this->createMock( StripState::class ) );
|
||||
$cite = new Cite();
|
||||
$spy = TestingAccessWrapper::newFromObject( $cite );
|
||||
$mockErrorReporter = $this->createMock( CiteErrorReporter::class );
|
||||
$mockErrorReporter->method( 'halfParsed' )->willReturnCallback(
|
||||
function ( ...$args ) {
|
||||
return json_encode( $args );
|
||||
}
|
||||
);
|
||||
$mockErrorReporter->method( 'plain' )->willReturnCallback(
|
||||
function ( ...$args ) {
|
||||
return json_encode( $args );
|
||||
}
|
||||
);
|
||||
/** @var CiteErrorReporter $mockErrorReporter */
|
||||
$spy->errorReporter = $mockErrorReporter;
|
||||
$mockFootnoteMarkFormatter = $this->createMock( FootnoteMarkFormatter::class );
|
||||
$mockFootnoteMarkFormatter->method( 'linkRef' )->willReturn( '<foot />' );
|
||||
$spy->footnoteMarkFormatter = $mockFootnoteMarkFormatter;
|
||||
$spy->inReferencesGroup = $inReferencesGroup;
|
||||
$spy->referenceStack = $this->createMock( ReferenceStack::class );
|
||||
$spy->referenceStack->method( 'getGroupRefs' )->willReturnCallback(
|
||||
function ( $group ) use ( $initialRefs ) {
|
||||
return $initialRefs[$group];
|
||||
} );
|
||||
$spy->referenceStack->method( 'hasGroup' )->willReturn( true );
|
||||
$spy->referenceStack->method( 'pushInvalidRef' )->willReturnCallback(
|
||||
function () use ( &$pushedRefs ) {
|
||||
$pushedRefs[] = false;
|
||||
}
|
||||
);
|
||||
$spy->referenceStack->method( 'pushRef' )->willReturnCallback(
|
||||
function (
|
||||
$text, $name, $group, $extends, $follow, $argv, $dir, $_stripState
|
||||
) use ( &$pushedRefs ) {
|
||||
$pushedRefs[] = [ $text, $name, $group, $extends, $follow, $argv, $dir ];
|
||||
return [
|
||||
'name' => $name,
|
||||
];
|
||||
}
|
||||
);
|
||||
$spy->referenceStack->method( 'setRefText' )->willReturnCallback(
|
||||
function ( $group, $name, $text ) use ( &$pushedRefs ) {
|
||||
$pushedRefs[] = [ 'setRefText', $group, $name, $text ];
|
||||
}
|
||||
);
|
||||
|
||||
$result = $spy->guardedRef( $text, $argv, $mockParser );
|
||||
$this->assertSame( $expectOutput, $result );
|
||||
$this->assertSame( $expectedErrors, $spy->mReferencesErrors );
|
||||
$this->assertSame( $expectedRefs, $pushedRefs );
|
||||
}
|
||||
|
||||
public function provideGuardedRef() {
|
||||
return [
|
||||
'Whitespace text' => [
|
||||
' ',
|
||||
[
|
||||
'name' => 'a',
|
||||
],
|
||||
null,
|
||||
[],
|
||||
'<foot />',
|
||||
[],
|
||||
[
|
||||
[ null, 'a', '', null, null, [ 'name' => 'a' ], null ]
|
||||
]
|
||||
],
|
||||
'Empty in default references' => [
|
||||
'',
|
||||
[],
|
||||
'',
|
||||
[],
|
||||
'',
|
||||
[ '["cite_error_references_no_key"]' ],
|
||||
[]
|
||||
],
|
||||
'Fallback to references group' => [
|
||||
'text',
|
||||
[
|
||||
'name' => 'a',
|
||||
],
|
||||
'foo',
|
||||
[
|
||||
'foo' => [
|
||||
'a' => []
|
||||
]
|
||||
],
|
||||
'',
|
||||
[],
|
||||
[
|
||||
[ 'setRefText', 'foo', 'a', 'text' ]
|
||||
]
|
||||
],
|
||||
'Successful ref' => [
|
||||
'text',
|
||||
[
|
||||
'name' => 'a',
|
||||
],
|
||||
null,
|
||||
[],
|
||||
'<foot />',
|
||||
[],
|
||||
[
|
||||
[ 'text', 'a', '', null, null, [ 'name' => 'a' ], null ]
|
||||
]
|
||||
],
|
||||
'Invalid ref' => [
|
||||
'text',
|
||||
[
|
||||
'name' => 'a',
|
||||
'badkey' => 'b',
|
||||
],
|
||||
null,
|
||||
[],
|
||||
'["cite_error_ref_too_many_keys"]',
|
||||
[],
|
||||
[ false ]
|
||||
],
|
||||
'Successful references ref' => [
|
||||
'text',
|
||||
[
|
||||
'name' => 'a',
|
||||
],
|
||||
'',
|
||||
[
|
||||
'' => [
|
||||
'a' => []
|
||||
]
|
||||
],
|
||||
'',
|
||||
[],
|
||||
[
|
||||
[ 'setRefText', '', 'a', 'text' ]
|
||||
]
|
||||
],
|
||||
'Mismatched text in references' => [
|
||||
'text-2',
|
||||
[
|
||||
'name' => 'a',
|
||||
],
|
||||
'',
|
||||
[
|
||||
'' => [
|
||||
'a' => [
|
||||
'text' => 'text-1',
|
||||
]
|
||||
]
|
||||
],
|
||||
'',
|
||||
[],
|
||||
[
|
||||
[ 'setRefText', '', 'a', 'text-1 ["cite_error_references_duplicate_key","a"]' ]
|
||||
]
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::guardedRef
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue