b2fb19384c86c30f9a393f84030b791647199317
[yaffs-website] / BooleanFormatterTest.php
1 <?php
2
3 namespace Drupal\Tests\field\Kernel\Boolean;
4
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;
11
12 /**
13  * Tests the boolean formatter.
14  *
15  * @group field
16  */
17 class BooleanFormatterTest extends KernelTestBase {
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['field', 'text', 'entity_test', 'user', 'system'];
25
26   /**
27    * @var string
28    */
29   protected $entityType;
30
31   /**
32    * @var string
33    */
34   protected $bundle;
35
36   /**
37    * @var string
38    */
39   protected $fieldName;
40
41   /**
42    * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
43    */
44   protected $display;
45
46   /**
47    * {@inheritdoc}
48    */
49   protected function setUp() {
50     parent::setUp();
51
52     $this->installConfig(['field']);
53     $this->installEntitySchema('entity_test');
54
55     $this->entityType = 'entity_test';
56     $this->bundle = $this->entityType;
57     $this->fieldName = mb_strtolower($this->randomMachineName());
58
59     $field_storage = FieldStorageConfig::create([
60       'field_name' => $this->fieldName,
61       'entity_type' => $this->entityType,
62       'type' => 'boolean',
63     ]);
64     $field_storage->save();
65
66     $instance = FieldConfig::create([
67       'field_storage' => $field_storage,
68       'bundle' => $this->bundle,
69       'label' => $this->randomMachineName(),
70     ]);
71     $instance->save();
72
73     $this->display = entity_get_display($this->entityType, $this->bundle, 'default')
74       ->setComponent($this->fieldName, [
75         'type' => 'boolean',
76         'settings' => [],
77       ]);
78     $this->display->save();
79   }
80
81   /**
82    * Renders fields of a given entity with a given display.
83    *
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.
88    *
89    * @return string
90    *   The rendered entity fields.
91    */
92   protected function renderEntityFields(FieldableEntityInterface $entity, EntityViewDisplayInterface $display) {
93     $content = $display->build($entity);
94     $content = $this->render($content);
95     return $content;
96   }
97
98   /**
99    * Tests boolean formatter output.
100    */
101   public function testBooleanFormatter() {
102     $data = [];
103     $data[] = [0, [], 'Off'];
104     $data[] = [1, [], 'On'];
105
106     $format = ['format' => 'enabled-disabled'];
107     $data[] = [0, $format, 'Disabled'];
108     $data[] = [1, $format, 'Enabled'];
109
110     $format = ['format' => 'unicode-yes-no'];
111     $data[] = [1, $format, '✔'];
112     $data[] = [0, $format, '✖'];
113
114     $format = [
115       'format' => 'custom',
116       'format_custom_false' => 'FALSE',
117       'format_custom_true' => 'TRUE',
118     ];
119     $data[] = [0, $format, 'FALSE'];
120     $data[] = [1, $format, 'TRUE'];
121
122     foreach ($data as $test_data) {
123       list($value, $settings, $expected) = $test_data;
124
125       $component = $this->display->getComponent($this->fieldName);
126       $component['settings'] = $settings;
127       $this->display->setComponent($this->fieldName, $component);
128
129       $entity = EntityTest::create([]);
130       $entity->{$this->fieldName}->value = $value;
131
132       // Verify that all HTML is escaped and newlines are retained.
133       $this->renderEntityFields($entity, $this->display);
134       $this->assertRaw($expected);
135     }
136   }
137
138 }