From 6121869900069f0c5dd2bc22b1a109f66a806f28 Mon Sep 17 00:00:00 2001 From: Kelv Date: Mon, 23 Oct 2023 14:46:10 +0800 Subject: [PATCH] FIX: display and link to correct grant_by users for granted badges (#24021) * FIX: display and link to correct grant_by users for granted badges in admin * DEV: add system test for admin user badge granted by behaviour --- .../addon/controllers/admin-user-badges.js | 7 +++++++ .../admin/addon/templates/user-badges.hbs | 2 +- .../discourse/app/models/user-badge.js | 5 +++++ spec/system/admin_user_badges_spec.rb | 21 +++++++++++++++++++ spec/system/page_objects/admin_user_badges.rb | 19 +++++++++++++++++ 5 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 spec/system/admin_user_badges_spec.rb create mode 100644 spec/system/page_objects/admin_user_badges.rb diff --git a/app/assets/javascripts/admin/addon/controllers/admin-user-badges.js b/app/assets/javascripts/admin/addon/controllers/admin-user-badges.js index 774d4de4466..51ed277a596 100644 --- a/app/assets/javascripts/admin/addon/controllers/admin-user-badges.js +++ b/app/assets/javascripts/admin/addon/controllers/admin-user-badges.js @@ -8,6 +8,7 @@ import { grantableBadges } from "discourse/lib/grant-badge-utils"; import UserBadge from "discourse/models/user-badge"; import discourseComputed from "discourse-common/utils/decorators"; import I18n from "discourse-i18n"; +import AdminUser from "admin/models/admin-user"; export default class AdminUserBadgesController extends Controller { @service dialog; @@ -61,6 +62,12 @@ export default class AdminUserBadgesController extends Controller { expanded.push(result); }); + expanded.forEach((badgeGroup) => { + const user = badgeGroup.granted_by; + if (user) { + badgeGroup.granted_by = AdminUser.create(user); + } + }); return expanded.sortBy("granted_at").reverse(); } diff --git a/app/assets/javascripts/admin/addon/templates/user-badges.hbs b/app/assets/javascripts/admin/addon/templates/user-badges.hbs index f847c98c90a..cab90603640 100644 --- a/app/assets/javascripts/admin/addon/templates/user-badges.hbs +++ b/app/assets/javascripts/admin/addon/templates/user-badges.hbs @@ -57,7 +57,7 @@ @count={{userBadge.count}} /> - + {{avatar userBadge.granted_by imageSize="tiny"}} {{userBadge.granted_by.username}} diff --git a/app/assets/javascripts/discourse/app/models/user-badge.js b/app/assets/javascripts/discourse/app/models/user-badge.js index 1757dd98157..5ee048e949b 100644 --- a/app/assets/javascripts/discourse/app/models/user-badge.js +++ b/app/assets/javascripts/discourse/app/models/user-badge.js @@ -44,6 +44,11 @@ UserBadge.reopenClass({ users[userJson.id] = User.create(userJson); }); + json.granted_bies = json.granted_bies ?? []; + json.granted_bies.forEach(function (userJson) { + users[userJson.id] = User.create(userJson); + }); + // Create Topic objects. if (json.topics === undefined) { json.topics = []; diff --git a/spec/system/admin_user_badges_spec.rb b/spec/system/admin_user_badges_spec.rb new file mode 100644 index 00000000000..46ed28048a5 --- /dev/null +++ b/spec/system/admin_user_badges_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +describe "Admin User Badges Page", type: :system do + before { SiteSetting.enable_badges = true } + + fab!(:granter) { Fabricate(:admin) } + fab!(:user) { Fabricate(:user) } + fab!(:badge) { Fabricate(:manually_grantable_badge) } + let(:user_badges_page) { PageObjects::Pages::AdminUserBadges.new } + + before { sign_in(granter) } + + it "displays badge granter and links to their profile" do + BadgeGranter.grant(badge, user, granted_by: granter) + badge_row = user_badges_page.visit_page(user).find_badge_row_by_granter(granter) + expect(badge_row).to have_css("[data-badge-name='#{badge.name}']") + + badge_row.click_link(granter.username) + expect(page).to have_current_path "/admin/users/#{granter.id}/#{granter.username}" + end +end diff --git a/spec/system/page_objects/admin_user_badges.rb b/spec/system/page_objects/admin_user_badges.rb new file mode 100644 index 00000000000..7cb746ee5ac --- /dev/null +++ b/spec/system/page_objects/admin_user_badges.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module PageObjects + module Pages + class AdminUserBadges < PageObjects::Pages::Base + def visit_page(user) + page.visit "/admin/users/#{user.id}/#{user.username}/badges" + self + end + + def user_badges_table + page.find(:table, id: "user-badges", visible: true) + end + def find_badge_row_by_granter(granter) + user_badges_table.find(:table_row, { "Granted By" => "#{granter.username}" }) + end + end + end +end