You are here

public function FieldDisplayTest::testFieldVisualHidden in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Kernel/FieldDisplayTest.php \Drupal\Tests\field\Kernel\FieldDisplayTest::testFieldVisualHidden()
  2. 9 core/modules/field/tests/src/Kernel/FieldDisplayTest.php \Drupal\Tests\field\Kernel\FieldDisplayTest::testFieldVisualHidden()

Tests that visually hidden works with core.

File

core/modules/field/tests/src/Kernel/FieldDisplayTest.php, line 102

Class

FieldDisplayTest
Tests Field display.

Namespace

Drupal\Tests\field\Kernel

Code

public function testFieldVisualHidden() {
  $value = $this
    ->randomMachineName();

  // Set the formatter to link to the entity.
  $this->display
    ->setComponent($this->fieldName, [
    'type' => 'string',
    'label' => 'visually_hidden',
    'settings' => [],
  ])
    ->save();
  $entity = EntityTestRev::create([]);
  $entity->{$this->fieldName}->value = $value;
  $entity
    ->save();
  $build = $this->display
    ->build($entity);
  $renderer = \Drupal::service('renderer');
  $content = $renderer
    ->renderPlain($build);
  $this
    ->setRawContent((string) $content);
  $css_selector_converter = new CssSelectorConverter();
  $elements = $this
    ->xpath($css_selector_converter
    ->toXPath('.visually-hidden'));
  $this
    ->assertCount(1, $elements, $content);
}