1 email_example.test public EmailExampleTestCase::testContactForm()

Verify the functionality of the example module.

File

modules/examples/email_example/tests/email_example.test, line 27
Simpletest case for email_example module.

Class

EmailExampleTestCase
Functionality tests for email example module.

Code

public function testContactForm() {
  // Create and login user.
  $account = $this->backdropCreateUser();
  $this->backdropLogin($account);

  // Set default language for t() translations.
  $t_options = array(
    'langcode' => language_default()->langcode,
  );

  // First try to send to an invalid email address.
  $email_options = array(
    'email' => $this->randomName(),
    'message' => $this->randomName(128),
  );
  $result = $this->backdropPost('example/email_example', $email_options, t('Submit'));

  // Verify that email address is invalid and email was not sent.
  $this->assertText(t('That e-mail address is not valid.'), 'Options were validated and form submitted.');
  $this->assertTrue(!count($this->backdropGetMails()), 'No email was sent.');

  // Now try with a valid email address.
  $email_options['email'] = $this->randomName() . '@' . $this->randomName() . '.backdrop';
  $result = $this->backdropPost('example/email_example', $email_options, t('Submit'));

  // Verify that email address is valid and email was sent.
  $this->assertTrue(count($this->backdropGetMails()), 'An email has been sent.');

  // Validate sent email.
  $email = $this->backdropGetMails();
  // Grab the first entry.
  $email = $email[0];

  // Verify email recipient.
  $this->assertEqual(
  $email['to'], 
  $email_options['email'], 
  'Email recipient successfully verified.'
  );

  // Verify email subject.
  $this->assertEqual(
  $email['subject'], 
  t('E-mail sent from @site-name', array('@site-name' => config_get('system.core', 'site_name')), $t_options), 
  'Email subject successfully verified.'
  );

  // Verify email body.
  $this->assertTrue(
  strstr(
  $email['body'], 
  t('@name sent you the following message:', array('@name' => $account->name), $t_options)
  ), 
  'Email body successfully verified.'
  );

  // Verify that signature is attached.
  $this->assertTrue(
  strstr(
  $email['body'], 
  t("--\nMail altered by email_example module.", array(), $t_options)
  ), 
  'Email signature successfully verified.'
  );
}