You are here

function SimplenewsRolesUserDeleteTestCase::testUserDelete in Simplenews Roles 7

File

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

Class

SimplenewsRolesUserDeleteTestCase

Code

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.'));
}