2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2018-07-16 05:39:24 +00:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class ElasticsearchActiveTest < ActiveSupport::TestCase
|
|
|
|
include SearchindexHelper
|
|
|
|
|
2021-07-09 13:05:05 +00:00
|
|
|
setup do
|
2018-07-16 05:39:24 +00:00
|
|
|
|
|
|
|
configure_elasticsearch(required: true)
|
|
|
|
|
|
|
|
rebuild_searchindex
|
|
|
|
|
2019-09-02 11:52:11 +00:00
|
|
|
roles = Role.where(name: 'Agent')
|
2018-07-16 05:39:24 +00:00
|
|
|
|
|
|
|
@agent = User.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
login: 'es-agent@example.com',
|
|
|
|
firstname: 'E',
|
|
|
|
lastname: 'S',
|
|
|
|
email: 'es-agent@example.com',
|
|
|
|
password: 'agentpw',
|
|
|
|
active: true,
|
|
|
|
roles: roles,
|
2018-07-16 05:39:24 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
|
|
|
|
roles = Role.where(name: 'Customer')
|
|
|
|
|
|
|
|
(1..6).each do |i|
|
|
|
|
name = i.even? ? "Active-#{i}" : "Inactive-#{i}"
|
|
|
|
User.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
login: "#{name}-customer#{i}@example.com",
|
|
|
|
firstname: 'ActiveTest',
|
|
|
|
lastname: name,
|
|
|
|
active: i.even?,
|
|
|
|
roles: roles,
|
2018-07-16 05:39:24 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
Organization.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: "TestOrg-#{name}",
|
|
|
|
active: i.even?,
|
2018-07-16 05:39:24 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
travel 10.seconds
|
|
|
|
end
|
|
|
|
|
|
|
|
# execute background jobs to index created/changed objects
|
|
|
|
Scheduler.worker(true)
|
|
|
|
sleep 2 # for ES to come ready/indexed
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'active users appear before inactive users in search results' do
|
|
|
|
result = User.search(
|
|
|
|
current_user: @agent,
|
2018-12-19 17:31:51 +00:00
|
|
|
query: 'ActiveTest',
|
|
|
|
limit: 15,
|
2018-07-16 05:39:24 +00:00
|
|
|
)
|
|
|
|
assert(result.present?, 'result should not be empty')
|
|
|
|
|
|
|
|
names = result.map(&:lastname)
|
2018-07-20 13:20:21 +00:00
|
|
|
correct_names = %w[Active-2 Active-4 Active-6 Inactive-1 Inactive-3 Inactive-5]
|
|
|
|
assert_equal(correct_names[0, 3], names[0, 3].sort)
|
|
|
|
assert_equal(correct_names[3, 6], names[3, 6].sort)
|
2018-07-16 05:39:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'active organizations appear before inactive organizations in search results' do
|
|
|
|
result = Organization.search(
|
|
|
|
current_user: @agent,
|
2018-12-19 17:31:51 +00:00
|
|
|
query: 'TestOrg',
|
|
|
|
limit: 15,
|
2018-07-16 05:39:24 +00:00
|
|
|
)
|
|
|
|
assert(result.present?, 'result should not be empty')
|
|
|
|
|
|
|
|
names = result.map(&:name)
|
|
|
|
correct_names = %w[TestOrg-Active-2
|
|
|
|
TestOrg-Active-4
|
|
|
|
TestOrg-Active-6
|
|
|
|
TestOrg-Inactive-1
|
|
|
|
TestOrg-Inactive-3
|
|
|
|
TestOrg-Inactive-5]
|
2018-07-20 13:20:21 +00:00
|
|
|
assert_equal(correct_names[0, 3], names[0, 3].sort)
|
|
|
|
assert_equal(correct_names[3, 6], names[3, 6].sort)
|
2018-07-16 05:39:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'ordering of tickets are not affected by the lack of active flags' do
|
|
|
|
ticket_setup
|
|
|
|
|
|
|
|
result = Ticket.search(
|
|
|
|
current_user: User.find(1),
|
2018-12-19 17:31:51 +00:00
|
|
|
query: 'ticket',
|
|
|
|
limit: 15,
|
2018-07-16 05:39:24 +00:00
|
|
|
)
|
|
|
|
assert(result.present?, 'result should not be empty')
|
|
|
|
|
|
|
|
names = result.map(&:title)
|
|
|
|
correct_names = %w[Ticket-6 Ticket-5 Ticket-4 Ticket-3 Ticket-2 Ticket-1]
|
|
|
|
assert_equal(correct_names, names)
|
|
|
|
end
|
|
|
|
|
|
|
|
def ticket_setup
|
|
|
|
result = Ticket.search(
|
|
|
|
current_user: User.find(1),
|
2018-12-19 17:31:51 +00:00
|
|
|
query: 'ticket',
|
|
|
|
limit: 15,
|
2018-07-16 05:39:24 +00:00
|
|
|
)
|
|
|
|
return if result.present?
|
|
|
|
|
|
|
|
(1..6).each do |i|
|
|
|
|
Ticket.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
title: "Ticket-#{i}",
|
|
|
|
group: Group.lookup(name: 'Users'),
|
|
|
|
customer_id: 1,
|
|
|
|
state: Ticket::State.lookup(name: 'new'),
|
|
|
|
priority: Ticket::Priority.lookup(name: '2 normal'),
|
2018-07-16 05:39:24 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
travel 10.seconds
|
|
|
|
end
|
|
|
|
|
|
|
|
# execute background jobs to index created/changed objects
|
|
|
|
Scheduler.worker(true)
|
|
|
|
sleep 2 # for ES to come ready/indexed
|
|
|
|
end
|
|
|
|
end
|