mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Cite
synced 2024-12-04 19:38:16 +00:00
013e1bfa90
* Always have an empty line between @param and @return to improve readability as well as consistency within this codebase (before, both styles have been used). * Flip parameter order in validateRefInReferences() for consistency with the rest of the code. * In Cite::guardedRef() the Parser was now the 1st parameter. I changed all related functions the same way to make the code less surprising. * Same in CiteUnitTest. This is really just the @dataProvider. But I feel it's still helpful to have the arguments in the same order everywhere, if possible. * Add a few strict type hints. * It seems the preferred style for PHP7 return types is `… ) : string {` with a space before the `:`. There is currently no PHPCS sniff for this. However, I think this codebase should be consistent, one way or the other. Change-Id: I91d232be727afd26ff20526ab4ef63aa5ba6bacf
138 lines
3.5 KiB
PHP
138 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace Cite\Tests;
|
|
|
|
use Cite\Cite;
|
|
use Cite\ErrorReporter;
|
|
use Cite\ReferencesFormatter;
|
|
use Cite\ReferenceStack;
|
|
use Language;
|
|
use Parser;
|
|
use ParserOptions;
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
/**
|
|
* @coversDefaultClass \Cite\Cite
|
|
*
|
|
* @license GPL-2.0-or-later
|
|
*/
|
|
class CiteIntegrationTest extends \MediaWikiIntegrationTestCase {
|
|
|
|
protected function setUp() : void {
|
|
parent::setUp();
|
|
|
|
$this->setMwGlobals( [
|
|
'wgLanguageCode' => 'qqx',
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* @covers ::checkRefsNoReferences
|
|
* @dataProvider provideCheckRefsNoReferences
|
|
*/
|
|
public function testCheckRefsNoReferences(
|
|
array $initialRefs, bool $isSectionPreview, string $expectedOutput
|
|
) {
|
|
global $wgCiteResponsiveReferences;
|
|
$wgCiteResponsiveReferences = true;
|
|
|
|
$mockReferenceStack = $this->createMock( ReferenceStack::class );
|
|
$mockReferenceStack->method( 'getGroups' )->willReturn( array_keys( $initialRefs ) );
|
|
$mockReferenceStack->method( 'popGroup' )->willReturnCallback( function ( $group ) use (
|
|
$initialRefs
|
|
) {
|
|
return $initialRefs[$group];
|
|
} );
|
|
|
|
$mockErrorReporter = $this->createMock( ErrorReporter::class );
|
|
$mockErrorReporter->method( 'halfParsed' )->willReturnCallback(
|
|
function ( $parser, ...$args ) {
|
|
return '(' . implode( '|', $args ) . ')';
|
|
}
|
|
);
|
|
|
|
$referencesFormatter = $this->createMock( ReferencesFormatter::class );
|
|
$referencesFormatter->method( 'formatReferences' )->willReturn( '<references />' );
|
|
|
|
$cite = $this->newCite();
|
|
/** @var Cite $spy */
|
|
$spy = TestingAccessWrapper::newFromObject( $cite );
|
|
$spy->referenceStack = $mockReferenceStack;
|
|
$spy->errorReporter = $mockErrorReporter;
|
|
$spy->referencesFormatter = $referencesFormatter;
|
|
$spy->isSectionPreview = $isSectionPreview;
|
|
|
|
$output = $cite->checkRefsNoReferences(
|
|
$this->createMock( Parser::class ), $isSectionPreview );
|
|
$this->assertSame( $expectedOutput, $output );
|
|
}
|
|
|
|
public function provideCheckRefsNoReferences() {
|
|
return [
|
|
'Default group' => [
|
|
[
|
|
'' => [
|
|
[
|
|
'name' => 'a',
|
|
]
|
|
]
|
|
],
|
|
false,
|
|
'<references />'
|
|
],
|
|
'Default group in preview' => [
|
|
[
|
|
'' => [
|
|
[
|
|
'name' => 'a',
|
|
]
|
|
]
|
|
],
|
|
true,
|
|
"\n" . '<div class="mw-ext-cite-cite_section_preview_references">' .
|
|
'<h2 id="mw-ext-cite-cite_section_preview_references_header">' .
|
|
'(cite_section_preview_references)</h2><references /></div>'
|
|
],
|
|
'Named group' => [
|
|
[
|
|
'foo' => [
|
|
[
|
|
'name' => 'a',
|
|
]
|
|
]
|
|
],
|
|
false,
|
|
"\n" . '<br />(cite_error_group_refs_without_references|foo)'
|
|
],
|
|
'Named group in preview' => [
|
|
[
|
|
'foo' => [
|
|
[
|
|
'name' => 'a',
|
|
]
|
|
]
|
|
],
|
|
true,
|
|
"\n" . '<div class="mw-ext-cite-cite_section_preview_references">' .
|
|
'<h2 id="mw-ext-cite-cite_section_preview_references_header">' .
|
|
'(cite_section_preview_references)</h2><references /></div>'
|
|
]
|
|
];
|
|
}
|
|
|
|
private function newCite() : Cite {
|
|
$mockOptions = $this->createMock( ParserOptions::class );
|
|
$mockOptions->method( 'getIsPreview' )->willReturn( false );
|
|
$mockOptions->method( 'getIsSectionPreview' )->willReturn( false );
|
|
$mockOptions->method( 'getUserLangObj' )->willReturn(
|
|
$this->createMock( Language::class ) );
|
|
$mockParser = $this->createMock( Parser::class );
|
|
$mockParser->method( 'getOptions' )->willReturn( $mockOptions );
|
|
$mockParser->method( 'getContentLanguage' )->willReturn(
|
|
$this->createMock( Language::class ) );
|
|
/** @var Parser $mockParser */
|
|
return new Cite( $mockParser );
|
|
}
|
|
|
|
}
|