You are here

public function FeedsCSVtoUsersTest::testAdditionalRolesSetting in Feeds 7.2

Tests if additional roles are assigned when creating or updating users.

File

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

Class

FeedsCSVtoUsersTest
Test aggregating a feed as data records.

Code

public function testAdditionalRolesSetting() {

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

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

  // Set that the "manager" role should be assigned to every user that is
  // imported.
  $this
    ->setSettings('user_import', 'FeedsUserProcessor', array(
    "roles[{$manager_rid}]" => TRUE,
    'update_existing' => FEEDS_UPDATE_EXISTING,
  ));

  // Create account for Gomez and give it the "editor" role. After import
  // Gomez should have the roles "editor" and "manager".
  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 every imported user has gained the "manager" role.
  $user_names = array(
    'Morticia',
    'Fester',
    'Pugsley',
  );
  foreach ($user_names as $user_name) {
    $vars = array(
      '@user' => $user_name,
    );

    // Assert that this user has the "manager" role.
    $account = user_load_by_name($user_name);
    $this
      ->assertTrue(isset($account->roles[$manager_rid]), format_string('@user has the manager role.', $vars));
    $this
      ->assertEqual(2, count($account->roles), format_string('@user has two roles.', $vars));
  }

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