Commit 23d59bba authored by catch's avatar catch
Browse files

Issue #3122056 by Wim Leers, mohit_aghera, Kristen Pol: Do not track viewing...

Issue #3122056 by Wim Leers, mohit_aghera, Kristen Pol: Do not track viewing history for unsaved entities, nor when previewing existing entities
parent 23dcd9dc
......@@ -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';
......
......@@ -49,6 +49,7 @@ protected function setUp(): void {
$this->user = $this->drupalCreateUser([
'create page content',
'edit own page content',
'access content',
]);
$this->drupalLogin($this->user);
......@@ -103,6 +104,11 @@ protected function markNodeAsRead($node_id) {
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 @@ public function testHistory() {
$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 @@ public function testHistory() {
$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);
}
}
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