You are here

public function SubscribersTest::testFieldedMessageBundles in Message Subscribe 8

Tests message bundles with fields are properly cloned.

@covers ::sendMessage

File

tests/src/Kernel/SubscribersTest.php, line 406

Class

SubscribersTest
Test getting subscribes from context.

Namespace

Drupal\Tests\message_subscribe\Kernel

Code

public function testFieldedMessageBundles() {
  $field_name = mb_strtolower($this
    ->randomMachineName());
  $field_storage = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'message',
    'type' => 'text',
  ]);
  $field_storage
    ->save();
  $field_config = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => $this->template
      ->id(),
  ]);
  $field_config
    ->save();

  // Enable the email notifier.
  \Drupal::configFactory()
    ->getEditable('message_subscribe.settings')
    ->set('default_notifiers', [
    'email',
  ])
    ->save();
  $message = Message::create([
    'template' => $this->template
      ->id(),
    'uid' => $this->users[1],
    $field_name => $this
      ->randomString(),
  ]);

  // Save and reload to mimic queue behavior.
  $message
    ->save();
  $message = $message
    ->load($message
    ->id());

  // Send message set to save the cloned message.
  $node = $this->nodes[0];
  $this->messageSubscribers
    ->sendMessage($node, $message, [
    'email' => [
      'save on success' => TRUE,
    ],
  ], [
    'entity access' => FALSE,
  ]);
  $cloned_message = \Drupal::entityTypeManager()
    ->getStorage('message')
    ->load($message
    ->id() + 1);
  $this
    ->assertEquals($message->{$field_name}->value, $cloned_message->{$field_name}->value);
}