diff --git a/inmail_cfortune/src/Plugin/inmail/Analyzer/CfortuneAnalyzer.php b/inmail_cfortune/src/Plugin/inmail/Analyzer/CfortuneAnalyzer.php index 53e011a..961198e 100644 --- a/inmail_cfortune/src/Plugin/inmail/Analyzer/CfortuneAnalyzer.php +++ b/inmail_cfortune/src/Plugin/inmail/Analyzer/CfortuneAnalyzer.php @@ -3,7 +3,7 @@ namespace Drupal\inmail_cfortune\Plugin\inmail\Analyzer; use cfortune\PHPBounceHandler\BounceHandler; -use Drupal\inmail\BounceAnalyzerResult; +use Drupal\inmail\DefaultAnalyzerResult; use Drupal\inmail\DSNStatus; use Drupal\inmail\MIME\MessageInterface; use Drupal\inmail\Plugin\inmail\Analyzer\AnalyzerBase; @@ -33,8 +33,9 @@ class CfortuneAnalyzer extends AnalyzerBase { * {@inheritdoc} */ public function analyze(MessageInterface $message, ProcessorResultInterface $processor_result) { - /** @var \Drupal\inmail\BounceAnalyzerResult $result */ - $result = $processor_result->ensureAnalyzerResult(BounceAnalyzerResult::TOPIC, BounceAnalyzerResult::createFactory()); + /** @var \Drupal\inmail\DefaultAnalyzerResult $result */ + $result = $processor_result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC); + $bounce_data = $result->ensureContext('bounce', 'inmail_bounce'); // All operational code is contained in the BounceHandler class. $handler = new BounceHandler(); @@ -45,11 +46,11 @@ class CfortuneAnalyzer extends AnalyzerBase { // The recipient property possibly contains the target recipient of the // message that bounced. if ($handler->recipient) { - $result->setRecipient(trim($handler->recipient)); + $bounce_data->setRecipient(trim($handler->recipient)); } // The status property possibly contains an RFC 3463 status code. if ($handler->status) { - $result->setStatusCode(DSNStatus::parse($handler->status)); + $bounce_data->setStatusCode(DSNStatus::parse($handler->status)); } } diff --git a/inmail_mailmute/src/Plugin/inmail/Handler/MailmuteHandler.php b/inmail_mailmute/src/Plugin/inmail/Handler/MailmuteHandler.php index 4adccee..824f07e 100644 --- a/inmail_mailmute/src/Plugin/inmail/Handler/MailmuteHandler.php +++ b/inmail_mailmute/src/Plugin/inmail/Handler/MailmuteHandler.php @@ -4,7 +4,7 @@ namespace Drupal\inmail_mailmute\Plugin\inmail\Handler; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; -use Drupal\inmail\BounceAnalyzerResult; +use Drupal\inmail\DefaultAnalyzerResult; use Drupal\inmail\MIME\MessageInterface; use Drupal\inmail\Plugin\inmail\Handler\HandlerBase; use Drupal\inmail\ProcessorResultInterface; @@ -65,21 +65,20 @@ class MailmuteHandler extends HandlerBase implements ContainerFactoryPluginInter * {@inheritdoc} */ public function invoke(MessageInterface $message, ProcessorResultInterface $processor_result) { - $result = $processor_result->getAnalyzerResult(BounceAnalyzerResult::TOPIC); - if (!$result instanceof BounceAnalyzerResult) { - return; - } + /** @var \Drupal\inmail\DefaultAnalyzerResult $result */ + $result = $processor_result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC); + $bounce_data = $result->ensureContext('bounce', 'inmail_bounce'); // Only handle bounces. - if (!$result->isBounce()) { + if (!$bounce_data->isBounce()) { return; } - $status_code = $result->getStatusCode(); + $status_code = $bounce_data->getStatusCode(); $log_context = ['%code' => $status_code->getCode()]; // Only handle bounces with an identifiable recipient. - if (!$address = $result->getRecipient()) { + if (!$address = $bounce_data->getRecipient()) { // @todo Log the message body or place it in a moderation queue: https://www.drupal.org/node/2379879 $processor_result->log('MailmuteHandler', 'Bounce with status %code received but no recipient identified.', $log_context); return; @@ -102,8 +101,8 @@ class MailmuteHandler extends HandlerBase implements ContainerFactoryPluginInter } $state_configuration = array( - 'code' => $result->getStatusCode(), - 'reason' => $result->getReason(), + 'code' => $bounce_data->getStatusCode(), + 'reason' => $bounce_data->getReason(), 'date' => $message->getReceivedDate(), ); diff --git a/inmail_mailmute/tests/src/Kernel/InmailMailmuteTest.php b/inmail_mailmute/tests/src/Kernel/InmailMailmuteTest.php index c4baf13..da0a516 100644 --- a/inmail_mailmute/tests/src/Kernel/InmailMailmuteTest.php +++ b/inmail_mailmute/tests/src/Kernel/InmailMailmuteTest.php @@ -3,7 +3,7 @@ namespace Drupal\Tests\inmail_mailmute\Kernel; use Drupal\Component\Utility\SafeMarkup; -use Drupal\inmail\BounceAnalyzerResult; +use Drupal\inmail\DefaultAnalyzerResult; use Drupal\inmail\DSNStatus; use Drupal\inmail\Entity\DelivererConfig; use Drupal\inmail\Entity\HandlerConfig; @@ -62,7 +62,7 @@ class InmailMailmuteTest extends KernelTestBase { $processor = \Drupal::service('inmail.processor'); // @todo Extend sample message collection https://www.drupal.org/node/2381029 - $cases = array( + $cases = [ // Normal message should not trigger mute. 'normal.eml' => 'send', // "Mailbox full" bounce should trigger counting. @@ -71,7 +71,7 @@ class InmailMailmuteTest extends KernelTestBase { 'nouser.eml' => 'inmail_invalid_address', // "Access denied" bounce should trigger mute. 'accessdenied.eml' => 'inmail_invalid_address', - ); + ]; foreach ($cases as $filename => $expected) { $this->resetUser(); @@ -114,9 +114,19 @@ class InmailMailmuteTest extends KernelTestBase { // Invoke the handler. $processor_result = new ProcessorResult(); - /** @var \Drupal\inmail\BounceAnalyzerResult $result */ - $result = $processor_result->ensureAnalyzerResult(BounceAnalyzerResult::TOPIC, BounceAnalyzerResult::createFactory()); - $result->setStatusCode($status); + /** @var \Drupal\inmail\DefaultAnalyzerResult $result */ + $result = $processor_result->ensureAnalyzerResult(DefaultAnalyzerResult::TOPIC, DefaultAnalyzerResult::createFactory()); + /** @var \Drupal\inmail\BounceDataDefinition $bounce_context */ + if (!$result->hasContext('bounce')) { + return; + } + + $bounce_context = $result->getContext('bounce'); + + /** @var \Drupal\inmail\Plugin\DataType\BounceData $bounce_data */ + $bounce_data = $bounce_context->getContextData(); + + $bounce_data->setStatusCode($status); /** @var \Drupal\inmail\Entity\HandlerConfig $handler_config */ $handler_config = \Drupal::entityManager()->getStorage('inmail_handler')->load('mailmute'); /** @var \Drupal\inmail\Plugin\inmail\Handler\HandlerInterface $handler */ diff --git a/inmail_phpmailerbmh/src/Plugin/inmail/Analyzer/PHPMailerBMHAnalyzer.php b/inmail_phpmailerbmh/src/Plugin/inmail/Analyzer/PHPMailerBMHAnalyzer.php index a5bc681..1d1fbf8 100644 --- a/inmail_phpmailerbmh/src/Plugin/inmail/Analyzer/PHPMailerBMHAnalyzer.php +++ b/inmail_phpmailerbmh/src/Plugin/inmail/Analyzer/PHPMailerBMHAnalyzer.php @@ -2,7 +2,7 @@ namespace Drupal\inmail_phpmailerbmh\Plugin\inmail\Analyzer; -use Drupal\inmail\BounceAnalyzerResult; +use Drupal\inmail\DefaultAnalyzerResult; use Drupal\inmail\DSNStatus; use Drupal\inmail\MIME\DSNEntity; use Drupal\inmail\MIME\MessageInterface; @@ -68,8 +68,9 @@ class PHPMailerBMHAnalyzer extends AnalyzerBase { * {@inheritdoc} */ public function analyze(MessageInterface $message, ProcessorResultInterface $processor_result) { - /** @var \Drupal\inmail\BounceAnalyzerResult $result */ - $result = $processor_result->ensureAnalyzerResult(BounceAnalyzerResult::TOPIC, BounceAnalyzerResult::createFactory()); + /** @var \Drupal\inmail\DefaultAnalyzerResult $result */ + $result = $processor_result->getAnalyzerResult('bounce', 'inmail_bounce'); + $bounce_data = $result->ensureContext('bounce', 'inmail_)bounce'); // The analysis part of the library is in the bmhDSNRules and bmhBodyRules // functions. @@ -91,12 +92,12 @@ class PHPMailerBMHAnalyzer extends AnalyzerBase { // - rule_no: references a single match condition in the code. // - email: the recipient causing the bounce, if identifiable. if (isset($bmh_result['email'])) { - $result->setRecipient($bmh_result['email']); + $bounce_data->setRecipient($bmh_result['email']); } if (isset(static::$rulecatStatusMap[$bmh_result['rule_cat']])) { $code = static::$rulecatStatusMap[$bmh_result['rule_cat']]; if ($code) { - $result->setStatusCode(DSNStatus::parse($code)); + $bounce_data->setStatusCode(DSNStatus::parse($code)); } } } diff --git a/src/AnalyzerResultInterface.php b/src/AnalyzerResultInterface.php index 3adbab2..0eb7a62 100644 --- a/src/AnalyzerResultInterface.php +++ b/src/AnalyzerResultInterface.php @@ -28,4 +28,6 @@ interface AnalyzerResultInterface { */ public function label(); + + } diff --git a/src/BounceDataDefinition.php b/src/BounceDataDefinition.php new file mode 100644 index 0000000..250bdcc --- /dev/null +++ b/src/BounceDataDefinition.php @@ -0,0 +1,29 @@ + DataDefinition::create('string'), + 'recipient' => DataDefinition::create('string'), + 'reason' => DataDefinition::create('string'), + ]; + } +} diff --git a/src/DefaultAnalyzerResult.php b/src/DefaultAnalyzerResult.php index 2b70117..d0581b8 100644 --- a/src/DefaultAnalyzerResult.php +++ b/src/DefaultAnalyzerResult.php @@ -2,6 +2,8 @@ namespace Drupal\inmail; +use Drupal\Core\Plugin\Context\Context; +use Drupal\Core\Plugin\Context\ContextDefinition; use Drupal\Core\Plugin\Context\ContextInterface; use Drupal\user\UserInterface; @@ -290,4 +292,31 @@ class DefaultAnalyzerResult implements AnalyzerResultInterface { return $filtered_contexts; } + /** + * Returns the bounce data. + * + * @param $name + * The name of context. + * + * @param $data_type + * The data type of context. + * + * @return \Drupal\inmail\Plugin\DataType\BounceData + * The bonce data of $name context. + */ + public function ensureContext($name, $data_type) { + $data = NULL; + if ($this->hasContext($name)) { + $bounce_data = $this->getContext($name)->getContextData(); + } + else { + /** @var \Drupal\Core\TypedData\TypedDataManagerInterface $typed_data_manager */ + $typed_data_manager =\Drupal::service('typed_data_manager'); + $bounce_data = $typed_data_manager->create(BounceDataDefinition::create($data_type)); + $bounce_context = new Context(new ContextDefinition($data_type), $bounce_data); + $this->setContext($name, $bounce_context); + } + + return $bounce_data; + } } diff --git a/src/BounceAnalyzerResult.php b/src/Plugin/DataType/BounceData.php similarity index 52% rename from src/BounceAnalyzerResult.php rename to src/Plugin/DataType/BounceData.php index 3e1dfeb..3809f32 100644 --- a/src/BounceAnalyzerResult.php +++ b/src/Plugin/DataType/BounceData.php @@ -1,59 +1,17 @@ recipient)) { - $this->recipient = $recipient; + if (!$this->getRecipient()) { + $this->set('recipient',$recipient); } } @@ -74,16 +32,16 @@ class BounceAnalyzerResult implements AnalyzerResultInterface { * A status code. */ public function setStatusCode(DSNStatus $code) { - if (!isset($this->statusCode)) { - $this->statusCode = $code; + if (!$this->getStatusCode()) { + $this->set('status_code', $code->getCode()); return; } // If subject and detail are 0 (like X.0.0), allow overriding those. - $current_code = $this->statusCode; + $current_code = $this->getStatusCode(); if ($current_code->getSubject() == 0 && $current_code->getDetail() == 0) { $new_code = new DSNStatus($current_code->getClass(), $code->getSubject(), $code->getDetail()); - $this->statusCode = $new_code; + $this->set('status_code',$new_code->getCode()); } } @@ -91,11 +49,11 @@ class BounceAnalyzerResult implements AnalyzerResultInterface { * Report the reason for a bounce message. * * @param string $reason - * Human-readable information in English explaning why the bounce happened. + * Human-readable information in English explaining why the bounce happened. */ public function setReason($reason) { - if (!isset($this->reason)) { - $this->reason = $reason; + if (!$this->getReason()) { + $this->set('reason',$reason); } } @@ -107,17 +65,21 @@ class BounceAnalyzerResult implements AnalyzerResultInterface { * reported. */ public function getRecipient() { - return $this->recipient; + return $this->get('recipient')->getValue(); } /** * Returns the reported status code of a bounce message. * - * @return \Drupal\inmail\DSNStatus + * @return \Drupal\inmail\DSNStatus|null * The status code, or NULL if it has not been reported. */ public function getStatusCode() { - return $this->statusCode; + if ($code = $this->get('status_code')->getValue()) { + return DSNStatus::parse($this->get('status_code')->getValue()); + } + + return NULL; } /** @@ -127,7 +89,7 @@ class BounceAnalyzerResult implements AnalyzerResultInterface { * The reason message, in English, or NULL if it has not been reported. */ public function getReason() { - return $this->reason; + return $this->get('reason')->getValue(); } /** @@ -154,24 +116,4 @@ class BounceAnalyzerResult implements AnalyzerResultInterface { return TRUE; } - /** - * {@inheritdoc} - */ - public function summarize() { - $summary = array(); - if ($this->getRecipient()) { - $summary['recipient'] = $this->getRecipient(); - } - if ($this->getStatusCode() && $this->getStatusCode()->getCode()) { - $summary['code'] = $this->getStatusCode()->getCode(); - } - return $summary; - } - - /** - * {@inheritdoc} - */ - public function label() { - return t('Bounce'); - } } diff --git a/src/Plugin/inmail/Analyzer/StandardDSNAnalyzer.php b/src/Plugin/inmail/Analyzer/StandardDSNAnalyzer.php index 58662eb..a55bd72 100644 --- a/src/Plugin/inmail/Analyzer/StandardDSNAnalyzer.php +++ b/src/Plugin/inmail/Analyzer/StandardDSNAnalyzer.php @@ -2,7 +2,7 @@ namespace Drupal\inmail\Plugin\inmail\Analyzer; -use Drupal\inmail\BounceAnalyzerResult; +use Drupal\inmail\DefaultAnalyzerResult; use Drupal\inmail\DSNStatus; use Drupal\inmail\MIME\DSNEntity; use Drupal\inmail\MIME\MessageInterface; @@ -40,8 +40,9 @@ class StandardDSNAnalyzer extends AnalyzerBase { return; } - /** @var \Drupal\inmail\BounceAnalyzerResult $result */ - $result = $processor_result->ensureAnalyzerResult(BounceAnalyzerResult::TOPIC, BounceAnalyzerResult::createFactory()); + /** @var \Drupal\inmail\DefaultAnalyzerResult $result */ + $result = $processor_result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC); + $bounce_data = $result->ensureContext('bounce', 'inmail_bounce'); // @todo Store date for bounces https://www.drupal.org/node/2379923 // Iterate over per-recipient field groups in the DSN. @@ -50,17 +51,16 @@ class StandardDSNAnalyzer extends AnalyzerBase { // Parse the 'Status:' field, having the format X.XXX.XXX. $subcodes = explode('.', $fields->getFieldBody('Status')); if (count($subcodes) == 3) { - $result->setStatusCode(new DSNStatus($subcodes[0], $subcodes[1], $subcodes[2])); + $bounce_data->setStatusCode(new DSNStatus($subcodes[0], $subcodes[1], $subcodes[2])); } // Extract address from the 'Final-Recipient:' field, which has the format // "type; address". $field_parts = preg_split('/;\s*/', $fields->getFieldBody('Final-Recipient')); if (count($field_parts) == 2) { - $result->setRecipient($field_parts[1]); + $bounce_data->setRecipient($field_parts[1]); } } - } } diff --git a/src/Plugin/inmail/Analyzer/StandardDSNReasonAnalyzer.php b/src/Plugin/inmail/Analyzer/StandardDSNReasonAnalyzer.php index 304fd57..01ac448 100644 --- a/src/Plugin/inmail/Analyzer/StandardDSNReasonAnalyzer.php +++ b/src/Plugin/inmail/Analyzer/StandardDSNReasonAnalyzer.php @@ -2,7 +2,7 @@ namespace Drupal\inmail\Plugin\inmail\Analyzer; -use Drupal\inmail\BounceAnalyzerResult; +use Drupal\inmail\DefaultAnalyzerResult; use Drupal\inmail\MIME\DSNEntity; use Drupal\inmail\MIME\MessageInterface; use Drupal\inmail\ProcessorResultInterface; @@ -30,10 +30,11 @@ class StandardDSNReasonAnalyzer extends AnalyzerBase { return; } - /** @var \Drupal\inmail\BounceAnalyzerResult $result */ - $result = $processor_result->ensureAnalyzerResult(BounceAnalyzerResult::TOPIC, BounceAnalyzerResult::createFactory()); + /** @var \Drupal\inmail\DefaultAnalyzerResult $result */ + $result = $processor_result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC); + $bounce_data = $result->ensureContext('bounce', 'inmail_bounce'); // Save the human-readable bounce reason. - $result->setReason(trim($message->getHumanPart()->getDecodedBody())); + $bounce_data->setReason(trim($message->getHumanPart()->getDecodedBody())); } } diff --git a/src/Plugin/inmail/Analyzer/VERPAnalyzer.php b/src/Plugin/inmail/Analyzer/VERPAnalyzer.php index c52ad5d..02fe4ac 100644 --- a/src/Plugin/inmail/Analyzer/VERPAnalyzer.php +++ b/src/Plugin/inmail/Analyzer/VERPAnalyzer.php @@ -2,7 +2,7 @@ namespace Drupal\inmail\Plugin\inmail\Analyzer; -use Drupal\inmail\BounceAnalyzerResult; +use Drupal\inmail\DefaultAnalyzerResult; use Drupal\inmail\MIME\MessageInterface; use Drupal\inmail\ProcessorResultInterface; @@ -48,8 +48,9 @@ class VerpAnalyzer extends AnalyzerBase { * {@inheritdoc} */ public function analyze(MessageInterface $message, ProcessorResultInterface $processor_result) { - /** @var \Drupal\inmail\BounceAnalyzerResult $result */ - $result = $processor_result->ensureAnalyzerResult(BounceAnalyzerResult::TOPIC, BounceAnalyzerResult::createFactory()); + /** @var \Drupal\inmail\DefaultAnalyzerResult $result */ + $result = $processor_result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC); + $bounce_data = $result->ensureContext('bounce', 'inmail_bounce'); // Split the site address to facilitate matching. $return_path = \Drupal::config('inmail.settings')->get('return_path') ?: \Drupal::config('system.site')->get('mail'); @@ -65,7 +66,7 @@ class VerpAnalyzer extends AnalyzerBase { // $matches. if (preg_match(':^' . $return_path_split[0] . '\+(.*)=(.*)@' . $return_path_split[1] . '$:', $message->getTo(), $matches)) { // Report the recipient address (alice@example.com). - $result->setRecipient($matches[1] . '@' . $matches[2]); + $bounce_data->setRecipient($matches[1] . '@' . $matches[2]); } } diff --git a/src/Plugin/inmail/Handler/ModeratorForwardHandler.php b/src/Plugin/inmail/Handler/ModeratorForwardHandler.php index 866f47b..d83234e 100644 --- a/src/Plugin/inmail/Handler/ModeratorForwardHandler.php +++ b/src/Plugin/inmail/Handler/ModeratorForwardHandler.php @@ -5,7 +5,7 @@ namespace Drupal\inmail\Plugin\inmail\Handler; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Mail\MailManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; -use Drupal\inmail\BounceAnalyzerResult; +use Drupal\inmail\DefaultAnalyzerResult; use Drupal\inmail\MIME\MessageInterface; use Drupal\inmail\ProcessorResultInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -71,18 +71,19 @@ class ModeratorForwardHandler extends HandlerBase implements ContainerFactoryPlu return; } - /** @var \Drupal\inmail\BounceAnalyzerResult $result */ - $result = $processor_result->getAnalyzerResult(BounceAnalyzerResult::TOPIC); + /** @var \Drupal\inmail\DefaultAnalyzerResult $result */ + $result = $processor_result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC); + $bounce_data = $result->ensureContext('bounce', 'inmail_bounce'); // Cancel if the message is successfully classified. - if ($result->isBounce()) { + if ($bounce_data->isBounce()) { return; } // Cancel and make noise if it was the moderator address that bounced! // This is for the off chance that we identified the intended recipient // but not a bounce status code. - if ($result->getRecipient() == $moderator) { + if ($bounce_data->getRecipient() == $moderator) { $processor_result->log('ModeratorForwardHandler', 'Moderator %address is bouncing.', array('%address' => $moderator)); return; } diff --git a/src/ProcessorResult.php b/src/ProcessorResult.php index bce95e5..1caf83b 100644 --- a/src/ProcessorResult.php +++ b/src/ProcessorResult.php @@ -32,6 +32,11 @@ class ProcessorResult implements ProcessorResultInterface { */ protected $log = array(); + public function __construct() { + $this->analyzerResults[DefaultAnalyzerResult::TOPIC] = new DefaultAnalyzerResult(); + } + + /** * {@inheritdoc} */ diff --git a/src/ProcessorResultInterface.php b/src/ProcessorResultInterface.php index 8ce6c06..2db7166 100644 --- a/src/ProcessorResultInterface.php +++ b/src/ProcessorResultInterface.php @@ -67,6 +67,8 @@ interface ProcessorResultInterface { */ public function getAnalyzerResults(); + + /** * Add a log message to the processing logger. * diff --git a/tests/modules/inmail_test/src/Plugin/inmail/Analyzer/UnavailableAnalyzer.php b/tests/modules/inmail_test/src/Plugin/inmail/Analyzer/UnavailableAnalyzer.php index 4a76d8b..c526788 100644 --- a/tests/modules/inmail_test/src/Plugin/inmail/Analyzer/UnavailableAnalyzer.php +++ b/tests/modules/inmail_test/src/Plugin/inmail/Analyzer/UnavailableAnalyzer.php @@ -23,7 +23,6 @@ class UnavailableAnalyzer extends AnalyzerBase { public function analyze(MessageInterface $message, ProcessorResultInterface $processor_result) { /** @var \Drupal\inmail\DefaultAnalyzerResult $default_result */ $default_result = $processor_result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC); - // Do the fake body update. This should not be executed as we only execute // available analyzers. $default_result->setBody('The body has been updated by UnavailableAnalyzer.'); diff --git a/tests/src/Kernel/AnalyzerTest.php b/tests/src/Kernel/AnalyzerTest.php index 24ac543..19b86ba 100644 --- a/tests/src/Kernel/AnalyzerTest.php +++ b/tests/src/Kernel/AnalyzerTest.php @@ -4,14 +4,12 @@ namespace Drupal\Tests\inmail\Kernel; use Drupal\Core\Plugin\Context\Context; use Drupal\Core\Plugin\Context\ContextDefinition; -use Drupal\inmail\BounceAnalyzerResult; use Drupal\inmail\Entity\AnalyzerConfig; use Drupal\inmail\Entity\DelivererConfig; use Drupal\inmail\Entity\HandlerConfig; use Drupal\inmail\DefaultAnalyzerResult; use Drupal\inmail_test\Plugin\inmail\Handler\ResultKeeperHandler; use Drupal\KernelTests\KernelTestBase; -use Drupal\user\Entity\User; /** * Tests analyzers. @@ -83,12 +81,15 @@ EOF; $processor->process($raw, DelivererConfig::create(array('id' => 'test'))); $processor_result = ResultKeeperHandler::getResult(); - /** @var \Drupal\inmail\BounceAnalyzerResult $result */ - $result = $processor_result->getAnalyzerResult(BounceAnalyzerResult::TOPIC); + /** @var \Drupal\inmail\DefaultAnalyzerResult $result */ + $result = $processor_result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC); /** @var \Drupal\inmail\DefaultAnalyzerResult $default_result */ $default_result = $processor_result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC); + $bounce_context = $result->getContext('bounce'); + /** @var \Drupal\inmail\Plugin\DataType\BounceData $bounce_data */ + $bounce_data = $bounce_context->getContextData(); - $this->assertEqual($result->getRecipient(), 'verp-parsed@example.org'); + $this->assertEqual($bounce_data->getRecipient(), 'verp-parsed@example.org'); $this->assertEquals('Demo User', $default_result->getAccount()->getDisplayName()); $this->assertEquals('Sample context value', $default_result->getContext('test')->getContextValue()); @@ -108,11 +109,4 @@ EOF; } } - /** - * Tests BounceAnalyzerResult by instantiation of the object and calls a method. - */ - public function testBounce() { - $bouncetest = new BounceAnalyzerResult(); - $bouncetest->summarize(); - } } diff --git a/tests/src/Kernel/StandardDSNAnalyzerTest.php b/tests/src/Kernel/StandardDSNAnalyzerTest.php new file mode 100644 index 0000000..ce3dc67 --- /dev/null +++ b/tests/src/Kernel/StandardDSNAnalyzerTest.php @@ -0,0 +1,102 @@ +doTestAnalyze($args[0],$args[1],$args[2]); +// } +// } + + public function testAnalyze($filename, $expected_code, $expected_recipient) { + $message = (new Parser(new LoggerChannel('test')))->parseMessage($this->getRaw($filename)); + // Run the analyzer. + $analyzer = new StandardDSNAnalyzer(array(), $this->randomMachineName(), array()); + $processor_result = new ProcessorResult(); + $analyzer->analyze($message, $processor_result); + /** @var \Drupal\inmail\DefaultAnalyzerResult $result */ + $result = $processor_result->ensureAnalyzerResult(DefaultAnalyzerResult::TOPIC, DefaultAnalyzerResult::createFactory()); + $bounce_data = $result->ensureContext('bounce', 'inmail_bounce'); + /** @var \Drupal\inmail\BounceDataDefinition $bounce_context */ + $bounce_context = $result->getContext('bounce'); + + // No result object if nothing to report. + if (!isset($expected_code) && !isset($expected_recipient)) { + $this->assertFalse(is_null($bounce_context)); + } + + // Test the reported code. + if (isset($expected_code)) { + $this->assertEquals($expected_code, $bounce_data->getStatusCode()->getCode()); + } + + // Test the reported target recipient. + if (isset($expected_recipient)) { + $this->assertEquals($expected_recipient, $bounce_data->getRecipient()); + } + } + + /** + * Provides expected analysis results for test message files. + */ + public function provideExpectedResults() { + return [ + ['accessdenied.eml', '5.0.0', 'user@example.org'], + ['full.eml', '4.2.2', 'user@example.org'], + ['normal.eml', NULL, NULL], + ['nouser.eml', '5.1.1', 'user@example.org'], + ]; + } + +} diff --git a/tests/src/Unit/Plugin/inmail/Analyzer/StandardDSNReasonAnalyzerTest.php b/tests/src/Kernel/StandardDSNReasonAnalyzerTest.php similarity index 69% rename from tests/src/Unit/Plugin/inmail/Analyzer/StandardDSNReasonAnalyzerTest.php rename to tests/src/Kernel/StandardDSNReasonAnalyzerTest.php index 1ddd001..c9402ee 100644 --- a/tests/src/Unit/Plugin/inmail/Analyzer/StandardDSNReasonAnalyzerTest.php +++ b/tests/src/Kernel/StandardDSNReasonAnalyzerTest.php @@ -1,13 +1,13 @@ randomMachineName(), array()); $processor_result = new ProcessorResult(); $analyzer->analyze($message, $processor_result); - /** @var \Drupal\inmail\BounceAnalyzerResult $result */ - $result = $processor_result->getAnalyzerResult(BounceAnalyzerResult::TOPIC); + /** @var \Drupal\inmail\DefaultAnalyzerResult $result */ + $result = $processor_result->getAnalyzerResult(DefaultAnalyzerResult::TOPIC); + $bounce_data = $result->ensureContext('bounce', 'inmail_bounce'); + + $bounce_context = $result->getContext('bounce'); + if (isset($expected_reason)) { - $this->assertEquals($expected_reason, $result->getReason()); + $this->assertEquals($expected_reason, $bounce_data->getReason()); } else { - $this->assertNull($result); + $this->assertFalse(is_null($bounce_context)); + } } @@ -85,4 +92,18 @@ because: ]; } + /** + * Returns the raw contents of a given test message file. + * + * @param string $filename + * The name of the file. + * + * @return string + * The message content. + */ + protected function getRaw($filename) { + $path = __DIR__ . '/../../modules/inmail_test/eml/' . $filename; + return file_get_contents($path); + } + } diff --git a/tests/src/Kernel/VerpTest.php b/tests/src/Kernel/VerpTest.php index 9ca5035..554b767 100644 --- a/tests/src/Kernel/VerpTest.php +++ b/tests/src/Kernel/VerpTest.php @@ -2,7 +2,7 @@ namespace Drupal\Tests\inmail\Kernel; -use Drupal\inmail\BounceAnalyzerResult; +use Drupal\inmail\DefaultAnalyzerResult; use Drupal\inmail\Entity\AnalyzerConfig; use Drupal\inmail\Entity\DelivererConfig; use Drupal\inmail\Entity\HandlerConfig; @@ -61,9 +61,16 @@ class VerpTest extends KernelTestBase { $processor = \Drupal::service('inmail.processor'); $processor->process($raw, DelivererConfig::create(array('id' => 'test'))); - /** @var \Drupal\inmail\BounceAnalyzerResult $result */ - $result = ResultKeeperHandler::getResult()->getAnalyzerResult(BounceAnalyzerResult::TOPIC); - $parsed_recipient = $result->getRecipient(); + /** @var \Drupal\inmail\DefaultAnalyzerResult $result */ + $result = ResultKeeperHandler::getResult()->getAnalyzerResult(DefaultAnalyzerResult::TOPIC); + if (!$result->hasContext('bounce')) { + return; + } + $bounce_context = $result->getContext('bounce'); + + /** @var \Drupal\inmail\Plugin\DataType\BounceData $bounce_data */ + $bounce_data = $bounce_context->getContextData(); + $parsed_recipient = $bounce_data->getRecipient(); $this->assertEqual($parsed_recipient, $recipient); // VERP should be skipped for messages with Cc recipients. diff --git a/tests/src/Unit/Plugin/inmail/Analyzer/StandardDSNAnalyzerTest.php b/tests/src/Unit/Plugin/inmail/Analyzer/StandardDSNAnalyzerTest.php deleted file mode 100644 index 863ce6b..0000000 --- a/tests/src/Unit/Plugin/inmail/Analyzer/StandardDSNAnalyzerTest.php +++ /dev/null @@ -1,66 +0,0 @@ -parseMessage($this->getRaw($filename)); - - // Run the analyzer. - $analyzer = new StandardDSNAnalyzer(array(), $this->randomMachineName(), array()); - $processor_result = new ProcessorResult(); - $analyzer->analyze($message, $processor_result); - /** @var \Drupal\inmail\BounceAnalyzerResult $result */ - $result = $processor_result->getAnalyzerResult(BounceAnalyzerResult::TOPIC); - - // No result object if nothing to report. - if (!isset($expected_code) && !isset($expected_recipient)) { - $this->assertNull($result); - } - - // Test the reported code. - if (isset($expected_code)) { - $this->assertEquals($expected_code, $result->getStatusCode()->getCode()); - } - - // Test the reported target recipient. - if (isset($expected_recipient)) { - $this->assertEquals($expected_recipient, $result->getRecipient()); - } - } - - /** - * Provides expected analysis results for test message files. - */ - public function provideExpectedResults() { - return [ - ['accessdenied.eml', '5.0.0', 'user@example.org'], - ['full.eml', '4.2.2', 'user@example.org'], - ['normal.eml', NULL, NULL], - ['nouser.eml', '5.1.1', 'user@example.org'], - ]; - } - -}