DEV: trigger :username_changed when a user is renamed (#9301)

DEV: trigger :username_changed when a user is renamed (#9301)

diff --git a/app/jobs/regular/update_username.rb b/app/jobs/regular/update_username.rb
index a92cf82..d43c119 100644
--- a/app/jobs/regular/update_username.rb
+++ b/app/jobs/regular/update_username.rb
@@ -34,6 +34,8 @@ module Jobs
       update_revisions
       update_notifications
       update_post_custom_fields
+
+      DiscourseEvent.trigger(:username_changed, @old_username, @new_username)
     end
 
     def update_posts
diff --git a/spec/services/username_changer_spec.rb b/spec/services/username_changer_spec.rb
index 3fb20ce..0013c82 100644
--- a/spec/services/username_changer_spec.rb
+++ b/spec/services/username_changer_spec.rb
@@ -11,13 +11,20 @@ describe UsernameChanger do
     let(:user) { Fabricate(:user) }
 
     context 'success' do
+      let!(:old_username) { user.username }
       let(:new_username) { "#{user.username}1234" }
 
       it 'should change the username' do
-        @result = UsernameChanger.change(user, new_username)
+        event = DiscourseEvent.track_events {
+          @result = UsernameChanger.change(user, new_username)
+        }.last
 
         expect(@result).to eq(true)
 
+        expect(event[:event_name]).to eq(:username_changed)
+        expect(event[:params].first).to eq(old_username)
+        expect(event[:params].second).to eq(new_username)
+
         user.reload
         expect(user.username).to eq(new_username)
         expect(user.username_lower).to eq(new_username.downcase)

GitHub sha: 96e841a6

This commit appears in #9301 which was approved by gschlager and eviltrout. It was merged by jjaffeux.