Loading src/Feeds/Target/UserRole.php +32 −1 Changes for src/Feeds/Target/UserRole.php: 32 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -77,7 +77,7 @@ class UserRole extends ConfigEntityReference { } // Merge the remaining values. $values = array_merge($item_list->getValue(), $original_values); $values = $this->mergeRoles($item_list->getValue(), $original_values); $item_list->setValue($values); } Loading Loading @@ -253,4 +253,35 @@ class UserRole extends ConfigEntityReference { return $summary; } /** * Merge two arrays of user roles together and remove duplicates. * * @param array $roles * An array of roles. * @param array $merging_roles * An array of merging roles. * * @return array * An array of merged roles. */ protected function mergeRoles(array $roles, array $merging_roles) { $merged_roles = array_merge($roles, $merging_roles); $existing_map = []; $unique_roles = []; foreach ($merged_roles as $role) { // Ignore role ids that already exist. if (isset($existing_map[$role['target_id']])) { continue; } // Add the role and mark the role id as existed. $unique_roles[] = $role; $existing_map[$role['target_id']] = 1; } return $unique_roles; } } tests/src/Kernel/Feeds/Target/UserRoleTest.php +39 −0 Changes for tests/src/Kernel/Feeds/Target/UserRoleTest.php: 39 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -505,4 +505,43 @@ class UserRoleTest extends FeedsKernelTestBase { $this->assertRoleCount(2, $account, 'Pugsley has two roles.'); } /** * Tests updating a user with an existing role. */ public function testImportWithExistingRole() { // Create a user with the editor role. $this->createRole([], 'editor'); $user = $this->createUser([ 'name' => 'Morticia', ]); $user->addRole('editor'); $user->save(); // Add mapping to role. $this->feedType->addMapping([ 'target' => 'roles', 'map' => ['target_id' => 'role_ids'], 'settings' => [ 'allowed_roles' => [ 'editor' => 'editor', ], ], ]); $this->feedType->save(); // Import CSV file. $feed = $this->createFeed($this->feedType->id(), [ 'source' => $this->resourcesPath() . '/csv/users_roles.csv', ]); $feed->import(); /** @var \Drupal\user\Entity\User $account */ $account = user_load_by_name('Morticia'); // Assert that the editor role is still there. $this->assertHasRole($account, 'editor'); // Assert that there is only 1 role, editor. $this->assertRoleCount(1, $account); } } Loading
src/Feeds/Target/UserRole.php +32 −1 Changes for src/Feeds/Target/UserRole.php: 32 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -77,7 +77,7 @@ class UserRole extends ConfigEntityReference { } // Merge the remaining values. $values = array_merge($item_list->getValue(), $original_values); $values = $this->mergeRoles($item_list->getValue(), $original_values); $item_list->setValue($values); } Loading Loading @@ -253,4 +253,35 @@ class UserRole extends ConfigEntityReference { return $summary; } /** * Merge two arrays of user roles together and remove duplicates. * * @param array $roles * An array of roles. * @param array $merging_roles * An array of merging roles. * * @return array * An array of merged roles. */ protected function mergeRoles(array $roles, array $merging_roles) { $merged_roles = array_merge($roles, $merging_roles); $existing_map = []; $unique_roles = []; foreach ($merged_roles as $role) { // Ignore role ids that already exist. if (isset($existing_map[$role['target_id']])) { continue; } // Add the role and mark the role id as existed. $unique_roles[] = $role; $existing_map[$role['target_id']] = 1; } return $unique_roles; } }
tests/src/Kernel/Feeds/Target/UserRoleTest.php +39 −0 Changes for tests/src/Kernel/Feeds/Target/UserRoleTest.php: 39 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -505,4 +505,43 @@ class UserRoleTest extends FeedsKernelTestBase { $this->assertRoleCount(2, $account, 'Pugsley has two roles.'); } /** * Tests updating a user with an existing role. */ public function testImportWithExistingRole() { // Create a user with the editor role. $this->createRole([], 'editor'); $user = $this->createUser([ 'name' => 'Morticia', ]); $user->addRole('editor'); $user->save(); // Add mapping to role. $this->feedType->addMapping([ 'target' => 'roles', 'map' => ['target_id' => 'role_ids'], 'settings' => [ 'allowed_roles' => [ 'editor' => 'editor', ], ], ]); $this->feedType->save(); // Import CSV file. $feed = $this->createFeed($this->feedType->id(), [ 'source' => $this->resourcesPath() . '/csv/users_roles.csv', ]); $feed->import(); /** @var \Drupal\user\Entity\User $account */ $account = user_load_by_name('Morticia'); // Assert that the editor role is still there. $this->assertHasRole($account, 'editor'); // Assert that there is only 1 role, editor. $this->assertRoleCount(1, $account); } }