You are here

public function UserRoleTest::testWithoutRoleCreation in Feeds 8.3

Tests mapping to role without automatically creating new roles.

File

tests/src/Kernel/Feeds/Target/UserRoleTest.php, line 131

Class

UserRoleTest
@coversDefaultClass \Drupal\feeds\Feeds\Target\UserRole @group feeds

Namespace

Drupal\Tests\feeds\Kernel\Feeds\Target

Code

public function testWithoutRoleCreation() {

  // Create the manager role.
  $this
    ->createRole([], 'manager');

  // Add mapping to role.
  $this->feedType
    ->addMapping([
    'target' => 'roles',
    'map' => [
      'target_id' => 'role_ids',
    ],
  ]);
  $this->feedType
    ->save();

  // Import.
  $feed = $this
    ->createFeed($this->feedType
    ->id(), [
    'source' => $this
      ->resourcesPath() . '/csv/users_roles.csv',
  ]);
  $feed
    ->import();

  // Assert that Morticia did not get any roles.
  $account = user_load_by_name('Morticia');
  $this
    ->assertNotHasRole($account, 'editor', 'Morticia does not have the editor role.');
  $this
    ->assertRoleCount(0, $account, 'Morticia has no special roles.');

  // Assert that Fester got the manager role and one role in total.
  $account = user_load_by_name('Fester');
  $this
    ->assertHasRole($account, 'manager', 'Fester has the manager role.');
  $this
    ->assertRoleCount(1, $account, 'Fester has one role.');

  // Assert that Gomez got the manager role but not the tester role, since
  // that role doesn't exist on the system.
  $account = user_load_by_name('Gomez');
  $this
    ->assertHasRole($account, 'manager', 'Gomez has the manager role.');
  $this
    ->assertNotHasRole($account, 'tester', 'Gomez does not have the tester role.');
  $this
    ->assertRoleCount(1, $account, 'Gomez has one role.');

  // Assert that Pugsley has no roles.
  $account = user_load_by_name('Pugsley');
  $this
    ->assertRoleCount(0, $account, 'Pugsley has no special roles.');

  // Assert that only one role exists:
  // - manager.
  $roles = $this->roleStorage
    ->loadMultiple();
  $this
    ->assertEquals(1, count($roles), 'Only one role exists.');
}