function ChosenFieldWidgetsTest::testSelectListMultiple in Chosen 3.0.x
Same name and namespace in other branches
- 8.2 modules/chosen_field/tests/src/Functional/ChosenFieldWidgetsTest.php \Drupal\Tests\chosen_field\Functional\ChosenFieldWidgetsTest::testSelectListMultiple()
Tests the 'options_select' widget (multiple select).
File
- modules/
chosen_field/ tests/ src/ Functional/ ChosenFieldWidgetsTest.php, line 233
Class
- ChosenFieldWidgetsTest
- Test the Chosen widgets.
Namespace
Drupal\Tests\chosen_field\FunctionalCode
function testSelectListMultiple() {
// Create an instance of the 'multiple values' field.
$instance = \Drupal::entityTypeManager()
->getStorage('field_config')
->create([
'field_storage' => $this->card_2,
'bundle' => 'entity_test',
]);
$instance
->save();
\Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load('entity_test.entity_test.default')
->setComponent($this->card_2
->getName(), [
'type' => 'chosen_select',
])
->save();
// Create an entity.
$entity = \Drupal::entityTypeManager()
->getStorage('entity_test')
->create([
'user_id' => 1,
'name' => $this
->randomMachineName(),
]);
$entity
->save();
$entity_init = clone $entity;
// Display form: with no field data, nothing is selected.
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$options = [
0,
1,
2,
];
$id = 'edit-card-2';
foreach ($options as $option) {
$option_field = $this
->assertSession()
->optionExists($id, $option);
$message = "Option {$option} for field {$id} is not selected.";
$this
->assertEmpty($option_field
->hasAttribute('selected'), $message);
}
$this
->assertSession()
->responseContains('Some dangerous & unescaped markup');
// Submit form: select first and third options.
$edit = [
'card_2[]' => [
0 => 0,
2 => 2,
],
];
$this
->submitForm($edit, 'Save');
$this
->assertFieldValues($entity_init, 'card_2', [
0,
2,
]);
// Display form: check that the right options are selected.
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$id = 'edit-card-2';
$option = 0;
$option_field = $this
->assertSession()
->optionExists($id, $option);
$message = "Option {$option} for field {$id} is selected.";
$this
->assertNotEmpty($option_field
->hasAttribute('selected'), $message);
$option = 1;
$id = 'edit-card-2';
$option_field = $this
->assertSession()
->optionExists($id, $option);
$message = "Option {$option} for field {$id} is not selected.";
$this
->assertEmpty($option_field
->hasAttribute('selected'), $message);
$id = 'edit-card-2';
$option = 2;
$option_field = $this
->assertSession()
->optionExists($id, $option);
$message = "Option {$option} for field {$id} is selected.";
$this
->assertNotEmpty($option_field
->hasAttribute('selected'), $message);
// Submit form: select only first option.
$edit = [
'card_2[]' => [
0 => 0,
],
];
$this
->submitForm($edit, 'Save');
$this
->assertFieldValues($entity_init, 'card_2', [
0,
]);
// Display form: check that the right options are selected.
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$id = 'edit-card-2';
$option = 0;
$option_field = $this
->assertSession()
->optionExists($id, $option);
$message = "Option {$option} for field {$id} is selected.";
$this
->assertNotEmpty($option_field
->hasAttribute('selected'), $message);
$options = [
1,
2,
];
$id = 'edit-card-2';
foreach ($options as $option) {
$option_field = $this
->assertSession()
->optionExists($id, $option);
$message = "Option {$option} for field {$id} is not selected.";
$this
->assertEmpty($option_field
->hasAttribute('selected'), $message);
}
// Submit form: select the three options while the field accepts only 2.
$edit = [
'card_2[]' => [
0 => 0,
1 => 1,
2 => 2,
],
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('this field cannot hold more than 2 values');
// Submit form: uncheck all options.
$edit = [
'card_2[]' => [],
];
$this
->submitForm($edit, 'Save');
$this
->assertFieldValues($entity_init, 'card_2', []);
// A required select list does not have an empty key.
$instance
->setRequired(TRUE);
$instance
->save();
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertSession()
->elementNotExists('xpath', '//select[@id="edit-card-2"]//option[@value=""]');
// We do not have to test that a required select list with one option is
// auto-selected because the browser does it for us.
// Test optgroups.
// Use a callback function defining optgroups.
$this->card_2
->setSetting('allowed_values', []);
$this->card_2
->setSetting('allowed_values_function', 'options_test_allowed_values_callback');
$this->card_2
->save();
$instance
->setRequired(FALSE);
$instance
->save();
// Display form: with no field data, nothing is selected.
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$options = [
0,
1,
2,
];
$id = 'edit-card-2';
foreach ($options as $option) {
$option_field = $this
->assertSession()
->optionExists($id, $option);
$message = "Option {$option} for field {$id} is not selected.";
$this
->assertEmpty($option_field
->hasAttribute('selected'), $message);
}
$this
->assertSession()
->responseContains('Some dangerous & unescaped markup');
$this
->assertSession()
->responseContains('Group 1');
// Submit form: select first option.
$edit = [
'card_2[]' => [
0 => 0,
],
];
$this
->submitForm($edit, 'Save');
$this
->assertFieldValues($entity_init, 'card_2', [
0,
]);
// Display form: check that the right options are selected.
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$id = 'edit-card-2';
$option = 0;
$option_field = $this
->assertSession()
->optionExists($id, $option);
$message = "Option {$option} for field {$id} is selected.";
$this
->assertNotEmpty($option_field
->hasAttribute('selected'), $message);
$options = [
1,
2,
];
$id = 'edit-card-2';
foreach ($options as $option) {
$option_field = $this
->assertSession()
->optionExists($id, $option);
$message = "Option {$option} for field {$id} is not selected.";
$this
->assertEmpty($option_field
->hasAttribute('selected'), $message);
}
}