2019-12-06 03:20:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Cite\Tests\Unit;
|
|
|
|
|
2019-12-09 16:08:57 +00:00
|
|
|
use Cite\ErrorReporter;
|
2019-12-06 03:20:20 +00:00
|
|
|
use Cite\ReferenceMessageLocalizer;
|
|
|
|
use Language;
|
|
|
|
use Message;
|
|
|
|
use Parser;
|
|
|
|
use ParserOptions;
|
|
|
|
|
|
|
|
/**
|
2019-12-09 16:08:57 +00:00
|
|
|
* @coversDefaultClass \Cite\ErrorReporter
|
2019-12-06 03:20:20 +00:00
|
|
|
*
|
|
|
|
* @license GPL-2.0-or-later
|
|
|
|
*/
|
2019-12-10 15:24:49 +00:00
|
|
|
class ErrorReporterTest extends \MediaWikiUnitTestCase {
|
2019-12-06 03:20:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::plain
|
|
|
|
* @covers ::__construct
|
|
|
|
* @dataProvider provideErrors
|
|
|
|
*/
|
|
|
|
public function testPlain(
|
|
|
|
string $key,
|
|
|
|
string $expectedHtml,
|
|
|
|
array $expectedCategories
|
|
|
|
) {
|
2019-12-11 15:05:19 +00:00
|
|
|
$reporter = $this->createReporter();
|
|
|
|
$mockParser = $this->createParser( $expectedCategories );
|
2019-12-06 03:20:20 +00:00
|
|
|
$this->assertSame(
|
|
|
|
$expectedHtml,
|
2019-12-11 15:05:19 +00:00
|
|
|
$reporter->plain( $mockParser, $key, 'first param' ) );
|
2019-12-06 03:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::halfParsed
|
|
|
|
*/
|
|
|
|
public function testHalfParsed() {
|
2019-12-11 15:05:19 +00:00
|
|
|
$reporter = $this->createReporter();
|
|
|
|
$mockParser = $this->createParser( [] );
|
2019-12-06 03:20:20 +00:00
|
|
|
$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>]',
|
2019-12-11 15:05:19 +00:00
|
|
|
$reporter->halfParsed( $mockParser, 'cite_warning_example', 'first param' ) );
|
2019-12-06 03:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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>',
|
|
|
|
[]
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-12-11 15:05:19 +00:00
|
|
|
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 ) {
|
2019-12-06 03:20:20 +00:00
|
|
|
$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 . ']';
|
|
|
|
}
|
|
|
|
);
|
2019-12-11 15:05:19 +00:00
|
|
|
return $mockParser;
|
2019-12-06 03:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|