Use the message localizer in Cite

Allows us to convert another integration test into a unit test.

Change-Id: Id7a4036e64920acdeccb4dfcf6bef31d0e5657ab
This commit is contained in:
Adam Wight 2019-12-04 17:53:05 +01:00 committed by WMDE-Fisch
parent c48c2f7942
commit 430086cb6b
3 changed files with 113 additions and 116 deletions

View file

@ -99,6 +99,11 @@ class Cite {
*/
private $referenceStack;
/**
* @var ReferenceMessageLocalizer $messageLocalizer
*/
private $messageLocalizer;
/**
* @param Parser $parser
*/
@ -112,12 +117,12 @@ class Cite {
$parser
);
$this->referenceStack = new ReferenceStack( $this->errorReporter );
$messageLocalizer = new ReferenceMessageLocalizer( $parser->getContentLanguage() );
$this->messageLocalizer = new ReferenceMessageLocalizer( $parser->getContentLanguage() );
$citeKeyFormatter = new CiteKeyFormatter();
$this->footnoteMarkFormatter = new FootnoteMarkFormatter(
$this->mParser, $this->errorReporter, $citeKeyFormatter );
$this->footnoteBodyFormatter = new FootnoteBodyFormatter(
$this->mParser, $this->errorReporter, $citeKeyFormatter, $messageLocalizer );
$this->mParser, $this->errorReporter, $citeKeyFormatter, $this->messageLocalizer );
}
}
@ -543,7 +548,7 @@ class Cite {
}
}
if ( $isSectionPreview && $s !== '' ) {
$headerMsg = wfMessage( 'cite_section_preview_references' );
$headerMsg = $this->messageLocalizer->msg( 'cite_section_preview_references' );
if ( !$headerMsg->isDisabled() ) {
$s = '<h2 id="mw-ext-cite-cite_section_preview_references_header" >'
. $headerMsg->escaped()

View file

@ -1,112 +0,0 @@
<?php
namespace Cite\Tests;
use Cite\Cite;
use Cite\CiteErrorReporter;
use Cite\FootnoteBodyFormatter;
use Cite\ReferenceStack;
use Wikimedia\TestingAccessWrapper;
/**
* @coversDefaultClass \Cite\Cite
*
* @license GPL-2.0-or-later
*/
class CiteTest extends \MediaWikiIntegrationTestCase {
protected function setUp(): void {
parent::setUp();
$this->setMwGlobals( [
'wgCiteBookReferencing' => true,
] );
}
/**
* @covers ::checkRefsNoReferences
* @dataProvider provideCheckRefsNoReferences
*/
public function testCheckRefsNoReferences(
array $initialRefs, bool $isSectionPreview, string $expectedOutput
) {
$cite = new Cite();
$spy = TestingAccessWrapper::newFromObject( $cite );
$mockReferenceStack = $this->createMock( ReferenceStack::class );
$mockReferenceStack->method( 'getGroups' )->willReturn( array_keys( $initialRefs ) );
$mockReferenceStack->method( 'getGroupRefs' )->willReturnCallback( function ( $group ) use (
$initialRefs
) {
return $initialRefs[$group];
} );
$spy->referenceStack = $mockReferenceStack;
$mockErrorReporter = $this->createMock( CiteErrorReporter::class );
$mockErrorReporter->method( 'halfParsed' )->willReturnCallback(
function ( ...$args ) {
return json_encode( $args );
}
);
/** @var CiteErrorReporter $mockErrorReporter */
$spy->errorReporter = $mockErrorReporter;
$mockFootnoteBodyFormatter = $this->createMock( FootnoteBodyFormatter::class );
$mockFootnoteBodyFormatter->method( 'referencesFormat' )->willReturn( '<references />' );
$spy->footnoteBodyFormatter = $mockFootnoteBodyFormatter;
$spy->isSectionPreview = $isSectionPreview;
$output = $cite->checkRefsNoReferences( $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" >' .
'Preview of 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" >' .
'Preview of references</h2><references /></div>'
]
];
}
}

View file

@ -4,9 +4,11 @@ namespace Cite\Tests\Unit;
use Cite\Cite;
use Cite\CiteErrorReporter;
use Cite\FootnoteMarkFormatter;
use Cite\FootnoteBodyFormatter;
use Cite\FootnoteMarkFormatter;
use Cite\ReferenceMessageLocalizer;
use Cite\ReferenceStack;
use Message;
use Parser;
use ParserOutput;
use StripState;
@ -26,6 +28,108 @@ class CiteUnitTest extends \MediaWikiUnitTestCase {
$wgCiteBookReferencing = true;
}
/**
* @covers ::checkRefsNoReferences
* @dataProvider provideCheckRefsNoReferences
*/
public function testCheckRefsNoReferences(
array $initialRefs, bool $isSectionPreview, string $expectedOutput
) {
global $wgCiteResponsiveReferences;
$wgCiteResponsiveReferences = true;
$cite = new Cite();
$spy = TestingAccessWrapper::newFromObject( $cite );
$mockReferenceStack = $this->createMock( ReferenceStack::class );
$mockReferenceStack->method( 'getGroups' )->willReturn( array_keys( $initialRefs ) );
$mockReferenceStack->method( 'getGroupRefs' )->willReturnCallback( function ( $group ) use (
$initialRefs
) {
return $initialRefs[$group];
} );
$spy->referenceStack = $mockReferenceStack;
$mockErrorReporter = $this->createMock( CiteErrorReporter::class );
$mockErrorReporter->method( 'halfParsed' )->willReturnCallback(
function ( ...$args ) {
return '(' . implode( '|', $args ) . ')';
}
);
/** @var CiteErrorReporter $mockErrorReporter */
$spy->errorReporter = $mockErrorReporter;
$mockFootnoteBodyFormatter = $this->createMock( FootnoteBodyFormatter::class );
$mockFootnoteBodyFormatter->method( 'referencesFormat' )->willReturn( '<references />' );
$spy->footnoteBodyFormatter = $mockFootnoteBodyFormatter;
$spy->isSectionPreview = $isSectionPreview;
$spy->messageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
$spy->messageLocalizer->method( 'msg' )->willReturnCallback(
function ( ...$args ) {
$mockMessage = $this->createMock( Message::class );
$mockMessage->method( 'escaped' )->willReturnCallback(
function () use ( $args ) {
return '(' . implode( '|', $args ) . ')';
}
);
return $mockMessage;
}
);
$output = $cite->checkRefsNoReferences( $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>'
]
];
}
/**
* @covers ::validateRef
* @dataProvider provideValidateRef