Tests the Hidden widget.
function testFieldFormHiddenWidget() {
$this->field = $this->field_single;
$this->field_name = $this->field['field_name'];
$this->instance['field_name'] = $this->field_name;
$this->instance['default_value'] = array(
0 => array(
'value' => 99,
),
);
field_create_field($this->field);
field_create_instance($this->instance);
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->instance['field_name'], array(
'type' => 'hidden',
))
->save();
$langcode = Language::LANGCODE_NOT_SPECIFIED;
// Display the entity creation form.
$this
->drupalGet('test-entity/add/test_bundle');
// Create an entity and test that the default value is assigned correctly to
// the field that uses the hidden widget.
$this
->assertNoField("{$this->field_name}[{$langcode}][0][value]", 'The hidden widget is not displayed');
$this
->drupalPost(NULL, array(), t('Save'));
preg_match('|test-entity/manage/(\\d+)/edit|', $this->url, $match);
$id = $match[1];
$this
->assertRaw(t('test_entity @id has been created.', array(
'@id' => $id,
)), 'Entity was created');
$entity = field_test_entity_test_load($id);
$this
->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], 99, 'Default value was saved');
// Update the instance to remove the default value and switch to the
// default widget.
$this->instance['default_value'] = NULL;
field_update_instance($this->instance);
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->instance['field_name'], array(
'type' => 'test_field_widget',
))
->save();
// Display edit form.
$this
->drupalGet('test-entity/manage/' . $id . '/edit');
$this
->assertFieldByName("{$this->field_name}[{$langcode}][0][value]", 99, 'Widget is displayed with the correct default value');
// Update the entity.
$value = mt_rand(1, 127);
$edit = array(
"{$this->field_name}[{$langcode}][0][value]" => $value,
);
$this
->drupalPost(NULL, $edit, t('Save'));
$this
->assertRaw(t('test_entity @id has been updated.', array(
'@id' => $id,
)), 'Entity was updated');
entity_get_controller('test_entity')
->resetCache(array(
$id,
));
$entity = field_test_entity_test_load($id);
$this
->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], $value, 'Field value was updated');
// Update the form display and switch to the Hidden widget again.
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->instance['field_name'], array(
'type' => 'hidden',
))
->save();
// Create a new revision.
$edit = array(
'revision' => TRUE,
);
$this
->drupalPost('test-entity/manage/' . $id . '/edit', $edit, t('Save'));
// Check that the expected value has been carried over to the new revision.
entity_get_controller('test_entity')
->resetCache(array(
$id,
));
$entity = field_test_entity_test_load($id);
$this
->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], $value, 'New revision has the expected value for the field with the Hidden widget');
}