You are here

public function FieldSqlStorageTest::testLongNames in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldSqlStorageTest::testLongNames()

Tests that long entity type and field names do not break.

File

core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php, line 275

Class

FieldSqlStorageTest
Tests Field SQL Storage .

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testLongNames() {

  // Use one of the longest entity_type names in core.
  $entity_type = $bundle = 'entity_test_label_callback';
  $this
    ->installEntitySchema('entity_test_label_callback');
  $storage = $this->container
    ->get('entity_type.manager')
    ->getStorage($entity_type);

  // Create two fields and generate random values.
  $name_base = mb_strtolower($this
    ->randomMachineName(FieldStorageConfig::NAME_MAX_LENGTH - 1));
  $field_names = [];
  $values = [];
  for ($i = 0; $i < 2; $i++) {
    $field_names[$i] = $name_base . $i;
    FieldStorageConfig::create([
      'field_name' => $field_names[$i],
      'entity_type' => $entity_type,
      'type' => 'test_field',
    ])
      ->save();
    FieldConfig::create([
      'field_name' => $field_names[$i],
      'entity_type' => $entity_type,
      'bundle' => $bundle,
    ])
      ->save();
    $values[$field_names[$i]] = mt_rand(1, 127);
  }

  // Save an entity with values.
  $entity = $this->container
    ->get('entity_type.manager')
    ->getStorage($entity_type)
    ->create($values);
  $entity
    ->save();

  // Load the entity back and check the values.
  $entity = $storage
    ->load($entity
    ->id());
  foreach ($field_names as $field_name) {
    $this
      ->assertEqual($entity
      ->get($field_name)->value, $values[$field_name]);
  }
}