public function UserRoleTest::testRevokeRoles in Feeds 8.3
Tests that roles can be revoked and that only allowed roles are revoked.
File
- tests/
src/ Kernel/ Feeds/ Target/ UserRoleTest.php, line 372
Class
- UserRoleTest
- @coversDefaultClass \Drupal\feeds\Feeds\Target\UserRole @group feeds
Namespace
Drupal\Tests\feeds\Kernel\Feeds\TargetCode
public function testRevokeRoles() {
// Create the manager, editor and tester roles.
$this
->createRole([], 'manager');
$this
->createRole([], 'editor');
$this
->createRole([], 'tester');
// Add mapping to role. The manager role may not be revoked, but the editor
// role may.
$this->feedType
->addMapping([
'target' => 'roles',
'map' => [
'target_id' => 'role_ids',
],
'settings' => [
'allowed_roles' => [
'manager' => FALSE,
'editor' => 'editor',
'tester' => 'tester',
],
'revoke_roles' => TRUE,
],
]);
$this->feedType
->save();
// Create account for Morticia with roles "manager" and "editor". In the
// source only "editor" is specified. Morticia should keep both roles.
$this->userStorage
->create([
'name' => 'Morticia',
'mail' => 'morticia@example.com',
'pass' => 'mort',
'status' => 1,
'roles' => [
'manager',
'editor',
],
])
->save();
// Create account for Pugsley with roles "manager", "editor" and "tester".
// Pugsley has no roles in the source so should only keep the "manager"
// role.
$this->userStorage
->create([
'name' => 'Pugsley',
'mail' => 'pugsley@example.com',
'pass' => 'pugs',
'status' => 1,
'roles' => [
'manager',
'editor',
'tester',
],
])
->save();
// Create account for Gomez and give it the "editor" role. Gomez has roles
// "tester" and "manager" in the source, so it should lose the "editor" role
// and gain the "tester" role.
$this->userStorage
->create([
'name' => 'Gomez',
'mail' => 'gomez@example.com',
'pass' => 'gome',
'status' => 1,
'roles' => [
'editor',
],
])
->save();
// Import CSV file.
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/csv/users_roles.csv',
]);
$feed
->import();
// Assert that Morticia kept the manager and editor roles.
$account = user_load_by_name('Morticia');
$this
->assertHasRole($account, 'manager', 'Morticia still has the manager role.');
$this
->assertHasRole($account, 'editor', 'Morticia has the editor role.');
$this
->assertRoleCount(2, $account, 'Morticia has two roles.');
// Assert that Pugsley only kept the manager role.
$account = user_load_by_name('Pugsley');
$this
->assertHasRole($account, 'manager', 'Pugsley still has the manager role.');
$this
->assertNotHasRole($account, 'editor', 'Pugsley no longer has the editor role.');
$this
->assertNotHasRole($account, 'tester', 'Pugsley no longer has the tester role.');
$this
->assertRoleCount(1, $account, 'Pugsley has one role.');
// Assert that Gomez lost the editor role, and gained the tester role.
$account = user_load_by_name('Gomez');
$this
->assertNotHasRole($account, 'editor', 'Gomez no longer has the editor role.');
$this
->assertHasRole($account, 'tester', 'Gomez has the tester role.');
$this
->assertRoleCount(1, $account, 'Gomez has one role.');
}