You are here

public function EntityAccessControlHandlerTest::testUserLabelAccess in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Entity/EntityAccessControlHandlerTest.php \Drupal\KernelTests\Core\Entity\EntityAccessControlHandlerTest::testUserLabelAccess()
  2. 9 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 58

Class

EntityAccessControlHandlerTest
Tests the entity access control handler.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testUserLabelAccess() {

  // Set up a non-admin user.
  \Drupal::currentUser()
    ->setAccount($this
    ->createUser([
    '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();
}