Skip to content

Commit 3b25417

Browse files
committed
added tests for the FixRejectedEditStatesTask & simplified it
1 parent 6153957 commit 3b25417

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

app/tasks/maintenance/fix_rejected_edit_states_task.rb

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ def last_revision_before_decided(edit)
1919
def initial_revision_before_decided(edit)
2020
PostHistory.of_type('initial_revision')
2121
.where(post: edit.post)
22-
.where(created_at: edit.post.created_at..edit.decided_at)
2322
.order(created_at: :desc)
2423
.first
2524
end
@@ -42,18 +41,18 @@ def process(edit)
4241

4342
return unless last_revision.present?
4443

45-
begin
46-
status = edit.update({ before_body: render_markdown(last_revision.after_state),
47-
before_body_markdown: last_revision.after_state,
48-
before_tags: last_revision.after_tags.dup,
49-
before_tags_cache: last_revision.after_tags.map(&:name),
50-
before_title: last_revision.after_title })
51-
52-
unless status
53-
Rails.logger.warn("Failed to fix edit ##{edit.id}")
54-
end
55-
rescue StandardError => e
56-
Rails.logger.warn(e)
44+
status = edit.update({ before_body: if last_revision.after_state.present?
45+
render_markdown(last_revision.after_state)
46+
else
47+
edit.before_body
48+
end,
49+
before_body_markdown: last_revision.after_state,
50+
before_tags: last_revision.after_tags.dup,
51+
before_tags_cache: last_revision.after_tags.map(&:name),
52+
before_title: last_revision.after_title })
53+
54+
unless status
55+
Rails.logger.warn("Failed to fix edit ##{edit.id}")
5756
end
5857
end
5958
end

test/fixtures/post_histories.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@ question_one_hidden_revision:
1414
comment: 'A hidden revision made by an admin user'
1515
community: sample
1616
hidden: true
17+
18+
question_two_initial_revision:
19+
post_history_type: initial_revision
20+
user: editor
21+
post: question_two
22+
community: sample
23+
after_state: This is the body of test question two. Note that we did not include any markdown or HTML in here.
24+
after_title: Q2 - This is test question number two
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'test_helper'
2+
3+
module Maintenance
4+
class FixRejectedEditStatesTaskTest < ActiveSupport::TestCase
5+
test 'perform should correctly fix rejected edit states' do
6+
edit = suggested_edits(:rejected_suggested_edit)
7+
8+
Maintenance::FixRejectedEditStatesTask.process(edit)
9+
edit.reload
10+
11+
assert_not_nil edit.before_body
12+
assert_not_nil edit.before_body_markdown
13+
assert_not_nil edit.before_title
14+
end
15+
end
16+
end

0 commit comments

Comments
 (0)