function CommentLinkBuilderTest::testCommentLinkBuilder

Same name in other branches
  1. 9 core/modules/comment/tests/src/Unit/CommentLinkBuilderTest.php \Drupal\Tests\comment\Unit\CommentLinkBuilderTest::testCommentLinkBuilder()
  2. 10 core/modules/comment/tests/src/Unit/CommentLinkBuilderTest.php \Drupal\Tests\comment\Unit\CommentLinkBuilderTest::testCommentLinkBuilder()
  3. 11.x core/modules/comment/tests/src/Unit/CommentLinkBuilderTest.php \Drupal\Tests\comment\Unit\CommentLinkBuilderTest::testCommentLinkBuilder()

Test the buildCommentedEntityLinks method.

@dataProvider getLinkCombinations

@covers ::buildCommentedEntityLinks

Parameters

\Drupal\node\NodeInterface|\PHPUnit\Framework\MockObject\MockObject $node: 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/comment/tests/src/Unit/CommentLinkBuilderTest.php, line 115

Class

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

Namespace

Drupal\Tests\comment\Unit

Code

public function testCommentLinkBuilder(NodeInterface $node, $context, $has_access_comments, $history_exists, $has_post_comments, $is_anonymous, $expected) {
    $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->commentLinkBuilder
        ->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.