View source
<?php
namespace Drupal\comment\Tests;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Entity\CommentType;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\simpletest\TestBase;
use Drupal\system\Tests\Entity\EntityUnitTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
class CommentFieldAccessTest extends EntityUnitTestBase {
use CommentTestTrait;
public static $modules = array(
'comment',
'entity_test',
'user',
);
protected $administrativeFields = array(
'uid',
'status',
'created',
);
protected $readOnlyFields = array(
'changed',
'hostname',
'cid',
'thread',
);
protected $createOnlyFields = [
'uuid',
'pid',
'comment_type',
'entity_id',
'entity_type',
'field_name',
];
protected $contactFields = array(
'name',
'mail',
'homepage',
);
protected function setUp() {
parent::setUp();
$this
->installConfig(array(
'user',
'comment',
));
$this
->installSchema('comment', array(
'comment_entity_statistics',
));
}
public function testAccessToAdministrativeFields() {
$comment_type = CommentType::create([
'id' => 'comment',
'label' => 'Default comments',
'description' => 'Default comment field',
'target_entity_type_id' => 'entity_test',
]);
$comment_type
->save();
$host = EntityTest::create();
$host
->save();
$comment_admin_user = $this
->createUser([
'uid' => 2,
'name' => 'admin',
], [
'administer comments',
'access comments',
]);
$comment_enabled_user = $this
->createUser([
'name' => 'enabled',
], [
'post comments',
'skip comment approval',
'edit own comments',
'access comments',
]);
$comment_no_edit_user = $this
->createUser([
'name' => 'no edit',
], [
'post comments',
'skip comment approval',
'access comments',
]);
$comment_disabled_user = $this
->createUser([
'name' => 'disabled',
], [
'access content',
]);
$role = Role::load(RoleInterface::ANONYMOUS_ID);
$role
->grantPermission('post comments')
->save();
$anonymous_user = new AnonymousUserSession();
$this
->addDefaultCommentField('entity_test', 'entity_test', 'comment');
$this
->addDefaultCommentField('entity_test', 'entity_test', 'comment_other');
$instance = FieldConfig::loadByName('entity_test', 'entity_test', 'comment_other');
$instance
->setSetting('anonymous', COMMENT_ANONYMOUS_MAY_CONTACT);
$instance
->save();
$comment1 = Comment::create([
'entity_type' => 'entity_test',
'name' => 'Tony',
'hostname' => 'magic.example.com',
'mail' => 'tonythemagicalpony@example.com',
'subject' => 'Bruce the Mesopotamian moose',
'entity_id' => $host
->id(),
'comment_type' => 'comment',
'field_name' => 'comment',
'pid' => 0,
'uid' => 0,
'status' => 1,
]);
$comment1
->save();
$comment2 = Comment::create([
'entity_type' => 'entity_test',
'hostname' => 'magic.example.com',
'subject' => 'Brian the messed up lion',
'entity_id' => $host
->id(),
'comment_type' => 'comment',
'field_name' => 'comment',
'status' => 1,
'pid' => 0,
'uid' => $comment_enabled_user
->id(),
]);
$comment2
->save();
$comment3 = Comment::create([
'entity_type' => 'entity_test',
'hostname' => 'magic.example.com',
'status' => 0,
'subject' => 'Gail the minky whale',
'entity_id' => $host
->id(),
'comment_type' => 'comment',
'field_name' => 'comment_other',
'pid' => $comment2
->id(),
'uid' => $comment_no_edit_user
->id(),
]);
$comment3
->save();
$comment4 = Comment::create([
'entity_type' => 'entity_test',
'hostname' => 'magic.example.com',
'status' => 0,
'subject' => 'Daniel the Cocker-Spaniel',
'entity_id' => $host
->id(),
'comment_type' => 'comment',
'field_name' => 'comment_other',
'pid' => 0,
'uid' => $anonymous_user
->id(),
]);
$combinations = [
'comment' => [
$comment1,
$comment2,
$comment3,
$comment4,
],
'user' => [
$comment_admin_user,
$comment_enabled_user,
$comment_no_edit_user,
$comment_disabled_user,
$anonymous_user,
],
];
$permutations = TestBase::generatePermutations($combinations);
foreach ($this->administrativeFields as $field) {
foreach ($permutations as $set) {
$may_view = $set['comment']->{$field}
->access('view', $set['user']);
$may_update = $set['comment']->{$field}
->access('edit', $set['user']);
$this
->assertTrue($may_view, SafeMarkup::format('User @user can view field @field on comment @comment', [
'@user' => $set['user']
->getUsername(),
'@comment' => $set['comment']
->getSubject(),
'@field' => $field,
]));
$this
->assertEqual($may_update, $set['user']
->hasPermission('administer comments'), SafeMarkup::format('User @user @state update field @field on comment @comment', [
'@user' => $set['user']
->getUsername(),
'@state' => $may_update ? 'can' : 'cannot',
'@comment' => $set['comment']
->getSubject(),
'@field' => $field,
]));
}
}
foreach ($permutations as $set) {
$may_update = $set['comment']
->access('update', $set['user']) && $set['comment']->subject
->access('edit', $set['user']);
$this
->assertEqual($may_update, $set['user']
->hasPermission('administer comments') || $set['user']
->hasPermission('edit own comments') && $set['user']
->id() == $set['comment']
->getOwnerId(), SafeMarkup::format('User @user @state update field subject on comment @comment', [
'@user' => $set['user']
->getUsername(),
'@state' => $may_update ? 'can' : 'cannot',
'@comment' => $set['comment']
->getSubject(),
]));
}
foreach ($this->readOnlyFields as $field) {
foreach ($permutations as $set) {
$may_view = $set['comment']->{$field}
->access('view', $set['user']);
$may_update = $set['comment']->{$field}
->access('edit', $set['user']);
if ($field === 'hostname') {
$view_access = FALSE;
$state = 'cannot';
}
else {
$view_access = TRUE;
$state = 'can';
}
$this
->assertEqual($may_view, $view_access, SafeMarkup::format('User @user @state view field @field on comment @comment', [
'@user' => $set['user']
->getUsername(),
'@comment' => $set['comment']
->getSubject(),
'@field' => $field,
'@state' => $state,
]));
$this
->assertFalse($may_update, SafeMarkup::format('User @user @state update field @field on comment @comment', [
'@user' => $set['user']
->getUsername(),
'@state' => $may_update ? 'can' : 'cannot',
'@comment' => $set['comment']
->getSubject(),
'@field' => $field,
]));
}
}
foreach ($this->createOnlyFields as $field) {
foreach ($permutations as $set) {
$may_view = $set['comment']->{$field}
->access('view', $set['user']);
$may_update = $set['comment']->{$field}
->access('edit', $set['user']);
$this
->assertEqual($may_view, TRUE, SafeMarkup::format('User @user can view field @field on comment @comment', [
'@user' => $set['user']
->getUsername(),
'@comment' => $set['comment']
->getSubject(),
'@field' => $field,
]));
$this
->assertEqual($may_update, $set['user']
->hasPermission('post comments') && $set['comment']
->isNew(), SafeMarkup::format('User @user @state update field @field on comment @comment', [
'@user' => $set['user']
->getUsername(),
'@state' => $may_update ? 'can' : 'cannot',
'@comment' => $set['comment']
->getSubject(),
'@field' => $field,
]));
}
}
foreach ($this->contactFields as $field) {
foreach ($permutations as $set) {
$may_update = $set['comment']->{$field}
->access('edit', $set['user']);
$this
->assertEqual($may_update, $set['user']
->hasPermission('administer comments') || $set['user']
->isAnonymous() && $set['comment']
->isNew() && $set['user']
->hasPermission('post comments') && $set['comment']
->getFieldName() == 'comment_other', SafeMarkup::format('User @user @state update field @field on comment @comment', [
'@user' => $set['user']
->getUsername(),
'@state' => $may_update ? 'can' : 'cannot',
'@comment' => $set['comment']
->getSubject(),
'@field' => $field,
]));
}
}
foreach ($permutations as $set) {
$may_view = $set['comment']->mail
->access('view', $set['user']);
$this
->assertEqual($may_view, $set['user']
->hasPermission('administer comments'));
}
}
}