function CommentLinkBuilderTest::getMockNode

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

Builds a mock node based on given scenario.

Parameters

bool $has_field: TRUE if the node has the 'comment' field.

int $comment_status: One of CommentItemInterface::OPEN|HIDDEN|CLOSED

int $form_location: One of CommentItemInterface::FORM_BELOW|FORM_SEPARATE_PAGE

int $comment_count: Number of comments against the field.

Return value

\Drupal\node\NodeInterface|\PHPUnit\Framework\MockObject\MockObject Mock node for testing.

1 call to CommentLinkBuilderTest::getMockNode()
CommentLinkBuilderTest::testCommentLinkBuilder in core/modules/comment/tests/src/Unit/CommentLinkBuilderTest.php
Tests the buildCommentedEntityLinks method.

File

core/modules/comment/tests/src/Unit/CommentLinkBuilderTest.php, line 275

Class

CommentLinkBuilderTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21comment%21src%21CommentLinkBuilder.php/class/CommentLinkBuilder/11.x" title="Defines a class for building markup for comment links on a commented entity." class="local">\Drupal\comment\CommentLinkBuilder</a> @group comment

Namespace

Drupal\Tests\comment\Unit

Code

protected function getMockNode($has_field, $comment_status, $form_location, $comment_count) {
    $node = $this->createMock('\\Drupal\\node\\NodeInterface');
    $node->expects($this->any())
        ->method('hasField')
        ->willReturn($has_field);
    if (empty($this->timestamp)) {
        $this->timestamp = time();
    }
    $field_item = (object) [
        'status' => $comment_status,
        'comment_count' => $comment_count,
        'last_comment_timestamp' => $this->timestamp,
    ];
    $node->expects($this->any())
        ->method('get')
        ->with('comment')
        ->willReturn($field_item);
    $field_definition = $this->createMock('\\Drupal\\Core\\Field\\FieldDefinitionInterface');
    $field_definition->expects($this->any())
        ->method('getSetting')
        ->with('form_location')
        ->willReturn($form_location);
    $node->expects($this->any())
        ->method('getFieldDefinition')
        ->with('comment')
        ->willReturn($field_definition);
    $node->expects($this->any())
        ->method('language')
        ->willReturn('und');
    $node->expects($this->any())
        ->method('getEntityTypeId')
        ->willReturn('node');
    $node->expects($this->any())
        ->method('id')
        ->willReturn(1);
    $url = Url::fromRoute('node.view');
    $node->expects($this->any())
        ->method('toUrl')
        ->willReturn($url);
    return $node;
}

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