You are here

public function UserRoleTest::testRoleCreationUsingLabel in Feeds 8.3

Tests automatically creating new roles based on label.

File

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

Class

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

Namespace

Drupal\Tests\feeds\Kernel\Feeds\Target

Code

public function testRoleCreationUsingLabel() {

  // Add mapping to role.
  $this->feedType
    ->addMapping([
    'target' => 'roles',
    'map' => [
      'target_id' => 'role_labels',
    ],
    'settings' => [
      'reference_by' => 'label',
      'autocreate' => TRUE,
    ],
  ]);
  $this->feedType
    ->save();

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

  // Assert that Morticia got the Article Editor role.
  $account = user_load_by_name('Morticia');
  $this
    ->assertHasRole($account, 'article_editor', 'Morticia got the Article Editor role.');
  $this
    ->assertRoleCount(1, $account, 'Morticia has one role.');

  // Assert that Gomez got the manager and tester roles.
  $account = user_load_by_name('Gomez');
  $this
    ->assertHasRole($account, 'account_manager', 'Gomez has the manager role.');
  $this
    ->assertHasRole($account, 'software_tester', 'Gomez has the tester role.');
  $this
    ->assertRoleCount(2, $account, 'Gomez has two roles.');

  // Assert that the roles all got the expected label.
  $roles = $this->roleStorage
    ->loadMultiple();
  $this
    ->assertEquals(3, count($roles), 'Three roles exist.');
  $this
    ->assertEquals('Article Editor', $roles['article_editor']
    ->label());
  $this
    ->assertEquals('Account Manager', $roles['account_manager']
    ->label());
  $this
    ->assertEquals('Software Tester', $roles['software_tester']
    ->label());
}