You are here

class SimplenewsRolesResyncTestCase in Simplenews Roles 7

Hierarchy

Expanded class hierarchy of SimplenewsRolesResyncTestCase

File

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

View source
class SimplenewsRolesResyncTestCase extends SimplenewsRolesTestCase {
  public static function getInfo() {
    return array(
      'name' => t('Resync'),
      'description' => t('Test to check the general behavior of simplenews_roles module and simplenews_roles_update_subscriptions() method.'),
      'group' => t('Simplenews Roles'),
    );
  }

  /*
   * Test regular subscription is still working and no accidental unsubscribes happen.
   *
   * Steps performed:
   *   1. Create an admin user,
   *   2. Create a test user and subscribe to default newsletter. Subscribe random mail.
   *   3. Update newsletter settings. Run Cron.
   *   4. Check user is still subscribed.
   */
  function testRegular() {

    // Find $tid based on the category name.
    $tid = $this
      ->getRandomNewsletter();

    // 1. 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);
    $this
      ->drupalLogout();

    // 2. Create a test user and subscribe to default newsletter. Subscribe random mail.
    $user_1 = $this
      ->drupalCreateUser(array(
      'administer permissions',
      'administer users',
    ));
    $this
      ->drupalLogin($user_1);
    $edit = array();
    $this
      ->drupalPost('user/' . $user_1->uid . '/edit', $edit, t('Save'));
    simplenews_subscribe_user($user_1->mail, $tid, FALSE, 'test');
    $this
      ->drupalLogout();
    $this
      ->drupalLogin($admin_user);
    $mail_1 = $this
      ->randomEmail();
    simplenews_subscribe_user($mail_1, $tid, FALSE, 'test');

    // Check user is already subscribed.
    drupal_static_reset('simplenews_user_is_subscribed');
    drupal_static_reset('simplenews_categories_load_multiple');
    $this
      ->assertTrue(simplenews_user_is_subscribed($user_1->mail, $tid), t('User (1) has subscribed.'));
    $this
      ->assertTrue(simplenews_user_is_subscribed($mail_1, $tid), t('Mail (1) has subscribed.'));

    // 3. Update newsletter settings. Run Cron.
    $this
      ->drupalGet('admin/config/services/simplenews');
    $this
      ->clickLink(t('edit newsletter category'));

    // Trigger Save
    $edit = array(
      'from_address' => 'example@example.com',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));

    // Make sure to run cron to allow processing everything (accidentally).
    simplenews_roles_cron();

    // 4. Check user is still subscribed.
    drupal_static_reset('simplenews_user_is_subscribed');
    drupal_static_reset('simplenews_categories_load_multiple');
    $this
      ->assertTrue(simplenews_user_is_subscribed($user_1->mail, $tid), t('User (1) has still subscribed.'));
    $this
      ->assertTrue(simplenews_user_is_subscribed($mail_1, $tid), t('Mail (1) has still subscribed.'));
  }

  /*
   * Test to check the general behavior of simplenews_roles module
   * and simplenews_roles_update_subscriptions() method.
   *
   * Steps performed:
   *   1. Create an admin user,
   *   2. Create role "subscriber to list x",
   *   3. Create three users and assign them "subscriber to list x" role,
   *   4. Create newsletter category,
   *   5. Add user 4 to list, add anonymous subscribers also,
   *   6. Setup sync (list => role), trigger sync,
   *   7. Make sure user 4 got removed and users 1..3 are in list,
   *   8. Also anonymous subscribers need to be removed.
   */
  function testResync() {

    // 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 role "subscriber to list x".
    $role = new stdClass();
    $role->name = 'subscriber to list x';
    $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);
    $this
      ->drupalLogout();

    // Create three users and assign them "subscriber to list x" role.
    $user_1 = $this
      ->drupalCreateUser(array(
      'administer permissions',
      'administer users',
    ));
    $this
      ->drupalLogin($user_1);
    $edit = array();
    $edit['roles[' . $role->rid . ']'] = $role->rid;
    $this
      ->drupalPost('user/' . $user_1->uid . '/edit', $edit, t('Save'));
    $this
      ->drupalLogout();
    $user_2 = $this
      ->drupalCreateUser(array(
      'administer permissions',
      'administer users',
    ));
    $this
      ->drupalLogin($user_2);
    $edit = array();
    $edit['roles[' . $role->rid . ']'] = $role->rid;
    $this
      ->drupalPost('user/' . $user_2->uid . '/edit', $edit, t('Save'));
    $this
      ->drupalLogout();
    $user_3 = $this
      ->drupalCreateUser(array(
      'administer permissions',
      'administer users',
    ));
    $this
      ->drupalLogin($user_3);
    $edit = array();
    $edit['roles[' . $role->rid . ']'] = $role->rid;
    $this
      ->drupalPost('user/' . $user_3->uid . '/edit', $edit, t('Save'));
    $this
      ->drupalLogout();
    $user_4 = $this
      ->drupalCreateUser(array(
      'administer permissions',
      'administer users',
    ));

    // Create newsletter category.
    $this
      ->drupalLogin($admin_user);
    $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;
      }
    }

    // Add user 4 to list, add anonymous subscribers also.
    $this
      ->drupalGet('admin/people/simplenews');
    $this
      ->clickLink(t('Mass subscribe'));
    $anonymous = array();
    $anonymous[] = $this
      ->randomName() . '@' . $this
      ->randomName() . '.com';
    $anonymous[] = $this
      ->randomName() . '@' . $this
      ->randomName() . '.com';
    $anonymous[] = $this
      ->randomName() . '@' . $this
      ->randomName() . '.com';
    $edit = array(
      'emails' => $user_4->mail . ',' . rtrim(implode(',', $anonymous), ','),
      "newsletters[{$tid}]" => $tid,
    );
    $this
      ->drupalPost(NULL, $edit, t('Subscribe'));

    // Setup sync (list => role), trigger sync.
    $edit = array();
    $edit['roles[' . $role->rid . ']'] = $role->rid;
    $this
      ->drupalPost('admin/config/services/simplenews/categories/' . $tid . '/edit', $edit, t('Save'));
    drupal_static_reset('simplenews_user_is_subscribed');

    // Make sure user 4 got removed and users 1..3 are in list.
    $this
      ->assertFalse(simplenews_user_is_subscribed($user_4->mail, $tid), t('User (4) has been deleted.'));
    $this
      ->assertTrue(simplenews_user_is_subscribed($user_1->mail, $tid), t('User (1) has subscribed.'));
    $this
      ->assertTrue(simplenews_user_is_subscribed($user_2->mail, $tid), t('User (2) has subscribed.'));
    $this
      ->assertTrue(simplenews_user_is_subscribed($user_3->mail, $tid), t('User (3) has subscribed.'));

    // Also anonymous subscribers need to be removed.
    $this
      ->assertFalse(simplenews_user_is_subscribed($anonymous[0], $tid), t('Anonymous (1) has been deleted.'));
    $this
      ->assertFalse(simplenews_user_is_subscribed($anonymous[1], $tid), t('Anonymous (2) has been deleted.'));
    $this
      ->assertFalse(simplenews_user_is_subscribed($anonymous[2], $tid), t('Anonymous (3) has been deleted.'));
  }

}

Members