function UserUpdateRoleMigrateTest::testRolePermissions

Same name and namespace in other branches
  1. 10 core/modules/user/tests/src/Functional/Update/UserUpdateRoleMigrateTest.php \Drupal\Tests\user\Functional\Update\UserUpdateRoleMigrateTest::testRolePermissions()

Tests that roles have only existing permissions.

File

core/modules/user/tests/src/Functional/Update/UserUpdateRoleMigrateTest.php, line 30

Class

UserUpdateRoleMigrateTest
Tests <a href="/api/drupal/core%21modules%21user%21user.install/function/user_update_10000/10" title="Remove non-existent permissions created by migrations." class="local">user_update_10000</a>() upgrade path.

Namespace

Drupal\Tests\user\Functional\Update

Code

public function testRolePermissions() {
    
    /** @var \Drupal\Core\Database\Connection $connection */
    $connection = \Drupal::service('database');
    // Edit the authenticated role to have a non-existent permission.
    $authenticated = $connection->select('config')
        ->fields('config', [
        'data',
    ])
        ->condition('collection', '')
        ->condition('name', 'user.role.authenticated')
        ->execute()
        ->fetchField();
    $authenticated = unserialize($authenticated);
    $authenticated['permissions'][] = 'does_not_exist';
    $connection->update('config')
        ->fields([
        'data' => serialize($authenticated),
    ])
        ->condition('collection', '')
        ->condition('name', 'user.role.authenticated')
        ->execute();
    $authenticated = Role::load('authenticated');
    $this->assertTrue($authenticated->hasPermission('does_not_exist'), 'Authenticated role has a permission that does not exist');
    $this->runUpdates();
    $this->assertSession()
        ->pageTextContains('The role Authenticated user has had non-existent permissions removed. Check the logs for details.');
    $authenticated = Role::load('authenticated');
    $this->assertFalse($authenticated->hasPermission('does_not_exist'), 'Authenticated role does not have a permission that does not exist');
    $this->drupalLogin($this->createUser([
        'access site reports',
    ]));
    $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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.