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 {
22 * Views used by this test.
26 public static $testViews = ['test_comment'];
29 * Test the comment approve link.
31 public function testLinkApprove() {
33 // Create an unapproved comment.
34 $comment = $this->commentStorage->create([
35 'uid' => $this->adminUser->id(),
36 'entity_type' => 'entity_test',
37 'comment_type' => 'entity_test',
42 $view = Views::getView('test_comment');
45 $view->displayHandlers->get('default')->overrideOption('fields', [
46 'approve_comment' => [
48 'field' => 'approve_comment',
49 'id' => 'approve_comment',
50 'plugin_id' => 'comment_link_approve',
55 /* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
56 $account_switcher = \Drupal::service('account_switcher');
57 $account_switcher->switchTo($this->adminUser);
61 // Check if I can see the comment approve link on an unapproved comment.
62 $approve_comment = $view->style_plugin->getField(0, 'approve_comment');
63 $options = ['query' => ['destination' => '/']];
64 $url = Url::fromRoute('comment.approve', ['comment' => $comment->id()], $options);
65 $this->assertEqual(\Drupal::l('Approve', $url), (string) $approve_comment, 'Found a comment approve link for an unapproved comment.');
67 // Approve the comment.
68 $comment->setPublished(CommentInterface::PUBLISHED);
70 $view = Views::getView('test_comment');
73 // Check if I can see the comment approve link on an approved comment.
74 $approve_comment = $view->style_plugin->getField(1, 'approve_comment');
75 $this->assertFalse((string) $approve_comment, "Didn't find a comment approve link for an already approved comment.");
77 // Check if I can see the comment approve link on an approved comment as an
79 $account_switcher->switchTo(new AnonymousUserSession());
80 // Set the comment as unpublished again.
81 $comment->setPublished(CommentInterface::NOT_PUBLISHED);
84 $view = Views::getView('test_comment');
86 $replyto_comment = $view->style_plugin->getField(0, 'approve_comment');
87 $this->assertFalse((string) $replyto_comment, "I can't approve the comment as an anonymous user.");
91 * Test the comment reply link.
93 public function testLinkReply() {
94 $this->enableModules(['field', 'entity_test']);
95 $this->installEntitySchema('entity_test');
96 $this->installSchema('comment', ['comment_entity_statistics']);
97 $this->installConfig(['field']);
99 $field_storage_comment = FieldStorageConfig::create([
100 'field_name' => 'comment',
102 'entity_type' => 'entity_test',
104 $field_storage_comment->save();
105 // Create a comment field which allows threading.
106 $field_comment = FieldConfig::create([
107 'field_name' => 'comment',
108 'entity_type' => 'entity_test',
109 'bundle' => 'entity_test',
111 'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED,
114 $field_comment->save();
116 $host = EntityTest::create(['name' => $this->randomString()]);
118 // Attach an unapproved comment to the test entity.
119 $comment = $this->commentStorage->create([
120 'uid' => $this->adminUser->id(),
121 'entity_type' => 'entity_test',
122 'entity_id' => $host->id(),
123 'comment_type' => 'entity_test',
124 'field_name' => $field_storage_comment->getName(),
129 $view = Views::getView('test_comment');
132 $view->displayHandlers->get('default')->overrideOption('fields', [
133 'replyto_comment' => [
134 'table' => 'comment',
135 'field' => 'replyto_comment',
136 'id' => 'replyto_comment',
137 'plugin_id' => 'comment_link_reply',
138 'entity_type' => 'comment',
143 /* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
144 $account_switcher = \Drupal::service('account_switcher');
145 $account_switcher->switchTo($this->adminUser);
148 // Check if I can see the reply link on an unapproved comment.
149 $replyto_comment = $view->style_plugin->getField(0, 'replyto_comment');
150 $this->assertFalse((string) $replyto_comment, "I can't reply to an unapproved comment.");
152 // Approve the comment.
153 $comment->setPublished(CommentInterface::PUBLISHED);
155 $view = Views::getView('test_comment');
158 // Check if I can see the reply link on an approved comment.
159 $replyto_comment = $view->style_plugin->getField(0, 'replyto_comment');
160 $url = Url::fromRoute('comment.reply', [
161 'entity_type' => 'entity_test',
162 'entity' => $host->id(),
163 'field_name' => 'comment',
164 'pid' => $comment->id(),
166 $this->assertEqual(\Drupal::l('Reply', $url), (string) $replyto_comment, 'Found the comment reply link as an admin user.');
168 // Check if I can see the reply link as an anonymous user.
169 $account_switcher->switchTo(new AnonymousUserSession());
170 $view = Views::getView('test_comment');
172 $replyto_comment = $view->style_plugin->getField(0, 'replyto_comment');
173 $this->assertFalse((string) $replyto_comment, "Didn't find the comment reply link as an anonymous user.");