public function MessageNotifyTest::testPostSendRenderedField in Message Notify 8
Test populating the rednered output to fields.
File
- tests/src/ Kernel/ MessageNotifyTest.php, line 126 
Class
- MessageNotifyTest
- Test the Message notifier plugins handling.
Namespace
Drupal\Tests\message_notify\KernelCode
public function testPostSendRenderedField() {
  $this
    ->attachRenderedFields();
  // Test plain fields.
  $options = [
    'rendered fields' => [
      'foo' => 'rendered_foo',
      'bar' => 'rendered_bar',
    ],
  ];
  $message = Message::create([
    'template' => $this->messageTemplate
      ->id(),
  ]);
  $this->messageNotifier
    ->send($message, $options, 'test');
  $this
    ->assertTrue($message->rendered_foo->value && $message->rendered_bar->value, 'Message is rendered to fields.');
  // Test field with text-processing.
  $options = [
    'rendered fields' => [
      'foo' => 'rendered_baz',
      'bar' => 'rendered_bar',
    ],
  ];
  $message = Message::create([
    'template' => $this->messageTemplate
      ->id(),
  ]);
  $this->messageNotifier
    ->send($message, $options, 'test');
  $this
    ->assertTrue($message->rendered_baz->value && $message->rendered_bar->value, 'Message is rendered to fields with text-processing.');
  // Test missing view mode key in the rendered fields.
  $options = [
    'rendered fields' => [
      'foo' => 'rendered_foo',
    ],
  ];
  $message = Message::create([
    'template' => $this->messageTemplate
      ->id(),
  ]);
  try {
    $this->messageNotifier
      ->send($message, $options, 'test');
    $this
      ->fail('Can save rendered message with missing view mode.');
  } catch (MessageNotifyException $e) {
    $this
      ->pass('Cannot save rendered message with missing view mode.');
  }
  // Test invalid field name.
  $options = [
    'rendered fields' => [
      'foo' => 'wrong_field',
      'bar' => 'rendered_bar',
    ],
  ];
  $message = Message::create([
    'template' => $this->messageTemplate
      ->id(),
  ]);
  try {
    $this->messageNotifier
      ->send($message, $options, 'test');
    $this
      ->fail('Can save rendered message to non-existing field.');
  } catch (MessageNotifyException $e) {
    $this
      ->pass('Cannot save rendered message to non-existing field.');
  }
}