Version 1
[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    * Views used by this test.
23    *
24    * @var array
25    */
26   public static $testViews = ['test_comment'];
27
28   /**
29    * Test the comment approve link.
30    */
31   public function testLinkApprove() {
32
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',
38       'status' => 0,
39     ]);
40     $comment->save();
41
42     $view = Views::getView('test_comment');
43     $view->setDisplay();
44
45     $view->displayHandlers->get('default')->overrideOption('fields', [
46       'approve_comment' => [
47         'table' => 'comment',
48         'field' => 'approve_comment',
49         'id' => 'approve_comment',
50         'plugin_id' => 'comment_link_approve',
51       ],
52     ]);
53     $view->save();
54
55     /* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
56     $account_switcher = \Drupal::service('account_switcher');
57     $account_switcher->switchTo($this->adminUser);
58
59     $view->preview();
60
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.');
66
67     // Approve the comment.
68     $comment->setPublished(CommentInterface::PUBLISHED);
69     $comment->save();
70     $view = Views::getView('test_comment');
71     $view->preview();
72
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.");
76
77     // Check if I can see the comment approve link on an approved comment as an
78     // anonymous user.
79     $account_switcher->switchTo(new AnonymousUserSession());
80     // Set the comment as unpublished again.
81     $comment->setPublished(CommentInterface::NOT_PUBLISHED);
82     $comment->save();
83
84     $view = Views::getView('test_comment');
85     $view->preview();
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.");
88   }
89
90   /**
91    * Test the comment reply link.
92    */
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']);
98
99     $field_storage_comment = FieldStorageConfig::create([
100       'field_name' => 'comment',
101       'type' => 'comment',
102       'entity_type' => 'entity_test',
103     ]);
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',
110       'settings' => [
111         'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED,
112       ],
113     ]);
114     $field_comment->save();
115
116     $host = EntityTest::create(['name' => $this->randomString()]);
117     $host->save();
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(),
125       'status' => 0,
126     ]);
127     $comment->save();
128
129     $view = Views::getView('test_comment');
130     $view->setDisplay();
131
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',
139       ],
140     ]);
141     $view->save();
142
143     /* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
144     $account_switcher = \Drupal::service('account_switcher');
145     $account_switcher->switchTo($this->adminUser);
146     $view->preview();
147
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.");
151
152     // Approve the comment.
153     $comment->setPublished(CommentInterface::PUBLISHED);
154     $comment->save();
155     $view = Views::getView('test_comment');
156     $view->preview();
157
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(),
165     ]);
166     $this->assertEqual(\Drupal::l('Reply', $url), (string) $replyto_comment, 'Found the comment reply link as an admin user.');
167
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');
171     $view->preview();
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.");
174   }
175
176 }