Skip to content

A platform for community discussion. Free, open, simple.

FIX: Allow to delete first posts with bulk destroy (#37180)

Loïc Guitaut3ac5a0fa5deb
diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
index 205c654ff9d..a15ba79f549 100644
--- a/app/controllers/posts_controller.rb
+++ b/app/controllers/posts_controller.rb
@@ -424,8 +424,7 @@ class PostsController < ApplicationController
posts = Post.where(id: post_ids_including_replies).order(:id)
raise Discourse::InvalidParameters.new(:post_ids) if posts.blank?
- # Make sure we can delete the posts
- posts.each { |p| guardian.ensure_can_delete!(p) }
+ posts.each { |p| guardian.ensure_can_delete_post_or_topic!(p) }
Post.transaction do
posts.each_with_index do |p, i|
diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rb
index 059adfaf526..3e95564c7cd 100644
--- a/spec/requests/posts_controller_spec.rb
+++ b/spec/requests/posts_controller_spec.rb
@@ -360,22 +360,22 @@ RSpec.describe PostsController do
end
describe "#destroy_many" do
- include_examples "action requires login",
- :delete,
- "/posts/destroy_many.json",
- params: {
- post_ids: [123, 345],
- }
-
- describe "when logged in" do
+ it_behaves_like "action requires login",
+ :delete,
+ "/posts/destroy_many.json",
+ params: {
+ post_ids: [123, 345],
+ }
+
+ context "when logged in" do
fab!(:poster, :moderator)
- fab!(:post1) { Fabricate(:post, user: poster, post_number: 2) }
+ fab!(:post1) { Fabricate(:post, user: poster, post_number: 1) }
fab!(:post2) do
Fabricate(
:post,
topic: post1.topic,
user: poster,
- post_number: 3,
+ post_number: 2,
reply_to_post_number: post1.post_number,
)
end
@@ -405,6 +405,7 @@ RSpec.describe PostsController do
delete "/posts/destroy_many.json", params: { post_ids: [post1.id, post2.id] }
expect(response.status).to eq(200)
end
+
# bookmark
it "triggers DiscourseEvent with :posts_destroyed and correct params" do
sign_in(poster)
@@ -424,17 +425,19 @@ RSpec.describe PostsController do
delete "/posts/destroy_many.json", params: { post_ids: [post1.id, post2.id] }
end
- describe "can delete replies" do
- before { PostReply.create(post_id: post1.id, reply_post_id: post2.id) }
+ context "with replies" do
+ fab!(:post_reply) { PostReply.create(post: post1, reply: post2) }
+
+ before { sign_in(poster) }
it "deletes the post and the reply to it" do
- sign_in(poster)
- PostDestroyer.any_instance.expects(:destroy).twice
- delete "/posts/destroy_many.json",
- params: {
- post_ids: [post1.id],
- reply_post_ids: [post1.id],
- }
+ expect do
+ delete "/posts/destroy_many.json",
+ params: {
+ post_ids: [post1.id],
+ reply_post_ids: [post1.id],
+ }
+ end.to change { Post.count }.by(-2).and change { Topic.count }.by(-1)
end
end