You are here

public function FileDownloadLinkTokenTest::testFormatterUserTokens in File Download Link 8

Test the formatter using a token from the user.

This is unikely to be used. But we ensure user info is cached right.

File

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

Class

FileDownloadLinkTokenTest
Class for testing file_download_link formatter with tokens.

Namespace

Drupal\Tests\file_download_link\Kernel

Code

public function testFormatterUserTokens() {
  $settings = [
    'link_text' => 'Download this, [current-user:uid]',
    'new_tab' => FALSE,
    'force_download' => FALSE,
  ];
  $render = $this->entity->field_image
    ->view([
    'type' => 'file_download_link',
    'label' => 'hidden',
    'settings' => $settings,
  ]);
  $expected_render = [
    '#type' => 'link',
    '#title' => 'Download this, 99',
    '#url' => Url::fromUri(file_create_url('public://file.png')),
    '#options' => [
      'attributes' => [
        'class' => [
          'file-download',
          'file-download-image',
          'file-download-png',
        ],
      ],
    ],
    '#cache' => [
      'tags' => [
        'file:1',
        'node:1',
        'user:99',
      ],
      'contexts' => [
        'user',
      ],
      'max-age' => -1,
    ],
    '#attached' => [],
  ];
  Assert::assertEquals($expected_render, $render[0]);
  $settings = [
    'link_text' => 'Download',
    'link_title' => 'You know you want it, [current-user:uid]',
    'new_tab' => FALSE,
    'force_download' => FALSE,
  ];
  $render = $this->entity->field_image
    ->view([
    'type' => 'file_download_link',
    'label' => 'hidden',
    'settings' => $settings,
  ]);
  $expected_render = [
    '#type' => 'link',
    '#title' => 'Download',
    '#url' => Url::fromUri(file_create_url('public://file.png')),
    '#options' => [
      'attributes' => [
        'class' => [
          'file-download',
          'file-download-image',
          'file-download-png',
        ],
        'title' => 'You know you want it, 99',
      ],
    ],
    '#cache' => [
      'tags' => [
        'file:1',
        'node:1',
        'user:99',
      ],
      'contexts' => [
        'user',
      ],
      'max-age' => -1,
    ],
    '#attached' => [],
  ];
  Assert::assertEquals($expected_render, $render[0]);
}