mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Cite
synced 2024-12-04 19:38:16 +00:00
92607eecfd
We forgot about these when restructuring the code and introducing these new methods. Change-Id: If856a7857f2d50d1dc66a57999d98baa8b924d51
110 lines
3.2 KiB
PHP
110 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace Cite\Tests\Unit;
|
|
|
|
use Cite\ErrorReporter;
|
|
use Cite\ReferenceMessageLocalizer;
|
|
use Language;
|
|
use Message;
|
|
use Parser;
|
|
use ParserOptions;
|
|
|
|
/**
|
|
* @coversDefaultClass \Cite\ErrorReporter
|
|
*
|
|
* @license GPL-2.0-or-later
|
|
*/
|
|
class ErrorReporterTest extends \MediaWikiUnitTestCase {
|
|
|
|
/**
|
|
* @covers ::__construct
|
|
* @covers ::getInterfaceLanguageAndSplitCache
|
|
* @covers ::plain
|
|
* @dataProvider provideErrors
|
|
*/
|
|
public function testPlain(
|
|
string $key,
|
|
string $expectedHtml,
|
|
array $expectedCategories
|
|
) {
|
|
$reporter = $this->createReporter();
|
|
$mockParser = $this->createParser( $expectedCategories );
|
|
$this->assertSame(
|
|
$expectedHtml,
|
|
$reporter->plain( $mockParser, $key, 'first param' ) );
|
|
}
|
|
|
|
/**
|
|
* @covers ::halfParsed
|
|
*/
|
|
public function testHalfParsed() {
|
|
$reporter = $this->createReporter();
|
|
$mockParser = $this->createParser( [] );
|
|
$expectedHtml = '';
|
|
$this->assertSame(
|
|
'[<span class="warning mw-ext-cite-warning mw-ext-cite-warning-example" lang="qqx" ' .
|
|
'dir="rtl">(cite_warning|(cite_warning_example|first param))</span>]',
|
|
$reporter->halfParsed( $mockParser, 'cite_warning_example', 'first param' ) );
|
|
}
|
|
|
|
public function provideErrors() {
|
|
return [
|
|
'Example error' => [
|
|
'cite_error_example',
|
|
'<span class="error mw-ext-cite-error" lang="qqx" dir="rtl">' .
|
|
'(cite_error|(cite_error_example|first param))</span>',
|
|
[ 'cite-tracking-category-cite-error' ]
|
|
],
|
|
'Warning error' => [
|
|
'cite_warning_example',
|
|
'<span class="warning mw-ext-cite-warning mw-ext-cite-warning-example" lang="qqx" ' .
|
|
'dir="rtl">(cite_warning|(cite_warning_example|first param))</span>',
|
|
[]
|
|
],
|
|
];
|
|
}
|
|
|
|
private function createReporter() : ErrorReporter {
|
|
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
|
|
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
|
|
function ( ...$args ) {
|
|
$rendered = '(' . implode( '|', $args ) . ')';
|
|
$mockMessage = $this->createMock( Message::class );
|
|
$mockMessage->method( 'getKey' )->willReturn( $args[0] );
|
|
$mockMessage->method( 'plain' )->willReturn( $rendered );
|
|
// FIXME: Doesn't prove that we've set the language correctly.
|
|
$mockMessage->method( 'inLanguage' )->willReturnSelf();
|
|
return $mockMessage;
|
|
}
|
|
);
|
|
|
|
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
|
|
return new ErrorReporter( $mockMessageLocalizer );
|
|
}
|
|
|
|
public function createParser( array $expectedCategories ) {
|
|
$mockParser = $this->createMock( Parser::class );
|
|
foreach ( $expectedCategories as $category ) {
|
|
$mockParser->method( 'addTrackingCategory' )->with( $category );
|
|
}
|
|
$mockParser->method( 'getOptions' )->willReturnCallback(
|
|
function () {
|
|
$mockLanguage = $this->createMock( Language::class );
|
|
$mockLanguage->method( 'getCode' )->willReturn( 'ui' );
|
|
$mockLanguage->method( 'getDir' )->willReturn( 'rtl' );
|
|
$mockLanguage->method( 'getHtmlCode' )->willReturn( 'qqx' );
|
|
$mockOptions = $this->createMock( ParserOptions::class );
|
|
$mockOptions->method( 'getUserLangObj' )->willReturn( $mockLanguage );
|
|
return $mockOptions;
|
|
}
|
|
);
|
|
$mockParser->method( 'recursiveTagParse' )->willReturnCallback(
|
|
function ( $content ) {
|
|
return '[' . $content . ']';
|
|
}
|
|
);
|
|
return $mockParser;
|
|
}
|
|
|
|
}
|