You are here

public function CommentLinkBuilderTest::getLinkCombinations in Drupal 8

Same name and namespace in other branches
  1. 9 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 160

Class

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

Namespace

Drupal\Tests\comment\Unit

Code

public function getLinkCombinations() {
  $cases = [];

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

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

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

    // 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'] = [
              '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;
}