You are here

protected function FieldUnitTestBase::createFieldWithStorage in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/FieldUnitTestBase.php \Drupal\field\Tests\FieldUnitTestBase::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 FieldUnitTestBase::createFieldWithStorage()
ConfigFieldDefinitionTest::setUp in core/modules/field/src/Tests/ConfigFieldDefinitionTest.php
Set the default field storage backend for fields created during tests.
FieldAttachOtherTest::setUp in core/modules/field/src/Tests/FieldAttachOtherTest.php
Set the default field storage backend for fields created during tests.
FieldAttachOtherTest::testEntityCache in core/modules/field/src/Tests/FieldAttachOtherTest.php
Test entity cache.
FieldAttachOtherTest::testEntityDisplayBuild in core/modules/field/src/Tests/FieldAttachOtherTest.php
Test rendering fields with EntityDisplay build().
FieldAttachOtherTest::testEntityFormDisplayBuildForm in core/modules/field/src/Tests/FieldAttachOtherTest.php
Tests \Drupal\Core\Entity\Display\EntityFormDisplayInterface::buildForm().

... See full list

File

core/modules/field/src/Tests/FieldUnitTestBase.php, line 83
Contains \Drupal\field\Tests\FieldUnitTestBase.

Class

FieldUnitTestBase
Parent class for Field API unit tests.

Namespace

Drupal\field\Tests

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} = Unicode::strtolower($this
    ->randomMachineName() . '_field_name' . $suffix);
  $this->fieldTestData->{$field_storage} = entity_create('field_storage_config', array(
    '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} = array(
    'field_storage' => $this->fieldTestData->{$field_storage},
    'bundle' => $bundle,
    'label' => $this
      ->randomMachineName() . '_label',
    'description' => $this
      ->randomMachineName() . '_description',
    'settings' => array(
      'test_field_setting' => $this
        ->randomMachineName(),
    ),
  );
  $this->fieldTestData->{$field} = entity_create('field_config', $this->fieldTestData->{$field_definition});
  $this->fieldTestData->{$field}
    ->save();
  entity_get_form_display($entity_type, $bundle, 'default')
    ->setComponent($this->fieldTestData->{$field_name}, array(
    'type' => 'test_field_widget',
    'settings' => array(
      'test_widget_setting' => $this
        ->randomMachineName(),
    ),
  ))
    ->save();
}