View source
<?php
namespace Drupal\Tests\options\Kernel;
use Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
class OptionsFieldTest extends OptionsFieldUnitTestBase {
protected static $modules = [
'options',
];
public function testUpdateAllowedValues() {
$entity = EntityTest::create();
$form = \Drupal::service('entity.form_builder')
->getForm($entity);
$this
->assertTrue(!empty($form[$this->fieldName]['widget'][1]), 'Option 1 exists');
$this
->assertTrue(!empty($form[$this->fieldName]['widget'][2]), 'Option 2 exists');
$this
->assertTrue(!empty($form[$this->fieldName]['widget'][3]), 'Option 3 exists');
$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) {
}
unset($entity->{$this->fieldName});
$entity
->save();
$this->fieldStorage
->setSetting('allowed_values', [
2 => 'Two',
]);
$this->fieldStorage
->save();
$entity = EntityTest::create();
$form = \Drupal::service('entity.form_builder')
->getForm($entity);
$this
->assertTrue(empty($form[$this->fieldName]['widget'][1]), 'Option 1 does not exist');
$this
->assertTrue(!empty($form[$this->fieldName]['widget'][2]), 'Option 2 exists');
$this
->assertTrue(empty($form[$this->fieldName]['widget'][3]), 'Option 3 does not exist');
$this->fieldStorage
->setSetting('allowed_values', [
10 => 'Update',
20 => 'Twenty',
]);
$this->fieldStorage
->save();
$entity = EntityTest::create();
$form = \Drupal::service('entity.form_builder')
->getForm($entity);
$this
->assertTrue(empty($form[$this->fieldName]['widget'][1]), 'Option 1 does not exist');
$this
->assertTrue(empty($form[$this->fieldName]['widget'][2]), 'Option 2 does not exist');
$this
->assertTrue(empty($form[$this->fieldName]['widget'][3]), 'Option 3 does not exist');
$this
->assertTrue(!empty($form[$this->fieldName]['widget'][10]), 'Option 10 exists');
$this
->assertTrue(!empty($form[$this->fieldName]['widget'][20]), 'Option 20 exists');
$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
->assertTrue(!empty($form[$this->fieldName]['widget'][1]), 'Option 1 exists');
$this
->assertTrue(!empty($form[$this->fieldName]['widget'][2]), 'Option 2 exists');
$this
->assertTrue(!empty($form[$this->fieldName]['widget'][3]), 'Option 3 exists');
$entity = EntityTest::create();
$entity->{$this->fieldName}
->generateSampleItems();
$this
->entityValidateAndSave($entity);
}
}