View source
<?php
namespace Drupal\KernelTests\Core\Entity;
use Drupal\Core\Database\Database;
use Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
class FieldSqlStorageTest extends EntityKernelTestBase {
protected static $modules = [
'field',
'field_test',
'text',
'entity_test',
];
protected $fieldName;
protected $fieldCardinality;
protected $fieldStorage;
protected $field;
protected $table;
protected $revisionTable;
protected $tableMapping;
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('entity_test_rev');
$entity_type = 'entity_test_rev';
$this->fieldName = strtolower($this
->randomMachineName());
$this->fieldCardinality = 4;
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $this->fieldName,
'entity_type' => $entity_type,
'type' => 'test_field',
'cardinality' => $this->fieldCardinality,
]);
$this->fieldStorage
->save();
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'bundle' => $entity_type,
]);
$this->field
->save();
$table_mapping = \Drupal::entityTypeManager()
->getStorage($entity_type)
->getTableMapping();
$this->tableMapping = $table_mapping;
$this->table = $table_mapping
->getDedicatedDataTableName($this->fieldStorage);
$this->revisionTable = $table_mapping
->getDedicatedRevisionTableName($this->fieldStorage);
}
public function testFieldLoad() {
$entity_type = $bundle = 'entity_test_rev';
$storage = $this->container
->get('entity_type.manager')
->getStorage($entity_type);
$columns = [
'bundle',
'deleted',
'entity_id',
'revision_id',
'delta',
'langcode',
$this->tableMapping
->getFieldColumnName($this->fieldStorage, 'value'),
];
$revision_ids = [];
$entity = $this->container
->get('entity_type.manager')
->getStorage($entity_type)
->create();
$entity
->save();
$revision_ids[] = $entity
->getRevisionId();
for ($i = 0; $i < 4; $i++) {
$entity
->setNewRevision();
$entity
->save();
$revision_ids[] = $entity
->getRevisionId();
}
$values = [];
$connection = Database::getConnection();
$query = $connection
->insert($this->revisionTable)
->fields($columns);
foreach ($revision_ids as $revision_id) {
for ($delta = 0; $delta <= $this->fieldCardinality; $delta++) {
$value = mt_rand(1, 127);
$values[$revision_id][] = $value;
$query
->values([
$bundle,
0,
$entity
->id(),
$revision_id,
$delta,
$entity
->language()
->getId(),
$value,
]);
}
$query
->execute();
}
$query = $connection
->insert($this->table)
->fields($columns);
foreach ($values[$revision_id] as $delta => $value) {
$query
->values([
$bundle,
0,
$entity
->id(),
$revision_id,
$delta,
$entity
->language()
->getId(),
$value,
]);
}
$query
->execute();
foreach ($revision_ids as $revision_id) {
$entity = $storage
->loadRevision($revision_id);
foreach ($values[$revision_id] as $delta => $value) {
if ($delta < $this->fieldCardinality) {
$this
->assertEquals($value, $entity->{$this->fieldName}[$delta]->value);
}
else {
$this
->assertArrayNotHasKey($delta, $entity->{$this->fieldName});
}
}
}
$entity = $storage
->load($entity
->id());
foreach ($values[$revision_id] as $delta => $value) {
if ($delta < $this->fieldCardinality) {
$this
->assertEquals($value, $entity->{$this->fieldName}[$delta]->value);
}
else {
$this
->assertArrayNotHasKey($delta, $entity->{$this->fieldName});
}
}
$unavailable_langcode = 'xx';
$values = [
$bundle,
0,
$entity
->id(),
$entity
->getRevisionId(),
0,
$unavailable_langcode,
mt_rand(1, 127),
];
$connection
->insert($this->table)
->fields($columns)
->values($values)
->execute();
$connection
->insert($this->revisionTable)
->fields($columns)
->values($values)
->execute();
$entity = $storage
->load($entity
->id());
$this
->assertArrayNotHasKey($unavailable_langcode, $entity->{$this->fieldName});
}
public function testFieldWrite() {
$entity_type = $bundle = 'entity_test_rev';
$entity = $this->container
->get('entity_type.manager')
->getStorage($entity_type)
->create();
$revision_values = [];
$values = [];
for ($delta = 0; $delta <= $this->fieldCardinality; $delta++) {
$values[$delta]['value'] = mt_rand(1, 127);
}
$entity->{$this->fieldName} = $values;
$entity
->save();
$connection = Database::getConnection();
$rows = $connection
->select($this->table, 't')
->fields('t')
->execute()
->fetchAllAssoc('delta', \PDO::FETCH_ASSOC);
$this
->assertCount($this->fieldCardinality, $rows);
foreach ($rows as $delta => $row) {
$expected = [
'bundle' => $bundle,
'deleted' => 0,
'entity_id' => $entity
->id(),
'revision_id' => $entity
->getRevisionId(),
'langcode' => $entity
->language()
->getId(),
'delta' => $delta,
$this->fieldName . '_value' => $values[$delta]['value'],
];
$this
->assertEquals($expected, $row, "Row {$delta} was stored as expected.");
}
$values = [];
for ($delta = 0; $delta <= $this->fieldCardinality - 2; $delta++) {
$values[$delta]['value'] = mt_rand(1, 127);
}
$values_count = count($values);
$entity->{$this->fieldName} = $values;
$entity
->save();
$rows = $connection
->select($this->table, 't')
->fields('t')
->execute()
->fetchAllAssoc('delta', \PDO::FETCH_ASSOC);
$this
->assertCount($values_count, $rows);
foreach ($rows as $delta => $row) {
$expected = [
'bundle' => $bundle,
'deleted' => 0,
'entity_id' => $entity
->id(),
'revision_id' => $entity
->getRevisionId(),
'langcode' => $entity
->language()
->getId(),
'delta' => $delta,
$this->fieldName . '_value' => $values[$delta]['value'],
];
$this
->assertEquals($expected, $row, "Row {$delta} was stored as expected.");
}
$revision_values[$entity
->getRevisionId()] = $values;
$values = [];
for ($delta = 0; $delta < $this->fieldCardinality; $delta++) {
$values[$delta]['value'] = mt_rand(1, 127);
}
$entity->{$this->fieldName} = $values;
$entity
->setNewRevision();
$entity
->save();
$revision_values[$entity
->getRevisionId()] = $values;
foreach ($revision_values as $revision_id => $values) {
$rows = $connection
->select($this->revisionTable, 't')
->fields('t')
->condition('revision_id', $revision_id)
->execute()
->fetchAllAssoc('delta', \PDO::FETCH_ASSOC);
$this
->assertCount(min(count($values), $this->fieldCardinality), $rows);
foreach ($rows as $delta => $row) {
$expected = [
'bundle' => $bundle,
'deleted' => 0,
'entity_id' => $entity
->id(),
'revision_id' => $revision_id,
'langcode' => $entity
->language()
->getId(),
'delta' => $delta,
$this->fieldName . '_value' => $values[$delta]['value'],
];
$this
->assertEquals($expected, $row, "Row {$delta} was stored as expected.");
}
}
$entity->{$this->fieldName} = NULL;
$entity
->save();
$rows = $connection
->select($this->table, 't')
->fields('t')
->execute()
->fetchAllAssoc('delta', \PDO::FETCH_ASSOC);
$this
->assertCount(0, $rows);
}
public function testLongNames() {
$entity_type = $bundle = 'entity_test_multivalue_basefield';
$this
->installEntitySchema('entity_test_multivalue_basefield');
$storage = $this->container
->get('entity_type.manager')
->getStorage($entity_type);
$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);
}
$entity = $this->container
->get('entity_type.manager')
->getStorage($entity_type)
->create($values);
$entity
->save();
$entity = $storage
->load($entity
->id());
foreach ($field_names as $field_name) {
$this
->assertEquals($values[$field_name], $entity
->get($field_name)->value);
}
}
public function testUpdateFieldSchemaWithData() {
$entity_type = 'entity_test_rev';
$field_storage = FieldStorageConfig::create([
'field_name' => 'decimal52',
'entity_type' => $entity_type,
'type' => 'decimal',
'settings' => [
'precision' => 5,
'scale' => 2,
],
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => $entity_type,
]);
$field
->save();
$entity = $this->container
->get('entity_type.manager')
->getStorage($entity_type)
->create([
'id' => 0,
'revision_id' => 0,
]);
$entity->decimal52->value = '1.235';
$entity
->save();
$field_storage
->setSetting('scale', 3);
$this
->expectException(FieldStorageDefinitionUpdateForbiddenException::class);
$field_storage
->save();
}
public function testFieldUpdateFailure() {
$field_storage = FieldStorageConfig::create([
'field_name' => 'test_text',
'entity_type' => 'entity_test_rev',
'type' => 'text',
'settings' => [
'max_length' => 255,
],
]);
$field_storage
->save();
$prior_field_storage = $field_storage;
$field_storage
->setSetting('max_length', '-1)');
try {
$field_storage
->save();
$this
->fail('Update succeeded.');
} catch (\Exception $e) {
}
$tables = [
$this->tableMapping
->getDedicatedDataTableName($prior_field_storage),
$this->tableMapping
->getDedicatedRevisionTableName($prior_field_storage),
];
$schema = Database::getConnection()
->schema();
foreach ($tables as $table_name) {
$this
->assertTrue($schema
->tableExists($table_name), 'Table $table_name exists.');
}
}
public function testFieldUpdateIndexesWithData() {
$field_name = 'testfield';
$entity_type = 'entity_test_rev';
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => $entity_type,
'type' => 'text',
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => $entity_type,
]);
$field
->save();
$tables = [
$this->tableMapping
->getDedicatedDataTableName($field_storage),
$this->tableMapping
->getDedicatedRevisionTableName($field_storage),
];
foreach ($tables as $table) {
$this
->assertFalse(Database::getConnection()
->schema()
->indexExists($table, 'value'), 'No index named value exists in $table');
$this
->assertFalse(Database::getConnection()
->schema()
->indexExists($table, 'value_format'), 'No index named value_format exists in $table');
}
$entity = $this->container
->get('entity_type.manager')
->getStorage($entity_type)
->create([
'id' => 1,
'revision_id' => 1,
]);
$entity->{$field_name}->value = 'field data';
$entity
->enforceIsNew();
$entity
->save();
$field_storage
->setIndexes([
'value' => [
[
'value',
255,
],
],
]);
$field_storage
->save();
foreach ($tables as $table) {
$this
->assertTrue(Database::getConnection()
->schema()
->indexExists($table, "{$field_name}_value"), "Index on value created in @table", [
'@table' => $table,
]);
}
$field_storage
->setIndexes([
'value_format' => [
[
'value',
127,
],
[
'format',
127,
],
],
]);
$field_storage
->save();
foreach ($tables as $table) {
$this
->assertTrue(Database::getConnection()
->schema()
->indexExists($table, "{$field_name}_value_format"), "Index on value_format created in @table", [
'@table' => $table,
]);
$this
->assertFalse(Database::getConnection()
->schema()
->indexExists($table, "{$field_name}_value"), "Index on value removed in @table", [
'@table' => $table,
]);
}
$entity = $this->container
->get('entity_type.manager')
->getStorage($entity_type)
->load(1);
$this
->assertEquals('field data', $entity->{$field_name}->value);
}
public function testFieldSqlStorageForeignKeys() {
$field_name = 'testfield';
$foreign_key_name = 'shape';
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'shape',
'settings' => [
'foreign_key_name' => $foreign_key_name,
],
]);
$field_storage
->save();
$schema = $field_storage
->getSchema();
$this
->assertEquals($foreign_key_name, $schema['foreign keys'][$foreign_key_name]['table'], 'Foreign key table name preserved through CRUD');
$this
->assertEquals('id', $schema['foreign keys'][$foreign_key_name]['columns'][$foreign_key_name], 'Foreign key column name preserved through CRUD');
$foreign_key_name = 'color';
$field_storage
->setSetting('foreign_key_name', $foreign_key_name);
$field_storage
->save();
$schema = $field_storage
->getSchema();
$this
->assertEquals($foreign_key_name, $schema['foreign keys'][$foreign_key_name]['table'], 'Foreign key table name modified after update');
$this
->assertEquals('id', $schema['foreign keys'][$foreign_key_name]['columns'][$foreign_key_name], 'Foreign key column name modified after update');
}
public function testTableNames() {
$entity_type = 'short_entity_type';
$field_name = 'short_field_name';
$field_storage = FieldStorageConfig::create([
'entity_type' => $entity_type,
'field_name' => $field_name,
'type' => 'test_field',
]);
$expected = 'short_entity_type__short_field_name';
$this
->assertEquals($expected, $this->tableMapping
->getDedicatedDataTableName($field_storage));
$expected = 'short_entity_type_revision__short_field_name';
$this
->assertEquals($expected, $this->tableMapping
->getDedicatedRevisionTableName($field_storage));
$entity_type = 'short_entity_type';
$field_name = 'long_field_name_abcdefghijklmnopqrstuvwxyz';
$field_storage = FieldStorageConfig::create([
'entity_type' => $entity_type,
'field_name' => $field_name,
'type' => 'test_field',
]);
$expected = 'short_entity_type__' . substr(hash('sha256', $field_storage
->uuid()), 0, 10);
$this
->assertEquals($expected, $this->tableMapping
->getDedicatedDataTableName($field_storage));
$expected = 'short_entity_type_r__' . substr(hash('sha256', $field_storage
->uuid()), 0, 10);
$this
->assertEquals($expected, $this->tableMapping
->getDedicatedRevisionTableName($field_storage));
$entity_type = 'long_entity_type_abcdefghijklmnopqrstuvwxyz';
$field_name = 'short_field_name';
$field_storage = FieldStorageConfig::create([
'entity_type' => $entity_type,
'field_name' => $field_name,
'type' => 'test_field',
]);
$expected = 'long_entity_type_abcdefghijklmno__' . substr(hash('sha256', $field_storage
->uuid()), 0, 10);
$this
->assertEquals($expected, $this->tableMapping
->getDedicatedDataTableName($field_storage));
$expected = 'long_entity_type_abcdefghijklmno_r__' . substr(hash('sha256', $field_storage
->uuid()), 0, 10);
$this
->assertEquals($expected, $this->tableMapping
->getDedicatedRevisionTableName($field_storage));
$entity_type = 'long_entity_type_abcdefghijklmnopqrstuvwxyz';
$field_name = 'long_field_name_abcdefghijklmnopqrstuvwxyz';
$field_storage = FieldStorageConfig::create([
'entity_type' => $entity_type,
'field_name' => $field_name,
'type' => 'test_field',
]);
$expected = 'long_entity_type_abcdefghijklmno__' . substr(hash('sha256', $field_storage
->uuid()), 0, 10);
$this
->assertEquals($expected, $this->tableMapping
->getDedicatedDataTableName($field_storage));
$expected = 'long_entity_type_abcdefghijklmno_r__' . substr(hash('sha256', $field_storage
->uuid()), 0, 10);
$this
->assertEquals($expected, $this->tableMapping
->getDedicatedRevisionTableName($field_storage));
$field_storage2 = FieldStorageConfig::create([
'entity_type' => $entity_type,
'field_name' => $field_name . '2',
'type' => 'test_field',
]);
$this
->assertNotEquals($this->tableMapping
->getDedicatedDataTableName($field_storage), $this->tableMapping
->getDedicatedDataTableName($field_storage2));
$this
->assertNotEquals($this->tableMapping
->getDedicatedRevisionTableName($field_storage), $this->tableMapping
->getDedicatedRevisionTableName($field_storage2));
$field_storage = FieldStorageConfig::create([
'entity_type' => 'some_entity_type',
'field_name' => 'some_field_name',
'type' => 'test_field',
'deleted' => TRUE,
]);
$expected = 'field_deleted_data_' . substr(hash('sha256', $field_storage
->uuid()), 0, 10);
$this
->assertEquals($expected, $this->tableMapping
->getDedicatedDataTableName($field_storage, TRUE));
$expected = 'field_deleted_revision_' . substr(hash('sha256', $field_storage
->uuid()), 0, 10);
$this
->assertEquals($expected, $this->tableMapping
->getDedicatedRevisionTableName($field_storage, TRUE));
$field_storage = FieldStorageConfig::create([
'entity_type' => 'entity_test_rev',
'field_name' => 'some_field_name',
'type' => 'test_field',
'cardinality' => 2,
]);
$field_storage
->save();
$table_mapping = \Drupal::entityTypeManager()
->getStorage('entity_test_rev')
->getTableMapping();
$this
->assertEquals($table_mapping
->getDedicatedDataTableName($field_storage), $table_mapping
->getFieldTableName('some_field_name'));
}
}