function OptionsWidgetsTest::testSelectListSingle
Same name in other branches
- 9 core/modules/options/tests/src/Functional/OptionsWidgetsTest.php \Drupal\Tests\options\Functional\OptionsWidgetsTest::testSelectListSingle()
- 8.9.x core/modules/options/tests/src/Functional/OptionsWidgetsTest.php \Drupal\Tests\options\Functional\OptionsWidgetsTest::testSelectListSingle()
- 11.x core/modules/options/tests/src/Functional/OptionsWidgetsTest.php \Drupal\Tests\options\Functional\OptionsWidgetsTest::testSelectListSingle()
Tests the 'options_select' widget (single select).
File
-
core/
modules/ options/ tests/ src/ Functional/ OptionsWidgetsTest.php, line 276
Class
- OptionsWidgetsTest
- Tests the Options widgets.
Namespace
Drupal\Tests\options\FunctionalCode
public function testSelectListSingle() : void {
// Create an instance of the 'single value' field.
$field = FieldConfig::create([
'field_storage' => $this->card1,
'bundle' => 'entity_test',
'required' => TRUE,
]);
$field->save();
\Drupal::service('entity_display.repository')->getFormDisplay('entity_test', 'entity_test')
->setComponent($this->card1
->getName(), [
'type' => 'options_select',
])
->save();
// Create an entity.
$entity = EntityTest::create([
'user_id' => 1,
'name' => $this->randomMachineName(),
]);
$entity->save();
$entity_init = clone $entity;
// Display form.
$this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
// A required field without any value has a "none" option.
$option = $this->assertSession()
->optionExists('edit-card-1', '_none');
$this->assertSame('- Select a value -', $option->getText());
// With no field data, nothing is selected.
$this->assertTrue($this->assertSession()
->optionExists('card_1', '_none')
->isSelected());
$this->assertFalse($this->assertSession()
->optionExists('card_1', 0)
->isSelected());
$this->assertFalse($this->assertSession()
->optionExists('card_1', 1)
->isSelected());
$this->assertFalse($this->assertSession()
->optionExists('card_1', 2)
->isSelected());
$this->assertSession()
->responseContains('Some dangerous & unescaped markup');
// Submit form: select invalid 'none' option.
$edit = [
'card_1' => '_none',
];
$this->submitForm($edit, 'Save');
$this->assertSession()
->pageTextContains("{$field->getName()} field is required.");
// Submit form: select first option.
$edit = [
'card_1' => 0,
];
$this->submitForm($edit, 'Save');
$this->assertFieldValues($entity_init, 'card_1', [
0,
]);
// Display form: check that the right options are selected.
$this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
// A required field with a value has no 'none' option.
$this->assertSession()
->optionNotExists('edit-card-1', '_none');
$this->assertTrue($this->assertSession()
->optionExists('card_1', 0)
->isSelected());
$this->assertFalse($this->assertSession()
->optionExists('card_1', 1)
->isSelected());
$this->assertFalse($this->assertSession()
->optionExists('card_1', 2)
->isSelected());
// Make the field non required.
$field->setRequired(FALSE);
$field->save();
// Display form.
$this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
// A non-required field has a 'none' option.
$option = $this->assertSession()
->optionExists('edit-card-1', '_none');
$this->assertSame('- None -', $option->getText());
// Submit form: Unselect the option.
$edit = [
'card_1' => '_none',
];
$this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
$this->submitForm($edit, 'Save');
$this->assertFieldValues($entity_init, 'card_1', []);
// Test optgroups.
$this->card1
->setSetting('allowed_values', []);
$this->card1
->setSetting('allowed_values_function', 'options_test_allowed_values_callback');
$this->card1
->save();
// Display form: with no field data, nothing is selected
$this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
$this->assertFalse($this->assertSession()
->optionExists('card_1', 0)
->isSelected());
$this->assertFalse($this->assertSession()
->optionExists('card_1', 1)
->isSelected());
$this->assertFalse($this->assertSession()
->optionExists('card_1', 2)
->isSelected());
$this->assertSession()
->responseContains('Some dangerous & unescaped markup');
$this->assertSession()
->responseContains('More <script>dangerous</script> markup');
$this->assertSession()
->responseContains('Group 1');
// Submit form: select first option.
$edit = [
'card_1' => 0,
];
$this->submitForm($edit, 'Save');
$this->assertFieldValues($entity_init, 'card_1', [
0,
]);
// Display form: check that the right options are selected.
$this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
$this->assertTrue($this->assertSession()
->optionExists('card_1', 0)
->isSelected());
$this->assertFalse($this->assertSession()
->optionExists('card_1', 1)
->isSelected());
$this->assertFalse($this->assertSession()
->optionExists('card_1', 2)
->isSelected());
// Submit form: Unselect the option.
$edit = [
'card_1' => '_none',
];
$this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
$this->submitForm($edit, 'Save');
$this->assertFieldValues($entity_init, 'card_1', []);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.