diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb index c94d03e108f..eab76c38fdf 100644 --- a/lib/post_revisor.rb +++ b/lib/post_revisor.rb @@ -75,11 +75,17 @@ class PostRevisor end end - track_topic_field(:category_id) do |tc, category_id| + track_topic_field(:category_id) do |tc, category_id, fields| if category_id == 0 && tc.topic.private_message? tc.record_change('category_id', tc.topic.category_id, nil) tc.topic.category_id = nil elsif category_id == 0 || tc.guardian.can_move_topic_to_category?(category_id) + tags = fields[:tags] || tc.topic.tags.map(&:name) + if category_id != 0 && !DiscourseTagging.validate_min_required_tags_for_category(tc.guardian, tc.topic, Category.find(category_id), tags) + tc.check_result(false) + next + end + tc.record_change('category_id', tc.topic.category_id, category_id) tc.check_result(tc.topic.change_category_to_id(category_id)) end @@ -454,7 +460,7 @@ class PostRevisor Topic.transaction do PostRevisor.tracked_topic_fields.each do |f, cb| if !@topic_changes.errored? && @fields.has_key?(f) - cb.call(@topic_changes, @fields[f]) + cb.call(@topic_changes, @fields[f], @fields) end end diff --git a/spec/components/post_revisor_spec.rb b/spec/components/post_revisor_spec.rb index 00f49f1342a..7926c487239 100644 --- a/spec/components/post_revisor_spec.rb +++ b/spec/components/post_revisor_spec.rb @@ -84,6 +84,28 @@ describe PostRevisor do post.revise(post.user, category_id: new_category.id) expect(post.reload.topic.category_id).to eq(new_category.id) end + + it 'does not revise category if incorrect amount of tags' do + SiteSetting.min_trust_to_create_tag = 0 + SiteSetting.min_trust_level_to_tag_topics = 0 + + new_category = Fabricate(:category, minimum_required_tags: 1) + + post = create_post + old_category_id = post.topic.category_id + + post.revise(post.user, category_id: new_category.id) + expect(post.reload.topic.category_id).to eq(old_category_id) + + tag = Fabricate(:tag) + topic_tag = Fabricate(:topic_tag, topic: post.topic, tag: tag) + post.revise(post.user, category_id: new_category.id) + expect(post.reload.topic.category_id).to eq(new_category.id) + topic_tag.destroy + + post.revise(post.user, category_id: new_category.id, tags: ['test_tag']) + expect(post.reload.topic.category_id).to eq(new_category.id) + end end context 'revise wiki' do