1 entityreference.handlers.test public EntityReferenceHandlersTestCase::testCommentHandler()

Test the comment-specific overrides of the entity handler.

File

core/modules/entityreference/tests/entityreference.handlers.test, line 351
Contains EntityReferenceHandlersTestCase

Class

EntityReferenceHandlersTestCase
Test for Entity Reference handlers.

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.
  $build_nodes = array(
    'published' => array(
      'type' => 'post',
      'status' => 1,
      'title' => 'Node published',
      'uid' => 1,
    ),
    'unpublished' => array(
      'type' => 'post',
      'status' => 0,
      'title' => 'Node unpublished',
      'uid' => 1,
    ),
  );
  foreach ($build_nodes as $key => $settings) {
    $nodes[$key] = $this->backdropCreateNode($settings);
  }

  $build_comments = array(
    'published_published' => array(
      'nid' => $nodes['published']->nid,
      'uid' => 1,
      'cid' => NULL,
      'pid' => 0,
      'status' => COMMENT_PUBLISHED,
      'subject' => 'Comment Published <&>',
      'hostname' => ip_address(),
      'language' => LANGUAGE_NONE,
    ),
    'published_unpublished' => array(
      'nid' => $nodes['published']->nid,
      'uid' => 1,
      'cid' => NULL,
      'pid' => 0,
      'status' => COMMENT_NOT_PUBLISHED,
      'subject' => 'Comment Unpublished <&>',
      'hostname' => ip_address(),
      'language' => LANGUAGE_NONE,
    ),
    'unpublished_published' => array(
      'nid' => $nodes['unpublished']->nid,
      'uid' => 1,
      'cid' => NULL,
      'pid' => 0,
      'status' => COMMENT_NOT_PUBLISHED,
      'subject' => 'Comment Published on Unpublished node <&>',
      'hostname' => ip_address(),
      'language' => LANGUAGE_NONE,
    ),
  );

  $comment_labels = array();
  foreach ($build_comments as $key => $settings) {
    $comment = entity_create('comment', $settings);
    comment_save($comment);
    $comments[$key] = $comment;
    $comment_labels[$key] = check_plain($comment->subject);
  }

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

  // Test as a comment admin.
  $admin_user = $this->backdropCreateUser(array('access content', 'access comments', 'administer comments'));
  $GLOBALS['user'] = $admin_user;
  $referenceable_tests = array(
    array(
      'arguments' => array(
        array(NULL, 'CONTAINS'),
      ),
      'result' => array(
        'comment_node_post' => array(
          $comments['published_published']->cid => $comment_labels['published_published'],
          $comments['published_unpublished']->cid => $comment_labels['published_unpublished'],
        ),
      ),
    ),
  );
  $this->assertReferenceable($field, $referenceable_tests, 'Comment handler (comment admin)');

  // Test as a node and comment admin.
  $admin_user = $this->backdropCreateUser(array('access content', 'access comments', 'administer comments', 'bypass node access'));
  $GLOBALS['user'] = $admin_user;
  $referenceable_tests = array(
    array(
      'arguments' => array(
        array(NULL, 'CONTAINS'),
      ),
      'result' => array(
        'comment_node_post' => 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->assertReferenceable($field, $referenceable_tests, 'Comment handler (comment + node admin)');
}