3 namespace Drupal\Tests\user\Functional;
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\user\Entity\User;
9 * Tests specific parts of the user entity like the URI callback and the label
14 class UserEntityCallbacksTest extends BrowserTestBase {
21 public static $modules = ['user', 'user_hooks_test'];
24 * An authenticated user to use for testing.
26 * @var \Drupal\user\UserInterface
31 * An anonymous user to use for testing.
33 * @var \Drupal\user\UserInterface
37 protected function setUp() {
40 $this->account = $this->drupalCreateUser();
41 $this->anonymous = User::create(['uid' => 0]);
45 * Test label callback.
47 public function testLabelCallback() {
48 $this->assertEqual($this->account->label(), $this->account->getUsername(), 'The username should be used as label');
50 // Setup a random anonymous name to be sure the name is used.
51 $name = $this->randomMachineName();
52 $this->config('user.settings')->set('anonymous', $name)->save();
53 $this->assertEqual($this->anonymous->label(), $name, 'The variable anonymous should be used for name of uid 0');
54 $this->assertEqual($this->anonymous->getDisplayName(), $name, 'The variable anonymous should be used for display name of uid 0');
55 $this->assertEqual($this->anonymous->getUserName(), '', 'The raw anonymous user name should be empty string');
57 // Set to test the altered username.
58 \Drupal::state()->set('user_hooks_test_user_format_name_alter', TRUE);
60 $this->assertEqual($this->account->getDisplayName(), '<em>' . $this->account->id() . '</em>', 'The user display name should be altered.');
61 $this->assertEqual($this->account->getUsername(), $this->account->name->value, 'The user name should not be altered.');