Skip to content

A platform for community discussion. Free, open, simple.

FIX: Explain disabled tags when required groups are unsatisfied (#44409)

Régis Hanol4b5c64f8c8eb
diff --git a/app/services/tags/search.rb b/app/services/tags/search.rb
index 9523f64b141..e1bb7dff4dd 100644
--- a/app/services/tags/search.rb
+++ b/app/services/tags/search.rb
@@ -124,6 +124,7 @@ class Tags::Search
context[:tags] = TagsController.tag_counts_json(tags_with_counts, guardian)
context[:required_tag_group] = filter_result_context[:required_tag_group]
+ context[:remaining_required_tag_count] = filter_result_context[:remaining_required_tag_count]
context[:forbidden] = nil
context[:forbidden_message] = nil
end
@@ -136,9 +137,16 @@ class Tags::Search
Tag.recently_used_by(guardian.user).presence
end
- def append_disabled_tags(params:, category:, tags:, guardian:)
- selected_ids = params.resolved_selected_tag_ids
- skip_ids = tags.map { |t| t[:id] } | selected_ids
+ def append_disabled_tags(
+ params:,
+ category:,
+ tags:,
+ guardian:,
+ required_tag_group:,
+ remaining_required_tag_count:
+ )
+ selected_tag_ids = params.resolved_selected_tag_ids
+ skip_ids = tags.map { |tag| tag[:id] } | selected_tag_ids
candidate_tags =
visible_tags(guardian)
@@ -149,15 +157,43 @@ class Tags::Search
return if candidate_tags.empty?
- excluded_tags = reject_allowed_tags(candidate_tags, params:, category:, tags:, guardian:)
+ excluded_tags = tags_excluded_by_filter(candidate_tags, params:, category:, tags:, guardian:)
+ return if excluded_tags.empty?
+
+ allowed_without_required_tag_groups =
+ if remaining_required_tag_count&.positive?
+ DiscourseTagging
+ .filter_allowed_tags(
+ guardian,
+ **params.filter_options.merge(
+ category:,
+ only_tag_names: excluded_tags.map(&:name),
+ limit: nil,
+ ignore_required_tag_groups: true,
+ ),
+ )
+ .map(&:name)
+ .to_set
+ else
+ Set.new
+ end
- disabled =
+ disabled_tags =
excluded_tags.map do |tag|
- reason = explain_exclusion(tag, params, selected_ids, guardian)
- { id: tag.id, name: tag.name, text: tag.name, count: 0, disabled: true, title: reason }
+ title =
+ if allowed_without_required_tag_groups.include?(tag.name)
+ I18n.t(
+ "tags.forbidden.required_tag_group",
+ count: remaining_required_tag_count,
+ tag_group_name: required_tag_group[:name],
+ )
+ else
+ explain_exclusion(tag, params:, selected_tag_ids:, guardian:)
+ end
+ { id: tag.id, name: tag.name, text: tag.name, count: 0, disabled: true, title: }
end
- context[:tags] = tags.concat(disabled) if disabled.present?
+ tags.concat(disabled_tags)
end
def detect_forbidden_tag(params:, category:, tags:, guardian:)
@@ -165,124 +201,142 @@ class Tags::Search
tag = visible_tags(guardian).where_name(params.term).first
return unless tag
- return if reject_allowed_tags([tag], params:, category:, tags:, guardian:).empty?
+ return if tags_excluded_by_filter([tag], params:, category:, tags:, guardian:).empty?
context[:forbidden] = params.q
context[:forbidden_message] = explain_exclusion(
tag,
- params,
- params.resolved_selected_tag_ids,
- guardian,
+ params:,
+ selected_tag_ids: params.resolved_selected_tag_ids,
+ guardian:,
)
end
- def reject_allowed_tags(candidates, params:, category:, tags:, guardian:)
- return candidates unless params.capped_limit && tags.size >= params.capped_limit
+ def tags_excluded_by_filter(candidates, params:, category:, tags:, guardian:)
+ limit = params.capped_limit
+ return candidates unless limit && tags.size >= limit
- only_tag_names = candidates.map(&:name)
-
- allowed_names =
+ allowed_tag_names =
DiscourseTagging
.filter_allowed_tags(
guardian,
- **params.filter_options.merge(category:, only_tag_names:, limit: nil),
+ **params.filter_options.merge(
+ category:,
+ only_tag_names: candidates.map(&:name),
+ limit: nil,
+ ),
)
.map(&:name)
.to_set
- candidates.reject { |tag| allowed_names.include?(tag.name) }
+ candidates.reject { |tag| allowed_tag_names.include?(tag.name) }
end
- def explain_exclusion(tag, params, selected_ids, guardian)
- if params.excludeSynonyms && tag.synonym? && tag_visible?(tag.target_tag_id, guardian)
- return I18n.t("tags.forbidden.synonym", tag_name: tag.target_tag.name)
- end
+ def explain_exclusion(tag, params:, selected_tag_ids:, guardian:)
+ synonym_exclusion_reason(tag, params:, guardian:) ||
+ has_synonyms_exclusion_reason(tag, params:) ||
+ one_per_topic_group_exclusion_reason(tag, selected_tag_ids:, guardian:) ||
+ missing_parent_tag_exclusion_reason(tag, params:, selected_tag_ids:, guardian:) ||
+ category_restriction_exclusion_reason(tag, params:, guardian:)
+ end
- if params.excludeHasSynonyms && tag.synonyms.exists?
- return I18n.t("tags.forbidden.has_synonyms", tag_name: tag.name)
+ def synonym_exclusion_reason(tag, params:, guardian:)
+ unless params.excludeSynonyms && tag.synonym? && tag_visible?(tag.target_tag_id, guardian)
+ return
end
- if selected_ids.present?
- group =
- TagGroup
- .joins(:tag_group_memberships)
- .where(one_per_topic: true, tag_group_memberships: { tag_id: tag.id })
- .where(id: TagGroupMembership.where(tag_id: selected_ids).select(:tag_group_id))
- .first
-
- if group
- conflicting_tag_names =
- visible_tags(guardian)
- .where(id: selected_ids)
- .joins(:tag_group_memberships)
- .where(tag_group_memberships: { tag_group_id: group.id })
- .order(:name)
- .pluck(:name)
-
- if conflicting_tag_names.present?
- return(
- I18n.t(
- "tags.forbidden.one_tag_per_topic_group",
- tag_group_name: group.name,
- tag_names: conflicting_tag_names.join(", "),
- )
- )
- end
+ I18n.t("tags.forbidden.synonym", tag_name: tag.target_tag.name)
+ end
- return I18n.t("tags.forbidden.one_tag_per_topic_group_without_names")
- end
- end
+ def has_synonyms_exclusion_reason(tag, params:)
+ return unless params.excludeHasSynonyms && tag.synonyms.exists?
- if params.filterForInput
- group =
- TagGroup
- .joins(:tag_group_memberships)
- .where(tag_group_memberships: { tag_id: tag.id })
- .where.not(parent_tag_id: [nil, *selected_ids])
- .includes(:parent_tag)
- .first
-
- if group&.parent_tag && tag_visible?(group.parent_tag_id, guardian)
- return(
- I18n.t(
- "tags.forbidden.missing_parent_tag",
- parent_tag_name: group.parent_tag.name,
- tag_group_name: group.name,
- )
- )
- end
- end
+ I18n.t("tags.forbidden.has_synonyms", tag_name: tag.name)
+ end
+
+ def one_per_topic_group_exclusion_reason(tag, selected_tag_ids:, guardian:)
+ return if selected_tag_ids.blank?
- category_names = tag.categories.where(id: guardian.allowed_category_ids).pluck(:name)
- category_names +=
- Category
- .joins(tag_groups: :tags)
- .where(id: guardian.allowed_category_ids, "tags.id": tag.id)
+ group =
+ TagGroup
+ .joins(:tag_group_memberships)
+ .where(one_per_topic: true, tag_group_memberships: { tag_id: tag.id })
+ .where(id: TagGroupMembership.where(tag_id: selected_tag_ids).select(:tag_group_id))
+ .first
+ return unless group
+
+ conflicting_tag_names =
+ visible_tags(guardian)
+ .where(id: selected_tag_ids)
+ .joins(:tag_group_memberships)
+ .where(tag_group_memberships: { tag_group_id: group.id })
+ .order(:name)
.pluck(:name)
- if category_names.present?
- category_names.uniq!
- category_names.sort!
+ if conflicting_tag_names.blank?
+ return I18n.t("tags.forbidden.one_tag_per_topic_group_without_names")
+ end
+
+ I18n.t(
+ "tags.forbidden.one_tag_per_topic_group",
+ tag_group_name: group.name,
+ tag_names: conflicting_tag_names.join(", "),
+ )
+ end
+
+ def missing_parent_tag_exclusion_reason(tag, params:, selected_tag_ids:, guardian:)
+ return unless params.filterForInput
+
+ group =
+ TagGroup
+ .joins(:tag_group_memberships)
+ .where(tag_group_memberships: { tag_id: tag.id })
+ .where.not(parent_tag_id: [nil, *selected_tag_ids])
+ .includes(:parent_tag)
+ .first
+ return unless group&.parent_tag && tag_visible?(group.parent_tag_id, guardian)
+
+ I18n.t(
+ "tags.forbidden.missing_parent_tag",
+ parent_tag_name: group.parent_tag.name,
+ tag_group_name: group.name,
+ )
+ end
+
+ def category_restriction_exclusion_reason(tag, params:, guardian:)
+ allowed_category_ids = guardian.allowed_category_ids
+ category_names =
+ tag.categories.where(id: allowed_category_ids).pluck(:name) +
+ Category
+ .joins(tag_groups: :tags)
+ .where(id: allowed_category_ids, "tags.id": tag.id)
+ .pluck(:name)
+ category_names = category_names.uniq.sort
+
+ if category_names.empty?
+ if params.categoryId.present?
+ return I18n.t("tags.forbidden.in_this_category", tag_name: tag.name)
+ end
+
+ return I18n.t("tags.forbidden.not_allowed", tag_name: tag.name)
+ end
- if category_names.size > 3
+ if category_names.size > 3
+ return(
I18n.t(
"tags.forbidden.restricted_to_truncated",
tag_name: tag.name,
category_names: category_names.first(3).join(", "),
more_count: category_names.size - 3,
)
- else
- I18n.t(
- "tags.forbidden.restricted_to",
- count: category_names.count,
- tag_name: tag.name,
- category_names: category_names.join(", "),
- )
- end
- elsif params.categoryId.present?
- I18n.t("tags.forbidden.in_this_category", tag_name: tag.name)
- else
- I18n.t("tags.forbidden.not_allowed", tag_name: tag.name)
+ )
end
+
+ I18n.t(
+ "tags.forbidden.restricted_to",
+ count: category_names.size,
+ tag_name: tag.name,
+ category_names: category_names.join(", "),
+ )
end
end
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 3b9627af2fd..a68e83f5d88 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -6201,6 +6201,9 @@ en:
one: 'Only available in the "%{category_names}" category'
other: "Only available in: %{category_names}"
restricted_to_truncated: "Only available in: %{category_names}, and %{more_count} more"
+ required_tag_group:
+ one: 'Select %{count} more tag from the "%{tag_group_name}" tag group before using this tag'
+ other: 'Select %{count} more tags from the "%{tag_group_name}" tag group before using this tag'
synonym: 'Please use "%{tag_name}" instead'
has_synonyms: '"%{tag_name}" cannot be used because it has synonyms.'
one_tag_per_topic_group: 'Conflicts with "%{tag_names}" (only one allowed per topic)'
diff --git a/lib/discourse_tagging.rb b/lib/discourse_tagging.rb
index 1c5ca1ee7ff..10d214d64f2 100644
--- a/lib/discourse_tagging.rb
+++ b/lib/discourse_tagging.rb
@@ -508,6 +508,7 @@ module DiscourseTagging
# selected_tag_ids: an array of tag ids that are in the current selection
# only_tag_names: limit results to tags with these names
# exclude_synonyms: exclude synonyms from results
+ # ignore_required_tag_groups: skip category-required tag groups when filtering for an input
# order_search_results: result should be ordered for name search results
# order_popularity: order result by topic_count
# order_recent_tag_ids: ordered tag ids (most recent first) to prioritize at the top of the results
@@ -641,14 +642,18 @@ module DiscourseTagging
# - and no search term has been included
required_tag_ids = nil
required_category_tag_group = nil
- if opts[:for_input] && category&.category_required_tag_groups.present? &&
- (filter_for_non_admin || term.blank?)
- category.category_required_tag_groups.each do |crtg|
+ remaining_required_tag_count = nil
+ category_required_tag_groups = category&.category_required_tag_groups
+ if !opts[:ignore_required_tag_groups] && opts[:for_input] &&
+ category_required_tag_groups.present? && (filter_for_non_admin || term.blank?)
+ category_required_tag_groups.each do |crtg|
group_tags = crtg.tag_group.tags.pluck(:id)
- next if (group_tags & selected_tag_ids).size >= crtg.min_count
+ selected_tag_count = (group_tags & selected_tag_ids).size
+ next if selected_tag_count >= crtg.min_count
if filter_for_non_admin || group_tags.size >= opts[:limit].to_i
required_category_tag_group = crtg
required_tag_ids = group_tags
+ remaining_required_tag_count = crtg.min_count - selected_tag_count
builder.where("id IN (?)", required_tag_ids)
end
break
@@ -722,6 +727,7 @@ module DiscourseTagging
name: required_category_tag_group.tag_group.name,
min_count: required_category_tag_group.min_count,
}
+ context[:remaining_required_tag_count] = remaining_required_tag_count
end
[result, context]
else
diff --git a/spec/services/tags/search_spec.rb b/spec/services/tags/search_spec.rb
index ff071b8e023..e18941387bf 100644
--- a/spec/services/tags/search_spec.rb
+++ b/spec/services/tags/search_spec.rb
@@ -2,31 +2,18 @@
RSpec.describe(Tags::Search) do
describe described_class::Contract, type: :model do
- it "is valid with no limit" do
- expect(described_class.new(limit: nil)).to be_valid
- end
-
- it "is valid with a positive limit" do
- expect(described_class.new(limit: 3)).to be_valid
- end
-
- it "rejects a negative limit" do
- contract = described_class.new(limit: -1)
- expect(contract.valid?).to be false
- expect(contract.errors[:limit]).to be_present
- end
-
- it "rejects a non-numeric limit" do
- contract = described_class.new
- contract.assign_attributes(limit: "abc")
- expect(contract.valid?).to be false
- expect(contract.errors[:limit]).to be_present
+ it "accepts a blank or in-range limit" do
+ [nil, 0, 3, SiteSetting.max_tag_search_results].each do |limit|
+ expect(described_class.new(limit:)).to be_valid
+ end
end
- it "rejects a limit exceeding max_tag_search_results" do
- contract = described_class.new(limit: SiteSetting.max_tag_search_results + 1)
- expect(contract.valid?).to be false
- expect(contract.errors[:limit]).to be_present
+ it "rejects malformed or out-of-range limits" do
+ [-1, "abc", SiteSetting.max_tag_search_results + 1].each do |limit|
+ contract = described_class.new(limit:)
+ expect(contract).to be_invalid
+ expect(contract.errors[:limit]).to be_present
+ end
end
end
@@ -49,21 +36,10 @@ RSpec.describe(Tags::Search) do
end
context "when everything's ok" do
- it { is_expected.to run_successfully }
-
- it "returns matching tags" do
- expect(result[:tags].map { |t| t[:name] }).to include("alpha")
- end
-
- it "does not return non-matching tags" do
- expect(result[:tags].map { |t| t[:name] }).not_to include("beta")
- end
-
- it "sets forbidden to nil" do
+ it "returns matching tags without a forbidden result" do
+ expect(result).to run_successfully
+ expect(result[:tags].map { |tag| tag[:name] }).to contain_exactly("alpha")
expect(result[:forbidden]).to be_nil
- end
-
- it "sets forbidden_message to nil" do
expect(result[:forbidden_message]).to be_nil
end
end
@@ -71,9 +47,8 @@ RSpec.describe(Tags::Search) do
context "with blank query" do
let(:params) { {} }
- it { is_expected.to run_successfully }
-
- it "returns tags ordered by popularity" do
+ it "returns tags for the blank query" do
+ expect(result).to run_successfully
expect(result[:tags]).to be_present
end
end
@@ -100,16 +75,13 @@ RSpec.describe(Tags::Search) do
expect(names.index("recentb")).to be < names.index("popular")
end
- it "falls back to popularity ordering when the upcoming change is disabled" do
+ it "uses popularity ordering when recent-tag prioritization is unavailable" do
SiteSetting.prioritize_recently_used_tags = false
Fabricate(:topic, user: user, tags: [recent_tag_a])
expect(tag_names(params).first).to eq("popular")
- end
-
- it "falls back to popularity ordering for anonymous users" do
- Fabricate(:topic, user: user, tags: [recent_tag_a])
+ SiteSetting.prioritize_recently_used_tags = true
expect(tag_names(params, guardian: Guardian.new).first).to eq("popular")
end
@@ -126,13 +98,19 @@ RSpec.describe(Tags::Search) do
let(:params) { { q: "alpha", categoryId: category.id } }
- it { is_expected.to run_successfully }
+ it "returns tags filtered for the category" do
+ expect(result).to run_successfully
+ expect(result[:tags].map { |tag| tag[:name] }).to contain_exactly("alpha")
+ end
end
context "with a non-existent category" do
let(:params) { { q: "alpha", categoryId: -999 } }
- it { is_expected.to run_successfully }
+ it "treats a non-existent category as no category filter" do
+ expect(result).to run_successfully
+ expect(result[:tags].map { |tag| tag[:name] }).to contain_exactly("alpha")
+ end
end
context "with filterForInput returning disabled tags for one_per_topic groups" do
@@ -143,17 +121,17 @@ RSpec.describe(Tags::Search) do
let(:params) { { q: "gamma", filterForInput: true, selected_tags: [tag1.name] } }
- it { is_expected.to run_successfully }
-
- it "marks the excluded tag as disabled" do
- disabled = result[:tags].select { |t| t[:disabled] }
- expect(disabled.map { |t| t[:name] }).to include("gamma")
- end
-
- it "names the conflicting tag in the one_per_topic reason" do
- disabled = result[:tags].find { |t| t[:name] == "gamma" && t[:disabled] }
- expect(disabled[:title]).to eq(
- I18n.t("tags.forbidden.one_tag_per_topic_group", tag_names: "alpha"),
+ it "returns the excluded tag with its conflicting tag in the reason" do
+ expect(result).to run_successfully
+
+ expect(result[:tags].find { |tag| tag[:name] == "gamma" }).to include(
+ disabled: true,
+ title:
+ I18n.t(
+ "tags.forbidden.one_tag_per_topic_group",
+ tag_group_name: tag_group.name,
+ tag_names: tag1.name,
+ ),
)
end
end
@@ -180,10 +158,9 @@ RSpec.describe(Tags::Search) do
it "uses a generic one_per_topic reason instead of leaking the hidden selected tag name" do
disabled = result[:tags].find { |tag| tag[:name] == "public-sibling" && tag[:disabled] }
- expect(disabled).to be_present
- expect(disabled[:title]).not_to include(hidden_selected_tag.name)
- expect(disabled[:title]).to eq(
- I18n.t("tags.forbidden.one_tag_per_topic_group_without_names"),
+ expect(disabled).to include(
+ disabled: true,
+ title: I18n.t("tags.forbidden.one_tag_per_topic_group_without_names"),
)
end
end
@@ -197,22 +174,25 @@ RSpec.describe(Tags::Search) do
let(:params) { { q: "childtag", filterForInput: true } }
- it { is_expected.to run_successfully }
-
- it "marks the child tag as disabled" do
- disabled = result[:tags].select { |t| t[:disabled] }
- expect(disabled.map { |t| t[:name] }).to include("childtag")
- end
-
- it "includes the missing parent tag reason" do
- disabled = result[:tags].find { |t| t[:name] == "childtag" && t[:disabled] }
- expect(disabled[:title]).to include("parent")
+ it "disables the child tag with a missing-parent reason" do
+ expect(result).to run_successfully
+ child = result[:tags].find { |tag| tag[:name] == child_tag.name }
+
+ expect(child).to include(
+ disabled: true,
+ title:
+ I18n.t(
+ "tags.forbidden.missing_parent_tag",
+ parent_tag_name: parent_tag.name,
+ tag_group_name: tag_group.name,
+ ),
+ )
end
it "does not surface the tag when the term only matches mid-word" do
result =
described_class.call(params: { q: "hildtag", filterForInput: true }, **dependencies)
- expect(result[:tags].map { |t| t[:name] }).not_to include("childtag")
+ expect(result[:tags].map { |tag| tag[:name] }).not_to include("childtag")
end
end
@@ -224,16 +204,50 @@ RSpec.describe(Tags::Search) do
let(:params) { { q: "restricted", filterForInput: true } }
- it { is_expected.to run_successfully }
+ it "disables the tag with the category restriction reason" do
+ expect(result).to run_successfully
+ expect(result[:tags].find { |tag| tag[:name] == restricted_tag.name }).to include(
+ disabled: true,
+ title:
+ I18n.t(
+ "tags.forbidden.restricted_to",
+ count: 1,
+ tag_name: restricted_tag.name,
+ category_names: category.name,
+ ),
+ )
+ end
+ end
- it "marks the category-restricted tag as disabled" do
- disabled = result[:tags].select { |t| t[:disabled] }
- expect(disabled.map { |t| t[:name] }).to include("restricted")
+ context "with a tag restricted to more than three accessible categories" do
+ fab!(:category_a) { Fabricate(:category, name: "Category A") }
+ fab!(:category_b) { Fabricate(:category, name: "Category B") }
+ fab!(:category_c) { Fabricate(:category, name: "Category C") }
+ fab!(:category_d) { Fabricate(:category, name: "Category D") }
+ fab!(:restricted_tag) { Fabricate(:tag, name: "many-categories") }
+
+ before do
+ [category_a, category_b, category_c, category_d].each do |category|
+ CategoryTag.create!(category:, tag: restricted_tag)
+ end
end
- it "includes the category name in the reason" do
- disabled = result[:tags].find { |t| t[:name] == "restricted" && t[:disabled] }
- expect(disabled[:title]).to include(category.name)
+ let(:params) { { q: restricted_tag.name, filterForInput: true } }
+
+ it "lists three category names and the number of additional categories" do
+ visible_category_names = [category_a, category_b, category_c, category_d].map(&:name).sort
+ expected_reason =
+ I18n.t(
+ "tags.forbidden.restricted_to_truncated",
+ tag_name: restricted_tag.name,
+ category_names: visible_category_names.first(3).join(", "),
+ more_count: 1,
+ )
+
+ expect(result[:tags].find { |tag| tag[:name] == restricted_tag.name }).to include(
+ disabled: true,
+ title: expected_reason,
+ )
end
end
@@ -246,31 +260,27 @@ RSpec.describe(Tags::Search) do
let(:params) { { q: "bots", filterForInput: true } }
- it "does not leak the tag name to unauthorized users" do
- names = result[:tags].map { |t| t[:name] }
- expect(names).not_to include("bots-gone-mad")
+ it "hides the tag from users without access" do
+ expect(result[:tags].map { |tag| tag[:name] }).not_to include(secret_tag.name)
end
- it "still shows the tag to admins" do
+ it "shows the tag to admins" do
admin = Fabricate(:admin)
- admin_result =
- described_class.call(params:, **dependencies.merge(guardian: Guardian.new(admin)))
- expect(admin_result[:tags].map { |t| t[:name] }).to include("bots-gone-mad")
+ admin_result = described_class.call(params:, guardian: Guardian.new(admin))
+ expect(admin_result[:tags].map { |tag| tag[:name] }).to include(secret_tag.name)
end
- it "still shows the tag to users who can access the category" do
+ it "shows the tag to users who can access the category" do
staff = Fabricate(:user)
staff_group.add(staff)
- staff_result =
- described_class.call(params:, **dependencies.merge(guardian: Guardian.new(staff)))
- expect(staff_result[:tags].map { |t| t[:name] }).to include("bots-gone-mad")
+ staff_result = described_class.call(params:, guardian: Guardian.new(staff))
+ expect(staff_result[:tags].map { |tag| tag[:name] }).to include(secret_tag.name)
end
- it "still shows the tag when it is also attached to a category the user can access" do
+ it "shows the tag when it is also attached to a category the user can access" do
public_category = Fabricate(:category)
CategoryTag.create!(category: public_category, tag: secret_tag)
- names = result[:tags].map { |t| t[:name] }
- expect(names).to include("bots-gone-mad")
+ expect(result[:tags].map { |tag| tag[:name] }).to include(secret_tag.name)
end
end
@@ -284,9 +294,8 @@ RSpec.describe(Tags::Search) do
let(:params) { { q: "insider", filterForInput: true } }
- it "does not leak the tag name to unauthorized users" do
- names = result[:tags].map { |t| t[:name] }
- expect(names).not_to include("insider-info")
+ it "does not return the tag restricted by its group" do
+ expect(result[:tags].map { |tag| tag[:name] }).not_to include(secret_tag.name)
end
end
@@ -299,8 +308,8 @@ RSpec.describe(Tags::Search) do
let(:params) { { q: "bots" } }
- it "does not leak the tag name as an allowed result" do
- expect(result[:tags].map { |t| t[:name] }).not_to include("bots-gone-mad")
+ it "does not return the tag as an allowed result" do
+ expect(result[:tags].map { |tag| tag[:name] }).not_to include(secret_tag.name)
end
end
@@ -314,10 +323,12 @@ RSpec.describe(Tags::Search) do
let(:params) { { q: "alpha-global", categoryId: private_category.id } }
- it "behaves as if the category did not exist (no enumeration vector)" do
- blind =
- described_class.call(params: { q: "alpha-global", categoryId: -999 }, **dependencies)
- expect(result[:tags].map { |t| t[:name] }).to eq(blind[:tags].map { |t| t[:name] })
+ it "behaves as if the category did not exist" do
+ expect(result).to run_successfully
+ missing_category_result =
+ described_class.call(params: { q: global_tag.name, categoryId: -999 }, **dependencies)
+
+ expect(result[:tags]).to eq(missing_category_result[:tags])
end
end
@@ -327,10 +338,11 @@ RSpec.describe(Tags::Search) do
let(:params) { { q: "maintag2", filterForInput: true, excludeHasSynonyms: true } }
- it "marks the tag as disabled with a synonyms reason" do
- disabled = result[:tags].find { |t| t[:name] == "maintag2" && t[:disabled] }
- expect(disabled).to be_present
- expect(disabled[:title]).to include("synonyms")
+ it "disables the tag with a reason" do
+ expect(result[:tags].find { |tag| tag[:name] == target_with_syn.name }).to include(
+ disabled: true,
+ title: I18n.t("tags.forbidden.has_synonyms", tag_name: target_with_syn.name),
+ )
end
end
@@ -344,33 +356,17 @@ RSpec.describe(Tags::Search) do
let(:dependencies) { { guardian: Guardian.new } }
let(:params) { { q: "anon-secret", filterForInput: true } }
- it "does not leak tags restricted to inaccessible categories" do
- names = result[:tags].map { |t| t[:name] }
- expect(names).not_to include("anon-secret")
- end
- end
-
- context "with an admin guardian" do
- fab!(:staff_group) { Group[:staff] }
- fab!(:private_category) { Fabricate(:private_category, group: staff_group) }
- fab!(:secret_tag) { Fabricate(:tag, name: "admin-visible") }
-
- before { CategoryTag.create!(category: private_category, tag: secret_tag) }
-
- let(:dependencies) { { guardian: Guardian.new(Fabricate(:admin)) } }
- let(:params) { { q: "admin-visible" } }
-
- it "sees tags restricted to any category" do
- expect(result[:tags].map { |t| t[:name] }).to include("admin-visible")
+ it "does not return tags restricted to inaccessible categories" do
+ expect(result[:tags].map { |tag| tag[:name] }).not_to include(secret_tag.name)
end
end
context "with a global tag disabled inside a category that disallows globals" do
fab!(:strict_category) do
- Fabricate(:category).tap do |c|
- c.update!(allow_global_tags: false)
+ Fabricate(:category).tap do |category_record|
+ category_record.update!(allow_global_tags: false)
Fabricate(:tag_group, tags: [Fabricate(:tag, name: "strict-only")]).tap do |tg|
- CategoryTagGroup.create!(category: c, tag_group: tg)
+ CategoryTagGroup.create!(category: category_record, tag_group: tg)
end
end
end
@@ -378,10 +374,11 @@ RSpec.describe(Tags::Search) do
let(:params) { { q: "truly-global", filterForInput: true, categoryId: strict_category.id } }
- it "uses the 'in this category' fallback wording" do
- disabled = result[:tags].find { |t| t[:name] == "truly-global" && t[:disabled] }
- expect(disabled).to be_present
- expect(disabled[:title]).to include("this category")
+ it "explains that the global tag is unavailable in this category" do
+ expect(result[:tags].find { |tag| tag[:name] == global_tag.name }).to include(
+ disabled: true,
+ title: I18n.t("tags.forbidden.in_this_category", tag_name: global_tag.name),
+ )
end
end
@@ -395,10 +392,10 @@ RSpec.describe(Tags::Search) do
let(:params) { { q: "payload-syn" } }
- it "does not leak the target tag name in the serialized payload" do
- entry = result[:tags].find { |t| t[:name] == "payload-syn" }
- expect(entry).to be_present
- expect(entry[:target_tag]).to be_nil
+ it "omits the inaccessible target from the serialized payload" do
+ expect(result[:tags].find { |tag| tag[:name] == public_synonym.name }).to include(
+ target_tag: nil,
+ )
end
end
@@ -412,10 +409,10 @@ RSpec.describe(Tags::Search) do
let(:params) { { q: "public-syn", filterForInput: true, excludeSynonyms: true } }
- it "does not leak the target tag name in the disabled reason" do
- disabled = result[:tags].find { |t| t[:name] == "public-syn" && t[:disabled] }
- expect(disabled).to be_present
- expect(disabled[:title]).not_to include("secret-target")
+ it "does not expose the target in the disabled reason" do
+ disabled = result[:tags].find { |tag| tag[:name] == public_synonym.name }
+ expect(disabled).to include(disabled: true)
+ expect(disabled[:title]).not_to include(secret_target.name)
end
end
@@ -432,10 +429,11 @@ RSpec.describe(Tags::Search) do
let(:params) { { q: "orphan-tag", filterForInput: true } }
- it "does not mention 'this category' when there is no category context" do
- disabled = result[:tags].find { |t| t[:name] == "orphan-tag" && t[:disabled] }
- expect(disabled).to be_present
- expect(disabled[:title]).not_to include("this category")
+ it "uses the general restriction wording without category context" do
+ expect(result[:tags].find { |tag| tag[:name] == orphan_tag.name }).to include(
+ disabled: true,
+ title: I18n.t("tags.forbidden.not_allowed", tag_name: orphan_tag.name),
+ )
end
end
@@ -452,10 +450,10 @@ RSpec.describe(Tags::Search) do
let(:params) { { q: "public-child", filterForInput: true } }
- it "does not leak the parent tag name in the disabled reason" do
- disabled = result[:tags].find { |t| t[:name] == "public-child" && t[:disabled] }
- expect(disabled).to be_present
- expect(disabled[:title]).not_to include("secret-parent")
+ it "does not expose the parent in the disabled reason" do
+ disabled = result[:tags].find { |tag| tag[:name] == child_tag.name }
+ expect(disabled).to include(disabled: true)
+ expect(disabled[:title]).not_to include(secret_parent.name)
end
end
@@ -468,7 +466,8 @@ RSpec.describe(Tags::Search) do
let(:params) { { q: "bots-gone-mad", filterForInput: true } }
- it "does not confirm the tag exists via forbidden_message" do
+ it "does not confirm the tag exists in results or the forbidden fields" do
+ expect(result[:tags].map { |tag| tag[:name] }).not_to include(secret_tag.name)
expect(result[:forbidden]).to be_nil
expect(result[:forbidden_message]).to be_nil
end
@@ -488,9 +487,7 @@ RSpec.describe(Tags::Search) do
let(:params) { { q: "alpha", filterForInput: true, selected_tags: [tag2.name] } }
it "lists allowed tags before disabled tags" do
- names = result[:tags].map { |t| t[:name] }
- expect(names).to include("alpha", "alphablocked")
- expect(names.index("alpha")).to be < names.index("alphablocked")
+ expect(result[:tags].map { |tag| tag[:name] }).to eq(%w[alpha alphablocked])
end
end
@@ -501,7 +498,7 @@ RSpec.describe(Tags::Search) do
let(:params) { { q: "foomatch", filterForInput: true, limit: 1 } }
it "does not mislabel allowed tags as disabled" do
- disabled = result[:tags].select { |t| t[:disabled] }
+ disabled = result[:tags].select { |tag| tag[:disabled] }
expect(disabled).to be_empty
end
end
@@ -525,22 +522,12 @@ RSpec.describe(Tags::Search) do
let(:params) { { q: "syntag", excludeSynonyms: true } }
- it { is_expected.to run_successfully }
-
- it "sets forbidden to the search query" do
+ it "returns the forbidden query and its synonym reason" do
+ expect(result).to run_successfully
expect(result[:forbidden]).to eq("syntag")
- end
-
- it "sets forbidden_message explaining the synonym" do
- expect(result[:forbidden_message]).to include("maintag")
- end
- end
-
- context "when the forbidden tag is already in results" do
- let(:params) { { q: "alpha" } }
-
- it "does not set forbidden" do
- expect(result[:forbidden]).to be_nil
+ expect(result[:forbidden_message]).to eq(
+ I18n.t("tags.forbidden.synonym", tag_name: target_tag.name),
+ )
end
end
@@ -550,17 +537,14 @@ RSpec.describe(Tags::Search) do
let(:params) { { q: "pmonly", categoryId: category.id, filterForInput: true, limit: 5 } }
- it "returns the tag as selectable instead of mislabeling it as unusable" do
+ before { SiteSetting.display_personal_messages_tag_counts = true }
+
+ it "keeps the tag selectable without exposing its PM count" do
row = result[:tags].find { |tag| tag[:name] == "pmonly" }
expect(row).to be_present
expect(row[:disabled]).to be_blank
expect(result[:forbidden]).to be_nil
expect(result[:forbidden_message]).to be_nil
- end
-
- it "does not expose the PM tag count to users who cannot tag PMs" do
- SiteSetting.display_personal_messages_tag_counts = true
- row = result[:tags].find { |tag| tag[:name] == "pmonly" }
expect(row).not_to have_key(:pm_count)
end
end
@@ -572,14 +556,11 @@ RSpec.describe(Tags::Search) do
let(:params) { { q: "secrethidden" } }
- it { is_expected.to run_successfully }
-
- it "does not expose the hidden tag as forbidden" do
+ it "does not expose a hidden tag in results or as forbidden" do
+ expect(result).to run_successfully
expect(result[:forbidden]).to be_nil
- end
-
- it "does not include the hidden tag in results" do
- expect(result[:tags].map { |t| t[:name] }).not_to include("secrethidden")
+ expect(result[:forbidden_message]).to be_nil
+ expect(result[:tags].map { |tag| tag[:name] }).not_to include(hidden_tag.name)
end
end
@@ -599,12 +580,89 @@ RSpec.describe(Tags::Search) do
let(:params) { { filterForInput: true, categoryId: required_category.id } }
- it { is_expected.to run_successfully }
-
it "propagates the required_tag_group from the filter context" do
- expect(result[:required_tag_group]).to be_present
- expect(result[:required_tag_group][:name]).to eq("Required Group")
- expect(result[:required_tag_group][:min_count]).to eq(1)
+ expect(result).to run_successfully
+ expect(result[:required_tag_group]).to include(name: required_tag_group.name, min_count: 1)
+ end
+ end
+
+ context "with an unsatisfied required tag group" do
+ fab!(:app_tag) { Fabricate(:tag, name: "app-desktop") }
+ fab!(:hosting_tag1) { Fabricate(:tag, name: "server-default-cloud") }
+ fab!(:hosting_tag2) { Fabricate(:tag, name: "server-other-cloud") }
+ fab!(:app_tag_group) { Fabricate(:tag_group, name: "Apps", tags: [app_tag]) }
+ fab!(:hosting_tag_group) do
+ Fabricate(:tag_group, name: "Hosting", tags: [hosting_tag1, hosting_tag2])
+ end
+ fab!(:required_category, :category)
+ fab!(:otherwise_allowed_tag) { Fabricate(:tag, name: "os-linux") }
+ fab!(:restricted_category) { Fabricate(:category, name: "Other category") }
+ fab!(:restricted_tag) { Fabricate(:tag, name: "other-only") }
+
+ before do
+ CategoryRequiredTagGroup.create!(
+ category: required_category,
+ tag_group: app_tag_group,
+ min_count: 1,
+ order: 1,
+ )
+ CategoryRequiredTagGroup.create!(
+ category: required_category,
+ tag_group: hosting_tag_group,
+ min_count: 2,
+ order: 2,
+ )
+ CategoryTag.create!(category: restricted_category, tag: restricted_tag)
+ end
+
+ let(:params) do
+ {
+ q: "os-linux",
+ filterForInput: true,
+ categoryId: required_category.id,
+ selected_tag_ids: [app_tag.id, hosting_tag1.id],
+ }
+ end
+
+ it "explains how many more required-group tags are needed" do
+ disabled = result[:tags].find { |tag| tag[:name] == otherwise_allowed_tag.name }
+
+ expect(disabled[:disabled]).to be true
+ expect(disabled[:title]).to eq(
+ I18n.t(
+ "tags.forbidden.required_tag_group",
+ count: 1,
+ tag_group_name: hosting_tag_group.name,
+ ),
+ )
+ end
+
+ it "keeps the category restriction reason for a genuinely restricted tag" do
+ restricted_result =
+ described_class.call(params: params.merge(q: restricted_tag.name), **dependencies)
+ disabled = restricted_result[:tags].find { |tag| tag[:name] == restricted_tag.name }
+
+ expect(disabled[:disabled]).to be true
+ expect(disabled[:title]).to eq(
+ I18n.t(
+ "tags.forbidden.restricted_to",
+ count: 1,
+ tag_name: restricted_tag.name,
+ category_names: restricted_category.name,
+ ),
+ )
+ end
+
+ it "allows other tags once the required group is satisfied" do
+ satisfied_result =
+ described_class.call(
+ params: params.merge(selected_tag_ids: [app_tag.id, hosting_tag1.id, hosting_tag2.id]),
+ **dependencies,
+ )
+ selectable = satisfied_result[:tags].find { |tag| tag[:name] == otherwise_allowed_tag.name }
+
+ expect(selectable).to be_present
+ expect(selectable[:disabled]).to be_blank
end
end
@@ -623,12 +681,12 @@ RSpec.describe(Tags::Search) do
it "matches tags by their localized name in the current locale" do
I18n.with_locale(:ja) do
- expect(result[:tags].map { |t| t[:name] }).to contain_exactly("戦略")
+ expect(result[:tags].map { |tag| tag[:name] }).to contain_exactly("戦略")
end
end
it "does not match localizations from other locales" do
- I18n.with_locale(:en) { expect(result[:tags].map { |t| t[:name] }).to be_empty }
+ I18n.with_locale(:en) { expect(result[:tags].map { |tag| tag[:name] }).to be_empty }
end
end
end