DEV: Plugin modifier to skip enqueue PostCreator jobs on PostMove (#30344)

This allows plugins to skip the "posted" notifications for watching users, when posts get moved. The specs are kind of wild looking, as this unit tests a private method. This is difficult to isolate otherwise, with lots of trickery needed to make sure that this actually works.

I opted to unit test just this method instead.
This commit is contained in:
Mark VanLandingham 2024-12-18 12:37:52 -06:00 committed by GitHub
parent 4d0cbc08dc
commit 5721c29429
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 1 deletions

View File

@ -746,7 +746,12 @@ class PostMover
end
def enqueue_jobs(topic)
@post_creator.enqueue_jobs if @post_creator
enqueue_post_creator_jobs =
DiscoursePluginRegistry.apply_modifier(
:post_mover_enqueue_post_creator_jobs,
@post_creator.present?,
)
@post_creator.enqueue_jobs if enqueue_post_creator_jobs
Jobs.enqueue(:notify_moved_posts, post_ids: @post_ids_after_move, moved_by_id: user.id)

View File

@ -3169,5 +3169,44 @@ RSpec.describe PostMover do
end
end
end
describe "#enqueue_jobs" do
fab!(:admin)
fab!(:original_topic) { Fabricate(:topic) }
fab!(:post_1) { Fabricate(:post, topic: original_topic) }
fab!(:post_2) { Fabricate(:post, topic: original_topic) }
fab!(:destination_topic) { Fabricate(:topic) }
it "calls enqueue jobs for PostCreator when @post_creator is present" do
post_mover = PostMover.new(original_topic, admin, [post_1, post_2])
PostCreator.any_instance.expects(:enqueue_jobs).once
post_mover.instance_variable_set(:@post_creator, PostCreator.new(admin, {}))
post_mover.send(:enqueue_jobs, destination_topic)
end
context "with post_mover_enqueue_post_creator_jobs modifier" do
let(:modifier_block) { Proc.new { |_| false } }
it "skips enqueuing jobs when the modifier returns false" do
plugin_instance = Plugin::Instance.new
plugin_instance.register_modifier(:post_mover_enqueue_post_creator_jobs, &modifier_block)
post_mover = PostMover.new(original_topic, admin, [post_1, post_2])
PostCreator.any_instance.expects(:enqueue_jobs).never
post_mover.instance_variable_set(:@post_creator, PostCreator.new(admin, {}))
post_mover.send(:enqueue_jobs, destination_topic)
ensure
DiscoursePluginRegistry.unregister_modifier(
plugin_instance,
:post_mover_enqueue_post_creator_jobs,
&modifier_block
)
end
end
end
end
end