2013-03-01 03:52:35 +08:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-08-02 08:31:36 +08:00
|
|
|
require_dependency 'search'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
describe Search do
|
|
|
|
|
2014-04-12 04:07:39 +08:00
|
|
|
class TextHelper
|
|
|
|
extend ActionView::Helpers::TextHelper
|
|
|
|
end
|
|
|
|
|
2013-05-14 09:59:55 +08:00
|
|
|
before do
|
2016-12-22 10:13:14 +08:00
|
|
|
SearchIndexer.enable
|
2013-05-14 09:59:55 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
context 'post indexing observer' do
|
|
|
|
before do
|
2013-02-06 03:16:51 +08:00
|
|
|
@category = Fabricate(:category, name: 'america')
|
2013-03-11 21:51:24 +08:00
|
|
|
@topic = Fabricate(:topic, title: 'sam saffron test topic', category: @category)
|
2013-02-06 03:16:51 +08:00
|
|
|
@post = Fabricate(:post, topic: @topic, raw: 'this <b>fun test</b> <img src="bla" title="my image">')
|
2013-05-23 03:33:33 +08:00
|
|
|
@indexed = @post.post_search_data.search_data
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2014-09-01 15:04:40 +08:00
|
|
|
|
|
|
|
it "should index correctly" do
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(@indexed).to match(/fun/)
|
|
|
|
expect(@indexed).to match(/sam/)
|
|
|
|
expect(@indexed).to match(/america/)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-07 09:09:31 +08:00
|
|
|
@topic.title = "harpi is the new title"
|
2013-02-06 03:16:51 +08:00
|
|
|
@topic.save!
|
2013-05-23 03:33:33 +08:00
|
|
|
@post.post_search_data.reload
|
|
|
|
|
|
|
|
@indexed = @post.post_search_data.search_data
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(@indexed).to match(/harpi/)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
context 'user indexing observer' do
|
|
|
|
before do
|
2013-02-06 03:16:51 +08:00
|
|
|
@user = Fabricate(:user, username: 'fred', name: 'bob jones')
|
2013-05-23 03:33:33 +08:00
|
|
|
@indexed = @user.user_search_data.search_data
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-09-02 17:15:08 +08:00
|
|
|
it "should pick up on data" do
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(@indexed).to match(/fred/)
|
|
|
|
expect(@indexed).to match(/jone/)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
context 'category indexing observer' do
|
|
|
|
before do
|
2013-02-06 03:16:51 +08:00
|
|
|
@category = Fabricate(:category, name: 'america')
|
2013-05-23 03:33:33 +08:00
|
|
|
@indexed = @category.category_search_data.search_data
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it "should pick up on name" do
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(@indexed).to match(/america/)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-03-07 23:52:01 +08:00
|
|
|
it 'does not search when the search term is too small' do
|
2016-08-10 02:48:39 +08:00
|
|
|
search = Search.new('evil', min_search_term_length: 5)
|
|
|
|
search.execute
|
|
|
|
expect(search.valid?).to eq(false)
|
|
|
|
expect(search.term).to eq('')
|
|
|
|
end
|
|
|
|
|
2016-08-11 23:52:53 +08:00
|
|
|
it 'needs at least one term that hits the length' do
|
2016-08-10 02:48:39 +08:00
|
|
|
search = Search.new('a b c d', min_search_term_length: 5)
|
|
|
|
search.execute
|
|
|
|
expect(search.valid?).to eq(false)
|
|
|
|
expect(search.term).to eq('')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'searches for quoted short terms' do
|
|
|
|
search = Search.new('"a b c d"', min_search_term_length: 5)
|
|
|
|
search.execute
|
|
|
|
expect(search.valid?).to eq(true)
|
|
|
|
expect(search.term).to eq('"a b c d"')
|
|
|
|
end
|
|
|
|
|
2016-08-11 23:52:53 +08:00
|
|
|
it 'searches for short terms if one hits the length' do
|
2016-08-10 02:48:39 +08:00
|
|
|
search = Search.new('a b c okaylength', min_search_term_length: 5)
|
|
|
|
search.execute
|
|
|
|
expect(search.valid?).to eq(true)
|
2016-08-11 23:52:53 +08:00
|
|
|
expect(search.term).to eq('a b c okaylength')
|
2013-03-07 23:52:01 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
it 'escapes non alphanumeric characters' do
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(Search.execute('foo :!$);}]>@\#\"\'').posts.length).to eq(0) # There are at least three levels of sanitation for Search.query!
|
2013-08-27 04:25:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't raise an error when single quotes are present" do
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(Search.execute("'hello' world").posts.length).to eq(0) # There are at least three levels of sanitation for Search.query!
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'works when given two terms with spaces' do
|
2015-04-25 23:18:35 +08:00
|
|
|
expect { Search.execute('evil trout') }.not_to raise_error
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'users' do
|
|
|
|
let!(:user) { Fabricate(:user) }
|
2014-09-02 17:15:08 +08:00
|
|
|
let(:result) { Search.execute('bruce', type_filter: 'user') }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it 'returns a result' do
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(result.users.length).to eq(1)
|
|
|
|
expect(result.users[0].id).to eq(user.id)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2015-10-29 02:56:08 +08:00
|
|
|
|
|
|
|
context 'hiding user profiles' do
|
|
|
|
before { SiteSetting.stubs(:hide_user_profiles_from_public).returns(true) }
|
|
|
|
|
|
|
|
it 'returns no result for anon' do
|
|
|
|
expect(result.users.length).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a result for logged in users' do
|
|
|
|
result = Search.execute('bruce', type_filter: 'user', guardian: Guardian.new(user))
|
|
|
|
expect(result.users.length).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-10-25 07:20:41 +08:00
|
|
|
context 'inactive users' do
|
|
|
|
let!(:inactive_user) { Fabricate(:inactive_user, active: false) }
|
|
|
|
let(:result) { Search.execute('bruce') }
|
|
|
|
|
|
|
|
it 'does not return a result' do
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(result.users.length).to eq(0)
|
2014-10-25 07:20:41 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-19 04:06:59 +08:00
|
|
|
context 'staged users' do
|
|
|
|
let(:staged) { Fabricate(:staged) }
|
|
|
|
let(:result) { Search.execute(staged.username) }
|
|
|
|
|
|
|
|
it 'does not return a result' do
|
|
|
|
expect(result.users.length).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-04 10:46:52 +08:00
|
|
|
context 'private messages' do
|
|
|
|
|
|
|
|
let(:topic) {
|
|
|
|
Fabricate(:topic,
|
|
|
|
category_id: nil,
|
|
|
|
archetype: 'private_message')
|
|
|
|
}
|
|
|
|
|
|
|
|
let(:post) { Fabricate(:post, topic: topic) }
|
|
|
|
let(:reply) { Fabricate(:post, topic: topic,
|
|
|
|
raw: 'hello from mars, we just landed') }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it 'searches correctly' do
|
|
|
|
|
2017-05-12 03:58:43 +08:00
|
|
|
expect do
|
|
|
|
Search.execute('mars', type_filter: 'private_messages')
|
|
|
|
end.to raise_error(Discourse::InvalidAccess)
|
2014-12-04 10:46:52 +08:00
|
|
|
|
2017-05-12 03:58:43 +08:00
|
|
|
TopicAllowedUser.create!(user_id: reply.user_id, topic_id: topic.id)
|
|
|
|
TopicAllowedUser.create!(user_id: post.user_id, topic_id: topic.id)
|
2014-12-04 10:46:52 +08:00
|
|
|
|
2017-05-12 03:58:43 +08:00
|
|
|
results = Search.execute('mars',
|
|
|
|
type_filter: 'private_messages',
|
|
|
|
guardian: Guardian.new(reply.user))
|
2014-12-04 10:46:52 +08:00
|
|
|
|
2017-05-12 03:58:43 +08:00
|
|
|
expect(results.posts.length).to eq(1)
|
2014-12-04 10:46:52 +08:00
|
|
|
|
2015-02-19 09:56:49 +08:00
|
|
|
|
2017-05-12 03:58:43 +08:00
|
|
|
results = Search.execute('mars',
|
|
|
|
search_context: topic,
|
|
|
|
guardian: Guardian.new(reply.user))
|
2015-02-19 09:56:49 +08:00
|
|
|
|
2017-05-12 03:58:43 +08:00
|
|
|
expect(results.posts.length).to eq(1)
|
2015-02-19 09:56:49 +08:00
|
|
|
|
2017-05-12 03:58:43 +08:00
|
|
|
# does not leak out
|
|
|
|
results = Search.execute('mars',
|
|
|
|
type_filter: 'private_messages',
|
|
|
|
guardian: Guardian.new(Fabricate(:user)))
|
2014-12-04 10:46:52 +08:00
|
|
|
|
2017-05-12 03:58:43 +08:00
|
|
|
expect(results.posts.length).to eq(0)
|
2014-12-04 10:46:52 +08:00
|
|
|
|
2017-05-12 03:58:43 +08:00
|
|
|
Fabricate(:topic, category_id: nil, archetype: 'private_message')
|
|
|
|
Fabricate(:post, topic: topic, raw: 'another secret pm from mars, testing')
|
2014-12-04 10:46:52 +08:00
|
|
|
|
|
|
|
|
2017-05-12 03:58:43 +08:00
|
|
|
# admin can search everything with correct context
|
|
|
|
results = Search.execute('mars',
|
|
|
|
type_filter: 'private_messages',
|
|
|
|
search_context: post.user,
|
|
|
|
guardian: Guardian.new(Fabricate(:admin)))
|
2014-12-04 10:46:52 +08:00
|
|
|
|
2017-05-12 03:58:43 +08:00
|
|
|
expect(results.posts.length).to eq(1)
|
2014-12-04 10:46:52 +08:00
|
|
|
|
2017-05-12 03:58:43 +08:00
|
|
|
results = Search.execute('mars in:private',
|
|
|
|
search_context: post.user,
|
|
|
|
guardian: Guardian.new(post.user))
|
2017-05-12 03:06:30 +08:00
|
|
|
|
2017-05-12 03:58:43 +08:00
|
|
|
expect(results.posts.length).to eq(1)
|
|
|
|
|
|
|
|
# can search group PMs as well as non admin
|
|
|
|
#
|
|
|
|
user = Fabricate(:user)
|
|
|
|
group = Fabricate.build(:group)
|
|
|
|
group.add(user)
|
|
|
|
group.save!
|
|
|
|
|
|
|
|
TopicAllowedGroup.create!(group_id: group.id, topic_id: topic.id)
|
|
|
|
|
|
|
|
results = Search.execute('mars in:private',
|
|
|
|
guardian: Guardian.new(user))
|
|
|
|
|
|
|
|
expect(results.posts.length).to eq(1)
|
2017-05-12 03:06:30 +08:00
|
|
|
|
2014-12-04 10:46:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
context 'topics' do
|
2014-09-02 17:15:08 +08:00
|
|
|
let(:post) { Fabricate(:post) }
|
|
|
|
let(:topic) { post.topic}
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-02-17 10:54:51 +08:00
|
|
|
|
|
|
|
context 'search within topic' do
|
|
|
|
|
|
|
|
def new_post(raw, topic)
|
|
|
|
Fabricate(:post, topic: topic, topic_id: topic.id, user: topic.user, raw: raw)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'displays multiple results within a topic' do
|
2014-02-19 05:59:18 +08:00
|
|
|
|
2014-02-17 10:54:51 +08:00
|
|
|
topic = Fabricate(:topic)
|
|
|
|
topic2 = Fabricate(:topic)
|
|
|
|
|
|
|
|
new_post('this is the other post I am posting', topic2)
|
2014-06-20 13:48:34 +08:00
|
|
|
new_post('this is my fifth post I am posting', topic2)
|
|
|
|
|
2014-02-17 10:54:51 +08:00
|
|
|
post1 = new_post('this is the other post I am posting', topic)
|
|
|
|
post2 = new_post('this is my first post I am posting', topic)
|
|
|
|
post3 = new_post('this is a real long and complicated bla this is my second post I am Posting birds
|
|
|
|
with more stuff bla bla', topic)
|
|
|
|
post4 = new_post('this is my fourth post I am posting', topic)
|
|
|
|
|
2014-02-19 05:59:18 +08:00
|
|
|
# update posts_count
|
|
|
|
topic.reload
|
|
|
|
|
2014-09-02 17:15:08 +08:00
|
|
|
results = Search.execute('posting', search_context: post1.topic)
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(results.posts.map(&:id)).to eq([post1.id, post2.id, post3.id, post4.id])
|
2014-02-17 10:54:51 +08:00
|
|
|
|
2017-05-24 23:24:41 +08:00
|
|
|
results = Search.execute('posting l', search_context: post1.topic)
|
|
|
|
expect(results.posts.map(&:id)).to eq([post4.id, post3.id, post2.id, post1.id])
|
|
|
|
|
2014-06-20 13:48:34 +08:00
|
|
|
# stop words should work
|
2014-09-02 17:15:08 +08:00
|
|
|
results = Search.execute('this', search_context: post1.topic)
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(results.posts.length).to eq(4)
|
2016-07-25 13:06:25 +08:00
|
|
|
|
|
|
|
# phrase search works as expected
|
|
|
|
results = Search.execute('"fourth post I am posting"', search_context: post1.topic)
|
|
|
|
expect(results.posts.length).to eq(1)
|
2014-02-17 10:54:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
context 'searching the OP' do
|
2014-09-02 17:15:08 +08:00
|
|
|
let!(:post) { Fabricate(:post_with_long_raw_content) }
|
|
|
|
let(:result) { Search.execute('hundred', type_filter: 'topic', include_blurbs: true) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-05-13 08:48:32 +08:00
|
|
|
it 'returns a result correctly' do
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(result.posts.length).to eq(1)
|
|
|
|
expect(result.posts[0].id).to eq(post.id)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-05-14 05:04:41 +08:00
|
|
|
end
|
|
|
|
|
2013-06-29 09:22:17 +08:00
|
|
|
context 'searching for a post' do
|
|
|
|
let!(:reply) { Fabricate(:basic_reply, topic: topic, user: topic.user) }
|
2014-09-02 17:15:08 +08:00
|
|
|
let(:result) { Search.execute('quotes', type_filter: 'topic', include_blurbs: true) }
|
2013-06-29 09:22:17 +08:00
|
|
|
|
|
|
|
it 'returns the post' do
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(result).to be_present
|
|
|
|
expect(result.posts.length).to eq(1)
|
2014-09-02 17:15:08 +08:00
|
|
|
p = result.posts[0]
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(p.topic.id).to eq(topic.id)
|
|
|
|
expect(p.id).to eq(reply.id)
|
|
|
|
expect(result.blurb(p)).to eq("this reply has no quotes")
|
2013-06-29 09:22:17 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-29 03:42:29 +08:00
|
|
|
context "search for a topic by id" do
|
2014-09-02 17:15:08 +08:00
|
|
|
let(:result) { Search.execute(topic.id, type_filter: 'topic', search_for_id: true, min_search_term_length: 1) }
|
2014-08-29 03:42:29 +08:00
|
|
|
|
|
|
|
it 'returns the topic' do
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(result.posts.length).to eq(1)
|
|
|
|
expect(result.posts.first.id).to eq(post.id)
|
2014-08-29 03:42:29 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-14 05:04:41 +08:00
|
|
|
context "search for a topic by url" do
|
2014-09-02 17:15:08 +08:00
|
|
|
let(:result) { Search.execute(topic.relative_url, search_for_id: true, type_filter: 'topic')}
|
2013-05-14 05:04:41 +08:00
|
|
|
|
|
|
|
it 'returns the topic' do
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(result.posts.length).to eq(1)
|
|
|
|
expect(result.posts.first.id).to eq(post.id)
|
2013-05-14 05:04:41 +08:00
|
|
|
end
|
2013-05-13 08:48:32 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'security' do
|
2013-06-29 09:22:17 +08:00
|
|
|
|
2013-05-13 08:48:32 +08:00
|
|
|
def result(current_user)
|
2014-10-18 12:19:08 +08:00
|
|
|
Search.execute('hello', guardian: Guardian.new(current_user))
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-05-13 08:48:32 +08:00
|
|
|
it 'secures results correctly' do
|
|
|
|
category = Fabricate(:category)
|
|
|
|
|
|
|
|
topic.category_id = category.id
|
|
|
|
topic.save
|
|
|
|
|
2013-07-14 09:24:16 +08:00
|
|
|
category.set_permissions(:staff => :full)
|
2013-05-13 08:48:32 +08:00
|
|
|
category.save
|
|
|
|
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(result(nil).posts).not_to be_present
|
|
|
|
expect(result(Fabricate(:user)).posts).not_to be_present
|
|
|
|
expect(result(Fabricate(:admin)).posts).to be_present
|
2013-05-13 08:48:32 +08:00
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-03-01 03:52:35 +08:00
|
|
|
context 'cyrillic topic' do
|
|
|
|
let!(:cyrillic_topic) { Fabricate(:topic) do
|
|
|
|
user
|
|
|
|
title { sequence(:title) { |i| "Тестовая запись #{i}" } }
|
|
|
|
end
|
|
|
|
}
|
|
|
|
let!(:post) {Fabricate(:post, topic: cyrillic_topic, user: cyrillic_topic.user)}
|
2014-09-02 17:15:08 +08:00
|
|
|
let(:result) { Search.execute('запись') }
|
2013-03-01 03:52:35 +08:00
|
|
|
|
|
|
|
it 'finds something when given cyrillic query' do
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(result.posts).to be_present
|
2013-03-01 03:52:35 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-07 09:40:57 +08:00
|
|
|
it 'does not tokenize search term' do
|
|
|
|
Fabricate(:post, raw: 'thing is canned should still be found!')
|
|
|
|
expect(Search.execute('canned').posts).to be_present
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
context 'categories' do
|
|
|
|
|
|
|
|
let!(:category) { Fabricate(:category) }
|
2014-09-02 17:15:08 +08:00
|
|
|
def search
|
|
|
|
Search.execute(category.name)
|
2013-05-13 16:04:03 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-05-13 08:48:32 +08:00
|
|
|
it 'returns the correct result' do
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(search.categories).to be_present
|
2013-05-13 16:04:03 +08:00
|
|
|
|
2013-07-14 09:24:16 +08:00
|
|
|
category.set_permissions({})
|
2013-05-13 16:04:03 +08:00
|
|
|
category.save
|
|
|
|
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(search.categories).not_to be_present
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
context 'type_filter' do
|
|
|
|
|
|
|
|
let!(:user) { Fabricate(:user, username: 'amazing', email: 'amazing@amazing.com') }
|
|
|
|
let!(:category) { Fabricate(:category, name: 'amazing category', user: user) }
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
context 'user filter' do
|
2014-09-02 17:15:08 +08:00
|
|
|
let(:results) { Search.execute('amazing', type_filter: 'user') }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it "returns a user result" do
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(results.categories.length).to eq(0)
|
|
|
|
expect(results.posts.length).to eq(0)
|
|
|
|
expect(results.users.length).to eq(1)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'category filter' do
|
2014-09-02 17:15:08 +08:00
|
|
|
let(:results) { Search.execute('amazing', type_filter: 'category') }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-05-21 16:56:04 +08:00
|
|
|
it "returns a category result" do
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(results.categories.length).to eq(1)
|
|
|
|
expect(results.posts.length).to eq(0)
|
|
|
|
expect(results.users.length).to eq(0)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-05-25 02:03:45 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'search_context' do
|
|
|
|
|
2014-09-02 17:15:08 +08:00
|
|
|
it 'can find a user when using search context' do
|
|
|
|
|
|
|
|
coding_horror = Fabricate(:coding_horror)
|
|
|
|
post = Fabricate(:post)
|
2013-05-25 02:03:45 +08:00
|
|
|
|
2014-09-02 17:15:08 +08:00
|
|
|
Fabricate(:post, user: coding_horror)
|
2013-05-25 04:17:09 +08:00
|
|
|
|
2014-09-02 17:15:08 +08:00
|
|
|
result = Search.execute('hello', search_context: post.user)
|
|
|
|
|
|
|
|
result.posts.first.topic_id = post.topic_id
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(result.posts.length).to eq(1)
|
2013-05-25 02:03:45 +08:00
|
|
|
end
|
|
|
|
|
2014-09-02 17:15:08 +08:00
|
|
|
it 'can use category as a search context' do
|
|
|
|
category = Fabricate(:category)
|
|
|
|
topic = Fabricate(:topic, category: category)
|
|
|
|
topic_no_cat = Fabricate(:topic)
|
|
|
|
|
2017-03-11 04:58:47 +08:00
|
|
|
# includes subcategory in search
|
|
|
|
subcategory = Fabricate(:category, parent_category_id: category.id)
|
|
|
|
sub_topic = Fabricate(:topic, category: subcategory)
|
|
|
|
|
2014-09-02 17:15:08 +08:00
|
|
|
post = Fabricate(:post, topic: topic, user: topic.user )
|
|
|
|
_another_post = Fabricate(:post, topic: topic_no_cat, user: topic.user )
|
2017-03-11 04:58:47 +08:00
|
|
|
sub_post = Fabricate(:post, raw: 'I am saying hello from a subcategory', topic: sub_topic, user: topic.user )
|
2013-05-25 02:03:45 +08:00
|
|
|
|
2014-09-02 17:15:08 +08:00
|
|
|
search = Search.execute('hello', search_context: category)
|
2017-03-11 04:58:47 +08:00
|
|
|
expect(search.posts.map(&:id).sort).to eq([post.id,sub_post.id].sort)
|
|
|
|
expect(search.posts.length).to eq(2)
|
2013-05-25 02:03:45 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-06-26 07:58:49 +08:00
|
|
|
describe 'Chinese search' do
|
|
|
|
it 'splits English / Chinese' do
|
|
|
|
SiteSetting.default_locale = 'zh_CN'
|
|
|
|
data = Search.prepare_data('Discourse社区指南').split(' ')
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(data).to eq(['Discourse', '社区','指南'])
|
2014-06-26 07:58:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'finds chinese topic based on title' do
|
2015-04-24 05:22:46 +08:00
|
|
|
skip("skipped until pg app installs the db correctly") if RbConfig::CONFIG["arch"] =~ /darwin/
|
|
|
|
|
2014-06-26 07:58:49 +08:00
|
|
|
SiteSetting.default_locale = 'zh_TW'
|
2016-08-10 03:20:28 +08:00
|
|
|
SiteSetting.min_search_term_length = 1
|
2014-09-16 19:15:05 +08:00
|
|
|
topic = Fabricate(:topic, title: 'My Title Discourse社區指南')
|
2014-09-02 17:15:08 +08:00
|
|
|
post = Fabricate(:post, topic: topic)
|
2014-06-26 07:58:49 +08:00
|
|
|
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(Search.execute('社區指南').posts.first.id).to eq(post.id)
|
|
|
|
expect(Search.execute('指南').posts.first.id).to eq(post.id)
|
2014-06-26 07:58:49 +08:00
|
|
|
end
|
2015-11-27 13:35:27 +08:00
|
|
|
|
|
|
|
it 'finds chinese topic based on title if tokenization is forced' do
|
|
|
|
skip("skipped until pg app installs the db correctly") if RbConfig::CONFIG["arch"] =~ /darwin/
|
|
|
|
|
|
|
|
SiteSetting.search_tokenize_chinese_japanese_korean = true
|
2016-08-10 03:20:28 +08:00
|
|
|
SiteSetting.min_search_term_length = 1
|
2015-11-27 13:35:27 +08:00
|
|
|
|
|
|
|
topic = Fabricate(:topic, title: 'My Title Discourse社區指南')
|
|
|
|
post = Fabricate(:post, topic: topic)
|
|
|
|
|
|
|
|
expect(Search.execute('社區指南').posts.first.id).to eq(post.id)
|
|
|
|
expect(Search.execute('指南').posts.first.id).to eq(post.id)
|
|
|
|
end
|
2014-06-26 07:58:49 +08:00
|
|
|
end
|
|
|
|
|
2014-09-03 19:54:10 +08:00
|
|
|
describe 'Advanced search' do
|
2015-06-23 11:21:50 +08:00
|
|
|
|
2016-03-18 13:26:54 +08:00
|
|
|
it 'supports pinned and unpinned' do
|
|
|
|
topic = Fabricate(:topic)
|
|
|
|
Fabricate(:post, raw: 'hi this is a test 123 123', topic: topic)
|
|
|
|
_post = Fabricate(:post, raw: 'boom boom shake the room', topic: topic)
|
|
|
|
|
|
|
|
topic.update_pinned(true)
|
|
|
|
|
|
|
|
user = Fabricate(:user)
|
|
|
|
guardian = Guardian.new(user)
|
|
|
|
|
|
|
|
expect(Search.execute('boom in:pinned').posts.length).to eq(1)
|
|
|
|
expect(Search.execute('boom in:unpinned', guardian: guardian).posts.length).to eq(0)
|
|
|
|
|
|
|
|
topic.clear_pin_for(user)
|
|
|
|
|
|
|
|
expect(Search.execute('boom in:unpinned', guardian: guardian).posts.length).to eq(1)
|
|
|
|
end
|
|
|
|
|
2016-05-13 16:26:53 +08:00
|
|
|
it 'supports wiki' do
|
|
|
|
topic = Fabricate(:topic)
|
2017-03-08 22:46:23 +08:00
|
|
|
topic_2 = Fabricate(:topic)
|
|
|
|
post = Fabricate(:post, raw: 'this is a test 248', wiki: true, topic: topic)
|
|
|
|
Fabricate(:post, raw: 'this is a test 248', wiki: false, topic: topic_2)
|
2016-05-13 16:26:53 +08:00
|
|
|
|
2017-03-08 22:46:23 +08:00
|
|
|
expect(Search.execute('test 248').posts.length).to eq(2)
|
|
|
|
expect(Search.execute('test 248 in:wiki').posts.first).to eq(post)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports searching for posts that the user has seen/unseen' do
|
|
|
|
topic = Fabricate(:topic)
|
|
|
|
topic_2 = Fabricate(:topic)
|
|
|
|
post = Fabricate(:post, raw: 'logan is longan', topic: topic)
|
|
|
|
post_2 = Fabricate(:post, raw: 'longan is logan', topic: topic_2)
|
|
|
|
|
|
|
|
[post.user, topic.user].each do |user|
|
|
|
|
PostTiming.create!(
|
|
|
|
post_number: post.post_number,
|
|
|
|
topic: topic,
|
|
|
|
user: user,
|
|
|
|
msecs: 1
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(post.seen?(post.user)).to eq(true)
|
|
|
|
|
|
|
|
expect(Search.execute('longan').posts.sort).to eq([post, post_2])
|
|
|
|
|
|
|
|
expect(Search.execute('longan in:seen', guardian: Guardian.new(post.user)).posts)
|
|
|
|
.to eq([post])
|
|
|
|
|
|
|
|
expect(Search.execute('longan in:seen').posts.sort).to eq([post, post_2])
|
|
|
|
|
|
|
|
expect(Search.execute('longan in:seen', guardian: Guardian.new(post_2.user)).posts)
|
|
|
|
.to eq([])
|
|
|
|
|
|
|
|
expect(Search.execute('longan', guardian: Guardian.new(post_2.user)).posts.sort)
|
|
|
|
.to eq([post, post_2])
|
|
|
|
|
|
|
|
expect(Search.execute('longan in:unseen', guardian: Guardian.new(post_2.user)).posts.sort)
|
|
|
|
.to eq([post, post_2])
|
|
|
|
|
|
|
|
expect(Search.execute('longan in:unseen', guardian: Guardian.new(post.user)).posts)
|
|
|
|
.to eq([post_2])
|
2016-05-13 16:26:53 +08:00
|
|
|
end
|
|
|
|
|
2016-05-12 16:43:44 +08:00
|
|
|
it 'supports before and after, in:first, user:, @username' do
|
2016-03-14 20:27:02 +08:00
|
|
|
|
|
|
|
time = Time.zone.parse('2001-05-20 2:55')
|
|
|
|
freeze_time(time)
|
|
|
|
|
|
|
|
topic = Fabricate(:topic)
|
|
|
|
Fabricate(:post, raw: 'hi this is a test 123 123', topic: topic, created_at: time.months_ago(2))
|
2015-08-14 09:53:16 +08:00
|
|
|
_post = Fabricate(:post, raw: 'boom boom shake the room', topic: topic)
|
2015-06-23 11:21:50 +08:00
|
|
|
|
2016-03-14 20:27:02 +08:00
|
|
|
expect(Search.execute('test before:1').posts.length).to eq(1)
|
|
|
|
expect(Search.execute('test before:2001-04-20').posts.length).to eq(1)
|
|
|
|
expect(Search.execute('test before:2001').posts.length).to eq(0)
|
|
|
|
expect(Search.execute('test before:monday').posts.length).to eq(1)
|
|
|
|
|
|
|
|
expect(Search.execute('test after:jan').posts.length).to eq(1)
|
2015-06-23 11:21:50 +08:00
|
|
|
|
2015-06-23 11:39:40 +08:00
|
|
|
expect(Search.execute('test in:first').posts.length).to eq(1)
|
|
|
|
expect(Search.execute('boom').posts.length).to eq(1)
|
|
|
|
expect(Search.execute('boom in:first').posts.length).to eq(0)
|
|
|
|
|
2015-08-14 09:53:16 +08:00
|
|
|
expect(Search.execute('user:nobody').posts.length).to eq(0)
|
|
|
|
expect(Search.execute("user:#{_post.user.username}").posts.length).to eq(1)
|
2015-11-19 04:06:59 +08:00
|
|
|
expect(Search.execute("user:#{_post.user_id}").posts.length).to eq(1)
|
2016-05-12 16:43:44 +08:00
|
|
|
|
|
|
|
expect(Search.execute("@#{_post.user.username}").posts.length).to eq(1)
|
2015-06-23 11:21:50 +08:00
|
|
|
end
|
|
|
|
|
2015-09-15 15:39:14 +08:00
|
|
|
it 'supports group' do
|
|
|
|
topic = Fabricate(:topic, created_at: 3.months.ago)
|
|
|
|
post = Fabricate(:post, raw: 'hi this is a test 123 123', topic: topic)
|
|
|
|
|
|
|
|
group = Group.create!(name: "Like_a_Boss")
|
|
|
|
GroupUser.create!(user_id: post.user_id, group_id: group.id)
|
|
|
|
|
|
|
|
expect(Search.execute('group:like_a_boss').posts.length).to eq(1)
|
|
|
|
expect(Search.execute('group:"like a brick"').posts.length).to eq(0)
|
|
|
|
end
|
|
|
|
|
2015-09-15 16:47:27 +08:00
|
|
|
it 'supports badge' do
|
2015-09-15 15:21:46 +08:00
|
|
|
|
|
|
|
topic = Fabricate(:topic, created_at: 3.months.ago)
|
|
|
|
post = Fabricate(:post, raw: 'hi this is a test 123 123', topic: topic)
|
|
|
|
|
|
|
|
badge = Badge.create!(name: "Like a Boss", badge_type_id: 1)
|
|
|
|
UserBadge.create!(user_id: post.user_id, badge_id: badge.id, granted_at: 1.minute.ago, granted_by_id: -1)
|
|
|
|
|
2015-09-15 16:47:27 +08:00
|
|
|
expect(Search.execute('badge:"like a boss"').posts.length).to eq(1)
|
|
|
|
expect(Search.execute('badge:"test"').posts.length).to eq(0)
|
2015-09-15 15:21:46 +08:00
|
|
|
end
|
|
|
|
|
2015-08-10 15:41:14 +08:00
|
|
|
it 'can search numbers correctly, and match exact phrases' do
|
|
|
|
topic = Fabricate(:topic, created_at: 3.months.ago)
|
|
|
|
Fabricate(:post, raw: '3.0 eta is in 2 days horrah', topic: topic)
|
|
|
|
|
|
|
|
expect(Search.execute('3.0 eta').posts.length).to eq(1)
|
|
|
|
expect(Search.execute('"3.0, eta is"').posts.length).to eq(0)
|
|
|
|
end
|
|
|
|
|
2014-09-03 19:54:10 +08:00
|
|
|
it 'can find by status' do
|
|
|
|
post = Fabricate(:post, raw: 'hi this is a test 123 123')
|
|
|
|
topic = post.topic
|
|
|
|
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(Search.execute('test status:closed').posts.length).to eq(0)
|
|
|
|
expect(Search.execute('test status:open').posts.length).to eq(1)
|
2015-06-23 10:14:06 +08:00
|
|
|
expect(Search.execute('test posts_count:1').posts.length).to eq(1)
|
2016-11-28 22:57:18 +08:00
|
|
|
expect(Search.execute('test min_post_count:1').posts.length).to eq(1)
|
2014-09-03 19:54:10 +08:00
|
|
|
|
|
|
|
topic.closed = true
|
|
|
|
topic.save
|
|
|
|
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(Search.execute('test status:closed').posts.length).to eq(1)
|
2015-06-23 10:14:06 +08:00
|
|
|
expect(Search.execute('status:closed').posts.length).to eq(1)
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(Search.execute('test status:open').posts.length).to eq(0)
|
2014-09-03 19:54:10 +08:00
|
|
|
|
|
|
|
topic.archived = true
|
|
|
|
topic.closed = false
|
|
|
|
topic.save
|
|
|
|
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(Search.execute('test status:archived').posts.length).to eq(1)
|
|
|
|
expect(Search.execute('test status:open').posts.length).to eq(0)
|
2014-10-18 11:54:11 +08:00
|
|
|
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(Search.execute('test status:noreplies').posts.length).to eq(1)
|
2014-10-18 11:54:11 +08:00
|
|
|
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(Search.execute('test in:likes', guardian: Guardian.new(topic.user)).posts.length).to eq(0)
|
2014-10-18 12:19:08 +08:00
|
|
|
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(Search.execute('test in:posted', guardian: Guardian.new(topic.user)).posts.length).to eq(1)
|
2014-10-18 12:19:08 +08:00
|
|
|
|
2014-10-18 12:34:05 +08:00
|
|
|
TopicUser.change(topic.user.id, topic.id, notification_level: TopicUser.notification_levels[:tracking])
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(Search.execute('test in:watching', guardian: Guardian.new(topic.user)).posts.length).to eq(0)
|
|
|
|
expect(Search.execute('test in:tracking', guardian: Guardian.new(topic.user)).posts.length).to eq(1)
|
2014-10-18 12:34:05 +08:00
|
|
|
|
2014-09-03 20:10:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can find by latest' do
|
|
|
|
topic1 = Fabricate(:topic, title: 'I do not like that Sam I am')
|
|
|
|
post1 = Fabricate(:post, topic: topic1)
|
|
|
|
|
|
|
|
post2 = Fabricate(:post, raw: 'that Sam I am, that Sam I am')
|
|
|
|
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(Search.execute('sam').posts.map(&:id)).to eq([post1.id, post2.id])
|
|
|
|
expect(Search.execute('sam order:latest').posts.map(&:id)).to eq([post2.id, post1.id])
|
2017-05-24 23:24:41 +08:00
|
|
|
expect(Search.execute('sam l').posts.map(&:id)).to eq([post2.id, post1.id])
|
|
|
|
expect(Search.execute('l sam').posts.map(&:id)).to eq([post2.id, post1.id])
|
2017-03-30 01:33:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can order by topic creation' do
|
|
|
|
today = Date.today
|
|
|
|
yesterday = 1.day.ago
|
|
|
|
two_days_ago = 2.days.ago
|
|
|
|
|
|
|
|
old_topic = Fabricate(:topic,
|
|
|
|
title: 'First Topic, testing the created_at sort',
|
|
|
|
created_at: two_days_ago)
|
|
|
|
latest_topic = Fabricate(:topic,
|
|
|
|
title: 'Second Topic, testing the created_at sort',
|
|
|
|
created_at: yesterday)
|
|
|
|
|
|
|
|
old_relevant_topic_post = Fabricate(:post, topic: old_topic, created_at: yesterday, raw: 'Relevant Topic')
|
|
|
|
latest_irelevant_topic_post = Fabricate(:post, topic: latest_topic, created_at: today, raw: 'Not Relevant')
|
|
|
|
|
|
|
|
# Expecting the default results
|
|
|
|
expect(Search.execute('Topic').posts.map(&:id)).to eq([old_relevant_topic_post.id, latest_irelevant_topic_post.id])
|
2014-09-03 19:54:10 +08:00
|
|
|
|
2017-03-30 01:33:23 +08:00
|
|
|
# Expecting the ordered by topic creation results
|
|
|
|
expect(Search.execute('Topic order:latest_topic').posts.map(&:id)).to eq([latest_irelevant_topic_post.id, old_relevant_topic_post.id])
|
2014-09-03 19:54:10 +08:00
|
|
|
end
|
2016-05-11 17:53:54 +08:00
|
|
|
|
2016-07-25 14:26:33 +08:00
|
|
|
it 'can tokenize dots' do
|
|
|
|
post = Fabricate(:post, raw: 'Will.2000 Will.Bob.Bill...')
|
|
|
|
expect(Search.execute('bill').posts.map(&:id)).to eq([post.id])
|
|
|
|
end
|
|
|
|
|
2016-06-02 20:41:24 +08:00
|
|
|
it 'supports category slug and tags' do
|
2016-05-11 17:53:54 +08:00
|
|
|
# main category
|
|
|
|
category = Fabricate(:category, name: 'category 24', slug: 'category-24')
|
|
|
|
topic = Fabricate(:topic, created_at: 3.months.ago, category: category)
|
2017-02-08 04:53:37 +08:00
|
|
|
post = Fabricate(:post, raw: 'Sams first post', topic: topic)
|
2016-05-11 17:53:54 +08:00
|
|
|
|
2017-02-08 04:53:37 +08:00
|
|
|
expect(Search.execute('sams post #category-24').posts.length).to eq(1)
|
|
|
|
expect(Search.execute("sams post category:#{category.id}").posts.length).to eq(1)
|
|
|
|
expect(Search.execute('sams post #category-25').posts.length).to eq(0)
|
2016-05-11 17:53:54 +08:00
|
|
|
|
|
|
|
sub_category = Fabricate(:category, name: 'sub category', slug: 'sub-category', parent_category_id: category.id)
|
|
|
|
second_topic = Fabricate(:topic, created_at: 3.months.ago, category: sub_category)
|
2017-02-08 04:53:37 +08:00
|
|
|
Fabricate(:post, raw: 'sams second post', topic: second_topic)
|
2016-05-11 17:53:54 +08:00
|
|
|
|
2017-02-08 04:53:37 +08:00
|
|
|
expect(Search.execute("sams post category:category-24").posts.length).to eq(2)
|
|
|
|
expect(Search.execute("sams post category:=category-24").posts.length).to eq(1)
|
|
|
|
|
|
|
|
expect(Search.execute("sams post #category-24").posts.length).to eq(2)
|
|
|
|
expect(Search.execute("sams post #=category-24").posts.length).to eq(1)
|
|
|
|
expect(Search.execute("sams post #sub-category").posts.length).to eq(1)
|
2016-06-02 20:41:24 +08:00
|
|
|
|
|
|
|
# tags
|
|
|
|
topic.tags = [Fabricate(:tag, name: 'alpha')]
|
|
|
|
expect(Search.execute('this is a test #alpha').posts.map(&:id)).to eq([post.id])
|
|
|
|
expect(Search.execute('this is a test #beta').posts.size).to eq(0)
|
2016-05-11 17:53:54 +08:00
|
|
|
end
|
2016-05-05 02:02:47 +08:00
|
|
|
|
|
|
|
it "can find with tag" do
|
|
|
|
topic1 = Fabricate(:topic, title: 'Could not, would not, on a boat')
|
|
|
|
topic1.tags = [Fabricate(:tag, name: 'eggs'), Fabricate(:tag, name: 'ham')]
|
2016-06-09 01:50:33 +08:00
|
|
|
Fabricate(:post, topic: topic1)
|
2016-05-05 02:02:47 +08:00
|
|
|
post2 = Fabricate(:post, topic: topic1, raw: "It probably doesn't help that they're green...")
|
|
|
|
|
|
|
|
expect(Search.execute('green tags:eggs').posts.map(&:id)).to eq([post2.id])
|
|
|
|
expect(Search.execute('green tags:plants').posts.size).to eq(0)
|
|
|
|
end
|
2017-05-30 19:01:17 +08:00
|
|
|
|
|
|
|
context 'tags' do
|
|
|
|
let(:tag1) { Fabricate(:tag, name: 'lunch') }
|
|
|
|
let(:tag2) { Fabricate(:tag, name: 'eggs') }
|
|
|
|
let(:topic1) { Fabricate(:topic, tags: [tag2, Fabricate(:tag)]) }
|
|
|
|
let(:topic2) { Fabricate(:topic, tags: [tag1]) }
|
|
|
|
let(:topic3) { Fabricate(:topic, tags: [tag1, tag2]) }
|
|
|
|
let!(:post1) { Fabricate(:post, topic: topic1)}
|
|
|
|
let!(:post2) { Fabricate(:post, topic: topic2)}
|
|
|
|
let!(:post3) { Fabricate(:post, topic: topic3)}
|
|
|
|
|
|
|
|
it 'can find posts with any tag from multiple tags' do
|
|
|
|
Fabricate(:post)
|
|
|
|
|
|
|
|
expect(Search.execute('tags:eggs|lunch').posts.map(&:id).sort).to eq([post1.id, post2.id, post3.id].sort)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can find posts which contains all provided tags' do
|
|
|
|
expect(Search.execute('tags:lunch,eggs').posts.map(&:id)).to eq([post3.id])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-03 19:54:10 +08:00
|
|
|
end
|
|
|
|
|
2015-08-13 15:55:10 +08:00
|
|
|
it 'can parse complex strings using ts_query helper' do
|
|
|
|
str = " grigio:babel deprecated? "
|
2015-08-19 07:27:47 +08:00
|
|
|
str << "page page on Atmosphere](https://atmospherejs.com/grigio/babel)xxx: aaa.js:222 aaa'\"bbb"
|
2015-08-13 15:55:10 +08:00
|
|
|
|
|
|
|
ts_query = Search.ts_query(str, "simple")
|
|
|
|
Post.exec_sql("SELECT to_tsvector('bbb') @@ " << ts_query)
|
|
|
|
end
|
|
|
|
|
2016-03-14 20:27:02 +08:00
|
|
|
context '#word_to_date' do
|
|
|
|
it 'parses relative dates correctly' do
|
|
|
|
time = Time.zone.parse('2001-02-20 2:55')
|
|
|
|
freeze_time(time)
|
|
|
|
|
|
|
|
expect(Search.word_to_date('yesterday')).to eq(time.beginning_of_day.yesterday)
|
|
|
|
expect(Search.word_to_date('suNday')).to eq(Time.zone.parse('2001-02-18'))
|
|
|
|
expect(Search.word_to_date('thursday')).to eq(Time.zone.parse('2001-02-15'))
|
|
|
|
expect(Search.word_to_date('deCember')).to eq(Time.zone.parse('2000-12-01'))
|
|
|
|
expect(Search.word_to_date('deC')).to eq(Time.zone.parse('2000-12-01'))
|
|
|
|
expect(Search.word_to_date('january')).to eq(Time.zone.parse('2001-01-01'))
|
|
|
|
expect(Search.word_to_date('jan')).to eq(Time.zone.parse('2001-01-01'))
|
|
|
|
|
|
|
|
|
|
|
|
expect(Search.word_to_date('100')).to eq(time.beginning_of_day.days_ago(100))
|
|
|
|
|
|
|
|
expect(Search.word_to_date('invalid')).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'parses absolute dates correctly' do
|
|
|
|
expect(Search.word_to_date('2001-1-20')).to eq(Time.zone.parse('2001-01-20'))
|
|
|
|
expect(Search.word_to_date('2030-10-2')).to eq(Time.zone.parse('2030-10-02'))
|
|
|
|
expect(Search.word_to_date('2030-10')).to eq(Time.zone.parse('2030-10-01'))
|
|
|
|
expect(Search.word_to_date('2030')).to eq(Time.zone.parse('2030-01-01'))
|
|
|
|
expect(Search.word_to_date('2030-01-32')).to eq(nil)
|
|
|
|
expect(Search.word_to_date('10000')).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-11 03:40:58 +08:00
|
|
|
context "#min_post_id" do
|
|
|
|
it "returns 0 when prefer_recent_posts is disabled" do
|
|
|
|
SiteSetting.search_prefer_recent_posts = false
|
|
|
|
expect(Search.min_post_id_no_cache).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a value when prefer_recent_posts is enabled" do
|
|
|
|
SiteSetting.search_prefer_recent_posts = true
|
|
|
|
SiteSetting.search_recent_posts_size = 1
|
|
|
|
|
|
|
|
Fabricate(:post)
|
|
|
|
p2 = Fabricate(:post)
|
|
|
|
|
|
|
|
expect(Search.min_post_id_no_cache).to eq(p2.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|