function LinkFormatterTest::testLinkFormatter

Tests the link formatters.

@dataProvider providerLinkFormatter

Parameters

string $formatter: The name of the link formatter to test.

File

core/modules/link/tests/src/Kernel/LinkFormatterTest.php, line 94

Class

LinkFormatterTest
Tests the Field Formatter for the link field type.

Namespace

Drupal\Tests\link\Kernel

Code

public function testLinkFormatter(string $formatter) : void {
  $entity = $this->container
    ->get('entity_type.manager')
    ->getStorage($this->entityType)
    ->create([
    'name' => $this->randomMachineName(),
    $this->fieldName => [
      'uri' => 'https://www.drupal.org/',
      'title' => 'Hello world',
      'options' => [
        'attributes' => [
          'class' => 'classy',
          'onmouseover' => 'alert(document.cookie)',
        ],
      ],
    ],
  ]);
  $entity->save();
  $build = $entity->get($this->fieldName)
    ->view([
    'type' => $formatter,
  ]);
  $renderer = $this->container
    ->get('renderer');
  $renderer->renderRoot($build[0]);
  $output = (string) $build[0]['#markup'];
  $this->assertStringContainsString('<a href="https://www.drupal.org/" class="classy">', $output);
  $this->assertStringNotContainsString('onmouseover=', $output);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.