You are here

function FormTest::testFieldFormSingle 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::testFieldFormSingle()

File

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

Class

FormTest
Tests field form handling.

Namespace

Drupal\field\Tests

Code

function testFieldFormSingle() {
  $field_storage = $this->fieldStorageSingle;
  $field_name = $field_storage['field_name'];
  $this->field['field_name'] = $field_name;
  entity_create('field_storage_config', $field_storage)
    ->save();
  entity_create('field_config', $this->field)
    ->save();
  entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
    ->setComponent($field_name)
    ->save();

  // Display creation form.
  $this
    ->drupalGet('entity_test/add');

  // Create token value expected for description.
  $token_description = Html::escape($this
    ->config('system.site')
    ->get('name')) . '_description';
  $this
    ->assertText($token_description, 'Token replacement for description is displayed');
  $this
    ->assertFieldByName("{$field_name}[0][value]", '', 'Widget is displayed');
  $this
    ->assertNoField("{$field_name}[1][value]", 'No extraneous widget is displayed');

  // Check that hook_field_widget_form_alter() does not believe this is the
  // default value form.
  $this
    ->assertNoText('From hook_field_widget_form_alter(): Default form is true.', 'Not default value form in hook_field_widget_form_alter().');

  // Submit with invalid value (field-level validation).
  $edit = array(
    "{$field_name}[0][value]" => -1,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertRaw(t('%name does not accept the value -1.', array(
    '%name' => $this->field['label'],
  )), 'Field validation fails with invalid input.');

  // TODO : check that the correct field is flagged for error.
  // Create an entity
  $value = mt_rand(1, 127);
  $edit = array(
    "{$field_name}[0][value]" => $value,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  preg_match('|entity_test/manage/(\\d+)|', $this->url, $match);
  $id = $match[1];
  $this
    ->assertText(t('entity_test @id has been created.', array(
    '@id' => $id,
  )), 'Entity was created');
  $entity = entity_load('entity_test', $id);
  $this
    ->assertEqual($entity->{$field_name}->value, $value, 'Field value was saved');

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

  // 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 @id has been updated.', array(
    '@id' => $id,
  )), 'Entity was updated');
  $this->container
    ->get('entity.manager')
    ->getStorage('entity_test')
    ->resetCache(array(
    $id,
  ));
  $entity = entity_load('entity_test', $id);
  $this
    ->assertEqual($entity->{$field_name}->value, $value, 'Field value was updated');

  // Empty the field.
  $value = '';
  $edit = array(
    "{$field_name}[0][value]" => $value,
  );
  $this
    ->drupalPostForm('entity_test/manage/' . $id . '/edit', $edit, t('Save'));
  $this
    ->assertText(t('entity_test @id has been updated.', array(
    '@id' => $id,
  )), 'Entity was updated');
  $this->container
    ->get('entity.manager')
    ->getStorage('entity_test')
    ->resetCache(array(
    $id,
  ));
  $entity = entity_load('entity_test', $id);
  $this
    ->assertTrue($entity->{$field_name}
    ->isEmpty(), 'Field was emptied');
}