You are here

public function ToolbarAdminMenuTest::testUserRoleUpdateSubtreesHashCacheClear in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/toolbar/tests/src/Functional/ToolbarAdminMenuTest.php \Drupal\Tests\toolbar\Functional\ToolbarAdminMenuTest::testUserRoleUpdateSubtreesHashCacheClear()

Exercises the toolbar_user_role_update() and toolbar_user_update() hook implementations.

File

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

Class

ToolbarAdminMenuTest
Tests the caching of the admin menu subtree items.

Namespace

Drupal\Tests\toolbar\Functional

Code

public function testUserRoleUpdateSubtreesHashCacheClear() {

  // 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.');
}