2019-12-06 12:09:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Cite\Tests;
|
|
|
|
|
|
|
|
use Cite\Cite;
|
2019-12-09 16:08:57 +00:00
|
|
|
use Cite\ErrorReporter;
|
2019-12-09 15:36:34 +00:00
|
|
|
use Cite\ReferencesFormatter;
|
2019-12-06 12:09:28 +00:00
|
|
|
use Cite\ReferenceStack;
|
2019-12-11 13:58:25 +00:00
|
|
|
use Language;
|
|
|
|
use Parser;
|
|
|
|
use ParserOptions;
|
2019-12-06 12:09:28 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @coversDefaultClass \Cite\Cite
|
|
|
|
*
|
|
|
|
* @license GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
class CiteIntegrationTest extends \MediaWikiIntegrationTestCase {
|
|
|
|
|
2021-07-22 05:44:15 +00:00
|
|
|
protected function setUp(): void {
|
2019-12-06 12:09:28 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->setMwGlobals( [
|
|
|
|
'wgLanguageCode' => 'qqx',
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::checkRefsNoReferences
|
|
|
|
* @dataProvider provideCheckRefsNoReferences
|
|
|
|
*/
|
|
|
|
public function testCheckRefsNoReferences(
|
|
|
|
array $initialRefs, bool $isSectionPreview, string $expectedOutput
|
|
|
|
) {
|
|
|
|
global $wgCiteResponsiveReferences;
|
|
|
|
$wgCiteResponsiveReferences = true;
|
|
|
|
|
2019-12-09 16:08:57 +00:00
|
|
|
$mockErrorReporter = $this->createMock( ErrorReporter::class );
|
2019-12-06 12:09:28 +00:00
|
|
|
$mockErrorReporter->method( 'halfParsed' )->willReturnCallback(
|
2021-05-03 07:03:03 +00:00
|
|
|
static function ( $parser, ...$args ) {
|
2019-12-06 12:09:28 +00:00
|
|
|
return '(' . implode( '|', $args ) . ')';
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2023-06-05 14:36:03 +00:00
|
|
|
$referenceStack = new ReferenceStack( $mockErrorReporter );
|
|
|
|
TestingAccessWrapper::newFromObject( $referenceStack )->refs = $initialRefs;
|
2020-01-20 15:27:53 +00:00
|
|
|
|
2019-12-09 15:36:34 +00:00
|
|
|
$referencesFormatter = $this->createMock( ReferencesFormatter::class );
|
|
|
|
$referencesFormatter->method( 'formatReferences' )->willReturn( '<references />' );
|
2019-12-06 12:09:28 +00:00
|
|
|
|
2019-12-11 13:58:25 +00:00
|
|
|
$cite = $this->newCite();
|
2019-12-06 12:09:28 +00:00
|
|
|
/** @var Cite $spy */
|
|
|
|
$spy = TestingAccessWrapper::newFromObject( $cite );
|
2020-01-20 15:27:53 +00:00
|
|
|
$spy->referenceStack = $referenceStack;
|
2019-12-06 12:09:28 +00:00
|
|
|
$spy->errorReporter = $mockErrorReporter;
|
2019-12-09 15:36:34 +00:00
|
|
|
$spy->referencesFormatter = $referencesFormatter;
|
2019-12-06 12:09:28 +00:00
|
|
|
$spy->isSectionPreview = $isSectionPreview;
|
|
|
|
|
2019-12-11 15:02:49 +00:00
|
|
|
$output = $cite->checkRefsNoReferences(
|
|
|
|
$this->createMock( Parser::class ), $isSectionPreview );
|
2019-12-06 12:09:28 +00:00
|
|
|
$this->assertSame( $expectedOutput, $output );
|
|
|
|
}
|
|
|
|
|
2023-05-20 10:03:41 +00:00
|
|
|
public static function provideCheckRefsNoReferences() {
|
2019-12-06 12:09:28 +00:00
|
|
|
return [
|
|
|
|
'Default group' => [
|
2020-10-08 09:07:35 +00:00
|
|
|
[ '' => [ [ 'name' => 'a' ] ] ],
|
2019-12-06 12:09:28 +00:00
|
|
|
false,
|
2020-05-06 15:36:47 +00:00
|
|
|
"\n<references />"
|
2019-12-06 12:09:28 +00:00
|
|
|
],
|
|
|
|
'Default group in preview' => [
|
2020-10-08 09:07:35 +00:00
|
|
|
[ '' => [ [ 'name' => 'a' ] ] ],
|
2019-12-06 12:09:28 +00:00
|
|
|
true,
|
2019-12-09 14:07:37 +00:00
|
|
|
"\n" . '<div class="mw-ext-cite-cite_section_preview_references">' .
|
|
|
|
'<h2 id="mw-ext-cite-cite_section_preview_references_header">' .
|
2020-05-06 15:36:47 +00:00
|
|
|
'(cite_section_preview_references)</h2><references /></div>'
|
2019-12-06 12:09:28 +00:00
|
|
|
],
|
|
|
|
'Named group' => [
|
2020-10-08 09:07:35 +00:00
|
|
|
[ 'foo' => [ [ 'name' => 'a' ] ] ],
|
2019-12-06 12:09:28 +00:00
|
|
|
false,
|
|
|
|
"\n" . '<br />(cite_error_group_refs_without_references|foo)'
|
|
|
|
],
|
|
|
|
'Named group in preview' => [
|
2020-10-08 09:07:35 +00:00
|
|
|
[ 'foo' => [ [ 'name' => 'a' ] ] ],
|
2019-12-06 12:09:28 +00:00
|
|
|
true,
|
2019-12-09 14:07:37 +00:00
|
|
|
"\n" . '<div class="mw-ext-cite-cite_section_preview_references">' .
|
|
|
|
'<h2 id="mw-ext-cite-cite_section_preview_references_header">' .
|
2020-05-06 15:36:47 +00:00
|
|
|
'(cite_section_preview_references)</h2><references /></div>'
|
2019-12-06 12:09:28 +00:00
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-07-22 05:44:15 +00:00
|
|
|
private function newCite(): Cite {
|
2019-12-11 13:58:25 +00:00
|
|
|
$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 ) );
|
|
|
|
return new Cite( $mockParser );
|
|
|
|
}
|
|
|
|
|
2019-12-06 12:09:28 +00:00
|
|
|
}
|