Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / comment / tests / src / Functional / CommentLinksTest.php
1 <?php
2
3 namespace Drupal\Tests\comment\Functional;
4
5 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\comment\CommentInterface;
8 use Drupal\user\RoleInterface;
9 use Drupal\comment\Entity\Comment;
10
11 /**
12  * Basic comment links tests to ensure markup present.
13  *
14  * @group comment
15  */
16 class CommentLinksTest extends CommentTestBase {
17
18   /**
19    * Comment being tested.
20    *
21    * @var \Drupal\comment\CommentInterface
22    */
23   protected $comment;
24
25   /**
26    * Seen comments, array of comment IDs.
27    *
28    * @var array
29    */
30   protected $seen = [];
31
32   /**
33    * Use the main node listing to test rendering on teasers.
34    *
35    * @var array
36    *
37    * @todo Remove this dependency.
38    */
39   public static $modules = ['views'];
40
41   /**
42    * Tests that comment links are output and can be hidden.
43    */
44   public function testCommentLinks() {
45     // Bartik theme alters comment links, so use a different theme.
46     \Drupal::service('theme_handler')->install(['stark']);
47     $this->config('system.theme')
48       ->set('default', 'stark')
49       ->save();
50
51     // Remove additional user permissions from $this->webUser added by setUp(),
52     // since this test is limited to anonymous and authenticated roles only.
53     $roles = $this->webUser->getRoles();
54     entity_delete_multiple('user_role', [reset($roles)]);
55
56     // Create a comment via CRUD API functionality, since
57     // $this->postComment() relies on actual user permissions.
58     $comment = Comment::create([
59       'cid' => NULL,
60       'entity_id' => $this->node->id(),
61       'entity_type' => 'node',
62       'field_name' => 'comment',
63       'pid' => 0,
64       'uid' => 0,
65       'status' => CommentInterface::PUBLISHED,
66       'subject' => $this->randomMachineName(),
67       'hostname' => '127.0.0.1',
68       'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
69       'comment_body' => [['value' => $this->randomMachineName()]],
70     ]);
71     $comment->save();
72     $this->comment = $comment;
73
74     // Change comment settings.
75     $this->setCommentSettings('form_location', CommentItemInterface::FORM_BELOW, 'Set comment form location');
76     $this->setCommentAnonymous(TRUE);
77     $this->node->comment = CommentItemInterface::OPEN;
78     $this->node->save();
79
80     // Change user permissions.
81     $perms = [
82       'access comments' => 1,
83       'post comments' => 1,
84       'skip comment approval' => 1,
85       'edit own comments' => 1,
86     ];
87     user_role_change_permissions(RoleInterface::ANONYMOUS_ID, $perms);
88
89     $nid = $this->node->id();
90
91     // Assert basic link is output, actual functionality is unit-tested in
92     // \Drupal\comment\Tests\CommentLinkBuilderTest.
93     foreach (['node', "node/$nid"] as $path) {
94       $this->drupalGet($path);
95
96       // In teaser view, a link containing the comment count is always
97       // expected.
98       if ($path == 'node') {
99         $this->assertLink(t('1 comment'));
100       }
101       $this->assertLink('Add new comment');
102     }
103
104     // Change weight to make links go before comment body.
105     entity_get_display('comment', 'comment', 'default')
106       ->setComponent('links', ['weight' => -100])
107       ->save();
108     $this->drupalGet($this->node->urlInfo());
109     $element = $this->cssSelect('article.js-comment > div');
110     // Get last child element.
111     $element = end($element);
112     $this->assertIdentical($element->getTagName(), 'div', 'Last element is comment body.');
113
114     // Change weight to make links go after comment body.
115     entity_get_display('comment', 'comment', 'default')
116       ->setComponent('links', ['weight' => 100])
117       ->save();
118     $this->drupalGet($this->node->urlInfo());
119     $element = $this->cssSelect('article.js-comment > div');
120     // Get last child element.
121     $element = end($element);
122     $this->assertNotEmpty($element->find('css', 'ul.links'), 'Last element is comment links.');
123
124     // Make sure we can hide node links.
125     entity_get_display('node', $this->node->bundle(), 'default')
126       ->removeComponent('links')
127       ->save();
128     $this->drupalGet($this->node->urlInfo());
129     $this->assertNoLink('1 comment');
130     $this->assertNoLink('Add new comment');
131
132     // Visit the full node, make sure there are links for the comment.
133     $this->drupalGet('node/' . $this->node->id());
134     $this->assertText($comment->getSubject());
135     $this->assertLink('Reply');
136
137     // Make sure we can hide comment links.
138     entity_get_display('comment', 'comment', 'default')
139       ->removeComponent('links')
140       ->save();
141     $this->drupalGet('node/' . $this->node->id());
142     $this->assertText($comment->getSubject());
143     $this->assertNoLink('Reply');
144   }
145
146 }