You are here

protected function FieldKernelTestBase::createFieldWithStorage in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Kernel/FieldKernelTestBase.php \Drupal\Tests\field\Kernel\FieldKernelTestBase::createFieldWithStorage()

Create a field and an associated field storage.

Parameters

string $suffix: (optional) A string that should only contain characters that are valid in PHP variable names as well.

string $entity_type: (optional) The entity type on which the field should be created. Defaults to "entity_test".

string $bundle: (optional) The entity type on which the field should be created. Defaults to the default bundle of the entity type.

14 calls to FieldKernelTestBase::createFieldWithStorage()
ConfigFieldDefinitionTest::setUp in core/modules/field/tests/src/Kernel/ConfigFieldDefinitionTest.php
Set the default field storage backend for fields created during tests.
FieldAttachOtherTest::setUp in core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php
Set the default field storage backend for fields created during tests.
FieldAttachOtherTest::testEntityCache in core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php
Tests entity cache.
FieldAttachOtherTest::testEntityDisplayBuild in core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php
Tests rendering fields with EntityDisplay build().
FieldAttachOtherTest::testEntityFormDisplayBuildForm in core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php
Tests \Drupal\Core\Entity\Display\EntityFormDisplayInterface::buildForm().

... See full list

File

core/modules/field/tests/src/Kernel/FieldKernelTestBase.php, line 87

Class

FieldKernelTestBase
Parent class for Field API unit tests.

Namespace

Drupal\Tests\field\Kernel

Code

protected function createFieldWithStorage($suffix = '', $entity_type = 'entity_test', $bundle = NULL) {
  if (empty($bundle)) {
    $bundle = $entity_type;
  }
  $field_name = 'field_name' . $suffix;
  $field_storage = 'field_storage' . $suffix;
  $field_storage_uuid = 'field_storage_uuid' . $suffix;
  $field = 'field' . $suffix;
  $field_definition = 'field_definition' . $suffix;
  $this->fieldTestData->{$field_name} = mb_strtolower($this
    ->randomMachineName() . '_field_name' . $suffix);
  $this->fieldTestData->{$field_storage} = FieldStorageConfig::create([
    'field_name' => $this->fieldTestData->{$field_name},
    'entity_type' => $entity_type,
    'type' => 'test_field',
    'cardinality' => 4,
  ]);
  $this->fieldTestData->{$field_storage}
    ->save();
  $this->fieldTestData->{$field_storage_uuid} = $this->fieldTestData->{$field_storage}
    ->uuid();
  $this->fieldTestData->{$field_definition} = [
    'field_storage' => $this->fieldTestData->{$field_storage},
    'bundle' => $bundle,
    'label' => $this
      ->randomMachineName() . '_label',
    'description' => $this
      ->randomMachineName() . '_description',
    'settings' => [
      'test_field_setting' => $this
        ->randomMachineName(),
    ],
  ];
  $this->fieldTestData->{$field} = FieldConfig::create($this->fieldTestData->{$field_definition});
  $this->fieldTestData->{$field}
    ->save();
  \Drupal::service('entity_display.repository')
    ->getFormDisplay($entity_type, $bundle)
    ->setComponent($this->fieldTestData->{$field_name}, [
    'type' => 'test_field_widget',
    'settings' => [
      'test_widget_setting' => $this
        ->randomMachineName(),
    ],
  ])
    ->save();
}