You are here

public function PrintableLinkBuilderTest::testBuildLinks in Printer and PDF versions for Drupal 8+ 8

Same name and namespace in other branches
  1. 2.x tests/src/Unit/PrintableLinkBuilderTest.php \Drupal\Tests\printable\Unit\PrintableLinkBuilderTest::testBuildLinks()

Tests generating the render array of printable links.

@covers PrintableLinkBuilder::BuildLinks

File

tests/src/Unit/PrintableLinkBuilderTest.php, line 32

Class

PrintableLinkBuilderTest
Tests the print format plugin.

Namespace

Drupal\Tests\printable\Unit

Code

public function testBuildLinks() {
  $definitions = [
    'foo' => [
      'title' => 'Foo',
    ],
    'bar' => [
      'title' => 'Bar',
    ],
  ];
  $entity_type = 'node';
  $entity_id = rand(1, 100);
  $config = $this
    ->getConfigFactoryStub([
    'printable.settings' => [
      'open_target_blank' => TRUE,
    ],
  ]);
  $printable_manager = $this
    ->getMockBuilder('Drupal\\printable\\PrintableFormatPluginManager')
    ->disableOriginalConstructor()
    ->getMock();
  $printable_manager
    ->expects($this
    ->once())
    ->method('getDefinitions')
    ->will($this
    ->returnValue($definitions));
  $link_builder = new PrintableLinkBuilder($config, $printable_manager);
  $entity = $this
    ->getMock('Drupal\\Core\\Entity\\EntityInterface');
  $entity
    ->expects($this
    ->exactly(2))
    ->method('getEntityTypeId')
    ->will($this
    ->returnValue($entity_type));
  $entity
    ->expects($this
    ->exactly(2))
    ->method('id')
    ->will($this
    ->returnValue($entity_id));
  $links = $link_builder
    ->buildLinks($entity);
  $this
    ->assertEquals(2, count($links));
  foreach ($definitions as $key => $definition) {
    $link = $links[$key];
    $this
      ->assertEquals($definition['title'], $link['title']);
    $this
      ->assertEquals(Url::fromRoute('printable.show_format.' . $entity_type, [
      'printable_format' => $key,
      'entity' => $entity_id,
    ]), $link['url']);
    $this
      ->assertEquals('_blank', $link['attributes']['target']);
  }
}