mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Cite
synced 2024-11-13 17:53:09 +00:00
6a4a0fd013
… if possible. In most cases it's possible to use the real object, and reach into it's private parts via TestingAccessWrapper. This is almost the same as using a mock, but I feel it's much more "light-weight". The main change is that there is no strict assertion any more for the number of ReferenceStack::pushInvalidRef() calls. Before this was mixed into the same array as the valid references, as elements set to "false". I think the test is as valueable as before without this extra check. If the rollback stack works or not is already covered by other tests. Change-Id: I90213557b164b3e43233a3dc393ee3f3d3d556a9
134 lines
3.4 KiB
PHP
134 lines
3.4 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;
|
|
|
|
$mockErrorReporter = $this->createMock( ErrorReporter::class );
|
|
$mockErrorReporter->method( 'halfParsed' )->willReturnCallback(
|
|
function ( $parser, ...$args ) {
|
|
return '(' . implode( '|', $args ) . ')';
|
|
}
|
|
);
|
|
|
|
/** @var ReferenceStack $referenceStack */
|
|
$referenceStack = TestingAccessWrapper::newFromObject( new ReferenceStack( $mockErrorReporter ) );
|
|
$referenceStack->refs = $initialRefs;
|
|
|
|
$referencesFormatter = $this->createMock( ReferencesFormatter::class );
|
|
$referencesFormatter->method( 'formatReferences' )->willReturn( '<references />' );
|
|
|
|
$cite = $this->newCite();
|
|
/** @var Cite $spy */
|
|
$spy = TestingAccessWrapper::newFromObject( $cite );
|
|
$spy->referenceStack = $referenceStack;
|
|
$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 );
|
|
}
|
|
|
|
}
|