mediawiki-extensions-Cite/tests/phpunit/unit/CitationToolDefinitionTest.php
thiemowmde 2b32f15c8c Allow visualeditor-cite-tool-name-… messages to be missing
The information read from the …cite-tool-definition.json files is
effectively user input, even if only interface administrators can
edit it. Usually we carefully validate user input. But as of now
this code starts failing with all kinds of uncatched errors.
* An entry with no name, an empty name, or a name that's not a
  string will cause all kinds of undefined behavior.
* An entry with an empty title results in an invisible button.
* A missing message results in a technical <…> placeholder, even if
  the name is usually a sensible fallback.

Note that hard-coding titles as plain text strings in the ….json
file was already possible.

Change-Id: Iddcedbe859e86ac4c3f79a53d36237daff86c0db
2024-03-06 14:21:08 +01:00

64 lines
1.7 KiB
PHP

<?php
namespace Cite\Tests\Unit;
use Cite\ResourceLoader\CitationToolDefinition;
use MediaWiki\ResourceLoader\Context;
use Message;
/**
* @covers \Cite\ResourceLoader\CitationToolDefinition
* @license GPL-2.0-or-later
*/
class CitationToolDefinitionTest extends \MediaWikiUnitTestCase {
public function testGetScript() {
$context = $this->createResourceLoaderContext();
$expected = [
[ 'name' => 'no-message', 'title' => 'Hard-coded title' ],
[ 'name' => 'missing-message', 'title' => 'missing-message' ],
[ 'name' => 'n', 'title' => 't' ],
];
$this->assertSame(
've.ui.mwCitationTools = ' . json_encode( $expected ) . ';',
CitationToolDefinition::makeScript( $context )
);
}
private function createResourceLoaderContext(): Context {
$definition = [
// We expect broken and incomplete entries to be skipped
[],
[ 'name' => '' ],
[ 'name' => 'no-message', 'title' => 'Hard-coded title' ],
[ 'name' => 'missing-message' ],
[ 'name' => 'n' ],
];
$msg = $this->createMock( Message::class );
$msg->method( 'inContentLanguage' )
->willReturnSelf();
$msg->method( 'plain' )
->willReturn( json_encode( $definition ) );
$msg->method( 'text' )
->willReturn( 't' );
$disabled = $this->createMock( Message::class );
$disabled->method( 'isDisabled' )->willReturn( true );
$context = $this->createStub( Context::class );
$context->method( 'msg' )
->willReturnMap( [
[ 'cite-tool-definition.json', $msg ],
[ 'visualeditor-cite-tool-definition.json', $msg ],
[ 'visualeditor-cite-tool-name-missing-message', $disabled ],
[ 'visualeditor-cite-tool-name-n', $msg ]
] );
$context->method( 'encodeJson' )->willReturnCallback( 'json_encode' );
return $context;
}
}