public function EntityReferenceSelectionAccessTest::testCommentHandler

Test the comment-specific overrides of the entity handler.

File

drupal/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionAccessTest.php, line 338
Contains \Drupal\entity_reference\Tests\EntityReferenceSelectionAccessTest.

Class

EntityReferenceSelectionAccessTest
Tests the Entity Reference Selection plugin.

Namespace

Drupal\entity_reference\Tests

Code

public function testCommentHandler() {

  // Build a fake field instance.
  $field = array(
    'translatable' => FALSE,
    'entity_types' => array(),
    'settings' => array(
      'target_type' => 'comment',
    ),
    'field_name' => 'test_field',
    'type' => 'entity_reference',
    'cardinality' => '1',
  );
  $instance = array(
    'settings' => array(
      'handler' => 'default',
      'handler_settings' => array(
        'target_bundles' => array(),
      ),
    ),
  );

  // 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;
  }
  $comment_values = array(
    'published_published' => array(
      'nid' => $nodes['published']->nid,
      'uid' => 1,
      'cid' => NULL,
      'pid' => 0,
      'status' => COMMENT_PUBLISHED,
      'subject' => 'Comment Published <&>',
      'language' => Language::LANGCODE_NOT_SPECIFIED,
    ),
    'published_unpublished' => array(
      'nid' => $nodes['published']->nid,
      'uid' => 1,
      'cid' => NULL,
      'pid' => 0,
      'status' => COMMENT_NOT_PUBLISHED,
      'subject' => 'Comment Unpublished <&>',
      'language' => Language::LANGCODE_NOT_SPECIFIED,
    ),
    'unpublished_published' => array(
      'nid' => $nodes['unpublished']->nid,
      'uid' => 1,
      'cid' => NULL,
      'pid' => 0,
      'status' => COMMENT_NOT_PUBLISHED,
      'subject' => 'Comment Published on Unpublished node <&>',
      'language' => Language::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] = check_plain($comment
      ->label());
  }

  // Test as a non-admin.
  $normal_user = $this
    ->drupalCreateUser(array(
    'access content',
    'access comments',
  ));
  $GLOBALS['user'] = $normal_user;
  $referencable_tests = array(
    array(
      'arguments' => array(
        array(
          NULL,
          'CONTAINS',
        ),
      ),
      'result' => array(
        'comment_node_article' => array(
          $comments['published_published']->cid->value => $comment_labels['published_published'],
        ),
      ),
    ),
    array(
      'arguments' => array(
        array(
          'Published',
          'CONTAINS',
        ),
      ),
      'result' => array(
        'comment_node_article' => 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
    ->assertReferencable($field, $instance, $referencable_tests, 'Comment handler');

  // Test as a comment admin.
  $admin_user = $this
    ->drupalCreateUser(array(
    'access content',
    'access comments',
    'administer comments',
  ));
  $GLOBALS['user'] = $admin_user;
  $referencable_tests = array(
    array(
      'arguments' => array(
        array(
          NULL,
          'CONTAINS',
        ),
      ),
      'result' => array(
        'comment_node_article' => array(
          $comments['published_published']->cid->value => $comment_labels['published_published'],
          $comments['published_unpublished']->cid->value => $comment_labels['published_unpublished'],
        ),
      ),
    ),
  );
  $this
    ->assertReferencable($field, $instance, $referencable_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',
  ));
  $GLOBALS['user'] = $admin_user;
  $referencable_tests = array(
    array(
      'arguments' => array(
        array(
          NULL,
          'CONTAINS',
        ),
      ),
      'result' => array(
        'comment_node_article' => 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
    ->assertReferencable($field, $instance, $referencable_tests, 'Comment handler (comment + node admin)');
}