2019-05-27 09:52:09 -04:00
# frozen_string_literal: true
require 'rails_helper'
describe Search do
2020-05-17 20:54:56 -04:00
context " # prepare_data " do
it " does not remove English stop words in mixed mode " do
SiteSetting . search_tokenize_chinese_japanese_korean = true
tokenized = Search . prepare_data ( " monkey 吃香蕉 in a loud volume " )
expect ( tokenized ) . to eq ( " monkey 吃 香蕉 in a loud volume " )
SiteSetting . default_locale = 'zh_CN'
tokenized = Search . prepare_data ( " monkey 吃香蕉 in a loud volume " )
expect ( tokenized ) . to eq ( " monkey 吃 香蕉 loud " )
end
end
2019-05-27 09:52:09 -04:00
context " # ts_config " do
it " maps locales to correct Postgres dictionaries " do
expect ( Search . ts_config ) . to eq ( " english " )
expect ( Search . ts_config ( " en " ) ) . to eq ( " english " )
2021-01-20 15:32:22 -05:00
expect ( Search . ts_config ( " en_GB " ) ) . to eq ( " english " )
2019-05-27 09:52:09 -04:00
expect ( Search . ts_config ( " pt_BR " ) ) . to eq ( " portuguese " )
expect ( Search . ts_config ( " tr " ) ) . to eq ( " turkish " )
expect ( Search . ts_config ( " xx " ) ) . to eq ( " simple " )
end
end
2019-10-30 13:07:16 -04:00
context " # GroupedSearchResults.blurb_for " do
it " strips audio and video URLs from search blurb " do
cooked = << ~ RAW
link to an external page : https : / / google . com / ?u = bar
link to an audio file : https : / /somesi te . com / content / file123 . m4a
2019-10-31 09:13:24 -04:00
link to a video file : https : / /somesi te . com / content / somethingelse . MOV
2019-10-30 13:07:16 -04:00
RAW
2020-07-17 04:27:30 -04:00
result = Search :: GroupedSearchResults . blurb_for ( cooked : cooked )
2019-10-30 13:07:16 -04:00
expect ( result ) . to eq ( " link to an external page: https://google.com/?u=bar link to an audio file: #{ I18n . t ( " search.audio " ) } link to a video file: #{ I18n . t ( " search.video " ) } " )
end
2019-11-06 10:32:15 -05:00
it " strips URLs correctly when blurb is longer than limit " do
2019-10-30 13:07:16 -04:00
cooked = << ~ RAW
Here goes a test cooked with enough characters to hit the blurb limit .
Something is very interesting about this audio file .
http : / / localhost / uploads / default / original / 1 X / 90 adc0092b30c04b761541bc0322d0dce3d896e7 . m4a
RAW
2020-07-17 04:27:30 -04:00
result = Search :: GroupedSearchResults . blurb_for ( cooked : cooked )
2019-10-30 13:07:16 -04:00
expect ( result ) . to eq ( " Here goes a test cooked with enough characters to hit the blurb limit. Something is very interesting about this audio file. #{ I18n . t ( " search.audio " ) } " )
end
2019-11-06 10:32:15 -05:00
it " does not fail on bad URLs " do
cooked = << ~ RAW
invalid URL : http : error ] should not trip up blurb generation .
RAW
2020-07-17 04:27:30 -04:00
result = Search :: GroupedSearchResults . blurb_for ( cooked : cooked )
2019-11-06 10:32:15 -05:00
expect ( result ) . to eq ( " invalid URL: http:error] should not trip up blurb generation. " )
end
2019-10-30 13:07:16 -04:00
end
2020-09-02 06:24:40 -04:00
context " # execute " do
before do
SiteSetting . tagging_enabled = true
end
context " staff tags " do
fab! ( :hidden_tag ) { Fabricate ( :tag ) }
let! ( :staff_tag_group ) { Fabricate ( :tag_group , permissions : { " staff " = > 1 } , tag_names : [ hidden_tag . name ] ) }
fab! ( :topic ) { Fabricate ( :topic , tags : [ hidden_tag ] ) }
fab! ( :post ) { Fabricate ( :post , topic : topic ) }
before do
SiteSetting . tagging_enabled = true
SearchIndexer . enable
SearchIndexer . index ( hidden_tag , force : true )
SearchIndexer . index ( topic , force : true )
end
it " are visible to staff users " do
result = Search . execute ( hidden_tag . name , guardian : Guardian . new ( Fabricate ( :admin ) ) )
expect ( result . tags ) . to contain_exactly ( hidden_tag )
end
it " are hidden to regular users " do
result = Search . execute ( hidden_tag . name , guardian : Guardian . new ( Fabricate ( :user ) ) )
expect ( result . tags ) . to contain_exactly ( )
end
end
end
2020-09-13 21:58:28 -04:00
context " custom_eager_load " do
fab! ( :topic ) { Fabricate ( :topic ) }
fab! ( :post ) { Fabricate ( :post , topic : topic ) }
before do
SearchIndexer . enable
SearchIndexer . index ( topic , force : true )
end
it " includes custom tables " do
begin
2021-06-07 11:07:46 -04:00
SiteSetting . tagging_enabled = false
2020-09-13 21:58:28 -04:00
expect ( Search . execute ( " test " ) . posts [ 0 ] . topic . association ( :category ) . loaded? ) . to be true
expect ( Search . execute ( " test " ) . posts [ 0 ] . topic . association ( :tags ) . loaded? ) . to be false
SiteSetting . tagging_enabled = true
Search . custom_topic_eager_load ( [ :topic_users ] )
Search . custom_topic_eager_load ( ) do
[ :bookmarks ]
end
expect ( Search . execute ( " test " ) . posts [ 0 ] . topic . association ( :tags ) . loaded? ) . to be true
expect ( Search . execute ( " test " ) . posts [ 0 ] . topic . association ( :topic_users ) . loaded? ) . to be true
expect ( Search . execute ( " test " ) . posts [ 0 ] . topic . association ( :bookmarks ) . loaded? ) . to be true
ensure
SiteSetting . tagging_enabled = false
Search . instance_variable_set ( :@custom_topic_eager_loads , [ ] )
end
end
end
2021-04-27 01:52:45 -04:00
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 ) )
2021-06-28 21:17:49 -04:00
expect ( result . users . find { | u | u . id == user . id } . custom_data ) . to eq ( [
2021-04-27 01:52:45 -04:00
{ name : " custom field " , value : " test " } ,
{ name : " another custom field " , value : " longer test " }
] )
2021-06-28 21:17:49 -04:00
expect ( result . users . find { | u | u . id == user2 . id } . custom_data ) . to eq ( [
2021-04-27 01:52:45 -04:00
{ name : " another custom field " , value : " second user test " }
] )
end
2021-09-06 09:59:35 -04:00
context " when using SiteSetting.enable_listing_suspended_users_on_search " do
fab! ( :suspended_user ) { Fabricate ( :user , username : 'revolver_ocelot' , suspended_at : Time . now , suspended_till : 5 . days . from_now ) }
before { SearchIndexer . index ( suspended_user , force : true ) }
it " should list suspended users to regular users if the setting is enabled " do
SiteSetting . enable_listing_suspended_users_on_search = true
result = Search . execute ( " revolver_ocelot " , guardian : Guardian . new ( user ) )
expect ( result . users ) . to contain_exactly ( suspended_user )
end
it " shouldn't list suspended users to regular users if the setting is disabled " do
SiteSetting . enable_listing_suspended_users_on_search = false
result = Search . execute ( " revolver_ocelot " , guardian : Guardian . new ( user ) )
expect ( result . users ) . to be_empty
end
it " should list suspended users to admins regardless of the setting " do
SiteSetting . enable_listing_suspended_users_on_search = false
result = Search . execute ( " revolver_ocelot " , guardian : Guardian . new ( Fabricate ( :admin ) ) )
expect ( result . users ) . to contain_exactly ( suspended_user )
end
end
2021-04-27 01:52:45 -04:00
end
2021-08-02 07:04:13 -04:00
context " categories " do
it " finds topics in sub-sub-categories " do
SiteSetting . max_category_nesting = 3
category = Fabricate ( :category_with_definition )
subcategory = Fabricate ( :category_with_definition , parent_category_id : category . id )
subsubcategory = Fabricate ( :category_with_definition , parent_category_id : subcategory . id )
topic = Fabricate ( :topic , category : subsubcategory )
post = Fabricate ( :post , topic : topic )
SearchIndexer . enable
SearchIndexer . index ( post , force : true )
expect ( Search . execute ( " test # #{ category . slug } " ) . posts ) . to contain_exactly ( post )
expect ( Search . execute ( " test # #{ category . slug } : #{ subcategory . slug } " ) . posts ) . to contain_exactly ( post )
expect ( Search . execute ( " test # #{ subcategory . slug } " ) . posts ) . to contain_exactly ( post )
expect ( Search . execute ( " test # #{ subcategory . slug } : #{ subsubcategory . slug } " ) . posts ) . to contain_exactly ( post )
expect ( Search . execute ( " test # #{ subsubcategory . slug } " ) . posts ) . to contain_exactly ( post )
expect ( Search . execute ( " test # = #{ category . slug } " ) . posts ) . to be_empty
expect ( Search . execute ( " test # = #{ category . slug } : #{ subcategory . slug } " ) . posts ) . to be_empty
expect ( Search . execute ( " test # = #{ subcategory . slug } " ) . posts ) . to be_empty
expect ( Search . execute ( " test # = #{ subcategory . slug } : #{ subsubcategory . slug } " ) . posts ) . to contain_exactly ( post )
expect ( Search . execute ( " test # = #{ subsubcategory . slug } " ) . posts ) . to contain_exactly ( post )
end
end
2019-05-27 09:52:09 -04:00
end