From 430086cb6b73689ea11dc03875d106e92d8674f0 Mon Sep 17 00:00:00 2001 From: Adam Wight Date: Wed, 4 Dec 2019 17:53:05 +0100 Subject: [PATCH] Use the message localizer in Cite Allows us to convert another integration test into a unit test. Change-Id: Id7a4036e64920acdeccb4dfcf6bef31d0e5657ab --- src/Cite.php | 11 ++- tests/phpunit/CiteTest.php | 112 ---------------------------- tests/phpunit/unit/CiteUnitTest.php | 106 +++++++++++++++++++++++++- 3 files changed, 113 insertions(+), 116 deletions(-) delete mode 100644 tests/phpunit/CiteTest.php diff --git a/src/Cite.php b/src/Cite.php index c1ce1d83a..6a7de1b23 100644 --- a/src/Cite.php +++ b/src/Cite.php @@ -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 = '

' . $headerMsg->escaped() diff --git a/tests/phpunit/CiteTest.php b/tests/phpunit/CiteTest.php deleted file mode 100644 index 8fcffe19b..000000000 --- a/tests/phpunit/CiteTest.php +++ /dev/null @@ -1,112 +0,0 @@ -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( '' ); - $spy->footnoteBodyFormatter = $mockFootnoteBodyFormatter; - $spy->isSectionPreview = $isSectionPreview; - - $output = $cite->checkRefsNoReferences( $isSectionPreview ); - $this->assertSame( $expectedOutput, $output ); - } - - public function provideCheckRefsNoReferences() { - return [ - 'Default group' => [ - [ - '' => [ - [ - 'name' => 'a', - ] - ] - ], - false, - '' - ], - 'Default group in preview' => [ - [ - '' => [ - [ - 'name' => 'a', - ] - ] - ], - true, - "\n" . '
' . - '

' . - 'Preview of references

' - ], - 'Named group' => [ - [ - 'foo' => [ - [ - 'name' => 'a', - ] - ] - ], - false, - "\n" . '
["cite_error_group_refs_without_references","foo"]' - ], - 'Named group in preview' => [ - [ - 'foo' => [ - [ - 'name' => 'a', - ] - ] - ], - true, - "\n" . '
' . - '

' . - 'Preview of references

' - ] - ]; - } -} diff --git a/tests/phpunit/unit/CiteUnitTest.php b/tests/phpunit/unit/CiteUnitTest.php index 7989da446..827442d6e 100644 --- a/tests/phpunit/unit/CiteUnitTest.php +++ b/tests/phpunit/unit/CiteUnitTest.php @@ -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( '' ); + $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, + '' + ], + 'Default group in preview' => [ + [ + '' => [ + [ + 'name' => 'a', + ] + ] + ], + true, + "\n" . '
' . + '

' . + '(cite_section_preview_references)

' + ], + 'Named group' => [ + [ + 'foo' => [ + [ + 'name' => 'a', + ] + ] + ], + false, + "\n" . '
(cite_error_group_refs_without_references|foo)' + ], + 'Named group in preview' => [ + [ + 'foo' => [ + [ + 'name' => 'a', + ] + ] + ], + true, + "\n" . '
' . + '

' . + '(cite_section_preview_references)

' + ] + ]; + } + /** * @covers ::validateRef * @dataProvider provideValidateRef