You are here

protected function CommentLinkBuilderTest::getMockNode in Drupal 9

Same name and namespace in other branches
  1. 8 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::getLinkCombinations in core/modules/comment/tests/src/Unit/CommentLinkBuilderTest.php
Data provider for ::testCommentLinkBuilder.

File

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

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;
}