3 namespace Drupal\Tests\comment\Kernel\Views;
5 use Drupal\comment\Entity\Comment;
6 use Drupal\Core\Session\AnonymousUserSession;
7 use Drupal\entity_test\Entity\EntityTest;
8 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
9 use Drupal\user\Entity\Role;
10 use Drupal\user\Entity\User;
11 use Drupal\views\Entity\View;
12 use Drupal\views\Views;
15 * Tests comment user name field
19 class CommentUserNameTest extends ViewsKernelTestBase {
24 * @var \Drupal\user\UserInterface
30 public static $modules = ['user', 'comment', 'entity_test'];
35 protected function setUp($import_test_views = TRUE) {
36 parent::setUp($import_test_views);
38 $this->installEntitySchema('user');
39 $this->installEntitySchema('comment');
40 $this->installEntitySchema('entity_test');
41 // Create the anonymous role.
42 $this->installConfig(['user']);
44 // Create an anonymous user.
45 $storage = \Drupal::entityManager()->getStorage('user');
46 // Insert a row for the anonymous user.
55 $admin_role = Role::create([
57 'permissions' => ['administer comments', 'access user profiles'],
61 /* @var \Drupal\user\RoleInterface $anonymous_role */
62 $anonymous_role = Role::load(Role::ANONYMOUS_ID);
63 $anonymous_role->grantPermission('access comments');
64 $anonymous_role->save();
66 $this->adminUser = User::create([
67 'name' => $this->randomMachineName(),
68 'roles' => [$admin_role->id()],
70 $this->adminUser->save();
72 $host = EntityTest::create(['name' => $this->randomString()]);
75 // Create some comments.
76 $comment = Comment::create([
77 'subject' => 'My comment title',
78 'uid' => $this->adminUser->id(),
79 'name' => $this->adminUser->label(),
80 'entity_type' => 'entity_test',
81 'entity_id' => $host->id(),
82 'comment_type' => 'entity_test',
87 $comment_anonymous = Comment::create([
88 'subject' => 'Anonymous comment title',
91 'mail' => 'test@example.com',
92 'homepage' => 'https://example.com',
93 'entity_type' => 'entity_test',
94 'entity_id' => $host->id(),
95 'comment_type' => 'entity_test',
99 $comment_anonymous->save();
103 * Test the username formatter.
105 public function testUsername() {
106 $view_id = $this->randomMachineName();
107 $view = View::create([
109 'base_table' => 'comment_field_data',
112 'display_plugin' => 'default',
114 'display_options' => [
117 'table' => 'comment_field_data',
120 'plugin_id' => 'field',
121 'type' => 'comment_username',
124 'table' => 'comment_field_data',
125 'field' => 'subject',
127 'plugin_id' => 'field',
130 'link_to_entity' => TRUE,
140 /* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
141 $account_switcher = \Drupal::service('account_switcher');
143 /* @var \Drupal\Core\Render\RendererInterface $renderer */
144 $renderer = \Drupal::service('renderer');
146 $account_switcher->switchTo($this->adminUser);
147 $executable = Views::getView($view_id);
148 $build = $executable->preview();
149 $this->setRawContent($renderer->renderRoot($build));
150 $this->verbose($this->getRawContent());
152 $this->assertLink('My comment title');
153 $this->assertLink('Anonymous comment title');
154 // Display plugin of the view is showing the name field. When comment
155 // belongs to an authenticated user the name field has no value.
156 $comment_author = $this->xpath('//div[contains(@class, :class)]/span[normalize-space(text())=""]', [
157 ':class' => 'views-field-subject',
159 $this->assertTrue(!empty($comment_author));
160 // When comment belongs to an anonymous user the name field has a value and
161 // it is rendered correctly.
162 $this->assertLink('barry (not verified)');
164 $account_switcher->switchTo(new AnonymousUserSession());
165 $executable = Views::getView($view_id);
166 $executable->storage->invalidateCaches();
168 $build = $executable->preview();
169 $this->setRawContent($renderer->renderRoot($build));
171 // No access to user-profiles, so shouldn't be able to see links.
172 $this->assertNoLink($this->adminUser->label());
173 // Note: External users aren't pointing to drupal user profiles.
174 $this->assertLink('barry (not verified)');
175 $this->verbose($this->getRawContent());
176 $this->assertLink('My comment title');
177 $this->assertLink('Anonymous comment title');