You are here

public function FileDownloadLinkTokenTest::testFormatterNodeTokens in File Download Link 8

Test the formatter using a token from the node.

File

tests/src/Kernel/FileDownloadLinkTokenTest.php, line 95

Class

FileDownloadLinkTokenTest
Class for testing file_download_link formatter with tokens.

Namespace

Drupal\Tests\file_download_link\Kernel

Code

public function testFormatterNodeTokens() {
  $settings = [
    'link_text' => 'The image width is [node:field_image:width]',
    'new_tab' => FALSE,
    'force_download' => FALSE,
  ];
  $render = $this->entity->field_image
    ->view([
    'type' => 'file_download_link',
    'label' => 'hidden',
    'settings' => $settings,
  ]);
  $file = $this->entity->field_image
    ->referencedEntities()[0];
  $expected_render = [
    '#type' => 'link',
    '#title' => 'The image width is 88',
    '#url' => Url::fromUri(file_create_url('public://file.png')),
    '#options' => [
      'attributes' => [
        'class' => [
          'file-download',
          'file-download-image',
          'file-download-png',
        ],
      ],
    ],
    '#cache' => [
      'tags' => array_merge($file
        ->getCacheTags(), $this->entity
        ->getCacheTags()),
      'contexts' => [],
      'max-age' => -1,
    ],
    '#attached' => [],
  ];
  Assert::assertEquals($expected_render, $render[0]);
}