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
|
|
|
}
|
|
|
|
|
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>',
|
2023-12-21 19:44:39 +00:00
|
|
|
'categories' => [ '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>',
|
2023-12-21 19:44:39 +00:00
|
|
|
'categories' => []
|
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;
|
|
|
|
}
|
|
|
|
|
2021-07-22 05:44:15 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return new ErrorReporter( $mockMessageLocalizer );
|
|
|
|
}
|
|
|
|
|
2021-07-22 05:44:15 +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 );
|
|
|
|
|
2023-07-27 09:39:46 +00:00
|
|
|
$parser = $this->createNoOpMock( Parser::class, [ 'addTrackingCategory', 'getOptions', 'recursiveTagParse' ] );
|
2020-01-20 13:49:44 +00:00
|
|
|
$parser->expects( $this->exactly( count( $expectedCategories ) ) )
|
|
|
|
->method( 'addTrackingCategory' )
|
2023-11-21 23:15:20 +00:00
|
|
|
->willReturnCallback( function ( $cat ) use ( &$expectedCategories ) {
|
|
|
|
$catIdx = array_search( $cat, $expectedCategories, true );
|
|
|
|
$this->assertNotFalse( $catIdx, "Unexpected category: $cat" );
|
|
|
|
unset( $expectedCategories[$catIdx] );
|
|
|
|
} );
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|