function ToolbarAdminMenuTest::testUserRoleUpdateSubtreesHashCacheClear

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

File

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

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->drupalPostForm('admin/people/permissions', $edit, t('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->assertRaw('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->assertRaw('id="toolbar-administration"');
  $this->hash = $this->getSubtreesHash();
  $rid = $this->drupalCreateRole([
    'administer content types',
  ]);
  // Assign the role to the user.
  $this->drupalPostForm('user/' . $this->adminUser
    ->id() . '/edit', [
    "roles[{$rid}]" => $rid,
  ], t('Save'));
  $this->assertText(t('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->assertEqual($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.