You are here

function ContactAttachContactFormsTestCase::setUp in Contact Attach 7

Sets up a Drupal site for running functional and integration tests.

Generates a random database prefix and installs Drupal with the specified installation profile in DrupalWebTestCase::$profile into the prefixed database. Afterwards, installs any additional modules specified by the test.

After installation all caches are flushed and several configuration values are reset to the values of the parent site executing the test, since the default values may be incompatible with the environment in which tests are being executed.

Parameters

...: List of modules to enable for the duration of the test. This can be either a single array or a variable number of string arguments.

Overrides DrupalWebTestCase::setUp

See also

DrupalWebTestCase::prepareDatabasePrefix()

DrupalWebTestCase::changeDatabasePrefix()

DrupalWebTestCase::prepareEnvironment()

File

./contact_attach.test, line 227
Tests for the Contact Attach module.

Class

ContactAttachContactFormsTestCase
Tests the Contact Attach functionality for site-wide and user contact forms.

Code

function setUp() {
  parent::setUp('contact_attach');

  // Grant all users access to contact forms.
  user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array(
    'access site-wide contact form',
    'access user contact forms',
  ));
  user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, array(
    'access site-wide contact form',
    'access user contact forms',
  ));

  // Create a default role for site administrators.
  $admin_role = new stdClass();
  $admin_role->name = 'administrator';
  $admin_role->weight = 2;
  user_role_save($admin_role);
  user_role_grant_permissions($admin_role->rid, array_keys(module_invoke_all('permission')));

  // Set this as the administrator role.
  variable_set('user_admin_role', $admin_role->rid);

  // Enable contact forms for users by default.
  variable_set('contact_default_status', TRUE);

  // Avoid that the flood control causes the tests to fail.
  variable_set('contact_threshold_limit', 50);

  // Create an admin user for its contact form to be used in the tests.
  $this->admin_user = $this
    ->drupalCreateUser();

  // Create a user that only has the authenticated user role.
  $this->auth_user = $this
    ->drupalCreateUser();

  // drupalCreateUser() does not remove the anonymous role from the new user.
  unset($this->auth_user->roles[DRUPAL_ANONYMOUS_RID]);

  // Create a user with its own role that will later be assigned a 2nd role.
  $this->specific_role_user = $this
    ->drupalCreateUser(array(
    'access site-wide contact form',
    'access user contact forms',
  ));

  // Figure out rid of the role created when specific_role_user was created.
  $specific_user_roles = $this->specific_role_user->roles;
  unset($specific_user_roles[DRUPAL_AUTHENTICATED_RID]);
  reset($specific_user_roles);
  $this->created_role_rid = key($specific_user_roles);

  // Create a second role for the specific role user.
  $this->created_role_2_rid = $this
    ->drupalCreateRole(array(
    'access site-wide contact form',
    'access user contact forms',
  ));

  // Assign the "administrator" role to the admin user.
  db_insert('users_roles')
    ->fields(array(
    'uid' => $this->admin_user->uid,
    'rid' => $admin_role->rid,
  ))
    ->execute();

  // Assign the specific role user the second created role.
  db_insert('users_roles')
    ->fields(array(
    'uid' => $this->specific_role_user->uid,
    'rid' => $this->created_role_2_rid,
  ))
    ->execute();
  $this->specific_role_user->roles[$this->created_role_2_rid] = (string) $this->created_role_2_rid;
}