You are here

protected function TestBase::prepareTestFields in Select (or other) 4.x

Same name and namespace in other branches
  1. 8 tests/src/Functional/TestBase.php \Drupal\Tests\select_or_other\Functional\TestBase::prepareTestFields()

Generate content types and fields for testing.

Parameters

string $field_type: The type of field to create.

array $field_settings: The field settings.

string $widget: The widget to use.

array $select_types: Which select elements should be used.

2 calls to TestBase::prepareTestFields()
ListTest::setUp in tests/src/Functional/ListTest.php
ReferenceTest::setUp in tests/src/Functional/ReferenceTest.php

File

tests/src/Functional/TestBase.php, line 101

Class

TestBase
Base test class for select or other widgets.

Namespace

Drupal\Tests\select_or_other\Functional

Code

protected function prepareTestFields($field_type, array $field_settings, $widget, array $select_types) {

  // Configure fields.
  foreach ($select_types as $select_type) {
    foreach (array(
      1,
      -1,
    ) as $cardinality) {
      foreach (array(
        TRUE,
        FALSE,
      ) as $required) {
        $bundle = $this
          ->drupalCreateContentType()
          ->id();
        $field_name = strtolower($this
          ->randomMachineName());
        $vocabulary = $this
          ->randomMachineName();
        $this->fields[$field_name] = [
          'bundle' => $bundle,
          'widget' => $widget,
          'select_type' => $select_type,
          'cardinality' => $cardinality,
          'required' => $required,
          'vocabulary' => strtolower($vocabulary),
        ];
        if (\Drupal::moduleHandler()
          ->moduleExists('taxonomy')) {

          // Create a vocabulary.
          \Drupal::entityTypeManager()
            ->getStorage('taxonomy_vocabulary')
            ->create([
            'vid' => strtolower($vocabulary),
            'name' => $vocabulary,
          ])
            ->save();
        }

        // Create the field.
        $field_defaults = [
          'field_name' => $field_name,
          'entity_type' => 'node',
          'bundle' => $bundle,
        ];
        $field_info = $field_defaults + [
          'type' => $field_type,
          'settings' => $field_settings,
          'cardinality' => $cardinality,
        ];
        \Drupal::entityTypeManager()
          ->getStorage('field_storage_config')
          ->create($field_info)
          ->save();

        // Create the instance.
        $instance_info = $field_defaults + [
          'field_type' => $field_type,
          'required' => $required,
          'label' => $field_name,
          'settings' => [
            'handler_settings' => [
              'target_bundles' => [
                $vocabulary => $vocabulary,
              ],
              'auto_create' => TRUE,
            ],
          ],
        ];
        $instance = \Drupal::entityTypeManager()
          ->getStorage('field_config')
          ->create($instance_info);
        $instance
          ->save();
        \Drupal::entityTypeManager()
          ->getStorage('entity_form_display')
          ->load('node' . '.' . $bundle . '.' . 'default')
          ->setComponent($field_name, [
          'type' => $widget,
          'settings' => [
            'select_element_type' => $select_type,
          ],
        ])
          ->save();
      }
    }
  }

  // Determine required permissions.
  $this->defaultPermissions = [
    'bypass node access',
  ];
}