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