View source
<?php
namespace Drupal\KernelTests\Core\Entity\Sql;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
class SqlContentEntityStorageSchemaTest extends EntityKernelTestBase {
protected $installedStorageSchema;
protected $entityDefinitionUpdateManager;
protected function setUp() : void {
parent::setUp();
$key_value_factory = $this->container
->get('keyvalue');
$this->installedStorageSchema = $key_value_factory
->get('entity.storage_schema.sql');
$this->entityDefinitionUpdateManager = $this->container
->get('entity.definition_update_manager');
}
public function testOnFieldStorageDefinitionUpdateShared() {
$field = BaseFieldDefinition::create('shape')
->setSetting('foreign_key_name', NULL)
->setName('shape')
->setProvider('entity_test');
$this->state
->set('entity_test.additional_base_field_definitions', [
'shape' => $field,
]);
$this->entityDefinitionUpdateManager
->installFieldStorageDefinition('shape', 'entity_test', 'entity_test', $field);
$expected = [
'entity_test' => [
'fields' => [
'shape__shape' => [
'type' => 'varchar',
'length' => 32,
'not null' => FALSE,
],
'shape__color' => [
'type' => 'varchar',
'length' => 32,
'not null' => FALSE,
],
],
],
];
$actual = $this->installedStorageSchema
->get('entity_test.field_schema_data.shape');
$this
->assertSame($expected, $actual);
$entity_type = $this->entityDefinitionUpdateManager
->getEntityType('entity_test');
$original_keys = $entity_type
->getKeys();
$entity_type
->set('entity_keys', $original_keys + [
'shape' => 'shape',
]);
$this->entityDefinitionUpdateManager
->updateEntityType($entity_type);
$this->entityDefinitionUpdateManager
->updateFieldStorageDefinition($field);
$expected['entity_test']['fields']['shape__shape']['not null'] = TRUE;
$expected['entity_test']['fields']['shape__color']['not null'] = TRUE;
$actual = $this->installedStorageSchema
->get('entity_test.field_schema_data.shape');
$this
->assertSame($expected, $actual);
$entity_type
->set('entity_keys', $original_keys);
$this->entityDefinitionUpdateManager
->updateEntityType($entity_type);
$this->entityDefinitionUpdateManager
->updateFieldStorageDefinition($field);
$expected['entity_test']['fields']['shape__shape']['not null'] = FALSE;
$expected['entity_test']['fields']['shape__color']['not null'] = FALSE;
$actual = $this->installedStorageSchema
->get('entity_test.field_schema_data.shape');
$this
->assertSame($expected, $actual);
$entity_storage = $this->entityTypeManager
->getStorage('entity_test');
$entity_storage
->create([
'shape' => [
'shape' => 'rectangle',
'color' => 'pink',
],
])
->save();
$entity_type
->set('entity_keys', $original_keys + [
'shape' => 'shape',
]);
$this->entityDefinitionUpdateManager
->updateEntityType($entity_type);
$this->entityDefinitionUpdateManager
->updateFieldStorageDefinition($field);
$expected['entity_test']['fields']['shape__shape']['not null'] = TRUE;
$expected['entity_test']['fields']['shape__color']['not null'] = TRUE;
$actual = $this->installedStorageSchema
->get('entity_test.field_schema_data.shape');
$this
->assertSame($expected, $actual);
$entity_type
->set('entity_keys', $original_keys);
$this->entityDefinitionUpdateManager
->updateEntityType($entity_type);
$this->entityDefinitionUpdateManager
->updateFieldStorageDefinition($field);
$expected['entity_test']['fields']['shape__shape']['not null'] = FALSE;
$expected['entity_test']['fields']['shape__color']['not null'] = FALSE;
$actual = $this->installedStorageSchema
->get('entity_test.field_schema_data.shape');
$this
->assertSame($expected, $actual);
}
}