DEV: Add tl3_custom_promotions plugin modifier to tl3_promotions.rb (#29834)

* DEV: Add tl3_custom_promotions plugin modifier to tl3_promotions.rb

* added tests

* added tests for demotions

* changed argument order in test
This commit is contained in:
Juan David Martínez Cubillos 2024-11-22 15:28:43 -05:00 committed by GitHub
parent 0295b4165c
commit 08440b0035
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 0 deletions

View File

@ -22,6 +22,19 @@ module Jobs
# Don't demote too soon after being promoted
next if u.on_tl3_grace_period?
modifier_applied, demoted_user_id =
DiscoursePluginRegistry.apply_modifier(
:tl3_custom_demotions,
false,
u,
demoted_user_ids,
)
if modifier_applied
demoted_user_ids << demoted_user_id
next
end
if Promotion.tl3_lost?(u)
demoted_user_ids << u.id
Promotion.new(u).change_trust_level!(TrustLevel[2])
@ -29,6 +42,10 @@ module Jobs
end
end
override =
DiscoursePluginRegistry.apply_modifier(:tl3_custom_promotions, false, demoted_user_ids)
return override if override
# Promotions
User
.real

View File

@ -14,6 +14,10 @@ RSpec.describe Jobs::Tl3Promotions do
subject(:run_job) { described_class.new.execute({}) }
let!(:plugin) { Plugin::Instance.new }
let!(:allow_block) { Proc.new { true } }
let!(:array_block) { Proc.new { [true, 1] } }
it "promotes tl2 user who qualifies for tl3" do
tl2_user = Fabricate(:user, trust_level: TrustLevel[2])
create_qualifying_stats(tl2_user)
@ -43,6 +47,26 @@ RSpec.describe Jobs::Tl3Promotions do
run_job
end
it "allows plugins to control tl3_promotion's promotions" do
DiscoursePluginRegistry.register_modifier(plugin, :tl3_custom_promotions, &allow_block)
TrustLevel3Requirements.any_instance.stubs(:requirements_met?).never
tl2_user = Fabricate(:user, trust_level: TrustLevel[2])
create_qualifying_stats(tl2_user)
run_job
ensure
DiscoursePluginRegistry.unregister_modifier(plugin, :tl3_custom_promotions, &allow_block)
end
it "allows plugins to control tl3_promotion's demotions" do
DiscoursePluginRegistry.register_modifier(plugin, :tl3_custom_demotions, &array_block)
TrustLevel3Requirements.any_instance.stubs(:requirements_lost?).never
Fabricate(:user, trust_level: TrustLevel[3])
run_job
ensure
DiscoursePluginRegistry.unregister_modifier(plugin, :tl3_custom_demotions, &array_block)
end
context "with tl3 user who doesn't qualify for tl3 anymore" do
def create_leader_user
user = Fabricate(:user, trust_level: TrustLevel[2])