You are here

public function CommentViewAccessTest::testUserCanNotViewCommentWithoutPermission in Open Social 10.2.x

Same name and namespace in other branches
  1. 10.3.x modules/social_features/social_comment/tests/src/Kernel/CommentViewAccessTest.php \Drupal\Tests\social_comment\Kernel\CommentViewAccessTest::testUserCanNotViewCommentWithoutPermission()

Test that a user can not view comment without permission.

Regardless of published status.

File

modules/social_features/social_comment/tests/src/Kernel/CommentViewAccessTest.php, line 98

Class

CommentViewAccessTest
Tests comment view level access.

Namespace

Drupal\Tests\social_comment\Kernel

Code

public function testUserCanNotViewCommentWithoutPermission() : void {
  $this
    ->setUpCurrentUser([], [
    'access comments',
  ]);
  $this
    ->createComment($this->node, [
    'status' => CommentInterface::NOT_PUBLISHED,
  ]);
  $this
    ->createComment($this->node, [
    'status' => CommentInterface::PUBLISHED,
  ]);

  // Create another user to try and view the comment.
  $this
    ->setUpCurrentUser();
  $all_comments = $this->storage
    ->getQuery()
    ->accessCheck(FALSE)
    ->condition('entity_id', $this->node
    ->id())
    ->condition('comment_type', 'comment')
    ->execute();
  self::assertCount(2, $all_comments);
  $visible_comments = $this->storage
    ->getQuery()
    ->accessCheck(TRUE)
    ->condition('entity_id', $this->node
    ->id())
    ->condition('comment_type', 'comment')
    ->execute();
  self::assertCount(0, $visible_comments);
}