Tests the 'options_buttons' widget (multiple select).
function testCheckBoxes() {
// Create an instance of the 'multiple values' field.
$instance = array(
'field_name' => $this->card_2['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_2['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;
// Display form: with no field data, nothing is checked.
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertNoFieldChecked("edit-card-2-{$langcode}-0");
$this
->assertNoFieldChecked("edit-card-2-{$langcode}-1");
$this
->assertNoFieldChecked("edit-card-2-{$langcode}-2");
$this
->assertRaw('Some dangerous & unescaped <strong>markup</strong>', 'Option text was properly filtered.');
// Submit form: select first and third options.
$edit = array(
"card_2[{$langcode}][0]" => TRUE,
"card_2[{$langcode}][1]" => FALSE,
"card_2[{$langcode}][2]" => TRUE,
);
$this
->drupalPost(NULL, $edit, t('Save'));
$this
->assertFieldValues($entity_init, 'card_2', $langcode, array(
0,
2,
));
// Display form: check that the right options are selected.
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertFieldChecked("edit-card-2-{$langcode}-0");
$this
->assertNoFieldChecked("edit-card-2-{$langcode}-1");
$this
->assertFieldChecked("edit-card-2-{$langcode}-2");
// Submit form: select only first option.
$edit = array(
"card_2[{$langcode}][0]" => TRUE,
"card_2[{$langcode}][1]" => FALSE,
"card_2[{$langcode}][2]" => FALSE,
);
$this
->drupalPost(NULL, $edit, t('Save'));
$this
->assertFieldValues($entity_init, 'card_2', $langcode, array(
0,
));
// Display form: check that the right options are selected.
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertFieldChecked("edit-card-2-{$langcode}-0");
$this
->assertNoFieldChecked("edit-card-2-{$langcode}-1");
$this
->assertNoFieldChecked("edit-card-2-{$langcode}-2");
// Submit form: select the three options while the field accepts only 2.
$edit = array(
"card_2[{$langcode}][0]" => TRUE,
"card_2[{$langcode}][1]" => TRUE,
"card_2[{$langcode}][2]" => TRUE,
);
$this
->drupalPost(NULL, $edit, t('Save'));
$this
->assertText('this field cannot hold more than 2 values', 'Validation error was displayed.');
// Submit form: uncheck all options.
$edit = array(
"card_2[{$langcode}][0]" => FALSE,
"card_2[{$langcode}][1]" => FALSE,
"card_2[{$langcode}][2]" => FALSE,
);
$this
->drupalPost(NULL, $edit, t('Save'));
// Check that the value was saved.
$this
->assertFieldValues($entity_init, 'card_2', $langcode, array());
// Required checkbox with one option is auto-selected.
$this->card_2['settings']['allowed_values'] = array(
99 => 'Only allowed value',
);
field_update_field($this->card_2);
$instance['required'] = TRUE;
field_update_instance($instance);
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertFieldChecked("edit-card-2-{$langcode}-99");
}