public function BooleanFieldTest::testBooleanField in Drupal 8
Same name and namespace in other branches
- 9 core/modules/field/tests/src/Functional/Boolean/BooleanFieldTest.php \Drupal\Tests\field\Functional\Boolean\BooleanFieldTest::testBooleanField()
Tests boolean field.
File
- core/
modules/ field/ tests/ src/ Functional/ Boolean/ BooleanFieldTest.php, line 65
Class
- BooleanFieldTest
- Tests boolean field functionality.
Namespace
Drupal\Tests\field\Functional\BooleanCode
public function testBooleanField() {
$on = $this
->randomMachineName();
$off = $this
->randomMachineName();
$label = $this
->randomMachineName();
// Create a field with settings to validate.
$field_name = mb_strtolower($this
->randomMachineName());
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'boolean',
]);
$this->fieldStorage
->save();
$this->field = FieldConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'label' => $label,
'required' => TRUE,
'settings' => [
'on_label' => $on,
'off_label' => $off,
],
]);
$this->field
->save();
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
$display_repository = \Drupal::service('entity_display.repository');
// Create a form display for the default form mode.
$display_repository
->getFormDisplay('entity_test', 'entity_test')
->setComponent($field_name, [
'type' => 'boolean_checkbox',
])
->save();
// Create a display for the full view mode.
$display_repository
->getViewDisplay('entity_test', 'entity_test', 'full')
->setComponent($field_name, [
'type' => 'boolean',
])
->save();
// Display creation form.
$this
->drupalGet('entity_test/add');
$this
->assertFieldByName("{$field_name}[value]", '', 'Widget found.');
$this
->assertText($this->field
->label(), 'Uses field label by default.');
$this
->assertNoRaw($on, 'Does not use the "On" label.');
// Submit and ensure it is accepted.
$edit = [
"{$field_name}[value]" => 1,
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\\d+)|', $this
->getUrl(), $match);
$id = $match[1];
$this
->assertText(t('entity_test @id has been created.', [
'@id' => $id,
]));
// Verify that boolean value is displayed.
$entity = EntityTest::load($id);
$this
->drupalGet($entity
->toUrl());
$this
->assertRaw('<div class="field__item">' . $on . '</div>');
// Test with "On" label option.
$display_repository
->getFormDisplay('entity_test', 'entity_test')
->setComponent($field_name, [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => FALSE,
],
])
->save();
$this
->drupalGet('entity_test/add');
$this
->assertFieldByName("{$field_name}[value]", '', 'Widget found.');
$this
->assertRaw($on);
$this
->assertNoText($this->field
->label());
// Test if we can change the on label.
$on = $this
->randomMachineName();
$edit = [
'settings[on_label]' => $on,
];
$this
->drupalPostForm('entity_test/structure/entity_test/fields/entity_test.entity_test.' . $field_name, $edit, t('Save settings'));
// Check if we see the updated labels in the creation form.
$this
->drupalGet('entity_test/add');
$this
->assertRaw($on);
// Go to the form display page and check if the default settings works as
// expected.
$fieldEditUrl = 'entity_test/structure/entity_test/form-display';
$this
->drupalGet($fieldEditUrl);
// Click on the widget settings button to open the widget settings form.
$this
->drupalPostForm(NULL, [], $field_name . "_settings_edit");
$this
->assertText('Use field label instead of the "On" label as the label.', t('Display setting checkbox available.'));
// Enable setting.
$edit = [
'fields[' . $field_name . '][settings_edit_form][settings][display_label]' => 1,
];
$this
->drupalPostForm(NULL, $edit, $field_name . "_plugin_settings_update");
$this
->drupalPostForm(NULL, NULL, 'Save');
// Go again to the form display page and check if the setting
// is stored and has the expected effect.
$this
->drupalGet($fieldEditUrl);
$this
->assertText('Use field label: Yes', 'Checking the display settings checkbox updated the value.');
$this
->drupalPostForm(NULL, [], $field_name . "_settings_edit");
$this
->assertText('Use field label instead of the "On" label as the label.', t('Display setting checkbox is available'));
$this
->getSession()
->getPage()
->hasCheckedField('fields[' . $field_name . '][settings_edit_form][settings][display_label]');
// Test the boolean field settings.
$this
->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.' . $field_name);
$this
->assertFieldById('edit-settings-on-label', $on);
$this
->assertFieldById('edit-settings-off-label', $off);
}