View source
<?php
namespace Drupal\Tests\mailhandler_comment\Kernel;
use Drupal\comment\CommentInterface;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\inmail\Entity\AnalyzerConfig;
use Drupal\inmail\Entity\DelivererConfig;
use Drupal\inmail\Entity\HandlerConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
use Drupal\user\RoleInterface;
class MailhandlerCommentTest extends KernelTestBase {
use CommentTestTrait;
public static $modules = [
'mailhandler',
'mailhandler_comment',
'inmail',
'comment',
'system',
'node',
'user',
'field',
'text',
];
public function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installEntitySchema('comment');
$this
->installEntitySchema('inmail_handler');
$this
->installSchema('comment', [
'comment_entity_statistics',
]);
$this
->installSchema('system', [
'sequences',
]);
$this
->installConfig([
'inmail',
'mailhandler',
'mailhandler_comment',
'node',
'user',
'comment',
]);
$this->blog = NodeType::create([
'type' => 'blog',
'name' => 'Blog',
]);
$this->blog
->save();
$role = Role::create([
'id' => 'mailhandler',
'label' => 'Mailhandler',
]);
$role
->grantPermission('post comments');
$role
->save();
$user = User::create([
'mail' => 'milos@example.com',
'name' => 'Milos',
]);
$user
->addRole($role
->id());
$user
->save();
$this->user = $user;
$this->node = Node::create([
'type' => $this->blog
->id(),
'title' => 'Sample blog post',
]);
$this->node
->save();
$this
->addDefaultCommentField('node', 'blog');
$this->processor = \Drupal::service('inmail.processor');
$this->parser = \Drupal::service('inmail.mime_parser');
$this->deliverer = DelivererConfig::create([
'id' => 'test',
]);
}
public function testMailhandlerCommentPluginAnonymous() {
$raw_comment_mail = $this
->getFileContent('eml/Comment.eml');
$raw_comment_mail = str_replace('[comment][#1]', '[comment][#' . $this->node
->id() . ']', $raw_comment_mail);
$handler_config = HandlerConfig::load('mailhandler_comment');
$this
->assertEquals('node', $handler_config
->getConfiguration()['entity_type']);
$this->processor
->process('test_key', $raw_comment_mail, $this->deliverer);
$comments = Comment::loadMultiple();
$this
->assertEmpty($comments, 'Anonymous user has no permission to post comments.');
drupal_flush_all_caches();
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
'post comments',
]);
$this->processor
->process('test_key', $raw_comment_mail, $this->deliverer);
$comments = Comment::loadMultiple();
$this
->assertEquals(count($comments), 1, 'There is a new comment created.');
$comment = reset($comments);
$this
->assertEquals('Anonymous', $comment
->getAuthorName());
$this
->assertEquals(0, $comment
->getOwnerId());
$this
->assertEquals('Great article!', $comment
->getSubject());
}
public function testMailhandlerCommentPluginAuthenticated() {
$raw_comment_mail = $this
->getFileContent('eml/Comment.eml');
$raw_comment_mail = str_replace('[comment][#1]', '[comment][#' . $this->node
->id() . ']', $raw_comment_mail);
$sender_analyzer = AnalyzerConfig::load('sender');
$sender_analyzer
->enable()
->save();
$this->processor
->process('test_key', $raw_comment_mail, $this->deliverer);
$comments = Comment::loadMultiple();
$this
->assertEquals(count($comments), 1, 'There is a new comment created.');
$comment = end($comments);
$this
->assertEquals($this->user
->getDisplayName(), $comment
->getAuthorName());
$this
->assertEquals($this->user
->id(), $comment
->getOwnerId());
$this
->assertEquals(CommentInterface::PUBLISHED, $comment
->getStatus());
$this
->assertEquals('Great article!', $comment
->getSubject());
}
public function getFileContent($filename) {
$path = drupal_get_path('module', 'mailhandler_comment') . '/tests/' . $filename;
return file_get_contents(DRUPAL_ROOT . '/' . $path);
}
}