You are here

protected function ConditionalFieldSelectTest::setUp in Conditional Fields 8

Same name and namespace in other branches
  1. 4.x tests/src/FunctionalJavascript/ConditionalFieldSelectTest.php \Drupal\Tests\conditional_fields\FunctionalJavascript\ConditionalFieldSelectTest::setUp()

Overrides ConditionalFieldTestBase::setUp

File

tests/src/FunctionalJavascript/ConditionalFieldSelectTest.php, line 77

Class

ConditionalFieldSelectTest
Test Conditional Fields Select Plugin.

Namespace

Drupal\Tests\conditional_fields\FunctionalJavascript

Code

protected function setUp() {
  parent::setUp();
  foreach ($this->fieldNames as $fieldName) {
    $this->fieldSelectors[$fieldName] = "[name=\"{$fieldName}\"]";
    $this->fieldStorageDefinitions[$fieldName] = [
      'field_name' => $fieldName,
      'entity_type' => 'node',
      // Cut 'select_single_' for getting field types.
      'type' => str_replace('select_single_', '', $fieldName),
      'cardinality' => 1,
    ];
  }

  // Define allowed values for each field type.
  $this->fieldStorageDefinitions['select_single_entity_reference']['settings']['target_type'] = 'user';
  $this->fieldStorageDefinitions['select_single_list_integer']['settings']['allowed_values'] = [
    1 => '1',
    2 => '2',
    3 => '3',
  ];
  $this->fieldStorageDefinitions['select_single_list_float']['settings']['allowed_values'] = [
    '1.5' => '1.5',
    '2.5' => '2.5',
    '3.5' => '3.5',
  ];
  $this->fieldStorageDefinitions['select_single_list_string']['settings']['allowed_values'] = [
    'one' => 'One',
    'two' => 'Two',
    'three' => 'Three',
  ];
  $entity_form_display = EntityFormDisplay::load('node.article.default');
  foreach ($this->fieldNames as $fieldName) {

    // Save field storage configurations.
    $this->fieldStorages[$fieldName] = FieldStorageConfig::create($this->fieldStorageDefinitions[$fieldName]);
    $this->fieldStorages[$fieldName]
      ->save();

    // Create a field configuration.
    $this->fields[$fieldName] = FieldConfig::create([
      'field_storage' => $this->fieldStorages[$fieldName],
      'bundle' => 'article',
    ]);
    $this->fields[$fieldName]
      ->save();

    // Set field form display settings for the field.
    $entity_form_display
      ->setComponent($fieldName, [
      'type' => 'options_select',
    ]);
  }
  $entity_form_display
    ->save();
}