cad7b2b9815e800eec3f46bbfc2845246e7c0306
[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\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;
12
13 /**
14  * Tests comment user name field
15  *
16  * @group comment
17  */
18 class CommentUserNameTest extends ViewsKernelTestBase {
19
20   /**
21    * Admin user.
22    *
23    * @var \Drupal\user\UserInterface
24    */
25   protected $adminUser;
26   /**
27    * {@inheritdoc}
28    */
29   public static $modules = ['user', 'comment', 'entity_test'];
30
31   /**
32    * {@inheritdoc}
33    */
34   protected function setUp($import_test_views = TRUE) {
35     parent::setUp($import_test_views);
36
37     $this->installEntitySchema('user');
38     $this->installEntitySchema('comment');
39     // Create the anonymous role.
40     $this->installConfig(['user']);
41
42     // Create an anonymous user.
43     $storage = \Drupal::entityManager()->getStorage('user');
44     // Insert a row for the anonymous user.
45     $storage
46       ->create([
47         'uid' => 0,
48         'name' => '',
49         'status' => 0,
50       ])
51       ->save();
52
53     $admin_role = Role::create([
54       'id' => 'admin',
55       'permissions' => ['administer comments', 'access user profiles'],
56     ]);
57     $admin_role->save();
58
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();
63
64     $this->adminUser = User::create([
65       'name' => $this->randomMachineName(),
66       'roles' => [$admin_role->id()],
67     ]);
68     $this->adminUser->save();
69
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',
77       'status' => 1,
78     ]);
79     $comment->save();
80
81     $comment_anonymous = Comment::create([
82       'subject' => 'Anonymous comment title',
83       'uid' => 0,
84       'name' => 'barry',
85       'mail' => 'test@example.com',
86       'homepage' => 'https://example.com',
87       'entity_type' => 'entity_test',
88       'comment_type' => 'entity_test',
89       'created' => 123456,
90       'status' => 1,
91     ]);
92     $comment_anonymous->save();
93   }
94
95   /**
96    * Test the username formatter.
97    */
98   public function testUsername() {
99     $view_id = $this->randomMachineName();
100     $view = View::create([
101       'id' => $view_id,
102       'base_table' => 'comment_field_data',
103       'display' => [
104         'default' => [
105           'display_plugin' => 'default',
106           'id' => 'default',
107           'display_options' => [
108             'fields' => [
109               'name' => [
110                 'table' => 'comment_field_data',
111                 'field' => 'name',
112                 'id' => 'name',
113                 'plugin_id' => 'field',
114                 'type' => 'comment_username'
115               ],
116               'subject' => [
117                 'table' => 'comment_field_data',
118                 'field' => 'subject',
119                 'id' => 'subject',
120                 'plugin_id' => 'field',
121                 'type' => 'string',
122                 'settings' => [
123                   'link_to_entity' => TRUE,
124                 ],
125               ],
126             ],
127           ],
128         ],
129       ],
130     ]);
131     $view->save();
132
133     /* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
134     $account_switcher = \Drupal::service('account_switcher');
135
136     /* @var \Drupal\Core\Render\RendererInterface $renderer */
137     $renderer = \Drupal::service('renderer');
138
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());
144
145     $this->assertLink('My comment title');
146     $this->assertLink('Anonymous comment title');
147     $this->assertLink($this->adminUser->label());
148     $this->assertLink('barry (not verified)');
149
150     $account_switcher->switchTo(new AnonymousUserSession());
151     $executable = Views::getView($view_id);
152     $executable->storage->invalidateCaches();
153
154     $build = $executable->preview();
155     $this->setRawContent($renderer->renderRoot($build));
156
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');
164   }
165
166 }