function CommentLinkBuilderTest::getMockNode

Same name 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. 11.x 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 \Drupal\comment\CommentLinkBuilder @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.