You are here

protected function OptionsDynamicValuesTestBase::setUp in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/options/tests/src/Functional/OptionsDynamicValuesTestBase.php \Drupal\Tests\options\Functional\OptionsDynamicValuesTestBase::setUp()

Overrides BrowserTestBase::setUp

File

core/modules/options/tests/src/Functional/OptionsDynamicValuesTestBase.php, line 36

Class

OptionsDynamicValuesTestBase
Base class for testing allowed values of options fields.

Namespace

Drupal\Tests\options\Functional

Code

protected function setUp() {
  parent::setUp();
  $field_name = 'test_options';
  $this->fieldStorage = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'entity_test_rev',
    'type' => 'list_string',
    'cardinality' => 1,
    'settings' => [
      'allowed_values_function' => 'options_test_dynamic_values_callback',
    ],
  ]);
  $this->fieldStorage
    ->save();
  $this->field = FieldConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'entity_test_rev',
    'bundle' => 'entity_test_rev',
    'required' => TRUE,
  ])
    ->save();
  \Drupal::service('entity_display.repository')
    ->getFormDisplay('entity_test_rev', 'entity_test_rev')
    ->setComponent($field_name, [
    'type' => 'options_select',
  ])
    ->save();

  // Create an entity and prepare test data that will be used by
  // options_test_dynamic_values_callback().
  $values = [
    'user_id' => mt_rand(1, 10),
    'name' => $this
      ->randomMachineName(),
  ];
  $this->entity = EntityTestRev::create($values);
  $this->entity
    ->save();
  $this->test = [
    'label' => $this->entity
      ->label(),
    'uuid' => $this->entity
      ->uuid(),
    'bundle' => $this->entity
      ->bundle(),
    'uri' => $this->entity
      ->toUrl()
      ->toString(),
  ];
}