You are here

public function EntityReferenceSelectionAccessTest::testCommentHandler in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Functional/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php \Drupal\Tests\system\Functional\Entity\EntityReferenceSelection\EntityReferenceSelectionAccessTest::testCommentHandler()
  2. 9 core/modules/system/tests/src/Functional/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php \Drupal\Tests\system\Functional\Entity\EntityReferenceSelection\EntityReferenceSelectionAccessTest::testCommentHandler()

Tests the comment-specific overrides of the entity handler.

File

core/modules/system/tests/src/Functional/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php, line 405

Class

EntityReferenceSelectionAccessTest
Tests for the base handlers provided by Entity Reference.

Namespace

Drupal\Tests\system\Functional\Entity\EntityReferenceSelection

Code

public function testCommentHandler() {
  $selection_options = [
    'target_type' => 'comment',
    'handler' => 'default',
    'target_bundles' => NULL,
  ];

  // Build a set of test data.
  $this
    ->createContentType([
    'type' => 'article',
    'name' => 'Article',
  ]);
  $node_values = [
    'published' => [
      'type' => 'article',
      'status' => 1,
      'title' => 'Node published',
      'uid' => 1,
    ],
    'unpublished' => [
      'type' => 'article',
      'status' => 0,
      'title' => 'Node unpublished',
      'uid' => 1,
    ],
  ];
  $nodes = [];
  foreach ($node_values as $key => $values) {
    $node = Node::create($values);
    $node
      ->save();
    $nodes[$key] = $node;
  }

  // Create comment field on article.
  $this
    ->addDefaultCommentField('node', 'article');
  $comment_values = [
    'published_published' => [
      'entity_id' => $nodes['published']
        ->id(),
      'entity_type' => 'node',
      'field_name' => 'comment',
      'uid' => 1,
      'cid' => NULL,
      'pid' => 0,
      'status' => CommentInterface::PUBLISHED,
      'subject' => 'Comment Published <&>',
      'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ],
    'published_unpublished' => [
      'entity_id' => $nodes['published']
        ->id(),
      'entity_type' => 'node',
      'field_name' => 'comment',
      'uid' => 1,
      'cid' => NULL,
      'pid' => 0,
      'status' => CommentInterface::NOT_PUBLISHED,
      'subject' => 'Comment Unpublished <&>',
      'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ],
    'unpublished_published' => [
      'entity_id' => $nodes['unpublished']
        ->id(),
      'entity_type' => 'node',
      'field_name' => 'comment',
      'uid' => 1,
      'cid' => NULL,
      'pid' => 0,
      'status' => CommentInterface::NOT_PUBLISHED,
      'subject' => 'Comment Published on Unpublished node <&>',
      'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ],
  ];
  $comments = [];
  $comment_labels = [];
  foreach ($comment_values as $key => $values) {
    $comment = Comment::create($values);
    $comment
      ->save();
    $comments[$key] = $comment;
    $comment_labels[$key] = Html::escape($comment
      ->label());
  }

  // Test as a non-admin.
  $normal_user = $this
    ->createUser([
    'access content',
    'access comments',
  ]);
  $this
    ->setCurrentUser($normal_user);
  $referenceable_tests = [
    [
      'arguments' => [
        [
          NULL,
          'CONTAINS',
        ],
      ],
      'result' => [
        'comment' => [
          $comments['published_published']->cid->value => $comment_labels['published_published'],
        ],
      ],
    ],
    [
      'arguments' => [
        [
          'Published',
          'CONTAINS',
        ],
      ],
      'result' => [
        'comment' => [
          $comments['published_published']->cid->value => $comment_labels['published_published'],
        ],
      ],
    ],
    [
      'arguments' => [
        [
          'invalid comment',
          'CONTAINS',
        ],
      ],
      'result' => [],
    ],
    [
      'arguments' => [
        [
          'Comment Unpublished',
          'CONTAINS',
        ],
      ],
      'result' => [],
    ],
  ];
  $this
    ->assertReferenceable($selection_options, $referenceable_tests, 'Comment handler');

  // Test as a comment admin.
  $admin_user = $this
    ->createUser([
    'access content',
    'access comments',
    'administer comments',
  ]);
  $this
    ->setCurrentUser($admin_user);
  $referenceable_tests = [
    [
      'arguments' => [
        [
          NULL,
          'CONTAINS',
        ],
      ],
      'result' => [
        'comment' => [
          $comments['published_published']->cid->value => $comment_labels['published_published'],
          $comments['published_unpublished']->cid->value => $comment_labels['published_unpublished'],
        ],
      ],
    ],
  ];
  $this
    ->assertReferenceable($selection_options, $referenceable_tests, 'Comment handler (comment admin)');

  // Test as a node and comment admin.
  $admin_user = $this
    ->createUser([
    'access content',
    'access comments',
    'administer comments',
    'bypass node access',
  ]);
  $this
    ->setCurrentUser($admin_user);
  $referenceable_tests = [
    [
      'arguments' => [
        [
          NULL,
          'CONTAINS',
        ],
      ],
      'result' => [
        'comment' => [
          $comments['published_published']->cid->value => $comment_labels['published_published'],
          $comments['published_unpublished']->cid->value => $comment_labels['published_unpublished'],
          $comments['unpublished_published']->cid->value => $comment_labels['unpublished_published'],
        ],
      ],
    ],
  ];
  $this
    ->assertReferenceable($selection_options, $referenceable_tests, 'Comment handler (comment + node admin)');
}