You are here

public function UserRoleTest::testWithAllowedRoles in Feeds 8.3

Tests mapping to role using only allowed roles.

File

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

Class

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

Namespace

Drupal\Tests\feeds\Kernel\Feeds\Target

Code

public function testWithAllowedRoles() {

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

  // Add mapping to role. The manager role may not be assigned to the user by
  // the feed.
  $this->feedType
    ->addMapping([
    'target' => 'roles',
    'map' => [
      'target_id' => 'role_ids',
    ],
    'settings' => [
      'allowed_roles' => [
        'manager' => FALSE,
        'editor' => 'editor',
      ],
      '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 editor role and one role in total.
  $account = user_load_by_name('Morticia');
  $this
    ->assertHasRole($account, 'editor', 'Morticia has the editor role.');
  $this
    ->assertRoleCount(1, $account, 'Morticia has one role.');

  // Assert that Fester did not got the manager role, because that role was
  // not an allowed value.
  $account = user_load_by_name('Fester');
  $this
    ->assertNotHasRole($account, 'manager', 'Fester does not have the manager role.');
  $this
    ->assertRoleCount(0, $account, 'Fester has no special roles.');

  // Assert that Gomez only got the tester role and not the manager role.
  $account = user_load_by_name('Gomez');
  $this
    ->assertNotHasRole($account, 'manager', 'Gomez does not have the manager role.');
  $this
    ->assertHasRole($account, 'tester', 'Gomez has the tester role.');
  $this
    ->assertRoleCount(1, $account, 'Gomez has one role.');
}