From 012bca1943f7c84a2e4c7e4dacacc36705062eb0 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Tue, 14 Mar 2017 08:56:27 +0000 Subject: [PATCH] Issue #2687977 by claudiu.cristea, alexpott, prics: Comment::preSave() unconditionally sets hostname to client host --- core/modules/comment/src/Entity/Comment.php | 6 ++++-- .../tests/src/Kernel/Migrate/d6/MigrateCommentTest.php | 3 +++ .../tests/src/Kernel/Migrate/d7/MigrateCommentTest.php | 1 + core/modules/migrate_drupal/tests/fixtures/drupal6.php | 6 +++--- core/modules/migrate_drupal/tests/fixtures/drupal7.php | 2 +- 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/core/modules/comment/src/Entity/Comment.php b/core/modules/comment/src/Entity/Comment.php index 83dffaa9e53..a5f19664697 100644 --- a/core/modules/comment/src/Entity/Comment.php +++ b/core/modules/comment/src/Entity/Comment.php @@ -151,9 +151,11 @@ class Comment extends ContentEntityBase implements CommentInterface { if ($this->getOwnerId() === \Drupal::currentUser()->id() && \Drupal::currentUser()->isAuthenticated()) { $this->setAuthorName(\Drupal::currentUser()->getUsername()); } - // Add the values which aren't passed into the function. $this->setThread($thread); - $this->setHostname(\Drupal::request()->getClientIP()); + if (!$this->getHostname()) { + // Ensure a client host from the current request. + $this->setHostname(\Drupal::request()->getClientIP()); + } } } diff --git a/core/modules/comment/tests/src/Kernel/Migrate/d6/MigrateCommentTest.php b/core/modules/comment/tests/src/Kernel/Migrate/d6/MigrateCommentTest.php index 5330e9416d4..c417c75ad7e 100644 --- a/core/modules/comment/tests/src/Kernel/Migrate/d6/MigrateCommentTest.php +++ b/core/modules/comment/tests/src/Kernel/Migrate/d6/MigrateCommentTest.php @@ -61,14 +61,17 @@ class MigrateCommentTest extends MigrateDrupal6TestBase { $this->assertIdentical('node', $comment->getCommentedEntityTypeId()); $this->assertIdentical('en', $comment->language()->getId()); $this->assertIdentical('comment_no_subject', $comment->getTypeId()); + $this->assertEquals('203.0.113.1', $comment->getHostname()); $comment = $comment_storage->load(2); $this->assertIdentical('The response to the second comment.', $comment->subject->value); $this->assertIdentical('3', $comment->pid->target_id); + $this->assertEquals('203.0.113.2', $comment->getHostname()); $comment = $comment_storage->load(3); $this->assertIdentical('The second comment.', $comment->subject->value); $this->assertIdentical(NULL, $comment->pid->target_id); + $this->assertEquals('203.0.113.3', $comment->getHostname()); } } diff --git a/core/modules/comment/tests/src/Kernel/Migrate/d7/MigrateCommentTest.php b/core/modules/comment/tests/src/Kernel/Migrate/d7/MigrateCommentTest.php index f1e094f39b8..a22bbe6605f 100644 --- a/core/modules/comment/tests/src/Kernel/Migrate/d7/MigrateCommentTest.php +++ b/core/modules/comment/tests/src/Kernel/Migrate/d7/MigrateCommentTest.php @@ -61,6 +61,7 @@ class MigrateCommentTest extends MigrateDrupal7TestBase { $this->assertIdentical('admin@local.host', $comment->getAuthorEmail()); $this->assertIdentical('This is a comment', $comment->comment_body->value); $this->assertIdentical('filtered_html', $comment->comment_body->format); + $this->assertEquals('2001:db8:ffff:ffff:ffff:ffff:ffff:ffff', $comment->getHostname()); $node = $comment->getCommentedEntity(); $this->assertTrue($node instanceof NodeInterface); diff --git a/core/modules/migrate_drupal/tests/fixtures/drupal6.php b/core/modules/migrate_drupal/tests/fixtures/drupal6.php index 4dd1418a6a4..1092c11755b 100644 --- a/core/modules/migrate_drupal/tests/fixtures/drupal6.php +++ b/core/modules/migrate_drupal/tests/fixtures/drupal6.php @@ -1772,7 +1772,7 @@ $connection->insert('comments') 'uid' => '0', 'subject' => 'The first comment.', 'comment' => 'The first comment body.', - 'hostname' => '127.0.0.1', + 'hostname' => '203.0.113.1', 'timestamp' => '1390264918', 'status' => '0', 'format' => '1', @@ -1788,7 +1788,7 @@ $connection->insert('comments') 'uid' => '0', 'subject' => 'The response to the second comment.', 'comment' => 'The second comment response body.', - 'hostname' => '127.0.0.1', + 'hostname' => '203.0.113.2', 'timestamp' => '1390264938', 'status' => '0', 'format' => '1', @@ -1804,7 +1804,7 @@ $connection->insert('comments') 'uid' => '0', 'subject' => 'The second comment.', 'comment' => 'The second comment body.', - 'hostname' => '127.0.0.1', + 'hostname' => '203.0.113.3', 'timestamp' => '1390264948', 'status' => '1', 'format' => '1', diff --git a/core/modules/migrate_drupal/tests/fixtures/drupal7.php b/core/modules/migrate_drupal/tests/fixtures/drupal7.php index 6d6cc498986..8c96dae605c 100644 --- a/core/modules/migrate_drupal/tests/fixtures/drupal7.php +++ b/core/modules/migrate_drupal/tests/fixtures/drupal7.php @@ -2512,7 +2512,7 @@ $connection->insert('comment') 'nid' => '1', 'uid' => '1', 'subject' => 'A comment', - 'hostname' => '::1', + 'hostname' => '2001:db8:ffff:ffff:ffff:ffff:ffff:ffff', 'created' => '1421727536', 'changed' => '1421727536', 'status' => '1',