You are here

public function FileDownloadLinkTokenTest::testFormatterDeltaTokens in File Download Link 8

Test that tokens work right for cardinality != 1.

File

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

Class

FileDownloadLinkTokenTest
Class for testing file_download_link formatter with tokens.

Namespace

Drupal\Tests\file_download_link\Kernel

Code

public function testFormatterDeltaTokens() {
  $settings = [
    'link_text' => '[node:field_image:alt]',
    '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_delta_0 = [
    '#type' => 'link',
    '#title' => 'This alt text is for the first image.',
    '#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' => [],
  ];
  $expected_delta_1 = [
    '#type' => 'link',
    '#title' => "When delta is 1 we should see this alt text. Let's add special chars & test them!",
    '#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_delta_0, $render[0]);
  Assert::assertEquals($expected_delta_1, $render[1]);
}