public function EntityDefinitionUpdateTest::testSingleActionCalls in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/system/src/Tests/Entity/EntityDefinitionUpdateTest.php \Drupal\system\Tests\Entity\EntityDefinitionUpdateTest::testSingleActionCalls()
Tests applying single updates.
File
- core/
modules/ system/ src/ Tests/ Entity/ EntityDefinitionUpdateTest.php, line 625 - Contains \Drupal\system\Tests\Entity\EntityDefinitionUpdateTest.
Class
- EntityDefinitionUpdateTest
- Tests EntityDefinitionUpdateManager functionality.
Namespace
Drupal\system\Tests\EntityCode
public function testSingleActionCalls() {
$db_schema = $this->database
->schema();
// Ensure that a non-existing entity type cannot be installed.
$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);
}
// Ensure that a field cannot be installed on non-existing entity type.
$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);
}
// Ensure that a non-existing field cannot be installed.
$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.");
// Ensure that installing an existing entity type is a no-op.
$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');
// Create a new base field.
$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.");
// Ensure that installing an existing field is a no-op.
$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');
// Update an existing field schema.
$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.");
// Drop an existing field schema.
$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.");
// Make the entity type revisionable.
$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.");
}