SqlContentEntityStorageSchemaColumnTest.php in Zircon Profile 8
File
core/modules/field/src/Tests/Entity/Update/SqlContentEntityStorageSchemaColumnTest.php
View source
<?php
namespace Drupal\field\Tests\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\simpletest\KernelTestBase;
class SqlContentEntityStorageSchemaColumnTest extends KernelTestBase {
public static $modules = [
'entity_test',
'field',
'text',
'user',
];
protected $entity;
protected $field;
protected $fieldStorage;
public function setUp() {
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);
try {
\Drupal::service('entity.definition_update_manager')
->applyUpdates();
$this
->fail('Failed to detect a schema change in a field with data.');
} catch (FieldStorageDefinitionUpdateForbiddenException $e) {
$this
->pass('Detected a schema change in a field with data.');
}
}
}