email_example.test

  1. examples
    1. 6 email_example/email_example.test
    2. 7 email_example/email_example.test
    3. 8 email_example/email_example.test

Simpletest case for email_example module.

Verify example module functionality.

Classes

NameDescription
EmailExampleTestCaseFunctionality tests for email example module.

File

email_example/email_example.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Simpletest case for email_example module.
  5. *
  6. * Verify example module functionality.
  7. */
  8. /**
  9. * Functionality tests for email example module.
  10. */
  11. class EmailExampleTestCase extends DrupalWebTestCase {
  12. public static function getInfo() {
  13. return array(
  14. 'name' => 'Email example',
  15. 'description' => 'Verify the email submission using the contact form.',
  16. 'group' => 'Examples',
  17. );
  18. }
  19. function setUp() {
  20. // Enable the email_example module.
  21. parent::setUp('email_example');
  22. }
  23. /**
  24. * Verify the functionality of the example module.
  25. */
  26. function testContactForm() {
  27. // Create and login user.
  28. $account = $this->drupalCreateUser();
  29. $this->drupalLogin($account);
  30. // Set default language for t() translations
  31. $t_options = array(
  32. 'langcode' => language_default()->language,
  33. );
  34. // First try to send to an invalid email address.
  35. $email_options = array(
  36. 'email' => $this->randomName(),
  37. 'message' => $this->randomName(128),
  38. );
  39. $result = $this->drupalPost('example/email_example', $email_options, t('Submit'));
  40. // Verify that email address is invalid and email was not sent.
  41. $this->assertText(t('That e-mail address is not valid.'), t('Options were validated and form submitted.'));
  42. $this->assertTrue(!count($this->drupalGetMails()), t('No email was sent.'));
  43. // Now try with a valid email address.
  44. $email_options['email'] = $this->randomName() . '@' . $this->randomName() . '.drupal';
  45. $result = $this->drupalPost('example/email_example', $email_options, t('Submit'));
  46. // Verify that email address is valid and email was sent.
  47. // $this->assertText(t('Your message has been sent.'), t('Options were validated and form submitted.'));
  48. $this->assertTrue(count($this->drupalGetMails()), t('An email has been sent.'));
  49. // Validate sent email.
  50. $email = $this->drupalGetMails();
  51. // Grab the first entry.
  52. $email = $email[0];
  53. // Verify email recipient.
  54. $this->assertEqual(
  55. $email['to'],
  56. $email_options['email'],
  57. t('Email recipient successfully verified.')
  58. );
  59. // Verify email subject.
  60. $this->assertEqual(
  61. $email['subject'],
  62. t('E-mail sent from @site-name', array('@site-name' => variable_get('site_name', 'Drupal')), $t_options),
  63. t('Email subject successfully verified.')
  64. );
  65. // Verify email body.
  66. $this->assertTrue(
  67. strstr(
  68. $email['body'],
  69. t('@name sent you the following message:', array('@name' => $account->name), $t_options)
  70. ),
  71. t('Email body successfully verified.')
  72. );
  73. // Verify that signature is attached.
  74. $this->assertTrue(
  75. strstr(
  76. $email['body'],
  77. t("--\nMail altered by email_example module.", array(), $t_options)
  78. ),
  79. t('Email signature successfully verified.')
  80. );
  81. }
  82. }
Login or register to post comments