3 namespace Drupal\Tests\comment\Kernel;
5 use Drupal\comment\Entity\Comment;
6 use Drupal\comment\Entity\CommentType;
7 use Drupal\comment\Tests\CommentTestTrait;
8 use Drupal\Component\Render\FormattableMarkup;
9 use Drupal\Core\Session\AnonymousUserSession;
10 use Drupal\entity_test\Entity\EntityTest;
11 use Drupal\field\Entity\FieldConfig;
12 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
13 use Drupal\Tests\Traits\Core\GeneratePermutationsTrait;
14 use Drupal\user\Entity\Role;
15 use Drupal\user\RoleInterface;
18 * Tests comment field level access.
23 class CommentFieldAccessTest extends EntityKernelTestBase {
26 use GeneratePermutationsTrait;
33 public static $modules = ['comment', 'entity_test', 'user'];
36 * Fields that only users with administer comments permissions can change.
40 protected $administrativeFields = [
47 * These fields are automatically managed and can not be changed by any user.
51 protected $readOnlyFields = [
59 * These fields can be edited on create only.
63 protected $createOnlyFields = [
73 * These fields can only be edited by the admin or anonymous users if allowed.
77 protected $contactFields = [
86 protected function setUp() {
88 $this->installConfig(['user', 'comment']);
89 $this->installSchema('comment', ['comment_entity_statistics']);
93 * Test permissions on comment fields.
95 public function testAccessToAdministrativeFields() {
96 // Create a comment type.
97 $comment_type = CommentType::create([
99 'label' => 'Default comments',
100 'description' => 'Default comment field',
101 'target_entity_type_id' => 'entity_test',
103 $comment_type->save();
105 // Create a comment against a test entity.
106 $host = EntityTest::create();
109 // An administrator user. No user exists yet, ensure that the first user
110 // does not have UID 1.
111 $comment_admin_user = $this->createUser(['uid' => 2, 'name' => 'admin'], [
112 'administer comments',
116 // Two comment enabled users, one with edit access.
117 $comment_enabled_user = $this->createUser(['name' => 'enabled'], [
119 'skip comment approval',
123 $comment_no_edit_user = $this->createUser(['name' => 'no edit'], [
125 'skip comment approval',
129 // An unprivileged user.
130 $comment_disabled_user = $this->createUser(['name' => 'disabled'], ['access content']);
132 $role = Role::load(RoleInterface::ANONYMOUS_ID);
133 $role->grantPermission('post comments')
136 $anonymous_user = new AnonymousUserSession();
139 $this->addDefaultCommentField('entity_test', 'entity_test', 'comment');
140 $this->addDefaultCommentField('entity_test', 'entity_test', 'comment_other');
142 // Change the second field's anonymous contact setting.
143 $instance = FieldConfig::loadByName('entity_test', 'entity_test', 'comment_other');
144 // Default is 'May not contact', for this field - they may contact.
145 $instance->setSetting('anonymous', COMMENT_ANONYMOUS_MAY_CONTACT);
148 // Create three "Comments". One is owned by our edit-enabled user.
149 $comment1 = Comment::create([
150 'entity_type' => 'entity_test',
152 'hostname' => 'magic.example.com',
153 'mail' => 'tonythemagicalpony@example.com',
154 'subject' => 'Bruce the Mesopotamian moose',
155 'entity_id' => $host->id(),
156 'comment_type' => 'comment',
157 'field_name' => 'comment',
163 $comment2 = Comment::create([
164 'entity_type' => 'entity_test',
165 'hostname' => 'magic.example.com',
166 'subject' => 'Brian the messed up lion',
167 'entity_id' => $host->id(),
168 'comment_type' => 'comment',
169 'field_name' => 'comment',
172 'uid' => $comment_enabled_user->id(),
175 $comment3 = Comment::create([
176 'entity_type' => 'entity_test',
177 'hostname' => 'magic.example.com',
180 'subject' => 'Gail the minky whale',
181 'entity_id' => $host->id(),
182 'comment_type' => 'comment',
183 'field_name' => 'comment_other',
184 'pid' => $comment2->id(),
185 'uid' => $comment_no_edit_user->id(),
188 // Note we intentionally don't save this comment so it remains 'new'.
189 $comment4 = Comment::create([
190 'entity_type' => 'entity_test',
191 'hostname' => 'magic.example.com',
194 'subject' => 'Daniel the Cocker-Spaniel',
195 'entity_id' => $host->id(),
196 'comment_type' => 'comment',
197 'field_name' => 'comment_other',
199 'uid' => $anonymous_user->id(),
202 // Generate permutations.
204 'comment' => [$comment1, $comment2, $comment3, $comment4],
205 'user' => [$comment_admin_user, $comment_enabled_user, $comment_no_edit_user, $comment_disabled_user, $anonymous_user],
207 $permutations = $this->generatePermutations($combinations);
209 // Check access to administrative fields.
210 foreach ($this->administrativeFields as $field) {
211 foreach ($permutations as $set) {
212 $may_view = $set['comment']->{$field}->access('view', $set['user']);
213 $may_update = $set['comment']->{$field}->access('edit', $set['user']);
214 $this->assertTrue($may_view, new FormattableMarkup('User @user can view field @field on comment @comment', [
215 '@user' => $set['user']->getUsername(),
216 '@comment' => $set['comment']->getSubject(),
219 $this->assertEqual($may_update, $set['user']->hasPermission('administer comments'), new FormattableMarkup('User @user @state update field @field on comment @comment', [
220 '@user' => $set['user']->getUsername(),
221 '@state' => $may_update ? 'can' : 'cannot',
222 '@comment' => $set['comment']->getSubject(),
228 // Check access to normal field.
229 foreach ($permutations as $set) {
230 $may_update = $set['comment']->access('update', $set['user']) && $set['comment']->subject->access('edit', $set['user']);
231 $this->assertEqual($may_update, $set['user']->hasPermission('administer comments') || ($set['user']->hasPermission('edit own comments') && $set['user']->id() == $set['comment']->getOwnerId()), new FormattableMarkup('User @user @state update field subject on comment @comment', [
232 '@user' => $set['user']->getUsername(),
233 '@state' => $may_update ? 'can' : 'cannot',
234 '@comment' => $set['comment']->getSubject(),
238 // Check read-only fields.
239 foreach ($this->readOnlyFields as $field) {
240 // Check view operation.
241 foreach ($permutations as $set) {
242 $may_view = $set['comment']->{$field}->access('view', $set['user']);
243 $may_update = $set['comment']->{$field}->access('edit', $set['user']);
244 // Nobody has access to view the hostname field.
245 if ($field === 'hostname') {
246 $view_access = FALSE;
253 $this->assertEqual($may_view, $view_access, new FormattableMarkup('User @user @state view field @field on comment @comment', [
254 '@user' => $set['user']->getUsername(),
255 '@comment' => $set['comment']->getSubject(),
259 $this->assertFalse($may_update, new FormattableMarkup('User @user @state update field @field on comment @comment', [
260 '@user' => $set['user']->getUsername(),
261 '@state' => $may_update ? 'can' : 'cannot',
262 '@comment' => $set['comment']->getSubject(),
268 // Check create-only fields.
269 foreach ($this->createOnlyFields as $field) {
270 // Check view operation.
271 foreach ($permutations as $set) {
272 $may_view = $set['comment']->{$field}->access('view', $set['user']);
273 $may_update = $set['comment']->{$field}->access('edit', $set['user']);
274 $this->assertEqual($may_view, TRUE, new FormattableMarkup('User @user can view field @field on comment @comment', [
275 '@user' => $set['user']->getUsername(),
276 '@comment' => $set['comment']->getSubject(),
279 $this->assertEqual($may_update, $set['user']->hasPermission('post comments') && $set['comment']->isNew(), new FormattableMarkup('User @user @state update field @field on comment @comment', [
280 '@user' => $set['user']->getUsername(),
281 '@state' => $may_update ? 'can' : 'cannot',
282 '@comment' => $set['comment']->getSubject(),
288 // Check contact fields.
289 foreach ($this->contactFields as $field) {
290 // Check view operation.
291 foreach ($permutations as $set) {
292 $may_update = $set['comment']->{$field}->access('edit', $set['user']);
293 // To edit the 'mail' or 'name' field, either the user has the
294 // "administer comments" permissions or the user is anonymous and
295 // adding a new comment using a field that allows contact details.
296 $this->assertEqual($may_update, $set['user']->hasPermission('administer comments') || (
297 $set['user']->isAnonymous() &&
298 $set['comment']->isNew() &&
299 $set['user']->hasPermission('post comments') &&
300 $set['comment']->getFieldName() == 'comment_other'
301 ), new FormattableMarkup('User @user @state update field @field on comment @comment', [
302 '@user' => $set['user']->getUsername(),
303 '@state' => $may_update ? 'can' : 'cannot',
304 '@comment' => $set['comment']->getSubject(),
309 foreach ($permutations as $set) {
310 // Check no view-access to mail field for other than admin.
311 $may_view = $set['comment']->mail->access('view', $set['user']);
312 $this->assertEqual($may_view, $set['user']->hasPermission('administer comments'));