You are here

UserTest.php in Multiple E-mail Addresses 2.x

File

src/Tests/UserTest.php
View source
<?php

/**
 * @file
 * Definition of Drupal\multiple_email\Tests\UserTest.
 */
namespace Drupal\multiple_email\Tests;

use Drupal\simpletest\WebTestBase;

/**
 * Test basic user registration with 'use multiple email' permissions and its
 * interactions with the module.
 *
 * @group multiple_email
 */
class UserTest extends WebTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array(
    'multiple_email',
  );
  protected function setUp() {

    // Set up basic Drupal install.
    parent::setUp();

    // Create a user allowed to have multiple emails.
    $this->user = $this
      ->drupalCreateUser(array(
      'use multiple emails',
    ));
    $this
      ->drupalLogin($this->user);
  }

  /**
   * Test that the Multiple E-mails user can add a new e-mail address.
   */
  public function testMultipleEmailUser() {

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

    // Test adding a new e-mail address.
    $edit = array(
      'email' => $this
        ->randomMachineName() . '@example.com',
    );
    $this
      ->drupalPostForm('user/' . $this->loggedInUser
      ->id() . '/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
      ->id());
    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
      ->drupalPostForm('user/' . $this->loggedInUser
      ->id() . '/edit/email-addresses/confirm/' . $address->eid . '/resend', array(), t('Resend'));
    $addresses = multiple_email_load_addresses($this->loggedInUser
      ->id());
    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
      ->id() . '/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
      ->drupalPostForm($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
      ->id() . '/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
      ->id() . '/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
      ->drupalPostForm('user/' . $this->loggedInUser
      ->id() . '/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
        ->randomMachineName() . '@example.com',
    );
    $this
      ->drupalPostForm('user/' . $this->loggedInUser
      ->id() . '/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
      ->id() . '/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
      ->drupalPostForm('user/' . $this->loggedInUser
      ->id() . '/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
      ->id() . '/edit');
    $this
      ->assertRaw($this->loggedInUser
      ->getEmail(), t('Primary e-mail address, @email, found before submitting user edit form.', array(
      '@email' => $this->loggedInUser
        ->getEmail(),
    )));

    // @todo change route to match
    // 'user/' . $this->loggedInUser->id() . '/edit/email-addresses'
    $this
      ->assertRaw(t('E-mail addresses are managed on the !multiple_email tab.', array(
      '!multiple_email' => \Drupal::l(t('E-mail addresses'), Url::fromRoute('todo.change.this.route')),
    )));
    $this
      ->drupalPostForm('user/' . $this->loggedInUser
      ->id() . '/edit', array(), t('Save'));
    $this
      ->assertRaw($this->loggedInUser
      ->getEmail(), t('Primary e-mail address, @email, found after user edit form', array(
      '@email' => $this->loggedInUser
        ->getEmail(),
    )));
    $this
      ->assertNoText(t('You must enter an e-mail address.'));
  }
  public function testNoEmailsError() {

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

    // Test adding a new e-mail address.
    $edit = array(
      'email' => $this
        ->randomMachineName() . '@example.com',
    );
    $this
      ->drupalPostForm('user/' . $this->loggedInUser
      ->id() . '/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.'));

    // Lets delete these emails via sql query
    // @TODO Can it be done via UI?
    $deleteQuery = db_delete('multiple_email')
      ->condition('uid', $this->loggedInUser
      ->id())
      ->execute();
    $this
      ->drupalGet('user/' . $this->loggedInUser
      ->id() . '/edit/email-addresses');
    $this
      ->assertText('Unable to find any e-mail addresses.', 'Error message was shown');
    $this
      ->assertNoRaw($this->loggedInUser
      ->getEmail(), 'Primary email was deleted from this view');
    $this
      ->assertNoRaw($edit['email'], 'Secondary email was deleted from this view');
  }

}

Classes

Namesort descending Description
UserTest Test basic user registration with 'use multiple email' permissions and its interactions with the module.