Skip to content

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

UX: Show disabled tags with explanations in composer tag search (#39072)

Régis HANOLabb8e4c162bb
diff --git a/app/assets/stylesheets/common/select-kit/tag-chooser.scss b/app/assets/stylesheets/common/select-kit/tag-chooser.scss
index 409fd3406ff..6f327062992 100644
--- a/app/assets/stylesheets/common/select-kit/tag-chooser.scss
+++ b/app/assets/stylesheets/common/select-kit/tag-chooser.scss
@@ -9,6 +9,22 @@
.discourse-tag-count {
margin-left: 5px;
}
+
+ &.disabled {
+ opacity: 1;
+ flex-wrap: wrap;
+
+ .discourse-tag {
+ opacity: 0.5;
+ }
+
+ .disabled-reason {
+ width: 100%;
+ font-size: var(--font-down-1);
+ color: var(--primary-medium);
+ font-style: italic;
+ }
+ }
}
}
}
diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb
index f6a672c7e9f..cf8f0554f7c 100644
--- a/app/controllers/tags_controller.rb
+++ b/app/controllers/tags_controller.rb
@@ -405,82 +405,19 @@ class TagsController < ::ApplicationController
)
end
- filter_params = {
- for_input: params[:filterForInput],
- selected_tags: params[:selected_tags],
- selected_tag_ids: params[:selected_tag_ids],
- exclude_synonyms: params[:excludeSynonyms],
- exclude_has_synonyms: params[:excludeHasSynonyms],
- }
-
- if limit = fetch_limit_from_params(default: nil, max: SiteSetting.max_tag_search_results)
- filter_params[:limit] = limit
- end
-
- filter_params[:category] = Category.find_by_id(params[:categoryId]) if params[:categoryId]
-
- if params[:q].present?
- clean_name = DiscourseTagging.clean_tag(params[:q])
- filter_params[:term] = clean_name
- filter_params[:order_search_results] = true
- else
- filter_params[:order_popularity] = true
- end
-
- tags_with_counts, filter_result_context =
- DiscourseTagging.filter_allowed_tags(guardian, **filter_params, with_context: true)
-
- tags_with_counts = Tag.with_localizations(tags_with_counts)
-
- tags = self.class.tag_counts_json(tags_with_counts, guardian)
-
- json_response = { results: tags }
-
- if clean_name && !tags.find { |h| h[:name].downcase == clean_name.downcase } &&
- tag = Tag.where_name(clean_name).first
- # filter_allowed_tags determined that the tag entered is not allowed
- json_response[:forbidden] = params[:q]
-
- if filter_params[:exclude_synonyms] && tag.synonym?
- json_response[:forbidden_message] = I18n.t(
- "tags.forbidden.synonym",
- tag_name: tag.target_tag.name,
- )
- elsif filter_params[:exclude_has_synonyms] && tag.synonyms.exists?
- json_response[:forbidden_message] = I18n.t(
- "tags.forbidden.has_synonyms",
- tag_name: tag.name,
- )
- else
- 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)
- .pluck(:name)
-
- if category_names.present?
- category_names.uniq!
- json_response[:forbidden_message] = I18n.t(
- "tags.forbidden.restricted_to",
- count: category_names.count,
- tag_name: tag.name,
- category_names: category_names.join(", "),
- )
- else
- json_response[:forbidden_message] = I18n.t(
- "tags.forbidden.in_this_category",
- tag_name: tag.name,
- )
- end
+ Tags::Search.call(service_params) do
+ on_success do |tags:, forbidden:, forbidden_message:, required_tag_group:|
+ json_response = { results: tags }
+ json_response[:forbidden] = forbidden if forbidden
+ json_response[:forbidden_message] = forbidden_message if forbidden_message
+ json_response[:required_tag_group] = required_tag_group if required_tag_group
+ render json: json_response
end
+ on_failed_contract do |contract|
+ render json: failed_json.merge(errors: contract.errors.full_messages), status: :bad_request
+ end
+ on_failure { render json: failed_json, status: :unprocessable_entity }
end
-
- if required_tag_group = filter_result_context[:required_tag_group]
- json_response[:required_tag_group] = required_tag_group
- end
-
- render json: json_response
end
def notifications
diff --git a/app/services/tags/search.rb b/app/services/tags/search.rb
new file mode 100644
index 00000000000..33688bda826
--- /dev/null
+++ b/app/services/tags/search.rb
@@ -0,0 +1,211 @@
+# frozen_string_literal: true
+
+class Tags::Search
+ include Service::Base
+
+ # @!method self.call(guardian:, params:)
+ # @param [Guardian] guardian
+ # @param [Hash] params
+ # @option params [String] :q Search query
+ # @option params [Integer] :limit Max results
+ # @option params [Integer] :categoryId Category to scope search to
+ # @option params [Array<Integer>] :selected_tag_ids Currently selected tag IDs
+ # @option params [Array<String>] :selected_tags Currently selected tag names (deprecated)
+ # @option params [Boolean] :filterForInput Whether filtering for tag input
+ # @option params [Boolean] :excludeSynonyms Exclude synonym tags
+ # @option params [Boolean] :excludeHasSynonyms Exclude tags that have synonyms
+ # @return [Service::Base::Context]
+
+ params do
+ attribute :q, :string
+ attribute :limit, :integer
+ attribute :categoryId, :integer
+ attribute :selected_tag_ids, :array
+ attribute :selected_tags, :array
+ attribute :filterForInput, :boolean
+ attribute :excludeSynonyms, :boolean
+ attribute :excludeHasSynonyms, :boolean
+
+ validate :limit_is_valid
+
+ def limit_is_valid
+ raw = raw_attributes["limit"]
+ return if raw.blank?
+
+ unless raw.to_s.match?(/\A\d+\z/)
+ errors.add(:limit, :invalid)
+ return
+ end
+
+ value = raw.to_i
+ errors.add(:limit, :invalid) if value < 0 || value > SiteSetting.max_tag_search_results
+ end
+
+ def term
+ q.present? ? DiscourseTagging.clean_tag(q) : nil
+ end
+
+ def capped_limit
+ limit.present? ? [limit, SiteSetting.max_tag_search_results].min : nil
+ end
+
+ def filter_options
+ opts = {
+ for_input: filterForInput,
+ selected_tags: selected_tags,
+ selected_tag_ids: selected_tag_ids,
+ exclude_synonyms: excludeSynonyms,
+ exclude_has_synonyms: excludeHasSynonyms,
+ }
+
+ opts[:limit] = capped_limit if capped_limit
+
+ if term.present?
+ opts[:term] = term
+ opts[:order_search_results] = true
+ else
+ opts[:order_popularity] = true
+ end
+
+ opts
+ end
+
+ def resolved_selected_tag_ids
+ if selected_tag_ids.present?
+ selected_tag_ids.map(&:to_i)
+ elsif selected_tags.present?
+ Tag.where_name(selected_tags).pluck(:id)
+ else
+ []
+ end
+ end
+ end
+
+ model :category, optional: true
+ step :search_tags
+ only_if(:has_term_for_input) { step :append_disabled_tags }
+ only_if(:has_term) { step :detect_forbidden_tag }
+
+ private
+
+ def fetch_category(params:)
+ Category.find_by(id: params.categoryId) if params.categoryId.present?
+ end
+
+ def has_term_for_input(params:)
+ params.term.present? && params.filterForInput
+ end
+
+ def has_term(params:)
+ params.term.present?
+ end
+
+ def search_tags(params:, category:, guardian:)
+ filter_options = params.filter_options.merge(category: category)
+
+ tags_with_counts, filter_result_context =
+ DiscourseTagging.filter_allowed_tags(guardian, **filter_options, with_context: true)
+
+ tags_with_counts = Tag.with_localizations(tags_with_counts)
+
+ context[:tags] = TagsController.tag_counts_json(tags_with_counts, guardian)
+ context[:required_tag_group] = filter_result_context[:required_tag_group]
+ context[:forbidden] = nil
+ context[:forbidden_message] = nil
+ end
+
+ def append_disabled_tags(params:, tags:, guardian:)
+ selected_ids = params.resolved_selected_tag_ids
+ skip_ids = tags.map { |t| t[:id] } | selected_ids
+
+ excluded_tags =
+ DiscourseTagging
+ .filter_visible(
+ Tag.where("position(LOWER(?) IN LOWER(tags.name)) <> 0", params.term),
+ guardian,
+ )
+ .where.not(id: skip_ids)
+ .limit(SiteSetting.max_tag_search_results)
+
+ disabled =
+ 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 }
+ end
+
+ context[:tags] = tags.concat(disabled) if disabled.present?
+ end
+
+ def detect_forbidden_tag(params:, tags:, guardian:)
+ return if tags.any? { |h| h[:name].downcase == params.term.downcase }
+
+ tag = Tag.where_name(params.term).first
+ return unless tag && guardian.can_see_tag?(tag)
+
+ context[:forbidden] = params.q
+ context[:forbidden_message] = explain_exclusion(
+ tag,
+ params,
+ params.resolved_selected_tag_ids,
+ guardian,
+ )
+ end
+
+ def explain_exclusion(tag, params, selected_ids, guardian)
+ if params.excludeSynonyms && tag.synonym?
+ return I18n.t("tags.forbidden.synonym", tag_name: tag.target_tag.name)
+ end
+
+ if params.excludeHasSynonyms && tag.synonyms.exists?
+ return I18n.t("tags.forbidden.has_synonyms", tag_name: tag.name)
+ 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
+ return I18n.t("tags.forbidden.one_tag_per_topic_group", tag_group_name: group.name) if group
+ end
+
+ 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 && guardian.can_see_tag?(group.parent_tag)
+ return(
+ I18n.t(
+ "tags.forbidden.missing_parent_tag",
+ parent_tag_name: group.parent_tag.name,
+ tag_group_name: group.name,
+ )
+ )
+ end
+ end
+
+ 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)
+ .pluck(:name)
+
+ if category_names.present?
+ category_names.uniq!
+ I18n.t(
+ "tags.forbidden.restricted_to",
+ count: category_names.count,
+ tag_name: tag.name,
+ category_names: category_names.join(", "),
+ )
+ else
+ I18n.t("tags.forbidden.in_this_category", tag_name: tag.name)
+ end
+ end
+end
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 7eba7ed0b8c..2bbd643a0a4 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -5546,8 +5546,10 @@ en:
restricted_to:
one: '"%{tag_name}" is restricted to the "%{category_names}" category'
other: '"%{tag_name}" is restricted to the following categories: %{category_names}'
- synonym: 'Synonyms are not allowed. Use "%{tag_name}" instead.'
+ synonym: 'This is a synonym for "%{tag_name}". Use "%{tag_name}" instead.'
has_synonyms: '"%{tag_name}" cannot be used because it has synonyms.'
+ one_tag_per_topic_group: 'Only one tag from the "%{tag_group_name}" group is allowed per topic'
+ missing_parent_tag: 'Requires the "%{parent_tag_name}" tag to use tags from the "%{tag_group_name}" group'
restricted_tags_cannot_be_used_in_category:
one: 'The "%{tags}" tag cannot be used in the "%{category}" category. Please remove it.'
other: 'The following tags cannot be used in the "%{category}" category: %{tags}. Please remove them.'
diff --git a/frontend/discourse/select-kit/components/mini-tag-chooser.js b/frontend/discourse/select-kit/components/mini-tag-chooser.js
index e942cd56ba9..4fcce5e565b 100644
--- a/frontend/discourse/select-kit/components/mini-tag-chooser.js
+++ b/frontend/discourse/select-kit/components/mini-tag-chooser.js
@@ -169,6 +169,7 @@ export default class MiniTagChooser extends MultiSelectComponent {
if (!this.selectKit.options.everyTag) {
data.filterForInput = true;
+ data.excludeSynonyms = true;
}
return this.tagUtils.searchTags(
diff --git a/frontend/discourse/select-kit/components/select-kit/select-kit-row.gjs b/frontend/discourse/select-kit/components/select-kit/select-kit-row.gjs
index f88bc90c541..fe908190074 100644
--- a/frontend/discourse/select-kit/components/select-kit/select-kit-row.gjs
+++ b/frontend/discourse/select-kit/components/select-kit/select-kit-row.gjs
@@ -167,7 +167,9 @@ export default class SelectKitRow extends Component {
click(event) {
event.preventDefault();
event.stopPropagation();
- this.selectKit.select(this.rowValue, this.item);
+ if (!this.rowDisabled) {
+ this.selectKit.select(this.rowValue, this.item);
+ }
return false;
}
@@ -201,10 +203,12 @@ export default class SelectKitRow extends Component {
} else if (event.key === "Enter") {
event.stopImmediatePropagation();
- this.selectKit.select(
- this.getValue(this.selectKit.highlighted),
- this.selectKit.highlighted
- );
+ if (!this.rowDisabled) {
+ this.selectKit.select(
+ this.getValue(this.selectKit.highlighted),
+ this.selectKit.highlighted
+ );
+ }
return false;
} else if (event.key === "Escape") {
this.selectKit.close(event);
diff --git a/frontend/discourse/select-kit/components/tag-row.gjs b/frontend/discourse/select-kit/components/tag-row.gjs
index cb0bb1a54b1..8b2a9fb9b1c 100644
--- a/frontend/discourse/select-kit/components/tag-row.gjs
+++ b/frontend/discourse/select-kit/components/tag-row.gjs
@@ -18,6 +18,9 @@ export default class TagRow extends SelectKitRowComponent {
description=this.item.description
count=this.item.count
}}
+ {{#if this.rowDisabled}}
+ <span class="disabled-reason">{{this.title}}</span>
+ {{/if}}
{{else}}
<span class="name">{{this.rowName}}</span>
{{/if}}
diff --git a/spec/requests/tags_controller_spec.rb b/spec/requests/tags_controller_spec.rb
index 6faf6273d7a..ecfb730ce7a 100644
--- a/spec/requests/tags_controller_spec.rb
+++ b/spec/requests/tags_controller_spec.rb
@@ -1417,6 +1417,44 @@ RSpec.describe TagsController do
)
end
+ it "returns category-restricted tag as disabled for input" do
+ category
+ get "/tags/filter/search.json",
+ params: {
+ q: yup.name,
+ filterForInput: true,
+ categoryId: Fabricate(:category).id,
+ }
+
+ expect(response.status).to eq(200)
+ result = response.parsed_body["results"].find { |t| t["name"] == yup.name }
+ expect(result).to be_present
+ expect(result["disabled"]).to eq(true)
+ expect(result["title"]).to eq(
+ I18n.t(
+ "tags.forbidden.restricted_to",
+ count: 1,
+ tag_name: yup.name,
+ category_names: category.name,
+ ),
+ )
+ end
+
+ it "returns category-restricted tag as disabled for partial match" do
+ category
+ get "/tags/filter/search.json",
+ params: {
+ q: "yu",
+ filterForInput: true,
+ categoryId: Fabricate(:category).id,
+ }
+
+ expect(response.status).to eq(200)
+ result = response.parsed_body["results"].find { |t| t["name"] == yup.name }
+ expect(result).to be_present
+ expect(result["disabled"]).to eq(true)
+ end
+
it "can filter on category without q param" do
Fabricate(:tag, name: "nope")
get "/tags/filter/search.json", params: { categoryId: category.id }
@@ -1446,6 +1484,21 @@ RSpec.describe TagsController do
expect(response.parsed_body["results"]).to match([include(name: tag.name, id: tag.id)])
end
+ it "returns synonym as disabled when excluded for input" do
+ get "/tags/filter/search.json",
+ params: {
+ q: "plants",
+ filterForInput: true,
+ excludeSynonyms: "true",
+ }
+
+ expect(response.status).to eq(200)
+ result = response.parsed_body["results"].find { |t| t["name"] == "plants" }
+ expect(result).to be_present
+ expect(result["disabled"]).to eq(true)
+ expect(result["title"]).to eq(I18n.t("tags.forbidden.synonym", tag_name: tag.name))
+ end
+
it "can return a message about synonyms not being allowed" do
get "/tags/filter/search.json", params: { q: "plants", excludeSynonyms: "true" }
expect(response.status).to eq(200)
@@ -1457,6 +1510,133 @@ RSpec.describe TagsController do
end
end
+ context "with one_per_topic tag group" do
+ fab!(:workflow_tag1) { Fabricate(:tag, name: "todo") }
+ fab!(:workflow_tag2) { Fabricate(:tag, name: "ready-to-deploy") }
+ fab!(:tag_group) do
+ Fabricate(
+ :tag_group,
+ name: "Workflow",
+ tags: [workflow_tag1, workflow_tag2],
+ one_per_topic: true,
+ )
+ end
+
+ it "returns sibling tag as disabled when already selected from group" do
+ get "/tags/filter/search.json",
+ params: {
+ q: "ready-to-deploy",
+ filterForInput: true,
+ selected_tag_ids: [workflow_tag1.id],
+ }
+
+ expect(response.status).to eq(200)
+ result = response.parsed_body["results"].find { |t| t["name"] == "ready-to-deploy" }
+ expect(result).to be_present
+ expect(result["disabled"]).to eq(true)
+ expect(result["title"]).to eq(
+ I18n.t("tags.forbidden.one_tag_per_topic_group", tag_group_name: "Workflow"),
+ )
+ end
+
+ it "returns sibling tag as disabled for partial match" do
+ get "/tags/filter/search.json",
+ params: {
+ q: "ready",
+ filterForInput: true,
+ selected_tag_ids: [workflow_tag1.id],
+ }
+
+ expect(response.status).to eq(200)
+ result = response.parsed_body["results"].find { |t| t["name"] == "ready-to-deploy" }
+ expect(result).to be_present
+ expect(result["disabled"]).to eq(true)
+ end
+
+ it "returns tag as enabled when no sibling is selected" do
+ get "/tags/filter/search.json", params: { q: "ready-to-deploy", filterForInput: true }
+
+ expect(response.status).to eq(200)
+ result = response.parsed_body["results"].find { |t| t["name"] == "ready-to-deploy" }
+ expect(result).to be_present
+ expect(result["disabled"]).to be_nil
+ end
+ end
+
+ context "with parent tag group" do
+ fab!(:parent_tag) { Fabricate(:tag, name: "vehicles") }
+ fab!(:child_tag) { Fabricate(:tag, name: "sedan") }
+ fab!(:tag_group) do
+ Fabricate(:tag_group, name: "Vehicle Types", tags: [child_tag], parent_tag: parent_tag)
+ end
+
+ it "returns child tag as disabled when parent is not selected" do
+ get "/tags/filter/search.json", params: { q: "sedan", filterForInput: true }
+
+ expect(response.status).to eq(200)
+ result = response.parsed_body["results"].find { |t| t["name"] == "sedan" }
+ expect(result).to be_present
+ expect(result["disabled"]).to eq(true)
+ expect(result["title"]).to eq(
+ I18n.t(
+ "tags.forbidden.missing_parent_tag",
+ parent_tag_name: "vehicles",
+ tag_group_name: "Vehicle Types",
+ ),
+ )
+ end
+
+ it "returns child tag as enabled when parent is selected" do
+ get "/tags/filter/search.json",
+ params: {
+ q: "sedan",
+ filterForInput: true,
+ selected_tag_ids: [parent_tag.id],
+ }
+
+ expect(response.status).to eq(200)
+ result = response.parsed_body["results"].find { |t| t["name"] == "sedan" }
+ expect(result).to be_present
+ expect(result["disabled"]).to be_nil
+ end
+
+ it "does not leak hidden parent tag name" do
+ staff_group = Fabricate(:group)
+ parent_tag_group = Fabricate(:tag_group, tags: [parent_tag])
+ parent_tag_group.permissions = [
+ [staff_group.id, TagGroupPermission.permission_types[:full]],
+ ]
+ parent_tag_group.save!
+
+ get "/tags/filter/search.json", params: { q: "sedan", filterForInput: true }
+
+ expect(response.status).to eq(200)
+ disabled_result = response.parsed_body["results"].find { |t| t["name"] == "sedan" }
+ expect(disabled_result["title"]).not_to include("vehicles") if disabled_result
+ expect(response.parsed_body["forbidden_message"]).to be_nil
+ end
+ end
+
+ context "with staff-only tag group" do
+ fab!(:hidden_tag) { Fabricate(:tag, name: "staff-only-tag") }
+ fab!(:group)
+ fab!(:tag_group) do
+ Fabricate(:tag_group, tags: [hidden_tag]).tap do |tg|
+ tg.permissions = [[group.id, TagGroupPermission.permission_types[:full]]]
+ tg.save!
+ end
+ end
+
+ it "does not leak hidden tag existence via forbidden message" do
+ get "/tags/filter/search.json", params: { q: "staff-only-tag", filterForInput: true }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["results"]).to be_empty
+ expect(response.parsed_body["forbidden"]).to be_nil
+ expect(response.parsed_body["forbidden_message"]).to be_nil
+ end
+ end
+
it "matches tags after sanitizing input" do
Fabricate(:tag, name: "yup")
nope = Fabricate(:tag, name: "nope")
diff --git a/spec/services/tags/search_spec.rb b/spec/services/tags/search_spec.rb
new file mode 100644
index 00000000000..2334ef7b7ce
--- /dev/null
+++ b/spec/services/tags/search_spec.rb
@@ -0,0 +1,227 @@
+# frozen_string_literal: true
+
+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
+ 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
+ end
+ end
+
+ describe ".call" do
+ subject(:result) { described_class.call(params:, **dependencies) }
+
+ fab!(:user)
+ fab!(:tag1) { Fabricate(:tag, name: "alpha") }
+ fab!(:tag2) { Fabricate(:tag, name: "beta") }
+
+ let(:params) { { q: "alpha" } }
+ let(:dependencies) { { guardian: Guardian.new(user) } }
+
+ before { SiteSetting.tagging_enabled = true }
+
+ context "when contract is invalid" do
+ let(:params) { { q: "test", limit: -1 } }
+
+ it { is_expected.to fail_a_contract }
+ 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
+ expect(result[:forbidden]).to be_nil
+ end
+
+ it "sets forbidden_message to nil" do
+ expect(result[:forbidden_message]).to be_nil
+ end
+ end
+
+ context "with blank query" do
+ let(:params) { {} }
+
+ it { is_expected.to run_successfully }
+
+ it "returns tags ordered by popularity" do
+ expect(result[:tags]).to be_present
+ end
+ end
+
+ context "with a category" do
+ fab!(:category)
+
+ let(:params) { { q: "alpha", categoryId: category.id } }
+
+ it { is_expected.to run_successfully }
+ end
+
+ context "with a non-existent category" do
+ let(:params) { { q: "alpha", categoryId: -999 } }
+
+ it { is_expected.to run_successfully }
+ end
+
+ context "with filterForInput returning disabled tags for one_per_topic groups" do
+ fab!(:tag3) { Fabricate(:tag, name: "gamma") }
+ fab!(:tag_group) do
+ Fabricate(:tag_group, name: "Exclusive Group", one_per_topic: true, tags: [tag1, tag3])
+ end
+
+ 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 "includes the one_per_topic reason" do
+ disabled = result[:tags].find { |t| t[:name] == "gamma" && t[:disabled] }
+ expect(disabled[:title]).to include("Exclusive Group")
+ end
+ end
+
+ context "with filterForInput returning disabled tags for missing parent tag" do
+ fab!(:parent_tag) { Fabricate(:tag, name: "parent") }
+ fab!(:child_tag) { Fabricate(:tag, name: "childtag") }
+ fab!(:tag_group) do
+ Fabricate(:tag_group, name: "Child Group", parent_tag: parent_tag, tags: [child_tag])
+ end
+
+ 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")
+ end
+ end
+
+ context "with filterForInput returning disabled tags for category restrictions" do
+ fab!(:category)
+ fab!(:restricted_tag) { Fabricate(:tag, name: "restricted") }
+
+ before { CategoryTag.create!(category: category, tag: restricted_tag) }
+
+ let(:params) { { q: "restricted", filterForInput: true } }
+
+ it { is_expected.to run_successfully }
+
+ 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")
+ 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)
+ end
+ end
+
+ context "when a forbidden tag is detected" do
+ fab!(:target_tag) { Fabricate(:tag, name: "maintag") }
+ fab!(:synonym_tag) { Fabricate(:tag, name: "syntag", target_tag: target_tag) }
+
+ let(:params) { { q: "syntag", excludeSynonyms: true } }
+
+ it { is_expected.to run_successfully }
+
+ it "sets forbidden to the search query" do
+ 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
+ end
+ end
+
+ context "when a hidden tag does not leak via forbidden" do
+ fab!(:hidden_tag) { Fabricate(:tag, name: "secrethidden") }
+
+ before { create_hidden_tags(%w[secrethidden]) }
+
+ let(:params) { { q: "secrethidden" } }
+
+ it { is_expected.to run_successfully }
+
+ it "does not expose the hidden tag as forbidden" do
+ 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")
+ end
+ end
+
+ context "with required_tag_group propagation" do
+ fab!(:required_tag_group, :tag_group) do
+ Fabricate(:tag_group, name: "Required Group", tags: [tag1])
+ end
+ fab!(:required_category, :category)
+
+ before do
+ CategoryRequiredTagGroup.create!(
+ category: required_category,
+ tag_group: required_tag_group,
+ min_count: 1,
+ )
+ end
+
+ 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)
+ end
+ end
+ end
+end
diff --git a/spec/system/tags/tag_search_hints_spec.rb b/spec/system/tags/tag_search_hints_spec.rb
new file mode 100644
index 00000000000..9798eed2324
--- /dev/null
+++ b/spec/system/tags/tag_search_hints_spec.rb
@@ -0,0 +1,61 @@
+# frozen_string_literal: true
+
+describe "Tag search hints in composer" do
+ fab!(:admin)
+
+ let(:composer) { PageObjects::Components::Composer.new }
+
+ before do
+ SiteSetting.tagging_enabled = true
+ SiteSetting.tag_topic_allowed_groups = Group::AUTO_GROUPS[:trust_level_0]
+ end
+
+ context "with one_per_topic tag group" do
+ fab!(:todo_tag) { Fabricate(:tag, name: "todo") }
+ fab!(:ready_tag) { Fabricate(:tag, name: "ready-to-deploy") }
+
+ fab!(:tag_group) do
+ Fabricate(:tag_group, name: "Workflow", tags: [todo_tag, ready_tag], one_per_topic: true)
+ end
+
+ fab!(:topic) { Fabricate(:topic, user: admin, tags: [todo_tag]) }
+ fab!(:post) { Fabricate(:post, topic: topic, user: admin) }
+
+ it "shows sibling tag as disabled with tooltip" do
+ sign_in(admin)
+ visit topic.url
+
+ find("#post_1 .post-controls .edit").click
+ expect(composer).to be_opened
+
+ tag_chooser = PageObjects::Components::SelectKit.new(".composer-fields .mini-tag-chooser")
+ tag_chooser.expand
+ tag_chooser.search("ready-to-deploy")
+
+ expect(page).to have_css(
+ ".mini-tag-chooser .select-kit-row.disabled[data-name='ready-to-deploy']",
+ )
+ end
+ end
+
+ context "with parent tag group" do
+ fab!(:parent_tag) { Fabricate(:tag, name: "vehicles") }
+ fab!(:child_tag) { Fabricate(:tag, name: "sedan") }
+
+ fab!(:tag_group) do
+ Fabricate(:tag_group, name: "Vehicle Types", tags: [child_tag], parent_tag: parent_tag)
+ end
+
+ it "shows child tag as disabled when parent is not selected" do
+ sign_in(admin)
+ visit "/new-topic"
+ expect(composer).to be_opened
+
+ tag_chooser = PageObjects::Components::SelectKit.new(".composer-fields .mini-tag-chooser")
+ tag_chooser.expand
+ tag_chooser.search("sedan")
+
+ expect(page).to have_css(".mini-tag-chooser .select-kit-row.disabled[data-name='sedan']")
+ end
+ end
+end