Comments

agentrickard created an issue. See original summary.

damontgomery’s picture

Status: Active » Needs review
StatusFileSize
new3.56 KB

As far as I can tell, the section information for roles is already cleaned up when a role is deleted. I wrote an automated test for this and tested it manually as well.

I am using:
- Drupal: 8.7.9
- Workbench Access: 1.x-dev
- Palantir.net Drupal Skeleton: https://github.com/palantirnet/drupal-skeleton
- PHPUnit: 6.5.14

- Create two roles, /admin/people/roles/add
- Assign the "Allow all members of this role to be assigned to Workbench Access sections" permission to these roles. (If you assign the permission to authenticated users, it doesn't apply to those roles. :( )
- Create a scheme based on the main navigation, /admin/config/workflow/workbench_access/access_scheme/add, assign it to the Article type that comes with the standard profile
- Assign the two created roles to the top level section
- Confirm the list of roles says "2" and the roles are listed
- Delete one of the roles, /admin/people/roles
- View the list of roles again and confirm it says "1" and the other role is still assigned
- Add another role to the assignment to confirm you can still edit it

The attached patch adds the test for this and updates the interface for role storage. Role IDs in Drupal 8 are strings, not integers.

Let me know if I missed something and there is other behavior we need to test for.

agentrickard’s picture

This test is great.

I think the difference is because of how data is stored for the two entity types. Roles are not fielded, so I think all data is stored right on the record.

larowlan’s picture

+++ b/tests/src/Functional/DeleteRoleTest.php
@@ -0,0 +1,90 @@
+    $this->assertEquals(['role_a', 'role_b'], $assigned_roles, 'The test roles are not assigned to the section.');
+    ¶
+    /* @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */

nit whitespace here

I think the reference to the role still exists in the section storage, but isn't found because \Drupal\workbench_access\RoleSectionStorage::getRoleSections uses $results = $this->sectionStorage()->loadByProperties(['role_id' => $account->getRoles(), 'access_scheme' => $scheme->id()]); where the call to $account->getRoles() doesn't include the deleted role, because user_user_role_delete does the clean up for the user.

So I think this might still manifest in things where we don't use the getRoleSections method - possibly in views?

agentrickard’s picture

So best practice would be to delete just in case...

agentrickard’s picture

Status: Needs review » Needs work
StatusFileSize
new4.35 KB

I updated the test and it shows that we are still storing the data.

agentrickard’s picture

Status: Needs work » Needs review
StatusFileSize
new6.68 KB

New tests pass locally.

larowlan’s picture

+++ b/src/RoleSectionStorage.php
@@ -174,7 +174,7 @@ class RoleSectionStorage implements RoleSectionStorageInterface {
-  protected function saveRoleSections(AccessSchemeInterface $scheme, $role_id, array $settings = []) {
+  public function saveRoleSections(AccessSchemeInterface $scheme, $role_id, array $settings = []) {

is this change needed?

other than that, looks good

agentrickard’s picture

I will double check. The test failed if the method stayed private.

agentrickard’s picture

1) Drupal\Tests\workbench_access\Functional\DeleteRoleTest::testRoleDelete
Error: Call to private method Drupal\workbench_access\RoleSectionStorage::deleteRoleSections() from context ''

That comes from the invocation in:

function workbench_access_user_role_delete($role) {
  $role_section_storage = \Drupal::service('workbench_access.role_section_storage');
  $scheme_storage = \Drupal::entityTypeManager()->getStorage('access_scheme');
  foreach ($scheme_storage->loadMultiple() as $scheme) {
    $role_section_storage->deleteRoleSections($scheme, $role->id());
  }
}

Which seems like an object context to me, but isn't, clearly. PHP 7.1.22.

agentrickard’s picture

agentrickard’s picture

I think we're ok making these methods public since they are not defined in the interface.

agentrickard’s picture

Status: Needs review » Fixed

Committed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.