discourse/spec/jobs/sync_topic_user_bookmarked_spec.rb
Martin Brennan 66d17fdd6b
FIX: Topic user bookmarked column is out of sync after post moves (#12612)
When posts are moved from one topic to another, the `topic_user.bookmarked` column for all users in the new and the old topic needs to be resynced, for example because a user bookmarks post 12 in topic 1, then it is moved to topic 2, the topic_user record for topic 1 should no longer be bookmarked. A background job has been added to sync the column for a specified topic, or for no topic at all, which does it for all topics like the migration.

Also includes a migration that we have run in the past to fix bad data.

----

This has been addressed in other places in the past:

https://github.com/discourse/discourse/pull/10211
https://github.com/discourse/discourse/pull/10188
2021-04-14 09:10:53 +10:00

54 lines
2.0 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Jobs::SyncTopicUserBookmarked do
it "corrects all topic_users.bookmarked records for the topic" do
topic = Fabricate(:topic)
Fabricate(:post, topic: topic)
Fabricate(:post, topic: topic)
Fabricate(:post, topic: topic)
tu1 = Fabricate(:topic_user, topic: topic, bookmarked: false)
tu2 = Fabricate(:topic_user, topic: topic, bookmarked: false)
tu3 = Fabricate(:topic_user, topic: topic, bookmarked: true)
tu4 = Fabricate(:topic_user, topic: topic, bookmarked: true)
tu5 = Fabricate(:topic_user, bookmarked: false)
Fabricate(:bookmark, user: tu1.user, topic: topic, post: topic.posts.sample)
Fabricate(:bookmark, user: tu4.user, topic: topic, post: topic.posts.sample)
subject.execute(topic_id: topic.id)
expect(tu1.reload.bookmarked).to eq(true)
expect(tu2.reload.bookmarked).to eq(false)
expect(tu3.reload.bookmarked).to eq(false)
expect(tu4.reload.bookmarked).to eq(true)
expect(tu5.reload.bookmarked).to eq(false)
end
it "works when no topic id is provided (runs for all topics)" do
topic = Fabricate(:topic)
Fabricate(:post, topic: topic)
Fabricate(:post, topic: topic)
Fabricate(:post, topic: topic)
tu1 = Fabricate(:topic_user, topic: topic, bookmarked: false)
tu2 = Fabricate(:topic_user, topic: topic, bookmarked: false)
tu3 = Fabricate(:topic_user, topic: topic, bookmarked: true)
tu4 = Fabricate(:topic_user, topic: topic, bookmarked: true)
tu5 = Fabricate(:topic_user, bookmarked: false)
Fabricate(:bookmark, user: tu1.user, topic: topic, post: topic.posts.sample)
Fabricate(:bookmark, user: tu4.user, topic: topic, post: topic.posts.sample)
subject.execute
expect(tu1.reload.bookmarked).to eq(true)
expect(tu2.reload.bookmarked).to eq(false)
expect(tu3.reload.bookmarked).to eq(false)
expect(tu4.reload.bookmarked).to eq(true)
expect(tu5.reload.bookmarked).to eq(false)
end
end