summaryrefslogtreecommitdiffstats
path: root/core/modules
diff options
context:
space:
mode:
authoreffulgentsia2018-01-12 23:29:04 (GMT)
committerGabor Hojtsy2018-01-19 15:22:48 (GMT)
commit0cb1cd502b3df10dae5d88f59f5270c7cb6209bc (patch)
tree766e5daa4a52c12a3aaa2bb0fe615367cff2fa08 /core/modules
parent4c7237cb09be32978ce1ef965392681e72b0705d (diff)
Issue #2891215 by plach, timmillwood, hchonov, amateescu, catch, gabesullice: Add a way to track whether a revision was default when originally created
(cherry picked from commit 49c6761b56fd7f5470806ebf004fbf069bfa01f3)
Diffstat (limited to 'core/modules')
-rw-r--r--core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentResourceTestBase.php5
-rw-r--r--core/modules/rest/tests/src/Functional/EntityResource/Media/MediaResourceTestBase.php5
-rw-r--r--core/modules/rest/tests/src/Functional/EntityResource/Node/NodeResourceTestBase.php5
-rw-r--r--core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php4
-rw-r--r--core/modules/system/system.install49
-rw-r--r--core/modules/system/tests/modules/entity_test_update/update/entity_rev_pub_updates_8400.inc3
-rw-r--r--core/modules/system/tests/src/Functional/Update/EntityUpdateAddRevisionDefaultTest.php90
-rw-r--r--core/modules/system/tests/src/Functional/Update/EntityUpdateToRevisionableAndPublishableTest.php3
8 files changed, 162 insertions, 2 deletions
diff --git a/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentResourceTestBase.php
index 5766817..f5b3fdc 100644
--- a/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentResourceTestBase.php
+++ b/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentResourceTestBase.php
@@ -122,6 +122,11 @@ abstract class BlockContentResourceTestBase extends EntityResourceTestBase {
'value' => TRUE,
],
],
+ 'revision_default' => [
+ [
+ 'value' => TRUE,
+ ],
+ ],
'default_langcode' => [
[
'value' => TRUE,
diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Media/MediaResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Media/MediaResourceTestBase.php
index 40f7997..414b3eb 100644
--- a/core/modules/rest/tests/src/Functional/EntityResource/Media/MediaResourceTestBase.php
+++ b/core/modules/rest/tests/src/Functional/EntityResource/Media/MediaResourceTestBase.php
@@ -212,6 +212,11 @@ abstract class MediaResourceTestBase extends EntityResourceTestBase {
'value' => TRUE,
],
],
+ 'revision_default' => [
+ [
+ 'value' => TRUE,
+ ],
+ ],
];
}
diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeResourceTestBase.php
index bf0ba7a..fc827ac 100644
--- a/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeResourceTestBase.php
+++ b/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeResourceTestBase.php
@@ -152,6 +152,11 @@ abstract class NodeResourceTestBase extends EntityResourceTestBase {
'value' => TRUE,
],
],
+ 'revision_default' => [
+ [
+ 'value' => TRUE,
+ ],
+ ],
'default_langcode' => [
[
'value' => TRUE,
diff --git a/core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php b/core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php
index 72da3cb..65c82eb 100644
--- a/core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php
+++ b/core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php
@@ -150,6 +150,9 @@ class EntitySerializationTest extends NormalizerTestBase {
'default_langcode' => [
['value' => TRUE],
],
+ 'revision_default' => [
+ ['value' => TRUE],
+ ],
'revision_translation_affected' => [
['value' => TRUE],
],
@@ -226,6 +229,7 @@ class EntitySerializationTest extends NormalizerTestBase {
'user_id' => '<user_id><target_id>' . $this->user->id() . '</target_id><target_type>' . $this->user->getEntityTypeId() . '</target_type><target_uuid>' . $this->user->uuid() . '</target_uuid><url>' . $this->user->url() . '</url></user_id>',
'revision_id' => '<revision_id><value>' . $this->entity->getRevisionId() . '</value></revision_id>',
'default_langcode' => '<default_langcode><value>1</value></default_langcode>',
+ 'revision_default' => '<revision_default><value>1</value></revision_default>',
'revision_translation_affected' => '<revision_translation_affected><value>1</value></revision_translation_affected>',
'non_mul_field' => '<non_mul_field/>',
'non_rev_field' => '<non_rev_field/>',
diff --git a/core/modules/system/system.install b/core/modules/system/system.install
index ef6f124..0fd4b26 100644
--- a/core/modules/system/system.install
+++ b/core/modules/system/system.install
@@ -2056,3 +2056,52 @@ function system_update_8403() {
}
}
}
+
+/**
+ * Add the 'revision_default' field to all relevant entity types.
+ */
+function system_update_8501() {
+ $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
+
+ // Clear the cached entity type definitions so we get the new
+ // 'revision_default' revision metadata key.
+ \Drupal::entityTypeManager()->clearCachedDefinitions();
+
+ // Get a list of revisionable entity types.
+ /** @var \Drupal\Core\Entity\ContentEntityTypeInterface[] $definitions */
+ $definitions = array_filter(\Drupal::entityTypeManager()->getDefinitions(), function (EntityTypeInterface $entity_type) use ($definition_update_manager) {
+ if ($entity_type = $definition_update_manager->getEntityType($entity_type->id())) {
+ return $entity_type->isRevisionable();
+ }
+ return FALSE;
+ });
+
+ // Install the 'revision_default' field.
+ foreach ($definitions as $entity_type_id => $entity_type) {
+ $field_name = $entity_type->getRevisionMetadataKey('revision_default');
+ // Install the 'revision_default' field if needed.
+ if (!$definition_update_manager->getFieldStorageDefinition($field_name, $entity_type_id)) {
+ // Make sure the new "revision_default" revision metadata key is available
+ // also to code using the latest installed definition.
+ $installed_entity_type = $definition_update_manager->getEntityType($entity_type_id);
+ $revision_metadata_keys = $installed_entity_type->get('revision_metadata_keys');
+ $revision_metadata_keys['revision_default'] = $field_name;
+ $installed_entity_type->set('revision_metadata_keys', $revision_metadata_keys);
+ $definition_update_manager->updateEntityType($installed_entity_type);
+
+ $storage_definition = BaseFieldDefinition::create('boolean')
+ ->setLabel(t('Default revision'))
+ ->setDescription(t('A flag indicating whether this was a default revision when it was saved.'))
+ ->setStorageRequired(TRUE)
+ ->setTranslatable(FALSE)
+ ->setRevisionable(TRUE)
+ // We cannot tell whether existing revisions were default or not when
+ // they were created, but since we did not support creating non-default
+ // revisions in any core stable UI so far, we default to TRUE.
+ ->setInitialValue(TRUE);
+
+ $definition_update_manager
+ ->installFieldStorageDefinition($field_name, $entity_type_id, $entity_type_id, $storage_definition);
+ }
+ }
+}
diff --git a/core/modules/system/tests/modules/entity_test_update/update/entity_rev_pub_updates_8400.inc b/core/modules/system/tests/modules/entity_test_update/update/entity_rev_pub_updates_8400.inc
index 988505d..0a4ed4f 100644
--- a/core/modules/system/tests/modules/entity_test_update/update/entity_rev_pub_updates_8400.inc
+++ b/core/modules/system/tests/modules/entity_test_update/update/entity_rev_pub_updates_8400.inc
@@ -38,7 +38,8 @@ function entity_test_update_update_8400() {
$revision_metadata_keys = [
'revision_user' => 'revision_user',
'revision_created' => 'revision_created',
- 'revision_log_message' => 'revision_log_message'
+ 'revision_log_message' => 'revision_log_message',
+ 'revision_default' => 'revision_default',
];
$entity_type->set('revision_metadata_keys', $revision_metadata_keys);
diff --git a/core/modules/system/tests/src/Functional/Update/EntityUpdateAddRevisionDefaultTest.php b/core/modules/system/tests/src/Functional/Update/EntityUpdateAddRevisionDefaultTest.php
new file mode 100644
index 0000000..e226540
--- /dev/null
+++ b/core/modules/system/tests/src/Functional/Update/EntityUpdateAddRevisionDefaultTest.php
@@ -0,0 +1,90 @@
+<?php
+
+namespace Drupal\Tests\system\Functional\Update;
+
+use Drupal\FunctionalTests\Update\UpdatePathTestBase;
+use Drupal\system\Tests\Entity\EntityDefinitionTestTrait;
+
+/**
+ * Tests the upgrade path for adding the 'revision_default' field.
+ *
+ * @see https://www.drupal.org/project/drupal/issues/2891215
+ *
+ * @group Update
+ */
+class EntityUpdateAddRevisionDefaultTest extends UpdatePathTestBase {
+
+ use EntityDefinitionTestTrait;
+ use DbUpdatesTrait;
+
+ /**
+ * The entity manager service.
+ *
+ * @var \Drupal\Core\Entity\EntityManagerInterface
+ */
+ protected $entityManager;
+
+ /**
+ * The last installed schema repository service.
+ *
+ * @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface
+ */
+ protected $lastInstalledSchemaRepository;
+
+ /**
+ * The state service.
+ *
+ * @var \Drupal\Core\State\StateInterface
+ */
+ protected $state;
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function setUp() {
+ parent::setUp();
+
+ $this->entityManager = \Drupal::entityManager();
+ $this->lastInstalledSchemaRepository = \Drupal::service('entity.last_installed_schema.repository');
+ $this->state = \Drupal::state();
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function setDatabaseDumpFiles() {
+ $this->databaseDumpFiles = [
+ __DIR__ . '/../../../fixtures/update/drupal-8.0.0-rc1-filled.standard.entity_test_update_mul_rev.php.gz',
+ ];
+ }
+
+ /**
+ * Tests the addition of the 'revision_default' base field.
+ *
+ * @see system_update_8501()
+ */
+ public function testAddingTheRevisionDefaultField() {
+ // Make the entity type revisionable and translatable prior to running the
+ // updates.
+ $this->updateEntityTypeToRevisionableAndTranslatable();
+
+ // Check that the test entity type does not have the 'revision_default'
+ // field before running the updates.
+ $field_storage_definitions = $this->lastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions('entity_test_update');
+ $this->assertFalse(isset($field_storage_definitions['revision_default']));
+
+ $this->runUpdates();
+
+ // Check that the 'revision_default' field has been added by
+ // system_update_8501().
+ $field_storage_definitions = $this->lastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions('entity_test_update');
+ $this->assertTrue(isset($field_storage_definitions['revision_default']));
+
+ // Check that the correct initial value was set when the field was
+ // installed.
+ /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
+ $entity = $this->entityManager->getStorage('entity_test_update')->load(1);
+ $this->assertTrue($entity->wasDefaultRevision());
+ }
+
+}
diff --git a/core/modules/system/tests/src/Functional/Update/EntityUpdateToRevisionableAndPublishableTest.php b/core/modules/system/tests/src/Functional/Update/EntityUpdateToRevisionableAndPublishableTest.php
index e72f9f1..2312dc6 100644
--- a/core/modules/system/tests/src/Functional/Update/EntityUpdateToRevisionableAndPublishableTest.php
+++ b/core/modules/system/tests/src/Functional/Update/EntityUpdateToRevisionableAndPublishableTest.php
@@ -161,7 +161,8 @@ class EntityUpdateToRevisionableAndPublishableTest extends UpdatePathTestBase {
$revision_metadata_keys = [
'revision_user' => 'revision_user',
'revision_created' => 'revision_created',
- 'revision_log_message' => 'revision_log_message'
+ 'revision_log_message' => 'revision_log_message',
+ 'revision_default' => 'revision_default',
];
$entity_type->set('revision_metadata_keys', $revision_metadata_keys);