You are here

public function UserRoleTest::testNoRevokeRoles in Feeds 8.3

Tests if no roles are revoked if the option "Revoke roles" is disabled.

File

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

Class

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

Namespace

Drupal\Tests\feeds\Kernel\Feeds\Target

Code

public function testNoRevokeRoles() {

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

  // Add mapping to role. Set option to not revoke roles.
  $this->feedType
    ->addMapping([
    'target' => 'roles',
    'map' => [
      'target_id' => 'role_ids',
    ],
    'settings' => [
      'allowed_roles' => [
        'manager' => FALSE,
        'editor' => 'editor',
      ],
      'revoke_roles' => FALSE,
    ],
  ]);
  $this->feedType
    ->save();

  // Create account for Pugsley with roles "manager" and "editor". Pugsley has
  // no roles in the source file, but roles should not be revoked, so Pugsley
  // should keep all roles.
  $this->userStorage
    ->create([
    'name' => 'Pugsley',
    'mail' => 'pugsley@example.com',
    'pass' => 'pugs',
    'status' => 1,
    'roles' => [
      'manager',
      'editor',
    ],
  ])
    ->save();

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

  // Assert that Pugsley kept all roles.
  $account = user_load_by_name('Pugsley');
  $this
    ->assertHasRole($account, 'manager', 'Pugsley still has the manager role.');
  $this
    ->assertHasRole($account, 'editor', 'Pugsley still has the editor role.');
  $this
    ->assertRoleCount(2, $account, 'Pugsley has two roles.');
}