Verified Commit 4fc16796 authored by godotislate's avatar godotislate
Browse files

fix: #3380334 user_update_10000 fails on role with no data

By: fabienscotto
By: daletrexel
By: quietone
By: thidd
By: danielveza
By: xjm
By: godotislate
parent 1f5f2c4e
Loading
Loading
Loading
Loading
Loading
+22 −0
Changes for core/modules/user/tests/src/Functional/Update/UserUpdateRoleMigrateTest.php: 22 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -51,6 +51,14 @@ public function testRolePermissions(): void {
    $authenticated = Role::load('authenticated');
    $this->assertTrue($authenticated->hasPermission('does_not_exist'), 'Authenticated role has a permission that does not exist');

    $connection->insert('config')
      ->fields([
        'collection' => '',
        'name' => 'user.role.broken_role',
        'data' => serialize([]),
      ])
      ->execute();

    $this->runUpdates();

    $this->assertSession()->pageTextContains('The role Authenticated user has had non-existent permissions removed. Check the logs for details.');
@@ -62,6 +70,20 @@ public function testRolePermissions(): void {
    $this->drupalGet('admin/reports/dblog', ['query' => ['type[]' => 'update']]);
    $this->clickLink('The role Authenticated user has had the following non-…');
    $this->assertSession()->pageTextContains('The role Authenticated user has had the following non-existent permission(s) removed: does_not_exist.');

    // Verify the broken role log message is recorded.
    $this->drupalGet('admin/reports/dblog', ['query' => ['type[]' => 'update']]);
    $this->clickLink('The role user.role.broken_role has no permissions and…');
    $this->assertSession()->pageTextContains('The role user.role.broken_role has no permissions and should be removed.');

    // Verify the broken role config still exists (it was skipped, not deleted).
    $broken_role_exists = $connection
      ->select('config', 'c')
      ->condition('name', 'user.role.broken_role')
      ->countQuery()
      ->execute()
      ->fetchField();
    $this->assertEquals(1, $broken_role_exists, 'The broken role config still exists in the database.');
  }

}
+11 −0
Changes for core/modules/user/user.install: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -156,6 +156,17 @@ function user_update_10000() {
  $role_ids = $config_factory->listAll('user.role.');
  foreach ($role_ids as $role_id) {
    $role_config = $config_factory->getEditable($role_id);
    if ($role_config->get('permissions') === NULL) {
      // The data array for this role is empty. It is broken and should be
      // removed.
      \Drupal::logger('update')->notice(
        'The role %role has no permissions and should be removed.',
        [
          '%role' => $role_config->getName(),
        ]
      );
      continue;
    }
    $removed_permissions = array_diff($role_config->get('permissions'), $existing_permissions);
    if (!empty($removed_permissions)) {
      $cleaned_roles[] = $role_config->get('label');