Newer
Older
<?php
Alex Pott
committed
namespace Drupal\Tests\comment\Kernel\Views;
use Drupal\comment\CommentInterface;
use Drupal\comment\CommentManagerInterface;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\Core\Url;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\views\Views;
/**
* Tests the comment link field handlers.
*
* @group comment
*/
Alex Pott
committed
class CommentLinksTest extends CommentViewsKernelTestBase {
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = ['test_comment'];
/**
* Test the comment approve link.
*/
public function testLinkApprove() {
// Create an unapproved comment.
$comment = $this->commentStorage->create([
'uid' => $this->adminUser->id(),
'entity_type' => 'entity_test',
'comment_type' => 'entity_test',
'status' => 0,
]);
$comment->save();
$view = Views::getView('test_comment');
$view->setDisplay();
Alex Pott
committed
$view->displayHandlers->get('default')->overrideOption('fields', [
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'approve_comment' => [
'table' => 'comment',
'field' => 'approve_comment',
'id' => 'approve_comment',
'plugin_id' => 'comment_link_approve',
],
]);
$view->save();
/* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
$account_switcher = \Drupal::service('account_switcher');
$account_switcher->switchTo($this->adminUser);
$view->preview();
// Check if I can see the comment approve link on an unapproved comment.
$approve_comment = $view->style_plugin->getField(0, 'approve_comment');
$options = ['query' => ['destination' => '/']];
$url = Url::fromRoute('comment.approve', ['comment' => $comment->id()], $options);
$this->assertEqual(\Drupal::l('Approve', $url), (string) $approve_comment, 'Found a comment approve link for an unapproved comment.');
// Approve the comment.
$comment->setPublished(CommentInterface::PUBLISHED);
$comment->save();
$view = Views::getView('test_comment');
$view->preview();
// Check if I can see the comment approve link on an approved comment.
$approve_comment = $view->style_plugin->getField(1, 'approve_comment');
$this->assertFalse((string) $approve_comment, "Didn't find a comment approve link for an already approved comment.");
// Check if I can see the comment approve link on an approved comment as an
// anonymous user.
$account_switcher->switchTo(new AnonymousUserSession());
// Set the comment as unpublished again.
$comment->setPublished(CommentInterface::NOT_PUBLISHED);
$comment->save();
$view = Views::getView('test_comment');
$view->preview();
$replyto_comment = $view->style_plugin->getField(0, 'approve_comment');
$this->assertFalse((string) $replyto_comment, "I can't approve the comment as an anonymous user.");
}
/**
* Test the comment reply link.
*/
public function testLinkReply() {
$this->enableModules(['field', 'entity_test']);
$this->installEntitySchema('entity_test');
$this->installSchema('comment', ['comment_entity_statistics']);
$this->installConfig(['field']);
$field_storage_comment = FieldStorageConfig::create([
'field_name' => 'comment',
'type' => 'comment',
'entity_type' => 'entity_test',
]);
$field_storage_comment->save();
// Create a comment field which allows threading.
$field_comment = FieldConfig::create([
'field_name' => 'comment',
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'settings' => [
'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED,
],
]);
$field_comment->save();
$host = EntityTest::create(['name' => $this->randomString()]);
$host->save();
// Attach an unapproved comment to the test entity.
$comment = $this->commentStorage->create([
'uid' => $this->adminUser->id(),
'entity_type' => 'entity_test',
'entity_id' => $host->id(),
'comment_type' => 'entity_test',
'field_name' => $field_storage_comment->getName(),
'status' => 0,
]);
$comment->save();
$view = Views::getView('test_comment');
$view->setDisplay();
Alex Pott
committed
$view->displayHandlers->get('default')->overrideOption('fields', [
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'replyto_comment' => [
'table' => 'comment',
'field' => 'replyto_comment',
'id' => 'replyto_comment',
'plugin_id' => 'comment_link_reply',
'entity_type' => 'comment',
],
]);
$view->save();
/* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
$account_switcher = \Drupal::service('account_switcher');
$account_switcher->switchTo($this->adminUser);
$view->preview();
// Check if I can see the reply link on an unapproved comment.
$replyto_comment = $view->style_plugin->getField(0, 'replyto_comment');
$this->assertFalse((string) $replyto_comment, "I can't reply to an unapproved comment.");
// Approve the comment.
$comment->setPublished(CommentInterface::PUBLISHED);
$comment->save();
$view = Views::getView('test_comment');
$view->preview();
// Check if I can see the reply link on an approved comment.
$replyto_comment = $view->style_plugin->getField(0, 'replyto_comment');
$url = Url::fromRoute('comment.reply', [
'entity_type' => 'entity_test',
'entity' => $host->id(),
'field_name' => 'comment',
'pid' => $comment->id(),
]);
$this->assertEqual(\Drupal::l('Reply', $url), (string) $replyto_comment, 'Found the comment reply link as an admin user.');
// Check if I can see the reply link as an anonymous user.
$account_switcher->switchTo(new AnonymousUserSession());
$view = Views::getView('test_comment');
$view->preview();
$replyto_comment = $view->style_plugin->getField(0, 'replyto_comment');
$this->assertFalse((string) $replyto_comment, "Didn't find the comment reply link as an anonymous user.");
}
}