View source
<?php
namespace Drupal\system\Tests\Entity;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Database\Database;
use Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException;
use Drupal\field\Entity\FieldStorageConfig;
class FieldSqlStorageTest extends EntityUnitTestBase {
public static $modules = array(
'field',
'field_test',
'text',
'entity_test',
);
protected $fieldName;
protected $fieldCardinality;
protected $fieldStorage;
protected $field;
protected $table;
protected $revisionTable;
protected $tableMapping;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('entity_test_rev');
$entity_type = 'entity_test_rev';
$this->fieldName = strtolower($this
->randomMachineName());
$this->fieldCardinality = 4;
$this->fieldStorage = entity_create('field_storage_config', array(
'field_name' => $this->fieldName,
'entity_type' => $entity_type,
'type' => 'test_field',
'cardinality' => $this->fieldCardinality,
));
$this->fieldStorage
->save();
$this->field = entity_create('field_config', array(
'field_storage' => $this->fieldStorage,
'bundle' => $entity_type,
));
$this->field
->save();
$table_mapping = \Drupal::entityManager()
->getStorage($entity_type)
->getTableMapping();
$this->tableMapping = $table_mapping;
$this->table = $table_mapping
->getDedicatedDataTableName($this->fieldStorage);
$this->revisionTable = $table_mapping
->getDedicatedRevisionTableName($this->fieldStorage);
}
function testFieldLoad() {
$entity_type = $bundle = 'entity_test_rev';
$storage = $this->container
->get('entity.manager')
->getStorage($entity_type);
$columns = array(
'bundle',
'deleted',
'entity_id',
'revision_id',
'delta',
'langcode',
$this->tableMapping
->getFieldColumnName($this->fieldStorage, 'value'),
);
$revision_ids = array();
$entity = entity_create($entity_type);
$entity
->save();
$revision_ids[] = $entity
->getRevisionId();
for ($i = 0; $i < 4; $i++) {
$entity
->setNewRevision();
$entity
->save();
$revision_ids[] = $entity
->getRevisionId();
}
$values = array();
$query = db_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(array(
$bundle,
0,
$entity
->id(),
$revision_id,
$delta,
$entity
->language()
->getId(),
$value,
));
}
$query
->execute();
}
$query = db_insert($this->table)
->fields($columns);
foreach ($values[$revision_id] as $delta => $value) {
$query
->values(array(
$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
->assertEqual($entity->{$this->fieldName}[$delta]->value, $value);
}
else {
$this
->assertFalse(array_key_exists($delta, $entity->{$this->fieldName}));
}
}
}
$entity = $storage
->load($entity
->id());
foreach ($values[$revision_id] as $delta => $value) {
if ($delta < $this->fieldCardinality) {
$this
->assertEqual($entity->{$this->fieldName}[$delta]->value, $value);
}
else {
$this
->assertFalse(array_key_exists($delta, $entity->{$this->fieldName}));
}
}
$unavailable_langcode = 'xx';
$values = array(
$bundle,
0,
$entity
->id(),
$entity
->getRevisionId(),
0,
$unavailable_langcode,
mt_rand(1, 127),
);
db_insert($this->table)
->fields($columns)
->values($values)
->execute();
db_insert($this->revisionTable)
->fields($columns)
->values($values)
->execute();
$entity = $storage
->load($entity
->id());
$this
->assertFalse(array_key_exists($unavailable_langcode, $entity->{$this->fieldName}));
}
function testFieldWrite() {
$entity_type = $bundle = 'entity_test_rev';
$entity = entity_create($entity_type);
$revision_values = array();
$values = array();
for ($delta = 0; $delta <= $this->fieldCardinality; $delta++) {
$values[$delta]['value'] = mt_rand(1, 127);
}
$entity->{$this->fieldName} = $values;
$entity
->save();
$rows = db_select($this->table, 't')
->fields('t')
->execute()
->fetchAllAssoc('delta', \PDO::FETCH_ASSOC);
$this
->assertEqual(count($rows), $this->fieldCardinality);
foreach ($rows as $delta => $row) {
$expected = array(
'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
->assertEqual($row, $expected, "Row {$delta} was stored as expected.");
}
$values = array();
for ($delta = 0; $delta <= $this->fieldCardinality - 2; $delta++) {
$values[$delta]['value'] = mt_rand(1, 127);
}
$entity->{$this->fieldName} = $values;
$entity
->save();
$rows = db_select($this->table, 't')
->fields('t')
->execute()
->fetchAllAssoc('delta', \PDO::FETCH_ASSOC);
$this
->assertEqual(count($rows), count($values));
foreach ($rows as $delta => $row) {
$expected = array(
'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
->assertEqual($row, $expected, "Row {$delta} was stored as expected.");
}
$revision_values[$entity
->getRevisionId()] = $values;
$values = array();
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 = db_select($this->revisionTable, 't')
->fields('t')
->condition('revision_id', $revision_id)
->execute()
->fetchAllAssoc('delta', \PDO::FETCH_ASSOC);
$this
->assertEqual(count($rows), min(count($values), $this->fieldCardinality));
foreach ($rows as $delta => $row) {
$expected = array(
'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
->assertEqual($row, $expected, "Row {$delta} was stored as expected.");
}
}
$entity->{$this->fieldName} = NULL;
$entity
->save();
$rows = db_select($this->table, 't')
->fields('t')
->execute()
->fetchAllAssoc('delta', \PDO::FETCH_ASSOC);
$this
->assertEqual(count($rows), 0);
}
function testLongNames() {
$entity_type = $bundle = 'entity_test_label_callback';
$storage = $this->container
->get('entity.manager')
->getStorage($entity_type);
$name_base = Unicode::strtolower($this
->randomMachineName(FieldStorageConfig::NAME_MAX_LENGTH - 1));
$field_names = array();
$values = array();
for ($i = 0; $i < 2; $i++) {
$field_names[$i] = $name_base . $i;
entity_create('field_storage_config', array(
'field_name' => $field_names[$i],
'entity_type' => $entity_type,
'type' => 'test_field',
))
->save();
entity_create('field_config', array(
'field_name' => $field_names[$i],
'entity_type' => $entity_type,
'bundle' => $bundle,
))
->save();
$values[$field_names[$i]] = mt_rand(1, 127);
}
$entity = entity_create($entity_type, $values);
$entity
->save();
$entity = $storage
->load($entity
->id());
foreach ($field_names as $field_name) {
$this
->assertEqual($entity
->get($field_name)->value, $values[$field_name]);
}
}
function testUpdateFieldSchemaWithData() {
$entity_type = 'entity_test_rev';
$field_storage = entity_create('field_storage_config', array(
'field_name' => 'decimal52',
'entity_type' => $entity_type,
'type' => 'decimal',
'settings' => array(
'precision' => 5,
'scale' => 2,
),
));
$field_storage
->save();
$field = entity_create('field_config', array(
'field_storage' => $field_storage,
'bundle' => $entity_type,
));
$field
->save();
$entity = entity_create($entity_type, array(
'id' => 0,
'revision_id' => 0,
));
$entity->decimal52->value = '1.235';
$entity
->save();
$field_storage
->setSetting('scale', 3);
try {
$field_storage
->save();
$this
->fail(t('Cannot update field schema with data.'));
} catch (FieldStorageDefinitionUpdateForbiddenException $e) {
$this
->pass(t('Cannot update field schema with data.'));
}
}
function testFieldUpdateFailure() {
$field_storage = entity_create('field_storage_config', array(
'field_name' => 'test_text',
'entity_type' => 'entity_test_rev',
'type' => 'text',
'settings' => array(
'max_length' => 255,
),
));
$field_storage
->save();
$prior_field_storage = $field_storage;
$field_storage
->setSetting('max_length', '-1)');
try {
$field_storage
->save();
$this
->fail(t('Update succeeded.'));
} catch (\Exception $e) {
$this
->pass(t('Update properly failed.'));
}
$tables = array(
$this->tableMapping
->getDedicatedDataTableName($prior_field_storage),
$this->tableMapping
->getDedicatedRevisionTableName($prior_field_storage),
);
foreach ($tables as $table_name) {
$this
->assertTrue(db_table_exists($table_name), t('Table %table exists.', array(
'%table' => $table_name,
)));
}
}
function testFieldUpdateIndexesWithData() {
$field_name = 'testfield';
$entity_type = 'entity_test_rev';
$field_storage = entity_create('field_storage_config', array(
'field_name' => $field_name,
'entity_type' => $entity_type,
'type' => 'text',
));
$field_storage
->save();
$field = entity_create('field_config', array(
'field_storage' => $field_storage,
'bundle' => $entity_type,
));
$field
->save();
$tables = array(
$this->tableMapping
->getDedicatedDataTableName($field_storage),
$this->tableMapping
->getDedicatedRevisionTableName($field_storage),
);
foreach ($tables as $table) {
$this
->assertFalse(Database::getConnection()
->schema()
->indexExists($table, 'value'), t("No index named value exists in @table", array(
'@table' => $table,
)));
$this
->assertFalse(Database::getConnection()
->schema()
->indexExists($table, 'value_format'), t("No index named value_format exists in @table", array(
'@table' => $table,
)));
}
$entity = entity_create($entity_type, array(
'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"), t("Index on value created in @table", array(
'@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"), t("Index on value_format created in @table", array(
'@table' => $table,
)));
$this
->assertFalse(Database::getConnection()
->schema()
->indexExists($table, "{$field_name}_value"), t("Index on value removed in @table", array(
'@table' => $table,
)));
}
$entity = $this->container
->get('entity.manager')
->getStorage($entity_type)
->load(1);
$this
->assertEqual($entity->{$field_name}->value, 'field data', t("Index changes performed without dropping the tables"));
}
function testFieldSqlStorageForeignKeys() {
$field_name = 'testfield';
$foreign_key_name = 'shape';
$field_storage = entity_create('field_storage_config', array(
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'shape',
'settings' => array(
'foreign_key_name' => $foreign_key_name,
),
));
$field_storage
->save();
$schema = $field_storage
->getSchema();
$this
->assertEqual($schema['foreign keys'][$foreign_key_name]['table'], $foreign_key_name, 'Foreign key table name preserved through CRUD');
$this
->assertEqual($schema['foreign keys'][$foreign_key_name]['columns'][$foreign_key_name], 'id', '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
->assertEqual($schema['foreign keys'][$foreign_key_name]['table'], $foreign_key_name, 'Foreign key table name modified after update');
$this
->assertEqual($schema['foreign keys'][$foreign_key_name]['columns'][$foreign_key_name], 'id', 'Foreign key column name modified after update');
}
public function testTableNames() {
$entity_type = 'short_entity_type';
$field_name = 'short_field_name';
$field_storage = entity_create('field_storage_config', array(
'entity_type' => $entity_type,
'field_name' => $field_name,
'type' => 'test_field',
));
$expected = 'short_entity_type__short_field_name';
$this
->assertEqual($this->tableMapping
->getDedicatedDataTableName($field_storage), $expected);
$expected = 'short_entity_type_revision__short_field_name';
$this
->assertEqual($this->tableMapping
->getDedicatedRevisionTableName($field_storage), $expected);
$entity_type = 'short_entity_type';
$field_name = 'long_field_name_abcdefghijklmnopqrstuvwxyz';
$field_storage = entity_create('field_storage_config', array(
'entity_type' => $entity_type,
'field_name' => $field_name,
'type' => 'test_field',
));
$expected = 'short_entity_type__' . substr(hash('sha256', $field_storage
->uuid()), 0, 10);
$this
->assertEqual($this->tableMapping
->getDedicatedDataTableName($field_storage), $expected);
$expected = 'short_entity_type_r__' . substr(hash('sha256', $field_storage
->uuid()), 0, 10);
$this
->assertEqual($this->tableMapping
->getDedicatedRevisionTableName($field_storage), $expected);
$entity_type = 'long_entity_type_abcdefghijklmnopqrstuvwxyz';
$field_name = 'short_field_name';
$field_storage = entity_create('field_storage_config', array(
'entity_type' => $entity_type,
'field_name' => $field_name,
'type' => 'test_field',
));
$expected = 'long_entity_type_abcdefghijklmnopq__' . substr(hash('sha256', $field_storage
->uuid()), 0, 10);
$this
->assertEqual($this->tableMapping
->getDedicatedDataTableName($field_storage), $expected);
$expected = 'long_entity_type_abcdefghijklmnopq_r__' . substr(hash('sha256', $field_storage
->uuid()), 0, 10);
$this
->assertEqual($this->tableMapping
->getDedicatedRevisionTableName($field_storage), $expected);
$entity_type = 'long_entity_type_abcdefghijklmnopqrstuvwxyz';
$field_name = 'long_field_name_abcdefghijklmnopqrstuvwxyz';
$field_storage = entity_create('field_storage_config', array(
'entity_type' => $entity_type,
'field_name' => $field_name,
'type' => 'test_field',
));
$expected = 'long_entity_type_abcdefghijklmnopq__' . substr(hash('sha256', $field_storage
->uuid()), 0, 10);
$this
->assertEqual($this->tableMapping
->getDedicatedDataTableName($field_storage), $expected);
$expected = 'long_entity_type_abcdefghijklmnopq_r__' . substr(hash('sha256', $field_storage
->uuid()), 0, 10);
$this
->assertEqual($this->tableMapping
->getDedicatedRevisionTableName($field_storage), $expected);
$field_storage2 = entity_create('field_storage_config', array(
'entity_type' => $entity_type,
'field_name' => $field_name . '2',
'type' => 'test_field',
));
$this
->assertNotEqual($this->tableMapping
->getDedicatedDataTableName($field_storage), $this->tableMapping
->getDedicatedDataTableName($field_storage2));
$this
->assertNotEqual($this->tableMapping
->getDedicatedRevisionTableName($field_storage), $this->tableMapping
->getDedicatedRevisionTableName($field_storage2));
$field_storage = entity_create('field_storage_config', array(
'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
->assertEqual($this->tableMapping
->getDedicatedDataTableName($field_storage, TRUE), $expected);
$expected = 'field_deleted_revision_' . substr(hash('sha256', $field_storage
->uuid()), 0, 10);
$this
->assertEqual($this->tableMapping
->getDedicatedRevisionTableName($field_storage, TRUE), $expected);
}
}