function EntityAccessControlHandlerTest::testUserLabelAccess

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityAccessControlHandlerTest.php \Drupal\KernelTests\Core\Entity\EntityAccessControlHandlerTest::testUserLabelAccess()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityAccessControlHandlerTest.php \Drupal\KernelTests\Core\Entity\EntityAccessControlHandlerTest::testUserLabelAccess()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityAccessControlHandlerTest.php \Drupal\KernelTests\Core\Entity\EntityAccessControlHandlerTest::testUserLabelAccess()

Ensures user labels are accessible for everyone.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityAccessControlHandlerTest.php, line 60

Class

EntityAccessControlHandlerTest
Tests the entity access control handler.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testUserLabelAccess() : void {
  // Set up a non-admin user.
  \Drupal::currentUser()->setAccount($this->createUser([], NULL, FALSE, [
    'uid' => 2,
  ]));
  $anonymous_user = User::getAnonymousUser();
  $user = $this->createUser();
  // The current user is allowed to view the anonymous user label.
  $this->assertEntityAccess([
    'create' => FALSE,
    'update' => FALSE,
    'delete' => FALSE,
    'view' => FALSE,
    'view label' => TRUE,
  ], $anonymous_user);
  // The current user is allowed to view user labels.
  $this->assertEntityAccess([
    'create' => FALSE,
    'update' => FALSE,
    'delete' => FALSE,
    'view' => FALSE,
    'view label' => TRUE,
  ], $user);
  // Switch to an anonymous user account.
  $account_switcher = \Drupal::service('account_switcher');
  $account_switcher->switchTo(new AnonymousUserSession());
  // The anonymous user is allowed to view the anonymous user label.
  $this->assertEntityAccess([
    'create' => FALSE,
    'update' => FALSE,
    'delete' => FALSE,
    'view' => FALSE,
    'view label' => TRUE,
  ], $anonymous_user);
  // The anonymous user is allowed to view user labels.
  $this->assertEntityAccess([
    'create' => FALSE,
    'update' => FALSE,
    'delete' => FALSE,
    'view' => FALSE,
    'view label' => TRUE,
  ], $user);
  // Restore user account.
  $account_switcher->switchBack();
}

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