2144ebd94aa5d61731ac5cd39ed3845dd75374ee
[yaffs-website] / web / core / modules / comment / tests / src / Kernel / Views / CommentUserNameTest.php
1 <?php
2
3 namespace Drupal\Tests\comment\Kernel\Views;
4
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;
13
14 /**
15  * Tests comment user name field
16  *
17  * @group comment
18  */
19 class CommentUserNameTest extends ViewsKernelTestBase {
20
21   /**
22    * Admin user.
23    *
24    * @var \Drupal\user\UserInterface
25    */
26   protected $adminUser;
27   /**
28    * {@inheritdoc}
29    */
30   public static $modules = ['user', 'comment', 'entity_test'];
31
32   /**
33    * {@inheritdoc}
34    */
35   protected function setUp($import_test_views = TRUE) {
36     parent::setUp($import_test_views);
37
38     $this->installEntitySchema('user');
39     $this->installEntitySchema('comment');
40     $this->installEntitySchema('entity_test');
41     // Create the anonymous role.
42     $this->installConfig(['user']);
43
44     // Create an anonymous user.
45     $storage = \Drupal::entityManager()->getStorage('user');
46     // Insert a row for the anonymous user.
47     $storage
48       ->create([
49         'uid' => 0,
50         'name' => '',
51         'status' => 0,
52       ])
53       ->save();
54
55     $admin_role = Role::create([
56       'id' => 'admin',
57       'permissions' => ['administer comments', 'access user profiles'],
58     ]);
59     $admin_role->save();
60
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();
65
66     $this->adminUser = User::create([
67       'name' => $this->randomMachineName(),
68       'roles' => [$admin_role->id()],
69     ]);
70     $this->adminUser->save();
71
72     $host = EntityTest::create(['name' => $this->randomString()]);
73     $host->save();
74
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',
83       'status' => 1,
84     ]);
85     $comment->save();
86
87     $comment_anonymous = Comment::create([
88       'subject' => 'Anonymous comment title',
89       'uid' => 0,
90       'name' => 'barry',
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',
96       'created' => 123456,
97       'status' => 1,
98     ]);
99     $comment_anonymous->save();
100   }
101
102   /**
103    * Test the username formatter.
104    */
105   public function testUsername() {
106     $view_id = $this->randomMachineName();
107     $view = View::create([
108       'id' => $view_id,
109       'base_table' => 'comment_field_data',
110       'display' => [
111         'default' => [
112           'display_plugin' => 'default',
113           'id' => 'default',
114           'display_options' => [
115             'fields' => [
116               'name' => [
117                 'table' => 'comment_field_data',
118                 'field' => 'name',
119                 'id' => 'name',
120                 'plugin_id' => 'field',
121                 'type' => 'comment_username',
122               ],
123               'subject' => [
124                 'table' => 'comment_field_data',
125                 'field' => 'subject',
126                 'id' => 'subject',
127                 'plugin_id' => 'field',
128                 'type' => 'string',
129                 'settings' => [
130                   'link_to_entity' => TRUE,
131                 ],
132               ],
133             ],
134           ],
135         ],
136       ],
137     ]);
138     $view->save();
139
140     /* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
141     $account_switcher = \Drupal::service('account_switcher');
142
143     /* @var \Drupal\Core\Render\RendererInterface $renderer */
144     $renderer = \Drupal::service('renderer');
145
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());
151
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',
158     ]);
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)');
163
164     $account_switcher->switchTo(new AnonymousUserSession());
165     $executable = Views::getView($view_id);
166     $executable->storage->invalidateCaches();
167
168     $build = $executable->preview();
169     $this->setRawContent($renderer->renderRoot($build));
170
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');
178   }
179
180 }