You are here

public function UserFieldsAccessChangeTest::testUserNameLink in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/user/tests/src/Functional/Views/UserFieldsAccessChangeTest.php \Drupal\Tests\user\Functional\Views\UserFieldsAccessChangeTest::testUserNameLink()
  2. 9 core/modules/user/tests/src/Functional/Views/UserFieldsAccessChangeTest.php \Drupal\Tests\user\Functional\Views\UserFieldsAccessChangeTest::testUserNameLink()

Tests the user name formatter shows a link to the user when there is access but not otherwise.

File

core/modules/user/tests/src/Functional/Views/UserFieldsAccessChangeTest.php, line 61

Class

UserFieldsAccessChangeTest
Checks changing entity and field access.

Namespace

Drupal\Tests\user\Functional\Views

Code

public function testUserNameLink() {
  $test_user = $this
    ->drupalCreateUser();
  $xpath = "//td/a[.='" . $test_user
    ->getAccountName() . "']/@href[.='" . $test_user
    ->toUrl()
    ->toString() . "']";
  $attributes = [
    'title' => 'View user profile.',
  ];
  $link = $test_user
    ->toLink(NULL, 'canonical', [
    'attributes' => $attributes,
  ])
    ->toString();

  // No access, so no link.
  $this
    ->drupalGet('test_user_fields_access');
  $this
    ->assertSession()
    ->pageTextContains($test_user
    ->getAccountName());
  $result = $this
    ->xpath($xpath);
  $this
    ->assertCount(0, $result, 'User is not a link');

  // Assign sub-admin role to grant extra access.
  $user = $this
    ->drupalCreateUser([
    'sub-admin',
  ]);
  $this
    ->drupalLogin($user);
  $this
    ->drupalGet('test_user_fields_access');
  $result = $this
    ->xpath($xpath);
  $this
    ->assertCount(1, $result, 'User is a link');
}