2013-02-28 14:52:35 -05:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
require 'spec_helper'
|
2013-08-01 20:31:36 -04:00
|
|
|
require_dependency 'search'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
describe Search do
|
|
|
|
|
2014-04-11 16:07:39 -04:00
|
|
|
class TextHelper
|
|
|
|
extend ActionView::Helpers::TextHelper
|
|
|
|
end
|
|
|
|
|
2013-05-13 21:59:55 -04:00
|
|
|
before do
|
|
|
|
ActiveRecord::Base.observers.enable :search_observer
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
context 'post indexing observer' do
|
|
|
|
before do
|
2013-02-05 14:16:51 -05:00
|
|
|
@category = Fabricate(:category, name: 'america')
|
2013-03-11 09:51:24 -04:00
|
|
|
@topic = Fabricate(:topic, title: 'sam saffron test topic', category: @category)
|
2013-02-05 14:16:51 -05:00
|
|
|
@post = Fabricate(:post, topic: @topic, raw: 'this <b>fun test</b> <img src="bla" title="my image">')
|
2013-05-22 15:33:33 -04:00
|
|
|
@indexed = @post.post_search_data.search_data
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2014-09-01 03:04:40 -04:00
|
|
|
|
|
|
|
it "should index correctly" do
|
2013-02-05 14:16:51 -05:00
|
|
|
@indexed.should =~ /fun/
|
|
|
|
@indexed.should =~ /sam/
|
|
|
|
@indexed.should =~ /america/
|
|
|
|
|
2013-02-06 20:09:31 -05:00
|
|
|
@topic.title = "harpi is the new title"
|
2013-02-05 14:16:51 -05:00
|
|
|
@topic.save!
|
2013-05-22 15:33:33 -04:00
|
|
|
@post.post_search_data.reload
|
|
|
|
|
|
|
|
@indexed = @post.post_search_data.search_data
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
@indexed.should =~ /harpi/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
context 'user indexing observer' do
|
|
|
|
before do
|
2013-02-05 14:16:51 -05:00
|
|
|
@user = Fabricate(:user, username: 'fred', name: 'bob jones')
|
2013-05-22 15:33:33 -04:00
|
|
|
@indexed = @user.user_search_data.search_data
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
it "should pick up on data" do
|
2013-02-05 14:16:51 -05:00
|
|
|
@indexed.should =~ /fred/
|
|
|
|
@indexed.should =~ /jone/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
context 'category indexing observer' do
|
|
|
|
before do
|
2013-02-05 14:16:51 -05:00
|
|
|
@category = Fabricate(:category, name: 'america')
|
2013-05-22 15:33:33 -04:00
|
|
|
@indexed = @category.category_search_data.search_data
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "should pick up on name" do
|
2013-02-05 14:16:51 -05:00
|
|
|
@indexed.should =~ /america/
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-03-07 10:52:01 -05:00
|
|
|
it 'does not search when the search term is too small' do
|
|
|
|
ActiveRecord::Base.expects(:exec_sql).never
|
2014-09-02 05:15:08 -04:00
|
|
|
Search.execute('evil', min_search_term_length: 5)
|
2013-03-07 10:52:01 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
it 'escapes non alphanumeric characters' do
|
2014-09-02 05:15:08 -04:00
|
|
|
Search.execute('foo :!$);}]>@\#\"\'').posts.length.should == 0 # There are at least three levels of sanitation for Search.query!
|
2013-08-26 16:25:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't raise an error when single quotes are present" do
|
2014-09-02 05:15:08 -04:00
|
|
|
Search.execute("'hello' world").posts.length.should == 0 # There are at least three levels of sanitation for Search.query!
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'works when given two terms with spaces' do
|
2014-09-02 05:15:08 -04:00
|
|
|
lambda { Search.execute('evil trout') }.should_not raise_error
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'users' do
|
|
|
|
let!(:user) { Fabricate(:user) }
|
2014-09-02 05:15:08 -04:00
|
|
|
let(:result) { Search.execute('bruce', type_filter: 'user') }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it 'returns a result' do
|
2014-09-02 05:15:08 -04:00
|
|
|
result.users.length.should == 1
|
|
|
|
result.users[0].id.should == user.id
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'topics' do
|
2014-09-02 05:15:08 -04:00
|
|
|
let(:post) { Fabricate(:post) }
|
|
|
|
let(:topic) { post.topic}
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-02-16 21:54:51 -05: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-18 16:59:18 -05:00
|
|
|
|
2014-02-16 21:54:51 -05:00
|
|
|
topic = Fabricate(:topic)
|
|
|
|
topic2 = Fabricate(:topic)
|
|
|
|
|
|
|
|
new_post('this is the other post I am posting', topic2)
|
2014-06-20 01:48:34 -04:00
|
|
|
new_post('this is my fifth post I am posting', topic2)
|
|
|
|
|
2014-02-16 21:54:51 -05: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-18 16:59:18 -05:00
|
|
|
# update posts_count
|
|
|
|
topic.reload
|
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
results = Search.execute('posting', search_context: post1.topic)
|
|
|
|
results.posts.map(&:id).should == [post1.id, post2.id, post3.id, post4.id]
|
2014-02-16 21:54:51 -05:00
|
|
|
|
2014-06-20 01:48:34 -04:00
|
|
|
# stop words should work
|
2014-09-02 05:15:08 -04:00
|
|
|
results = Search.execute('this', search_context: post1.topic)
|
|
|
|
results.posts.length.should == 4
|
2014-06-20 01:48:34 -04:00
|
|
|
|
2014-02-16 21:54:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
context 'searching the OP' do
|
2014-09-02 05:15:08 -04:00
|
|
|
let!(:post) { Fabricate(:post_with_long_raw_content) }
|
|
|
|
let(:result) { Search.execute('hundred', type_filter: 'topic', include_blurbs: true) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-12 20:48:32 -04:00
|
|
|
it 'returns a result correctly' do
|
2014-09-02 05:15:08 -04:00
|
|
|
result.posts.length.should == 1
|
|
|
|
result.posts[0].id.should == post.id
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-05-13 17:04:41 -04:00
|
|
|
end
|
|
|
|
|
2013-06-28 21:22:17 -04:00
|
|
|
context 'searching for a post' do
|
|
|
|
let!(:reply) { Fabricate(:basic_reply, topic: topic, user: topic.user) }
|
2014-09-02 05:15:08 -04:00
|
|
|
let(:result) { Search.execute('quotes', type_filter: 'topic', include_blurbs: true) }
|
2013-06-28 21:22:17 -04:00
|
|
|
|
|
|
|
it 'returns the post' do
|
|
|
|
result.should be_present
|
2014-09-02 05:15:08 -04:00
|
|
|
result.posts.length.should == 1
|
|
|
|
p = result.posts[0]
|
|
|
|
p.topic.id.should == topic.id
|
|
|
|
p.id.should == reply.id
|
2014-09-04 01:01:01 -04:00
|
|
|
result.blurb(p).should == "this reply has no quotes"
|
2013-06-28 21:22:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-28 15:42:29 -04:00
|
|
|
context "search for a topic by id" do
|
2014-09-02 05:15:08 -04:00
|
|
|
let(:result) { Search.execute(topic.id, type_filter: 'topic', search_for_id: true, min_search_term_length: 1) }
|
2014-08-28 15:42:29 -04:00
|
|
|
|
|
|
|
it 'returns the topic' do
|
2014-09-02 05:15:08 -04:00
|
|
|
result.posts.length.should == 1
|
|
|
|
result.posts.first.id.should == post.id
|
2014-08-28 15:42:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-13 17:04:41 -04:00
|
|
|
context "search for a topic by url" do
|
2014-09-02 05:15:08 -04:00
|
|
|
let(:result) { Search.execute(topic.relative_url, search_for_id: true, type_filter: 'topic')}
|
2013-05-13 17:04:41 -04:00
|
|
|
|
|
|
|
it 'returns the topic' do
|
2014-09-02 05:15:08 -04:00
|
|
|
result.posts.length.should == 1
|
|
|
|
result.posts.first.id.should == post.id
|
2013-05-13 17:04:41 -04:00
|
|
|
end
|
2013-05-12 20:48:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'security' do
|
2013-06-28 21:22:17 -04:00
|
|
|
|
2013-05-12 20:48:32 -04:00
|
|
|
def result(current_user)
|
2014-10-18 00:19:08 -04:00
|
|
|
Search.execute('hello', guardian: Guardian.new(current_user))
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-05-12 20:48:32 -04:00
|
|
|
it 'secures results correctly' do
|
|
|
|
category = Fabricate(:category)
|
|
|
|
|
|
|
|
topic.category_id = category.id
|
|
|
|
topic.save
|
|
|
|
|
2013-07-13 21:24:16 -04:00
|
|
|
category.set_permissions(:staff => :full)
|
2013-05-12 20:48:32 -04:00
|
|
|
category.save
|
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
result(nil).posts.should_not be_present
|
|
|
|
result(Fabricate(:user)).posts.should_not be_present
|
|
|
|
result(Fabricate(:admin)).posts.should be_present
|
2013-05-12 20:48:32 -04:00
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-28 14:52:35 -05: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 05:15:08 -04:00
|
|
|
let(:result) { Search.execute('запись') }
|
2013-02-28 14:52:35 -05:00
|
|
|
|
|
|
|
it 'finds something when given cyrillic query' do
|
2014-09-02 05:15:08 -04:00
|
|
|
result.posts.should be_present
|
2013-02-28 14:52:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context 'categories' do
|
|
|
|
|
|
|
|
let!(:category) { Fabricate(:category) }
|
2014-09-02 05:15:08 -04:00
|
|
|
def search
|
|
|
|
Search.execute(category.name)
|
2013-05-13 04:04:03 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-12 20:48:32 -04:00
|
|
|
it 'returns the correct result' do
|
2014-09-02 05:15:08 -04:00
|
|
|
search.categories.should be_present
|
2013-05-13 04:04:03 -04:00
|
|
|
|
2013-07-13 21:24:16 -04:00
|
|
|
category.set_permissions({})
|
2013-05-13 04:04:03 -04:00
|
|
|
category.save
|
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
search.categories.should_not be_present
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05: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-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
context 'user filter' do
|
2014-09-02 05:15:08 -04:00
|
|
|
let(:results) { Search.execute('amazing', type_filter: 'user') }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it "returns a user result" do
|
2014-09-02 05:15:08 -04:00
|
|
|
results.categories.length.should == 0
|
|
|
|
results.posts.length.should == 0
|
|
|
|
results.users.length.should == 1
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'category filter' do
|
2014-09-02 05:15:08 -04:00
|
|
|
let(:results) { Search.execute('amazing', type_filter: 'category') }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-21 04:56:04 -04:00
|
|
|
it "returns a category result" do
|
2014-09-02 05:15:08 -04:00
|
|
|
results.categories.length.should == 1
|
|
|
|
results.posts.length.should == 0
|
|
|
|
results.users.length.should == 0
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-05-24 14:03:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'search_context' do
|
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
it 'can find a user when using search context' do
|
|
|
|
|
|
|
|
coding_horror = Fabricate(:coding_horror)
|
|
|
|
post = Fabricate(:post)
|
2013-05-24 14:03:45 -04:00
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
Fabricate(:post, user: coding_horror)
|
2013-05-24 16:17:09 -04:00
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
result = Search.execute('hello', search_context: post.user)
|
|
|
|
|
|
|
|
result.posts.first.topic_id = post.topic_id
|
|
|
|
result.posts.length.should == 1
|
2013-05-24 14:03:45 -04:00
|
|
|
end
|
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
it 'can use category as a search context' do
|
|
|
|
category = Fabricate(:category)
|
|
|
|
topic = Fabricate(:topic, category: category)
|
|
|
|
topic_no_cat = Fabricate(:topic)
|
|
|
|
|
|
|
|
post = Fabricate(:post, topic: topic, user: topic.user )
|
|
|
|
_another_post = Fabricate(:post, topic: topic_no_cat, user: topic.user )
|
2013-05-24 14:03:45 -04:00
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
search = Search.execute('hello', search_context: category)
|
|
|
|
search.posts.length.should == 1
|
|
|
|
search.posts.first.id.should == post.id
|
2013-05-24 14:03:45 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-06-25 19:58:49 -04:00
|
|
|
describe 'Chinese search' do
|
|
|
|
it 'splits English / Chinese' do
|
|
|
|
SiteSetting.default_locale = 'zh_CN'
|
|
|
|
data = Search.prepare_data('Discourse社区指南').split(' ')
|
|
|
|
data.should == ['Discourse', '社区','指南']
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'finds chinese topic based on title' do
|
|
|
|
SiteSetting.default_locale = 'zh_TW'
|
|
|
|
topic = Fabricate(:topic, title: 'My Title Discourse社区指南')
|
2014-09-02 05:15:08 -04:00
|
|
|
post = Fabricate(:post, topic: topic)
|
2014-06-25 19:58:49 -04:00
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
Search.execute('社区指南').posts.first.id.should == post.id
|
|
|
|
Search.execute('指南').posts.first.id.should == post.id
|
2014-06-25 19:58:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-03 07:54:10 -04:00
|
|
|
describe 'Advanced search' do
|
|
|
|
it 'can find by status' do
|
|
|
|
post = Fabricate(:post, raw: 'hi this is a test 123 123')
|
|
|
|
topic = post.topic
|
|
|
|
|
|
|
|
Search.execute('test status:closed').posts.length.should == 0
|
|
|
|
Search.execute('test status:open').posts.length.should == 1
|
|
|
|
|
|
|
|
topic.closed = true
|
|
|
|
topic.save
|
|
|
|
|
|
|
|
Search.execute('test status:closed').posts.length.should == 1
|
|
|
|
Search.execute('test status:open').posts.length.should == 0
|
|
|
|
|
|
|
|
topic.archived = true
|
|
|
|
topic.closed = false
|
|
|
|
topic.save
|
|
|
|
|
2014-10-17 23:54:11 -04:00
|
|
|
Search.execute('test status:archived').posts.length.should == 1
|
2014-09-03 07:54:10 -04:00
|
|
|
Search.execute('test status:open').posts.length.should == 0
|
2014-10-17 23:54:11 -04:00
|
|
|
|
|
|
|
Search.execute('test status:noreplies').posts.length.should == 1
|
|
|
|
|
2014-10-18 00:19:08 -04:00
|
|
|
Search.execute('test in:likes', guardian: Guardian.new(topic.user)).posts.length.should == 0
|
|
|
|
|
|
|
|
Search.execute('test in:posted', guardian: Guardian.new(topic.user)).posts.length.should == 1
|
|
|
|
|
2014-10-18 00:34:05 -04:00
|
|
|
TopicUser.change(topic.user.id, topic.id, notification_level: TopicUser.notification_levels[:tracking])
|
|
|
|
Search.execute('test in:watching', guardian: Guardian.new(topic.user)).posts.length.should == 0
|
|
|
|
Search.execute('test in:tracking', guardian: Guardian.new(topic.user)).posts.length.should == 1
|
|
|
|
|
2014-09-03 08:10:18 -04: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')
|
|
|
|
|
|
|
|
Search.execute('sam').posts.map(&:id).should == [post1.id, post2.id]
|
|
|
|
Search.execute('sam order:latest').posts.map(&:id).should == [post2.id, post1.id]
|
2014-09-03 07:54:10 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|