View source
<?php
namespace Drupal\system\Tests\Entity;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\DatabaseExceptionWrapper;
use Drupal\Core\Database\IntegrityConstraintViolationException;
use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityTypeEvents;
use Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionEvents;
use Drupal\Core\Language\LanguageInterface;
use Drupal\entity_test\Entity\EntityTestUpdate;
class EntityDefinitionUpdateTest extends EntityUnitTestBase {
use EntityDefinitionTestTrait;
protected $entityDefinitionUpdateManager;
protected $database;
protected function setUp() {
parent::setUp();
$this->entityDefinitionUpdateManager = $this->container
->get('entity.definition_update_manager');
$this->database = $this->container
->get('database');
foreach (array_diff_key($this->entityManager
->getDefinitions(), array_flip(array(
'user',
'entity_test',
))) as $entity_type_id => $entity_type) {
$this
->installEntitySchema($entity_type_id);
}
}
public function testNewEntityType() {
$entity_type_id = 'entity_test_new';
$schema = $this->database
->schema();
$entity_types = $this->entityManager
->getDefinitions();
$this
->assertFalse(isset($entity_types[$entity_type_id]), 'The "entity_test_new" entity type does not exist.');
$this
->assertFalse($schema
->tableExists($entity_type_id), 'Schema for the "entity_test_new" entity type does not exist.');
$this
->enableNewEntityType();
$entity_types = $this->entityManager
->getDefinitions();
$this
->assertTrue(isset($entity_types[$entity_type_id]), 'The "entity_test_new" entity type exists.');
$this
->assertTrue($schema
->tableExists($entity_type_id), 'Schema for the "entity_test_new" entity type has been created.');
}
public function testNoUpdates() {
$this
->assertFalse($this->entityDefinitionUpdateManager
->needsUpdates(), 'EntityDefinitionUpdateManager reports that no updates are needed.');
$this
->assertIdentical($this->entityDefinitionUpdateManager
->getChangeSummary(), array(), 'EntityDefinitionUpdateManager reports an empty change summary.');
$this->entityDefinitionUpdateManager
->applyUpdates();
}
public function testEntityTypeUpdateWithoutData() {
$this
->assertFalse($this->database
->schema()
->tableExists('entity_test_update_revision'), 'Revision table not created for entity_test_update.');
$this
->updateEntityTypeToRevisionable();
$this
->assertTrue($this->entityDefinitionUpdateManager
->needsUpdates(), 'EntityDefinitionUpdateManager reports that updates are needed.');
$expected = array(
'entity_test_update' => array(
t('Update the %entity_type entity type.', array(
'%entity_type' => $this->entityManager
->getDefinition('entity_test_update')
->getLabel(),
)),
),
);
$this
->assertEqual($this->entityDefinitionUpdateManager
->getChangeSummary(), $expected);
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->assertTrue($this->database
->schema()
->tableExists('entity_test_update_revision'), 'Revision table created for entity_test_update.');
}
public function testEntityTypeUpdateWithData() {
$this->entityManager
->getStorage('entity_test_update')
->create()
->save();
$this
->updateEntityTypeToRevisionable();
try {
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->fail('EntityStorageException thrown when trying to apply an update that requires data migration.');
} catch (EntityStorageException $e) {
$this
->pass('EntityStorageException thrown when trying to apply an update that requires data migration.');
}
}
public function testBaseFieldCreateUpdateDeleteWithoutData() {
$this
->addBaseField();
$this
->assertTrue($this->entityDefinitionUpdateManager
->needsUpdates(), 'EntityDefinitionUpdateManager reports that updates are needed.');
$expected = array(
'entity_test_update' => array(
t('Create the %field_name field.', array(
'%field_name' => t('A new base field'),
)),
),
);
$this
->assertEqual($this->entityDefinitionUpdateManager
->getChangeSummary(), $expected, 'EntityDefinitionUpdateManager reports the expected change summary.');
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->assertTrue($this->database
->schema()
->fieldExists('entity_test_update', 'new_base_field'), 'Column created in shared table for new_base_field.');
$this
->addBaseFieldIndex();
$this
->assertTrue($this->entityDefinitionUpdateManager
->needsUpdates(), 'EntityDefinitionUpdateManager reports that updates are needed.');
$expected = array(
'entity_test_update' => array(
t('Update the %field_name field.', array(
'%field_name' => t('A new base field'),
)),
),
);
$this
->assertEqual($this->entityDefinitionUpdateManager
->getChangeSummary(), $expected, 'EntityDefinitionUpdateManager reports the expected change summary.');
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->assertTrue($this->database
->schema()
->indexExists('entity_test_update', 'entity_test_update_field__new_base_field'), 'Index created.');
$this
->removeBaseFieldIndex();
$this
->assertTrue($this->entityDefinitionUpdateManager
->needsUpdates(), 'EntityDefinitionUpdateManager reports that updates are needed.');
$expected = array(
'entity_test_update' => array(
t('Update the %field_name field.', array(
'%field_name' => t('A new base field'),
)),
),
);
$this
->assertEqual($this->entityDefinitionUpdateManager
->getChangeSummary(), $expected, 'EntityDefinitionUpdateManager reports the expected change summary.');
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->assertFalse($this->database
->schema()
->indexExists('entity_test_update', 'entity_test_update_field__new_base_field'), 'Index deleted.');
$this
->modifyBaseField();
$this
->assertTrue($this->entityDefinitionUpdateManager
->needsUpdates(), 'EntityDefinitionUpdateManager reports that updates are needed.');
$expected = array(
'entity_test_update' => array(
t('Update the %field_name field.', array(
'%field_name' => t('A new base field'),
)),
),
);
$this
->assertEqual($this->entityDefinitionUpdateManager
->getChangeSummary(), $expected, 'EntityDefinitionUpdateManager reports the expected change summary.');
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->assertFalse($this->database
->schema()
->fieldExists('entity_test_update', 'new_base_field'), 'Original column deleted in shared table for new_base_field.');
$this
->assertTrue($this->database
->schema()
->fieldExists('entity_test_update', 'new_base_field__value'), 'Value column created in shared table for new_base_field.');
$this
->assertTrue($this->database
->schema()
->fieldExists('entity_test_update', 'new_base_field__format'), 'Format column created in shared table for new_base_field.');
$this
->removeBaseField();
$this
->assertTrue($this->entityDefinitionUpdateManager
->needsUpdates(), 'EntityDefinitionUpdateManager reports that updates are needed.');
$expected = array(
'entity_test_update' => array(
t('Delete the %field_name field.', array(
'%field_name' => t('A new base field'),
)),
),
);
$this
->assertEqual($this->entityDefinitionUpdateManager
->getChangeSummary(), $expected, 'EntityDefinitionUpdateManager reports the expected change summary.');
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->assertFalse($this->database
->schema()
->fieldExists('entity_test_update', 'new_base_field_value'), 'Value column deleted from shared table for new_base_field.');
$this
->assertFalse($this->database
->schema()
->fieldExists('entity_test_update', 'new_base_field_format'), 'Format column deleted from shared table for new_base_field.');
}
public function testBundleFieldCreateUpdateDeleteWithoutData() {
$this
->addBundleField();
$this
->assertTrue($this->entityDefinitionUpdateManager
->needsUpdates(), 'EntityDefinitionUpdateManager reports that updates are needed.');
$expected = array(
'entity_test_update' => array(
t('Create the %field_name field.', array(
'%field_name' => t('A new bundle field'),
)),
),
);
$this
->assertEqual($this->entityDefinitionUpdateManager
->getChangeSummary(), $expected, 'EntityDefinitionUpdateManager reports the expected change summary.');
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->assertTrue($this->database
->schema()
->tableExists('entity_test_update__new_bundle_field'), 'Dedicated table created for new_bundle_field.');
$this
->modifyBundleField();
$this
->assertTrue($this->entityDefinitionUpdateManager
->needsUpdates(), 'EntityDefinitionUpdateManager reports that updates are needed.');
$expected = array(
'entity_test_update' => array(
t('Update the %field_name field.', array(
'%field_name' => t('A new bundle field'),
)),
),
);
$this
->assertEqual($this->entityDefinitionUpdateManager
->getChangeSummary(), $expected, 'EntityDefinitionUpdateManager reports the expected change summary.');
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->assertTrue($this->database
->schema()
->fieldExists('entity_test_update__new_bundle_field', 'new_bundle_field_format'), 'Format column created in dedicated table for new_base_field.');
$this
->removeBundleField();
$this
->assertTrue($this->entityDefinitionUpdateManager
->needsUpdates(), 'EntityDefinitionUpdateManager reports that updates are needed.');
$expected = array(
'entity_test_update' => array(
t('Delete the %field_name field.', array(
'%field_name' => t('A new bundle field'),
)),
),
);
$this
->assertEqual($this->entityDefinitionUpdateManager
->getChangeSummary(), $expected, 'EntityDefinitionUpdateManager reports the expected change summary.');
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->assertFalse($this->database
->schema()
->tableExists('entity_test_update__new_bundle_field'), 'Dedicated table deleted for new_bundle_field.');
}
public function testBaseFieldCreateDeleteWithExistingEntities() {
$name = $this
->randomString();
$storage = $this->entityManager
->getStorage('entity_test_update');
$entity = $storage
->create(array(
'name' => $name,
));
$entity
->save();
$this
->addBaseField();
$this->entityDefinitionUpdateManager
->applyUpdates();
$schema_handler = $this->database
->schema();
$this
->assertTrue($schema_handler
->fieldExists('entity_test_update', 'new_base_field'), 'Column created in shared table for new_base_field.');
$entity = $this->entityManager
->getStorage('entity_test_update')
->load($entity
->id());
$this
->assertIdentical($entity->name->value, $name, 'Entity data preserved during field creation.');
$this
->removeBaseField();
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->assertFalse($schema_handler
->fieldExists('entity_test_update', 'new_base_field'), 'Column deleted from shared table for new_base_field.');
$entity = $this->entityManager
->getStorage('entity_test_update')
->load($entity
->id());
$this
->assertIdentical($entity->name->value, $name, 'Entity data preserved during field deletion.');
$this
->addBaseField('shape_required');
$this->entityDefinitionUpdateManager
->applyUpdates();
$assert = $schema_handler
->fieldExists('entity_test_update', 'new_base_field__shape') && $schema_handler
->fieldExists('entity_test_update', 'new_base_field__color');
$this
->assertTrue($assert, 'Columns created in shared table for new_base_field.');
$entity
->delete();
$this
->removeBaseField();
$this->entityDefinitionUpdateManager
->applyUpdates();
$assert = !$schema_handler
->fieldExists('entity_test_update', 'new_base_field__shape') && !$schema_handler
->fieldExists('entity_test_update', 'new_base_field__color');
$this
->assert($assert, 'Columns removed from the shared table for new_base_field.');
$this
->addBaseField('shape_required');
$this->entityDefinitionUpdateManager
->applyUpdates();
$assert = $schema_handler
->fieldExists('entity_test_update', 'new_base_field__shape') && $schema_handler
->fieldExists('entity_test_update', 'new_base_field__color');
$this
->assertTrue($assert, 'Columns created again in shared table for new_base_field.');
$entity = $storage
->create(array(
'name' => $name,
));
$entity
->save();
$this
->pass('The new_base_field columns are still nullable');
}
public function testBundleFieldCreateDeleteWithExistingEntities() {
$name = $this
->randomString();
$storage = $this->entityManager
->getStorage('entity_test_update');
$entity = $storage
->create(array(
'name' => $name,
));
$entity
->save();
$this
->addBundleField();
$this->entityDefinitionUpdateManager
->applyUpdates();
$schema_handler = $this->database
->schema();
$this
->assertTrue($schema_handler
->tableExists('entity_test_update__new_bundle_field'), 'Dedicated table created for new_bundle_field.');
$entity = $this->entityManager
->getStorage('entity_test_update')
->load($entity
->id());
$this
->assertIdentical($entity->name->value, $name, 'Entity data preserved during field creation.');
$this
->removeBundleField();
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->assertFalse($schema_handler
->tableExists('entity_test_update__new_bundle_field'), 'Dedicated table deleted for new_bundle_field.');
$entity = $this->entityManager
->getStorage('entity_test_update')
->load($entity
->id());
$this
->assertIdentical($entity->name->value, $name, 'Entity data preserved during field deletion.');
$this
->addBundleField('shape_required');
$this->entityDefinitionUpdateManager
->applyUpdates();
$message = 'The new_bundle_field_shape column is not nullable.';
$values = array(
'bundle' => $entity
->bundle(),
'deleted' => 0,
'entity_id' => $entity
->id(),
'revision_id' => $entity
->id(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'delta' => 0,
'new_bundle_field_color' => $this
->randomString(),
);
try {
$this->database
->insert('entity_test_update__new_bundle_field')
->fields($values)
->execute();
$this
->fail($message);
} catch (\RuntimeException $e) {
if ($e instanceof DatabaseExceptionWrapper || $e instanceof IntegrityConstraintViolationException) {
$values['new_bundle_field_shape'] = $this
->randomString();
$this->database
->insert('entity_test_update__new_bundle_field')
->fields($values)
->execute();
$this
->pass($message);
}
else {
throw $e;
}
}
}
public function testBaseFieldDeleteWithExistingData() {
$this
->addBaseField();
$this->entityDefinitionUpdateManager
->applyUpdates();
$this->entityManager
->getStorage('entity_test_update')
->create(array(
'new_base_field' => 'foo',
))
->save();
$this
->removeBaseField();
try {
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->fail('FieldStorageDefinitionUpdateForbiddenException thrown when trying to apply an update that deletes a non-purgeable field with data.');
} catch (FieldStorageDefinitionUpdateForbiddenException $e) {
$this
->pass('FieldStorageDefinitionUpdateForbiddenException thrown when trying to apply an update that deletes a non-purgeable field with data.');
}
}
public function testBundleFieldDeleteWithExistingData() {
$this
->addBundleField();
$this->entityDefinitionUpdateManager
->applyUpdates();
entity_test_create_bundle('custom');
$this->entityManager
->getStorage('entity_test_update')
->create(array(
'type' => 'test_bundle',
'new_bundle_field' => 'foo',
))
->save();
$this
->removeBundleField();
try {
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->fail('FieldStorageDefinitionUpdateForbiddenException thrown when trying to apply an update that deletes a non-purgeable field with data.');
} catch (FieldStorageDefinitionUpdateForbiddenException $e) {
$this
->pass('FieldStorageDefinitionUpdateForbiddenException thrown when trying to apply an update that deletes a non-purgeable field with data.');
}
}
public function testBaseFieldUpdateWithExistingData() {
$this
->addBaseField();
$this->entityDefinitionUpdateManager
->applyUpdates();
$this->entityManager
->getStorage('entity_test_update')
->create(array(
'new_base_field' => 'foo',
))
->save();
$this
->modifyBaseField();
try {
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->fail('FieldStorageDefinitionUpdateForbiddenException thrown when trying to update a field schema that has data.');
} catch (FieldStorageDefinitionUpdateForbiddenException $e) {
$this
->pass('FieldStorageDefinitionUpdateForbiddenException thrown when trying to update a field schema that has data.');
}
}
public function testBundleFieldUpdateWithExistingData() {
$this
->addBundleField();
$this->entityDefinitionUpdateManager
->applyUpdates();
entity_test_create_bundle('custom');
$this->entityManager
->getStorage('entity_test_update')
->create(array(
'type' => 'test_bundle',
'new_bundle_field' => 'foo',
))
->save();
$this
->modifyBundleField();
try {
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->fail('FieldStorageDefinitionUpdateForbiddenException thrown when trying to update a field schema that has data.');
} catch (FieldStorageDefinitionUpdateForbiddenException $e) {
$this
->pass('FieldStorageDefinitionUpdateForbiddenException thrown when trying to update a field schema that has data.');
}
}
public function testEntityIndexCreateDeleteWithoutData() {
$this
->addEntityIndex();
$this
->assertTrue($this->entityDefinitionUpdateManager
->needsUpdates(), 'EntityDefinitionUpdateManager reports that updates are needed.');
$expected = array(
'entity_test_update' => array(
t('Update the %entity_type entity type.', array(
'%entity_type' => $this->entityManager
->getDefinition('entity_test_update')
->getLabel(),
)),
),
);
$this
->assertEqual($this->entityDefinitionUpdateManager
->getChangeSummary(), $expected, 'EntityDefinitionUpdateManager reports the expected change summary.');
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->assertTrue($this->database
->schema()
->indexExists('entity_test_update', 'entity_test_update__new_index'), 'Index created.');
$this
->removeEntityIndex();
$this
->assertTrue($this->entityDefinitionUpdateManager
->needsUpdates(), 'EntityDefinitionUpdateManager reports that updates are needed.');
$expected = array(
'entity_test_update' => array(
t('Update the %entity_type entity type.', array(
'%entity_type' => $this->entityManager
->getDefinition('entity_test_update')
->getLabel(),
)),
),
);
$this
->assertEqual($this->entityDefinitionUpdateManager
->getChangeSummary(), $expected, 'EntityDefinitionUpdateManager reports the expected change summary.');
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->assertFalse($this->database
->schema()
->indexExists('entity_test_update', 'entity_test_update__new_index'), 'Index deleted.');
$this
->addEntityIndex();
$this->entityDefinitionUpdateManager
->applyUpdates();
$storage_definition = $this->entityDefinitionUpdateManager
->getFieldStorageDefinition('name', 'entity_test_update');
$this->entityDefinitionUpdateManager
->updateFieldStorageDefinition($storage_definition);
$this
->assertTrue($this->database
->schema()
->indexExists('entity_test_update', 'entity_test_update__new_index'), 'Index created.');
$this->entityDefinitionUpdateManager
->uninstallFieldStorageDefinition($storage_definition);
$this
->assertFalse($this->database
->schema()
->indexExists('entity_test_update', 'entity_test_update__new_index'), 'Index deleted.');
$this->entityDefinitionUpdateManager
->installFieldStorageDefinition('name', 'entity_test_update', 'entity_test', $storage_definition);
$this
->assertTrue($this->database
->schema()
->indexExists('entity_test_update', 'entity_test_update__new_index'), 'Index created again.');
}
public function testEntityIndexCreateWithData() {
$name = $this
->randomString();
$entity = $this->entityManager
->getStorage('entity_test_update')
->create(array(
'name' => $name,
));
$entity
->save();
$this
->addEntityIndex();
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->assertTrue($this->database
->schema()
->indexExists('entity_test_update', 'entity_test_update__new_index'), 'Index added.');
}
public function testDefinitionEvents() {
$event_subscriber = $this->container
->get('entity_test.definition.subscriber');
$event_subscriber
->enableEventTracking();
$storage_definition = current($this->entityManager
->getFieldStorageDefinitions('entity_test_rev'));
$this
->assertFalse($event_subscriber
->hasEventFired(FieldStorageDefinitionEvents::DELETE), 'Entity type delete was not dispatched yet.');
$this->entityManager
->onFieldStorageDefinitionDelete($storage_definition);
$this
->assertTrue($event_subscriber
->hasEventFired(FieldStorageDefinitionEvents::DELETE), 'Entity type delete event successfully dispatched.');
$this
->assertFalse($event_subscriber
->hasEventFired(FieldStorageDefinitionEvents::CREATE), 'Entity type create was not dispatched yet.');
$this->entityManager
->onFieldStorageDefinitionCreate($storage_definition);
$this
->assertTrue($event_subscriber
->hasEventFired(FieldStorageDefinitionEvents::CREATE), 'Entity type create event successfully dispatched.');
$this
->assertFalse($event_subscriber
->hasEventFired(FieldStorageDefinitionEvents::UPDATE), 'Entity type update was not dispatched yet.');
$this->entityManager
->onFieldStorageDefinitionUpdate($storage_definition, $storage_definition);
$this
->assertTrue($event_subscriber
->hasEventFired(FieldStorageDefinitionEvents::UPDATE), 'Entity type update event successfully dispatched.');
$entity_type = $this->entityManager
->getDefinition('entity_test_rev');
$this
->assertFalse($event_subscriber
->hasEventFired(EntityTypeEvents::CREATE), 'Entity type create was not dispatched yet.');
$this->entityManager
->onEntityTypeCreate($entity_type);
$this
->assertTrue($event_subscriber
->hasEventFired(EntityTypeEvents::CREATE), 'Entity type create event successfully dispatched.');
$this
->assertFalse($event_subscriber
->hasEventFired(EntityTypeEvents::UPDATE), 'Entity type update was not dispatched yet.');
$this->entityManager
->onEntityTypeUpdate($entity_type, $entity_type);
$this
->assertTrue($event_subscriber
->hasEventFired(EntityTypeEvents::UPDATE), 'Entity type update event successfully dispatched.');
$this
->assertFalse($event_subscriber
->hasEventFired(EntityTypeEvents::DELETE), 'Entity type delete was not dispatched yet.');
$this->entityManager
->onEntityTypeDelete($entity_type);
$this
->assertTrue($event_subscriber
->hasEventFired(EntityTypeEvents::DELETE), 'Entity type delete event successfully dispatched.');
}
public function testEntityTypeSchemaUpdateAndBaseFieldCreateWithoutData() {
$this
->updateEntityTypeToRevisionable();
$this
->addBaseField();
$message = 'Successfully updated entity schema and created base field at the same time.';
try {
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->pass($message);
} catch (\Exception $e) {
$this
->fail($message);
throw $e;
}
}
public function testEntityTypeSchemaUpdateAndRevisionableBaseFieldCreateWithoutData() {
$this
->updateEntityTypeToRevisionable();
$this
->addRevisionableBaseField();
$message = 'Successfully updated entity schema and created revisionable base field at the same time.';
try {
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->pass($message);
} catch (\Exception $e) {
$this
->fail($message);
throw $e;
}
}
public function testSingleActionCalls() {
$db_schema = $this->database
->schema();
$message = 'A non-existing entity type cannot be installed';
try {
$this->entityDefinitionUpdateManager
->installEntityType(new ContentEntityType([
'id' => 'foo',
]));
$this
->fail($message);
} catch (PluginNotFoundException $e) {
$this
->pass($message);
}
$message = 'A field cannot be installed on a non-existing entity type';
try {
$storage_definition = BaseFieldDefinition::create('string')
->setLabel(t('A new revisionable base field'))
->setRevisionable(TRUE);
$this->entityDefinitionUpdateManager
->installFieldStorageDefinition('bar', 'foo', 'entity_test', $storage_definition);
$this
->fail($message);
} catch (PluginNotFoundException $e) {
$this
->pass($message);
}
$storage_definition = BaseFieldDefinition::create('string')
->setLabel(t('A new revisionable base field'))
->setRevisionable(TRUE);
$this->entityDefinitionUpdateManager
->installFieldStorageDefinition('bar', 'entity_test_update', 'entity_test', $storage_definition);
$this
->assertFalse($db_schema
->fieldExists('entity_test_update', 'bar'), "A non-existing field cannot be installed.");
$entity_type = $this->entityDefinitionUpdateManager
->getEntityType('entity_test_update');
$this->entityDefinitionUpdateManager
->installEntityType($entity_type);
$this
->assertTrue($db_schema
->tableExists('entity_test_update'), 'Installing an existing entity type is a no-op');
$this
->addRevisionableBaseField();
$storage_definition = BaseFieldDefinition::create('string')
->setLabel(t('A new revisionable base field'))
->setRevisionable(TRUE);
$this
->assertFalse($db_schema
->fieldExists('entity_test_update', 'new_base_field'), "New field 'new_base_field' does not exist before applying the update.");
$this->entityDefinitionUpdateManager
->installFieldStorageDefinition('new_base_field', 'entity_test_update', 'entity_test', $storage_definition);
$this
->assertTrue($db_schema
->fieldExists('entity_test_update', 'new_base_field'), "New field 'new_base_field' has been created on the 'entity_test_update' table.");
$this->entityDefinitionUpdateManager
->installFieldStorageDefinition('new_base_field', 'entity_test_update', 'entity_test', $storage_definition);
$this
->assertTrue($db_schema
->fieldExists('entity_test_update', 'new_base_field'), 'Installing an existing field is a no-op');
$this
->modifyBaseField();
$storage_definition = BaseFieldDefinition::create('text')
->setName('new_base_field')
->setTargetEntityTypeId('entity_test_update')
->setLabel(t('A new revisionable base field'))
->setRevisionable(TRUE);
$this->entityDefinitionUpdateManager
->updateFieldStorageDefinition($storage_definition);
$this
->assertFalse($db_schema
->fieldExists('entity_test_update', 'new_base_field'), "Previous schema for 'new_base_field' no longer exists.");
$this
->assertTrue($db_schema
->fieldExists('entity_test_update', 'new_base_field__value') && $db_schema
->fieldExists('entity_test_update', 'new_base_field__format'), "New schema for 'new_base_field' has been created.");
$this->entityDefinitionUpdateManager
->uninstallFieldStorageDefinition($storage_definition);
$this
->assertFalse($db_schema
->fieldExists('entity_test_update', 'new_base_field__value') || $db_schema
->fieldExists('entity_test_update', 'new_base_field__format'), "The schema for 'new_base_field' has been dropped.");
$this
->updateEntityTypeToRevisionable();
$this
->assertFalse($db_schema
->tableExists('entity_test_update_revision'), "The 'entity_test_update_revision' does not exist before applying the update.");
$entity_type = $this->entityDefinitionUpdateManager
->getEntityType('entity_test_update');
$keys = $entity_type
->getKeys();
$keys['revision'] = 'revision_id';
$entity_type
->set('entity_keys', $keys);
$this->entityDefinitionUpdateManager
->updateEntityType($entity_type);
$this
->assertTrue($db_schema
->tableExists('entity_test_update_revision'), "The 'entity_test_update_revision' table has been created.");
}
public function testCreateFieldAndIndexOnSharedTable() {
$this
->addBaseField();
$this
->addBaseFieldIndex();
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->assertTrue($this->database
->schema()
->fieldExists('entity_test_update', 'new_base_field'), "New field 'new_base_field' has been created on the 'entity_test_update' table.");
$this
->assertTrue($this->database
->schema()
->indexExists('entity_test_update', 'entity_test_update_field__new_base_field'), "New index 'entity_test_update_field__new_base_field' has been created on the 'entity_test_update' table.");
if (Database::getConnection()
->driver() == 'mysql') {
$result = Database::getConnection()
->query('SHOW INDEX FROM {entity_test_update} WHERE key_name = \'entity_test_update_field__new_base_field\' and column_name = \'new_base_field\'')
->fetchObject();
$this
->assertEqual(191, $result->Sub_part, 'The index length has been restricted to 191 characters for UTF8MB4 compatibility.');
}
}
public function testCreateIndexUsingEntityStorageSchemaWithData() {
$name = $this
->randomString();
$storage = $this->entityManager
->getStorage('entity_test_update');
$entity = $storage
->create(array(
'name' => $name,
));
$entity
->save();
$indexes = array(
'entity_test_update__type_index' => array(
'type',
),
);
$this->state
->set('entity_test_update.additional_entity_indexes', $indexes);
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->assertTrue($this->database
->schema()
->indexExists('entity_test_update', 'entity_test_update__type_index'), "New index 'entity_test_update__type_index' has been created on the 'entity_test_update' table.");
if (Database::getConnection()
->driver() == 'mysql') {
$result = Database::getConnection()
->query('SHOW INDEX FROM {entity_test_update} WHERE key_name = \'entity_test_update__type_index\' and column_name = \'type\'')
->fetchObject();
$this
->assertEqual(191, $result->Sub_part, 'The index length has been restricted to 191 characters for UTF8MB4 compatibility.');
}
}
public function testBaseFieldEntityKeyUpdateWithExistingData() {
$this
->addBaseField();
$this->entityDefinitionUpdateManager
->applyUpdates();
$this->entityManager
->getStorage('entity_test_update')
->create([
'new_base_field' => $this
->randomString(),
])
->save();
$entity = $this->entityManager
->getStorage('entity_test_update')
->create();
$entity
->save();
$this
->makeBaseFieldEntityKey();
$message = 'An error occurs when trying to enabling NOT NULL constraints with NULL data.';
try {
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->fail($message);
} catch (EntityStorageException $e) {
$this
->pass($message);
}
$entity
->set('new_base_field', $this
->randomString());
$entity
->save();
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->pass('The update is correctly performed when no NULL data exists.');
$entity
->set('new_base_field', NULL);
$message = 'The NOT NULL constraint was correctly applied.';
try {
$entity
->save();
$this
->fail($message);
} catch (EntityStorageException $e) {
$this
->pass($message);
}
}
function testLongNameFieldIndexes() {
$this
->addLongNameBaseField();
$entity_type_id = 'entity_test_update';
$entity_type = $this->entityManager
->getDefinition($entity_type_id);
$definitions = EntityTestUpdate::baseFieldDefinitions($entity_type);
$name = 'new_long_named_entity_reference_base_field';
$this->entityDefinitionUpdateManager
->installFieldStorageDefinition($name, $entity_type_id, 'entity_test', $definitions[$name]);
$this
->assertFalse($this->entityDefinitionUpdateManager
->needsUpdates(), 'Entity and field schema data are correctly detected.');
}
}