3 namespace Drupal\Tests\field\Kernel;
5 use Drupal\entity_test\Entity\EntityTestRev;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\KernelTests\KernelTestBase;
9 use Symfony\Component\CssSelector\CssSelectorConverter;
12 * Tests Field display.
16 class FieldDisplayTest extends KernelTestBase {
23 public static $modules = [
31 * Test entity type name.
35 protected $entityType;
38 * Test entity bundle name.
52 * Entity view display.
54 * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
61 protected function setUp() {
64 // Configure the theme system.
65 $this->installConfig(['system', 'field']);
66 $this->installEntitySchema('entity_test_rev');
68 $this->entityType = 'entity_test_rev';
69 $this->bundle = $this->entityType;
70 $this->fieldName = mb_strtolower($this->randomMachineName());
72 $field_storage = FieldStorageConfig::create([
73 'field_name' => $this->fieldName,
74 'entity_type' => $this->entityType,
77 $field_storage->save();
79 $instance = FieldConfig::create([
80 'field_storage' => $field_storage,
81 'bundle' => $this->bundle,
82 'label' => $this->randomMachineName(),
87 'targetEntityType' => $this->entityType,
88 'bundle' => $this->bundle,
93 $this->display = \Drupal::entityTypeManager()
94 ->getStorage('entity_view_display')
96 $this->display->save();
100 * Tests that visually hidden works with core.
102 public function testFieldVisualHidden() {
103 $value = $this->randomMachineName();
105 // Set the formatter to link to the entity.
106 $this->display->setComponent($this->fieldName, [
108 'label' => 'visually_hidden',
112 $entity = EntityTestRev::create([]);
113 $entity->{$this->fieldName}->value = $value;
116 $build = $this->display->build($entity);
117 $renderer = \Drupal::service('renderer');
118 $content = $renderer->renderPlain($build);
119 $this->setRawContent((string) $content);
121 $css_selector_converter = new CssSelectorConverter();
122 $elements = $this->xpath($css_selector_converter->toXPath('.visually-hidden'));
123 $this->assertCount(1, $elements, $content);