From 23d59bba609575f5a9e8f32737ab3e3ad1ca852d Mon Sep 17 00:00:00 2001 From: catch Date: Wed, 9 Jun 2021 14:06:35 +0100 Subject: [PATCH] Issue #3122056 by Wim Leers, mohit_aghera, Kristen Pol: Do not track viewing history for unsaved entities, nor when previewing existing entities --- core/modules/history/history.module | 3 +++ .../tests/src/Functional/HistoryTest.php | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/core/modules/history/history.module b/core/modules/history/history.module index 5495d539fff..02dd964fd7c 100644 --- a/core/modules/history/history.module +++ b/core/modules/history/history.module @@ -135,6 +135,9 @@ function history_cron() { * Implements hook_ENTITY_TYPE_view_alter() for node entities. */ function history_node_view_alter(array &$build, EntityInterface $node, EntityViewDisplayInterface $display) { + if ($node->isNew() || isset($node->in_preview)) { + return; + } // Update the history table, stating that this user viewed this node. if ($display->getOriginalMode() === 'full') { $build['#cache']['contexts'][] = 'user.roles:authenticated'; diff --git a/core/modules/history/tests/src/Functional/HistoryTest.php b/core/modules/history/tests/src/Functional/HistoryTest.php index e89fc8e988e..6117a2dd80a 100644 --- a/core/modules/history/tests/src/Functional/HistoryTest.php +++ b/core/modules/history/tests/src/Functional/HistoryTest.php @@ -49,6 +49,7 @@ class HistoryTest extends BrowserTestBase { $this->user = $this->drupalCreateUser([ 'create page content', + 'edit own page content', 'access content', ]); $this->drupalLogin($this->user); @@ -103,6 +104,11 @@ class HistoryTest extends BrowserTestBase { public function testHistory() { $nid = $this->testNode->id(); + // Verify that previews of new entities do not create the history. + $this->drupalGet("node/add/page"); + $this->submitForm(['title[0][value]' => 'Unsaved page'], 'Preview'); + $this->assertArrayNotHasKey('ajaxPageState', $this->getDrupalSettings()); + // Retrieve "last read" timestamp for test node, for the current user. $response = $this->getNodeReadTimestamps([$nid]); $this->assertEquals(200, $response->getStatusCode()); @@ -134,6 +140,11 @@ class HistoryTest extends BrowserTestBase { $response = $this->getNodeReadTimestamps([]); $this->assertEquals(404, $response->getStatusCode()); + // Verify that previews of existing entities do not update the history. + $this->drupalGet("node/$nid/edit"); + $this->submitForm([], 'Preview'); + $this->assertArrayNotHasKey('ajaxPageState', $this->getDrupalSettings()); + // Accessing either endpoint as the anonymous user should return a 403. $this->drupalLogout(); $response = $this->getNodeReadTimestamps([$nid]); @@ -142,6 +153,13 @@ class HistoryTest extends BrowserTestBase { $this->assertEquals(403, $response->getStatusCode()); $response = $this->markNodeAsRead($nid); $this->assertEquals(403, $response->getStatusCode()); + + // Additional check to ensure that we did not forget to verify anything. + $rows = \Drupal::database()->query('SELECT * FROM {history}')->fetchAll(); + $this->assertCount(1, $rows); + $this->assertSame($this->user->id(), $rows[0]->uid); + $this->assertSame($this->testNode->id(), $rows[0]->nid); + $this->assertSame($timestamp, (int) $rows[0]->timestamp); } }