Unverified Commit 75850cdd authored by larowlan's avatar larowlan

Issue #2860259 by claudiu.cristea, larowlan: Move the comment hostname default...

Issue #2860259 by claudiu.cristea, larowlan: Move the comment hostname default value to a default value callback
parent ce1f1fc1
......@@ -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);
}
......@@ -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();
}
}
<?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());
}
}
<?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());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment