email_example.test 2.8 KB

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