You are here

function ManageFieldsTest::testDefaultValue in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field_ui/src/Tests/ManageFieldsTest.php \Drupal\field_ui\Tests\ManageFieldsTest::testDefaultValue()

Tests that default value is correctly validated and saved.

File

core/modules/field_ui/src/Tests/ManageFieldsTest.php, line 372
Contains \Drupal\field_ui\Tests\ManageFieldsTest.

Class

ManageFieldsTest
Tests the Field UI "Manage fields" screen.

Namespace

Drupal\field_ui\Tests

Code

function testDefaultValue() {

  // Create a test field storage and field.
  $field_name = 'test';
  FieldStorageConfig::create(array(
    'field_name' => $field_name,
    'entity_type' => 'node',
    'type' => 'test_field',
  ))
    ->save();
  $field = FieldConfig::create(array(
    'field_name' => $field_name,
    'entity_type' => 'node',
    'bundle' => $this->contentType,
  ));
  $field
    ->save();
  entity_get_form_display('node', $this->contentType, 'default')
    ->setComponent($field_name)
    ->save();
  $admin_path = 'admin/structure/types/manage/' . $this->contentType . '/fields/' . $field
    ->id();
  $element_id = "edit-default-value-input-{$field_name}-0-value";
  $element_name = "default_value_input[{$field_name}][0][value]";
  $this
    ->drupalGet($admin_path);
  $this
    ->assertFieldById($element_id, '', 'The default value widget was empty.');

  // Check that invalid default values are rejected.
  $edit = array(
    $element_name => '-1',
  );
  $this
    ->drupalPostForm($admin_path, $edit, t('Save settings'));
  $this
    ->assertText("{$field_name} does not accept the value -1", 'Form validation failed.');

  // Check that the default value is saved.
  $edit = array(
    $element_name => '1',
  );
  $this
    ->drupalPostForm($admin_path, $edit, t('Save settings'));
  $this
    ->assertText("Saved {$field_name} configuration", 'The form was successfully submitted.');
  $field = FieldConfig::loadByName('node', $this->contentType, $field_name);
  $this
    ->assertEqual($field
    ->getDefaultValueLiteral(), array(
    array(
      'value' => 1,
    ),
  ), 'The default value was correctly saved.');

  // Check that the default value shows up in the form
  $this
    ->drupalGet($admin_path);
  $this
    ->assertFieldById($element_id, '1', 'The default value widget was displayed with the correct value.');

  // Check that the default value can be emptied.
  $edit = array(
    $element_name => '',
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save settings'));
  $this
    ->assertText("Saved {$field_name} configuration", 'The form was successfully submitted.');
  $field = FieldConfig::loadByName('node', $this->contentType, $field_name);
  $this
    ->assertEqual($field
    ->getDefaultValueLiteral(), NULL, 'The default value was correctly saved.');

  // Check that the default value can be empty when the field is marked as
  // required and can store unlimited values.
  $field_storage = FieldStorageConfig::loadByName('node', $field_name);
  $field_storage
    ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
  $field_storage
    ->save();
  $this
    ->drupalGet($admin_path);
  $edit = array(
    'required' => 1,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save settings'));
  $this
    ->drupalGet($admin_path);
  $this
    ->drupalPostForm(NULL, array(), t('Save settings'));
  $this
    ->assertText("Saved {$field_name} configuration", 'The form was successfully submitted.');
  $field = FieldConfig::loadByName('node', $this->contentType, $field_name);
  $this
    ->assertEqual($field
    ->getDefaultValueLiteral(), NULL, 'The default value was correctly saved.');

  // Check that the default widget is used when the field is hidden.
  entity_get_form_display($field
    ->getTargetEntityTypeId(), $field
    ->getTargetBundle(), 'default')
    ->removeComponent($field_name)
    ->save();
  $this
    ->drupalGet($admin_path);
  $this
    ->assertFieldById($element_id, '', 'The default value widget was displayed when field is hidden.');
}