You are here

public function entityReferenceSelectionAccessTest::testCommentHandler in Entity reference 8

Test the comment-specific overrides of the entity handler.

File

lib/Drupal/entityreference/Tests/entityReferenceSelectionAccessTest.php, line 294
Contains Drupal\entityreference\Tests\entityReferenceSelectionAccessTest.

Class

entityReferenceSelectionAccessTest
Test for Entity Reference handlers.

Namespace

Drupal\entityreference\Tests

Code

public function testCommentHandler() {

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

  // 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_NOT_SPECIFIED,
    ),
    'published_unpublished' => array(
      'nid' => $nodes['published']->nid,
      'uid' => 1,
      'cid' => NULL,
      'pid' => 0,
      'status' => COMMENT_NOT_PUBLISHED,
      'subject' => 'Comment Unpublished <&>',
      'language' => LANGUAGE_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_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(
        $comments['published_published']->cid => $comment_labels['published_published'],
      ),
    ),
    array(
      'arguments' => array(
        array(
          'Published',
          'CONTAINS',
        ),
      ),
      'result' => array(
        $comments['published_published']->cid => $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, $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(
        $comments['published_published']->cid => $comment_labels['published_published'],
        $comments['published_unpublished']->cid => $comment_labels['published_unpublished'],
      ),
    ),
  );
  $this
    ->assertReferencable($field, $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(
        $comments['published_published']->cid => $comment_labels['published_published'],
        $comments['published_unpublished']->cid => $comment_labels['published_unpublished'],
        $comments['unpublished_published']->cid => $comment_labels['unpublished_published'],
      ),
    ),
  );
  $this
    ->assertReferencable($field, $referencable_tests, 'Comment handler (comment + node admin)');
}