public function UserRoleTest::testWithRoleCreation in Feeds 8.3
Tests mapping to role with automatically creating new roles.
File
- tests/
src/ Kernel/ Feeds/ Target/ UserRoleTest.php, line 178
Class
- UserRoleTest
- @coversDefaultClass \Drupal\feeds\Feeds\Target\UserRole @group feeds
Namespace
Drupal\Tests\feeds\Kernel\Feeds\TargetCode
public function testWithRoleCreation() {
// Create the manager role.
$this
->createRole([], 'manager');
// Add mapping to role.
$this->feedType
->addMapping([
'target' => 'roles',
'map' => [
'target_id' => 'role_ids',
],
'settings' => [
'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 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, the editor role and two roles in
// total.
$account = user_load_by_name('Gomez');
$this
->assertHasRole($account, 'manager', 'Gomez has the manager role.');
$this
->assertHasRole($account, 'tester', 'Gomez has the tester role.');
$this
->assertRoleCount(2, $account, 'Gomez has two roles.');
// Assert that Pugsley has no roles.
$account = user_load_by_name('Pugsley');
$this
->assertRoleCount(0, $account, 'Pugsley has no special roles.');
// Assert that three roles exist:
// - manager;
// - editor;
// - tester.
$roles = $this->roleStorage
->loadMultiple();
$this
->assertEquals(3, count($roles), 'Three roles exist.');
// Assert that the roles all got the expected label.
$this
->assertEquals('editor', $roles['editor']
->label());
$this
->assertEquals('tester', $roles['tester']
->label());
}