You are here

public function FileDownloadLinkMediaTest::testFormatterMediaCustom in File Download Link 8

Test the formatter using custom settings.

File

tests/src/Kernel/FileDownloadLinkMediaTest.php, line 105

Class

FileDownloadLinkMediaTest
Class for testing file_download_link_media formatter.

Namespace

Drupal\Tests\file_download_link\Kernel

Code

public function testFormatterMediaCustom() {
  $settings = [
    'link_text' => '',
    'link_title' => 'Click to download',
    'new_tab' => FALSE,
    'force_download' => FALSE,
    'custom_classes' => 'Howdy! p@rtner',
  ];
  $render = $this->node->field_media
    ->view([
    'type' => 'file_download_link_media',
    'label' => 'hidden',
    'settings' => $settings,
  ]);

  // Check that the thing that will get rendered looks right.
  $expected_deep_render = [
    '#type' => 'link',
    '#title' => 'file.txt',
    '#url' => Url::fromUri(file_create_url('public://file.txt')),
    '#options' => [
      'attributes' => [
        'class' => [
          'file-download',
          'file-download-text',
          'file-download-plain',
          'Howdy',
          'prtner',
        ],
        'title' => 'Click to download',
      ],
    ],
    '#cache' => [
      'tags' => [
        'file:1',
      ],
      'contexts' => [],
      'max-age' => -1,
    ],
  ];
  Assert::assertEquals($expected_deep_render, $render[0][0], json_encode($render));

  // Check that the render array has the right cache data.
  $expected_cache = [
    'contexts' => [
      'user.permissions',
    ],
    'tags' => [
      'media:1',
    ],
    'max-age' => -1,
  ];
  Assert::assertEquals($expected_cache, $render[0]['#cache'], json_encode($render));
}