You are here

public function FieldStorageCrudTest::testCreate in Drupal 10

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

Tests the creation of a field storage.

File

core/modules/field/tests/src/Kernel/FieldStorageCrudTest.php, line 34

Class

FieldStorageCrudTest
Tests field storage create, read, update, and delete.

Namespace

Drupal\Tests\field\Kernel

Code

public function testCreate() {
  $field_storage_definition = [
    'field_name' => 'field_2',
    'entity_type' => 'entity_test',
    'type' => 'test_field',
  ];
  field_test_memorize();
  $field_storage = FieldStorageConfig::create($field_storage_definition);
  $field_storage
    ->save();
  $field_storage = FieldStorageConfig::load($field_storage
    ->id());
  $this
    ->assertEquals('TRUE', $field_storage
    ->getSetting('storage_setting_from_config_data'));
  $this
    ->assertNull($field_storage
    ->getSetting('config_data_from_storage_setting'));
  $mem = field_test_memorize();
  $this
    ->assertSame($field_storage_definition['field_name'], $mem['field_test_field_storage_config_create'][0][0]
    ->getName(), 'hook_entity_create() called with correct arguments.');
  $this
    ->assertSame($field_storage_definition['type'], $mem['field_test_field_storage_config_create'][0][0]
    ->getType(), 'hook_entity_create() called with correct arguments.');

  // Read the configuration. Check against raw configuration data rather than
  // the loaded ConfigEntity, to be sure we check that the defaults are
  // applied on write.
  $field_storage_config = $this
    ->config('field.storage.' . $field_storage
    ->id())
    ->get();
  $this
    ->assertTrue($field_storage_config['settings']['config_data_from_storage_setting']);
  $this
    ->assertTrue(!isset($field_storage_config['settings']['storage_setting_from_config_data']));

  // Since we are working with raw configuration, this needs to be unset
  // manually.
  // @see Drupal\field_test\Plugin\Field\FieldType\TestItem::storageSettingsFromConfigData()
  unset($field_storage_config['settings']['config_data_from_storage_setting']);

  // Ensure that basic properties are preserved.
  $this
    ->assertEquals($field_storage_definition['field_name'], $field_storage_config['field_name'], 'The field name is properly saved.');
  $this
    ->assertEquals($field_storage_definition['entity_type'], $field_storage_config['entity_type'], 'The field entity type is properly saved.');
  $this
    ->assertEquals($field_storage_definition['entity_type'] . '.' . $field_storage_definition['field_name'], $field_storage_config['id'], 'The field id is properly saved.');
  $this
    ->assertEquals($field_storage_definition['type'], $field_storage_config['type'], 'The field type is properly saved.');

  // Ensure that cardinality defaults to 1.
  $this
    ->assertEquals(1, $field_storage_config['cardinality'], 'Cardinality defaults to 1.');

  // Ensure that default settings are present.
  $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
  $this
    ->assertEquals($field_type_manager
    ->getDefaultStorageSettings($field_storage_definition['type']), $field_storage_config['settings'], 'Default storage settings have been written.');

  // Guarantee that the name is unique.
  try {
    FieldStorageConfig::create($field_storage_definition)
      ->save();
    $this
      ->fail('Cannot create two fields with the same name.');
  } catch (\Exception $e) {
    $this
      ->assertInstanceOf(EntityStorageException::class, $e);
  }

  // Check that field type is required.
  try {
    $field_storage_definition = [
      'field_name' => 'field_1',
      'entity_type' => 'entity_type',
    ];
    FieldStorageConfig::create($field_storage_definition)
      ->save();
    $this
      ->fail('Cannot create a field with no type.');
  } catch (\Exception $e) {
    $this
      ->assertInstanceOf(FieldException::class, $e);
  }

  // Check that field name is required.
  try {
    $field_storage_definition = [
      'type' => 'test_field',
      'entity_type' => 'entity_test',
    ];
    FieldStorageConfig::create($field_storage_definition)
      ->save();
    $this
      ->fail('Cannot create an unnamed field.');
  } catch (\Exception $e) {
    $this
      ->assertInstanceOf(FieldException::class, $e);
  }

  // Check that entity type is required.
  try {
    $field_storage_definition = [
      'field_name' => 'test_field',
      'type' => 'test_field',
    ];
    FieldStorageConfig::create($field_storage_definition)
      ->save();
    $this
      ->fail('Cannot create a field without an entity type.');
  } catch (\Exception $e) {
    $this
      ->assertInstanceOf(FieldException::class, $e);
  }

  // Check that field name must start with a letter or _.
  try {
    $field_storage_definition = [
      'field_name' => '2field_2',
      'entity_type' => 'entity_test',
      'type' => 'test_field',
    ];
    FieldStorageConfig::create($field_storage_definition)
      ->save();
    $this
      ->fail('Cannot create a field with a name starting with a digit.');
  } catch (\Exception $e) {
    $this
      ->assertInstanceOf(FieldException::class, $e);
  }

  // Check that field name must only contain lowercase alphanumeric or _.
  try {
    $field_storage_definition = [
      'field_name' => 'field#_3',
      'entity_type' => 'entity_test',
      'type' => 'test_field',
    ];
    FieldStorageConfig::create($field_storage_definition)
      ->save();
    $this
      ->fail('Cannot create a field with a name containing an illegal character.');
  } catch (\Exception $e) {
    $this
      ->assertInstanceOf(FieldException::class, $e);
  }

  // Check that field name cannot be longer than 32 characters long.
  try {
    $field_storage_definition = [
      'field_name' => '_12345678901234567890123456789012',
      'entity_type' => 'entity_test',
      'type' => 'test_field',
    ];
    FieldStorageConfig::create($field_storage_definition)
      ->save();
    $this
      ->fail('Cannot create a field with a name longer than 32 characters.');
  } catch (\Exception $e) {
    $this
      ->assertInstanceOf(FieldException::class, $e);
  }

  // Check that field name can not be an entity key.
  // "id" is known as an entity key from the "entity_test" type.
  try {
    $field_storage_definition = [
      'type' => 'test_field',
      'field_name' => 'id',
      'entity_type' => 'entity_test',
    ];
    FieldStorageConfig::create($field_storage_definition)
      ->save();
    $this
      ->fail('Cannot create a field bearing the name of an entity key.');
  } catch (\Exception $e) {
    $this
      ->assertInstanceOf(FieldException::class, $e);
  }
}