Tests the 'options_select' widget (single select).
function testSelectListSingle() {
// Create an instance of the 'single value' field.
$instance = array(
'field_name' => $this->card_1['field_name'],
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'required' => TRUE,
);
$instance = field_create_instance($instance);
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($this->card_1['field_name'], array(
'type' => 'options_select',
))
->save();
$langcode = Language::LANGCODE_NOT_SPECIFIED;
// Create an entity.
$entity = entity_create('entity_test', array(
'user_id' => 1,
'name' => $this
->randomName(),
));
$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.
$this
->assertTrue($this
->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', array(
':id' => 'edit-card-1-' . $langcode,
':label' => t('- Select a value -'),
)), 'A required select list has a "Select a value" choice.');
// With no field data, nothing is selected.
$this
->assertNoOptionSelected("edit-card-1-{$langcode}", '_none');
$this
->assertNoOptionSelected("edit-card-1-{$langcode}", 0);
$this
->assertNoOptionSelected("edit-card-1-{$langcode}", 1);
$this
->assertNoOptionSelected("edit-card-1-{$langcode}", 2);
$this
->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.');
// Submit form: select invalid 'none' option.
$edit = array(
"card_1[{$langcode}]" => '_none',
);
$this
->drupalPost(NULL, $edit, t('Save'));
$this
->assertRaw(t('!title field is required.', array(
'!title' => $instance['field_name'],
)), 'Cannot save a required field when selecting "none" from the select list.');
// Submit form: select first option.
$edit = array(
"card_1[{$langcode}]" => 0,
);
$this
->drupalPost(NULL, $edit, t('Save'));
$this
->assertFieldValues($entity_init, 'card_1', $langcode, array(
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
->assertFalse($this
->xpath('//select[@id=:id]//option[@value="_none"]', array(
':id' => 'edit-card-1-' . $langcode,
)), 'A required select list with an actual value has no "none" choice.');
$this
->assertOptionSelected("edit-card-1-{$langcode}", 0);
$this
->assertNoOptionSelected("edit-card-1-{$langcode}", 1);
$this
->assertNoOptionSelected("edit-card-1-{$langcode}", 2);
// Make the field non required.
$instance['required'] = FALSE;
field_update_instance($instance);
// Display form.
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
// A non-required field has a 'none' option.
$this
->assertTrue($this
->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', array(
':id' => 'edit-card-1-' . $langcode,
':label' => t('- None -'),
)), 'A non-required select list has a "None" choice.');
// Submit form: Unselect the option.
$edit = array(
"card_1[{$langcode}]" => '_none',
);
$this
->drupalPost('entity_test/manage/' . $entity
->id() . '/edit', $edit, t('Save'));
$this
->assertFieldValues($entity_init, 'card_1', $langcode, array());
// Test optgroups.
$this->card_1['settings']['allowed_values'] = array();
$this->card_1['settings']['allowed_values_function'] = 'options_test_allowed_values_callback';
field_update_field($this->card_1);
// Display form: with no field data, nothing is selected
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertNoOptionSelected("edit-card-1-{$langcode}", 0);
$this
->assertNoOptionSelected("edit-card-1-{$langcode}", 1);
$this
->assertNoOptionSelected("edit-card-1-{$langcode}", 2);
$this
->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.');
$this
->assertRaw('Group 1', 'Option groups are displayed.');
// Submit form: select first option.
$edit = array(
"card_1[{$langcode}]" => 0,
);
$this
->drupalPost(NULL, $edit, t('Save'));
$this
->assertFieldValues($entity_init, 'card_1', $langcode, array(
0,
));
// Display form: check that the right options are selected.
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertOptionSelected("edit-card-1-{$langcode}", 0);
$this
->assertNoOptionSelected("edit-card-1-{$langcode}", 1);
$this
->assertNoOptionSelected("edit-card-1-{$langcode}", 2);
// Submit form: Unselect the option.
$edit = array(
"card_1[{$langcode}]" => '_none',
);
$this
->drupalPost('entity_test/manage/' . $entity
->id() . '/edit', $edit, t('Save'));
$this
->assertFieldValues($entity_init, 'card_1', $langcode, array());
}