You are here

public function LinkAllyFormatterTest::testLinkAllyFormatterLinkField in Element Class Formatter 8

Test formatter with link field.

@dataProvider providerLinkText

File

tests/src/Functional/LinkAllyFormatterTest.php, line 21

Class

LinkAllyFormatterTest
Defines a class for testing LinkAllyFormatter.

Namespace

Drupal\Tests\element_class_formatter\Functional

Code

public function testLinkAllyFormatterLinkField(string $link_text = NULL, string $wrapper = '') {
  $field_config = $this
    ->createEntityField('link_ally_class', 'link', [
    'class' => self::TEST_CLASS,
    'link_text' => $link_text,
    'screenreader_text' => 'about [entity_test:name]',
    'tag' => $wrapper,
  ]);
  $anchor_text = $this
    ->randomMachineName();
  $entity = EntityTest::create([
    $field_config
      ->getName() => [
      [
        'uri' => 'https://drupal.org',
        'title' => $anchor_text,
      ],
    ],
  ]);
  $entity
    ->save();
  $this
    ->drupalGet($entity
    ->toUrl());
  $assert_session = $this
    ->assertSession();
  $selector = 'a.' . self::TEST_CLASS;
  if ($wrapper) {
    $selector = "{$wrapper} {$selector}";
  }
  $element = $assert_session
    ->elementExists('css', $selector);
  $assert_session
    ->elementTextContains('css', $selector, $link_text ?: $anchor_text);
  $assert_session
    ->elementExists('css', sprintf('span.visually-hidden:contains("about %s")', $entity
    ->label()), $element);
}