You are here

public function MailhandlerCommentTest::testMailhandlerCommentPluginAnonymous in Mailhandler 8

Tests features of Mailhandler Comment plugin for anonymous users.

File

mailhandler_comment/tests/src/Kernel/MailhandlerCommentTest.php, line 100

Class

MailhandlerCommentTest
Tests the Comment handler plugin.

Namespace

Drupal\Tests\mailhandler_comment\Kernel

Code

public function testMailhandlerCommentPluginAnonymous() {

  // Get a sample comment mail message.
  $raw_comment_mail = $this
    ->getFileContent('eml/Comment.eml');

  // Replace a node ID with an actual node ID.
  $raw_comment_mail = str_replace('[comment][#1]', '[comment][#' . $this->node
    ->id() . ']', $raw_comment_mail);

  // Assert default handler configuration.

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

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

  // Since SenderAnalyzer is disabled by default, authenticated user will
  // fallback to an anonymous user.
  $comments = Comment::loadMultiple();
  $this
    ->assertEmpty($comments, 'Anonymous user has no permission to post comments.');

  // Clear the cache before updating permissions for anonymous users.
  drupal_flush_all_caches();

  // Grant "post comments" permission to anonymous users.
  user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
    'post comments',
  ]);

  // Trigger the processing again.
  $this->processor
    ->process('test_key', $raw_comment_mail, $this->deliverer);
  $comments = Comment::loadMultiple();
  $this
    ->assertEquals(count($comments), 1, 'There is a new comment created.');

  /** @var \Drupal\comment\CommentInterface $comment */
  $comment = reset($comments);
  $this
    ->assertEquals('Anonymous', $comment
    ->getAuthorName());
  $this
    ->assertEquals(0, $comment
    ->getOwnerId());
  $this
    ->assertEquals('Great article!', $comment
    ->getSubject());
}