function ToolbarAdminMenuTest::testUserRoleUpdateSubtreesHashCacheClear

Same name in other branches
  1. 9 core/modules/toolbar/tests/src/Functional/ToolbarAdminMenuTest.php \Drupal\Tests\toolbar\Functional\ToolbarAdminMenuTest::testUserRoleUpdateSubtreesHashCacheClear()
  2. 8.9.x core/modules/toolbar/tests/src/Functional/ToolbarAdminMenuTest.php \Drupal\Tests\toolbar\Functional\ToolbarAdminMenuTest::testUserRoleUpdateSubtreesHashCacheClear()
  3. 11.x core/modules/toolbar/tests/src/Functional/ToolbarAdminMenuTest.php \Drupal\Tests\toolbar\Functional\ToolbarAdminMenuTest::testUserRoleUpdateSubtreesHashCacheClear()

Tests Toolbar's responses to user data updates.

See also

toolbar_user_role_update()

toolbar_user_update()

File

core/modules/toolbar/tests/src/Functional/ToolbarAdminMenuTest.php, line 186

Class

ToolbarAdminMenuTest
Tests the caching of the admin menu subtree items.

Namespace

Drupal\Tests\toolbar\Functional

Code

public function testUserRoleUpdateSubtreesHashCacheClear() : void {
    // Find the new role ID.
    $all_rids = $this->adminUser
        ->getRoles();
    unset($all_rids[array_search(RoleInterface::AUTHENTICATED_ID, $all_rids)]);
    $rid = reset($all_rids);
    $edit = [];
    $edit[$rid . '[administer taxonomy]'] = FALSE;
    $this->drupalGet('admin/people/permissions');
    $this->submitForm($edit, 'Save permissions');
    // Assert that the subtrees hash has been altered because the subtrees
    // structure changed.
    $this->assertDifferentHash();
    // Test that assigning a user an extra role only affects that single user.
    // Get the hash for a second user.
    $this->drupalLogin($this->adminUser2);
    $this->drupalGet('test-page');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Assert that the toolbar is present in the HTML.
    $this->assertSession()
        ->responseContains('id="toolbar-administration"');
    $admin_user_2_hash = $this->getSubtreesHash();
    // Log in the first admin user again.
    $this->drupalLogin($this->adminUser);
    $this->drupalGet('test-page');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Assert that the toolbar is present in the HTML.
    $this->assertSession()
        ->responseContains('id="toolbar-administration"');
    $this->hash = $this->getSubtreesHash();
    $rid = $this->drupalCreateRole([
        'administer content types',
    ]);
    // Assign the role to the user.
    $this->drupalGet('user/' . $this->adminUser
        ->id() . '/edit');
    $this->submitForm([
        "roles[{$rid}]" => $rid,
    ], 'Save');
    $this->assertSession()
        ->pageTextContains('The changes have been saved.');
    // Assert that the subtrees hash has been altered because the subtrees
    // structure changed.
    $this->assertDifferentHash();
    // Log in the second user again and assert that their subtrees hash did not
    // change.
    $this->drupalLogin($this->adminUser2);
    // Request a new page to refresh the drupalSettings object.
    $this->drupalGet('test-page');
    $this->assertSession()
        ->statusCodeEquals(200);
    $new_subtree_hash = $this->getSubtreesHash();
    // Assert that the old admin menu subtree hash and the new admin menu
    // subtree hash are the same.
    $this->assertNotEmpty($new_subtree_hash, 'A valid hash value for the admin menu subtrees was created.');
    $this->assertEquals($admin_user_2_hash, $new_subtree_hash, 'The user-specific subtree menu hash has not been updated.');
}

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