2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe PostOwnerChanger do
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "#change_owner!" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:editor) { Fabricate(:admin) }
|
|
|
|
fab!(:user_a) { Fabricate(:user) }
|
2019-01-03 12:03:01 -05:00
|
|
|
let(:p1) { create_post(post_number: 1) }
|
|
|
|
let(:topic) { p1.topic }
|
|
|
|
let(:p2) { create_post(topic: topic, post_number: 2) }
|
|
|
|
let(:p3) { create_post }
|
2015-03-02 11:17:11 -05:00
|
|
|
|
|
|
|
it "raises an error with a parameter missing" do
|
|
|
|
expect {
|
2018-06-05 03:29:17 -04:00
|
|
|
PostOwnerChanger.new(post_ids: [p1.id], topic_id: topic.id, new_owner: nil, acting_user: editor)
|
2022-02-15 20:52:20 -05:00
|
|
|
}.to raise_error(ArgumentError, /new_owner/)
|
2015-03-02 11:17:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "calls PostRevisor" do
|
|
|
|
PostRevisor.any_instance.expects(:revise!)
|
2018-06-05 03:29:17 -04:00
|
|
|
PostOwnerChanger.new(post_ids: [p1.id], topic_id: topic.id, new_owner: user_a, acting_user: editor).change_owner!
|
2015-03-02 11:17:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "changes the user" do
|
2020-03-10 17:13:17 -04:00
|
|
|
bumped_at = freeze_time topic.bumped_at
|
2020-08-19 11:21:02 -04:00
|
|
|
now = Time.zone.now
|
|
|
|
freeze_time(now - 1.day)
|
2016-03-16 17:32:27 -04:00
|
|
|
|
2015-03-02 11:17:11 -05:00
|
|
|
old_user = p1.user
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.like(user_a, p1)
|
2017-08-11 03:55:00 -04:00
|
|
|
p1.reload
|
|
|
|
expect(p1.topic.like_count).to eq(1)
|
2020-03-10 17:13:17 -04:00
|
|
|
|
2020-08-19 11:21:02 -04:00
|
|
|
freeze_time(now)
|
2016-03-16 08:49:27 -04:00
|
|
|
PostOwnerChanger.new(post_ids: [p1.id], topic_id: topic.id, new_owner: user_a, acting_user: editor).change_owner!
|
2015-03-02 11:17:11 -05:00
|
|
|
p1.reload
|
2017-08-11 03:55:00 -04:00
|
|
|
expect(p1.topic.like_count).to eq(0)
|
2020-03-10 17:13:17 -04:00
|
|
|
expect(p1.topic.bumped_at).to eq_time(bumped_at)
|
2016-03-16 08:49:27 -04:00
|
|
|
expect(p1.topic.last_post_user_id).to eq(user_a.id)
|
2015-03-02 11:17:11 -05:00
|
|
|
expect(old_user).not_to eq(p1.user)
|
|
|
|
expect(p1.user).to eq(user_a)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "changes multiple posts" do
|
2018-06-05 03:29:17 -04:00
|
|
|
PostOwnerChanger.new(post_ids: [p1.id, p2.id], topic_id: topic.id, new_owner: user_a, acting_user: editor).change_owner!
|
2015-03-02 11:17:11 -05:00
|
|
|
p1.reload; p2.reload
|
|
|
|
expect(p1.user).not_to eq(nil)
|
|
|
|
expect(p1.user).to eq(user_a)
|
|
|
|
expect(p1.user).to eq(p2.user)
|
|
|
|
end
|
|
|
|
|
2016-06-17 14:14:52 -04:00
|
|
|
it "ignores posts in other topics" do
|
2018-06-05 03:29:17 -04:00
|
|
|
PostOwnerChanger.new(post_ids: [p1.id, p3.id], topic_id: topic.id, new_owner: user_a, acting_user: editor).change_owner!
|
2016-06-17 14:14:52 -04:00
|
|
|
p1.reload; p3.reload
|
|
|
|
expect(p1.user).to eq(user_a)
|
|
|
|
|
|
|
|
expect(p3.topic_id).not_to eq(p1.topic_id)
|
|
|
|
expect(p2.user).not_to eq(user_a)
|
|
|
|
end
|
|
|
|
|
2016-08-19 15:27:12 -04:00
|
|
|
it "skips creating new post revision if skip_revision is true" do
|
2018-06-05 03:29:17 -04:00
|
|
|
PostOwnerChanger.new(post_ids: [p1.id, p2.id], topic_id: topic.id, new_owner: user_a, acting_user: editor, skip_revision: true).change_owner!
|
2016-08-19 15:27:12 -04:00
|
|
|
p1.reload; p2.reload
|
|
|
|
expect(p1.revisions.size).to eq(0)
|
|
|
|
expect(p2.revisions.size).to eq(0)
|
|
|
|
end
|
|
|
|
|
2018-02-27 09:46:20 -05:00
|
|
|
it "changes the user even when the post does not pass validation" do
|
|
|
|
p1.update_attribute(:raw, "foo")
|
|
|
|
PostOwnerChanger.new(post_ids: [p1.id], topic_id: topic.id, new_owner: user_a, acting_user: editor).change_owner!
|
2018-03-01 11:31:58 -05:00
|
|
|
expect(p1.reload.user).to eq(user_a)
|
|
|
|
end
|
2018-02-27 09:46:20 -05:00
|
|
|
|
2018-03-01 11:31:58 -05:00
|
|
|
it "changes the user even when the topic does not pass validation" do
|
|
|
|
topic.update_column(:title, "short")
|
|
|
|
|
|
|
|
PostOwnerChanger.new(post_ids: [p1.id], topic_id: topic.id, new_owner: user_a, acting_user: editor).change_owner!
|
|
|
|
expect(p1.reload.user).to eq(user_a)
|
2018-02-27 09:46:20 -05:00
|
|
|
end
|
|
|
|
|
2018-05-16 11:02:43 -04:00
|
|
|
it "changes the owner when the post is deleted" do
|
2018-06-05 03:29:17 -04:00
|
|
|
p4 = Fabricate(:post, topic: topic, reply_to_post_number: p2.post_number)
|
2018-05-16 11:02:43 -04:00
|
|
|
PostDestroyer.new(editor, p4).destroy
|
|
|
|
|
|
|
|
PostOwnerChanger.new(post_ids: [p4.id], topic_id: topic.id, new_owner: user_a, acting_user: editor).change_owner!
|
|
|
|
expect(p4.reload.user).to eq(user_a)
|
|
|
|
end
|
|
|
|
|
2022-04-04 13:12:38 -04:00
|
|
|
it "sets 'posted' for TopicUser to true" do
|
|
|
|
PostOwnerChanger.new(post_ids: [p1.id], topic_id: topic.id, new_owner: user_a, acting_user: editor).change_owner!
|
|
|
|
expect(TopicUser.find_by(topic_id: topic.id, user_id: user_a.id).posted).to eq(true)
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when setting topic notification level for the new owner" do
|
2019-01-03 12:03:01 -05:00
|
|
|
let(:p4) { create_post(post_number: 2, topic: topic) }
|
2018-04-16 05:48:06 -04:00
|
|
|
|
|
|
|
it "'watching' if the first post gets a new owner" do
|
2018-06-05 03:29:17 -04:00
|
|
|
PostOwnerChanger.new(post_ids: [p1.id], topic_id: topic.id, new_owner: user_a, acting_user: editor).change_owner!
|
2018-04-16 05:48:06 -04:00
|
|
|
tu = TopicUser.find_by(user_id: user_a.id, topic_id: topic.id)
|
|
|
|
expect(tu.notification_level).to eq(3)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "'tracking' if other than the first post gets a new owner" do
|
2018-06-05 03:29:17 -04:00
|
|
|
PostOwnerChanger.new(post_ids: [p4.id], topic_id: topic.id, new_owner: user_a, acting_user: editor).change_owner!
|
2018-04-16 05:48:06 -04:00
|
|
|
tu = TopicUser.find_by(user_id: user_a.id, topic_id: topic.id)
|
|
|
|
expect(tu.notification_level).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with integration tests" do
|
2015-03-03 14:15:11 -05:00
|
|
|
let(:p1user) { p1.user }
|
|
|
|
let(:p2user) { p2.user }
|
2015-03-02 11:17:11 -05:00
|
|
|
|
2015-03-03 14:15:11 -05:00
|
|
|
before do
|
2017-01-15 21:18:10 -05:00
|
|
|
topic.update!(user_id: p1user.id)
|
|
|
|
|
|
|
|
p1user.user_stat.update!(
|
|
|
|
topic_count: 1,
|
2022-02-06 22:23:34 -05:00
|
|
|
post_count: 0,
|
2017-01-15 21:18:10 -05:00
|
|
|
first_post_created_at: p1.created_at,
|
|
|
|
)
|
|
|
|
|
|
|
|
p2user.user_stat.update!(
|
|
|
|
topic_count: 0,
|
|
|
|
post_count: 1,
|
|
|
|
first_post_created_at: p2.created_at,
|
|
|
|
)
|
2015-03-03 14:15:11 -05:00
|
|
|
|
|
|
|
UserAction.create!(action_type: UserAction::NEW_TOPIC, user_id: p1user.id, acting_user_id: p1user.id,
|
2015-05-29 17:39:24 -04:00
|
|
|
target_post_id: -1, target_topic_id: p1.topic_id, created_at: p1.created_at)
|
2015-03-03 14:15:11 -05:00
|
|
|
UserAction.create!(action_type: UserAction::REPLY, user_id: p2user.id, acting_user_id: p2user.id,
|
|
|
|
target_post_id: p2.id, target_topic_id: p2.topic_id, created_at: p2.created_at)
|
2016-12-21 23:03:40 -05:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
UserActionManager.enable
|
2015-03-03 14:15:11 -05:00
|
|
|
end
|
|
|
|
|
2017-01-15 21:18:10 -05:00
|
|
|
subject(:change_owners) do
|
2018-06-05 03:29:17 -04:00
|
|
|
PostOwnerChanger.new(
|
2017-01-15 21:18:10 -05:00
|
|
|
post_ids: [p1.id, p2.id],
|
|
|
|
topic_id: topic.id,
|
|
|
|
new_owner: user_a,
|
|
|
|
acting_user: editor
|
|
|
|
).change_owner!
|
|
|
|
end
|
2015-03-03 14:15:11 -05:00
|
|
|
|
|
|
|
it "updates users' topic and post counts" do
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.like(p2user, p1)
|
2017-01-15 21:18:10 -05:00
|
|
|
expect(p1user.user_stat.reload.likes_received).to eq(1)
|
|
|
|
|
2015-03-03 14:15:11 -05:00
|
|
|
change_owners
|
|
|
|
|
|
|
|
p1user.reload; p2user.reload; user_a.reload
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(p1user.topic_count).to eq(0)
|
|
|
|
expect(p1user.post_count).to eq(0)
|
|
|
|
expect(p2user.topic_count).to eq(0)
|
|
|
|
expect(p2user.post_count).to eq(0)
|
|
|
|
expect(user_a.topic_count).to eq(1)
|
2022-02-06 22:23:34 -05:00
|
|
|
expect(user_a.post_count).to eq(1)
|
2017-01-15 21:18:10 -05:00
|
|
|
|
|
|
|
p1_user_stat = p1user.user_stat
|
|
|
|
|
|
|
|
expect(p1_user_stat.first_post_created_at).to eq(nil)
|
|
|
|
expect(p1_user_stat.likes_received).to eq(0)
|
|
|
|
|
|
|
|
p2_user_stat = p2user.user_stat
|
|
|
|
|
|
|
|
expect(p2_user_stat.first_post_created_at).to eq(nil)
|
|
|
|
|
|
|
|
user_a_stat = user_a.user_stat
|
|
|
|
|
|
|
|
expect(user_a_stat.first_post_created_at).to be_present
|
|
|
|
expect(user_a_stat.likes_received).to eq(1)
|
|
|
|
end
|
|
|
|
|
2017-11-02 17:48:48 -04:00
|
|
|
it "handles whispers" do
|
|
|
|
whisper = PostCreator.new(
|
|
|
|
editor,
|
|
|
|
topic_id: p1.topic_id,
|
|
|
|
reply_to_post_number: 1,
|
|
|
|
post_type: Post.types[:whisper],
|
|
|
|
raw: 'this is a whispered reply'
|
|
|
|
).create
|
|
|
|
|
|
|
|
user_stat = editor.user_stat
|
|
|
|
|
|
|
|
expect {
|
2018-06-05 03:29:17 -04:00
|
|
|
PostOwnerChanger.new(
|
2017-11-02 17:48:48 -04:00
|
|
|
post_ids: [whisper.id],
|
|
|
|
topic_id: topic.id,
|
|
|
|
new_owner: Fabricate(:admin),
|
|
|
|
acting_user: editor
|
|
|
|
).change_owner!
|
|
|
|
}.to_not change { user_stat.reload.post_count }
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'with private message topic' do
|
2019-01-03 12:03:01 -05:00
|
|
|
let(:pm) do
|
|
|
|
create_post(
|
|
|
|
archetype: 'private_message',
|
|
|
|
target_usernames: [p2user.username]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
let(:pm_poster) { pm.user }
|
2017-01-15 21:18:10 -05:00
|
|
|
|
|
|
|
it "should update users' counts" do
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.like(p2user, pm)
|
2017-01-15 21:18:10 -05:00
|
|
|
|
2017-11-02 17:48:48 -04:00
|
|
|
expect {
|
2019-01-03 12:03:01 -05:00
|
|
|
PostOwnerChanger.new(
|
|
|
|
post_ids: [pm.id],
|
|
|
|
topic_id: pm.topic_id,
|
|
|
|
new_owner: user_a,
|
|
|
|
acting_user: editor
|
|
|
|
).change_owner!
|
|
|
|
}.to_not change { pm_poster.user_stat.post_count }
|
|
|
|
|
|
|
|
expect(pm_poster.user_stat.likes_received).to eq(0)
|
2017-01-15 21:18:10 -05:00
|
|
|
|
|
|
|
user_a_stat = user_a.user_stat
|
|
|
|
expect(user_a_stat.first_post_created_at).to be_present
|
|
|
|
expect(user_a_stat.likes_received).to eq(0)
|
2017-11-02 17:48:48 -04:00
|
|
|
expect(user_a_stat.post_count).to eq(0)
|
2017-01-15 21:18:10 -05:00
|
|
|
end
|
2015-03-03 14:15:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "updates UserAction records" do
|
|
|
|
g = Guardian.new(editor)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(UserAction.stats(user_a.id, g)).to eq([])
|
2015-03-03 14:15:11 -05:00
|
|
|
|
|
|
|
change_owners
|
2015-03-02 11:17:11 -05:00
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(UserAction.stats(p1user.id, g)).to eq([])
|
|
|
|
expect(UserAction.stats(p2user.id, g)).to eq([])
|
2015-03-03 14:15:11 -05:00
|
|
|
stats = UserAction.stats(user_a.id, g)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(stats.size).to eq(2)
|
|
|
|
expect(stats[0].action_type).to eq(UserAction::NEW_TOPIC)
|
|
|
|
expect(stats[0].count).to eq(1)
|
|
|
|
expect(stats[1].action_type).to eq(UserAction::REPLY)
|
|
|
|
expect(stats[1].count).to eq(1)
|
2015-03-03 14:15:11 -05:00
|
|
|
end
|
2021-07-27 14:49:08 -04:00
|
|
|
|
|
|
|
it "updates reply_to_user_id" do
|
|
|
|
p4 = Fabricate(:post, topic: topic, reply_to_post_number: p1.post_number, reply_to_user_id: p1.user_id)
|
|
|
|
p5 = Fabricate(:post, topic: topic, reply_to_post_number: p2.post_number, reply_to_user_id: p2.user_id)
|
|
|
|
|
|
|
|
PostOwnerChanger.new(post_ids: [p1.id], topic_id: topic.id, new_owner: user_a, acting_user: editor).change_owner!
|
|
|
|
p4.reload; p5.reload
|
|
|
|
|
|
|
|
expect(p4.reply_to_user_id).to eq(user_a.id)
|
|
|
|
expect(p5.reply_to_user_id).to eq(p2.user_id)
|
|
|
|
end
|
2015-03-02 11:17:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|