You are here

class MessageTemplateTest in Message 8

Unit tests for the message template entity.

@coversDefaultClass \Drupal\message\Entity\MessageTemplate

@group Message

Hierarchy

Expanded class hierarchy of MessageTemplateTest

File

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

Namespace

Drupal\Tests\message\Unit\Entity
View source
class MessageTemplateTest extends UnitTestCase {

  /**
   * A message template entity.
   *
   * @var \Drupal\message\MessageTemplateInterface
   */
  protected $messageTemplate;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->messageTemplate = new MessageTemplate([
      'template' => 'foo_template',
    ], 'message_template');
  }

  /**
   * Test the ID method.
   *
   * @covers ::id
   */
  public function testId() {
    $this
      ->assertSame('foo_template', $this->messageTemplate
      ->id());
  }

  /**
   * Tests getting and setting the Settings array.
   *
   * @covers ::setSettings
   * @covers ::getSettings
   * @covers ::getSettings
   */
  public function testSetSettings() {
    $settings = [
      'one' => 'foo',
      'two' => 'bar',
    ];
    $this->messageTemplate
      ->setSettings($settings);
    $this
      ->assertArrayEquals($settings, $this->messageTemplate
      ->getSettings());
    $this
      ->assertEquals($this->messageTemplate
      ->getSetting('one'), $this->messageTemplate
      ->getSetting('one'));
    $this
      ->assertEquals('bar', $this->messageTemplate
      ->getSetting('two'));
  }

  /**
   * Tests getting and setting description.
   *
   * @covers ::setDescription
   * @covers ::getDescription
   */
  public function testSetDescription() {
    $description = 'A description';
    $this->messageTemplate
      ->setDescription($description);
    $this
      ->assertEquals($description, $this->messageTemplate
      ->getDescription());
  }

  /**
   * Tests getting and setting label.
   *
   * @covers ::setLabel
   * @covers ::getLabel
   */
  public function testSetLabel() {
    $label = 'A label';
    $this->messageTemplate
      ->setLabel($label);
    $this
      ->assertEquals($label, $this->messageTemplate
      ->getLabel());
  }

  /**
   * Tests getting and setting template.
   *
   * @covers ::setTemplate
   * @covers ::getTemplate
   */
  public function testSetTemplate() {
    $template = 'a_template';
    $this->messageTemplate
      ->setTemplate($template);
    $this
      ->assertEquals($template, $this->messageTemplate
      ->getTemplate());
  }

  /**
   * Tests getting and setting uuid.
   *
   * @covers ::setUuid
   * @covers ::getUuid
   */
  public function testSetUuid() {
    $uuid = 'a-uuid-123';
    $this->messageTemplate
      ->setUuid($uuid);
    $this
      ->assertEquals($uuid, $this->messageTemplate
      ->getUuid());
  }

  /**
   * Tests if the template is locked.
   *
   * @covers ::isLocked
   */
  public function testIsLocked() {
    $this
      ->assertTrue($this->messageTemplate
      ->isLocked());
    $this->messageTemplate
      ->enforceIsNew(TRUE);
    $this
      ->assertFalse($this->messageTemplate
      ->isLocked());
  }

  /**
   * Tests the getText method with default language only.
   *
   * @covers ::getText
   */
  public function testGetTextDefaultLanguage() {

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

    // Should be empty by default.
    $this
      ->assertEmpty($this->messageTemplate
      ->getText());

    // Setup a renderer.
    $renderer = $this
      ->prophesize(RendererInterface::class);

    // Set some text.
    $text = [
      [
        'value' => 'foo text',
        'format' => 'foo_format',
      ],
      [
        'value' => 'bar text',
        'format' => 'bar_format',
      ],
    ];
    $expected_build = [
      '#type' => 'processed_text',
      '#text' => $text[0]['value'],
      '#format' => $text[0]['format'],
      '#langcode' => Language::LANGCODE_NOT_SPECIFIED,
    ];
    $renderer
      ->renderPlain($expected_build)
      ->willReturn('<div>foo text</div>');
    $expected_build = [
      '#type' => 'processed_text',
      '#text' => $text[1]['value'],
      '#format' => $text[1]['format'],
      '#langcode' => Language::LANGCODE_NOT_SPECIFIED,
    ];
    $renderer
      ->renderPlain($expected_build)
      ->willReturn('bar text');
    \Drupal::getContainer()
      ->set('renderer', $renderer
      ->reveal());
    $this->messageTemplate
      ->set('text', $text);
    $expected = [
      '<div>foo text</div>',
      'bar text',
    ];
    $this
      ->assertEquals($expected, $this->messageTemplate
      ->getText());

    // Test specific delta.
    $this
      ->assertEquals([
      $expected[1],
    ], $this->messageTemplate
      ->getText(Language::LANGCODE_NOT_SPECIFIED, 1));

    // Non-existent delta.
    $this
      ->assertEmpty($this->messageTemplate
      ->getText(Language::LANGCODE_NOT_SPECIFIED, 42));
  }

  /**
   * Tests the getText method using configurable languages.
   *
   * @covers ::getText
   */
  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'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MessageTemplateTest::$messageTemplate protected property A message template entity.
MessageTemplateTest::setUp public function Overrides UnitTestCase::setUp
MessageTemplateTest::testGetTextConfigurableLanguage public function Tests the getText method using configurable languages.
MessageTemplateTest::testGetTextDefaultLanguage public function Tests the getText method with default language only.
MessageTemplateTest::testId public function Test the ID method.
MessageTemplateTest::testIsLocked public function Tests if the template is locked.
MessageTemplateTest::testSetDescription public function Tests getting and setting description.
MessageTemplateTest::testSetLabel public function Tests getting and setting label.
MessageTemplateTest::testSetSettings public function Tests getting and setting the Settings array.
MessageTemplateTest::testSetTemplate public function Tests getting and setting template.
MessageTemplateTest::testSetUuid public function Tests getting and setting uuid.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.