You are here

public function MailhandlerNodeTest::testMailhandlerNodePlugin in Mailhandler 8

Tests features of Mailhandler Node plugin.

File

tests/src/Kernel/MailhandlerNodeTest.php, line 88

Class

MailhandlerNodeTest
Tests the Node handler plugin.

Namespace

Drupal\Tests\mailhandler\Kernel

Code

public function testMailhandlerNodePlugin() {
  $raw_node_mail = $this
    ->getFileContent('eml/Plain.eml');

  // Assert default handler configuration.

  /** @var \Drupal\inmail\Entity\HandlerConfig $handler_config */
  $handler_config = HandlerConfig::load('mailhandler_node');
  $this
    ->assertEquals('_mailhandler', $handler_config
    ->getConfiguration()['content_type']);

  // Update the handler configuration.
  $handler_config
    ->setConfiguration([
    'content_type' => $this->contentType1
      ->id(),
  ])
    ->save();

  // Enable "From" authentication since it is disabled by default.
  $sender_analyzer = AnalyzerConfig::load('sender');
  $sender_analyzer
    ->enable()
    ->save();

  // Process the mail.
  $this->processor
    ->process('test_key', $raw_node_mail, $this->deliverer);

  // Assert there is a new node created.
  $nodes = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadMultiple();

  /** @var \Drupal\node\NodeInterface $node */
  $node = reset($nodes);

  // Assert the node field values.
  $this
    ->assertEquals('Google Summer of Code 2016', $node
    ->getTitle());

  // The footer has been stripped out.
  $this
    ->assertEquals('Hello, Drupal!', $node
    ->get('body')->value);
  $this
    ->assertEquals('full_html', $node
    ->get('body')->format);
  $this
    ->assertEquals($this->user
    ->id(), $node
    ->getOwnerId());
  $this
    ->assertEquals($this->contentType1
    ->id(), $node
    ->getType());
  $this
    ->assertEquals(NODE_PUBLISHED, $node
    ->get('status')->value);

  // Change content type to "Detect (Mailhandler)".
  $handler_config
    ->setConfiguration([
    'content_type' => '_mailhandler',
  ])
    ->save();
  $this->processor
    ->process('test_key', $raw_node_mail, $this->deliverer);
  $nodes = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadMultiple();

  /** @var \Drupal\node\NodeInterface $node */
  $node = end($nodes);

  // Assert content type was successfully detected.
  $this
    ->assertEquals('Google Summer of Code 2016', $node
    ->getTitle());
  $this
    ->assertEquals($this->contentType2
    ->id(), $node
    ->getType());
}