diff --git a/core/modules/comment/src/CommentViewsData.php b/core/modules/comment/src/CommentViewsData.php index e8c4a7a..894236e 100644 --- a/core/modules/comment/src/CommentViewsData.php +++ b/core/modules/comment/src/CommentViewsData.php @@ -33,7 +33,6 @@ public function getViewsData() { $data['comment_field_data']['name']['title'] = t('Author'); $data['comment_field_data']['name']['help'] = t("The name of the comment's author. Can be rendered as a link to the author's homepage."); - $data['comment_field_data']['name']['field']['id'] = 'field'; $data['comment_field_data']['name']['field']['default_formatter'] = 'comment_username'; $data['comment_field_data']['homepage']['title'] = t("Author's website"); diff --git a/core/modules/comment/src/Plugin/Field/FieldFormatter/AuthorNameFormatter.php b/core/modules/comment/src/Plugin/Field/FieldFormatter/AuthorNameFormatter.php index a74628f..0a2a0ca 100644 --- a/core/modules/comment/src/Plugin/Field/FieldFormatter/AuthorNameFormatter.php +++ b/core/modules/comment/src/Plugin/Field/FieldFormatter/AuthorNameFormatter.php @@ -11,7 +11,7 @@ use Drupal\Core\Field\FormatterBase; /** - * Plugin implementation of the 'user_name' formatter. + * Plugin implementation of the 'comment_username' formatter. * * @FieldFormatter( * id = "comment_username", diff --git a/core/modules/comment/src/Tests/Views/CommentUserNameTest.php b/core/modules/comment/src/Tests/Views/CommentUserNameTest.php new file mode 100644 index 0000000..365d9f7 --- /dev/null +++ b/core/modules/comment/src/Tests/Views/CommentUserNameTest.php @@ -0,0 +1,162 @@ +installEntitySchema('user'); + $this->installEntitySchema('comment'); + // Create the anonymous role. + $this->installConfig(['user']); + + // Create an anonymous user. + $storage = \Drupal::entityManager()->getStorage('user'); + // Insert a row for the anonymous user. + $storage + ->create(array( + 'uid' => 0, + 'status' => 0, + )) + ->save(); + + $admin_role = Role::create([ + 'id' => 'admin', + 'permissions' => ['administer comments', 'access user profiles'], + ]); + $admin_role->save(); + + /* @var \Drupal\user\RoleInterface $anonymous_role */ + $anonymous_role = Role::load(Role::ANONYMOUS_ID); + $anonymous_role->grantPermission('view comments'); + $anonymous_role->save(); + + $this->adminUser = User::create([ + 'name' => $this->randomMachineName(), + 'roles' => [$admin_role->id()], + ]); + $this->adminUser->save(); + + // Create some comments. + $comment = Comment::create([ + 'subject' => 'My comment title', + 'uid' => $this->adminUser->id(), + 'entity_type' => 'entity_test', + 'comment_type' => 'entity_test', + 'status' => 1, + ]); + $comment->save(); + + $comment_anonymous = Comment::create([ + 'subject' => 'Anonymous comment title', + 'uid' => 0, + 'name' => 'barry', + 'mail' => 'test@example.com', + 'homepage' => 'https://example.com', + 'entity_type' => 'entity_test', + 'comment_type' => 'entity_test', + 'created' => 123456, + 'status' => 1, + ]); + $comment_anonymous->save(); + } + + /** + * Test the username formatter. + */ + public function testUsername() { + $view_id = $this->randomMachineName(); + $view = View::create([ + 'id' => $view_id, + 'base_table' => 'comment_field_data', + 'display' => [ + 'default' => [ + 'display_plugin' => 'default', + 'id' => 'default', + 'display_options' => [ + 'fields' => [ + 'name' => [ + 'table' => 'comment_field_data', + 'field' => 'name', + 'id' => 'name', + 'plugin_id' => 'field', + 'type' => 'comment_username' + ], + 'subject' => [ + 'table' => 'comment_field_data', + 'field' => 'subject', + 'id' => 'subject', + 'plugin_id' => 'field', + ], + ], + ], + ], + ], + ]); + $view->save(); + + /* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */ + $account_switcher = \Drupal::service('account_switcher'); + + /* @var \Drupal\Core\Render\RendererInterface $renderer */ + $renderer = \Drupal::service('renderer'); + + $account_switcher->switchTo($this->adminUser); + $executable = Views::getView($view_id); + $build = $executable->preview(); + $this->setRawContent($renderer->render($build)); + $this->verbose($this->getRawContent()); + + $this->assertLink('My comment title'); + $this->assertLink('Anonymous comment title'); + $this->assertLink($this->adminUser->label()); + $this->assertLink('barry (not verified)'); + + $account_switcher->switchTo(new AnonymousUserSession()); + $executable = Views::getView($view_id); + $build = $executable->preview(); + $this->setRawContent($renderer->render($build)); + + // No access to user-profiles, so shouldn't be able to see links. + $this->assertNoLink($this->adminUser->label()); + $this->assertNoLink('barry (not verified)'); + $this->verbose($this->getRawContent()); + $this->assertLink('My comment title'); + $this->assertLink('Anonymous comment title'); + } + +}