3 namespace Drupal\Tests\comment\Kernel\Views;
5 use Drupal\comment\CommentManagerInterface;
6 use Drupal\Core\Session\AnonymousUserSession;
8 use Drupal\entity_test\Entity\EntityTest;
9 use Drupal\field\Entity\FieldConfig;
10 use Drupal\field\Entity\FieldStorageConfig;
11 use Drupal\views\Views;
14 * Tests the comment link field handlers.
18 class CommentLinksTest extends CommentViewsKernelTestBase {
25 public static $modules = ['entity_test'];
28 * Views used by this test.
32 public static $testViews = ['test_comment'];
37 protected function setUp($import_test_views = TRUE) {
38 parent::setUp($import_test_views);
40 $this->installEntitySchema('entity_test');
44 * Test the comment approve link.
46 public function testLinkApprove() {
47 $host = EntityTest::create(['name' => $this->randomString()]);
50 // Create an unapproved comment.
51 $comment = $this->commentStorage->create([
52 'uid' => $this->adminUser->id(),
53 'entity_type' => 'entity_test',
54 'entity_id' => $host->id(),
55 'comment_type' => 'entity_test',
60 $view = Views::getView('test_comment');
63 $view->displayHandlers->get('default')->overrideOption('fields', [
64 'approve_comment' => [
66 'field' => 'approve_comment',
67 'id' => 'approve_comment',
68 'plugin_id' => 'comment_link_approve',
73 /* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
74 $account_switcher = \Drupal::service('account_switcher');
75 $account_switcher->switchTo($this->adminUser);
79 // Check if I can see the comment approve link on an unapproved comment.
80 $approve_comment = $view->style_plugin->getField(0, 'approve_comment');
81 $options = ['query' => ['destination' => '/']];
82 $url = Url::fromRoute('comment.approve', ['comment' => $comment->id()], $options);
83 $this->assertEqual(\Drupal::l('Approve', $url), (string) $approve_comment, 'Found a comment approve link for an unapproved comment.');
85 // Approve the comment.
86 $comment->setPublished();
88 $view = Views::getView('test_comment');
91 // Check if I can see the comment approve link on an approved comment.
92 $approve_comment = $view->style_plugin->getField(1, 'approve_comment');
93 $this->assertFalse((string) $approve_comment, "Didn't find a comment approve link for an already approved comment.");
95 // Check if I can see the comment approve link on an approved comment as an
97 $account_switcher->switchTo(new AnonymousUserSession());
98 // Set the comment as unpublished again.
99 $comment->setUnpublished();
102 $view = Views::getView('test_comment');
104 $replyto_comment = $view->style_plugin->getField(0, 'approve_comment');
105 $this->assertFalse((string) $replyto_comment, "I can't approve the comment as an anonymous user.");
109 * Test the comment reply link.
111 public function testLinkReply() {
112 $this->enableModules(['field']);
113 $this->installSchema('comment', ['comment_entity_statistics']);
114 $this->installConfig(['field']);
116 $field_storage_comment = FieldStorageConfig::create([
117 'field_name' => 'comment',
119 'entity_type' => 'entity_test',
121 $field_storage_comment->save();
122 // Create a comment field which allows threading.
123 $field_comment = FieldConfig::create([
124 'field_name' => 'comment',
125 'entity_type' => 'entity_test',
126 'bundle' => 'entity_test',
128 'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED,
131 $field_comment->save();
133 $host = EntityTest::create(['name' => $this->randomString()]);
135 // Attach an unapproved comment to the test entity.
136 $comment = $this->commentStorage->create([
137 'uid' => $this->adminUser->id(),
138 'entity_type' => 'entity_test',
139 'entity_id' => $host->id(),
140 'comment_type' => 'entity_test',
141 'field_name' => $field_storage_comment->getName(),
146 $view = Views::getView('test_comment');
149 $view->displayHandlers->get('default')->overrideOption('fields', [
150 'replyto_comment' => [
151 'table' => 'comment',
152 'field' => 'replyto_comment',
153 'id' => 'replyto_comment',
154 'plugin_id' => 'comment_link_reply',
155 'entity_type' => 'comment',
160 /* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
161 $account_switcher = \Drupal::service('account_switcher');
162 $account_switcher->switchTo($this->adminUser);
165 // Check if I can see the reply link on an unapproved comment.
166 $replyto_comment = $view->style_plugin->getField(0, 'replyto_comment');
167 $this->assertFalse((string) $replyto_comment, "I can't reply to an unapproved comment.");
169 // Approve the comment.
170 $comment->setPublished();
172 $view = Views::getView('test_comment');
175 // Check if I can see the reply link on an approved comment.
176 $replyto_comment = $view->style_plugin->getField(0, 'replyto_comment');
177 $url = Url::fromRoute('comment.reply', [
178 'entity_type' => 'entity_test',
179 'entity' => $host->id(),
180 'field_name' => 'comment',
181 'pid' => $comment->id(),
183 $this->assertEqual(\Drupal::l('Reply', $url), (string) $replyto_comment, 'Found the comment reply link as an admin user.');
185 // Check if I can see the reply link as an anonymous user.
186 $account_switcher->switchTo(new AnonymousUserSession());
187 $view = Views::getView('test_comment');
189 $replyto_comment = $view->style_plugin->getField(0, 'replyto_comment');
190 $this->assertFalse((string) $replyto_comment, "Didn't find the comment reply link as an anonymous user.");