Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / comment / tests / src / Kernel / Views / CommentLinksTest.php
1 <?php
2
3 namespace Drupal\Tests\comment\Kernel\Views;
4
5 use Drupal\comment\CommentInterface;
6 use Drupal\comment\CommentManagerInterface;
7 use Drupal\Core\Session\AnonymousUserSession;
8 use Drupal\Core\Url;
9 use Drupal\entity_test\Entity\EntityTest;
10 use Drupal\field\Entity\FieldConfig;
11 use Drupal\field\Entity\FieldStorageConfig;
12 use Drupal\views\Views;
13
14 /**
15  * Tests the comment link field handlers.
16  *
17  * @group comment
18  */
19 class CommentLinksTest extends CommentViewsKernelTestBase {
20
21   /**
22    * Modules to enable.
23    *
24    * @var array
25    */
26   public static $modules = ['entity_test'];
27
28   /**
29    * Views used by this test.
30    *
31    * @var array
32    */
33   public static $testViews = ['test_comment'];
34
35   /**
36    * {@inheritdoc}
37    */
38   protected function setUp($import_test_views = TRUE) {
39     parent::setUp($import_test_views);
40
41     $this->installEntitySchema('entity_test');
42   }
43
44   /**
45    * Test the comment approve link.
46    */
47   public function testLinkApprove() {
48     $host = EntityTest::create(['name' => $this->randomString()]);
49     $host->save();
50
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',
57       'status' => 0,
58     ]);
59     $comment->save();
60
61     $view = Views::getView('test_comment');
62     $view->setDisplay();
63
64     $view->displayHandlers->get('default')->overrideOption('fields', [
65       'approve_comment' => [
66         'table' => 'comment',
67         'field' => 'approve_comment',
68         'id' => 'approve_comment',
69         'plugin_id' => 'comment_link_approve',
70       ],
71     ]);
72     $view->save();
73
74     /* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
75     $account_switcher = \Drupal::service('account_switcher');
76     $account_switcher->switchTo($this->adminUser);
77
78     $view->preview();
79
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.');
85
86     // Approve the comment.
87     $comment->setPublished(CommentInterface::PUBLISHED);
88     $comment->save();
89     $view = Views::getView('test_comment');
90     $view->preview();
91
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.");
95
96     // Check if I can see the comment approve link on an approved comment as an
97     // anonymous user.
98     $account_switcher->switchTo(new AnonymousUserSession());
99     // Set the comment as unpublished again.
100     $comment->setPublished(CommentInterface::NOT_PUBLISHED);
101     $comment->save();
102
103     $view = Views::getView('test_comment');
104     $view->preview();
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.");
107   }
108
109   /**
110    * Test the comment reply link.
111    */
112   public function testLinkReply() {
113     $this->enableModules(['field']);
114     $this->installSchema('comment', ['comment_entity_statistics']);
115     $this->installConfig(['field']);
116
117     $field_storage_comment = FieldStorageConfig::create([
118       'field_name' => 'comment',
119       'type' => 'comment',
120       'entity_type' => 'entity_test',
121     ]);
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',
128       'settings' => [
129         'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED,
130       ],
131     ]);
132     $field_comment->save();
133
134     $host = EntityTest::create(['name' => $this->randomString()]);
135     $host->save();
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(),
143       'status' => 0,
144     ]);
145     $comment->save();
146
147     $view = Views::getView('test_comment');
148     $view->setDisplay();
149
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',
157       ],
158     ]);
159     $view->save();
160
161     /* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
162     $account_switcher = \Drupal::service('account_switcher');
163     $account_switcher->switchTo($this->adminUser);
164     $view->preview();
165
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.");
169
170     // Approve the comment.
171     $comment->setPublished(CommentInterface::PUBLISHED);
172     $comment->save();
173     $view = Views::getView('test_comment');
174     $view->preview();
175
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(),
183     ]);
184     $this->assertEqual(\Drupal::l('Reply', $url), (string) $replyto_comment, 'Found the comment reply link as an admin user.');
185
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');
189     $view->preview();
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.");
192   }
193
194 }