3 namespace Drupal\Tests\field\Kernel\Boolean;
5 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
6 use Drupal\Core\Entity\FieldableEntityInterface;
7 use Drupal\entity_test\Entity\EntityTest;
8 use Drupal\field\Entity\FieldConfig;
9 use Drupal\field\Entity\FieldStorageConfig;
10 use Drupal\KernelTests\KernelTestBase;
13 * Tests the boolean formatter.
17 class BooleanFormatterTest extends KernelTestBase {
24 public static $modules = ['field', 'text', 'entity_test', 'user', 'system'];
29 protected $entityType;
42 * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
49 protected function setUp() {
52 $this->installConfig(['field']);
53 $this->installEntitySchema('entity_test');
55 $this->entityType = 'entity_test';
56 $this->bundle = $this->entityType;
57 $this->fieldName = mb_strtolower($this->randomMachineName());
59 $field_storage = FieldStorageConfig::create([
60 'field_name' => $this->fieldName,
61 'entity_type' => $this->entityType,
64 $field_storage->save();
66 $instance = FieldConfig::create([
67 'field_storage' => $field_storage,
68 'bundle' => $this->bundle,
69 'label' => $this->randomMachineName(),
73 $this->display = entity_get_display($this->entityType, $this->bundle, 'default')
74 ->setComponent($this->fieldName, [
78 $this->display->save();
82 * Renders fields of a given entity with a given display.
84 * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
85 * The entity object with attached fields to render.
86 * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
87 * The display to render the fields in.
90 * The rendered entity fields.
92 protected function renderEntityFields(FieldableEntityInterface $entity, EntityViewDisplayInterface $display) {
93 $content = $display->build($entity);
94 $content = $this->render($content);
99 * Tests boolean formatter output.
101 public function testBooleanFormatter() {
103 $data[] = [0, [], 'Off'];
104 $data[] = [1, [], 'On'];
106 $format = ['format' => 'enabled-disabled'];
107 $data[] = [0, $format, 'Disabled'];
108 $data[] = [1, $format, 'Enabled'];
110 $format = ['format' => 'unicode-yes-no'];
111 $data[] = [1, $format, '✔'];
112 $data[] = [0, $format, '✖'];
115 'format' => 'custom',
116 'format_custom_false' => 'FALSE',
117 'format_custom_true' => 'TRUE',
119 $data[] = [0, $format, 'FALSE'];
120 $data[] = [1, $format, 'TRUE'];
122 foreach ($data as $test_data) {
123 list($value, $settings, $expected) = $test_data;
125 $component = $this->display->getComponent($this->fieldName);
126 $component['settings'] = $settings;
127 $this->display->setComponent($this->fieldName, $component);
129 $entity = EntityTest::create([]);
130 $entity->{$this->fieldName}->value = $value;
132 // Verify that all HTML is escaped and newlines are retained.
133 $this->renderEntityFields($entity, $this->display);
134 $this->assertRaw($expected);