3 namespace Drupal\comment\Tests;
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;
12 * Basic comment links tests to ensure markup present.
16 class CommentLinksTest extends CommentTestBase {
19 * Comment being tested.
21 * @var \Drupal\comment\CommentInterface
26 * Seen comments, array of comment IDs.
33 * Use the main node listing to test rendering on teasers.
37 * @todo Remove this dependency.
39 public static $modules = ['views'];
42 * Tests that comment links are output and can be hidden.
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')
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)]);
56 // Create a comment via CRUD API functionality, since
57 // $this->postComment() relies on actual user permissions.
58 $comment = Comment::create([
60 'entity_id' => $this->node->id(),
61 'entity_type' => 'node',
62 'field_name' => 'comment',
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()]],
72 $this->comment = $comment;
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;
80 // Change user permissions.
82 'access comments' => 1,
84 'skip comment approval' => 1,
85 'edit own comments' => 1,
87 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, $perms);
89 $nid = $this->node->id();
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);
96 // In teaser view, a link containing the comment count is always
98 if ($path == 'node') {
99 $this->assertLink(t('1 comment'));
101 $this->assertLink('Add new comment');
104 // Change weight to make links go before comment body.
105 entity_get_display('comment', 'comment', 'default')
106 ->setComponent('links', ['weight' => -100])
108 $this->drupalGet($this->node->urlInfo());
109 $element = $this->cssSelect('article.js-comment > div');
110 // Get last child element.
111 $element = end($element[0]);
112 $this->assertIdentical($element[0]->getName(), 'div', 'Last element is comment body.');
114 // Change weight to make links go after comment body.
115 entity_get_display('comment', 'comment', 'default')
116 ->setComponent('links', ['weight' => 100])
118 $this->drupalGet($this->node->urlInfo());
119 $element = $this->cssSelect('article.js-comment > div');
120 // Get last child element.
121 $element = end($element[0]);
122 $this->assertIdentical($element[0]->getName(), 'ul', 'Last element is comment links.');
124 // Make sure we can hide node links.
125 entity_get_display('node', $this->node->bundle(), 'default')
126 ->removeComponent('links')
128 $this->drupalGet($this->node->urlInfo());
129 $this->assertNoLink('1 comment');
130 $this->assertNoLink('Add new comment');
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');
137 // Make sure we can hide comment links.
138 entity_get_display('comment', 'comment', 'default')
139 ->removeComponent('links')
141 $this->drupalGet('node/' . $this->node->id());
142 $this->assertText($comment->getSubject());
143 $this->assertNoLink('Reply');