Tests the 'options_buttons' widget (single select).
function testRadioButtons() {
// Create an instance of the 'single value' field.
$instance = array(
'field_name' => $this->card_1['field_name'],
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
);
$instance = field_create_instance($instance);
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($this->card_1['field_name'], array(
'type' => 'options_buttons',
))
->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;
// With no field data, no buttons are checked.
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertNoFieldChecked("edit-card-1-{$langcode}-0");
$this
->assertNoFieldChecked("edit-card-1-{$langcode}-1");
$this
->assertNoFieldChecked("edit-card-1-{$langcode}-2");
$this
->assertRaw('Some dangerous & unescaped <strong>markup</strong>', 'Option text was properly filtered.');
// Select first option.
$edit = array(
"card_1[{$langcode}]" => 0,
);
$this
->drupalPost(NULL, $edit, t('Save'));
$this
->assertFieldValues($entity_init, 'card_1', $langcode, array(
0,
));
// Check that the selected button is checked.
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertFieldChecked("edit-card-1-{$langcode}-0");
$this
->assertNoFieldChecked("edit-card-1-{$langcode}-1");
$this
->assertNoFieldChecked("edit-card-1-{$langcode}-2");
// Unselect option.
$edit = array(
"card_1[{$langcode}]" => '_none',
);
$this
->drupalPost(NULL, $edit, t('Save'));
$this
->assertFieldValues($entity_init, 'card_1', $langcode, array());
// Check that required radios with one option is auto-selected.
$this->card_1['settings']['allowed_values'] = array(
99 => 'Only allowed value',
);
field_update_field($this->card_1);
$instance['required'] = TRUE;
field_update_instance($instance);
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertFieldChecked("edit-card-1-{$langcode}-99");
}