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;
|
|
|
|
|
|
|
|
/**
|
2020-10-08 08:59:50 +00:00
|
|
|
* @covers \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
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideErrors
|
|
|
|
*/
|
|
|
|
public function testPlain(
|
|
|
|
string $key,
|
|
|
|
string $expectedHtml,
|
|
|
|
array $expectedCategories
|
|
|
|
) {
|
2020-01-28 16:40:27 +00:00
|
|
|
$language = $this->createLanguage();
|
|
|
|
$reporter = $this->createReporter( $language );
|
|
|
|
$mockParser = $this->createParser( $language, $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
|
|
|
}
|
|
|
|
|
|
|
|
public function testHalfParsed() {
|
2020-01-28 16:40:27 +00:00
|
|
|
$language = $this->createLanguage();
|
|
|
|
$reporter = $this->createReporter( $language );
|
|
|
|
$mockParser = $this->createParser( $language, [] );
|
2019-12-06 03:20:20 +00:00
|
|
|
$this->assertSame(
|
2020-01-28 16:40:27 +00:00
|
|
|
'<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>',
|
|
|
|
[]
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-01-28 16:40:27 +00:00
|
|
|
private function createLanguage() : Language {
|
|
|
|
$language = $this->createMock( Language::class );
|
|
|
|
$language->method( 'getDir' )->willReturn( 'rtl' );
|
|
|
|
$language->method( 'getHtmlCode' )->willReturn( 'qqx' );
|
|
|
|
return $language;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createReporter( Language $language ) : ErrorReporter {
|
2019-12-11 15:05:19 +00:00
|
|
|
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
|
|
|
|
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
|
2020-01-28 16:40:27 +00:00
|
|
|
function ( ...$args ) use ( $language ) {
|
2020-01-20 13:49:44 +00:00
|
|
|
$message = $this->createMock( Message::class );
|
|
|
|
$message->method( 'getKey' )->willReturn( $args[0] );
|
|
|
|
$message->method( 'plain' )->willReturn( '(' . implode( '|', $args ) . ')' );
|
2020-01-28 16:40:27 +00:00
|
|
|
$message->method( 'inLanguage' )->with( $language )->willReturnSelf();
|
|
|
|
$message->method( 'getLanguage' )->willReturn( $language );
|
2020-01-20 13:49:44 +00:00
|
|
|
return $message;
|
2019-12-11 15:05:19 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
|
|
|
|
return new ErrorReporter( $mockMessageLocalizer );
|
|
|
|
}
|
|
|
|
|
2020-10-08 08:59:50 +00:00
|
|
|
private function createParser( Language $language, array $expectedCategories ) : Parser {
|
2020-01-20 13:49:44 +00:00
|
|
|
$parserOptions = $this->createMock( ParserOptions::class );
|
|
|
|
$parserOptions->method( 'getUserLangObj' )->willReturn( $language );
|
|
|
|
|
|
|
|
$parser = $this->createMock( Parser::class );
|
|
|
|
$parser->expects( $this->exactly( count( $expectedCategories ) ) )
|
|
|
|
->method( 'addTrackingCategory' )
|
|
|
|
->withConsecutive( $expectedCategories );
|
|
|
|
$parser->method( 'getOptions' )->willReturn( $parserOptions );
|
|
|
|
$parser->method( 'recursiveTagParse' )->willReturnCallback(
|
2021-05-03 07:03:03 +00:00
|
|
|
static function ( $content ) {
|
2019-12-06 03:20:20 +00:00
|
|
|
return '[' . $content . ']';
|
|
|
|
}
|
|
|
|
);
|
2020-01-20 13:49:44 +00:00
|
|
|
return $parser;
|
2019-12-06 03:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|