View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\ckeditor5\Kernel;
use Drupal\editor\Entity\Editor;
use Drupal\filter\Entity\FilterFormat;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Symfony\Component\Yaml\Yaml;
class LanguageTest extends KernelTestBase {
protected $ckeditor5;
protected static $modules = [
'system',
'user',
'ckeditor5',
'editor',
'filter',
'language',
'locale',
];
protected function setUp() : void {
parent::setUp();
$this->ckeditor5 = $this->container
->get('plugin.manager.editor')
->createInstance('ckeditor5');
FilterFormat::create(Yaml::parseFile('core/profiles/standard/config/install/filter.format.basic_html.yml'))
->save();
Editor::create([
'format' => 'basic_html',
'editor' => 'ckeditor5',
])
->save();
$this
->installConfig([
'language',
]);
}
public function test(string $drupal_langcode, string $cke5_langcode, bool $is_missing_mapping = FALSE) : void {
$editor = Editor::load('basic_html');
ConfigurableLanguage::createFromLangcode($drupal_langcode)
->save();
$this
->config('system.site')
->set('default_langcode', $drupal_langcode)
->save();
if ($is_missing_mapping) {
$settings = $this->ckeditor5
->getJSSettings($editor);
$this
->assertSame('en', $settings['language']['ui']);
$config = $this
->config('language.mappings');
$mapping = $config
->get('map');
$mapping += [
$cke5_langcode => $drupal_langcode,
];
$config
->set('map', $mapping)
->save();
}
$settings = $this->ckeditor5
->getJSSettings($editor);
$this
->assertSame($cke5_langcode, $settings['language']['ui']);
}
public function provider() : array {
$random_langcode = $this
->randomMachineName();
return [
'Language code transformed from browser mappings' => [
'drupal_langcode' => 'pt-pt',
'cke_langcode' => 'pt',
],
'Language code transformed from browser mappings 2' => [
'drupal_langcode' => 'zh-hans',
'cke_langcode' => 'zh-cn',
],
'Language code both in Drupal and CKEditor' => [
'drupal_langcode' => 'fi',
'cke_langcode' => 'fi',
],
'Language code not in Drupal but in CKEditor 5 requires new language.mappings entry' => [
'drupal_langcode' => $random_langcode,
'cke_langcode' => 'de-ch',
'is_missing_mapping' => TRUE,
],
];
}
}