FEATURE: the ability to search users by custom fields (#12762)

When the admin creates a new custom field they can specify if that field should be searchable or not.

That setting is taken into consideration for quick search results.
This commit is contained in:
Krzysztof Kotlarek
2021-04-27 15:52:45 +10:00
committed by GitHub
parent 8aeeadd8b0
commit e29605b79f
16 changed files with 154 additions and 19 deletions

View File

@@ -124,4 +124,45 @@ describe Search do
end
end
end
context "users" do
fab!(:user) { Fabricate(:user, username: "DonaldDuck") }
fab!(:user2) { Fabricate(:user) }
before do
SearchIndexer.enable
SearchIndexer.index(user, force: true)
end
it "finds users by their names or custom fields" do
result = Search.execute("donaldduck", guardian: Guardian.new(user2))
expect(result.users).to contain_exactly(user)
user_field = Fabricate(:user_field, name: "custom field")
UserCustomField.create!(user: user, value: "test", name: "user_field_#{user_field.id}")
Jobs::ReindexSearch.new.execute({})
result = Search.execute("test", guardian: Guardian.new(user2))
expect(result.users).to be_empty
user_field.update!(searchable: true)
Jobs::ReindexSearch.new.execute({})
result = Search.execute("test", guardian: Guardian.new(user2))
expect(result.users).to contain_exactly(user)
user_field2 = Fabricate(:user_field, name: "another custom field", searchable: true)
UserCustomField.create!(user: user, value: "longer test", name: "user_field_#{user_field2.id}")
UserCustomField.create!(user: user2, value: "second user test", name: "user_field_#{user_field2.id}")
SearchIndexer.index(user, force: true)
SearchIndexer.index(user2, force: true)
result = Search.execute("test", guardian: Guardian.new(user2))
expect(result.users.first.custom_data).to eq([
{ name: "custom field", value: "test" },
{ name: "another custom field", value: "longer test" }
])
expect(result.users.last.custom_data).to eq([
{ name: "another custom field", value: "second user test" }
])
end
end
end