SqlContentEntityStorageSchemaColumnTest.php in Drupal 9
File
core/modules/field/tests/src/Kernel/Entity/Update/SqlContentEntityStorageSchemaColumnTest.php
View source
<?php
namespace Drupal\Tests\field\Kernel\Entity\Update;
use Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException;
use Drupal\entity_test\Entity\EntityTestRev;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\system\Functional\Entity\Traits\EntityDefinitionTestTrait;
class SqlContentEntityStorageSchemaColumnTest extends KernelTestBase {
use EntityDefinitionTestTrait;
protected static $modules = [
'entity_test',
'field',
'text',
'user',
];
protected $entity;
protected $field;
protected $fieldStorage;
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('entity_test_rev');
$this
->installEntitySchema('user');
$field_name = 'test';
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test_rev',
'type' => 'string',
'cardinality' => 1,
]);
$this->fieldStorage
->save();
$this->field = FieldConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test_rev',
'bundle' => 'entity_test_rev',
'required' => TRUE,
]);
$this->field
->save();
$this->entity = EntityTestRev::create([
'user_id' => mt_rand(1, 10),
'name' => $this
->randomMachineName(),
$field_name => $this
->randomString(),
]);
$this->entity
->save();
}
public function testColumnUpdate() {
$schema = \Drupal::keyValue('entity.storage_schema.sql')
->get('entity_test_rev.field_schema_data.test');
$schema['entity_test_rev__test']['fields']['test_value']['type'] = 'varchar_ascii';
\Drupal::keyValue('entity.storage_schema.sql')
->set('entity_test_rev.field_schema_data.test', $schema);
$this
->expectException(FieldStorageDefinitionUpdateForbiddenException::class);
$entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$field_storage_definition = $entity_definition_update_manager
->getFieldStorageDefinition('test', 'entity_test_rev');
$entity_definition_update_manager
->updateFieldStorageDefinition($field_storage_definition);
}
}