function HistoryCommentLinkBuilderTest::testCommentLinkBuilder

Tests the buildCommentedEntityLinks method.

@legacy-covers ::buildCommentedEntityLinks

Attributes

#[DataProvider('getLinkCombinations')]

Parameters

array $node_args: Arguments for the mock node.

array $context: Context for the links.

bool $has_access_comments: TRUE if the user has 'access comments' permission.

bool $history_exists: TRUE if the history module exists.

bool $has_post_comments: TRUE if the use has 'post comments' permission.

bool $is_anonymous: TRUE if the user is anonymous.

array $expected: Array of expected links keyed by link ID. Can be either string (link title) or array of link properties.

File

core/modules/history/tests/src/Unit/HistoryCommentLinkBuilderTest.php, line 133

Class

HistoryCommentLinkBuilderTest
This is duplicated from CommentLinkBuilderTest with history additions.

Namespace

Drupal\Tests\history\Unit

Code

public function testCommentLinkBuilder(array $node_args, $context, $has_access_comments, $history_exists, $has_post_comments, $is_anonymous, $expected) : void {
  $node = $this->getMockNode(...$node_args);
  $this->moduleHandler
    ->expects($this->any())
    ->method('moduleExists')
    ->with('history')
    ->willReturn($history_exists);
  $this->currentUser
    ->expects($this->any())
    ->method('hasPermission')
    ->willReturnMap([
    [
      'access comments',
      $has_access_comments,
    ],
    [
      'post comments',
      $has_post_comments,
    ],
  ]);
  $this->currentUser
    ->expects($this->any())
    ->method('isAuthenticated')
    ->willReturn(!$is_anonymous);
  $this->currentUser
    ->expects($this->any())
    ->method('isAnonymous')
    ->willReturn($is_anonymous);
  $links = $this->decoratedCommentLinkBuilder
    ->buildCommentedEntityLinks($node, $context);
  if (!empty($expected)) {
    if (!empty($links)) {
      foreach ($expected as $link => $detail) {
        if (is_array($detail)) {
          // Array of link attributes.
          foreach ($detail as $key => $value) {
            $this->assertEquals($value, $links['comment__comment']['#links'][$link][$key]);
          }
        }
        else {
          // Just the title.
          $this->assertEquals($detail, $links['comment__comment']['#links'][$link]['title']);
        }
      }
    }
    else {
      $this->fail('Expected links but found none.');
    }
  }
  else {
    $this->assertSame($links, $expected);
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.