summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
Diffstat (limited to 'modules')
-rw-r--r--modules/simpletest/simpletest.module13
-rw-r--r--modules/simpletest/tests/mail.test24
-rw-r--r--modules/system/system.api.php8
3 files changed, 42 insertions, 3 deletions
diff --git a/modules/simpletest/simpletest.module b/modules/simpletest/simpletest.module
index b820f5307..980bc1463 100644
--- a/modules/simpletest/simpletest.module
+++ b/modules/simpletest/simpletest.module
@@ -504,3 +504,16 @@ function simpletest_clean_results_table($test_id = NULL) {
}
return 0;
}
+
+/**
+ * Implements hook_mail_alter().
+ *
+ * Aborts sending of messages with ID 'simpletest_cancel_test'.
+ *
+ * @see MailTestCase::testCancelMessage()
+ */
+function simpletest_mail_alter(&$message) {
+ if ($message['id'] == 'simpletest_cancel_test') {
+ $message['send'] = FALSE;
+ }
+}
diff --git a/modules/simpletest/tests/mail.test b/modules/simpletest/tests/mail.test
index a6c7b40e5..09dcde60c 100644
--- a/modules/simpletest/tests/mail.test
+++ b/modules/simpletest/tests/mail.test
@@ -22,7 +22,7 @@ class MailTestCase extends DrupalWebTestCase implements MailSystemInterface {
}
function setUp() {
- parent::setUp();
+ parent::setUp(array('simpletest'));
// Set MailTestCase (i.e. this class) as the SMTP library
variable_set('mail_system', array('default-system' => 'MailTestCase'));
@@ -35,10 +35,28 @@ class MailTestCase extends DrupalWebTestCase implements MailSystemInterface {
global $language;
// Use MailTestCase for sending a message.
- $message = drupal_mail('simpletest', 'mail_test', 'testing@drupal.org', $language);
+ $message = drupal_mail('simpletest', 'mail_test', 'testing@example.com', $language);
// Assert whether the message was sent through the send function.
- $this->assertEqual(self::$sent_message['to'], 'testing@drupal.org', t('Pluggable mail system is extendable.'));
+ $this->assertEqual(self::$sent_message['to'], 'testing@example.com', t('Pluggable mail system is extendable.'));
+ }
+
+ /**
+ * Test that message sending may be canceled.
+ *
+ * @see simpletest_mail_alter()
+ */
+ function testCancelMessage() {
+ global $language;
+
+ // Reset the class variable holding a copy of the last sent message.
+ self::$sent_message = NULL;
+
+ // Send a test message that simpletest_mail_alter should cancel.
+ $message = drupal_mail('simpletest', 'cancel_test', 'cancel@example.com', $language);
+
+ // Assert that the message was not actually sent.
+ $this->assertNull(self::$sent_message, 'Message was canceled.');
}
/**
diff --git a/modules/system/system.api.php b/modules/system/system.api.php
index a2e71d2cc..b2ae3192f 100644
--- a/modules/system/system.api.php
+++ b/modules/system/system.api.php
@@ -1903,11 +1903,19 @@ function hook_image_toolkits() {
* - 'language':
* The language object used to build the message before hook_mail_alter()
* is invoked.
+ * - 'send':
+ * Set to FALSE to abort sending this email message.
*
* @see drupal_mail()
*/
function hook_mail_alter(&$message) {
if ($message['id'] == 'modulename_messagekey') {
+ if (!example_notifications_optin($message['to'], $message['id'])) {
+ // If the recipient has opted to not receive such messages, cancel
+ // sending.
+ $message['send'] = FALSE;
+ return;
+ }
$message['body'][] = "--\nMail sent out from " . variable_get('site_name', t('Drupal'));
}
}