You are here

public function FeedsCSVtoUsersTest::testRoleTargetRevokeRoles in Feeds 7.2

Tests that roles can be revoked and that only allowed roles are revoked.

File

tests/feeds_processor_user.test, line 464
Tests for plugins/FeedsUserProcessor.inc.

Class

FeedsCSVtoUsersTest
Test aggregating a feed as data records.

Code

public function testRoleTargetRevokeRoles() {

  // Create manager role.
  $manager_rid = $this
    ->drupalCreateRole(array(
    'access content',
  ), 'manager');

  // Create editor role.
  $editor_rid = $this
    ->drupalCreateRole(array(
    'access content',
  ), 'editor');

  // Create tester role.
  $tester_rid = $this
    ->drupalCreateRole(array(
    'access content',
  ), 'tester');

  // Set to update existing users.
  $this
    ->setSettings('user_import', 'FeedsUserProcessor', array(
    'update_existing' => FEEDS_UPDATE_EXISTING,
  ));

  // Add mapping to role.
  // The manager role may not be revoked, but the editor role may.
  $this
    ->addMappings('user_import', array(
    4 => array(
      'source' => 'roles',
      'target' => 'roles_list',
      'allowed_roles' => array(
        $manager_rid => FALSE,
        $editor_rid => $editor_rid,
        $tester_rid => $tester_rid,
      ),
    ),
  ));

  // Create account for Morticia with roles "manager" and "editor". In the
  // source only "editor" is specified. Morticia should keep both roles.
  user_save(drupal_anonymous_user(), array(
    'name' => 'Morticia',
    'mail' => 'morticia@example.com',
    'pass' => 'mort',
    'status' => 1,
    'roles' => array(
      $manager_rid => $manager_rid,
      $editor_rid => $editor_rid,
    ),
  ));

  // Create account for Pugsley with roles "manager", "editor" and "tester".
  // Pugsley has no roles in the source so should only keep the "manager"
  // role.
  user_save(drupal_anonymous_user(), array(
    'name' => 'Pugsley',
    'mail' => 'pugsley@example.com',
    'pass' => 'pugs',
    'status' => 1,
    'roles' => array(
      $manager_rid => $manager_rid,
      $editor_rid => $editor_rid,
      $tester_rid => $tester_rid,
    ),
  ));

  // Create account for Gomez and give it the "editor" role. Gomez has roles
  // "tester" and "manager" in the source, so it should lose the "editor" role
  // and gain the "tester" role.
  user_save(drupal_anonymous_user(), array(
    'name' => 'Gomez',
    'mail' => 'gomez@example.com',
    'pass' => 'gome',
    'status' => 1,
    'roles' => array(
      $editor_rid => $editor_rid,
    ),
  ));

  // Import CSV file.
  $this
    ->importFile('user_import', $this
    ->absolutePath() . '/tests/feeds/users_roles.csv');

  // Assert that Morticia kept the manager and editor roles.
  $account = user_load_by_name('Morticia');
  $this
    ->assertTrue(isset($account->roles[$manager_rid]), 'Morticia still has the manager role.');
  $this
    ->assertTrue(isset($account->roles[$editor_rid]), 'Morticia has the editor role.');
  $this
    ->assertEqual(3, count($account->roles), 'Morticia has three roles.');

  // Assert that Pugsley only kept the manager role.
  $account = user_load_by_name('Pugsley');
  $this
    ->assertTrue(isset($account->roles[$manager_rid]), 'Pugsley still has the manager role.');
  $this
    ->assertFalse(isset($account->roles[$editor_rid]), 'Pugsley no longer has the editor role.');
  $this
    ->assertFalse(isset($account->roles[$tester_rid]), 'Pugsley no longer has the tester role.');
  $this
    ->assertEqual(2, count($account->roles), 'Pugsley has two roles.');

  // Assert that Gomez lost the editor role, and gained the tester role.
  $account = user_load_by_name('Gomez');
  $this
    ->assertFalse(isset($account->roles[$editor_rid]), 'Gomez no longer has the editor role.');
  $this
    ->assertTrue(isset($account->roles[$tester_rid]), 'Gomez has the tester role.');
  $this
    ->assertEqual(2, count($account->roles), 'Gomez has two roles.');
}