function FieldSqlStorageTest::testLongNames

Same name in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldSqlStorageTest::testLongNames()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldSqlStorageTest::testLongNames()
  3. 11.x 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 279

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_multivalue_basefield';
    $this->installEntitySchema('entity_test_multivalue_basefield');
    $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->assertEquals($values[$field_name], $entity->get($field_name)->value);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.