You are here

class SimplenewsRolesUserDeleteTestCase in Simplenews Roles 7

Hierarchy

Expanded class hierarchy of SimplenewsRolesUserDeleteTestCase

File

tests/simplenews_roles.test, line 242
SimplenewsRoles test functions.

View source
class SimplenewsRolesUserDeleteTestCase extends SimplenewsRolesTestCase {
  public static function getInfo() {
    return array(
      'name' => t('User Delete'),
      'description' => t('Check that the subscription is deleted after the user is deleted.'),
      'group' => t('Simplenews Roles'),
    );
  }

  /*
   * Check that the subscription is deleted after the user is deleted.
   *
   * @todo - do we need this test case, becase this functionality should be
   * handled by simplenews module and not simplenews_roles module?
   *
   * Steps performed:
   *   1. Set cancel method,
   *   2. Create an admin user,
   *   3. Create a newsletter category,
   *   4. Find $tid based on the category name,
   *   5. Create a newsletter,
   *   6. Add roles to the newsletter,
   *   7. Logout as admin user,
   *   8. Create a new user,
   *   9. Load real user object,
   *   10. Check if the user has subscribed to the appropriate newsletters,
   *   11. Cancel account,
   *   12. Check subscription again.
   */
  function testUserDelete() {

    // Set cancel method.
    variable_set('user_cancel_method', 'user_cancel_delete');

    // Create an admin user.
    $permissions = array(
      'administer content types',
      'administer nodes',
      'access administration pages',
      'administer newsletters',
      'administer simplenews settings',
      'administer simplenews subscriptions',
      'create simplenews content',
      'edit own simplenews content',
    );
    $admin_user = $this
      ->drupalCreateUser($permissions);
    $this
      ->drupalLogin($admin_user);

    // Create a newsletter category.
    $this
      ->drupalGet('admin/config/services/simplenews');
    $this
      ->clickLink(t('Add newsletter category'));
    $name = $this
      ->randomName();
    $edit = array(
      'name' => $name,
      'description' => $this
        ->randomString(20),
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));

    // Find $tid based on the category name.
    drupal_static_reset('simplenews_categories_load_multiple');
    $categories = simplenews_categories_load_multiple();
    $tid = NULL;
    foreach ($categories as $category) {
      if ($category->name == $name) {
        $tid = $category->tid;
      }
    }

    // Create a newsletter.
    $this
      ->drupalGet('node/add/simplenews');
    $this
      ->assertText(t('Newsletter category'));
    $edit = array(
      'title' => $this
        ->randomName(),
      'field_simplenews_term[und]' => $tid,
    );
    $this
      ->drupalPost(NULL, $edit, 'Save');

    // Add roles to the newsletter.
    $roles = array_keys(user_roles(TRUE));
    $edit = array();
    foreach ($roles as $role) {
      $edit['roles[' . $role . ']'] = $role;
    }
    $this
      ->drupalPost('admin/config/services/simplenews/categories/' . $tid . '/edit', $edit, t('Save'));

    // Logout as admin user.
    $this
      ->drupalLogout();

    // Create a new user.
    $user = $this
      ->drupalCreateUser(array(
      'cancel account',
    ));
    $this
      ->drupalLogin($user);

    // Load real user object.
    $user = user_load($user->uid, TRUE);

    // Check if the user has subscribed to the appropriate newsletters.
    $this
      ->assertTrue(simplenews_user_is_subscribed($user->mail, $tid), t('Subscription is successful.'));

    // Then we have to cancel account and check if the subscription is deleted.
    $this
      ->drupalGet('user/' . $user->uid . '/edit');
    $this
      ->drupalPost(NULL, NULL, t('Cancel account'));
    $this
      ->assertText(t('Are you sure you want to cancel your account?'), t('Confirmation form to cancel account displayed.'));

    // Confirm account cancellation.
    $timestamp = time();
    $this
      ->drupalPost(NULL, NULL, t('Cancel account'));
    $this
      ->assertText(t('A confirmation request to cancel your account has been sent to your e-mail address.'), t('Account cancellation request mailed message displayed.'));

    // Confirm account cancellation request.
    $this
      ->drupalGet("user/{$user->uid}/cancel/confirm/{$timestamp}/" . user_pass_rehash($user->pass, $timestamp, $user->login));
    $this
      ->assertFalse(user_load($user->uid, TRUE), t('User is not found in the database.'));

    // Check subscription again.
    drupal_static_reset('simplenews_user_is_subscribed');
    $this
      ->assertFalse(simplenews_user_is_subscribed($user->mail, $tid), t('Subscription is successfully deleted.'));
  }

}

Members