Skip to content

Commit

Permalink
Issue #6243: Cached node author name can be outdated.
Browse files Browse the repository at this point in the history
  • Loading branch information
indigoxela committed Oct 5, 2023
1 parent 1982f48 commit d3e300b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions .cspell/backdrop.dic
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ timeentry
timestep
titleslogan
Título
tnid
tnids
todate
tookit's
Expand Down
20 changes: 20 additions & 0 deletions core/modules/node/node.module
Original file line number Diff line number Diff line change
Expand Up @@ -1542,6 +1542,26 @@ function node_user_predelete($account) {
->execute();
}

/**
* Implements hook_user_update().
*/
function node_user_update($account) {
$orig = $account->original;
if ($account->name != $orig->name || $account->picture != $orig->picture || $account->data != $orig->data) {
$nids = db_select('node', 'n')
->fields('n', array('nid'))
->condition('uid', $account->uid)
->execute()
->fetchCol();

if (!empty($nids)) {
// Flush entity cache of nodes this user authored, to prevent outdated
// info (like old username) in cached data.
cache('entity_node')->deleteMultiple($nids);
}
}
}

/**
* Access callback: Checks node revision access.
*
Expand Down
26 changes: 25 additions & 1 deletion core/modules/node/tests/node.test
Original file line number Diff line number Diff line change
Expand Up @@ -3784,7 +3784,7 @@ class NodePageCacheTest extends NodeWebTestCase {
* Make sure that creating a new node does not affect existing entity cache
* records, and updating a node only acts on the affected cid.
*/
function testNodeUpdateInsertCache() {
protected function testNodeUpdateInsertCache() {
$node_one = $this->backdropCreateNode(array('title' => 'Node one'));
$node_two = $this->backdropCreateNode(array('title' => 'Node two'));
$ids = array($node_one->nid, $node_two->nid);
Expand Down Expand Up @@ -3818,6 +3818,30 @@ class NodePageCacheTest extends NodeWebTestCase {
$this->assertEqual(count($cached_after_insert), 3, 'All three nodes have a record in table cache_entity_node.');
$this->assertEqual($cached_after_insert[1]->created, $cached[1]->created, 'Cache timestamp of node 1 is unchanged.');
}

/**
* Check that cached node info does not run out of sync with user data.
*/
protected function testNodeSaveAfterUsernameChange() {
$author = $this->backdropCreateUser(array('create page content'));
$node = $this->backdropCreateNode(array(
'uid' => $author->uid,
'name' => $author->name,
));

// Trigger entity cache.
$this->backdropGet('node/' . $node->nid);

// Simulate a username change (without actual form interaction).
$author->name = 'new_name';
user_save($author);

// Freshly load data and compare.
$user = entity_load('user', $author->uid);
$node_after = entity_load('node', $node->nid);
$this->assertEqual($user->name, $node_after->name, 'Name property in loaded node is equal to username after change');
}

}

/**
Expand Down

1 comment on commit d3e300b

@backdrop-ci
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.