diff --git a/inmail.install b/inmail.install index bb72393..51c6b94 100644 --- a/inmail.install +++ b/inmail.install @@ -3,6 +3,7 @@ * @file * Implementations of install hooks for the Inmail module. */ +use Drupal\Core\Url; /** * Implements hook_requirements(). @@ -45,7 +46,19 @@ function inmail_get_requirements() { $requirements[$entity_key] = $plugin->checkInstanceRequirements(); // Add title in case requirements are not empty. if (!empty($requirements[$entity_key])) { - $requirements[$entity_key]['title'] = $entity->label() . ' (' . $entity->id() . ')'; + $requirements[$entity_key]['title'] = t('Inmail @plugin_label: @entity_label', [ + '@plugin_label' => $plugin->getPluginDefinition()['label'], + '@entity_label' => $entity->label(), + ] + ); + + $url = Url::fromRoute('entity.inmail_deliverer.edit_form', + ['inmail_deliverer' => $entity->id(),])->toString(); + $requirements[$entity_key]['description'] = t('@old_description Configure the plugin instance.', [ + '@old_description' => $requirements[$entity_key]['description'], + ':url' => $url, + ] + ); } // Check plugin requirements only if it is not already checked. @@ -56,7 +69,7 @@ function inmail_get_requirements() { catch (\Drupal\Component\Plugin\Exception\PluginException $e) { $requirements[$plugin_key] = [ 'title' => t('Missing plugin @id', ['@id' => $plugin_id]), - 'description' => t('Inmail @type plugin @id is missing and cannot be used.', ['@type' => $plugin_type, '@id' => $plugin_id]), + 'description' => t('Inmail @type plugin @id is missing and cannot be used.', ['@type' => $plugin_type, '@id' => $plugin_id,]), 'severity' => REQUIREMENT_ERROR, ]; } diff --git a/src/Plugin/inmail/Deliverer/ImapFetcher.php b/src/Plugin/inmail/Deliverer/ImapFetcher.php index d37f847..bfd7c11 100644 --- a/src/Plugin/inmail/Deliverer/ImapFetcher.php +++ b/src/Plugin/inmail/Deliverer/ImapFetcher.php @@ -121,13 +121,8 @@ class ImapFetcher extends FetcherBase implements ContainerFactoryPluginInterface */ protected function doImap(callable $callback) { // Connect to IMAP with details from configuration. - $mailbox = '{'. $this->configuration['host'] . ':' . $this->configuration['port'] . $this->getFlags() . '}'; + $imap_res = $this->doImapOpen(); - $imap_res = @imap_open( - $mailbox, - $this->configuration['username'], - $this->configuration['password'] - ); $errors = imap_errors(); if (empty($imap_res)) { // @todo Return noisily if misconfigured or imap missing. Possibly stop retrying, https://www.drupal.org/node/2405757 @@ -144,6 +139,30 @@ class ImapFetcher extends FetcherBase implements ContainerFactoryPluginInterface } /** + * Run imap_open() and return result. + * + * @return resource + * The IMAP resource. + */ + public function doImapOpen() { + return @imap_open( + $this->getMailbox(), + $this->getConfiguration()['username'], + $this->getConfiguration()['password'] + ); + } + + /** + * Returns the mailbox. + * + * @return string + * The mailbox for deliverer. + */ + public function getMailbox() { + return '{' . $this->configuration['host'] . ':' . $this->configuration['port'] . $this->getFlags() . '}'; + } + + /** * Return the flags for mailbox. * * @return string @@ -351,6 +370,36 @@ class ImapFetcher extends FetcherBase implements ContainerFactoryPluginInterface /** * {@inheritdoc} */ + public function checkInstanceRequirements() { + $requirements = [ + 'status' => REQUIREMENT_OK, + ]; + if ($this->isAvailable()) { + if (!$this->doImapOpen()) { + $requirements['description'] = $this->t( + 'Deliverer connection failed: @error.', + ['@error' => implode(' ', imap_errors())] + ); + $requirements['severity'] = REQUIREMENT_ERROR; + } + else { + $requirements['description'] = $this->t('Connection successful.'); + } + } + else { + $requirements['description'] = $this->t( + 'The PHP IMAP extension is missing, it must be enabled.', + [':imap' => 'http://www.php.net/imap'] + ); + $requirements['severity'] = REQUIREMENT_WARNING; + } + + return $requirements; + } + + /** + * {@inheritdoc} + */ public function isAvailable() { // Checks if the imap extension is enabled. return function_exists('imap_open'); diff --git a/tests/src/Kernel/ProcessorTest.php b/tests/src/Kernel/ProcessorTest.php index e2aec76..e5eb416 100644 --- a/tests/src/Kernel/ProcessorTest.php +++ b/tests/src/Kernel/ProcessorTest.php @@ -35,7 +35,7 @@ class ProcessorTest extends KernelTestBase { $processor = \Drupal::service('inmail.processor'); $path = drupal_get_path('module', 'inmail_test') . '/eml/malformed/headerbody.eml'; $raw = file_get_contents(DRUPAL_ROOT . '/' . $path); - $processor->process($raw, DelivererConfig::create(array('id' => 'test'))); + $processor->process($raw, DelivererConfig::create(['id' => 'test'])); // Check last DbLog message. $dblog_statement = \Drupal::database()->select('watchdog', 'w') @@ -66,7 +66,7 @@ EOF; // Add an unavailable analyzer. $unavailable_analyzer = AnalyzerConfig::create([ 'id' => 'unavailable_analyzer', - 'label' => 'Unavailable Analyzer', + 'label' => 'Unavailable analyzer instance', 'plugin' => 'unavailable_analyzer', ]); $unavailable_analyzer->save(); @@ -98,6 +98,13 @@ EOF; // The body message has not changed. It implies UnavailableAnalyzer // did not run. $this->assertEquals('Hello world!', $default_result->getBody()); + + module_load_install('inmail'); + $requirements = inmail_get_requirements(); + $this->assertEquals( + $requirements['analyzer_entity_unavailable_analyzer']['title'], + 'Inmail Unavailable Analyzer: Unavailable analyzer instance' + ); } }