diff --git a/inmail.install b/inmail.install index cbf3aab..fd62246 100644 --- a/inmail.install +++ b/inmail.install @@ -35,14 +35,12 @@ function inmail_get_plugin_requirements() { foreach ($enabled_instances as $enabled_instance) { $key = $plugin_type . '_' . $enabled_instance->id(); $plugins[$key] = $enabled_instance->checkRequirements(); - // Remove plugins that have no requirements. - // @todo: Think about improving it. - if ($plugins[$key] === TRUE) { - unset($plugins[$key]); - } } } + // Filter plugins with no requirements. + $plugins = array_filter($plugins); + return $plugins; } diff --git a/src/Entity/PluginConfigEntity.php b/src/Entity/PluginConfigEntity.php index 0c6b26c..4f4ba18 100644 --- a/src/Entity/PluginConfigEntity.php +++ b/src/Entity/PluginConfigEntity.php @@ -77,6 +77,7 @@ abstract class PluginConfigEntity extends ConfigEntityBase implements PluginRequ * {@inheritdoc} */ public function checkRequirements() { + $requirements = []; // @todo: Move to the entity level. $entity_type_parameters = explode('_', $this->getEntityTypeId()); $plugin_type = end($entity_type_parameters); @@ -89,7 +90,7 @@ abstract class PluginConfigEntity extends ConfigEntityBase implements PluginRequ return $plugin->checkRequirements(); } - return TRUE; + return $requirements; } } diff --git a/src/MessageProcessor.php b/src/MessageProcessor.php index 4c1d209..2333b94 100644 --- a/src/MessageProcessor.php +++ b/src/MessageProcessor.php @@ -115,7 +115,7 @@ class MessageProcessor implements MessageProcessorInterface { uasort($analyzer_configs, array($this->analyzerStorage->getEntityType()->getClass(), 'sort')); foreach ($analyzer_configs as $analyzer_config) { /** @var \Drupal\inmail\Entity\AnalyzerConfig $analyzer_config */ - if ($analyzer_config->status() && $analyzer_config->checkRequirements() === TRUE) { + if ($analyzer_config->status() && empty($analyzer_config->checkRequirements())) { /** @var \Drupal\inmail\Plugin\inmail\Analyzer\AnalyzerInterface $analyzer */ $analyzer = $this->analyzerManager->createInstance($analyzer_config->getPluginId(), $analyzer_config->getConfiguration()); $analyzer->analyze($message, $result); @@ -136,7 +136,7 @@ class MessageProcessor implements MessageProcessorInterface { // Handle message. foreach ($this->handlerStorage->loadMultiple() as $handler_config) { /** @var \Drupal\inmail\Entity\HandlerConfig $handler_config */ - if ($handler_config->status() && $handler_config->checkRequirements() === TRUE) { + if ($handler_config->status() && empty($handler_config->checkRequirements())) { /** @var \Drupal\inmail\Plugin\inmail\handler\HandlerInterface $handler */ $handler = $this->handlerManager->createInstance($handler_config->getPluginId(), $handler_config->getConfiguration()); $handler->invoke($message, $result); diff --git a/src/Plugin/inmail/Analyzer/AnalyzerBase.php b/src/Plugin/inmail/Analyzer/AnalyzerBase.php index 5e779a6..d18a68d 100644 --- a/src/Plugin/inmail/Analyzer/AnalyzerBase.php +++ b/src/Plugin/inmail/Analyzer/AnalyzerBase.php @@ -74,7 +74,7 @@ abstract class AnalyzerBase extends PluginBase implements AnalyzerInterface { */ public function checkRequirements() { // By default, plugins have no requirements. - return TRUE; + return []; } } diff --git a/src/Plugin/inmail/Deliverer/DelivererBase.php b/src/Plugin/inmail/Deliverer/DelivererBase.php index 504444c..8c9adea 100644 --- a/src/Plugin/inmail/Deliverer/DelivererBase.php +++ b/src/Plugin/inmail/Deliverer/DelivererBase.php @@ -27,7 +27,7 @@ abstract class DelivererBase extends PluginBase implements DelivererInterface { */ public function checkRequirements() { // By default, plugins have no requirements. - return TRUE; + return []; } } diff --git a/src/Plugin/inmail/Handler/HandlerBase.php b/src/Plugin/inmail/Handler/HandlerBase.php index d8683c3..e85ecc1 100644 --- a/src/Plugin/inmail/Handler/HandlerBase.php +++ b/src/Plugin/inmail/Handler/HandlerBase.php @@ -77,7 +77,7 @@ abstract class HandlerBase extends PluginBase implements HandlerInterface { */ public function checkRequirements() { // By default, plugins have no requirements. - return TRUE; + return []; } } diff --git a/src/PluginRequirementsInterface.php b/src/PluginRequirementsInterface.php index 71ec7a7..a4b36d6 100644 --- a/src/PluginRequirementsInterface.php +++ b/src/PluginRequirementsInterface.php @@ -10,8 +10,8 @@ interface PluginRequirementsInterface { /** * Checks requirements of the current plugin. * - * @return bool|string - * Returns the verbose output if requirements are not met. Otherwise, TRUE. + * @return array + * Returns the structured verbose output if requirements are not met. */ public function checkRequirements(); diff --git a/tests/modules/inmail_test/src/Plugin/inmail/Analyzer/MissingRequirementAnalyzer.php b/tests/modules/inmail_test/src/Plugin/inmail/Analyzer/MissingRequirementAnalyzer.php new file mode 100644 index 0000000..d9e02e7 --- /dev/null +++ b/tests/modules/inmail_test/src/Plugin/inmail/Analyzer/MissingRequirementAnalyzer.php @@ -0,0 +1,43 @@ +getAnalyzerResult(DefaultAnalyzerResult::TOPIC); + + // Do the fake body update. This should not be executed because of the + // plugin requirements check. + $default_result->setBody('The body has been updated by MissingRequirementAnalyzer.'); + } + + /** + * {@inheritdoc} + */ + public function checkRequirements() { + return [ + 'title' => $this->t('Missing test extension'), + 'description' => $this->t('Missing Requirement Analyzer cannot be used because of missing test extension'), + 'severity' => REQUIREMENT_ERROR, + ]; + } + +} diff --git a/tests/modules/inmail_test/src/Plugin/inmail/Analyzer/TestAnalyzer.php b/tests/modules/inmail_test/src/Plugin/inmail/Analyzer/TestAnalyzer.php index 08a0c2e..aeb12f3 100644 --- a/tests/modules/inmail_test/src/Plugin/inmail/Analyzer/TestAnalyzer.php +++ b/tests/modules/inmail_test/src/Plugin/inmail/Analyzer/TestAnalyzer.php @@ -40,6 +40,9 @@ class TestAnalyzer extends AnalyzerBase { $demo_user->save(); } $default_result->setAccount($demo_user); + + // Update the body of the default result. + $default_result->setBody($message->getBody()); } /** diff --git a/tests/src/Kernel/ProcessorTest.php b/tests/src/Kernel/ProcessorTest.php index e203425..d6a56e2 100644 --- a/tests/src/Kernel/ProcessorTest.php +++ b/tests/src/Kernel/ProcessorTest.php @@ -47,9 +47,9 @@ class ProcessorTest extends KernelTestBase { } /** - * Tests account switching mechanism. + * Tests the sample message processing. */ - public function testAccountSwitching() { + public function testMessageProcessing() { $raw = << @@ -62,6 +62,10 @@ EOF; $processor = \Drupal::service('inmail.processor'); AnalyzerConfig::create(['id' => 'test_analyzer', 'plugin' => 'test_analyzer'])->save(); + // Add an analyzer with missing requirements. + $missing_requirement_analyzer = AnalyzerConfig::create(['id' => 'missing_requirement_analyzer', 'plugin' => 'missing_requirement_analyzer']); + $missing_requirement_analyzer->save(); + HandlerConfig::create(['id' => 'result_keeper', 'plugin' => 'result_keeper'])->save(); $processor->process($raw, DelivererConfig::create(['id' => 'test'])); @@ -73,6 +77,16 @@ EOF; $this->assertEquals('Demo User', $default_result->getAccount()->getDisplayName()); // Assert the account was switched on handler's level. $this->assertEquals('Demo User', ResultKeeperHandler::getAccountName()); + + // Assert the requirements message. + $this->assertEquals([ + 'title' => t('Missing test extension'), + 'description' => t('Missing Requirement Analyzer cannot be used because of missing test extension'), + 'severity' => REQUIREMENT_ERROR, + ], $missing_requirement_analyzer->checkRequirements()); + // The body message has not changed. It implies MissingRequirementAnalyzer + // did not run. + $this->assertEquals('Hello world!', $default_result->getBody()); } }