diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index d4e9f3f0bd40f698c477686315c2c193cf93f276..d2aea372eb459ee27e5aac7564427b511381e229 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -262,6 +262,15 @@ class PostsController < ApplicationController if @post.update(deleted: true, deleted_at: DateTime.now, deleted_by: current_user, last_activity: DateTime.now, last_activity_by: current_user) PostHistory.post_deleted(@post, current_user) + if @post.children.any? + @post.children.update_all(deleted: true, deleted_at: DateTime.now, deleted_by_id: current_user.id, + last_activity: DateTime.now, last_activity_by_id: current_user.id) + histories = @post.children.map do |c| + { post_history_type: PostHistoryType.find_by(name: 'post_deleted'), user: current_user, post: c, + community: RequestContext.community } + end + PostHistory.create(histories) + end else flash[:danger] = "Can't delete this post right now. Try again later." end @@ -288,9 +297,18 @@ class PostsController < ApplicationController return end + deleted_at = @post.deleted_at if @post.update(deleted: false, deleted_at: nil, deleted_by: nil, last_activity: DateTime.now, last_activity_by: current_user) PostHistory.post_undeleted(@post, current_user) + restore_children = @post.children.where('deleted_at >= ?', deleted_at) + restore_children.update_all(deleted: true, deleted_at: DateTime.now, deleted_by_id: current_user.id, + last_activity: DateTime.now, last_activity_by_id: current_user.id) + histories = restore_children.map do |c| + { post_history_type: PostHistoryType.find_by(name: 'post_undeleted'), user: current_user, post: c, + community: RequestContext.community } + end + PostHistory.create(histories) else flash[:danger] = "Can't restore this post right now. Try again later." end diff --git a/test/controllers/posts_controller_test.rb b/test/controllers/posts_controller_test.rb index e0a7c821dee8c4cfe85e05721ee430f389b15329..f59de979b46d35a966f937339cd2a65274036a39 100644 --- a/test/controllers/posts_controller_test.rb +++ b/test/controllers/posts_controller_test.rb @@ -591,6 +591,19 @@ class PostsControllerTest < ActionController::TestCase assert_equal before_history, after_history, 'PostHistory event incorrectly created on deletion' end + test 'delete ensures all children are deleted' do + sign_in users(:deleter) + before_history = PostHistory.where(post_id: posts(:bad_answers).children.map(&:id)).count + post :delete, params: { id: posts(:bad_answers).id } + after_history = PostHistory.where(post_id: posts(:bad_answers).children.map(&:id)).count + assert_response 302 + assert_redirected_to post_path(assigns(:post)) + assert_nil flash[:danger] + assert assigns(:post).children.all?(&:deleted), 'Answers not deleted with question' + assert_equal before_history + posts(:bad_answers).children.count, after_history, + 'Answer PostHistory events not created on question deletion' + end + # Restore test 'can restore post' do @@ -652,6 +665,21 @@ class PostsControllerTest < ActionController::TestCase assert_equal before_history, after_history, 'PostHistory event incorrectly created on deletion' end + test 'restore brings back all answers deleted after question' do + sign_in users(:deleter) + deleted_at = posts(:deleted).deleted_at + children = posts(:deleted).children.where('deleted_at >= ?', deleted_at) + children_count = children.count + before_history = PostHistory.where(post_id: children.where('deleted_at >= ?', deleted_at)).count + post :restore, params: { id: posts(:deleted).id } + after_history = PostHistory.where(post_id: children.where('deleted_at >= ?', deleted_at)).count + assert_response 302 + assert_redirected_to post_path(assigns(:post)) + assert_nil flash[:danger] + assert_equal before_history + children_count, after_history, + 'Answer PostHistory events not created on question restore' + end + # Toggle comments test 'can toggle comments' do diff --git a/test/fixtures/posts.yml b/test/fixtures/posts.yml index bdea67b9f86f45b01feb236d2cea00824df523c9..54e50a21a3d00b8259dea8ec0b6219eff0a82e25 100644 --- a/test/fixtures/posts.yml +++ b/test/fixtures/posts.yml @@ -40,6 +40,27 @@ question_two: upvote_count: 0 downvote_count: 0 +bad_answers: + post_type: question + title: Q1 ABCDEF GHIJKL MNOPQR STUVWX YZ + body: ABCDEF GHIJKL MNOPQR STUVWX YZ ABCDEF GHIJKL MNOPQR STUVWX YZ + body_markdown: ABCDEF GHIJKL MNOPQR STUVWX YZ ABCDEF GHIJKL MNOPQR STUVWX YZ + tags_cache: + - discussion + - support + - bug + tags: + - discussion + - support + - bug + score: 0.5 + user: standard_user + community: sample + category: main + license: cc_by_sa + upvote_count: 0 + downvote_count: 0 + deleted: post_type: question title: Q3D ZY XWVUTS RQPONM LKJIHG FEDCBA @@ -188,6 +209,19 @@ answer_two: upvote_count: 0 downvote_count: 0 +bad_answer: + post_type: answer + body: A2 ABCDEF GHIJKL MNOPQR STUVWX YZ ABCDEF GHIJKL MNOPQR STUVWX YZ + body_markdown: ZY XWVUTS RQPONM LKJIHG FEDCBA ZY XWVUTS RQPONM LKJIHG FEDCBA + score: 0.4 + parent: question_one + user: editor + community: sample + category: main + license: cc_by_sa + upvote_count: 0 + downvote_count: 1 + really_old_answer: post_type: answer body: A3RO ABCDEF GHIJKL MNOPQR STUVWX YZ ABCDEF GHIJKL MNOPQR STUVWX YZ