3 namespace Drupal\Tests\field\Kernel\Boolean;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
7 use Drupal\Core\Entity\FieldableEntityInterface;
8 use Drupal\entity_test\Entity\EntityTest;
9 use Drupal\field\Entity\FieldConfig;
10 use Drupal\field\Entity\FieldStorageConfig;
11 use Drupal\KernelTests\KernelTestBase;
14 * Tests the boolean formatter.
18 class BooleanFormatterTest extends KernelTestBase {
25 public static $modules = ['field', 'text', 'entity_test', 'user', 'system'];
30 protected $entityType;
43 * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
50 protected function setUp() {
53 $this->installConfig(['field']);
54 $this->installEntitySchema('entity_test');
56 $this->entityType = 'entity_test';
57 $this->bundle = $this->entityType;
58 $this->fieldName = Unicode::strtolower($this->randomMachineName());
60 $field_storage = FieldStorageConfig::create([
61 'field_name' => $this->fieldName,
62 'entity_type' => $this->entityType,
65 $field_storage->save();
67 $instance = FieldConfig::create([
68 'field_storage' => $field_storage,
69 'bundle' => $this->bundle,
70 'label' => $this->randomMachineName(),
74 $this->display = entity_get_display($this->entityType, $this->bundle, 'default')
75 ->setComponent($this->fieldName, [
79 $this->display->save();
83 * Renders fields of a given entity with a given display.
85 * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
86 * The entity object with attached fields to render.
87 * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
88 * The display to render the fields in.
91 * The rendered entity fields.
93 protected function renderEntityFields(FieldableEntityInterface $entity, EntityViewDisplayInterface $display) {
94 $content = $display->build($entity);
95 $content = $this->render($content);
100 * Tests boolean formatter output.
102 public function testBooleanFormatter() {
104 $data[] = [0, [], 'Off'];
105 $data[] = [1, [], 'On'];
107 $format = ['format' => 'enabled-disabled'];
108 $data[] = [0, $format, 'Disabled'];
109 $data[] = [1, $format, 'Enabled'];
111 $format = ['format' => 'unicode-yes-no'];
112 $data[] = [1, $format, '✔'];
113 $data[] = [0, $format, '✖'];
116 'format' => 'custom',
117 'format_custom_false' => 'FALSE',
118 'format_custom_true' => 'TRUE'
120 $data[] = [0, $format, 'FALSE'];
121 $data[] = [1, $format, 'TRUE'];
123 foreach ($data as $test_data) {
124 list($value, $settings, $expected) = $test_data;
126 $component = $this->display->getComponent($this->fieldName);
127 $component['settings'] = $settings;
128 $this->display->setComponent($this->fieldName, $component);
130 $entity = EntityTest::create([]);
131 $entity->{$this->fieldName}->value = $value;
133 // Verify that all HTML is escaped and newlines are retained.
134 $this->renderEntityFields($entity, $this->display);
135 $this->assertRaw($expected);