From 35c58c1b009cccd795ff517ac4f19934bb5d905a Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Tue, 3 Mar 2015 14:15:11 -0500 Subject: [PATCH] FIX: after changing post owner, profile pages still showed previous owner and incorrect topic and post counts --- lib/post_revisor.rb | 5 ++ spec/services/post_owner_changer_spec.rb | 60 ++++++++++++++++++------ 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb index 251922b361e..6e7f22cf568 100644 --- a/lib/post_revisor.rb +++ b/lib/post_revisor.rb @@ -211,6 +211,11 @@ class PostRevisor @post.is_first_post? ? {topic_count: new_owner.topic_count + 1} : {} )) end + UserAction.where( target_post_id: @post.id, + user_id: @post.user_id, + action_type: [UserAction::NEW_TOPIC, UserAction::REPLY, UserAction::RESPONSE] ) + .find_each { |ua| ua.destroy } + # UserActionObserver will create new UserAction records for the new owner end POST_TRACKED_FIELDS.each do |field| diff --git a/spec/services/post_owner_changer_spec.rb b/spec/services/post_owner_changer_spec.rb index 699aa2e9fed..076accd1f60 100644 --- a/spec/services/post_owner_changer_spec.rb +++ b/spec/services/post_owner_changer_spec.rb @@ -35,24 +35,54 @@ describe PostOwnerChanger do expect(p1.user).to eq(p2.user) end - it "updates users' topic and post counts" do - p1user = p1.user - p2user = p2.user - topic.user_id = p1user.id - topic.save! + context "integration tests" do + let(:p1user) { p1.user } + let(:p2user) { p2.user } - p1user.user_stat.update_attributes(topic_count: 1, post_count: 1) - p2user.user_stat.update_attributes(topic_count: 0, post_count: 1) + before do + topic.user_id = p1user.id + topic.save! - described_class.new(post_ids: [p1.id, p2.id], topic_id: topic.id, new_owner: user_a, acting_user: editor).change_owner! + p1user.user_stat.update_attributes(topic_count: 1, post_count: 1) + p2user.user_stat.update_attributes(topic_count: 0, post_count: 1) - p1user.reload; p2user.reload; user_a.reload - p1user.topic_count.should == 0 - p1user.post_count.should == 0 - p2user.topic_count.should == 0 - p2user.post_count.should == 0 - user_a.topic_count.should == 1 - user_a.post_count.should == 2 + UserAction.create!( action_type: UserAction::NEW_TOPIC, user_id: p1user.id, acting_user_id: p1user.id, + target_post_id: p1.id, target_topic_id: p1.topic_id, created_at: p1.created_at ) + 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 ) + + ActiveRecord::Base.observers.enable :user_action_observer + end + + subject(:change_owners) { described_class.new(post_ids: [p1.id, p2.id], topic_id: topic.id, new_owner: user_a, acting_user: editor).change_owner! } + + it "updates users' topic and post counts" do + change_owners + + p1user.reload; p2user.reload; user_a.reload + p1user.topic_count.should == 0 + p1user.post_count.should == 0 + p2user.topic_count.should == 0 + p2user.post_count.should == 0 + user_a.topic_count.should == 1 + user_a.post_count.should == 2 + end + + it "updates UserAction records" do + g = Guardian.new(editor) + UserAction.stats(user_a.id, g).should == [] + + change_owners + + UserAction.stats(p1user.id, g).should == [] + UserAction.stats(p2user.id, g).should == [] + stats = UserAction.stats(user_a.id, g) + stats.size.should == 2 + stats[0].action_type.should == UserAction::NEW_TOPIC + stats[0].count.should == 1 + stats[1].action_type.should == UserAction::REPLY + stats[1].count.should == 1 + end end end end