home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (email_example.test)

This example Drupal source code file (email_example.test) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, email, email_options, form, function, has, language, message, php, sent, string, successfully, the, verified

The email_example.test Drupal example source code

<?php

/**
 * @file
 * Simpletest case for email_example module.
 *
 * Verify example module functionality.
 */

/**
 * Functionality tests for email example module.
 */
class EmailExampleTestCase extends DrupalWebTestCase {

  public static function getInfo() {
    return array(
      'name' => 'Email example',
      'description' => 'Verify the email submission using the contact form.',
      'group' => 'Examples',
    );
  }

  function setUp(){
    // Enable the exmaple module.
    parent::setUp('email_example');
  }

  /**
   * Verify the functionality of the example module.
   */
  function testContactForm() {
    // Create and login user.
    $account = $this->drupalCreateUser();
    $this->drupalLogin($account);

    $language = language_default();

    // First try to send to an invalid email address.
    $email_options = array(
      'email'   => $this->randomName(),
      'message' => $this->randomString(128),
    );
    $result = $this->drupalPost('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.'), t('Options were validated and form submitted.'));
    $this->assertTrue(!count($this->drupalGetMails()), t('No email was sent.'));

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

    // Verify that email address is valid and email was sent.
    $this->assertText(t('Your message has been sent.'), t('Options were validated and form submitted.'));
    $this->assertTrue(count($this->drupalGetMails()), t('An email has been sent.'));

    // Validate sent email. Try to find it using the recipient argument.
    $email  = $this->drupalGetMails(array('email' => $email_options['email']));

    // Grab the first email.
    $email = $email[0];

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

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

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

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



Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal email_example.test source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.