View source
<?php
namespace Drupal\Tests\field_permissions\Functional;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field_permissions\Plugin\FieldPermissionTypeInterface;
class FieldPermissionsCommentTest extends FieldPermissionsTestBase {
public static $modules = [
'comment',
];
protected $commentSubject;
protected $entityDisplayRepository;
public function setUp() {
parent::setUp();
$permissions = [
'post comments',
'skip comment approval',
'access comments',
'edit own comments',
];
foreach ($permissions as $permission) {
$this->limitUserRole
->grantPermission($permission);
}
$this->limitUserRole
->save();
$this->webUserRole
->grantPermission('access comments')
->grantPermission('administer comments')
->save();
$this->fieldName = 'comment_body';
$this->commentSubject = 'Test subject comment';
$this->fieldText = 'A comment';
$this->entityDisplayRepository = $this->container
->get('entity_display.repository');
}
public function testFieldPermissionComment() {
$this
->addCommentField();
$this
->checkBaseCommentFieldFunctionality();
$this
->checkPrivateCommentField();
$this
->checkPrivateFieldPermission();
$this
->checkCustomCommentField();
}
protected function addCommentField() {
$entity_manager = \Drupal::entityTypeManager();
$bundle = 'article';
$comment_type_storage = $entity_manager
->getStorage('comment_type');
$comment_type_id = 'comment';
$entity_type = 'node';
$field_name = 'comment';
$comment_type_storage
->create([
'id' => $comment_type_id,
'label' => 'Comment',
'target_entity_type_id' => $entity_type,
'description' => 'Default comment field',
])
->save();
$entity_manager
->getStorage('field_storage_config')
->create([
'entity_type' => $entity_type,
'field_name' => $field_name,
'type' => 'comment',
'settings' => [
'comment_type' => $comment_type_id,
],
])
->save();
$entity_manager
->getStorage('field_config')
->create([
'label' => 'Comments',
'description' => '',
'field_name' => $field_name,
'entity_type' => $entity_type,
'bundle' => $bundle,
'required' => 1,
'default_value' => [
[
'status' => 2,
'cid' => 0,
'last_comment_name' => '',
'last_comment_timestamp' => 0,
'last_comment_uid' => 0,
],
],
])
->save();
$this->entityDisplayRepository
->getFormDisplay($entity_type, $bundle, 'default')
->setComponent($field_name, [
'type' => 'comment_default',
'weight' => 20,
])
->save();
$this->entityDisplayRepository
->getViewDisplay($entity_type, $bundle)
->setComponent($field_name, [
'label' => 'above',
'type' => 'comment_default',
'weight' => 20,
])
->save();
$field = $entity_manager
->getStorage('field_config')
->create([
'label' => 'Comment',
'bundle' => $comment_type_id,
'required' => TRUE,
'field_storage' => FieldStorageConfig::loadByName('comment', $this->fieldName),
]);
$field
->save();
$this->entityDisplayRepository
->getFormDisplay('comment', $comment_type_id, 'default')
->setComponent($this->fieldName, [
'type' => 'text_textarea',
])
->save();
$this->entityDisplayRepository
->getViewDisplay('comment', $comment_type_id)
->setComponent($this->fieldName, [
'label' => 'hidden',
'type' => 'text_default',
'weight' => 0,
])
->save();
}
protected function setCommentFieldPermissions($perm, array $custom_permission, $path) {
$this
->drupalGet($path);
if ($perm === FieldPermissionTypeInterface::ACCESS_PUBLIC || $perm === FieldPermissionTypeInterface::ACCESS_PRIVATE) {
$edit = [
'type' => $perm,
];
$this
->submitForm($edit, 'Save settings');
}
elseif ($perm === FieldPermissionTypeInterface::ACCESS_CUSTOM && !empty($custom_permission)) {
$custom_permission['type'] = $perm;
$this
->submitForm($custom_permission, 'Save settings');
}
}
protected function checkBaseCommentFieldFunctionality() {
$edit = [];
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('node/add/article');
$this->node = $this
->drupalCreateNode([
'type' => 'article',
'uid' => $this->limitedUser
->id(),
]);
$this
->drupalGet('node/' . $this->node
->id());
$edit['subject[0][value]'] = $this->commentSubject;
$edit[$this->fieldName . '[0][value]'] = $this->fieldText;
$this
->submitForm($edit, 'Save');
$this
->drupalGet('node/' . $this->node
->id());
$this
->assertSession()
->assertEscaped($this->fieldText);
$this
->assertSession()
->assertEscaped($this->commentSubject);
$this
->drupalLogout();
$this
->drupalLogin($this->limitedUser);
$this
->drupalGet('node/' . $this->node
->id());
$this
->assertSession()
->assertEscaped($this->fieldText);
$this
->assertSession()
->pageTextContains($this->commentSubject);
$edit = [];
$edit['subject[0][value]'] = 'Limit User comment subject';
$edit[$this->fieldName . '[0][value]'] = 'Limit User comment body';
$this
->submitForm($edit, 'Save');
$this
->drupalGet('node/' . $this->node
->id());
$this
->assertSession()
->pageTextContains('Limit User comment subject');
$this
->assertSession()
->pageTextContains('Limit User comment body');
$this
->drupalLogout();
}
protected function checkPrivateCommentField() {
$path = 'admin/structure/comment/manage/comment/fields/comment.comment.' . $this->fieldName;
$permission = [];
$this
->drupalLogin($this->adminUser);
$this->adminUserRole
->grantPermission('administer field permissions')
->save();
$this
->setCommentFieldPermissions(FieldPermissionTypeInterface::ACCESS_PRIVATE, $permission, $path);
$this
->drupalLogout();
$this
->drupalLogin($this->limitedUser);
$this
->drupalGet('node/' . $this->node
->id());
$this
->assertSession()
->pageTextContains($this->commentSubject);
$this
->assertSession()
->pageTextNotContains($this->fieldText);
$this
->assertSession()
->pageTextContains('Limit User comment subject');
$this
->assertSession()
->pageTextContains('Limit User comment body');
$this
->drupalGet('comment/2/edit');
$this
->assertSession()
->pageTextContains('Limit User comment body');
$this
->drupalLogin($this->webUser);
$this
->drupalGet('comment/2/edit');
$this
->assertSession()
->pageTextNotContains('Limit User comment body');
$this
->drupalLogout();
}
protected function checkCustomCommentField() {
$path = 'admin/structure/comment/manage/comment/fields/comment.comment.' . $this->fieldName;
$permission = [];
$this
->drupalLogin($this->adminUser);
$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);
$this
->drupalGet('node/' . $this->node
->id());
$this
->assertSession()
->pageTextNotContains($this->fieldText);
$this
->assertSession()
->pageTextContains($this->commentSubject);
$this
->assertSession()
->pageTextContains('Limit User comment subject');
$this
->assertSession()
->pageTextContains('Limit User comment body');
$this
->drupalGet('comment/2/edit');
$this
->assertSession()
->pageTextNotContains('Limit User comment body');
$this
->drupalLogout();
$this
->drupalLogin($this->adminUser);
$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);
$this
->drupalGet('comment/2/edit');
$this
->assertSession()
->pageTextContains('Limit User comment body');
$this
->drupalLogout();
$this
->drupalLogin($this->adminUser);
$perm = [
'edit ' . $this->fieldName,
'view ' . $this->fieldName,
];
$permission = $this
->grantCustomPermissions($this->adminUserRole, $perm, $permission);
$this
->setCommentFieldPermissions(FieldPermissionTypeInterface::ACCESS_CUSTOM, $permission, $path);
$this
->drupalGet('node/' . $this->node
->id());
$this
->assertSession()
->pageTextContains('Limit User comment body');
$this
->drupalGet('comment/2/edit');
$this
->assertSession()
->pageTextContains('Limit User comment body');
$this
->drupalLogout();
}
protected function checkPrivateFieldPermission() {
$path = 'admin/structure/comment/manage/comment/fields/comment.comment.' . $this->fieldName;
$permission = [];
$this
->drupalLogin($this->adminUser);
$this
->setCommentFieldPermissions(FieldPermissionTypeInterface::ACCESS_PRIVATE, $permission, $path);
$this
->drupalLogin($this->webUser);
$this
->drupalGet('node/' . $this->node
->id());
$this
->assertSession()
->pageTextNotContains('Limit User comment body');
$this
->drupalGet('comment/2/edit');
$this
->assertSession()
->pageTextNotContains('Limit User comment body');
$this->webUserRole
->grantPermission('access private fields')
->save();
$this
->drupalGet('node/' . $this->node
->id());
$this
->assertSession()
->pageTextContains('Limit User comment body');
$this
->drupalGet('comment/2/edit');
$this
->assertSession()
->pageTextContains('Limit User comment body');
$this
->drupalLogout();
}
}