From 4ac8f969a775311449b69e16a620f746d0daf75c Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Tue, 17 May 2016 12:07:32 -0500 Subject: [PATCH] Issue #2716081 by timmillwood, dixon_: BlockContent should have revision_user and revision_created fields and implement RevisionLogInterface --- .../block_content/block_content.info.yml | 1 + .../block_content/block_content.install | 22 ++++++ .../block_content/src/BlockContentForm.php | 5 +- .../src/BlockContentInterface.php | 9 ++- .../block_content/src/Entity/BlockContent.php | 74 ++++++++++++++++++- .../src/Tests/BlockContentRevisionsTest.php | 19 ++++- .../src/Tests/BlockContentTestBase.php | 2 +- .../Tests/BlockContentUpdateEntityFields.php | 39 ++++++++++ ...CacheabilityMetadataConfigOverrideTest.php | 1 + 9 files changed, 163 insertions(+), 9 deletions(-) create mode 100644 core/modules/block_content/src/Tests/BlockContentUpdateEntityFields.php diff --git a/core/modules/block_content/block_content.info.yml b/core/modules/block_content/block_content.info.yml index d32bda79aab..b9ae5645c09 100644 --- a/core/modules/block_content/block_content.info.yml +++ b/core/modules/block_content/block_content.info.yml @@ -7,4 +7,5 @@ core: 8.x dependencies: - block - text + - user configure: entity.block_content.collection diff --git a/core/modules/block_content/block_content.install b/core/modules/block_content/block_content.install index 658eab16bdb..4279cdef722 100644 --- a/core/modules/block_content/block_content.install +++ b/core/modules/block_content/block_content.install @@ -39,3 +39,25 @@ function block_content_update_8002() { // are stable. // @see https://www.drupal.org/node/2569469 } + +/** + * Add 'revision_created' and 'revision_user' fields to 'block_content' entities. + */ +function block_content_update_8003() { + $revision_created = BaseFieldDefinition::create('created') + ->setLabel(t('Revision create time')) + ->setDescription(t('The time that the current revision was created.')) + ->setRevisionable(TRUE); + + \Drupal::entityDefinitionUpdateManager() + ->installFieldStorageDefinition('revision_created', 'block_content', 'block_content', $revision_created); + + $revision_user = BaseFieldDefinition::create('entity_reference') + ->setLabel(t('Revision user')) + ->setDescription(t('The user ID of the author of the current revision.')) + ->setSetting('target_type', 'user') + ->setRevisionable(TRUE); + + \Drupal::entityDefinitionUpdateManager() + ->installFieldStorageDefinition('revision_user', 'block_content', 'block_content', $revision_user); +} diff --git a/core/modules/block_content/src/BlockContentForm.php b/core/modules/block_content/src/BlockContentForm.php index 2410c7ce15c..8815f565269 100644 --- a/core/modules/block_content/src/BlockContentForm.php +++ b/core/modules/block_content/src/BlockContentForm.php @@ -88,7 +88,7 @@ class BlockContentForm extends ContentEntityForm { // Set up default values, if required. $block_type = $this->blockContentTypeStorage->load($block->bundle()); if (!$block->isNew()) { - $block->setRevisionLog(NULL); + $block->setRevisionLogMessage(NULL); } // Always use the default revision setting. $block->setNewRevision($block_type->shouldCreateNewRevision()); @@ -170,6 +170,9 @@ class BlockContentForm extends ContentEntityForm { // Save as a new revision if requested to do so. if (!$form_state->isValueEmpty('revision')) { $block->setNewRevision(); + // If a new revision is created, save the current user as revision author. + $block->setRevisionCreationTime(REQUEST_TIME); + $block->setRevisionUserId(\Drupal::currentUser()->id()); } $insert = $block->isNew(); diff --git a/core/modules/block_content/src/BlockContentInterface.php b/core/modules/block_content/src/BlockContentInterface.php index 4eefe28a954..130cae1c3cf 100644 --- a/core/modules/block_content/src/BlockContentInterface.php +++ b/core/modules/block_content/src/BlockContentInterface.php @@ -4,17 +4,21 @@ namespace Drupal\block_content; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityChangedInterface; +use Drupal\Core\Entity\RevisionLogInterface; /** * Provides an interface defining a custom block entity. */ -interface BlockContentInterface extends ContentEntityInterface, EntityChangedInterface { +interface BlockContentInterface extends ContentEntityInterface, EntityChangedInterface, RevisionLogInterface { /** * Returns the block revision log message. * * @return string * The revision log message. + * + * @deprecated in Drupal 8.2.0, will be removed before Drupal 9.0.0. Use + * \Drupal\Core\Entity\RevisionLogInterface::getRevisionLogMessage() instead. */ public function getRevisionLog(); @@ -37,6 +41,9 @@ interface BlockContentInterface extends ContentEntityInterface, EntityChangedInt * * @return \Drupal\block_content\BlockContentInterface * The class instance that this method is called on. + * + * @deprecated in Drupal 8.2.0, will be removed before Drupal 9.0.0. Use + * \Drupal\Core\Entity\RevisionLogInterface::setRevisionLogMessage() instead. */ public function setRevisionLog($revision_log); diff --git a/core/modules/block_content/src/Entity/BlockContent.php b/core/modules/block_content/src/Entity/BlockContent.php index 8c22644d739..9e0531e6da7 100644 --- a/core/modules/block_content/src/Entity/BlockContent.php +++ b/core/modules/block_content/src/Entity/BlockContent.php @@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\block_content\BlockContentInterface; +use Drupal\user\UserInterface; /** * Defines the custom block entity class. @@ -126,7 +127,7 @@ class BlockContent extends ContentEntityBase implements BlockContentInterface { // If we are updating an existing block_content without adding a new // revision and the user did not supply a revision log, keep the existing // one. - $record->revision_log = $this->original->getRevisionLog(); + $record->revision_log = $this->original->getRevisionLogMessage(); } } @@ -183,6 +184,17 @@ class BlockContent extends ContentEntityBase implements BlockContentInterface { ->setTranslatable(TRUE) ->setRevisionable(TRUE); + $fields['revision_created'] = BaseFieldDefinition::create('created') + ->setLabel(t('Revision create time')) + ->setDescription(t('The time that the current revision was created.')) + ->setRevisionable(TRUE); + + $fields['revision_user'] = BaseFieldDefinition::create('entity_reference') + ->setLabel(t('Revision user')) + ->setDescription(t('The user ID of the author of the current revision.')) + ->setSetting('target_type', 'user') + ->setRevisionable(TRUE); + $fields['revision_translation_affected'] = BaseFieldDefinition::create('boolean') ->setLabel(t('Revision translation affected')) ->setDescription(t('Indicates if the last edit of a translation belongs to current revision.')) @@ -197,7 +209,7 @@ class BlockContent extends ContentEntityBase implements BlockContentInterface { * {@inheritdoc} */ public function getRevisionLog() { - return $this->get('revision_log')->value; + return $this->getRevisionLogMessage(); } /** @@ -212,7 +224,63 @@ class BlockContent extends ContentEntityBase implements BlockContentInterface { * {@inheritdoc} */ public function setRevisionLog($revision_log) { - $this->set('revision_log', $revision_log); + return $this->setRevisionLogMessage($revision_log); + } + + /** + * {@inheritdoc} + */ + public function getRevisionCreationTime() { + return $this->get('revision_created')->value; + } + + /** + * {@inheritdoc} + */ + public function setRevisionCreationTime($timestamp) { + $this->set('revision_created', $timestamp); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getRevisionUser() { + return $this->get('revision_user')->entity; + } + + public function setRevisionUser(UserInterface $account) { + $this->set('revision_user', $account); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getRevisionUserId() { + return $this->get('revision_user')->entity->id(); + } + + /** + * {@inheritdoc} + */ + public function setRevisionUserId($user_id) { + $this->set('revision_user', $user_id); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getRevisionLogMessage() { + return $this->get('revision_log')->value; + } + + /** + * {@inheritdoc} + */ + public function setRevisionLogMessage($revision_log_message) { + $this->set('revision_log', $revision_log_message); return $this; } diff --git a/core/modules/block_content/src/Tests/BlockContentRevisionsTest.php b/core/modules/block_content/src/Tests/BlockContentRevisionsTest.php index 720443d5fca..cc08654fbe6 100644 --- a/core/modules/block_content/src/Tests/BlockContentRevisionsTest.php +++ b/core/modules/block_content/src/Tests/BlockContentRevisionsTest.php @@ -3,6 +3,8 @@ namespace Drupal\block_content\Tests; use Drupal\block_content\Entity\BlockContent; +use Drupal\user\Entity\User; +use Drupal\user\UserInterface; /** * Create a block with revisions. @@ -29,6 +31,9 @@ class BlockContentRevisionsTest extends BlockContentTestBase { protected function setUp() { parent::setUp(); + /** @var UserInterface $user */ + $user = User::load(1); + // Create initial block. $block = $this->createBlockContent('initial'); @@ -43,8 +48,10 @@ class BlockContentRevisionsTest extends BlockContentTestBase { $revision_count = 3; for ($i = 0; $i < $revision_count; $i++) { $block->setNewRevision(TRUE); - $block->setRevisionLog($this->randomMachineName(32)); - $logs[] = $block->getRevisionLog(); + $block->setRevisionLogMessage($this->randomMachineName(32)); + $block->setRevisionUser($this->adminUser); + $block->setRevisionCreationTime(REQUEST_TIME); + $logs[] = $block->getRevisionLogMessage(); $block->save(); $blocks[] = $block->getRevisionId(); } @@ -62,11 +69,17 @@ class BlockContentRevisionsTest extends BlockContentTestBase { foreach ($blocks as $delta => $revision_id) { // Confirm the correct revision text appears. + /** @var \Drupal\block_content\BlockContentInterface $loaded */ $loaded = entity_revision_load('block_content', $revision_id); // Verify revision log is the same. - $this->assertEqual($loaded->getRevisionLog(), $logs[$delta], format_string('Correct log message found for revision @revision', array( + $this->assertEqual($loaded->getRevisionLogMessage(), $logs[$delta], format_string('Correct log message found for revision @revision', array( '@revision' => $loaded->getRevisionId(), ))); + if ($delta > 0) { + $this->assertTrue($loaded->getRevisionUser() instanceof UserInterface, 'Revision User found.'); + $this->assertTrue(is_numeric($loaded->getRevisionUserId()), 'Revision User ID found.'); + $this->assertTrue(is_numeric($loaded->getRevisionCreationTime()), 'Revision time found.'); + } } // Confirm that this is the default revision. diff --git a/core/modules/block_content/src/Tests/BlockContentTestBase.php b/core/modules/block_content/src/Tests/BlockContentTestBase.php index 40210325396..16df8558ede 100644 --- a/core/modules/block_content/src/Tests/BlockContentTestBase.php +++ b/core/modules/block_content/src/Tests/BlockContentTestBase.php @@ -19,7 +19,7 @@ abstract class BlockContentTestBase extends WebTestBase { /** * Admin user * - * @var object + * @var \Drupal\user\UserInterface */ protected $adminUser; diff --git a/core/modules/block_content/src/Tests/BlockContentUpdateEntityFields.php b/core/modules/block_content/src/Tests/BlockContentUpdateEntityFields.php new file mode 100644 index 00000000000..ab59eba282a --- /dev/null +++ b/core/modules/block_content/src/Tests/BlockContentUpdateEntityFields.php @@ -0,0 +1,39 @@ +databaseDumpFiles = [ + __DIR__ . '/../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz', + ]; + } + + /** + * Tests that email token in status_blocked of user.mail is updated. + */ + public function testAddingFields() { + $this->runUpdates(); + + $post_revision_created = \Drupal::entityDefinitionUpdateManager()->getFieldStorageDefinition('revision_created', 'block_content'); + $post_revision_user = \Drupal::entityDefinitionUpdateManager()->getFieldStorageDefinition('revision_user', 'block_content'); + $this->assertTrue($post_revision_created instanceof BaseFieldDefinition, "Revision created field found"); + $this->assertTrue($post_revision_user instanceof BaseFieldDefinition, "Revision user field found"); + + $this->assertEqual('created', $post_revision_created->getType(), "Field is type created"); + $this->assertEqual('entity_reference', $post_revision_user->getType(), "Field is type entity_reference"); + } + +} diff --git a/core/tests/Drupal/KernelTests/Core/Config/CacheabilityMetadataConfigOverrideTest.php b/core/tests/Drupal/KernelTests/Core/Config/CacheabilityMetadataConfigOverrideTest.php index 306d102d7f3..c52e3ea8533 100644 --- a/core/tests/Drupal/KernelTests/Core/Config/CacheabilityMetadataConfigOverrideTest.php +++ b/core/tests/Drupal/KernelTests/Core/Config/CacheabilityMetadataConfigOverrideTest.php @@ -21,6 +21,7 @@ class CacheabilityMetadataConfigOverrideTest extends KernelTestBase { 'config', 'config_override_test', 'system', + 'user' ]; /**