mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Cite
synced 2024-11-27 16:30:12 +00:00
Rename two Cite… classes and clean up test setups
* All classes are in a Cite\ namespace now. No need to repeat the word "Cite" all over the place. * The "key formatter" is more an ID or anchor formatter. The strings it returns are all used in id="…" attributes, as well as in href="#…" links to jump to these IDs. * This patch also removes quite a bunch of callbacks from tests that don't need to be callbacks. * I'm also replacing all json_encode(). * To make the test code more readable, I shorten a bunch of variable names to e.g. $msg. The fact they are mocks is still relevant, and still visible because these variable names are only used in very short scopes. Change-Id: I2bd7c731efd815bcdc5d33bccb0c8e280d55bd06
This commit is contained in:
parent
69a8754e31
commit
3f2aeb7e31
|
@ -7,7 +7,7 @@ use Sanitizer;
|
|||
/**
|
||||
* @license GPL-2.0-or-later
|
||||
*/
|
||||
class CiteKeyFormatter {
|
||||
class AnchorFormatter {
|
||||
|
||||
/**
|
||||
* @var ReferenceMessageLocalizer
|
||||
|
@ -17,9 +17,7 @@ class CiteKeyFormatter {
|
|||
/**
|
||||
* @param ReferenceMessageLocalizer $messageLocalizer
|
||||
*/
|
||||
public function __construct(
|
||||
ReferenceMessageLocalizer $messageLocalizer
|
||||
) {
|
||||
public function __construct( ReferenceMessageLocalizer $messageLocalizer ) {
|
||||
$this->messageLocalizer = $messageLocalizer;
|
||||
}
|
||||
|
10
src/Cite.php
10
src/Cite.php
|
@ -68,7 +68,7 @@ class Cite {
|
|||
private $referencesFormatter;
|
||||
|
||||
/**
|
||||
* @var CiteErrorReporter
|
||||
* @var ErrorReporter
|
||||
*/
|
||||
private $errorReporter;
|
||||
|
||||
|
@ -107,13 +107,13 @@ class Cite {
|
|||
$this->isPagePreview = $parser->getOptions()->getIsPreview();
|
||||
$this->isSectionPreview = $parser->getOptions()->getIsSectionPreview();
|
||||
$messageLocalizer = new ReferenceMessageLocalizer( $parser->getContentLanguage() );
|
||||
$this->errorReporter = new CiteErrorReporter( $parser, $messageLocalizer );
|
||||
$this->errorReporter = new ErrorReporter( $parser, $messageLocalizer );
|
||||
$this->referenceStack = new ReferenceStack( $this->errorReporter );
|
||||
$citeKeyFormatter = new CiteKeyFormatter( $messageLocalizer );
|
||||
$anchorFormatter = new AnchorFormatter( $messageLocalizer );
|
||||
$this->footnoteMarkFormatter = new FootnoteMarkFormatter(
|
||||
$this->mParser, $this->errorReporter, $citeKeyFormatter, $messageLocalizer );
|
||||
$this->mParser, $this->errorReporter, $anchorFormatter, $messageLocalizer );
|
||||
$this->referencesFormatter = new ReferencesFormatter(
|
||||
$this->mParser, $this->errorReporter, $citeKeyFormatter, $messageLocalizer );
|
||||
$this->mParser, $this->errorReporter, $anchorFormatter, $messageLocalizer );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ use Sanitizer;
|
|||
/**
|
||||
* @license GPL-2.0-or-later
|
||||
*/
|
||||
class CiteErrorReporter {
|
||||
class ErrorReporter {
|
||||
|
||||
/**
|
||||
* @var Parser
|
|
@ -21,12 +21,12 @@ class FootnoteMarkFormatter {
|
|||
private $parser;
|
||||
|
||||
/**
|
||||
* @var CiteKeyFormatter
|
||||
* @var AnchorFormatter
|
||||
*/
|
||||
private $citeKeyFormatter;
|
||||
private $anchorFormatter;
|
||||
|
||||
/**
|
||||
* @var CiteErrorReporter
|
||||
* @var ErrorReporter
|
||||
*/
|
||||
private $errorReporter;
|
||||
|
||||
|
@ -37,18 +37,18 @@ class FootnoteMarkFormatter {
|
|||
|
||||
/**
|
||||
* @param Parser $parser
|
||||
* @param CiteErrorReporter $errorReporter
|
||||
* @param CiteKeyFormatter $citeKeyFormatter
|
||||
* @param ErrorReporter $errorReporter
|
||||
* @param AnchorFormatter $anchorFormatter
|
||||
* @param ReferenceMessageLocalizer $messageLocalizer
|
||||
*/
|
||||
public function __construct(
|
||||
Parser $parser,
|
||||
CiteErrorReporter $errorReporter,
|
||||
CiteKeyFormatter $citeKeyFormatter,
|
||||
ErrorReporter $errorReporter,
|
||||
AnchorFormatter $anchorFormatter,
|
||||
ReferenceMessageLocalizer $messageLocalizer
|
||||
) {
|
||||
$this->parser = $parser;
|
||||
$this->citeKeyFormatter = $citeKeyFormatter;
|
||||
$this->anchorFormatter = $anchorFormatter;
|
||||
$this->errorReporter = $errorReporter;
|
||||
$this->messageLocalizer = $messageLocalizer;
|
||||
}
|
||||
|
@ -82,8 +82,8 @@ class FootnoteMarkFormatter {
|
|||
return $this->parser->recursiveTagParse(
|
||||
$this->messageLocalizer->msg(
|
||||
'cite_reference_link',
|
||||
$this->citeKeyFormatter->refKey( $key, $count ),
|
||||
$this->citeKeyFormatter->getReferencesKey( $key . $subkey ),
|
||||
$this->anchorFormatter->refKey( $key, $count ),
|
||||
$this->anchorFormatter->getReferencesKey( $key . $subkey ),
|
||||
Sanitizer::safeEncodeAttribute( $label )
|
||||
)->plain()
|
||||
);
|
||||
|
|
|
@ -82,14 +82,14 @@ class ReferenceStack {
|
|||
|
||||
/**
|
||||
* @deprecated We should be able to push this responsibility to calling code.
|
||||
* @var CiteErrorReporter
|
||||
* @var ErrorReporter
|
||||
*/
|
||||
private $errorReporter;
|
||||
|
||||
/**
|
||||
* @param CiteErrorReporter $errorReporter
|
||||
* @param ErrorReporter $errorReporter
|
||||
*/
|
||||
public function __construct( CiteErrorReporter $errorReporter ) {
|
||||
public function __construct( ErrorReporter $errorReporter ) {
|
||||
$this->errorReporter = $errorReporter;
|
||||
}
|
||||
|
||||
|
|
|
@ -20,14 +20,14 @@ class ReferencesFormatter {
|
|||
private $backlinkLabels = null;
|
||||
|
||||
/**
|
||||
* @var CiteErrorReporter
|
||||
* @var ErrorReporter
|
||||
*/
|
||||
private $errorReporter;
|
||||
|
||||
/**
|
||||
* @var CiteKeyFormatter
|
||||
* @var AnchorFormatter
|
||||
*/
|
||||
private $citeKeyFormatter;
|
||||
private $anchorFormatter;
|
||||
|
||||
/**
|
||||
* @var Parser
|
||||
|
@ -41,18 +41,18 @@ class ReferencesFormatter {
|
|||
|
||||
/**
|
||||
* @param Parser $parser
|
||||
* @param CiteErrorReporter $errorReporter
|
||||
* @param CiteKeyFormatter $citeKeyFormatter
|
||||
* @param ErrorReporter $errorReporter
|
||||
* @param AnchorFormatter $anchorFormatter
|
||||
* @param ReferenceMessageLocalizer $messageLocalizer
|
||||
*/
|
||||
public function __construct(
|
||||
Parser $parser,
|
||||
CiteErrorReporter $errorReporter,
|
||||
CiteKeyFormatter $citeKeyFormatter,
|
||||
ErrorReporter $errorReporter,
|
||||
AnchorFormatter $anchorFormatter,
|
||||
ReferenceMessageLocalizer $messageLocalizer
|
||||
) {
|
||||
$this->errorReporter = $errorReporter;
|
||||
$this->citeKeyFormatter = $citeKeyFormatter;
|
||||
$this->anchorFormatter = $anchorFormatter;
|
||||
$this->parser = $parser;
|
||||
$this->messageLocalizer = $messageLocalizer;
|
||||
}
|
||||
|
@ -171,7 +171,7 @@ class ReferencesFormatter {
|
|||
if ( isset( $val['follow'] ) ) {
|
||||
return $this->messageLocalizer->msg(
|
||||
'cite_references_no_link',
|
||||
$this->citeKeyFormatter->getReferencesKey( $val['follow'] ),
|
||||
$this->anchorFormatter->getReferencesKey( $val['follow'] ),
|
||||
$text
|
||||
)->plain();
|
||||
}
|
||||
|
@ -181,14 +181,14 @@ class ReferencesFormatter {
|
|||
// Anonymous, auto-numbered references can't be reused and get marked with a -1.
|
||||
if ( $val['count'] < 0 ) {
|
||||
$id = $val['key'];
|
||||
$backlinkId = $this->citeKeyFormatter->refKey( $val['key'] );
|
||||
$backlinkId = $this->anchorFormatter->refKey( $val['key'] );
|
||||
} else {
|
||||
$id = $key . '-' . $val['key'];
|
||||
$backlinkId = $this->citeKeyFormatter->refKey( $key, $val['key'] . '-' . $val['count'] );
|
||||
$backlinkId = $this->anchorFormatter->refKey( $key, $val['key'] . '-' . $val['count'] );
|
||||
}
|
||||
return $this->messageLocalizer->msg(
|
||||
'cite_references_link_one',
|
||||
$this->citeKeyFormatter->getReferencesKey( $id ),
|
||||
$this->anchorFormatter->getReferencesKey( $id ),
|
||||
$backlinkId,
|
||||
$text . $error,
|
||||
$extraAttributes
|
||||
|
@ -201,7 +201,7 @@ class ReferencesFormatter {
|
|||
for ( $i = 0; $i <= ( $val['count'] ?? -1 ); $i++ ) {
|
||||
$backlinks[] = $this->messageLocalizer->msg(
|
||||
'cite_references_link_many_format',
|
||||
$this->citeKeyFormatter->refKey( $key, $val['key'] . '-' . $i ),
|
||||
$this->anchorFormatter->refKey( $key, $val['key'] . '-' . $i ),
|
||||
$this->referencesFormatEntryNumericBacklinkLabel(
|
||||
$val['number'] .
|
||||
( isset( $val['extendsIndex'] ) ? '.' . $val['extendsIndex'] : '' ),
|
||||
|
@ -213,7 +213,7 @@ class ReferencesFormatter {
|
|||
}
|
||||
return $this->messageLocalizer->msg(
|
||||
'cite_references_link_many',
|
||||
$this->citeKeyFormatter->getReferencesKey( $key . '-' . ( $val['key'] ?? '' ) ),
|
||||
$this->anchorFormatter->getReferencesKey( $key . '-' . ( $val['key'] ?? '' ) ),
|
||||
$this->listToText( $backlinks ),
|
||||
$text . $error,
|
||||
$extraAttributes
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
namespace Cite\Tests;
|
||||
|
||||
use Cite\Cite;
|
||||
use Cite\CiteErrorReporter;
|
||||
use Cite\ErrorReporter;
|
||||
use Cite\ReferencesFormatter;
|
||||
use Cite\ReferenceStack;
|
||||
use Wikimedia\TestingAccessWrapper;
|
||||
|
@ -41,7 +41,7 @@ class CiteIntegrationTest extends \MediaWikiIntegrationTestCase {
|
|||
return $initialRefs[$group];
|
||||
} );
|
||||
|
||||
$mockErrorReporter = $this->createMock( CiteErrorReporter::class );
|
||||
$mockErrorReporter = $this->createMock( ErrorReporter::class );
|
||||
$mockErrorReporter->method( 'halfParsed' )->willReturnCallback(
|
||||
function ( ...$args ) {
|
||||
return '(' . implode( '|', $args ) . ')';
|
||||
|
|
|
@ -2,18 +2,18 @@
|
|||
|
||||
namespace Cite\Tests\Unit;
|
||||
|
||||
use Cite\CiteKeyFormatter;
|
||||
use Cite\AnchorFormatter;
|
||||
use Cite\ReferenceMessageLocalizer;
|
||||
use MediaWikiUnitTestCase;
|
||||
use Message;
|
||||
use Wikimedia\TestingAccessWrapper;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Cite\CiteKeyFormatter
|
||||
* @coversDefaultClass \Cite\AnchorFormatter
|
||||
*
|
||||
* @license GPL-2.0-or-later
|
||||
*/
|
||||
class CiteKeyFormatterTest extends MediaWikiUnitTestCase {
|
||||
class AnchorFormatterTest extends MediaWikiUnitTestCase {
|
||||
|
||||
public function setUp() : void {
|
||||
parent::setUp();
|
||||
|
@ -29,21 +29,13 @@ class CiteKeyFormatterTest extends MediaWikiUnitTestCase {
|
|||
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
|
||||
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
|
||||
function ( ...$args ) {
|
||||
$mockMessage = $this->createMock( Message::class );
|
||||
$mockMessage->method( 'plain' )->willReturnCallback(
|
||||
function () use ( $args ) {
|
||||
return '(plain:' . implode( '|', $args ) . ')';
|
||||
}
|
||||
);
|
||||
$mockMessage->method( 'text' )->willReturnCallback(
|
||||
function () use ( $args ) {
|
||||
return '(text:' . implode( '|', $args ) . ')';
|
||||
}
|
||||
);
|
||||
return $mockMessage;
|
||||
$msg = $this->createMock( Message::class );
|
||||
$msg->method( 'plain' )->willReturn( '(plain:' . implode( '|', $args ) . ')' );
|
||||
$msg->method( 'text' )->willReturn( '(text:' . implode( '|', $args ) . ')' );
|
||||
return $msg;
|
||||
}
|
||||
);
|
||||
$formatter = new CiteKeyFormatter( $mockMessageLocalizer );
|
||||
$formatter = new AnchorFormatter( $mockMessageLocalizer );
|
||||
|
||||
$this->assertSame(
|
||||
'(text:cite_reference_link_prefix)key(text:cite_reference_link_suffix)',
|
||||
|
@ -61,16 +53,12 @@ class CiteKeyFormatterTest extends MediaWikiUnitTestCase {
|
|||
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
|
||||
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
|
||||
function ( ...$args ) {
|
||||
$mockMessage = $this->createMock( Message::class );
|
||||
$mockMessage->method( 'text' )->willReturnCallback(
|
||||
function () use ( $args ) {
|
||||
return '(' . implode( '|', $args ) . ')';
|
||||
}
|
||||
);
|
||||
return $mockMessage;
|
||||
$msg = $this->createMock( Message::class );
|
||||
$msg->method( 'text' )->willReturn( '(' . implode( '|', $args ) . ')' );
|
||||
return $msg;
|
||||
}
|
||||
);
|
||||
$formatter = new CiteKeyFormatter( $mockMessageLocalizer );
|
||||
$formatter = new AnchorFormatter( $mockMessageLocalizer );
|
||||
|
||||
$this->assertSame(
|
||||
'(cite_references_link_prefix)key(cite_references_link_suffix)',
|
||||
|
@ -83,8 +71,8 @@ class CiteKeyFormatterTest extends MediaWikiUnitTestCase {
|
|||
* @dataProvider provideKeyNormalizations
|
||||
*/
|
||||
public function testNormalizeKey( $key, $expected ) {
|
||||
/** @var CiteKeyFormatter $formatter */
|
||||
$formatter = TestingAccessWrapper::newFromObject( new CiteKeyFormatter(
|
||||
/** @var AnchorFormatter $formatter */
|
||||
$formatter = TestingAccessWrapper::newFromObject( new AnchorFormatter(
|
||||
$this->createMock( ReferenceMessageLocalizer::class ) ) );
|
||||
$this->assertSame( $expected, $formatter->normalizeKey( $key ) );
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Cite\Tests\Unit;
|
||||
|
||||
use Cite\CiteErrorReporter;
|
||||
use Cite\ErrorReporter;
|
||||
use Cite\ReferenceMessageLocalizer;
|
||||
use Language;
|
||||
use MediaWikiUnitTestCase;
|
||||
|
@ -11,7 +11,7 @@ use Parser;
|
|||
use ParserOptions;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Cite\CiteErrorReporter
|
||||
* @coversDefaultClass \Cite\ErrorReporter
|
||||
*
|
||||
* @license GPL-2.0-or-later
|
||||
*/
|
||||
|
@ -62,7 +62,7 @@ class CiteErrorReporterTest extends MediaWikiUnitTestCase {
|
|||
];
|
||||
}
|
||||
|
||||
private function createReporter( array $expectedCategories ) : CiteErrorReporter {
|
||||
private function createReporter( array $expectedCategories ) : ErrorReporter {
|
||||
$mockParser = $this->createMock( Parser::class );
|
||||
foreach ( $expectedCategories as $category ) {
|
||||
$mockParser->method( 'addTrackingCategory' )->with( $category );
|
||||
|
@ -98,7 +98,7 @@ class CiteErrorReporterTest extends MediaWikiUnitTestCase {
|
|||
|
||||
/** @var Parser $mockParser */
|
||||
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
|
||||
return new CiteErrorReporter( $mockParser, $mockMessageLocalizer );
|
||||
return new ErrorReporter( $mockParser, $mockMessageLocalizer );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
namespace Cite\Tests\Unit;
|
||||
|
||||
use Cite\Cite;
|
||||
use Cite\CiteErrorReporter;
|
||||
use Cite\ErrorReporter;
|
||||
use Cite\FootnoteMarkFormatter;
|
||||
use Cite\ReferencesFormatter;
|
||||
use Cite\ReferenceStack;
|
||||
|
@ -37,8 +37,8 @@ class CiteUnitTest extends MediaWikiUnitTestCase {
|
|||
?string $extends,
|
||||
$expected
|
||||
) {
|
||||
/** @var CiteErrorReporter $errorReporter */
|
||||
$errorReporter = $this->createMock( CiteErrorReporter::class );
|
||||
/** @var ErrorReporter $errorReporter */
|
||||
$errorReporter = $this->createMock( ErrorReporter::class );
|
||||
/** @var ReferenceStack $stack */
|
||||
$stack = TestingAccessWrapper::newFromObject( new ReferenceStack( $errorReporter ) );
|
||||
$stack->refs = $referencesStack;
|
||||
|
@ -323,11 +323,11 @@ class CiteUnitTest extends MediaWikiUnitTestCase {
|
|||
$cite = new Cite();
|
||||
/** @var Cite $spy */
|
||||
$spy = TestingAccessWrapper::newFromObject( $cite );
|
||||
/** @var CiteErrorReporter $mockErrorReporter */
|
||||
$spy->errorReporter = $this->createMock( CiteErrorReporter::class );
|
||||
/** @var ErrorReporter $mockErrorReporter */
|
||||
$spy->errorReporter = $this->createMock( ErrorReporter::class );
|
||||
$spy->errorReporter->method( 'halfParsed' )->willReturnCallback(
|
||||
function ( ...$args ) {
|
||||
return json_encode( $args );
|
||||
return '(' . implode( '|', $args ) . ')';
|
||||
}
|
||||
);
|
||||
$spy->referencesFormatter = $this->createMock( ReferencesFormatter::class );
|
||||
|
@ -389,7 +389,7 @@ class CiteUnitTest extends MediaWikiUnitTestCase {
|
|||
0,
|
||||
'',
|
||||
false,
|
||||
'["cite_error_references_invalid_parameters"]',
|
||||
'(cite_error_references_invalid_parameters)',
|
||||
],
|
||||
'Contains refs (which are broken)' => [
|
||||
Parser::MARKER_PREFIX . '-ref- and ' . Parser::MARKER_PREFIX . '-notref-',
|
||||
|
@ -397,7 +397,7 @@ class CiteUnitTest extends MediaWikiUnitTestCase {
|
|||
1,
|
||||
'',
|
||||
false,
|
||||
'references!' . "\n" . '["cite_error_references_no_key"]'
|
||||
'references!' . "\n" . '(cite_error_references_no_key)'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
@ -424,15 +424,15 @@ class CiteUnitTest extends MediaWikiUnitTestCase {
|
|||
$mockParser->method( 'getStripState' )
|
||||
->willReturn( $this->createMock( StripState::class ) );
|
||||
|
||||
$mockErrorReporter = $this->createMock( CiteErrorReporter::class );
|
||||
$mockErrorReporter = $this->createMock( ErrorReporter::class );
|
||||
$mockErrorReporter->method( 'halfParsed' )->willReturnCallback(
|
||||
function ( ...$args ) {
|
||||
return json_encode( $args );
|
||||
return '(' . implode( '|', $args ) . ')';
|
||||
}
|
||||
);
|
||||
$mockErrorReporter->method( 'plain' )->willReturnCallback(
|
||||
function ( ...$args ) {
|
||||
return json_encode( $args );
|
||||
return '(' . implode( '|', $args ) . ')';
|
||||
}
|
||||
);
|
||||
|
||||
|
@ -499,7 +499,7 @@ class CiteUnitTest extends MediaWikiUnitTestCase {
|
|||
'',
|
||||
[],
|
||||
'',
|
||||
[ '["cite_error_references_no_key"]' ],
|
||||
[ '(cite_error_references_no_key)' ],
|
||||
[]
|
||||
],
|
||||
'Fallback to references group' => [
|
||||
|
@ -540,7 +540,7 @@ class CiteUnitTest extends MediaWikiUnitTestCase {
|
|||
],
|
||||
null,
|
||||
[],
|
||||
'["cite_error_ref_too_many_keys"]',
|
||||
'(cite_error_ref_too_many_keys)',
|
||||
[],
|
||||
[ false ]
|
||||
],
|
||||
|
@ -577,7 +577,7 @@ class CiteUnitTest extends MediaWikiUnitTestCase {
|
|||
'',
|
||||
[],
|
||||
[
|
||||
[ 'setRefText', '', 'a', 'text-1 ["cite_error_references_duplicate_key","a"]' ]
|
||||
[ 'setRefText', '', 'a', 'text-1 (cite_error_references_duplicate_key|a)' ]
|
||||
]
|
||||
],
|
||||
];
|
||||
|
@ -602,7 +602,7 @@ class CiteUnitTest extends MediaWikiUnitTestCase {
|
|||
$cite = new Cite();
|
||||
/** @var Cite $spy */
|
||||
$spy = TestingAccessWrapper::newFromObject( $cite );
|
||||
$spy->errorReporter = $this->createMock( CiteErrorReporter::class );
|
||||
$spy->errorReporter = $this->createMock( ErrorReporter::class );
|
||||
$spy->referenceStack = $this->createMock( ReferenceStack::class );
|
||||
|
||||
$spy->guardedRef( 'text', [ Cite::BOOK_REF_ATTRIBUTE => 'a' ], $mockParser );
|
||||
|
|
|
@ -19,15 +19,15 @@ class FootnoteMarkFormatterTest extends MediaWikiUnitTestCase {
|
|||
public function testLinkRef( string $group, array $ref, string $expectedOutput ) {
|
||||
$fooLabels = 'a b c';
|
||||
|
||||
$mockErrorReporter = $this->createMock( CiteErrorReporter::class );
|
||||
$mockErrorReporter = $this->createMock( ErrorReporter::class );
|
||||
$mockErrorReporter->method( 'plain' )->willReturnCallback(
|
||||
function ( ...$args ) {
|
||||
return implode( '|', $args );
|
||||
}
|
||||
);
|
||||
$mockKeyFormatter = $this->createMock( CiteKeyFormatter::class );
|
||||
$mockKeyFormatter->method( 'getReferencesKey' )->willReturnArgument( 0 );
|
||||
$mockKeyFormatter->method( 'refKey' )->willReturnCallback(
|
||||
$anchorFormatter = $this->createMock( AnchorFormatter::class );
|
||||
$anchorFormatter->method( 'getReferencesKey' )->willReturnArgument( 0 );
|
||||
$anchorFormatter->method( 'refKey' )->willReturnCallback(
|
||||
function ( ...$args ) {
|
||||
return implode( '+', $args );
|
||||
}
|
||||
|
@ -37,18 +37,12 @@ class FootnoteMarkFormatterTest extends MediaWikiUnitTestCase {
|
|||
$mockMessageLocalizer->method( 'localizeDigits' )->willReturnArgument( 0 );
|
||||
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
|
||||
function ( ...$args ) use ( $group, $fooLabels ) {
|
||||
$mockMessage = $this->createMock( Message::class );
|
||||
$mockMessage->method( 'isDisabled' )->willReturn( $group !== 'foo' );
|
||||
if ( $args[0] === 'cite_reference_link' ) {
|
||||
$mockMessage->method( 'plain' )->willReturnCallback(
|
||||
function () use ( $args ) {
|
||||
return '(' . implode( '|', $args ) . ')';
|
||||
}
|
||||
);
|
||||
} else {
|
||||
$mockMessage->method( 'plain' )->willReturn( $fooLabels );
|
||||
}
|
||||
return $mockMessage;
|
||||
$msg = $this->createMock( Message::class );
|
||||
$msg->method( 'isDisabled' )->willReturn( $group !== 'foo' );
|
||||
$msg->method( 'plain' )->willReturn( $args[0] === 'cite_reference_link'
|
||||
? '(' . implode( '|', $args ) . ')'
|
||||
: $fooLabels );
|
||||
return $msg;
|
||||
}
|
||||
);
|
||||
$mockParser = $this->createMock( Parser::class );
|
||||
|
@ -57,7 +51,7 @@ class FootnoteMarkFormatterTest extends MediaWikiUnitTestCase {
|
|||
$formatter = TestingAccessWrapper::newFromObject( new FootnoteMarkFormatter(
|
||||
$mockParser,
|
||||
$mockErrorReporter,
|
||||
$mockKeyFormatter,
|
||||
$anchorFormatter,
|
||||
$mockMessageLocalizer ) );
|
||||
|
||||
$output = $formatter->linkRef( $group, $ref );
|
||||
|
@ -157,13 +151,13 @@ class FootnoteMarkFormatterTest extends MediaWikiUnitTestCase {
|
|||
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
|
||||
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
|
||||
function ( ...$args ) use ( $labelList ) {
|
||||
$mockMessage = $this->createMock( Message::class );
|
||||
$mockMessage->method( 'isDisabled' )->willReturn( $labelList === null );
|
||||
$mockMessage->method( 'plain' )->willReturn( $labelList );
|
||||
return $mockMessage;
|
||||
$msg = $this->createMock( Message::class );
|
||||
$msg->method( 'isDisabled' )->willReturn( $labelList === null );
|
||||
$msg->method( 'plain' )->willReturn( $labelList );
|
||||
return $msg;
|
||||
}
|
||||
);
|
||||
$mockErrorReporter = $this->createMock( CiteErrorReporter::class );
|
||||
$mockErrorReporter = $this->createMock( ErrorReporter::class );
|
||||
$mockErrorReporter->method( 'plain' )->willReturnCallback(
|
||||
function ( ...$args ) {
|
||||
return implode( '|', $args );
|
||||
|
@ -173,7 +167,7 @@ class FootnoteMarkFormatterTest extends MediaWikiUnitTestCase {
|
|||
$formatter = TestingAccessWrapper::newFromObject( new FootnoteMarkFormatter(
|
||||
$this->createMock( Parser::class ),
|
||||
$mockErrorReporter,
|
||||
$this->createMock( CiteKeyFormatter::class ),
|
||||
$this->createMock( AnchorFormatter::class ),
|
||||
$mockMessageLocalizer ) );
|
||||
|
||||
$output = $formatter->getLinkLabel( $group, $offset );
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Cite\Tests\Unit;
|
||||
|
||||
use Cite\CiteErrorReporter;
|
||||
use Cite\ErrorReporter;
|
||||
use Cite\ReferenceStack;
|
||||
use MediaWikiUnitTestCase;
|
||||
use StripState;
|
||||
|
@ -1075,7 +1075,7 @@ class ReferenceStackTest extends MediaWikiUnitTestCase {
|
|||
* @return ReferenceStack
|
||||
*/
|
||||
private function newStack() {
|
||||
$errorReporter = $this->createMock( CiteErrorReporter::class );
|
||||
$errorReporter = $this->createMock( ErrorReporter::class );
|
||||
$errorReporter->method( 'plain' )->willReturnArgument( 0 );
|
||||
$stack = new ReferenceStack( $errorReporter );
|
||||
return TestingAccessWrapper::newFromObject( $stack );
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
namespace Cite\Tests\Unit;
|
||||
|
||||
use Cite\CiteErrorReporter;
|
||||
use Cite\CiteKeyFormatter;
|
||||
use Cite\AnchorFormatter;
|
||||
use Cite\ErrorReporter;
|
||||
use Cite\ReferenceMessageLocalizer;
|
||||
use Cite\ReferencesFormatter;
|
||||
use MediaWikiUnitTestCase;
|
||||
|
@ -26,29 +26,28 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
|
|||
$mockParser = $this->createMock( Parser::class );
|
||||
$mockParser->method( 'recursiveTagParse' )->willReturnArgument( 0 );
|
||||
/** @var Parser $mockParser */
|
||||
$mockErrorReporter = $this->createMock( CiteErrorReporter::class );
|
||||
$mockErrorReporter = $this->createMock( ErrorReporter::class );
|
||||
$mockErrorReporter->method( 'plain' )->willReturnCallback(
|
||||
function ( ...$args ) {
|
||||
return json_encode( $args );
|
||||
return '(' . implode( '|', $args ) . ')';
|
||||
}
|
||||
);
|
||||
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
|
||||
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
|
||||
function ( ...$args ) {
|
||||
$mockMessage = $this->createMock( Message::class );
|
||||
$mockMessage->method( 'plain' )->willReturn(
|
||||
'<li>(' . implode( '|', $args ) . ')</li>' );
|
||||
return $mockMessage;
|
||||
$msg = $this->createMock( Message::class );
|
||||
$msg->method( 'plain' )->willReturn( '<li>(' . implode( '|', $args ) . ')</li>' );
|
||||
return $msg;
|
||||
}
|
||||
);
|
||||
|
||||
/** @var CiteErrorReporter $mockErrorReporter */
|
||||
/** @var ErrorReporter $mockErrorReporter */
|
||||
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
|
||||
/** @var ReferencesFormatter $formatter */
|
||||
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
||||
$mockParser,
|
||||
$mockErrorReporter,
|
||||
$this->createMock( CiteKeyFormatter::class ),
|
||||
$this->createMock( AnchorFormatter::class ),
|
||||
$mockMessageLocalizer
|
||||
) );
|
||||
|
||||
|
@ -126,7 +125,7 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
|
|||
'<div class="mw-references-wrap"><ol class="references">' . "\n" .
|
||||
'<li>(cite_references_link_many|||<span class="reference-text">t3</span>' . "\n" .
|
||||
'|)<ol class="mw-extended-references"><li>(cite_references_link_many|||' .
|
||||
'<span class="reference-text">t1 ["cite_error_ref_too_many_keys"]</span>' .
|
||||
'<span class="reference-text">t1 (cite_error_ref_too_many_keys)</span>' .
|
||||
"\n|)</li>\n" .
|
||||
'<li>(cite_references_link_many|||<span class="reference-text">t2</span>' .
|
||||
"\n|)</li>\n</ol></li>\n" .
|
||||
|
@ -173,8 +172,8 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
|
|||
public function testCloseIndention( $closingLi, $expectedOutput ) {
|
||||
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
||||
$this->createMock( Parser::class ),
|
||||
$this->createMock( CiteErrorReporter::class ),
|
||||
$this->createMock( CiteKeyFormatter::class ),
|
||||
$this->createMock( ErrorReporter::class ),
|
||||
$this->createMock( AnchorFormatter::class ),
|
||||
$this->createMock( ReferenceMessageLocalizer::class )
|
||||
) );
|
||||
|
||||
|
@ -199,37 +198,39 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
|
|||
array $val,
|
||||
string $expectedOutput
|
||||
) {
|
||||
$mockErrorReporter = $this->createMock( CiteErrorReporter::class );
|
||||
$mockErrorReporter = $this->createMock( ErrorReporter::class );
|
||||
$mockErrorReporter->method( 'plain' )->willReturnCallback(
|
||||
function ( ...$args ) {
|
||||
return '(' . implode( '|', $args ) . ')';
|
||||
}
|
||||
);
|
||||
$mockCiteKeyFormatter = $this->createMock( CiteKeyFormatter::class );
|
||||
$mockCiteKeyFormatter->method( 'refKey' )->willReturnCallback(
|
||||
function ( $key, $num = null ) {
|
||||
return $key . '+' . ( $num ?? 'null' );
|
||||
|
||||
$anchorFormatter = $this->createMock( AnchorFormatter::class );
|
||||
$anchorFormatter->method( 'refKey' )->willReturnCallback(
|
||||
function ( ...$args ) {
|
||||
return implode( '+', $args );
|
||||
}
|
||||
);
|
||||
|
||||
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
|
||||
$mockMessageLocalizer->method( 'formatNum' )->willReturnArgument( 0 );
|
||||
$mockMessageLocalizer->method( 'localizeDigits' )->willReturnArgument( 0 );
|
||||
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
|
||||
function ( ...$args ) {
|
||||
$mockMessage = $this->createMock( Message::class );
|
||||
$mockMessage->method( 'plain' )->willReturn(
|
||||
'(' . implode( '|', $args ) . ')' );
|
||||
return $mockMessage;
|
||||
$msg = $this->createMock( Message::class );
|
||||
$msg->method( 'plain' )->willReturn( '(' . implode( '|', $args ) . ')' );
|
||||
return $msg;
|
||||
}
|
||||
);
|
||||
|
||||
/** @var CiteErrorReporter $mockErrorReporter */
|
||||
/** @var ErrorReporter $mockErrorReporter */
|
||||
/** @var AnchorFormatter $anchorFormatter */
|
||||
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
|
||||
/** @var ReferencesFormatter $formatter */
|
||||
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
||||
$this->createMock( Parser::class ),
|
||||
$mockErrorReporter,
|
||||
$mockCiteKeyFormatter,
|
||||
$anchorFormatter,
|
||||
$mockMessageLocalizer
|
||||
) );
|
||||
|
||||
|
@ -289,7 +290,7 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
|
|||
'number' => 3,
|
||||
'text' => 't',
|
||||
],
|
||||
'(cite_references_link_one||5+null|<span class="reference-text">t</span>' . "\n|)"
|
||||
'(cite_references_link_one||5+|<span class="reference-text">t</span>' . "\n|)"
|
||||
],
|
||||
'Count positive' => [
|
||||
1,
|
||||
|
@ -319,18 +320,19 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
|
|||
bool $isSectionPreview,
|
||||
string $expectedOutput
|
||||
) {
|
||||
$mockErrorReporter = $this->createMock( CiteErrorReporter::class );
|
||||
$mockErrorReporter = $this->createMock( ErrorReporter::class );
|
||||
$mockErrorReporter->method( 'plain' )->willReturnCallback(
|
||||
function ( ...$args ) {
|
||||
return json_encode( $args );
|
||||
return '(' . implode( '|', $args ) . ')';
|
||||
}
|
||||
);
|
||||
|
||||
/** @var ErrorReporter $mockErrorReporter */
|
||||
/** @var ReferencesFormatter $formatter */
|
||||
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
||||
$this->createMock( Parser::class ),
|
||||
$mockErrorReporter,
|
||||
$this->createMock( CiteKeyFormatter::class ),
|
||||
$this->createMock( AnchorFormatter::class ),
|
||||
$this->createMock( ReferenceMessageLocalizer::class )
|
||||
) );
|
||||
|
||||
|
@ -344,13 +346,13 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
|
|||
1,
|
||||
null,
|
||||
false,
|
||||
'["cite_error_references_no_text",1]'
|
||||
'(cite_error_references_no_text|1)'
|
||||
],
|
||||
'No text, is preview' => [
|
||||
1,
|
||||
null,
|
||||
true,
|
||||
'["cite_warning_sectionpreview_no_text",1]'
|
||||
'(cite_warning_sectionpreview_no_text|1)'
|
||||
],
|
||||
'Has text' => [
|
||||
1,
|
||||
|
@ -377,23 +379,25 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
|
|||
$mockMessage = $this->createMock( Message::class );
|
||||
$mockMessage->method( 'exists' )->willReturn( (bool)$labelList );
|
||||
$mockMessage->method( 'plain' )->willReturn( $labelList ?? '<missing-junk>' );
|
||||
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
|
||||
|
||||
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
|
||||
$mockMessageLocalizer->method( 'msg' )
|
||||
->willReturn( $mockMessage );
|
||||
/** @var CiteErrorReporter $mockErrorReporter */
|
||||
$mockErrorReporter = $this->createMock( CiteErrorReporter::class );
|
||||
|
||||
$mockErrorReporter = $this->createMock( ErrorReporter::class );
|
||||
if ( $expectedLabel === null ) {
|
||||
$mockErrorReporter->expects( $this->once() )->method( 'plain' );
|
||||
} else {
|
||||
$mockErrorReporter->expects( $this->never() )->method( 'plain' );
|
||||
}
|
||||
|
||||
/** @var ErrorReporter $mockErrorReporter */
|
||||
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
|
||||
/** @var ReferencesFormatter $formatter */
|
||||
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
||||
$this->createMock( Parser::class ),
|
||||
$mockErrorReporter,
|
||||
$this->createMock( CiteKeyFormatter::class ),
|
||||
$this->createMock( AnchorFormatter::class ),
|
||||
$mockMessageLocalizer
|
||||
) );
|
||||
|
||||
|
@ -425,8 +429,8 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
|
|||
/** @var ReferencesFormatter $formatter */
|
||||
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
||||
$this->createMock( Parser::class ),
|
||||
$this->createMock( CiteErrorReporter::class ),
|
||||
$this->createMock( CiteKeyFormatter::class ),
|
||||
$this->createMock( ErrorReporter::class ),
|
||||
$this->createMock( AnchorFormatter::class ),
|
||||
$mockMessageLocalizer
|
||||
) );
|
||||
|
||||
|
@ -448,18 +452,19 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
|
|||
public function testListToText( array $list, $expected ) {
|
||||
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
|
||||
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
|
||||
function ( $msg ) {
|
||||
$mockMessage = $this->createMock( Message::class );
|
||||
$mockMessage->method( 'plain' )->willReturn( "({$msg})" );
|
||||
return $mockMessage;
|
||||
function ( ...$args ) {
|
||||
$msg = $this->createMock( Message::class );
|
||||
$msg->method( 'plain' )->willReturn( '(' . implode( '|', $args ) . ')' );
|
||||
return $msg;
|
||||
}
|
||||
);
|
||||
|
||||
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
|
||||
/** @var ReferencesFormatter $formatter */
|
||||
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
||||
$this->createMock( Parser::class ),
|
||||
$this->createMock( CiteErrorReporter::class ),
|
||||
$this->createMock( CiteKeyFormatter::class ),
|
||||
$this->createMock( ErrorReporter::class ),
|
||||
$this->createMock( AnchorFormatter::class ),
|
||||
$mockMessageLocalizer
|
||||
) );
|
||||
$this->assertSame( $expected, $formatter->listToText( $list ) );
|
||||
|
|
Loading…
Reference in a new issue