UserFieldsAccessChangeTest.php

Same filename and directory in other branches
  1. 9 core/modules/user/tests/src/Functional/Views/UserFieldsAccessChangeTest.php
  2. 10 core/modules/user/tests/src/Functional/Views/UserFieldsAccessChangeTest.php
  3. 11.x core/modules/user/tests/src/Functional/Views/UserFieldsAccessChangeTest.php

Namespace

Drupal\Tests\user\Functional\Views

File

core/modules/user/tests/src/Functional/Views/UserFieldsAccessChangeTest.php

View source
<?php

namespace Drupal\Tests\user\Functional\Views;


/**
 * Checks changing entity and field access.
 *
 * @group user
 */
class UserFieldsAccessChangeTest extends UserTestBase {
  
  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = [
    'user_access_test',
  ];
  
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'classy';
  
  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = [
    'test_user_fields_access',
  ];
  
  /**
   * Tests if another module can change field access.
   */
  public function testUserFieldAccess() {
    $this->drupalGet('test_user_fields_access');
    // User has access to name and created date by default.
    $this->assertText(t('Name'));
    $this->assertText(t('Created'));
    // User does not by default have access to init, mail and status.
    $this->assertNoText(t('Init'));
    $this->assertNoText(t('Email'));
    $this->assertNoText(t('Status'));
    // Assign sub-admin role to grant extra access.
    $user = $this->drupalCreateUser([
      'sub-admin',
    ]);
    $this->drupalLogin($user);
    $this->drupalGet('test_user_fields_access');
    // Access for init, mail and status is added in hook_entity_field_access().
    $this->assertText(t('Init'));
    $this->assertText(t('Email'));
    $this->assertText(t('Status'));
  }
  
  /**
   * Tests the user name formatter shows a link to the user when there is
   * access but not otherwise.
   */
  public function testUserNameLink() {
    $test_user = $this->drupalCreateUser();
    $xpath = "//td/a[.='" . $test_user->getAccountName() . "'][@class='username']/@href[.='" . $test_user->toUrl()
      ->toString() . "']";
    $attributes = [
      'title' => 'View user profile.',
      'class' => 'username',
    ];
    $link = $test_user->toLink(NULL, 'canonical', [
      'attributes' => $attributes,
    ])
      ->toString();
    // No access, so no link.
    $this->drupalGet('test_user_fields_access');
    $this->assertText($test_user->getAccountName(), 'Found user in view');
    $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');
  }

}

Classes

Title Deprecated Summary
UserFieldsAccessChangeTest Checks changing entity and field access.

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