3 namespace Drupal\Tests\comment\Kernel\Views;
5 use Drupal\comment\CommentInterface;
6 use Drupal\comment\CommentManagerInterface;
7 use Drupal\Core\Session\AnonymousUserSession;
9 use Drupal\entity_test\Entity\EntityTest;
10 use Drupal\field\Entity\FieldConfig;
11 use Drupal\field\Entity\FieldStorageConfig;
12 use Drupal\views\Views;
15 * Tests the comment link field handlers.
19 class CommentLinksTest extends CommentViewsKernelTestBase {
26 public static $modules = ['entity_test'];
29 * Views used by this test.
33 public static $testViews = ['test_comment'];
38 protected function setUp($import_test_views = TRUE) {
39 parent::setUp($import_test_views);
41 $this->installEntitySchema('entity_test');
45 * Test the comment approve link.
47 public function testLinkApprove() {
48 $host = EntityTest::create(['name' => $this->randomString()]);
51 // Create an unapproved comment.
52 $comment = $this->commentStorage->create([
53 'uid' => $this->adminUser->id(),
54 'entity_type' => 'entity_test',
55 'entity_id' => $host->id(),
56 'comment_type' => 'entity_test',
61 $view = Views::getView('test_comment');
64 $view->displayHandlers->get('default')->overrideOption('fields', [
65 'approve_comment' => [
67 'field' => 'approve_comment',
68 'id' => 'approve_comment',
69 'plugin_id' => 'comment_link_approve',
74 /* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
75 $account_switcher = \Drupal::service('account_switcher');
76 $account_switcher->switchTo($this->adminUser);
80 // Check if I can see the comment approve link on an unapproved comment.
81 $approve_comment = $view->style_plugin->getField(0, 'approve_comment');
82 $options = ['query' => ['destination' => '/']];
83 $url = Url::fromRoute('comment.approve', ['comment' => $comment->id()], $options);
84 $this->assertEqual(\Drupal::l('Approve', $url), (string) $approve_comment, 'Found a comment approve link for an unapproved comment.');
86 // Approve the comment.
87 $comment->setPublished(CommentInterface::PUBLISHED);
89 $view = Views::getView('test_comment');
92 // Check if I can see the comment approve link on an approved comment.
93 $approve_comment = $view->style_plugin->getField(1, 'approve_comment');
94 $this->assertFalse((string) $approve_comment, "Didn't find a comment approve link for an already approved comment.");
96 // Check if I can see the comment approve link on an approved comment as an
98 $account_switcher->switchTo(new AnonymousUserSession());
99 // Set the comment as unpublished again.
100 $comment->setPublished(CommentInterface::NOT_PUBLISHED);
103 $view = Views::getView('test_comment');
105 $replyto_comment = $view->style_plugin->getField(0, 'approve_comment');
106 $this->assertFalse((string) $replyto_comment, "I can't approve the comment as an anonymous user.");
110 * Test the comment reply link.
112 public function testLinkReply() {
113 $this->enableModules(['field']);
114 $this->installSchema('comment', ['comment_entity_statistics']);
115 $this->installConfig(['field']);
117 $field_storage_comment = FieldStorageConfig::create([
118 'field_name' => 'comment',
120 'entity_type' => 'entity_test',
122 $field_storage_comment->save();
123 // Create a comment field which allows threading.
124 $field_comment = FieldConfig::create([
125 'field_name' => 'comment',
126 'entity_type' => 'entity_test',
127 'bundle' => 'entity_test',
129 'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED,
132 $field_comment->save();
134 $host = EntityTest::create(['name' => $this->randomString()]);
136 // Attach an unapproved comment to the test entity.
137 $comment = $this->commentStorage->create([
138 'uid' => $this->adminUser->id(),
139 'entity_type' => 'entity_test',
140 'entity_id' => $host->id(),
141 'comment_type' => 'entity_test',
142 'field_name' => $field_storage_comment->getName(),
147 $view = Views::getView('test_comment');
150 $view->displayHandlers->get('default')->overrideOption('fields', [
151 'replyto_comment' => [
152 'table' => 'comment',
153 'field' => 'replyto_comment',
154 'id' => 'replyto_comment',
155 'plugin_id' => 'comment_link_reply',
156 'entity_type' => 'comment',
161 /* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
162 $account_switcher = \Drupal::service('account_switcher');
163 $account_switcher->switchTo($this->adminUser);
166 // Check if I can see the reply link on an unapproved comment.
167 $replyto_comment = $view->style_plugin->getField(0, 'replyto_comment');
168 $this->assertFalse((string) $replyto_comment, "I can't reply to an unapproved comment.");
170 // Approve the comment.
171 $comment->setPublished(CommentInterface::PUBLISHED);
173 $view = Views::getView('test_comment');
176 // Check if I can see the reply link on an approved comment.
177 $replyto_comment = $view->style_plugin->getField(0, 'replyto_comment');
178 $url = Url::fromRoute('comment.reply', [
179 'entity_type' => 'entity_test',
180 'entity' => $host->id(),
181 'field_name' => 'comment',
182 'pid' => $comment->id(),
184 $this->assertEqual(\Drupal::l('Reply', $url), (string) $replyto_comment, 'Found the comment reply link as an admin user.');
186 // Check if I can see the reply link as an anonymous user.
187 $account_switcher->switchTo(new AnonymousUserSession());
188 $view = Views::getView('test_comment');
190 $replyto_comment = $view->style_plugin->getField(0, 'replyto_comment');
191 $this->assertFalse((string) $replyto_comment, "Didn't find the comment reply link as an anonymous user.");