You are here

function SimplenewsRolesUpdateProfileTestCase::testUpdateProfileRole in Simplenews Roles 7

Check that the user has subscribed to appropriate newsletters after the new role assigned or existing unassigned.

Steps performed: 1. Create an admin user, 2. Create a newsletter category, 3. Find $tid based on the category name, 4. Create a newsletter, 5. Create a new role, 6. Add role to the newsletter, 7. Logout as admin user, 8. Create a new user, 9. Load real user object, 5. Check if the new user subscribed to newsletter, it should not be, 6. Assign a "newsletter" role to user, 7. Check again if the user has subscribed, it should be, 8. Unassign role, 9. Check if the user has unsubscribed.

File

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

Class

SimplenewsRolesUpdateProfileTestCase

Code

function testUpdateProfileRole() {

  // 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');

  // Create a new role.
  $role = new stdClass();
  $role->name = 'newsletter';
  $role->weight = 3;
  user_role_save($role);
  $permissions = array(
    'administer newsletters',
    'administer simplenews settings',
    'administer simplenews subscriptions',
  );
  $role = user_role_load_by_name($role->name);
  user_role_grant_permissions($role->rid, $permissions);

  // Add role to the newsletter.
  $edit = array();
  $edit['roles[' . $role->rid . ']'] = $role->rid;
  $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(
    'administer permissions',
    'administer users',
  ));
  $this
    ->drupalLogin($user);

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

  // Let's check if the new user subscribed to "$tid" newsletter.
  // It should not be.
  $this
    ->assertFalse(simplenews_user_is_subscribed($user->mail, $tid), t('User does not have a subscription.'));

  // Assign role.
  $edit = array();
  $edit['roles[' . $role->rid . ']'] = $role->rid;
  $this
    ->drupalPost('user/' . $user->uid . '/edit', $edit, t('Save'));

  // Check if the user has subscribed (should be).
  drupal_static_reset('simplenews_user_is_subscribed');
  $this
    ->assertTrue(simplenews_user_is_subscribed($user->mail, $tid), t('Successfully subscribed.'));

  // Unassign role.
  $edit = array();
  $edit['roles[' . $role->rid . ']'] = FALSE;
  $this
    ->drupalPost('user/' . $user->uid . '/edit', $edit, t('Save'));

  // Check if the user has unsubscribed (should be).
  drupal_static_reset('simplenews_user_is_subscribed');
  $this
    ->assertFalse(simplenews_user_is_subscribed($user->mail, $tid), t('Successfully unsubscribed.'));
}