You are here

public function EntityReferenceSelectionAccessTest::testCommentHandler in Zircon Profile 8

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

Test the comment-specific overrides of the entity handler.

File

core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php, line 343
Contains \Drupal\system\Tests\Entity\EntityReferenceSelection\EntityReferenceSelectionAccessTest.

Class

EntityReferenceSelectionAccessTest
Tests for the base handlers provided by Entity Reference.

Namespace

Drupal\system\Tests\Entity\EntityReferenceSelection

Code

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

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

  // Create comment field on article.
  $this
    ->addDefaultCommentField('node', 'article');
  $comment_values = array(
    'published_published' => array(
      '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' => array(
      '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' => array(
      '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 = array();
  $comment_labels = array();
  foreach ($comment_values as $key => $values) {
    $comment = entity_create('comment', $values);
    $comment
      ->save();
    $comments[$key] = $comment;
    $comment_labels[$key] = Html::escape($comment
      ->label());
  }

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

  // Test as a comment admin.
  $admin_user = $this
    ->drupalCreateUser(array(
    'access content',
    'access comments',
    'administer comments',
  ));
  \Drupal::currentUser()
    ->setAccount($admin_user);
  $referenceable_tests = array(
    array(
      'arguments' => array(
        array(
          NULL,
          'CONTAINS',
        ),
      ),
      'result' => array(
        'comment' => array(
          $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
    ->drupalCreateUser(array(
    'access content',
    'access comments',
    'administer comments',
    'bypass node access',
  ));
  \Drupal::currentUser()
    ->setAccount($admin_user);
  $referenceable_tests = array(
    array(
      'arguments' => array(
        array(
          NULL,
          'CONTAINS',
        ),
      ),
      'result' => array(
        'comment' => array(
          $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)');
}