diff --git a/mailhandler_d8_comment/src/Plugin/inmail/Handler/MailhandlerComment.php b/mailhandler_d8_comment/src/Plugin/inmail/Handler/MailhandlerComment.php
index 8ba9f3f..f9cf2b6 100644
--- a/mailhandler_d8_comment/src/Plugin/inmail/Handler/MailhandlerComment.php
+++ b/mailhandler_d8_comment/src/Plugin/inmail/Handler/MailhandlerComment.php
@@ -4,16 +4,14 @@ namespace Drupal\mailhandler_d8_comment\Plugin\inmail\Handler;
use Drupal\comment\CommentInterface;
use Drupal\comment\Entity\Comment;
-use Drupal\Core\Entity\Entity;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
+use Drupal\inmail\DefaultAnalyzerResult;
+use Drupal\inmail\DefaultAnalyzerResultInterface;
use Drupal\inmail\MIME\MessageInterface;
use Drupal\inmail\Plugin\inmail\Handler\HandlerBase;
use Drupal\inmail\ProcessorResultInterface;
-use Drupal\mailhandler_d8\MailhandlerAnalyzerResult;
-use Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface;
-use Drupal\user\Entity\User;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
@@ -69,9 +67,9 @@ class MailhandlerComment extends HandlerBase implements ContainerFactoryPluginIn
*/
public function invoke(MessageInterface $message, ProcessorResultInterface $processor_result) {
try {
- $result = $this->getMailhandlerResult($processor_result);
+ $result = $processor_result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC);
- if ($result->getEntityType() != 'comment') {
+ if (!$result->hasContext('entity_type') || $result->getContext('entity_type')->getContextValue()['entity_type'] != 'comment') {
// Do not run this handler in case we are not dealing with comments.
return;
}
@@ -88,37 +86,11 @@ class MailhandlerComment extends HandlerBase implements ContainerFactoryPluginIn
}
/**
- * Returns a Mailhandler analyzer result instance.
- *
- * @param \Drupal\inmail\ProcessorResultInterface $processor_result
- * The result and log container for the message, containing the message
- * deliverer and possibly analyzer results.
- * @return \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface
- * The Mailhandler analyzer result instance.
- *
- * @throws \Exception
- * Throws an exception in case there is no Mailhandler analyzer result
- * object created. It happens in case all Mailhandler analyzers are
- * disabled.
- */
- public function getMailhandlerResult(ProcessorResultInterface $processor_result) {
- /** @var \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $result */
- $result = $processor_result->getAnalyzerResult(MailhandlerAnalyzerResult::TOPIC);
-
- // @todo: Remove when support for core Inmail result objects is implemented.
- if (!$result) {
- throw new \Exception('Mailhandler Analyzer result object cannot be ensured.');
- }
-
- return $result;
- }
-
- /**
* Creates a new comment from given mail message.
*
* @param \Drupal\inmail\MIME\MessageInterface $message
* The mail message.
- * @param \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $result
+ * @param \Drupal\inmail\DefaultAnalyzerResultInterface $result
* The analyzer result.
*
* @return \Drupal\comment\Entity\Comment
@@ -127,10 +99,9 @@ class MailhandlerComment extends HandlerBase implements ContainerFactoryPluginIn
* @throws \Exception
* Throws an exception in case user is not authorized to create a comment.
*/
- protected function createComment(MessageInterface $message, MailhandlerAnalyzerResultInterface $result) {
+ protected function createComment(MessageInterface $message, DefaultAnalyzerResultInterface $result) {
$entity_id = $this->getEntityId($result);
-
// Validate whether user is allowed to post comments.
$user = $this->validateUser($result);
@@ -138,7 +109,7 @@ class MailhandlerComment extends HandlerBase implements ContainerFactoryPluginIn
$comment = Comment::create([
'entity_type' => $this->configuration['entity_type'],
'entity_id' => $entity_id,
- 'uid' => $user,
+ 'uid' => $user->id(),
'subject' => $result->getSubject(),
'comment_body' => [
'value' => $result->getBody(),
@@ -156,8 +127,8 @@ class MailhandlerComment extends HandlerBase implements ContainerFactoryPluginIn
/**
* Checks if the user is authenticated and authorized to post comments.
*
- * @param \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $result
- * The analyzer result instance.
+ * @param \Drupal\inmail\DefaultAnalyzerResultInterface $result
+ * The analyzer result.
*
* @return \Drupal\user\UserInterface
* The identified user.
@@ -165,15 +136,14 @@ class MailhandlerComment extends HandlerBase implements ContainerFactoryPluginIn
* @throws \Exception
* Throws an exception in case user is not validated.
*/
- protected function validateUser(MailhandlerAnalyzerResultInterface $result) {
+ protected function validateUser(DefaultAnalyzerResultInterface $result) {
// Do not allow unverified PGP-signed messages.
- if ($result->isSigned() && !$result->isVerified()) {
+ if ($result->hasContext('verified') && !$result->getContext('verified')->getContextValue()) {
throw new \Exception('Failed to process the message. PGP-signed message is not verified.');
}
- // Get the user or fallback to anonymous user.
- // @todo: Replace with current user after https://www.drupal.org/node/2754261.
- $user = $result->isUserAuthenticated() ? $result->getUser() : User::getAnonymousUser();
+ // Get the current user.
+ $user = \Drupal::currentUser()->getAccount();
// Authorize a user.
$access = $this->entityTypeManager->getAccessControlHandler('comment')->createAccess('comment', $user, [], TRUE);
@@ -239,8 +209,8 @@ class MailhandlerComment extends HandlerBase implements ContainerFactoryPluginIn
/**
* Returns a referenced entity ID.
*
- * @param \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $result
- * The analzer result instance.
+ * @param \Drupal\inmail\DefaultAnalyzerResultInterface $result
+ * The analyzer result.
*
* @return string
* The entity ID.
@@ -248,7 +218,7 @@ class MailhandlerComment extends HandlerBase implements ContainerFactoryPluginIn
* @throws \Exception.
* Throws an exception in case entity ID is not valid.
*/
- protected function getEntityId(MailhandlerAnalyzerResultInterface $result) {
+ protected function getEntityId(DefaultAnalyzerResultInterface $result) {
$subject = $result->getSubject();
if (!preg_match('/^\[#(\d+)\]\s+/', $subject, $matches)) {
throw new \Exception('Referenced entity ID of the comment could not be identified.');
diff --git a/src/MailhandlerAnalyzerResult.php b/src/MailhandlerAnalyzerResult.php
deleted file mode 100644
index e8658fc..0000000
--- a/src/MailhandlerAnalyzerResult.php
+++ /dev/null
@@ -1,316 +0,0 @@
-sender)) {
- $this->sender = $sender;
- }
- }
-
- /**
- * {@inheritdoc}
- */
- public function getSender() {
- return $this->sender;
- }
-
- /**
- * {@inheritdoc}
- */
- public function setUser($user) {
- if (!isset($this->user)) {
- $this->user = $user;
- }
- }
-
- /**
- * {@inheritdoc}
- */
- public function getUser() {
- return $this->user;
- }
-
- /**
- * {@inheritdoc}
- */
- public function isUserAuthenticated() {
- return $this->user ? $this->user->isAuthenticated() : FALSE;
- }
-
- /**
- * {@inheritdoc}
- */
- public function summarize() {
- $summary = [];
- if ($this->getSender()) {
- $summary['sender'] = $this->getSender();
- }
- if ($this->getSubject()) {
- $summary['subject'] = $this->getSubject();
- }
-
- return $summary;
- }
-
- /**
- * {@inheritdoc}
- */
- public function getBody() {
- return $this->body;
- }
-
- /**
- * {@inheritdoc}
- */
- public function setBody($body) {
- $this->body = $body;
- }
-
- /**
- * {@inheritdoc}
- */
- public function getFooter() {
- return $this->footer;
- }
-
- /**
- * {@inheritdoc}
- */
- public function setFooter($footer) {
- $this->footer = $footer;
- }
-
- /**
- * @inheritDoc
- */
- public function getSubject() {
- return $this->subject;
- }
-
- /**
- * @inheritDoc
- */
- public function setSubject($subject) {
- $this->subject = $subject;
- }
-
- /**
- * @inheritDoc
- */
- public function getEntityType() {
- return $this->entity_type;
- }
-
- /**
- * @inheritDoc
- */
- public function setEntityType($entity_type) {
- $this->entity_type = $entity_type;
- }
-
- /**
- * @inheritDoc
- */
- public function getBundle() {
- return $this->bundle;
- }
-
- /**
- * @inheritDoc
- */
- public function setBundle($bundle) {
- $this->bundle = $bundle;
- }
-
- /**
- * @inheritDoc
- */
- public function isSigned() {
- return (bool) $this->signature;
- }
-
- /**
- * @inheritDoc
- */
- public function getSignature() {
- return $this->signature;
- }
-
- /**
- * @inheritDoc
- */
- public function setSignature($signature) {
- $this->signature = $signature;
- }
-
- /**
- * @inheritDoc
- */
- public function getSignedText() {
- return $this->signedText;
- }
-
- /**
- * @inheritDoc
- */
- public function setSignedText($signed_text) {
- $this->signedText = $signed_text;
- }
-
- /**
- * @inheritDoc
- */
- public function getPgpType() {
- return $this->pgpType;
- }
-
- /**
- * @inheritDoc
- */
- public function setPgpType($pgp_type) {
- $this->pgpType = $pgp_type;
- }
-
- /**
- * @inheritDoc
- */
- public function setVerified($verified) {
- $this->verified = $verified;
- }
-
- /**
- * @inheritDoc
- */
- public function isVerified() {
- return $this->verified;
- }
-
-}
diff --git a/src/MailhandlerAnalyzerResultInterface.php b/src/MailhandlerAnalyzerResultInterface.php
deleted file mode 100644
index 505316f..0000000
--- a/src/MailhandlerAnalyzerResultInterface.php
+++ /dev/null
@@ -1,210 +0,0 @@
-ensureAnalyzerResult(MailhandlerAnalyzerResult::TOPIC, MailhandlerAnalyzerResult::createFactory());
+ $result = $processor_result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC);
$this->analyzeBody($message, $result);
}
@@ -35,10 +34,10 @@ class BodyAnalyzer extends AnalyzerBase {
*
* @param \Drupal\inmail\MIME\MessageInterface $message
* A mail message to be analyzed.
- * @param \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $result
+ * @param \Drupal\inmail\DefaultAnalyzerResultInterface $result
* The analyzer result.
*/
- protected function analyzeBody(MessageInterface $message, MailhandlerAnalyzerResultInterface $result) {
+ protected function analyzeBody(MessageInterface $message, DefaultAnalyzerResultInterface $result) {
// Use message processed body if available.
$body = $result->getBody() ?: $message->getBody();
diff --git a/src/Plugin/inmail/Analyzer/EntityTypeAnalyzer.php b/src/Plugin/inmail/Analyzer/EntityTypeAnalyzer.php
index 35882ab..30e3916 100644
--- a/src/Plugin/inmail/Analyzer/EntityTypeAnalyzer.php
+++ b/src/Plugin/inmail/Analyzer/EntityTypeAnalyzer.php
@@ -2,11 +2,13 @@
namespace Drupal\mailhandler_d8\Plugin\inmail\Analyzer;
+use Drupal\Core\Plugin\Context\Context;
+use Drupal\Core\Plugin\Context\ContextDefinition;
+use Drupal\inmail\DefaultAnalyzerResult;
+use Drupal\inmail\DefaultAnalyzerResultInterface;
use Drupal\inmail\MIME\MessageInterface;
use Drupal\inmail\Plugin\inmail\Analyzer\AnalyzerBase;
use Drupal\inmail\ProcessorResultInterface;
-use Drupal\mailhandler_d8\MailhandlerAnalyzerResult;
-use Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface;
/**
* An entity type and bundle analyzer.
@@ -24,8 +26,7 @@ class EntityTypeAnalyzer extends AnalyzerBase {
* {@inheritdoc}
*/
public function analyze(MessageInterface $message, ProcessorResultInterface $processor_result) {
- /** @var \Drupal\mailhandler_d8\MailhandlerAnalyzerResult $result */
- $result = $processor_result->ensureAnalyzerResult(MailhandlerAnalyzerResult::TOPIC, MailhandlerAnalyzerResult::createFactory());
+ $result = $processor_result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC);
$this->findEntityType($message, $result);
}
@@ -35,10 +36,10 @@ class EntityTypeAnalyzer extends AnalyzerBase {
*
* @param \Drupal\inmail\MIME\MessageInterface $message
* The mail message.
- * @param \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $result
- * The analyzed message result.
+ * @param \Drupal\inmail\DefaultAnalyzerResultInterface $result
+ * The analyzed result.
*/
- protected function findEntityType(MessageInterface $message, MailhandlerAnalyzerResultInterface $result) {
+ protected function findEntityType(MessageInterface $message, DefaultAnalyzerResultInterface $result) {
$subject = $result->getSubject() ?: $message->getSubject();
$entity_type = NULL;
$bundle = NULL;
@@ -54,8 +55,15 @@ class EntityTypeAnalyzer extends AnalyzerBase {
}
}
- $result->setEntityType($entity_type);
- $result->setBundle($bundle);
+ // Add entity type context.
+ $context_data = [
+ 'entity_type' => $entity_type,
+ 'bundle' => $bundle,
+ ];
+ $context_definition = new ContextDefinition('any', $this->t('Entity type context'));
+ $context = new Context($context_definition, $context_data);
+ $result->addContext('entity_type', $context);
+
$result->setSubject($subject);
}
diff --git a/src/Plugin/inmail/Analyzer/FooterAnalyzer.php b/src/Plugin/inmail/Analyzer/FooterAnalyzer.php
index 35958f3..b47a526 100644
--- a/src/Plugin/inmail/Analyzer/FooterAnalyzer.php
+++ b/src/Plugin/inmail/Analyzer/FooterAnalyzer.php
@@ -2,11 +2,11 @@
namespace Drupal\mailhandler_d8\Plugin\inmail\Analyzer;
+use Drupal\inmail\DefaultAnalyzerResult;
+use Drupal\inmail\DefaultAnalyzerResultInterface;
use Drupal\inmail\MIME\MessageInterface;
use Drupal\inmail\Plugin\inmail\Analyzer\AnalyzerBase;
use Drupal\inmail\ProcessorResultInterface;
-use Drupal\mailhandler_d8\MailhandlerAnalyzerResult;
-use Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface;
/**
* A message footer analyzer.
@@ -24,8 +24,7 @@ class FooterAnalyzer extends AnalyzerBase {
* {@inheritdoc}
*/
public function analyze(MessageInterface $message, ProcessorResultInterface $processor_result) {
- /** @var \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $result */
- $result = $processor_result->ensureAnalyzerResult(MailhandlerAnalyzerResult::TOPIC, MailhandlerAnalyzerResult::createFactory());
+ $result = $processor_result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC);
$this->findFooter($message, $result);
}
@@ -35,10 +34,10 @@ class FooterAnalyzer extends AnalyzerBase {
*
* @param \Drupal\inmail\MIME\MessageInterface $message
* A mail message to be analyzed.
- * @param \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $result
+ * @param \Drupal\inmail\DefaultAnalyzerResultInterface $result
* The analyzer result.
*/
- protected function findFooter(MessageInterface $message, MailhandlerAnalyzerResultInterface $result) {
+ protected function findFooter(MessageInterface $message, DefaultAnalyzerResultInterface $result) {
// Get a message body.
$body = $result->getBody() ?: $message->getBody();
$footer = NULL;
diff --git a/src/Plugin/inmail/Analyzer/PGPAnalyzer.php b/src/Plugin/inmail/Analyzer/PGPAnalyzer.php
index 5db7e59..aca9148 100644
--- a/src/Plugin/inmail/Analyzer/PGPAnalyzer.php
+++ b/src/Plugin/inmail/Analyzer/PGPAnalyzer.php
@@ -3,13 +3,15 @@
namespace Drupal\mailhandler_d8\Plugin\inmail\Analyzer;
use Drupal\Core\Logger\RfcLogLevel;
+use Drupal\Core\Plugin\Context\Context;
+use Drupal\Core\Plugin\Context\ContextDefinition;
+use Drupal\inmail\DefaultAnalyzerResult;
+use Drupal\inmail\DefaultAnalyzerResultInterface;
use Drupal\inmail\MIME\MessageInterface;
use Drupal\inmail\MIME\MultipartEntity;
use Drupal\inmail\MIME\MultipartMessage;
use Drupal\inmail\Plugin\inmail\Analyzer\AnalyzerBase;
use Drupal\inmail\ProcessorResultInterface;
-use Drupal\mailhandler_d8\MailhandlerAnalyzerResult;
-use Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface;
/**
* An analyzer for PGP signed messages.
@@ -27,9 +29,7 @@ class PGPAnalyzer extends AnalyzerBase {
* {@inheritdoc}
*/
public function analyze(MessageInterface $message, ProcessorResultInterface $processor_result) {
- // Ensure analyzer result instance.
- /** @var \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $result */
- $result = $processor_result->ensureAnalyzerResult(MailhandlerAnalyzerResult::TOPIC, MailhandlerAnalyzerResult::createFactory());
+ $result = $processor_result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC);
$context = [];
// Check if we are dealing with PGP-signed message.
@@ -52,15 +52,15 @@ class PGPAnalyzer extends AnalyzerBase {
*
* @param \Drupal\inmail\MIME\MessageInterface $message
* The message to check signature.
- * @param \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $result
- * The result object instance.
+ * @param \Drupal\inmail\DefaultAnalyzerResultInterface $result
+ * The analyzer result.
* @param array $context
* An array to provide context data in case the message is signed.
*
* @return bool
* TRUE if message is signed. Otherwise, FALSE.
*/
- protected function isSigned(MessageInterface $message, MailhandlerAnalyzerResultInterface $result, array &$context) {
+ protected function isSigned(MessageInterface $message, DefaultAnalyzerResultInterface $result, array &$context) {
// Support PGP/MIME signed messages.
if ($message instanceof MultipartMessage) {
$parameters = $message->getContentType()['parameters'];
@@ -71,8 +71,6 @@ class PGPAnalyzer extends AnalyzerBase {
// Check the subtype of a content type.
if ($part->getContentType()['subtype'] == 'pgp-signature') {
$signature = $part->getBody();
- $result->setSignature($signature);
- $result->setPgpType('mime');
// In order to find a signed text part of the message, we need to
// skip the signature.
@@ -81,9 +79,17 @@ class PGPAnalyzer extends AnalyzerBase {
$signed_text_part = $message->getPart($signed_text_index);
// Add index of the signed message part to the context.
$context['signed_text_index'] = $signed_text_index;
- // Include headers into the signed text.
- $signed_text = $signed_text_part->toString();
- $result->setSignedText($signed_text);
+ $context_definition = new ContextDefinition('any', $this->t('PGP context'));
+ $context_data = [
+ 'pgp_type' => 'mime',
+ // Include headers into the signed text.
+ 'signed_text' => $signed_text_part->toString(),
+ 'signature' => $signature,
+ ];
+ // @todo: Use setContext() after https://www.drupal.org/node/2770679.
+ if (!$result->hasContext('pgp')) {
+ $result->addContext('pgp', new Context($context_definition, $context_data));
+ }
// Update the subject field.
if ($signed_text_part->getHeader()->hasField('Subject')) {
@@ -99,17 +105,24 @@ class PGPAnalyzer extends AnalyzerBase {
else {
// Cleartext signed message validation was implemented by following
// RFC 4880. See https://tools.ietf.org/html/rfc4880#section-7
- $starts_with_pgp_header = strpos($message->getBody(), "-----BEGIN PGP SIGNED MESSAGE-----\nHash:") === 0;
+ $message_body = $message->getBody();
+ $starts_with_pgp_header = strpos($message_body, "-----BEGIN PGP SIGNED MESSAGE-----\nHash:") === 0;
if ($starts_with_pgp_header) {
- $has_pgp_signature = (bool) strpos($message->getBody(), "-----BEGIN PGP SIGNATURE-----\n");
+ $has_pgp_signature = (bool) strpos($message_body, "-----BEGIN PGP SIGNATURE-----\n");
$pgp_signature_end = '-----END PGP SIGNATURE-----';
- $ends_with_pgp_signature = trim(strstr($message->getBody(), "\n$pgp_signature_end")) === $pgp_signature_end;
+ $ends_with_pgp_signature = trim(strstr($message_body, "\n$pgp_signature_end")) === $pgp_signature_end;
if ($has_pgp_signature && $ends_with_pgp_signature) {
- // Populate the context array with signature data.
- $signed_text = $message->getBody();
- $result->setPgpType('inline');
- $result->setSignedText($signed_text);
- $result->setSignature(FALSE);
+ // Add a PGP context.
+ $context_definition = new ContextDefinition('any', $this->t('PGP context'));
+ $context_data = [
+ 'pgp_type' => 'inline',
+ 'signed_text' => $message_body,
+ 'signature' => FALSE,
+ ];
+ // @todo: Use setContext() after https://www.drupal.org/node/2770679.
+ if (!$result->hasContext('pgp')) {
+ $result->addContext('pgp', new Context($context_definition, $context_data));
+ }
return TRUE;
}
@@ -122,22 +135,23 @@ class PGPAnalyzer extends AnalyzerBase {
/**
* Verifies the PGP signature.
*
- * @param \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $result
- * The analyzer result instance containing information about signed message.
+ * @param \Drupal\inmail\DefaultAnalyzerResultInterface $result
+ * The analyzer result instance containing PGP context.
*
* @throws \Exception
* Throws an exception in case verification fails.
*/
- protected function verifySignature(MailhandlerAnalyzerResultInterface $result) {
+ protected function verifySignature(DefaultAnalyzerResultInterface $result) {
if (!extension_loaded('gnupg')) {
throw new \Exception('PHP extension "gnupg" has to enabled to verify the signature.');
}
+ $pgp_context = $result->getContext('pgp');
// Initialize GnuPG resource.
$gpg = gnupg_init();
// Verify PGP signature.
- $verification = gnupg_verify($gpg, $result->getSignedText(), $result->getSignature());
+ $verification = gnupg_verify($gpg, $pgp_context->getContextValue()['signed_text'], $pgp_context->getContextValue()['signature']);
// Only support "full" and "ultimate" trust levels.
if (!$verification || $verification[0]['validity'] < GNUPG_VALIDITY_FULL) {
@@ -150,7 +164,7 @@ class PGPAnalyzer extends AnalyzerBase {
$key_info = reset($key_info);
// Compare the fingerprint with the identified user's fingerprint.
- if ($fingerprint != $result->getUser()->get('mailhandler_gpg_key')->fingerprint) {
+ if ($fingerprint != $result->getAccount()->get('mailhandler_gpg_key')->fingerprint) {
throw new \Exception('Failed to analyze the message. GPG key fingerprint mismatch.');
}
@@ -159,8 +173,11 @@ class PGPAnalyzer extends AnalyzerBase {
throw new \Exception('Failed to analyze the message. GPG public key was either disabled, expired or revoked.');
}
- // Set a message verification flag.
- $result->setVerified(TRUE);
+ // Set a message verification flag to the context.
+ // @todo: Update PGP context after https://www.drupal.org/node/2770679.
+ if (!$result->hasContext('verified')) {
+ $result->addContext('verified', new Context(new ContextDefinition('string'), TRUE));
+ }
}
/**
@@ -168,7 +185,7 @@ class PGPAnalyzer extends AnalyzerBase {
*
* @param \Drupal\inmail\MIME\MessageInterface $message
* The mail message.
- * @param \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $result
+ * @param \Drupal\inmail\DefaultAnalyzerResultInterface $result
* The analyzer result.
* @param array $context
* The array with context data.
@@ -176,12 +193,12 @@ class PGPAnalyzer extends AnalyzerBase {
* @return string
* The analyzed message body.
*/
- protected function findBody(MessageInterface $message, MailhandlerAnalyzerResultInterface $result, array &$context) {
+ protected function findBody(MessageInterface $message, DefaultAnalyzerResultInterface $result, array &$context) {
// By default, use original message body.
$body = $message->getBody();
// Extract body from PGP/MIME messages.
- if ($result->getPgpType() == 'mime') {
+ if ($result->getContext('pgp')->getContextValue()['pgp_type'] == 'mime') {
/** @var \Drupal\inmail\MIME\MultipartMessage $message */
/** @var \Drupal\inmail\MIME\MultipartEntity $signed_message_part */
$signed_message_part = $message->getPart($context['signed_text_index']);
@@ -201,11 +218,10 @@ class PGPAnalyzer extends AnalyzerBase {
}
}
// Support for clear-text signed messages.
- if ($result->getPgpType() == 'inline') {
+ if ($result->getContext('pgp')->getContextValue()['pgp_type'] == 'inline') {
// Since the message was already checked for valid PGP signature, we
// can use the analyzed result instead of the raw message body.
- // See \Drupal\mailhandler_d8\Plugin\inmail\Analyzer\MailhandlerAnalyzer::isSigned
- $pgp_parts = explode("-----BEGIN PGP SIGNATURE-----", $result->getSignedText());
+ $pgp_parts = explode("-----BEGIN PGP SIGNATURE-----", $result->getContext('pgp')->getContextValue()['signed_text']);
// Get the message digest by following RFC 4880 recommendations.
// See https://tools.ietf.org/html/rfc4880#section-7.
// Remove PGP message header.
@@ -226,21 +242,21 @@ class PGPAnalyzer extends AnalyzerBase {
*
* @param \Drupal\inmail\MIME\MessageInterface $message
* The mail message.
- * @param \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $result
+ * @param \Drupal\inmail\DefaultAnalyzerResultInterface $result
* The analyzer result.
* @param array $context
* The array with context data.
* @throws \Exception
* Throws an exception if user is not authenticated.
*/
- protected function findSender(MessageInterface $message, MailhandlerAnalyzerResultInterface $result, array &$context) {
+ protected function findSender(MessageInterface $message, DefaultAnalyzerResultInterface $result, array &$context) {
$sender = NULL;
$user = NULL;
$matches = [];
$from = $message->getFrom();
// Use signed headers to extract "from" address for PGP/MIME messages.
- if ($result->getPgpType() == 'mime') {
+ if ($result->getContext('pgp')->getContextValue()['pgp_type'] == 'mime') {
/** @var \Drupal\inmail\MIME\MultipartEntity $message */
$signed_text_part = $message->getPart($context['signed_text_index']);
$from = $signed_text_part->getHeader()->getFieldBody('From') ?: $message->getFrom();
@@ -251,7 +267,7 @@ class PGPAnalyzer extends AnalyzerBase {
$sender = reset($matches);
$matched_users = \Drupal::entityTypeManager()->getStorage('user')->loadByProperties(['mail' => $sender]);
if (!empty($matched_users)) {
- $result->setUser(reset($matched_users));
+ $result->setAccount(reset($matched_users));
}
}
diff --git a/src/Plugin/inmail/Analyzer/SenderAnalyzer.php b/src/Plugin/inmail/Analyzer/SenderAnalyzer.php
index 01b4a62..48a0847 100644
--- a/src/Plugin/inmail/Analyzer/SenderAnalyzer.php
+++ b/src/Plugin/inmail/Analyzer/SenderAnalyzer.php
@@ -2,11 +2,11 @@
namespace Drupal\mailhandler_d8\Plugin\inmail\Analyzer;
+use Drupal\inmail\DefaultAnalyzerResult;
+use Drupal\inmail\DefaultAnalyzerResultInterface;
use Drupal\inmail\MIME\MessageInterface;
use Drupal\inmail\Plugin\inmail\Analyzer\AnalyzerBase;
use Drupal\inmail\ProcessorResultInterface;
-use Drupal\mailhandler_d8\MailhandlerAnalyzerResult;
-use Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface;
/**
* Finds the sender based on "From" mail header field.
@@ -26,8 +26,7 @@ class SenderAnalyzer extends AnalyzerBase {
* {@inheritdoc}
*/
public function analyze(MessageInterface $message, ProcessorResultInterface $processor_result) {
- /** @var \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $result */
- $result = $processor_result->ensureAnalyzerResult(MailhandlerAnalyzerResult::TOPIC, MailhandlerAnalyzerResult::createFactory());
+ $result = $processor_result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC);
$this->findSender($message, $result);
}
@@ -37,10 +36,10 @@ class SenderAnalyzer extends AnalyzerBase {
*
* @param \Drupal\inmail\MIME\MessageInterface $message
* The mail message.
- * @param \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $result
- * The analyzed message result.
+ * @param \Drupal\inmail\DefaultAnalyzerResultInterface $result
+ * The analyzer result.
*/
- protected function findSender(MessageInterface $message, MailhandlerAnalyzerResultInterface $result) {
+ protected function findSender(MessageInterface $message, DefaultAnalyzerResultInterface $result) {
$sender = NULL;
$user = NULL;
$matches = [];
@@ -56,12 +55,12 @@ class SenderAnalyzer extends AnalyzerBase {
$user = reset($matched_users);
}
- // Do not override a sender/user in case there is already one set.
+ // Do not override a sender/account in case there is already one set.
if (!$result->getSender()) {
$result->setSender($sender);
}
if ($user && !$result->isUserAuthenticated()) {
- $result->setUser($user);
+ $result->setAccount($user);
}
}
diff --git a/src/Plugin/inmail/Handler/MailhandlerNode.php b/src/Plugin/inmail/Handler/MailhandlerNode.php
index 67c40ed..2a31c64 100644
--- a/src/Plugin/inmail/Handler/MailhandlerNode.php
+++ b/src/Plugin/inmail/Handler/MailhandlerNode.php
@@ -7,11 +7,11 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
+use Drupal\inmail\DefaultAnalyzerResult;
+use Drupal\inmail\DefaultAnalyzerResultInterface;
use Drupal\inmail\MIME\MessageInterface;
use Drupal\inmail\Plugin\inmail\Handler\HandlerBase;
use Drupal\inmail\ProcessorResultInterface;
-use Drupal\mailhandler_d8\MailhandlerAnalyzerResult;
-use Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface;
use Drupal\node\Entity\Node;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -68,9 +68,9 @@ class MailhandlerNode extends HandlerBase implements ContainerFactoryPluginInter
*/
public function invoke(MessageInterface $message, ProcessorResultInterface $processor_result) {
try {
- $result = $this->getMailhandlerResult($processor_result);
+ $result = $processor_result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC);
- if ($result->getEntityType() != 'node') {
+ if (!$result->hasContext('entity_type') || $result->getContext('entity_type')->getContextValue()['entity_type'] != 'node') {
// Do not run this handler in case
// the identified entity type is not node.
return;
@@ -82,7 +82,7 @@ class MailhandlerNode extends HandlerBase implements ContainerFactoryPluginInter
// Create a node.
$node = $this->createNode($message, $result);
- \Drupal::logger('mailhandler')->log(RfcLogLevel::NOTICE, "\"{$node->label()}\" has been created by \"{$result->getUser()->getDisplayName()}\".");
+ \Drupal::logger('mailhandler')->log(RfcLogLevel::NOTICE, "\"{$node->label()}\" has been created by \"{$result->getAccount()->getDisplayName()}\".");
}
catch (\Exception $e) {
// Log error in case verification, authentication or authorization fails.
@@ -91,37 +91,11 @@ class MailhandlerNode extends HandlerBase implements ContainerFactoryPluginInter
}
/**
- * Returns a Mailhandler analyzer result instance.
- *
- * @param \Drupal\inmail\ProcessorResultInterface $processor_result
- * The result and log container for the message, containing the message
- * deliverer and possibly analyzer results.
- * @return \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface
- * The Mailhandler analyzer result instance.
- *
- * @throws \Exception
- * Throws an exception in case there is no Mailhandler analyzer result
- * object created. It happens in case all Mailhandler analyzers are
- * disabled.
- */
- public function getMailhandlerResult(ProcessorResultInterface $processor_result) {
- /** @var \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $result */
- $result = $processor_result->getAnalyzerResult(MailhandlerAnalyzerResult::TOPIC);
-
- // @todo: Remove when support for core Inmail result objects is implemented.
- if (!$result) {
- throw new \Exception('Mailhandler Analyzer result object cannot be ensured.');
- }
-
- return $result;
- }
-
- /**
* Creates a new node from given mail message.
*
* @param \Drupal\inmail\MIME\MessageInterface $message
* The mail message.
- * @param \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $result
+ * @param \Drupal\inmail\DefaultAnalyzerResultInterface $result
* The analyzer result.
*
* @return \Drupal\node\Entity\Node
@@ -130,14 +104,14 @@ class MailhandlerNode extends HandlerBase implements ContainerFactoryPluginInter
* @throws \Exception
* Throws an exception in case user is not authorized to create a node.
*/
- protected function createNode(MessageInterface $message, MailhandlerAnalyzerResultInterface $result) {
+ protected function createNode(MessageInterface $message, DefaultAnalyzerResultInterface $result) {
$node = Node::create([
'type' => $this->getContentType($result),
'body' => [
'value' => $result->getBody(),
'format' => 'full_html',
],
- 'uid' => $result->getUser(),
+ 'uid' => \Drupal::currentUser()->getAccount(),
'title' => $result->getSubject(),
]);
$node->save();
@@ -148,15 +122,15 @@ class MailhandlerNode extends HandlerBase implements ContainerFactoryPluginInter
/**
* Checks if the user is authenticated.
*
- * @param \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $result
+ * @param \Drupal\inmail\DefaultAnalyzerResultInterface $result
* The analyzer result instance.
*
* @throws \Exception
* Throws an exception in case user is not authenticated.
*/
- protected function authenticateUser(MailhandlerAnalyzerResultInterface $result) {
+ protected function authenticateUser(DefaultAnalyzerResultInterface $result) {
// Do not allow "From" mail header authorization for PGP-signed messages.
- if (!$result->isUserAuthenticated() || ($result->isSigned() && !$result->isVerified())) {
+ if (!$result->isUserAuthenticated() || ($result->hasContext('verified') && !$result->getContext('verified')->getContextValue())) {
throw new \Exception('Failed to process the message. User is not authenticated.');
}
}
@@ -164,8 +138,8 @@ class MailhandlerNode extends HandlerBase implements ContainerFactoryPluginInter
/**
* Returns the content type.
*
- * @param \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $result
- * The analyzer result instance.
+ * @param \Drupal\inmail\DefaultAnalyzerResultInterface $result
+ * The analyzer result.
*
* @return string
* The content type.
@@ -173,12 +147,12 @@ class MailhandlerNode extends HandlerBase implements ContainerFactoryPluginInter
* @throws \Exception
* Throws an exception in case user is not authorized to create a node.
*/
- protected function getContentType(MailhandlerAnalyzerResultInterface $result) {
+ protected function getContentType(DefaultAnalyzerResultInterface $result) {
$content_type = $this->configuration['content_type'];
$node = TRUE;
- if ($content_type == '_mailhandler') {
- $node = $result->getEntityType() == 'node' ? TRUE : FALSE;
- $content_type = $result->getBundle();
+ if ($content_type == '_mailhandler' && $result->hasContext('entity_type')) {
+ $node = $result->getContext('entity_type')->getContextValue()['entity_type'] == 'node';
+ $content_type = $result->getContext('entity_type')->getContextValue()['bundle'];
}
if (!$content_type || !$node) {
@@ -186,7 +160,7 @@ class MailhandlerNode extends HandlerBase implements ContainerFactoryPluginInter
}
// Authorize a user.
- $access = $this->entityTypeManager->getAccessControlHandler('node')->createAccess($content_type, $result->getUser(), [], TRUE);
+ $access = $this->entityTypeManager->getAccessControlHandler('node')->createAccess($content_type, $result->getAccount(), [], TRUE);
if (!$access->isAllowed()) {
throw new \Exception('Failed to process the message. User is not authorized to create a node of type "' . $content_type . '".');
}
diff --git a/tests/src/Kernel/BodyAnalyzerKernelTest.php b/tests/src/Kernel/BodyAnalyzerKernelTest.php
index db88e1c..e095c73 100644
--- a/tests/src/Kernel/BodyAnalyzerKernelTest.php
+++ b/tests/src/Kernel/BodyAnalyzerKernelTest.php
@@ -2,9 +2,9 @@
namespace Drupal\Tests\mailhandler_d8\Kernel;
+use Drupal\inmail\DefaultAnalyzerResult;
use Drupal\inmail\Entity\AnalyzerConfig;
use Drupal\inmail\ProcessorResult;
-use Drupal\mailhandler_d8\MailhandlerAnalyzerResult;
/**
* Tests the Body Analyzer plugin.
@@ -29,16 +29,14 @@ class BodyAnalyzerKernelTest extends AnalyzerTestBase {
$message = $this->parser->parseMessage($raw_message);
$result = new ProcessorResult();
+ $result->ensureAnalyzerResult(DefaultAnalyzerResult::TOPIC, DefaultAnalyzerResult::createFactory());
$body_analyzer = AnalyzerConfig::load('body');
/** @var \Drupal\mailhandler_d8\Plugin\inmail\Analyzer\BodyAnalyzer $analyzer */
$analyzer = $this->analyzerManager->createInstance($body_analyzer->getPluginId(), $body_analyzer->getConfiguration());
$analyzer->analyze($message, $result);
- // Mailhandler analyzer result.
- /** @var \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $mailhandler_result */
- $mailhandler_result = $result->getAnalyzerResult(MailhandlerAnalyzerResult::TOPIC);
-
+ $result = $result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC);
$expected_processed_body = <<
@@ -48,7 +46,7 @@ milos@example.com
EOF;
// New lines are replaced with
HTML tag.
- $this->assertEquals($expected_processed_body, $mailhandler_result->getBody());
+ $this->assertEquals($expected_processed_body, $result->getBody());
}
}
diff --git a/tests/src/Kernel/EntityTypeAnalyzerKernelTest.php b/tests/src/Kernel/EntityTypeAnalyzerKernelTest.php
index 8fbe455..00ec4dd 100644
--- a/tests/src/Kernel/EntityTypeAnalyzerKernelTest.php
+++ b/tests/src/Kernel/EntityTypeAnalyzerKernelTest.php
@@ -2,9 +2,9 @@
namespace Drupal\Tests\mailhandler_d8\Kernel;
+use Drupal\inmail\DefaultAnalyzerResult;
use Drupal\inmail\Entity\AnalyzerConfig;
use Drupal\inmail\ProcessorResult;
-use Drupal\mailhandler_d8\MailhandlerAnalyzerResult;
use Drupal\node\Entity\NodeType;
/**
@@ -30,20 +30,18 @@ class EntityTypeAnalyzerKernelTest extends AnalyzerTestBase {
$message = $this->parser->parseMessage($raw_message);
$result = new ProcessorResult();
+ $result->ensureAnalyzerResult(DefaultAnalyzerResult::TOPIC, DefaultAnalyzerResult::createFactory());
$entity_type_analyzer = AnalyzerConfig::load('entity_type');
/** @var \Drupal\mailhandler_d8\Plugin\inmail\Analyzer\EntityTypeAnalyzer $analyzer */
$analyzer = $this->analyzerManager->createInstance($entity_type_analyzer->getPluginId(), $entity_type_analyzer->getConfiguration());
$analyzer->analyze($message, $result);
+ $result = $result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC);
- // Mailhandler analyzer result.
- /** @var \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $mailhandler_result */
- $mailhandler_result = $result->getAnalyzerResult(MailhandlerAnalyzerResult::TOPIC);
-
- $this->assertEquals('Google Summer of Code 2016', $mailhandler_result->getSubject());
- $this->assertEquals('node', $mailhandler_result->getEntityType());
+ $this->assertEquals('Google Summer of Code 2016', $result->getSubject());
+ $this->assertEquals('node', $result->getContext('entity_type')->getContextValue()['entity_type']);
// The node type "page" is not recognized in the system.
- $this->assertEquals(NULL, $mailhandler_result->getBundle());
+ $this->assertEquals(NULL, $result->getContext('entity_type')->getContextValue()['bundle']);
// Create "page" node type.
$page = NodeType::create([
@@ -53,23 +51,23 @@ class EntityTypeAnalyzerKernelTest extends AnalyzerTestBase {
$page->save();
$result = new ProcessorResult();
+ $result->ensureAnalyzerResult(DefaultAnalyzerResult::TOPIC, DefaultAnalyzerResult::createFactory());
$analyzer->analyze($message, $result);
+ $result = $result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC);
- // Mailhandler analyzer result.
- /** @var \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $mailhandler_result */
- $mailhandler_result = $result->getAnalyzerResult(MailhandlerAnalyzerResult::TOPIC);
- $this->assertEquals('page', $mailhandler_result->getBundle());
+ $this->assertEquals('page', $result->getContext('entity_type')->getContextValue()['bundle']);
// Assert partial matching (entity type only) is handled properly.
$raw_message = str_replace('[node][page]', '[user][#id]', $raw_message);
/** @var \Drupal\inmail\MIME\MessageInterface $message */
$message = $this->parser->parseMessage($raw_message);
$result = new ProcessorResult();
+ $result->ensureAnalyzerResult(DefaultAnalyzerResult::TOPIC, DefaultAnalyzerResult::createFactory());
$analyzer->analyze($message, $result);
- $mailhandler_result = $result->getAnalyzerResult(MailhandlerAnalyzerResult::TOPIC);
- $this->assertEquals('user', $mailhandler_result->getEntityType());
- $this->assertEquals(NULL, $mailhandler_result->getBundle());
- $this->assertEquals('[#id] Google Summer of Code 2016', $mailhandler_result->getSubject());
+ $result = $result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC);
+ $this->assertEquals('user', $result->getContext('entity_type')->getContextValue()['entity_type']);
+ $this->assertEquals(NULL, $result->getContext('entity_type')->getContextValue()['bundle']);
+ $this->assertEquals('[#id] Google Summer of Code 2016', $result->getSubject());
}
}
diff --git a/tests/src/Kernel/FooterAnalyzerKernelTest.php b/tests/src/Kernel/FooterAnalyzerKernelTest.php
index c4f7b56..824d541 100644
--- a/tests/src/Kernel/FooterAnalyzerKernelTest.php
+++ b/tests/src/Kernel/FooterAnalyzerKernelTest.php
@@ -2,9 +2,9 @@
namespace Drupal\Tests\mailhandler_d8\Kernel;
+use Drupal\inmail\DefaultAnalyzerResult;
use Drupal\inmail\Entity\AnalyzerConfig;
use Drupal\inmail\ProcessorResult;
-use Drupal\mailhandler_d8\MailhandlerAnalyzerResult;
/**
* Tests the Footer Analyzer plugin.
@@ -29,15 +29,13 @@ class FooterAnalyzerKernelTest extends AnalyzerTestBase {
$message = $this->parser->parseMessage($raw_message);
$result = new ProcessorResult();
+ $result->ensureAnalyzerResult(DefaultAnalyzerResult::TOPIC, DefaultAnalyzerResult::createFactory());
$footer_analyzer = AnalyzerConfig::load('footer');
/** @var \Drupal\mailhandler_d8\Plugin\inmail\Analyzer\FooterAnalyzer $analyzer */
$analyzer = $this->analyzerManager->createInstance($footer_analyzer->getPluginId(), $footer_analyzer->getConfiguration());
$analyzer->analyze($message, $result);
-
- // Mailhandler analyzer result.
- /** @var \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $mailhandler_result */
- $mailhandler_result = $result->getAnalyzerResult(MailhandlerAnalyzerResult::TOPIC);
+ $result = $result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC);
$expected_processed_body = 'Hello, Drupal!';
$expected_footer = <<assertEquals($expected_processed_body, $mailhandler_result->getBody());
- $this->assertEquals($expected_footer, $mailhandler_result->getFooter());
+ $this->assertEquals($expected_processed_body, $result->getBody());
+ $this->assertEquals($expected_footer, $result->getFooter());
// Assert footer is not processed for signed messages.
$signed_mail = $this->getFileContent('eml/PGP_Signed_Inline.eml');
$message = $this->parser->parseMessage($signed_mail);
$result = new ProcessorResult();
+ $result->ensureAnalyzerResult(DefaultAnalyzerResult::TOPIC, DefaultAnalyzerResult::createFactory());
$analyzer = $this->analyzerManager->createInstance($footer_analyzer->getPluginId(), $footer_analyzer->getConfiguration());
$analyzer->analyze($message, $result);
- /** @var \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $mailhandler_result */
- $mailhandler_result = $result->getAnalyzerResult(MailhandlerAnalyzerResult::TOPIC);
+ $result = $result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC);
- $this->assertEquals(NULL, $mailhandler_result->getBody());
- $this->assertEquals(NULL, $mailhandler_result->getFooter());
+ $this->assertEquals(NULL, $result->getBody());
+ $this->assertEquals(NULL, $result->getFooter());
}
}
diff --git a/tests/src/Kernel/MailhandlerNodeTest.php b/tests/src/Kernel/MailhandlerNodeTest.php
index 5ee4fb8..bab2a8c 100644
--- a/tests/src/Kernel/MailhandlerNodeTest.php
+++ b/tests/src/Kernel/MailhandlerNodeTest.php
@@ -87,8 +87,6 @@ class MailhandlerNodeTest extends KernelTestBase {
*/
public function testMailhandlerNodePlugin() {
$raw_node_mail = $this->getFileContent('eml/Plain.eml');
- /** @var \Drupal\inmail\MIME\MessageInterface $node_mail */
- $node_mail = $this->parser->parseMessage($raw_node_mail);
// Assert default handler configuration.
/** @var \Drupal\inmail\Entity\HandlerConfig $handler_config */
diff --git a/tests/src/Kernel/PGPAnalyzerKernelTest.php b/tests/src/Kernel/PGPAnalyzerKernelTest.php
index 125cc7c..54ec77a 100644
--- a/tests/src/Kernel/PGPAnalyzerKernelTest.php
+++ b/tests/src/Kernel/PGPAnalyzerKernelTest.php
@@ -2,9 +2,9 @@
namespace Drupal\Tests\mailhandler_d8\Kernel;
+use Drupal\inmail\DefaultAnalyzerResult;
use Drupal\inmail\Entity\AnalyzerConfig;
use Drupal\inmail\ProcessorResult;
-use Drupal\mailhandler_d8\MailhandlerAnalyzerResult;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
@@ -52,22 +52,20 @@ class PGPAnalyzerKernelTest extends AnalyzerTestBase {
$user->save();
$result = new ProcessorResult();
+ $result->ensureAnalyzerResult(DefaultAnalyzerResult::TOPIC, DefaultAnalyzerResult::createFactory());
$pgp_analyzer = AnalyzerConfig::load('pgp');
/** @var \Drupal\mailhandler_d8\Plugin\inmail\Analyzer\PGPAnalyzer $analyzer */
$analyzer = $this->analyzerManager->createInstance($pgp_analyzer->getPluginId(), $pgp_analyzer->getConfiguration());
$analyzer->analyze($signed_mail, $result);
-
- // Mailhandler analyzer result.
- /** @var \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $mailhandler_result */
- $mailhandler_result = $result->getAnalyzerResult(MailhandlerAnalyzerResult::TOPIC);
+ $result = $result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC);
// @todo: Remove if condition after enabling GnuGP extension in tests.
if (extension_loaded('gnupg')) {
- $this->assertEquals($signed_mail->getSubject(), $mailhandler_result->getSubject());
- $this->assertEquals('Hello world!', $mailhandler_result->getBody());
- $this->assertEquals($user, $mailhandler_result->getUser());
- $this->assertEquals('inline', $mailhandler_result->getPgpType());
+ $this->assertEquals($signed_mail->getSubject(), $result->getSubject());
+ $this->assertEquals('Hello world!', $result->getBody());
+ $this->assertEquals($user, $result->getAccount());
+ $this->assertEquals('inline', $result->getContext('pgp')->getContextValue()['pgp_type']);
}
}
diff --git a/tests/src/Kernel/SenderAnalyzerKernelTest.php b/tests/src/Kernel/SenderAnalyzerKernelTest.php
index e87d8a1..08f55b5 100644
--- a/tests/src/Kernel/SenderAnalyzerKernelTest.php
+++ b/tests/src/Kernel/SenderAnalyzerKernelTest.php
@@ -2,9 +2,9 @@
namespace Drupal\Tests\mailhandler_d8\Kernel;
+use Drupal\inmail\DefaultAnalyzerResult;
use Drupal\inmail\Entity\AnalyzerConfig;
use Drupal\inmail\ProcessorResult;
-use Drupal\mailhandler_d8\MailhandlerAnalyzerResult;
use Drupal\user\Entity\User;
/**
@@ -30,19 +30,17 @@ class SenderAnalyzerKernelTest extends AnalyzerTestBase {
$message = $this->parser->parseMessage($raw_message);
$result = new ProcessorResult();
+ $result->ensureAnalyzerResult(DefaultAnalyzerResult::TOPIC, DefaultAnalyzerResult::createFactory());
$sender_analyzer = AnalyzerConfig::load('sender');
/** @var \Drupal\mailhandler_d8\Plugin\inmail\Analyzer\SenderAnalyzer $analyzer */
$analyzer = $this->analyzerManager->createInstance($sender_analyzer->getPluginId(), $sender_analyzer->getConfiguration());
$analyzer->analyze($message, $result);
+ $result = $result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC);
- // Mailhandler analyzer result.
- /** @var \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $mailhandler_result */
- $mailhandler_result = $result->getAnalyzerResult(MailhandlerAnalyzerResult::TOPIC);
-
- $this->assertEquals('milos@example.com', $mailhandler_result->getSender());
- $this->assertFalse($mailhandler_result->isUserAuthenticated());
- $this->assertNull($mailhandler_result->getUser());
+ $this->assertEquals('milos@example.com', $result->getSender());
+ $this->assertFalse($result->isUserAuthenticated());
+ $this->assertNull($result->getAccount());
// Add a new user.
$user = User::create([
@@ -52,13 +50,13 @@ class SenderAnalyzerKernelTest extends AnalyzerTestBase {
$user->save();
$result = new ProcessorResult();
+ $result->ensureAnalyzerResult(DefaultAnalyzerResult::TOPIC, DefaultAnalyzerResult::createFactory());
$analyzer->analyze($message, $result);
- /** @var \Drupal\mailhandler_d8\MailhandlerAnalyzerResultInterface $mailhandler_result */
- $mailhandler_result = $result->getAnalyzerResult(MailhandlerAnalyzerResult::TOPIC);
+ $result = $result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC);
- $this->assertEquals('milos@example.com', $mailhandler_result->getSender());
- $this->assertTrue($mailhandler_result->isUserAuthenticated());
- $this->assertEquals($user->id(), $mailhandler_result->getUser()->id());
+ $this->assertEquals('milos@example.com', $result->getSender());
+ $this->assertTrue($result->isUserAuthenticated());
+ $this->assertEquals($user->id(), $result->getAccount()->id());
}
}