mediawiki-extensions-Cite/tests/phpunit/unit/CiteDataModuleTest.php
Timo Tijhof bee357337c ve-cite: Export citationTools as native object instead of JSON string message
* Remove the overhead of serializing and then re-parsing client-side,
  instead assign it directly as native object literal.

* Move code for array slicing to PHP.

Change-Id: Iedcc8d57d3bddd3fa32a78b4e7ecc25615d94277
2021-12-07 12:53:24 +00:00

59 lines
1.5 KiB
PHP

<?php
namespace Cite\Tests\Unit;
use Cite\ResourceLoader\CiteVisualEditorModule;
use Message;
use ResourceLoaderContext;
/**
* @covers \Cite\ResourceLoader\CiteVisualEditorModule
*
* @license GPL-2.0-or-later
*/
class CiteDataModuleTest extends \MediaWikiUnitTestCase {
public function testGetScript() {
$module = new CiteVisualEditorModule();
$context = $this->createResourceLoaderContext();
$this->assertSame(
've.ui.mwCitationTools = [{"name":"n","title":"t"}];',
$module->makePrependedScript( $context )
);
}
public function testGetDefinitionSummary() {
$module = new CiteVisualEditorModule();
$context = $this->createResourceLoaderContext();
$summary = $module->getDefinitionSummary( $context );
$this->assertStringContainsString(
'{"name":"n","title":"t"}',
array_pop( $summary )['script']
);
}
private function createResourceLoaderContext(): ResourceLoaderContext {
$msg = $this->createMock( Message::class );
$msg->method( 'inContentLanguage' )
->willReturnSelf();
$msg->method( 'plain' )
->willReturnOnConsecutiveCalls( '', '[{"name":"n"}]' );
$msg->method( 'text' )
->willReturn( 't' );
$context = $this->createStub( ResourceLoaderContext::class );
$context->method( 'msg' )
->withConsecutive(
[ 'cite-tool-definition.json' ],
[ 'visualeditor-cite-tool-definition.json' ],
[ 'visualeditor-cite-tool-name-n' ]
)
->willReturn( $msg );
$context->method( 'encodeJson' )->willReturnCallback( 'json_encode' );
return $context;
}
}