You are here

public function EntityReferenceHandlersTestCase::testCommentHandler in Entity reference 7

Test the comment-specific overrides of the entity handler.

File

tests/entityreference.handlers.test, line 348
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.
  $nodes = array(
    'published' => (object) array(
      'type' => 'article',
      'status' => 1,
      'title' => 'Node published',
      'uid' => 1,
    ),
    'unpublished' => (object) array(
      'type' => 'article',
      'status' => 0,
      'title' => 'Node unpublished',
      'uid' => 1,
    ),
  );
  foreach ($nodes as $node) {
    node_save($node);
  }
  $comments = array(
    'published_published' => (object) 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' => (object) 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' => (object) 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 ($comments as $key => $comment) {
    comment_save($comment);
    $comment_labels[$key] = check_plain($comment->subject);
  }

  // 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 => $comment_labels['published_published'],
        ),
      ),
    ),
    array(
      'arguments' => array(
        array(
          'Published',
          'CONTAINS',
        ),
      ),
      'result' => array(
        'comment_node_article' => 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(
        'comment_node_article' => 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(
        'comment_node_article' => 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)');
}