You are here

public function SmartReadMoreLinkUnitTest::testWhenTeaserDoesNotMatchFull in Smart read more link 8

Test no link is generated when teaser and full views match.

File

tests/src/Unit/SmartReadMoreLinkUnitTest.php, line 112

Class

SmartReadMoreLinkUnitTest
Unit tests of smart_read_more_link module.

Namespace

Drupal\Tests\smart_read_more_link\Unit

Code

public function testWhenTeaserDoesNotMatchFull() {
  $field_definition = $this
    ->getMockBuilder(FieldDefinitionInterface::class)
    ->getMock();
  $base_formatter = $this
    ->getMockBuilder(FormatterBase::class)
    ->disableOriginalConstructor()
    ->getMock();
  $base_formatter
    ->method('viewElements')
    ->willReturn([
    [
      '#markup' => 'anything',
    ],
  ]);
  $random_string1 = $this
    ->getRandomGenerator()
    ->string(200);
  $random_string2 = $this
    ->getRandomGenerator()
    ->string(100);
  $default_formatter = $this
    ->getMockBuilder(FormatterBase::class)
    ->disableOriginalConstructor()
    ->getMock();
  $default_formatter
    ->method('viewElements')
    ->willReturn([
    [
      '#markup' => 'otherthing',
    ],
  ]);
  $formatter_manager = $this
    ->getMockBuilder(PluginManagerInterface::class)
    ->getMock();
  $formatter_manager
    ->method('createInstance')
    ->willReturnCallback(function ($plugin_id) use ($default_formatter, $base_formatter) {
    if ($plugin_id === 'text_summary_or_trimmed') {
      return $base_formatter;
    }
    else {
      return $default_formatter;
    }
  });
  $renderer = $this
    ->getMockBuilder(RendererInterface::class)
    ->getMock();

  // Rendered view is constant.
  $renderer
    ->method('render')
    ->willReturnOnConsecutiveCalls($random_string1, $random_string2);
  $formatter = new SmartReadMoreLinkFormatter('smart_read_more_link', [], $field_definition, [], 'Field label', 'view_mode', [], $formatter_manager, $renderer);
  $entity = $this
    ->getMockBuilder(EntityBase::class)
    ->disableOriginalConstructor()
    ->getMock();
  $entity
    ->method('label')
    ->willReturn('Entity label');
  $url = $this
    ->getMockBuilder(Url::class)
    ->disableOriginalConstructor()
    ->getMock();
  $entity
    ->method('urlInfo')
    ->willReturn($url);
  $entity
    ->method('language')
    ->willReturn('und');
  $items = $this
    ->getMockBuilder(FieldItemListInterface::class)
    ->getMock();
  $items
    ->method('getEntity')
    ->willReturn($entity);
  $formatter
    ->setStringTranslation($this
    ->getStringTranslationStub());
  $result = $formatter
    ->viewElements($items, 'und');

  // @todo be more thorough about this
  $this
    ->assertNotEmpty($result[0]['links'], 'link in output');
}