You are here

function FormTest::testHiddenField in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/FormTest.php \Drupal\field\Tests\FormTest::testHiddenField()

Tests hiding a field in a form.

File

core/modules/field/src/Tests/FormTest.php, line 594
Contains \Drupal\field\Tests\FormTest.

Class

FormTest
Tests field form handling.

Namespace

Drupal\field\Tests

Code

function testHiddenField() {
  $entity_type = 'entity_test_rev';
  $field_storage = $this->fieldStorageSingle;
  $field_storage['entity_type'] = $entity_type;
  $field_name = $field_storage['field_name'];
  $this->field['field_name'] = $field_name;
  $this->field['default_value'] = array(
    0 => array(
      'value' => 99,
    ),
  );
  $this->field['entity_type'] = $entity_type;
  $this->field['bundle'] = $entity_type;
  entity_create('field_storage_config', $field_storage)
    ->save();
  $this->field = entity_create('field_config', $this->field);
  $this->field
    ->save();

  // We explicitly do not assign a widget in a form display, so the field
  // stays hidden in forms.
  // Display the entity creation form.
  $this
    ->drupalGet($entity_type . '/add');

  // Create an entity and test that the default value is assigned correctly to
  // the field that uses the hidden widget.
  $this
    ->assertNoField("{$field_name}[0][value]", 'The field does not appear in the form');
  $this
    ->drupalPostForm(NULL, array(), t('Save'));
  preg_match('|' . $entity_type . '/manage/(\\d+)|', $this->url, $match);
  $id = $match[1];
  $this
    ->assertText(t('entity_test_rev @id has been created.', array(
    '@id' => $id,
  )), 'Entity was created');
  $entity = entity_load($entity_type, $id);
  $this
    ->assertEqual($entity->{$field_name}->value, 99, 'Default value was saved');

  // Update the field to remove the default value, and switch to the default
  // widget.
  $this->field
    ->setDefaultValue(array());
  $this->field
    ->save();
  entity_get_form_display($entity_type, $this->field
    ->getTargetBundle(), 'default')
    ->setComponent($this->field
    ->getName(), array(
    'type' => 'test_field_widget',
  ))
    ->save();

  // Display edit form.
  $this
    ->drupalGet($entity_type . '/manage/' . $id . '/edit');
  $this
    ->assertFieldByName("{$field_name}[0][value]", 99, 'Widget is displayed with the correct default value');

  // Update the entity.
  $value = mt_rand(1, 127);
  $edit = array(
    "{$field_name}[0][value]" => $value,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText(t('entity_test_rev @id has been updated.', array(
    '@id' => $id,
  )), 'Entity was updated');
  \Drupal::entityManager()
    ->getStorage($entity_type)
    ->resetCache(array(
    $id,
  ));
  $entity = entity_load($entity_type, $id);
  $this
    ->assertEqual($entity->{$field_name}->value, $value, 'Field value was updated');

  // Set the field back to hidden.
  entity_get_form_display($entity_type, $this->field
    ->getTargetBundle(), 'default')
    ->removeComponent($this->field
    ->getName())
    ->save();

  // Create a new revision.
  $edit = array(
    'revision' => TRUE,
  );
  $this
    ->drupalPostForm($entity_type . '/manage/' . $id . '/edit', $edit, t('Save'));

  // Check that the expected value has been carried over to the new revision.
  \Drupal::entityManager()
    ->getStorage($entity_type)
    ->resetCache(array(
    $id,
  ));
  $entity = entity_load($entity_type, $id);
  $this
    ->assertEqual($entity->{$field_name}->value, $value, 'New revision has the expected value for the field with the Hidden widget');
}