diff --git a/src/Entity/Storage/GroupRoleStorage.php b/src/Entity/Storage/GroupRoleStorage.php index f0650a8..5ff44fd 100644 --- a/src/Entity/Storage/GroupRoleStorage.php +++ b/src/Entity/Storage/GroupRoleStorage.php @@ -22,15 +22,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface; class GroupRoleStorage extends ConfigEntityStorage implements GroupRoleStorageInterface { /** - * Static cache of a user's group role IDs. - * - * @todo Perhaps we need to be able to clear this cache during runtime? - * - * @var array - */ - protected $userGroupRoleIds = []; - - /** * The group role synchronizer service. * * @var \Drupal\group\GroupRoleSynchronizerInterface @@ -87,7 +78,9 @@ class GroupRoleStorage extends ConfigEntityStorage implements GroupRoleStorageIn $uid = $account->id(); $gid = $group->id(); - if (!isset($this->userGroupRoleIds[$uid][$gid][$include_implied])) { + $user_group_role_ids = &drupal_static(__METHOD__); + + if (!isset($user_group_role_ids[$uid][$gid][$include_implied])) { $ids = []; // Get the IDs from the 'group_roles' field, without loading the roles. @@ -115,10 +108,10 @@ class GroupRoleStorage extends ConfigEntityStorage implements GroupRoleStorageIn } } - $this->userGroupRoleIds[$uid][$gid][$include_implied] = $ids; + $user_group_role_ids[$uid][$gid][$include_implied] = $ids; } - return $this->loadMultiple($this->userGroupRoleIds[$uid][$gid][$include_implied]); + return $this->loadMultiple($user_group_role_ids[$uid][$gid][$include_implied]); } } diff --git a/tests/src/Kernel/GroupRoleStorageTest.php b/tests/src/Kernel/GroupRoleStorageTest.php index 894a038..5474dea 100644 --- a/tests/src/Kernel/GroupRoleStorageTest.php +++ b/tests/src/Kernel/GroupRoleStorageTest.php @@ -85,6 +85,15 @@ class GroupRoleStorageTest extends GroupKernelTestBase { $this->assertCount(2, $roles, 'When a given user/group/internal role flag combination is used again after a bunch of other stuff, the correct roles are still returned.'); $this->assertArrayHasKey('roleA', $roles); $this->assertArrayHasKey('default-member', $roles); + + // Change user roles around and try again. + $groupA->getMember($accountA)->getGroupContent() + ->set('group_roles', ['roleB', 'roleC'])->save(); + drupal_static_reset('Drupal\group\Entity\Storage\GroupRoleStorage::loadByUserAndGroup'); + $roles = $this->storage->loadByUserAndGroup($accountA, $groupA, TRUE); + $this->assertArrayHasKey('roleB', $roles); + $this->assertArrayHasKey('roleC', $roles); + $this->assertArrayNotHasKey('roleA', $roles); } }