function OptionsFieldTest::testUpdateAllowedValues

Same name in other branches
  1. 9 core/modules/options/tests/src/Kernel/OptionsFieldTest.php \Drupal\Tests\options\Kernel\OptionsFieldTest::testUpdateAllowedValues()
  2. 8.9.x core/modules/options/tests/src/Kernel/OptionsFieldTest.php \Drupal\Tests\options\Kernel\OptionsFieldTest::testUpdateAllowedValues()
  3. 11.x core/modules/options/tests/src/Kernel/OptionsFieldTest.php \Drupal\Tests\options\Kernel\OptionsFieldTest::testUpdateAllowedValues()

Tests that allowed values can be updated.

File

core/modules/options/tests/src/Kernel/OptionsFieldTest.php, line 27

Class

OptionsFieldTest
Tests for the 'Options' field types.

Namespace

Drupal\Tests\options\Kernel

Code

public function testUpdateAllowedValues() : void {
    // All three options appear.
    $entity = EntityTest::create();
    $form = \Drupal::service('entity.form_builder')->getForm($entity);
    $this->assertArrayHasKey(1, $form[$this->fieldName]['widget'], 'Option 1 exists');
    $this->assertArrayHasKey(2, $form[$this->fieldName]['widget'], 'Option 2 exists');
    $this->assertArrayHasKey(3, $form[$this->fieldName]['widget'], 'Option 3 exists');
    // Use one of the values in an actual entity, and check that this value
    // cannot be removed from the list.
    $entity = EntityTest::create();
    $entity->{$this->fieldName}->value = 1;
    $entity->save();
    $this->fieldStorage
        ->setSetting('allowed_values', [
        2 => 'Two',
    ]);
    try {
        $this->fieldStorage
            ->save();
        $this->fail('Cannot update a list field storage to not include keys with existing data.');
    } catch (FieldStorageDefinitionUpdateForbiddenException $e) {
        // Expected exception; just continue testing.
    }
    // Empty the value, so that we can actually remove the option.
    unset($entity->{$this->fieldName});
    $entity->save();
    // Removed options do not appear.
    $this->fieldStorage
        ->setSetting('allowed_values', [
        2 => 'Two',
    ]);
    $this->fieldStorage
        ->save();
    $entity = EntityTest::create();
    $form = \Drupal::service('entity.form_builder')->getForm($entity);
    $this->assertArrayNotHasKey(1, $form[$this->fieldName]['widget'], 'Option 1 does not exist');
    $this->assertArrayHasKey(2, $form[$this->fieldName]['widget'], 'Option 2 exists');
    $this->assertArrayNotHasKey(3, $form[$this->fieldName]['widget'], 'Option 3 does not exist');
    // Completely new options appear.
    $this->fieldStorage
        ->setSetting('allowed_values', [
        10 => 'Update',
        20 => 'Twenty',
    ]);
    $this->fieldStorage
        ->save();
    // The entity holds an outdated field object with the old allowed values
    // setting, so we need to reinitialize the entity object.
    $entity = EntityTest::create();
    $form = \Drupal::service('entity.form_builder')->getForm($entity);
    $this->assertArrayNotHasKey(1, $form[$this->fieldName]['widget'], 'Option 1 does not exist');
    $this->assertArrayNotHasKey(2, $form[$this->fieldName]['widget'], 'Option 2 does not exist');
    $this->assertArrayNotHasKey(3, $form[$this->fieldName]['widget'], 'Option 3 does not exist');
    $this->assertArrayHasKey(10, $form[$this->fieldName]['widget'], 'Option 10 exists');
    $this->assertArrayHasKey(20, $form[$this->fieldName]['widget'], 'Option 20 exists');
    // Options are reset when a new field with the same name is created.
    $this->fieldStorage
        ->delete();
    FieldStorageConfig::create($this->fieldStorageDefinition)
        ->save();
    FieldConfig::create([
        'field_name' => $this->fieldName,
        'entity_type' => 'entity_test',
        'bundle' => 'entity_test',
        'required' => TRUE,
    ])
        ->save();
    \Drupal::service('entity_display.repository')->getFormDisplay('entity_test', 'entity_test')
        ->setComponent($this->fieldName, [
        'type' => 'options_buttons',
    ])
        ->save();
    $entity = EntityTest::create();
    $form = \Drupal::service('entity.form_builder')->getForm($entity);
    $this->assertArrayHasKey(1, $form[$this->fieldName]['widget'], 'Option 1 exists');
    $this->assertArrayHasKey(2, $form[$this->fieldName]['widget'], 'Option 2 exists');
    $this->assertArrayHasKey(3, $form[$this->fieldName]['widget'], 'Option 3 exists');
    // Test the generateSampleValue() method.
    $entity = EntityTest::create();
    $entity->{$this->fieldName}
        ->generateSampleItems();
    $this->entityValidateAndSave($entity);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.