X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Ffield%2Ftests%2Fsrc%2FKernel%2FEntity%2FUpdate%2FSqlContentEntityStorageSchemaColumnTest.php;fp=web%2Fcore%2Fmodules%2Ffield%2Ftests%2Fsrc%2FKernel%2FEntity%2FUpdate%2FSqlContentEntityStorageSchemaColumnTest.php;h=05db0dc5ccf693c4576e388b13e374c42b139875;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/field/tests/src/Kernel/Entity/Update/SqlContentEntityStorageSchemaColumnTest.php b/web/core/modules/field/tests/src/Kernel/Entity/Update/SqlContentEntityStorageSchemaColumnTest.php new file mode 100644 index 000000000..05db0dc5c --- /dev/null +++ b/web/core/modules/field/tests/src/Kernel/Entity/Update/SqlContentEntityStorageSchemaColumnTest.php @@ -0,0 +1,99 @@ +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(); + + // Create an entity with field data. + $this->entity = EntityTestRev::create([ + 'user_id' => mt_rand(1, 10), + 'name' => $this->randomMachineName(), + $field_name => $this->randomString(), + ]); + $this->entity->save(); + } + + /** + * Tests that column-level schema changes are detected for fields with data. + */ + public function testColumnUpdate() { + // Change the field type in the stored schema. + $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); + + // Now attempt to run automatic updates. An exception should be thrown + // since there is data in the table. + 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.'); + } + } + +}