You are here

public function MessageTemplateTest::testGetTextConfigurableLanguage in Message 8

Tests the getText method using configurable languages.

@covers ::getText

File

tests/src/Unit/Entity/MessageTemplateTest.php, line 184

Class

MessageTemplateTest
Unit tests for the message template entity.

Namespace

Drupal\Tests\message\Unit\Entity

Code

public function testGetTextConfigurableLanguage() {

  // Mock a language manager.
  $container = new ContainerBuilder();
  $language_manager = $this
    ->prophesize(LanguageManagerInterface::class)
    ->reveal();
  $container
    ->set('language_manager', $language_manager);
  \Drupal::setContainer($container);

  // Default language with configurable languages available.
  $default_language = $this
    ->prophesize(Language::class);
  $default_language
    ->getId()
    ->willReturn('hu');
  $language_manager = $this
    ->prophesize(ConfigurableLanguageManagerInterface::class);
  $language_manager
    ->getDefaultLanguage()
    ->willReturn($default_language);
  $language_manager
    ->getLanguageConfigOverride('hu', 'message.template.foo_template')
    ->willReturn($this->messageTemplate);
  \Drupal::getContainer()
    ->set('language_manager', $language_manager
    ->reveal());
  $text = [
    [
      'value' => 'foo text',
      'format' => 'foo_format',
    ],
    [
      'value' => 'bar text',
      'format' => 'bar_format',
    ],
  ];
  $this->messageTemplate
    ->set('text', $text);
  $renderer = $this
    ->prophesize(RendererInterface::class);
  $expected_build = [
    '#type' => 'processed_text',
    '#text' => $text[0]['value'],
    '#format' => $text[0]['format'],
    '#langcode' => 'hu',
  ];
  $renderer
    ->renderPlain($expected_build)
    ->willReturn('<div>foo text</div>');
  $expected_build = [
    '#type' => 'processed_text',
    '#text' => $text[1]['value'],
    '#format' => $text[1]['format'],
    '#langcode' => 'hu',
  ];
  $renderer
    ->renderPlain($expected_build)
    ->willReturn('bar text');
  \Drupal::getContainer()
    ->set('renderer', $renderer
    ->reveal());
  $expected = [
    '<div>foo text</div>',
    'bar text',
  ];
  $this
    ->assertEquals($expected, $this->messageTemplate
    ->getText());

  // Language without translation should return empty array.
  $message_template = $this
    ->prophesize(MessageTemplate::class);
  $language_manager
    ->getLanguageConfigOverride('xx', 'message.template.foo_template')
    ->willReturn($message_template
    ->reveal());
  \Drupal::getContainer()
    ->set('language_manager', $language_manager
    ->reveal());
  $this
    ->assertEmpty($this->messageTemplate
    ->getText('xx'));
}