function OptionsFieldTest::testUpdateAllowedValues

Test that allowed values can be updated.

File

drupal/core/modules/options/lib/Drupal/options/Tests/OptionsFieldTest.php, line 36
Definition of Drupal\options\Tests\OptionsFieldTest.

Class

OptionsFieldTest
Tests for the 'Options' field types.

Namespace

Drupal\options\Tests

Code

function testUpdateAllowedValues() {
  $langcode = Language::LANGCODE_NOT_SPECIFIED;

  // All three options appear.
  $entity = entity_create('entity_test', array());
  $form = entity_get_form($entity);
  $this
    ->assertTrue(!empty($form[$this->fieldName][$langcode][1]), 'Option 1 exists');
  $this
    ->assertTrue(!empty($form[$this->fieldName][$langcode][2]), 'Option 2 exists');
  $this
    ->assertTrue(!empty($form[$this->fieldName][$langcode][3]), 'Option 3 exists');

  // Use one of the values in an actual entity, and check that this value
  // cannot be removed from the list.
  $entity = entity_create('entity_test', array());
  $entity->{$this->fieldName}->value = 1;
  $entity
    ->save();
  $this->field['settings']['allowed_values'] = array(
    2 => 'Two',
  );
  try {
    field_update_field($this->field);
    $this
      ->fail(t('Cannot update a list field to not include keys with existing data.'));
  } catch (FieldException $e) {
    $this
      ->pass(t('Cannot update a list field to not include keys with existing data.'));
  }

  // Empty the value, so that we can actually remove the option.
  unset($entity->{$this->fieldName});
  $entity
    ->save();

  // Removed options do not appear.
  $this->field['settings']['allowed_values'] = array(
    2 => 'Two',
  );
  field_update_field($this->field);
  $entity = entity_create('entity_test', array());
  $form = entity_get_form($entity);
  $this
    ->assertTrue(empty($form[$this->fieldName][$langcode][1]), 'Option 1 does not exist');
  $this
    ->assertTrue(!empty($form[$this->fieldName][$langcode][2]), 'Option 2 exists');
  $this
    ->assertTrue(empty($form[$this->fieldName][$langcode][3]), 'Option 3 does not exist');

  // Completely new options appear.
  $this->field['settings']['allowed_values'] = array(
    10 => 'Update',
    20 => 'Twenty',
  );
  field_update_field($this->field);
  $form = entity_get_form($entity);
  $this
    ->assertTrue(empty($form[$this->fieldName][$langcode][1]), 'Option 1 does not exist');
  $this
    ->assertTrue(empty($form[$this->fieldName][$langcode][2]), 'Option 2 does not exist');
  $this
    ->assertTrue(empty($form[$this->fieldName][$langcode][3]), 'Option 3 does not exist');
  $this
    ->assertTrue(!empty($form[$this->fieldName][$langcode][10]), 'Option 10 exists');
  $this
    ->assertTrue(!empty($form[$this->fieldName][$langcode][20]), 'Option 20 exists');

  // Options are reset when a new field with the same name is created.
  field_delete_field($this->fieldName);
  unset($this->field['id']);
  field_create_field($this->fieldDefinition);
  $this->instance = array(
    'field_name' => $this->fieldName,
    'entity_type' => 'entity_test',
    'bundle' => 'entity_test',
  );
  field_create_instance($this->instance);
  entity_get_form_display('entity_test', 'entity_test', 'default')
    ->setComponent($this->fieldName, array(
    'type' => 'options_buttons',
  ))
    ->save();
  $entity = entity_create('entity_test', array());
  $form = entity_get_form($entity);
  $this
    ->assertTrue(!empty($form[$this->fieldName][$langcode][1]), 'Option 1 exists');
  $this
    ->assertTrue(!empty($form[$this->fieldName][$langcode][2]), 'Option 2 exists');
  $this
    ->assertTrue(!empty($form[$this->fieldName][$langcode][3]), 'Option 3 exists');
}