DEV: calls user_added_to_group on group/TL enforcement (#13222)

This commit is contained in:
Joffrey JAFFEUX 2021-06-01 10:34:41 +02:00 committed by GitHub
parent 1cd0424ccd
commit 624edac3bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -603,7 +603,8 @@ class Group < ActiveRecord::Base
desired.each do |id|
if group = find_by(id: id)
unless GroupUser.where(group_id: id, user_id: user_id).exists?
group.group_users.create!(user_id: user_id)
group_user = group.group_users.create!(user_id: user_id)
DiscourseEvent.trigger(:user_added_to_group, group_user.user, group, automatic: true)
end
else
name = AUTO_GROUP_IDS[trust_level]

View File

@ -546,6 +546,22 @@ describe Group do
expect(user.groups.map(&:name).sort).to eq ["trust_level_0"]
end
it "generates an event when applying group from trust level change" do
called = nil
block = Proc.new { |user, group| called = { user_id: user.id, group_id: group.id } }
begin
DiscourseEvent.on(:user_added_to_group, &block)
user = Fabricate(:user, trust_level: 2)
Group.user_trust_level_change!(user.id, 2)
expect(called).to eq(user_id: user.id, group_id: Group.find_by(name: 'trust_level_2').id)
ensure
DiscourseEvent.off(:user_added_to_group, &block)
end
end
context "group management" do
fab!(:group) { Fabricate(:group) }