beaa6e6b47403e98e6ec41bea8a8ad9cc397991e
[yaffs-website] / web / core / modules / field / tests / src / Kernel / FieldDisplayTest.php
1 <?php
2
3 namespace Drupal\Tests\field\Kernel;
4
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;
10
11 /**
12  * Tests Field display.
13  *
14  * @group field
15  */
16 class FieldDisplayTest extends KernelTestBase {
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = [
24     'entity_test',
25     'field',
26     'system',
27     'user',
28   ];
29
30   /**
31    * Test entity type name.
32    *
33    * @var string
34    */
35   protected $entityType;
36
37   /**
38    * Test entity bundle name.
39    *
40    * @var string
41    */
42   protected $bundle;
43
44   /**
45    * Test field name.
46    *
47    * @var string
48    */
49   protected $fieldName;
50
51   /**
52    * Entity view display.
53    *
54    * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
55    */
56   protected $display;
57
58   /**
59    * {@inheritdoc}
60    */
61   protected function setUp() {
62     parent::setUp();
63
64     // Configure the theme system.
65     $this->installConfig(['system', 'field']);
66     $this->installEntitySchema('entity_test_rev');
67
68     $this->entityType = 'entity_test_rev';
69     $this->bundle = $this->entityType;
70     $this->fieldName = mb_strtolower($this->randomMachineName());
71
72     $field_storage = FieldStorageConfig::create([
73       'field_name' => $this->fieldName,
74       'entity_type' => $this->entityType,
75       'type' => 'string',
76     ]);
77     $field_storage->save();
78
79     $instance = FieldConfig::create([
80       'field_storage' => $field_storage,
81       'bundle' => $this->bundle,
82       'label' => $this->randomMachineName(),
83     ]);
84     $instance->save();
85
86     $values = [
87       'targetEntityType' => $this->entityType,
88       'bundle' => $this->bundle,
89       'mode' => 'default',
90       'status' => TRUE,
91     ];
92
93     $this->display = \Drupal::entityTypeManager()
94       ->getStorage('entity_view_display')
95       ->create($values);
96     $this->display->save();
97   }
98
99   /**
100    * Tests that visually hidden works with core.
101    */
102   public function testFieldVisualHidden() {
103     $value = $this->randomMachineName();
104
105     // Set the formatter to link to the entity.
106     $this->display->setComponent($this->fieldName, [
107       'type' => 'string',
108       'label' => 'visually_hidden',
109       'settings' => [],
110     ])->save();
111
112     $entity = EntityTestRev::create([]);
113     $entity->{$this->fieldName}->value = $value;
114     $entity->save();
115
116     $build = $this->display->build($entity);
117     $renderer = \Drupal::service('renderer');
118     $content = $renderer->renderPlain($build);
119     $this->setRawContent((string) $content);
120
121     $css_selector_converter = new CssSelectorConverter();
122     $elements = $this->xpath($css_selector_converter->toXPath('.visually-hidden'));
123     $this->assertCount(1, $elements, $content);
124   }
125
126 }