diff --git a/core/modules/user/src/Tests/UserUsernameTest.php b/core/modules/user/src/Tests/UserUsernameTest.php index 98e4356..35db278 100644 --- a/core/modules/user/src/Tests/UserUsernameTest.php +++ b/core/modules/user/src/Tests/UserUsernameTest.php @@ -7,21 +7,17 @@ namespace Drupal\user\Tests; -use Drupal\Core\Url; -//use Drupal\views\Entity\View; -use Drupal\views\Views; -use Drupal\views\Tests\ViewTestBase; -//use Drupal\Core\Template\Attribute; use Drupal\Component\Utility\SafeMarkup; use Drupal\Component\Utility\Unicode; use Drupal\comment\Tests\CommentTestTrait; +use Drupal\simpletest\WebTestBase; /** * Tests username functionality. * * @group user */ -class UserUsernameTest extends ViewTestBase { +class UserUsernameTest extends WebTestBase { use CommentTestTrait; @@ -69,7 +65,7 @@ public function setUp() { 'skip comment approval', 'create article content', ), - $this->randomMachineName(25) + $this->randomString(30) ); // Create user with long username. @@ -81,10 +77,8 @@ public function setUp() { 'skip comment approval', 'create article content', ), - $this->randomMachineName(35) + $this->randomString(31) ); - - $this->drupalLogin($this->rootUser); } /** @@ -96,7 +90,7 @@ public function testTruncatedNodeAuthorName() { $node = $this->drupalCreateNode(array('type' => 'article')); $this->drupalGet('node/' . $node->id()); - $this->assertText($this->userShortName->getUsername()); + $this->assertText(SafeMarkup::checkPlain($this->userShortName->getUsername())); // Test truncated username. $this->drupalLogin($this->userLongName); @@ -106,8 +100,8 @@ public function testTruncatedNodeAuthorName() { // Tests if the username is trimmed to USERNAME_TRIM_LENGTH value. // @see template_preprocess_username(). - $this->assertTrue($this->userShortName->getUsername(), 'Username is marked safe'); - $this->assertText(Unicode::truncate($this->userLongName->getUsername(), USERNAME_TRIM_LENGTH - 1, FALSE, TRUE)); + $this->assertTrue(SafeMarkup::checkPlain($this->userLongName->getUsername())); + $this->assertText(SafeMarkup::checkPlain(Unicode::truncate($this->userLongName->getUsername(), USERNAME_TRIM_LENGTH - 1, FALSE, TRUE))); } /** @@ -122,7 +116,7 @@ public function testTruncatedCommentAuthorName() { 'comment_body[0][value]' => $this->randomMachineName(20), ); $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $comment, t('Save')); - $this->assertTrue($this->userShortName->getUsername(), 'Username is marked safe'); + $this->assertTrue(SafeMarkup::checkPlain($this->userLongName->getUsername())); $this->drupalLogin($this->userLongName); $comment = array( 'subject[0][value]' => $this->randomMachineName(), @@ -143,22 +137,22 @@ public function testTruncatedCommentAuthorName() { public function testTruncatedWhosNewName() { // Enable Who's New block. $this->drupalPlaceBlock('views_block:who_s_new-block_1', array('label' => 'user_view_block-block_1:1', 'views_label' => 'Who\'s new')); - $this->drupalGet(''); // The Who's new block requires a last access to be set. // Log the users in for them to show up. $this->drupalLogin($this->userShortName); $this->drupalLogin($this->userLongName); + // Log user out to prevent a false negative. + $this->drupalLogout(); + $this->drupalGet(''); // Tests if the username is trimmed to USERNAME_TRIM_LENGTH value. // @see template_preprocess_username(). - $this->assertText($this->userShortName->getUsername()); - $this->assertNoText($this->userLongName->getUsername()); + $this->assertText(SafeMarkup::checkPlain($this->userShortName->getUsername())); + $this->assertNoText(SafeMarkup::checkPlain($this->userLongName->getUsername())); $this->assertText(SafeMarkup::checkPlain(Unicode::truncate($this->userLongName->getUsername(), USERNAME_TRIM_LENGTH - 1, FALSE, TRUE))); - - $this->drupalLogout(); } } diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 322f91f..38c24b3 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -478,7 +478,8 @@ function template_preprocess_username(&$variables) { // unsanitized version, in case other preprocess functions want to implement // their own shortening logic or add markup. If they do so, they must ensure // that $variables['name'] is safe for printing. - $name = $variables['name_raw'] = $account->getUsername(); + $name = $account->getDisplayName(); + $variables['name_raw'] = $account->getUsername(); if (Unicode::strlen($name) > $variables['trim_length']) { $name = Unicode::truncate($name, $variables['trim_length'] - 1, FALSE, TRUE); $variables['truncated'] = TRUE;