3 namespace Drupal\Tests\field\Functional\Boolean;
5 use Drupal\entity_test\Entity\EntityTest;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\Tests\BrowserTestBase;
11 * Tests boolean field functionality.
15 class BooleanFieldTest extends BrowserTestBase {
22 public static $modules = [
26 'field_test_boolean_access_denied',
30 * A field to use in this test class.
32 * @var \Drupal\field\Entity\FieldStorageConfig
34 protected $fieldStorage;
37 * The field used in this test class.
39 * @var \Drupal\field\Entity\FieldConfig
46 protected function setUp() {
49 $this->drupalLogin($this->drupalCreateUser([
51 'administer entity_test content',
52 'administer entity_test form display',
53 'administer entity_test fields',
58 * Tests boolean field.
60 public function testBooleanField() {
61 $on = $this->randomMachineName();
62 $off = $this->randomMachineName();
63 $label = $this->randomMachineName();
65 // Create a field with settings to validate.
66 $field_name = mb_strtolower($this->randomMachineName());
67 $this->fieldStorage = FieldStorageConfig::create([
68 'field_name' => $field_name,
69 'entity_type' => 'entity_test',
72 $this->fieldStorage->save();
73 $this->field = FieldConfig::create([
74 'field_name' => $field_name,
75 'entity_type' => 'entity_test',
76 'bundle' => 'entity_test',
86 // Create a form display for the default form mode.
87 entity_get_form_display('entity_test', 'entity_test', 'default')
88 ->setComponent($field_name, [
89 'type' => 'boolean_checkbox',
92 // Create a display for the full view mode.
93 entity_get_display('entity_test', 'entity_test', 'full')
94 ->setComponent($field_name, [
99 // Display creation form.
100 $this->drupalGet('entity_test/add');
101 $this->assertFieldByName("{$field_name}[value]", '', 'Widget found.');
102 $this->assertText($this->field->label(), 'Uses field label by default.');
103 $this->assertNoRaw($on, 'Does not use the "On" label.');
105 // Submit and ensure it is accepted.
107 "{$field_name}[value]" => 1,
109 $this->drupalPostForm(NULL, $edit, t('Save'));
110 preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
112 $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
114 // Verify that boolean value is displayed.
115 $entity = EntityTest::load($id);
116 $this->drupalGet($entity->toUrl());
117 $this->assertRaw('<div class="field__item">' . $on . '</div>');
119 // Test with "On" label option.
120 entity_get_form_display('entity_test', 'entity_test', 'default')
121 ->setComponent($field_name, [
122 'type' => 'boolean_checkbox',
124 'display_label' => FALSE,
129 $this->drupalGet('entity_test/add');
130 $this->assertFieldByName("{$field_name}[value]", '', 'Widget found.');
131 $this->assertRaw($on);
132 $this->assertNoText($this->field->label());
134 // Test if we can change the on label.
135 $on = $this->randomMachineName();
137 'settings[on_label]' => $on,
139 $this->drupalPostForm('entity_test/structure/entity_test/fields/entity_test.entity_test.' . $field_name, $edit, t('Save settings'));
140 // Check if we see the updated labels in the creation form.
141 $this->drupalGet('entity_test/add');
142 $this->assertRaw($on);
144 // Go to the form display page and check if the default settings works as
146 $fieldEditUrl = 'entity_test/structure/entity_test/form-display';
147 $this->drupalGet($fieldEditUrl);
149 // Click on the widget settings button to open the widget settings form.
150 $this->drupalPostForm(NULL, [], $field_name . "_settings_edit");
153 'Use field label instead of the "On" label as the label.',
154 t('Display setting checkbox available.')
158 $edit = ['fields[' . $field_name . '][settings_edit_form][settings][display_label]' => 1];
159 $this->drupalPostForm(NULL, $edit, $field_name . "_plugin_settings_update");
160 $this->drupalPostForm(NULL, NULL, 'Save');
162 // Go again to the form display page and check if the setting
163 // is stored and has the expected effect.
164 $this->drupalGet($fieldEditUrl);
165 $this->assertText('Use field label: Yes', 'Checking the display settings checkbox updated the value.');
167 $this->drupalPostForm(NULL, [], $field_name . "_settings_edit");
169 'Use field label instead of the "On" label as the label.',
170 t('Display setting checkbox is available')
172 $this->getSession()->getPage()->hasCheckedField('fields[' . $field_name . '][settings_edit_form][settings][display_label]');
174 // Test the boolean field settings.
175 $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.' . $field_name);
176 $this->assertFieldById('edit-settings-on-label', $on);
177 $this->assertFieldById('edit-settings-off-label', $off);
183 public function testFormAccess() {
185 $off = 'boolean_off';
186 $label = 'boolean_label';
187 $field_name = 'boolean_name';
188 $this->fieldStorage = FieldStorageConfig::create([
189 'field_name' => $field_name,
190 'entity_type' => 'entity_test',
193 $this->fieldStorage->save();
194 $this->field = FieldConfig::create([
195 'field_name' => $field_name,
196 'entity_type' => 'entity_test',
197 'bundle' => 'entity_test',
204 $this->field->save();
206 // Create a form display for the default form mode.
207 entity_get_form_display('entity_test', 'entity_test', 'default')
208 ->setComponent($field_name, [
209 'type' => 'boolean_checkbox',
213 // Create a display for the full view mode.
214 entity_get_display('entity_test', 'entity_test', 'full')
215 ->setComponent($field_name, [
220 // Display creation form.
221 $this->drupalGet('entity_test/add');
222 $this->assertFieldByName("{$field_name}[value]");
224 // Should be posted OK.
225 $this->drupalPostForm(NULL, [], t('Save'));
226 preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
228 $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
230 // Tell the test module to disable access to the field.
231 \Drupal::state()->set('field.test_boolean_field_access_field', $field_name);
232 $this->drupalGet('entity_test/add');
233 // Field should not be there anymore.
234 $this->assertNoFieldByName("{$field_name}[value]");
235 // Should still be able to post the form.
236 $this->drupalPostForm(NULL, [], t('Save'));
237 preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
239 $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));