FIX: do not validate topic deletions

This commit is contained in:
Arpit Jalan 2018-07-13 21:45:59 +05:30
parent 06deffc9da
commit b1082924b9
2 changed files with 16 additions and 11 deletions

View File

@ -168,12 +168,12 @@ class PostDestroyer
def make_previous_post_the_last_one
last_post = Post.where("topic_id = ? and id <> ?", @post.topic_id, @post.id).order('created_at desc').limit(1).first
if last_post.present?
@post.topic.update!(
last_posted_at: last_post.created_at,
last_post_user_id: last_post.user_id,
highest_post_number: last_post.post_number
)
if last_post.present? && @post.topic.present?
topic = @post.topic
topic.last_posted_at = last_post.created_at
topic.last_post_user_id = last_post.user_id
topic.highest_post_number = last_post.post_number
topic.save!(validate: false)
end
end

View File

@ -265,9 +265,10 @@ describe PostDestroyer do
end
it "when topic is destroyed, it updates user_stats correctly" do
post
SiteSetting.min_topic_title_length = 5
post.topic.update_column(:title, "xyz")
user1 = post.user
user1.reload
user2 = Fabricate(:user)
reply = create_post(topic_id: post.topic_id, user: user2)
reply2 = create_post(topic_id: post.topic_id, user: user1)
@ -275,6 +276,7 @@ describe PostDestroyer do
expect(user1.user_stat.post_count).to eq(1)
expect(user2.user_stat.topic_count).to eq(0)
expect(user2.user_stat.post_count).to eq(1)
PostDestroyer.new(Fabricate(:admin), post).destroy
user1.reload
user2.reload
@ -390,12 +392,13 @@ describe PostDestroyer do
let(:user) { Fabricate(:user) }
let!(:post) { create_post(user: user) }
let(:topic) { post.topic.reload }
let(:topic) { post.topic }
let(:second_user) { Fabricate(:coding_horror) }
let!(:second_post) { create_post(topic: topic, user: second_user) }
before do
PostDestroyer.new(moderator, second_post).destroy
topic.reload
end
it 'resets the last_poster_id back to the OP' do
@ -406,6 +409,10 @@ describe PostDestroyer do
expect(topic.last_posted_at.to_i).to eq(post.created_at.to_i)
end
it 'resets the highest_post_number' do
expect(topic.highest_post_number).to eq(post.post_number)
end
context 'topic_user' do
let(:topic_user) { second_user.topic_users.find_by(topic_id: topic.id) }
@ -421,9 +428,7 @@ describe PostDestroyer do
it "sets the second user's last_read_post_number back to 1" do
expect(topic_user.highest_seen_post_number).to eq(1)
end
end
end
context "deleting a post belonging to a deleted topic" do