You are here

trait MessageTemplateCreateTrait in Message 8

Trait to assist message template creation for tests.

Hierarchy

2 files declare their use of MessageTemplateCreateTrait
MessageCheckAndDeleteWorkerTest.php in tests/src/Kernel/Plugin/QueueWorker/MessageCheckAndDeleteWorkerTest.php
MessageTestBase.php in tests/src/Functional/MessageTestBase.php

File

tests/src/Kernel/MessageTemplateCreateTrait.php, line 11

Namespace

Drupal\Tests\message\Kernel
View source
trait MessageTemplateCreateTrait {

  /**
   * Helper function to create and save a message template entity.
   *
   * @param string $template
   *   The message template.
   * @param string $label
   *   The message template label.
   * @param string $description
   *   The message template description.
   * @param array $text
   *   The text array for the message template.
   * @param array $settings
   *   Data overrides.
   * @param string $langcode
   *   The language to use.
   *
   * @return \Drupal\message\MessageTemplateInterface
   *   A saved message template entity.
   */
  protected function createMessageTemplate($template = NULL, $label = NULL, $description = NULL, array $text = [], array $settings = [], $langcode = Language::LANGCODE_NOT_SPECIFIED) {
    $settings += [
      'token options' => [
        'token replace' => TRUE,
        'clear' => FALSE,
      ],
    ];
    $template = $template ?: mb_strtolower($this
      ->randomMachineName());
    $label = $label ?: $this
      ->randomString();
    $description = $description ?: $this
      ->randomString();
    $text = $text ?: [
      $this
        ->randomString(),
    ];

    // If the $text array is simple text values, transform to text + format.
    foreach ($text as $key => $detail) {
      if (!is_array($detail)) {
        $text[$key] = [
          'value' => $detail,
          'format' => filter_default_format(),
        ];
      }
      elseif (!isset($detail['format'])) {
        $text[$key]['format'] = 'plain_text';
      }
    }
    $message_template = MessageTemplate::Create([
      'template' => $template,
      'label' => $label,
      'description' => $description,
      'text' => $text,
      'settings' => $settings,
      'langcode' => $langcode,
    ]);
    $message_template
      ->save();
    return $message_template;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MessageTemplateCreateTrait::createMessageTemplate protected function Helper function to create and save a message template entity.