diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install index c244fe4ce8b9dfb1be00cd22b5fc8a79f6b86823..82b578b48956d0bddb02517b5b0e6b58c8061b7a 100644 --- a/core/modules/comment/comment.install +++ b/core/modules/comment/comment.install @@ -5,6 +5,7 @@ * Install, update and uninstall functions for the Comment module. */ +use Drupal\comment\Entity\Comment; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\StringTranslation\PluralTranslatableMarkup; use Drupal\Core\StringTranslation\TranslatableMarkup; @@ -195,3 +196,14 @@ function comment_update_8400() { $entity_definition_update_manager = \Drupal::service('entity.definition_update_manager'); $entity_definition_update_manager->updateFieldStorageDefinition($entity_definition_update_manager->getFieldStorageDefinition('status', 'comment')); } + +/** + * Configure the comment hostname base field to use a default value callback. + */ +function comment_update_8600() { + $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager(); + /** @var \Drupal\Core\Field\BaseFieldDefinition $field_storage_definition */ + $field_storage_definition = $entity_definition_update_manager->getFieldStorageDefinition('hostname', 'comment'); + $field_storage_definition->setDefaultValueCallback(Comment::class . '::getDefaultHostname'); + $entity_definition_update_manager->updateFieldStorageDefinition($field_storage_definition); +} diff --git a/core/modules/comment/src/Entity/Comment.php b/core/modules/comment/src/Entity/Comment.php index 5f5a611565a948d55c8eb8ec96ba0fca7649c492..e0745d3da0e3d67b1a798ef646c49dc6aec96f43 100644 --- a/core/modules/comment/src/Entity/Comment.php +++ b/core/modules/comment/src/Entity/Comment.php @@ -143,10 +143,6 @@ public function preSave(EntityStorageInterface $storage) { $this->threadLock = $lock_name; } $this->setThread($thread); - if (!$this->getHostname()) { - // Ensure a client host from the current request. - $this->setHostname(\Drupal::request()->getClientIP()); - } } // The entity fields for name and mail have no meaning if the user is not // Anonymous. Set them to NULL to make it clearer that they are not used. @@ -291,7 +287,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setLabel(t('Hostname')) ->setDescription(t("The comment author's hostname.")) ->setTranslatable(TRUE) - ->setSetting('max_length', 128); + ->setSetting('max_length', 128) + ->setDefaultValueCallback(static::class . '::getDefaultHostname'); $fields['created'] = BaseFieldDefinition::create('created') ->setLabel(t('Created')) @@ -572,4 +569,14 @@ public static function getDefaultStatus() { return \Drupal::currentUser()->hasPermission('skip comment approval') ? CommentInterface::PUBLISHED : CommentInterface::NOT_PUBLISHED; } + /** + * Returns the default value for entity hostname base field. + * + * @return string + * The client host name. + */ + public static function getDefaultHostname() { + return \Drupal::request()->getClientIP(); + } + } diff --git a/core/modules/comment/tests/src/Functional/Update/CommentHostnameUpdateTest.php b/core/modules/comment/tests/src/Functional/Update/CommentHostnameUpdateTest.php new file mode 100644 index 0000000000000000000000000000000000000000..7d75b6e6dd4d3bdeb2fc1d9f9e2ede9895dbec49 --- /dev/null +++ b/core/modules/comment/tests/src/Functional/Update/CommentHostnameUpdateTest.php @@ -0,0 +1,45 @@ +<?php + +namespace Drupal\Tests\comment\Functional\Update; + +use Drupal\comment\Entity\Comment; +use Drupal\FunctionalTests\Update\UpdatePathTestBase; + +/** + * Tests that comment hostname settings are properly updated. + * + * @group comment + */ +class CommentHostnameUpdateTest extends UpdatePathTestBase { + + /** + * {@inheritdoc} + */ + protected function setDatabaseDumpFiles() { + $this->databaseDumpFiles = [ + __DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8-rc1.bare.standard.php.gz', + ]; + } + + /** + * Tests comment_update_8600(). + * + * @see comment_update_8600 + */ + public function testCommentUpdate8600() { + /** @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface $manager */ + $manager = $this->container->get('entity.definition_update_manager'); + + /** @var \Drupal\Core\Field\BaseFieldDefinition $definition */ + $definition = $manager->getFieldStorageDefinition('hostname', 'comment'); + // Check that 'hostname' base field doesn't have a default value callback. + $this->assertNull($definition->getDefaultValueCallback()); + + $this->runUpdates(); + + $definition = $manager->getFieldStorageDefinition('hostname', 'comment'); + // Check that 'hostname' base field default value callback was set. + $this->assertEquals(Comment::class . '::getDefaultHostname', $definition->getDefaultValueCallback()); + } + +} diff --git a/core/modules/comment/tests/src/Kernel/CommentHostnameTest.php b/core/modules/comment/tests/src/Kernel/CommentHostnameTest.php new file mode 100644 index 0000000000000000000000000000000000000000..dc4f37b63f35d4b238c59c14d3ce3d6d5e81c7b7 --- /dev/null +++ b/core/modules/comment/tests/src/Kernel/CommentHostnameTest.php @@ -0,0 +1,46 @@ +<?php + +namespace Drupal\Tests\comment\Kernel; + +use Drupal\comment\Entity\Comment; +use Drupal\comment\Entity\CommentType; +use Drupal\KernelTests\KernelTestBase; +use Symfony\Component\HttpFoundation\Request; + +/** + * Tests the hostname base field. + * + * @coversDefaultClass \Drupal\comment\Entity\Comment + * + * @group comment + */ +class CommentHostnameTest extends KernelTestBase { + + /** + * {@inheritdoc} + */ + protected static $modules = ['comment', 'entity_test', 'user']; + + /** + * Tests hostname default value callback. + * + * @covers ::getDefaultHostname + */ + public function testGetDefaultHostname() { + // Create a fake request to be used for testing. + $request = Request::create('/', 'GET', [], [], [], ['REMOTE_ADDR' => '203.0.113.1']); + /** @var \Symfony\Component\HttpFoundation\RequestStack $stack */ + $stack = $this->container->get('request_stack'); + $stack->push($request); + + CommentType::create([ + 'id' => 'foo', + 'target_entity_type_id' => 'entity_test', + ])->save(); + $comment = Comment::create(['comment_type' => 'foo']); + + // Check that the hostname was set correctly. + $this->assertEquals('203.0.113.1', $comment->getHostname()); + } + +}