0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-04 10:39:43 +08:00
discourse/spec/jobs/update_username_spec.rb
Régis Hanol 73dd5b4da8
FIX: Sync category description when post content changes outside PostRevisor (#39184)
When a group or user is renamed, `GroupMentionsUpdater` and
`Jobs::UpdateUsername` update the post's raw/cooked content but skip
`PostRevisor`, so `categories.description` (a denormalized copy of the
first paragraph) is never refreshed. The Edit Category page and category
banner keep showing the old name, while the actual topic shows the new
one. Same issue with `Post#rebake!` (Rebuild HTML) and `ProcessPost`.

Extract `Post#sync_category_description` as the single source of truth
for deriving `categories.description` from a post's cooked HTML — both
`PostRevisor` and the new `Post#sync_first_post_caches` now delegate to
it. `sync_first_post_caches` also updates `topics.excerpt`, centralizing
all first-post denormalized field updates.

Call `sync_first_post_caches` from every code path that modifies post
content outside PostRevisor: `GroupMentionsUpdater`, `UpdateUsername`,
`ChangeDisplayName`, `ProcessPost`, and `Post#rebake!`.

Skip the update when the description hasn't changed to avoid unnecessary
writes. When it has changed, call `publish_category` and
`Site.clear_cache` so connected clients see the update immediately
(fixes a pre-existing flicker where the old description would briefly
appear then get overwritten by stale cached site data).

Also add a targeted query in `UpdateUsername` for category description
posts authored by the system user, which were missed by the existing
`user_actions`-based and self-mention queries.

Ref - t/181445
2026-05-11 16:13:23 +02:00

67 lines
2 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe Jobs::UpdateUsername do
fab!(:user)
it "does not do anything if user_id is invalid" do
events =
DiscourseEvent.track_events do
described_class.new.execute(
user_id: -999,
old_username: user.username,
new_username: "somenewusername",
avatar_template: user.avatar_template,
)
end
expect(events).to eq([])
end
it "does not rewrite similar mentions when the old username contains a dot" do
renamed_user = Fabricate(:user, username: "foo.bar")
Fabricate(:user, username: "foo-bar")
author = Fabricate(:user)
topic = Fabricate(:topic, user: author)
Jobs.run_immediately!
UserActionManager.enable
post = create_post(user: author, topic: topic, raw: "@foo.bar @foo-bar")
described_class.new.execute(
user_id: renamed_user.id,
old_username: renamed_user.username,
new_username: "newname",
avatar_template: renamed_user.avatar_template,
)
post.reload
expect(post.raw).to eq("@newname @foo-bar")
expect(post.cooked).to match_html <<~HTML
<p><a class="mention" href="/u/newname">@newname</a> <a class="mention" href="/u/foo-bar">@foo-bar</a></p>
HTML
end
it "updates the category description when a category description topic mentions the user" do
category = Fabricate(:category_with_definition, user: Discourse.system_user)
first_post = category.topic.first_post
first_post.revise(first_post.user, { raw: "This category is managed by @#{user.username}" })
category.reload
expect(category.description).to include(user.username)
old_username = user.username
new_username = "new_username_123"
described_class.new.execute(
user_id: user.id,
old_username:,
new_username:,
avatar_template: user.avatar_template,
)
category.reload
expect(category.description).to include(new_username)
expect(category.description).not_to include(old_username)
end
end