You are here

function MultipleEmailUserTestCase::testMultipleEmailUser in Multiple E-mail Addresses 7

Same name and namespace in other branches
  1. 6 multiple_email.test \MultipleEmailUserTestCase::testMultipleEmailUser()

Test that the Multiple E-mails user can add a new e-mail address.

File

./multiple_email.test, line 38
Tests for the Multiple E-mail module

Class

MultipleEmailUserTestCase
@file Tests for the Multiple E-mail module

Code

function testMultipleEmailUser() {

  // Test that the e-mail address appears on the page
  $this
    ->drupalGet('user/' . $this->loggedInUser->uid . '/edit/email-addresses');
  $this
    ->assertText($this->loggedInUser->mail, t('Registered e-mail successfully added'));

  // Test adding a new e-mail address.
  $edit = array(
    'email' => $this
      ->randomName() . '@example.com',
  );
  $this
    ->drupalPost('user/' . $this->loggedInUser->uid . '/edit/email-addresses', $edit, t('Save'));
  $this
    ->assertRaw(t('The e-mail address %email has been added to your account. Check your e-mail in order to confirm this new address.', array(
    '%email' => $edit['email'],
  )), t('Email successfully added.'));
  $addresses = multiple_email_load_addresses($this->loggedInUser->uid);
  array_shift($addresses);
  foreach ($addresses as $address) {
    if ($address->email == $edit['email']) {
      break;
    }
  }

  // Test that the confirmation e-mail was sent correctly.
  $mail_params = array(
    'to' => $edit['email'],
    'subject' => multiple_email_message_part('subject', 'confirmation', $this->loggedInUser, $address),
  );
  foreach ($mail_params as $name => $value) {
    $this
      ->assertMail($name, $value, t('Confirmation e-mail @name verified.', array(
      '@name' => $name,
    )));
  }

  // Resend the confirmation email
  $this
    ->drupalPost('user/' . $this->loggedInUser->uid . '/edit/email-addresses/confirm/' . $address->eid . '/resend', array(), t('Resend'));
  $addresses = multiple_email_load_addresses($this->loggedInUser->uid);
  array_shift($addresses);
  foreach ($addresses as $address) {
    if ($address->email == $edit['email']) {
      break;
    }
  }

  // Test that the confirmation e-mail was re-sent correctly.
  $mail_params = array(
    'to' => $edit['email'],
    'subject' => multiple_email_message_part('subject', 'confirmation', $this->loggedInUser, $address),
  );
  foreach ($mail_params as $name => $value) {
    $this
      ->assertMail($name, $value, t('Resent confirmation e-mail @name verified.', array(
      '@name' => $name,
    )));
  }

  // Get the path to the confirmation page.
  $path = 'user/' . $this->loggedInUser->uid . '/edit/email-addresses/confirm/' . $address->eid . '/' . $address->confirm_code;

  // Test confirming the new e-mail address.
  $this
    ->drupalGet($path);
  $this
    ->assertRaw(t('The e-mail address %email is awaiting confirmation. You should have received an e-mail at that address with a confirmation code in it. Enter the code below and click confirm.', array(
    '%email' => $address->email,
  )));
  $this
    ->drupalPost($path, array(), t('Confirm'));
  $this
    ->assertRaw(t('The address %email has been confirmed!', array(
    '%email' => $address->email,
  )));

  // Test access to e-mail edit form.
  $this
    ->drupalGet('user/' . $this->loggedInUser->uid . '/edit/email-addresses/edit/' . $address->eid);
  $this
    ->assertText(t('Access denied'));

  // Allow for e-mail edit form.
  variable_set('multiple_email_edit_emails', 1);

  // Test submitting the edit address form with the same address.
  $this
    ->drupalGet('user/' . $this->loggedInUser->uid . '/edit/email-addresses/edit/' . $address->eid);
  $this
    ->assertText(t('E-mail Address'), t('Email edit page loaded'));
  $this
    ->assertRaw($address->email);
  $edit = array(
    'email' => $address->email,
  );
  $this
    ->drupalPost('user/' . $this->loggedInUser->uid . '/edit/email-addresses/edit/' . $address->eid, $edit, t('Save'));
  $this
    ->assertText(t('No change was made to the e-mail address.'));

  // Test submitting the edit address form with a different address.
  $edit = array(
    'email' => $this
      ->randomName() . '@example.com',
  );
  $this
    ->drupalPost('user/' . $this->loggedInUser->uid . '/edit/email-addresses/edit/' . $address->eid, $edit, t('Save'));
  $this
    ->assertRaw(t('The e-mail address %email is awaiting confirmation.', array(
    '%email' => $edit['email'],
  )));

  // Test deleting the address.
  $this
    ->drupalGet('user/' . $this->loggedInUser->uid . '/edit/email-addresses/delete/' . $address->eid);
  $this
    ->assertRaw(t('Are you sure you wish to delete the address %email from your user account?', array(
    '%email' => $edit['email'],
  )));
  $this
    ->drupalPost('user/' . $this->loggedInUser->uid . '/edit/email-addresses/delete/' . $address->eid, array(), t('Delete'));
  $this
    ->assertRaw(t('The e-mail address %email has been removed from your account.', array(
    '%email' => $edit['email'],
  )));

  // Test submitting the primary user edit page.
  $this
    ->drupalGet('user/' . $this->loggedInUser->uid . '/edit');
  $this
    ->assertRaw($this->loggedInUser->mail, t('Primary e-mail address, @email, found before submitting user edit form.', array(
    '@email' => $this->loggedInUser->mail,
  )));
  $this
    ->assertRaw(t('E-mail addresses are managed on the !multiple_email tab.', array(
    '!multiple_email' => l(t('E-mail addresses'), 'user/' . $this->loggedInUser->uid . '/edit/email-addresses'),
  )));
  $this
    ->drupalPost('user/' . $this->loggedInUser->uid . '/edit', array(), t('Save'));
  $this
    ->assertRaw($this->loggedInUser->mail, t('Primary e-mail address, @email, found after user edit form', array(
    '@email' => $this->loggedInUser->mail,
  )));
  $this
    ->assertNoText(t('You must enter an e-mail address.'));
}