View source
<?php
namespace Drupal\Tests\mailhandler\Kernel;
use Drupal\inmail\Entity\AnalyzerConfig;
use Drupal\inmail\Entity\DelivererConfig;
use Drupal\inmail\Entity\HandlerConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
class MailhandlerNodeTest extends KernelTestBase {
public static $modules = [
'mailhandler',
'inmail',
'system',
'node',
'user',
'field',
'text',
];
public function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installEntitySchema('inmail_handler');
$this
->installSchema('system', [
'sequences',
]);
$this
->installConfig([
'inmail',
'mailhandler',
'node',
'user',
]);
$this->contentType1 = NodeType::create([
'type' => 'blog',
'name' => 'Blog',
]);
$this->contentType1
->save();
node_add_body_field($this->contentType1);
$this->contentType2 = NodeType::create([
'type' => 'page',
'name' => 'Page',
]);
$this->contentType2
->save();
node_add_body_field($this->contentType2);
$role = Role::create([
'id' => 'mailhandler',
'label' => 'Mailhandler',
]);
$role
->grantPermission('create blog content');
$role
->grantPermission('create page content');
$role
->save();
$user = User::create([
'mail' => 'milos@example.com',
'name' => 'Milos',
]);
$user
->addRole($role
->id());
$user
->save();
$this->user = $user;
$this->processor = \Drupal::service('inmail.processor');
$this->parser = \Drupal::service('inmail.mime_parser');
$this->deliverer = DelivererConfig::create([
'id' => 'test',
]);
}
public function testMailhandlerNodePlugin() {
$raw_node_mail = $this
->getFileContent('eml/Plain.eml');
$handler_config = HandlerConfig::load('mailhandler_node');
$this
->assertEquals('_mailhandler', $handler_config
->getConfiguration()['content_type']);
$handler_config
->setConfiguration([
'content_type' => $this->contentType1
->id(),
])
->save();
$sender_analyzer = AnalyzerConfig::load('sender');
$sender_analyzer
->enable()
->save();
$this->processor
->process('test_key', $raw_node_mail, $this->deliverer);
$nodes = \Drupal::entityTypeManager()
->getStorage('node')
->loadMultiple();
$node = reset($nodes);
$this
->assertEquals('Google Summer of Code 2016', $node
->getTitle());
$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);
$handler_config
->setConfiguration([
'content_type' => '_mailhandler',
])
->save();
$this->processor
->process('test_key', $raw_node_mail, $this->deliverer);
$nodes = \Drupal::entityTypeManager()
->getStorage('node')
->loadMultiple();
$node = end($nodes);
$this
->assertEquals('Google Summer of Code 2016', $node
->getTitle());
$this
->assertEquals($this->contentType2
->id(), $node
->getType());
}
public function testSignedMails() {
$raw_signed_mail = $this
->getFileContent('eml/PGP_Signed_Inline.eml');
$signed_mail = $this->parser
->parseMessage($raw_signed_mail);
$this->user
->set('mailhandler_gpg_key', [
'public_key' => $this
->getFileContent('keys/public.key'),
]);
$this->user
->save();
$handler_config = HandlerConfig::load('mailhandler_node');
$handler_config
->setConfiguration([
'content_type' => $this->contentType1
->id(),
])
->save();
$this->processor
->process('test_key', $raw_signed_mail, $this->deliverer);
$nodes = \Drupal::entityTypeManager()
->getStorage('node')
->loadMultiple();
$node = reset($nodes);
if (extension_loaded('gnupg')) {
$this
->assertEquals($signed_mail
->getSubject(), $node
->getTitle());
$this
->assertEquals('Hello world!', $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);
}
}
public function getFileContent($filename) {
$path = drupal_get_path('module', 'mailhandler') . '/tests/' . $filename;
return file_get_contents(DRUPAL_ROOT . '/' . $path);
}
}