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