diff --git a/src/Driver/Cores/CoreInterface.php b/src/Driver/Cores/CoreInterface.php index b225728..cb9ad4d 100644 --- a/src/Driver/Cores/CoreInterface.php +++ b/src/Driver/Cores/CoreInterface.php @@ -229,4 +229,20 @@ public function state(); */ public function getEditableConfig($name); + /** + * Enable module. + * + * @param string $name + * The name of the module to enable. + */ + public function enableModule(string $name); + + /** + * Disable module. + * + * @param string $name + * The name of the module to disable. + */ + public function disableModule(string $name); + } diff --git a/src/Driver/Cores/Drupal8.php b/src/Driver/Cores/Drupal8.php index 8a039c1..aa3ac93 100644 --- a/src/Driver/Cores/Drupal8.php +++ b/src/Driver/Cores/Drupal8.php @@ -450,4 +450,20 @@ protected function saveFile($source) { return $file; } + /** + * {@inheritdoc} + */ + public function enableModule(string $name) { + $installer = \Drupal::service('module_installer'); + $installer->install([$name]); + } + + /** + * {@inheritdoc} + */ + public function disableModule(string $name) { + $installer = \Drupal::service('module_installer'); + $installer->uninstall([$name]); + } + } diff --git a/src/DrupalExtension/Context/EmailContext.php b/src/DrupalExtension/Context/EmailContext.php index 9dab43a..69b272b 100644 --- a/src/DrupalExtension/Context/EmailContext.php +++ b/src/DrupalExtension/Context/EmailContext.php @@ -17,13 +17,13 @@ class EmailContext extends RawDrupalContext { /** * Current mailsystem settings. * - * @var string + * @var array * Email address. * * @see FeatureContext::beforeScenarioEmail() * @see FeatureContext::afterScenarioEmail() */ - protected $mailsystem = ''; + protected $mailsystem = []; /** * Current contact settings. @@ -49,8 +49,11 @@ class EmailContext extends RawDrupalContext { */ public function assertEmailSentToRecipient($recipient) { $last_mail = $this->getLastEmail(); - if ($last_mail['to'] != $recipient) { - throw new \Exception("Unexpected recipient: " . $last_mail['to']); + $recipients = []; + $recipients = $last_mail['to'] ? array_merge($recipients, [$last_mail['to']]) : $recipients; + $recipients = $last_mail['params']['bcc_mail'] ? array_merge(explode(',', $last_mail['params']['bcc_mail']), $recipients) : $recipients; + if (!in_array($recipient, $recipients)) { + throw new \Exception("Unexpected recipient: " . implode(',', $recipients)); } } @@ -73,9 +76,21 @@ public function assertEmailSentWithProperties(TableNode $table) { * @BeforeScenario @email */ public function beforeScenarioEmail(BeforeScenarioScope $scope) { - $mailsystem = $this->getCore()->getEditableConfig('mailsystem.settings'); - $this->mailsystem = $mailsystem->get('defaults'); - $mailsystem->set('defaults.sender', 'test_mail_collector')->save(); + $module_list = $this->getCore()->getModuleList(); + if (in_array('symfony_mailer', $module_list)) { + $this->getCore()->enableModule('symfony_mailer_test'); + } elseif (in_array('mailsystem', $module_list)) { + $mailsystem = $this->getCore()->getEditableConfig('mailsystem.settings'); + $this->mailsystem = $mailsystem->get('defaults'); + $mailsystem->set('defaults.sender', 'test_mail_collector')->save(); + } else { + $system_mail = $this->getCore()->getEditableConfig('system.mail'); + $this->mailsystem['sender'] = $system_mail->get('interface.default'); + $system_mail + ->set('interface.default', 'test_mail_collector') + ->save(); + } + $this->getCore()->state()->set('system.test_mail_collector', []); } @@ -85,8 +100,18 @@ public function beforeScenarioEmail(BeforeScenarioScope $scope) { * @AfterScenario @email */ public function afterScenarioEmail(AfterScenarioScope $scope) { - $mailsystem = $this->getCore()->getEditableConfig('mailsystem.settings'); - $mailsystem->set('defaults.sender', $this->mailsystem['sender'])->save(); + $module_list = $this->getCore()->getModuleList(); + if (in_array('symfony_mailer', $module_list)) { + $this->getCore()->disableModule('symfony_mailer_test'); + } elseif (in_array('mailsystem', $module_list)) { + $mailsystem = $this->getCore()->getEditableConfig('mailsystem.settings'); + $mailsystem->set('defaults.sender', $this->mailsystem['sender'])->save(); + } else { + $system_mail = $this->getCore()->getEditableConfig('system.mail'); + $system_mail + ->set('interface.default', $this->mailsystem['sender']) + ->save(); + } } /**