123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
- require 'rails_helper'
- RSpec.describe Ticket::Selector::Sql do
- context 'when relative time range is selected in ticket selector' do
- def get_condition(operator, range)
- {
- 'ticket.created_at' => {
- operator: operator,
- range: range, # minute|hour|day|month|
- value: '10',
- },
- }
- end
- before do
- freeze_time
- end
- it 'calculates proper time interval, when operator is within last relative' do
- condition = get_condition('within last (relative)', 'minute')
- _, bind_params = Ticket.selector2sql(condition)
- expect(bind_params).to eq([10.minutes.ago, Time.zone.now])
- end
- it 'calculates proper time interval, when operator is within next relative' do
- condition = get_condition('within next (relative)', 'hour')
- _, bind_params = Ticket.selector2sql(condition)
- expect(bind_params).to eq([Time.zone.now, 10.hours.from_now])
- end
- it 'calculates proper time interval, when operator is before (relative)' do
- condition = get_condition('before (relative)', 'day')
- _, bind_params = Ticket.selector2sql(condition)
- expect(bind_params).to eq([10.days.ago])
- end
- it 'calculates proper time interval, when operator is after (relative)' do
- condition = get_condition('after (relative)', 'week')
- _, bind_params = Ticket.selector2sql(condition)
- expect(bind_params).to eq([10.weeks.from_now])
- end
- it 'calculates proper time interval, when operator is till (relative)' do
- condition = get_condition('till (relative)', 'month')
- _, bind_params = Ticket.selector2sql(condition)
- expect(bind_params).to eq([10.months.from_now])
- end
- it 'calculates proper time interval, when operator is from (relative)' do
- condition = get_condition('from (relative)', 'year')
- _, bind_params = Ticket.selector2sql(condition)
- expect(bind_params).to eq([10.years.ago])
- end
- context 'when today operator is used' do
- before do
- travel_to '2022-10-11 14:40:00'
- Setting.set('timezone_default', 'Europe/Berlin')
- end
- it 'calculates proper time interval when today operator is used', :aggregate_failures do
- _, bind_params = Ticket.selector2sql({ 'ticket.created_at' => { 'operator' => 'today' } })
- Time.use_zone(Setting.get('timezone_default_sanitized').presence) do
- expect(bind_params[0].to_s).to eq('2022-10-10 22:00:00 UTC')
- expect(bind_params[1].to_s).to eq('2022-10-11 21:59:59 UTC')
- end
- end
- end
- end
- describe 'Expert mode overview not working when using "owner is me" OR "subscribe is me #4547' do
- let(:agent) { create(:agent, groups: [Group.first]) }
- let(:ticket_1) { create(:ticket, owner: agent, group: Group.first) }
- let(:ticket_2) { create(:ticket, group: Group.first) }
- let(:ticket_3) { create(:ticket, owner: agent, group: Group.first) }
- before do
- Ticket.destroy_all
- ticket_1 && ticket_2 && ticket_3
- create(:mention, mentionable: ticket_2, user: agent)
- create(:mention, mentionable: ticket_3, user: agent)
- end
- it 'does return 1 mentioned ticket' do
- condition = {
- operator: 'AND',
- conditions: [
- {
- name: 'ticket.mention_user_ids',
- operator: 'is',
- pre_condition: 'specific',
- value: agent.id,
- }
- ]
- }
- count, = Ticket.selectors(condition, { current_user: agent })
- expect(count).to eq(2)
- end
- it 'does return 1 owned ticket' do
- condition = {
- operator: 'AND',
- conditions: [
- {
- name: 'ticket.owner_id',
- operator: 'is',
- pre_condition: 'specific',
- value: agent.id,
- }
- ]
- }
- count, = Ticket.selectors(condition, { current_user: agent })
- expect(count).to eq(2)
- end
- it 'does return 1 owned & subscribed ticket' do
- condition = {
- operator: 'AND',
- conditions: [
- {
- name: 'ticket.mention_user_ids',
- operator: 'is',
- pre_condition: 'specific',
- value: agent.id,
- },
- {
- name: 'ticket.owner_id',
- operator: 'is',
- pre_condition: 'specific',
- value: agent.id,
- }
- ]
- }
- count, = Ticket.selectors(condition, { current_user: agent })
- expect(count).to eq(1)
- end
- it 'does return 3 owned or subscribed tickets' do
- condition = {
- operator: 'OR',
- conditions: [
- {
- name: 'ticket.mention_user_ids',
- operator: 'is',
- pre_condition: 'specific',
- value: agent.id,
- },
- {
- name: 'ticket.owner_id',
- operator: 'is',
- pre_condition: 'specific',
- value: agent.id,
- }
- ]
- }
- count, = Ticket.selectors(condition, { current_user: agent })
- expect(count).to eq(3)
- end
- end
- describe 'Overviews: "Organization" does not work as a pre-condition in the expert mode #4557' do
- let(:agent) { create(:agent, groups: [Group.first]) }
- let(:organization) { create(:organization) }
- let(:customer_1) { create(:customer) }
- let(:customer_2) { create(:customer, organization: organization) }
- let(:ticket_1) { create(:ticket, customer: customer_1, group: Group.first) }
- let(:ticket_2) { create(:ticket, customer: customer_2, group: Group.first) }
- before do
- Ticket.destroy_all
- ticket_1 && ticket_2
- end
- it 'does return 1 customer ticket without organization' do
- condition = {
- operator: 'AND',
- conditions: [
- {
- name: 'ticket.organization_id',
- operator: 'is',
- pre_condition: 'not_set',
- }
- ]
- }
- count, = Ticket.selectors(condition, { current_user: agent })
- expect(count).to eq(1)
- end
- it 'does return 1 ticket with organization title' do
- condition = {
- operator: 'AND',
- conditions: [
- {
- name: 'organization.name',
- operator: 'is',
- value: organization.name,
- }
- ]
- }
- count, = Ticket.selectors(condition, { current_user: agent })
- expect(count).to eq(1)
- end
- it 'does return 1 ticket with organization and name' do
- condition = {
- operator: 'AND',
- conditions: [
- {
- name: 'ticket.organization_id',
- operator: 'is not',
- pre_condition: 'not_set',
- },
- {
- name: 'organization.name',
- operator: 'is',
- value: organization.name,
- }
- ]
- }
- count, = Ticket.selectors(condition, { current_user: agent })
- expect(count).to eq(1)
- end
- it 'does return 1 ticket without organization OR NO name' do
- condition = {
- operator: 'OR',
- conditions: [
- {
- name: 'ticket.organization_id',
- operator: 'is',
- pre_condition: 'not_set',
- },
- {
- name: 'organization.name',
- operator: 'is not',
- value: organization.name,
- }
- ]
- }
- count, = Ticket.selectors(condition, { current_user: agent })
- expect(count).to eq(1)
- end
- end
- end
|