You are here

function ToolbarAdminMenuTest::testUserRoleUpdateSubtreesHashCacheClear in Zircon Profile 8

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

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

File

core/modules/toolbar/src/Tests/ToolbarAdminMenuTest.php, line 152
Contains \Drupal\toolbar\Tests\ToolbarAdminMenuTest.

Class

ToolbarAdminMenuTest
Tests the caching of the admin menu subtree items.

Namespace

Drupal\toolbar\Tests

Code

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 = array();
  $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
    ->assertResponse(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
    ->assertResponse(200);

  // Assert that the toolbar is present in the HTML.
  $this
    ->assertRaw('id="toolbar-administration"');
  $this->hash = $this
    ->getSubtreesHash();
  $rid = $this
    ->drupalCreateRole(array(
    'administer content types',
  ));

  // Assign the role to the user.
  $this
    ->drupalPostForm('user/' . $this->adminUser
    ->id() . '/edit', array(
    "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
    ->assertResponse(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
    ->assertTrue($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.');
}