sql_spec.rb 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Ticket::Selector::Sql do
  4. context 'when relative time range is selected in ticket selector' do
  5. def get_condition(operator, range)
  6. {
  7. 'ticket.created_at' => {
  8. operator: operator,
  9. range: range, # minute|hour|day|month|
  10. value: '10',
  11. },
  12. }
  13. end
  14. before do
  15. freeze_time
  16. end
  17. it 'calculates proper time interval, when operator is within last relative' do
  18. condition = get_condition('within last (relative)', 'minute')
  19. _, bind_params = Ticket.selector2sql(condition)
  20. expect(bind_params).to eq([10.minutes.ago, Time.zone.now])
  21. end
  22. it 'calculates proper time interval, when operator is within next relative' do
  23. condition = get_condition('within next (relative)', 'hour')
  24. _, bind_params = Ticket.selector2sql(condition)
  25. expect(bind_params).to eq([Time.zone.now, 10.hours.from_now])
  26. end
  27. it 'calculates proper time interval, when operator is before (relative)' do
  28. condition = get_condition('before (relative)', 'day')
  29. _, bind_params = Ticket.selector2sql(condition)
  30. expect(bind_params).to eq([10.days.ago])
  31. end
  32. it 'calculates proper time interval, when operator is after (relative)' do
  33. condition = get_condition('after (relative)', 'week')
  34. _, bind_params = Ticket.selector2sql(condition)
  35. expect(bind_params).to eq([10.weeks.from_now])
  36. end
  37. it 'calculates proper time interval, when operator is till (relative)' do
  38. condition = get_condition('till (relative)', 'month')
  39. _, bind_params = Ticket.selector2sql(condition)
  40. expect(bind_params).to eq([10.months.from_now])
  41. end
  42. it 'calculates proper time interval, when operator is from (relative)' do
  43. condition = get_condition('from (relative)', 'year')
  44. _, bind_params = Ticket.selector2sql(condition)
  45. expect(bind_params).to eq([10.years.ago])
  46. end
  47. context 'when today operator is used' do
  48. before do
  49. travel_to '2022-10-11 14:40:00'
  50. Setting.set('timezone_default', 'Europe/Berlin')
  51. end
  52. it 'calculates proper time interval when today operator is used', :aggregate_failures do
  53. _, bind_params = Ticket.selector2sql({ 'ticket.created_at' => { 'operator' => 'today' } })
  54. Time.use_zone(Setting.get('timezone_default_sanitized').presence) do
  55. expect(bind_params[0].to_s).to eq('2022-10-10 22:00:00 UTC')
  56. expect(bind_params[1].to_s).to eq('2022-10-11 21:59:59 UTC')
  57. end
  58. end
  59. end
  60. end
  61. describe 'Expert mode overview not working when using "owner is me" OR "subscribe is me #4547' do
  62. let(:agent) { create(:agent, groups: [Group.first]) }
  63. let(:ticket_1) { create(:ticket, owner: agent, group: Group.first) }
  64. let(:ticket_2) { create(:ticket, group: Group.first) }
  65. let(:ticket_3) { create(:ticket, owner: agent, group: Group.first) }
  66. before do
  67. Ticket.destroy_all
  68. ticket_1 && ticket_2 && ticket_3
  69. create(:mention, mentionable: ticket_2, user: agent)
  70. create(:mention, mentionable: ticket_3, user: agent)
  71. end
  72. it 'does return 1 mentioned ticket' do
  73. condition = {
  74. operator: 'AND',
  75. conditions: [
  76. {
  77. name: 'ticket.mention_user_ids',
  78. operator: 'is',
  79. pre_condition: 'specific',
  80. value: agent.id,
  81. }
  82. ]
  83. }
  84. count, = Ticket.selectors(condition, { current_user: agent })
  85. expect(count).to eq(2)
  86. end
  87. it 'does return 1 owned ticket' do
  88. condition = {
  89. operator: 'AND',
  90. conditions: [
  91. {
  92. name: 'ticket.owner_id',
  93. operator: 'is',
  94. pre_condition: 'specific',
  95. value: agent.id,
  96. }
  97. ]
  98. }
  99. count, = Ticket.selectors(condition, { current_user: agent })
  100. expect(count).to eq(2)
  101. end
  102. it 'does return 1 owned & subscribed ticket' do
  103. condition = {
  104. operator: 'AND',
  105. conditions: [
  106. {
  107. name: 'ticket.mention_user_ids',
  108. operator: 'is',
  109. pre_condition: 'specific',
  110. value: agent.id,
  111. },
  112. {
  113. name: 'ticket.owner_id',
  114. operator: 'is',
  115. pre_condition: 'specific',
  116. value: agent.id,
  117. }
  118. ]
  119. }
  120. count, = Ticket.selectors(condition, { current_user: agent })
  121. expect(count).to eq(1)
  122. end
  123. it 'does return 3 owned or subscribed tickets' do
  124. condition = {
  125. operator: 'OR',
  126. conditions: [
  127. {
  128. name: 'ticket.mention_user_ids',
  129. operator: 'is',
  130. pre_condition: 'specific',
  131. value: agent.id,
  132. },
  133. {
  134. name: 'ticket.owner_id',
  135. operator: 'is',
  136. pre_condition: 'specific',
  137. value: agent.id,
  138. }
  139. ]
  140. }
  141. count, = Ticket.selectors(condition, { current_user: agent })
  142. expect(count).to eq(3)
  143. end
  144. end
  145. describe 'Overviews: "Organization" does not work as a pre-condition in the expert mode #4557' do
  146. let(:agent) { create(:agent, groups: [Group.first]) }
  147. let(:organization) { create(:organization) }
  148. let(:customer_1) { create(:customer) }
  149. let(:customer_2) { create(:customer, organization: organization) }
  150. let(:ticket_1) { create(:ticket, customer: customer_1, group: Group.first) }
  151. let(:ticket_2) { create(:ticket, customer: customer_2, group: Group.first) }
  152. before do
  153. Ticket.destroy_all
  154. ticket_1 && ticket_2
  155. end
  156. it 'does return 1 customer ticket without organization' do
  157. condition = {
  158. operator: 'AND',
  159. conditions: [
  160. {
  161. name: 'ticket.organization_id',
  162. operator: 'is',
  163. pre_condition: 'not_set',
  164. }
  165. ]
  166. }
  167. count, = Ticket.selectors(condition, { current_user: agent })
  168. expect(count).to eq(1)
  169. end
  170. it 'does return 1 ticket with organization title' do
  171. condition = {
  172. operator: 'AND',
  173. conditions: [
  174. {
  175. name: 'organization.name',
  176. operator: 'is',
  177. value: organization.name,
  178. }
  179. ]
  180. }
  181. count, = Ticket.selectors(condition, { current_user: agent })
  182. expect(count).to eq(1)
  183. end
  184. it 'does return 1 ticket with organization and name' do
  185. condition = {
  186. operator: 'AND',
  187. conditions: [
  188. {
  189. name: 'ticket.organization_id',
  190. operator: 'is not',
  191. pre_condition: 'not_set',
  192. },
  193. {
  194. name: 'organization.name',
  195. operator: 'is',
  196. value: organization.name,
  197. }
  198. ]
  199. }
  200. count, = Ticket.selectors(condition, { current_user: agent })
  201. expect(count).to eq(1)
  202. end
  203. it 'does return 1 ticket without organization OR NO name' do
  204. condition = {
  205. operator: 'OR',
  206. conditions: [
  207. {
  208. name: 'ticket.organization_id',
  209. operator: 'is',
  210. pre_condition: 'not_set',
  211. },
  212. {
  213. name: 'organization.name',
  214. operator: 'is not',
  215. value: organization.name,
  216. }
  217. ]
  218. }
  219. count, = Ticket.selectors(condition, { current_user: agent })
  220. expect(count).to eq(1)
  221. end
  222. end
  223. end