You are here

protected function FieldPermissionsCommentTest::checkCustomCommentField in Field Permissions 8.2

Same name and namespace in other branches
  1. 8 tests/src/Functional/FieldPermissionsCommentTest.php \Drupal\Tests\field_permissions\Functional\FieldPermissionsCommentTest::checkCustomCommentField()

Tests the comment field with custom permissions.

1 call to FieldPermissionsCommentTest::checkCustomCommentField()
FieldPermissionsCommentTest::testFieldPermissionComment in tests/src/Functional/FieldPermissionsCommentTest.php
Test field permissions on comments.

File

tests/src/Functional/FieldPermissionsCommentTest.php, line 258

Class

FieldPermissionsCommentTest
Test field permissions with the comment module and fields.

Namespace

Drupal\Tests\field_permissions\Functional

Code

protected function checkCustomCommentField() {
  $path = 'admin/structure/comment/manage/comment/fields/comment.comment.' . $this->fieldName;
  $permission = [];
  $this
    ->drupalLogin($this->adminUser);

  // Change custom permission view own field body.
  $perm = [
    'view own ' . $this->fieldName,
  ];
  $permission = $this
    ->grantCustomPermissions($this->limitUserRole, $perm, $permission);
  $this
    ->setCommentFieldPermissions(FieldPermissionTypeInterface::ACCESS_CUSTOM, $permission, $path);
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->limitedUser);

  // View your comment but not view field body comment post by admin.
  $this
    ->drupalGet('node/' . $this->node
    ->id());

  // Hide body comment post by Adminuser.
  $this
    ->assertSession()
    ->pageTextNotContains($this->fieldText);
  $this
    ->assertSession()
    ->pageTextContains($this->commentSubject);
  $this
    ->assertSession()
    ->pageTextContains('Limit User comment subject');
  $this
    ->assertSession()
    ->pageTextContains('Limit User comment body');

  // Edit your comment not accesss to body field.
  $this
    ->drupalGet('comment/2/edit');
  $this
    ->assertSession()
    ->pageTextNotContains('Limit User comment body');
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->adminUser);

  // Custom permission add edit_own field body.
  $perm = [
    'edit own ' . $this->fieldName,
  ];
  $permission = $this
    ->grantCustomPermissions($this->limitUserRole, $perm, $permission);
  $this
    ->setCommentFieldPermissions(FieldPermissionTypeInterface::ACCESS_CUSTOM, $permission, $path);
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->limitedUser);

  // Test edit your comment edit field body.
  $this
    ->drupalGet('comment/2/edit');
  $this
    ->assertSession()
    ->pageTextContains('Limit User comment body');
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->adminUser);

  // Add edit and view all comment.
  $perm = [
    'edit ' . $this->fieldName,
    'view ' . $this->fieldName,
  ];
  $permission = $this
    ->grantCustomPermissions($this->adminUserRole, $perm, $permission);
  $this
    ->setCommentFieldPermissions(FieldPermissionTypeInterface::ACCESS_CUSTOM, $permission, $path);

  // view.
  $this
    ->drupalGet('node/' . $this->node
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('Limit User comment body');

  // edit.
  $this
    ->drupalGet('comment/2/edit');
  $this
    ->assertSession()
    ->pageTextContains('Limit User comment body');
  $this
    ->drupalLogout();
}