2013-02-07 19:50:59 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe UserSearch do
|
|
|
|
|
|
|
|
let(:topic) { Fabricate :topic }
|
|
|
|
let(:topic2) { Fabricate :topic }
|
|
|
|
let(:topic3) { Fabricate :topic }
|
|
|
|
let(:user1) { Fabricate :user, username: "mrblonde", name: "Michael Madsen" }
|
|
|
|
let(:user2) { Fabricate :user, username: "mrblue", name: "Eddie Bunker" }
|
|
|
|
let(:user3) { Fabricate :user, username: "mrorange", name: "Tim Roth" }
|
|
|
|
let(:user4) { Fabricate :user, username: "mrpink", name: "Steve Buscemi" }
|
|
|
|
let(:user5) { Fabricate :user, username: "mrbrown", name: "Quentin Tarantino" }
|
|
|
|
let(:user6) { Fabricate :user, username: "mrwhite", name: "Harvey Keitel" }
|
|
|
|
|
|
|
|
before do
|
|
|
|
Fabricate :post, user: user1, topic: topic
|
|
|
|
Fabricate :post, user: user2, topic: topic2
|
|
|
|
Fabricate :post, user: user3, topic: topic
|
|
|
|
Fabricate :post, user: user4, topic: topic
|
|
|
|
Fabricate :post, user: user5, topic: topic3
|
|
|
|
Fabricate :post, user: user6, topic: topic
|
|
|
|
end
|
|
|
|
|
2013-10-31 03:45:13 +08:00
|
|
|
def search_for(*args)
|
|
|
|
UserSearch.new(*args).search
|
|
|
|
end
|
|
|
|
|
2013-04-22 09:05:54 +08:00
|
|
|
# this is a seriously expensive integration test, re-creating this entire test db is too expensive
|
|
|
|
# reuse
|
|
|
|
it "operates correctly" do
|
|
|
|
# normal search
|
2013-10-31 03:45:13 +08:00
|
|
|
results = search_for(user1.name.split(" ").first)
|
2013-04-22 09:05:54 +08:00
|
|
|
results.size.should == 1
|
|
|
|
results.first.should == user1
|
|
|
|
|
|
|
|
# lower case
|
2013-10-31 03:45:13 +08:00
|
|
|
results = search_for(user1.name.split(" ").first.downcase)
|
2013-04-22 09:05:54 +08:00
|
|
|
results.size.should == 1
|
|
|
|
results.first.should == user1
|
|
|
|
|
|
|
|
# username
|
2013-10-31 03:45:13 +08:00
|
|
|
results = search_for(user4.username)
|
2013-04-22 09:05:54 +08:00
|
|
|
results.size.should == 1
|
|
|
|
results.first.should == user4
|
|
|
|
|
|
|
|
# case insensitive
|
2013-10-31 03:45:13 +08:00
|
|
|
results = search_for(user4.username.upcase)
|
2013-04-22 09:05:54 +08:00
|
|
|
results.size.should == 1
|
|
|
|
results.first.should == user4
|
|
|
|
|
|
|
|
# substrings
|
2013-10-31 03:45:13 +08:00
|
|
|
results = search_for("mr")
|
2013-04-22 09:05:54 +08:00
|
|
|
results.size.should == 6
|
|
|
|
|
2013-10-31 03:45:13 +08:00
|
|
|
results = search_for("mrb")
|
2013-04-22 09:05:54 +08:00
|
|
|
results.size.should == 3
|
|
|
|
|
|
|
|
|
2013-10-31 03:45:13 +08:00
|
|
|
results = search_for("MR")
|
2013-04-22 09:05:54 +08:00
|
|
|
results.size.should == 6
|
|
|
|
|
2013-10-31 03:45:13 +08:00
|
|
|
results = search_for("MRB")
|
2013-04-22 09:05:54 +08:00
|
|
|
results.size.should == 3
|
|
|
|
|
|
|
|
# topic priority
|
2013-10-31 03:45:13 +08:00
|
|
|
results = search_for("mrb", topic.id)
|
2013-04-22 09:05:54 +08:00
|
|
|
results.first.should == user1
|
|
|
|
|
|
|
|
|
2013-10-31 03:45:13 +08:00
|
|
|
results = search_for("mrb", topic2.id)
|
2013-04-22 09:05:54 +08:00
|
|
|
results.first.should == user2
|
2013-02-07 19:50:59 +08:00
|
|
|
|
2013-10-31 03:45:13 +08:00
|
|
|
results = search_for("mrb", topic3.id)
|
2013-04-22 09:05:54 +08:00
|
|
|
results.first.should == user5
|
2013-10-31 03:45:13 +08:00
|
|
|
|
|
|
|
# When searching by name is enabled, it returns the record
|
|
|
|
SiteSetting.stubs(:enable_names).returns(true)
|
|
|
|
results = search_for("Tarantino")
|
|
|
|
results.size.should == 1
|
|
|
|
|
|
|
|
# When searching by name is disabled, it will not return the record
|
|
|
|
SiteSetting.stubs(:enable_names).returns(false)
|
|
|
|
results = search_for("Tarantino")
|
|
|
|
results.size.should == 0
|
|
|
|
|
2013-02-07 19:50:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|