You are here

public function ToolbarAdminMenuTest::testUserRoleUpdateSubtreesHashCacheClear in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/toolbar/tests/src/Functional/ToolbarAdminMenuTest.php \Drupal\Tests\toolbar\Functional\ToolbarAdminMenuTest::testUserRoleUpdateSubtreesHashCacheClear()
  2. 10 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 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.');
}