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;
|
2024-06-08 21:31:42 +00:00
|
|
|
use MediaWiki\Message\Message;
|
|
|
|
use MediaWiki\Parser\Parser;
|
2019-12-06 03:20:20 +00:00
|
|
|
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,
|
2024-02-01 16:19:29 +00:00
|
|
|
?string $expectedCategory
|
2019-12-06 03:20:20 +00:00
|
|
|
) {
|
2020-01-28 16:40:27 +00:00
|
|
|
$language = $this->createLanguage();
|
|
|
|
$reporter = $this->createReporter( $language );
|
2024-02-01 16:19:29 +00:00
|
|
|
$mockParser = $this->createParser( $language, $expectedCategory );
|
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
|
|
|
}
|
|
|
|
|
2024-02-01 16:17:53 +00:00
|
|
|
public function testDisabledWrapperMessages() {
|
|
|
|
$language = $this->createLanguage();
|
|
|
|
$reporter = $this->createReporter( $language, true );
|
|
|
|
$mockParser = $this->createParser( $language );
|
|
|
|
$this->assertSame(
|
|
|
|
'<span class="warning mw-ext-cite-warning mw-ext-cite-warning-a" lang="qqx" ' .
|
|
|
|
'dir="rtl">(cite_warning_a)</span>',
|
|
|
|
$reporter->plain( $mockParser, 'cite_warning_a' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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 );
|
2024-02-01 16:19:29 +00:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2023-05-20 10:03:41 +00:00
|
|
|
public static function provideErrors() {
|
2019-12-06 03:20:20 +00:00
|
|
|
return [
|
|
|
|
'Example error' => [
|
2023-12-21 19:44:39 +00:00
|
|
|
'key' => 'cite_error_example',
|
|
|
|
'expectedHtml' => '<span class="error mw-ext-cite-error" lang="qqx" dir="rtl">' .
|
2019-12-06 03:20:20 +00:00
|
|
|
'(cite_error|(cite_error_example|first param))</span>',
|
2024-02-01 16:19:29 +00:00
|
|
|
'expectedCategory' => 'cite-tracking-category-cite-error',
|
2019-12-06 03:20:20 +00:00
|
|
|
],
|
|
|
|
'Warning error' => [
|
2023-12-21 19:44:39 +00:00
|
|
|
'key' => 'cite_warning_example',
|
|
|
|
'expectedHtml' => '<span class="warning mw-ext-cite-warning mw-ext-cite-warning-example" lang="qqx" ' .
|
2019-12-06 03:20:20 +00:00
|
|
|
'dir="rtl">(cite_warning|(cite_warning_example|first param))</span>',
|
2024-02-01 16:19:29 +00:00
|
|
|
'expectedCategory' => null,
|
2019-12-06 03:20:20 +00:00
|
|
|
],
|
2023-12-04 16:51:38 +00:00
|
|
|
'Optional support for messages with dashes' => [
|
|
|
|
'key' => 'cite-warning-with-dashes',
|
|
|
|
'expectedHtml' => '<span class="warning mw-ext-cite-warning ' .
|
|
|
|
'mw-ext-cite-warning-with-dashes" lang="qqx" dir="rtl">' .
|
|
|
|
'(cite_warning|(cite-warning-with-dashes|first param))</span>',
|
|
|
|
'expectedCategory' => null,
|
|
|
|
],
|
2019-12-06 03:20:20 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-07-22 05:44:15 +00:00
|
|
|
private function createLanguage(): Language {
|
2023-07-27 09:39:46 +00:00
|
|
|
$language = $this->createNoOpMock( Language::class, [ 'getDir', 'getHtmlCode' ] );
|
2020-01-28 16:40:27 +00:00
|
|
|
$language->method( 'getDir' )->willReturn( 'rtl' );
|
|
|
|
$language->method( 'getHtmlCode' )->willReturn( 'qqx' );
|
|
|
|
return $language;
|
|
|
|
}
|
|
|
|
|
2024-02-01 16:17:53 +00:00
|
|
|
private function createReporter( Language $language, bool $disabled = false ): ErrorReporter {
|
2019-12-11 15:05:19 +00:00
|
|
|
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
|
|
|
|
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
|
2024-02-01 16:17:53 +00:00
|
|
|
function ( ...$args ) use ( $language, $disabled ) {
|
2020-01-20 13:49:44 +00:00
|
|
|
$message = $this->createMock( Message::class );
|
2024-02-01 16:17:53 +00:00
|
|
|
$message->method( 'isDisabled' )->willReturn( $disabled );
|
2020-01-20 13:49:44 +00:00
|
|
|
$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
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return new ErrorReporter( $mockMessageLocalizer );
|
|
|
|
}
|
|
|
|
|
2024-02-01 16:19:29 +00:00
|
|
|
private function createParser( Language $language, string $expectedCategory = null ): Parser {
|
2020-01-20 13:49:44 +00:00
|
|
|
$parserOptions = $this->createMock( ParserOptions::class );
|
|
|
|
$parserOptions->method( 'getUserLangObj' )->willReturn( $language );
|
|
|
|
|
2023-07-27 09:39:46 +00:00
|
|
|
$parser = $this->createNoOpMock( Parser::class, [ 'addTrackingCategory', 'getOptions', 'recursiveTagParse' ] );
|
2024-02-01 16:19:29 +00:00
|
|
|
$parser->expects( $expectedCategory ? $this->once() : $this->never() )
|
2020-01-20 13:49:44 +00:00
|
|
|
->method( 'addTrackingCategory' )
|
2024-02-01 16:19:29 +00:00
|
|
|
->with( $expectedCategory );
|
2020-01-20 13:49:44 +00:00
|
|
|
$parser->method( 'getOptions' )->willReturn( $parserOptions );
|
|
|
|
$parser->method( 'recursiveTagParse' )->willReturnCallback(
|
2023-12-14 07:58:49 +00:00
|
|
|
static fn ( $content ) => '[' . $content . ']'
|
2019-12-06 03:20:20 +00:00
|
|
|
);
|
2020-01-20 13:49:44 +00:00
|
|
|
return $parser;
|
2019-12-06 03:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|