You are here

public function CommentLinkBuilderTest::getLinkCombinations in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/comment/tests/src/Unit/CommentLinkBuilderTest.php \Drupal\Tests\comment\Unit\CommentLinkBuilderTest::getLinkCombinations()

Data provider for ::testCommentLinkBuilder.

File

core/modules/comment/tests/src/Unit/CommentLinkBuilderTest.php, line 162
Contains \Drupal\Tests\comment\Unit\CommentLinkBuilderTest.

Class

CommentLinkBuilderTest
@coversDefaultClass \Drupal\comment\CommentLinkBuilder @group comment

Namespace

Drupal\Tests\comment\Unit

Code

public function getLinkCombinations() {
  $cases = array();

  // No links should be created if the entity doesn't have the field.
  $cases[] = array(
    $this
      ->getMockNode(FALSE, CommentItemInterface::OPEN, CommentItemInterface::FORM_BELOW, 1),
    array(
      'view_mode' => 'teaser',
    ),
    TRUE,
    TRUE,
    TRUE,
    TRUE,
    array(),
  );
  foreach (array(
    'search_result',
    'search_index',
    'print',
  ) as $view_mode) {

    // Nothing should be output in these view modes.
    $cases[] = array(
      $this
        ->getMockNode(TRUE, CommentItemInterface::OPEN, CommentItemInterface::FORM_BELOW, 1),
      array(
        'view_mode' => $view_mode,
      ),
      TRUE,
      TRUE,
      TRUE,
      TRUE,
      array(),
    );
  }

  // All other combinations.
  $combinations = array(
    'is_anonymous' => array(
      FALSE,
      TRUE,
    ),
    'comment_count' => array(
      0,
      1,
    ),
    'has_access_comments' => array(
      0,
      1,
    ),
    'history_exists' => array(
      FALSE,
      TRUE,
    ),
    'has_post_comments' => array(
      0,
      1,
    ),
    'form_location' => array(
      CommentItemInterface::FORM_BELOW,
      CommentItemInterface::FORM_SEPARATE_PAGE,
    ),
    'comments' => array(
      CommentItemInterface::OPEN,
      CommentItemInterface::CLOSED,
      CommentItemInterface::HIDDEN,
    ),
    'view_mode' => array(
      'teaser',
      'rss',
      'full',
    ),
  );
  $permutations = TestBase::generatePermutations($combinations);
  foreach ($permutations as $combination) {
    $case = array(
      $this
        ->getMockNode(TRUE, $combination['comments'], $combination['form_location'], $combination['comment_count']),
      array(
        'view_mode' => $combination['view_mode'],
      ),
      $combination['has_access_comments'],
      $combination['history_exists'],
      $combination['has_post_comments'],
      $combination['is_anonymous'],
    );
    $expected = array();

    // When comments are enabled in teaser mode, and comments exist, and the
    // user has access - we can output the comment count.
    if ($combination['comments'] && $combination['view_mode'] == 'teaser' && $combination['comment_count'] && $combination['has_access_comments']) {
      $expected['comment-comments'] = '1 comment';

      // And if history module exists, we can show a 'new comments' link.
      if ($combination['history_exists']) {
        $expected['comment-new-comments'] = '';
      }
    }

    // All view modes other than RSS.
    if ($combination['view_mode'] != 'rss') {

      // Where commenting is open.
      if ($combination['comments'] == CommentItemInterface::OPEN) {

        // And the user has post-comments permission.
        if ($combination['has_post_comments']) {

          // If the view mode is teaser, or the user can access comments and
          // comments exist or the form is on a separate page.
          if ($combination['view_mode'] == 'teaser' || $combination['has_access_comments'] && $combination['comment_count'] || $combination['form_location'] == CommentItemInterface::FORM_SEPARATE_PAGE) {

            // There should be a add comment link.
            $expected['comment-add'] = array(
              'title' => 'Add new comment',
            );
            if ($combination['form_location'] == CommentItemInterface::FORM_BELOW) {

              // On the same page.
              $expected['comment-add']['url'] = Url::fromRoute('node.view');
            }
            else {

              // On a separate page.
              $expected['comment-add']['url'] = Url::fromRoute('comment.reply', [
                'entity_type' => 'node',
                'entity' => 1,
                'field_name' => 'comment',
              ]);
            }
          }
        }
        elseif ($combination['is_anonymous']) {

          // Anonymous users get the forbidden message if the can't post
          // comments.
          $expected['comment-forbidden'] = "Can't let you do that Dave.";
        }
      }
    }
    $case[] = $expected;
    $cases[] = $case;
  }
  return $cases;
}