You are here

abstract class JsonapiKernelTestBase in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Kernel/JsonapiKernelTestBase.php \Drupal\Tests\jsonapi\Kernel\JsonapiKernelTestBase
  2. 9 core/modules/jsonapi/tests/src/Kernel/JsonapiKernelTestBase.php \Drupal\Tests\jsonapi\Kernel\JsonapiKernelTestBase

Contains shared test utility methods.

@internal

Hierarchy

Expanded class hierarchy of JsonapiKernelTestBase

6 files declare their use of JsonapiKernelTestBase
FieldItemNormalizerTest.php in core/modules/jsonapi/tests/src/Kernel/Normalizer/FieldItemNormalizerTest.php
FieldResolverTest.php in core/modules/jsonapi/tests/src/Kernel/Context/FieldResolverTest.php
RelatedResourceTypesTest.php in core/modules/jsonapi/tests/src/Kernel/ResourceType/RelatedResourceTypesTest.php
ResourceTypeRepositoryTest.php in core/modules/jsonapi/tests/src/Kernel/ResourceType/ResourceTypeRepositoryTest.php
TemporaryJsonapiFileFieldUploaderTest.php in core/modules/jsonapi/tests/src/Kernel/Controller/TemporaryJsonapiFileFieldUploaderTest.php

... See full list

File

core/modules/jsonapi/tests/src/Kernel/JsonapiKernelTestBase.php, line 14

Namespace

Drupal\Tests\jsonapi\Kernel
View source
abstract class JsonapiKernelTestBase extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'jsonapi',
  ];

  /**
   * Creates a field of an entity reference field storage on the bundle.
   *
   * @param string $entity_type
   *   The type of entity the field will be attached to.
   * @param string $bundle
   *   The bundle name of the entity the field will be attached to.
   * @param string $field_name
   *   The name of the field; if it exists, a new instance of the existing.
   *   field will be created.
   * @param string $field_label
   *   The label of the field.
   * @param string $target_entity_type
   *   The type of the referenced entity.
   * @param string $selection_handler
   *   The selection handler used by this field.
   * @param array $handler_settings
   *   An array of settings supported by the selection handler specified above.
   *   (e.g. 'target_bundles', 'sort', 'auto_create', etc).
   * @param int $cardinality
   *   The cardinality of the field.
   *
   * @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase::buildConfigurationForm()
   */
  protected function createEntityReferenceField($entity_type, $bundle, $field_name, $field_label, $target_entity_type, $selection_handler = 'default', array $handler_settings = [], $cardinality = 1) {

    // Look for or add the specified field to the requested entity bundle.
    if (!FieldStorageConfig::loadByName($entity_type, $field_name)) {
      FieldStorageConfig::create([
        'field_name' => $field_name,
        'type' => 'entity_reference',
        'entity_type' => $entity_type,
        'cardinality' => $cardinality,
        'settings' => [
          'target_type' => $target_entity_type,
        ],
      ])
        ->save();
    }
    if (!FieldConfig::loadByName($entity_type, $bundle, $field_name)) {
      FieldConfig::create([
        'field_name' => $field_name,
        'entity_type' => $entity_type,
        'bundle' => $bundle,
        'label' => $field_label,
        'settings' => [
          'handler' => $selection_handler,
          'handler_settings' => $handler_settings,
        ],
      ])
        ->save();
    }
  }

  /**
   * Creates a field of an entity reference field storage on the bundle.
   *
   * @param string $entity_type
   *   The type of entity the field will be attached to.
   * @param string $bundle
   *   The bundle name of the entity the field will be attached to.
   * @param string $field_name
   *   The name of the field; if it exists, a new instance of the existing.
   *   field will be created.
   * @param string $field_label
   *   The label of the field.
   * @param int $cardinality
   *   The cardinality of the field.
   *
   * @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase::buildConfigurationForm()
   */
  protected function createTextField($entity_type, $bundle, $field_name, $field_label, $cardinality = 1) {

    // Look for or add the specified field to the requested entity bundle.
    if (!FieldStorageConfig::loadByName($entity_type, $field_name)) {
      FieldStorageConfig::create([
        'field_name' => $field_name,
        'type' => 'text',
        'entity_type' => $entity_type,
        'cardinality' => $cardinality,
      ])
        ->save();
    }
    if (!FieldConfig::loadByName($entity_type, $bundle, $field_name)) {
      FieldConfig::create([
        'field_name' => $field_name,
        'entity_type' => $entity_type,
        'bundle' => $bundle,
        'label' => $field_label,
      ])
        ->save();
    }
  }

}

Members