Skip to content
16 changes: 16 additions & 0 deletions src/Driver/Cores/CoreInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
16 changes: 16 additions & 0 deletions src/Driver/Cores/Drupal8.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

}
43 changes: 34 additions & 9 deletions src/DrupalExtension/Context/EmailContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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));
}
}

Expand All @@ -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', []);
}

Expand All @@ -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();
}
}

/**
Expand Down