You are here

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

File

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

Class

FormTest
Tests field form handling.

Namespace

Drupal\field\Tests

Code

function testFieldFormSingleRequired() {
  $field_storage = $this->fieldStorageSingle;
  $field_name = $field_storage['field_name'];
  $this->field['field_name'] = $field_name;
  $this->field['required'] = TRUE;
  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();

  // Submit with missing required value.
  $edit = array();
  $this
    ->drupalPostForm('entity_test/add', $edit, t('Save'));
  $this
    ->assertRaw(t('@name field is required.', array(
    '@name' => $this->field['label'],
  )), 'Required field with no value fails validation');

  // 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');

  // Edit with missing required value.
  $value = '';
  $edit = array(
    "{$field_name}[0][value]" => $value,
  );
  $this
    ->drupalPostForm('entity_test/manage/' . $id . '/edit', $edit, t('Save'));
  $this
    ->assertRaw(t('@name field is required.', array(
    '@name' => $this->field['label'],
  )), 'Required field with no value fails validation');
}