3 namespace Drupal\Tests\comment\Kernel\Views;
5 use Drupal\comment\Entity\Comment;
6 use Drupal\Core\Session\AnonymousUserSession;
7 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
8 use Drupal\user\Entity\Role;
9 use Drupal\user\Entity\User;
10 use Drupal\views\Entity\View;
11 use Drupal\views\Views;
14 * Tests comment user name field
18 class CommentUserNameTest extends ViewsKernelTestBase {
23 * @var \Drupal\user\UserInterface
29 public static $modules = ['user', 'comment', 'entity_test'];
34 protected function setUp($import_test_views = TRUE) {
35 parent::setUp($import_test_views);
37 $this->installEntitySchema('user');
38 $this->installEntitySchema('comment');
39 // Create the anonymous role.
40 $this->installConfig(['user']);
42 // Create an anonymous user.
43 $storage = \Drupal::entityManager()->getStorage('user');
44 // Insert a row for the anonymous user.
53 $admin_role = Role::create([
55 'permissions' => ['administer comments', 'access user profiles'],
59 /* @var \Drupal\user\RoleInterface $anonymous_role */
60 $anonymous_role = Role::load(Role::ANONYMOUS_ID);
61 $anonymous_role->grantPermission('access comments');
62 $anonymous_role->save();
64 $this->adminUser = User::create([
65 'name' => $this->randomMachineName(),
66 'roles' => [$admin_role->id()],
68 $this->adminUser->save();
70 // Create some comments.
71 $comment = Comment::create([
72 'subject' => 'My comment title',
73 'uid' => $this->adminUser->id(),
74 'name' => $this->adminUser->label(),
75 'entity_type' => 'entity_test',
76 'comment_type' => 'entity_test',
81 $comment_anonymous = Comment::create([
82 'subject' => 'Anonymous comment title',
85 'mail' => 'test@example.com',
86 'homepage' => 'https://example.com',
87 'entity_type' => 'entity_test',
88 'comment_type' => 'entity_test',
92 $comment_anonymous->save();
96 * Test the username formatter.
98 public function testUsername() {
99 $view_id = $this->randomMachineName();
100 $view = View::create([
102 'base_table' => 'comment_field_data',
105 'display_plugin' => 'default',
107 'display_options' => [
110 'table' => 'comment_field_data',
113 'plugin_id' => 'field',
114 'type' => 'comment_username'
117 'table' => 'comment_field_data',
118 'field' => 'subject',
120 'plugin_id' => 'field',
123 'link_to_entity' => TRUE,
133 /* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
134 $account_switcher = \Drupal::service('account_switcher');
136 /* @var \Drupal\Core\Render\RendererInterface $renderer */
137 $renderer = \Drupal::service('renderer');
139 $account_switcher->switchTo($this->adminUser);
140 $executable = Views::getView($view_id);
141 $build = $executable->preview();
142 $this->setRawContent($renderer->renderRoot($build));
143 $this->verbose($this->getRawContent());
145 $this->assertLink('My comment title');
146 $this->assertLink('Anonymous comment title');
147 $this->assertLink($this->adminUser->label());
148 $this->assertLink('barry (not verified)');
150 $account_switcher->switchTo(new AnonymousUserSession());
151 $executable = Views::getView($view_id);
152 $executable->storage->invalidateCaches();
154 $build = $executable->preview();
155 $this->setRawContent($renderer->renderRoot($build));
157 // No access to user-profiles, so shouldn't be able to see links.
158 $this->assertNoLink($this->adminUser->label());
159 // Note: External users aren't pointing to drupal user profiles.
160 $this->assertLink('barry (not verified)');
161 $this->verbose($this->getRawContent());
162 $this->assertLink('My comment title');
163 $this->assertLink('Anonymous comment title');