ticket_overview_out_of_office_test.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'test_helper'
  3. class TicketOverviewOutOfOfficeTest < ActiveSupport::TestCase
  4. setup do
  5. group = Group.create_or_update(
  6. name: 'OverviewReplacementTest',
  7. updated_at: '2015-02-05 16:37:00',
  8. updated_by_id: 1,
  9. created_by_id: 1,
  10. )
  11. roles = Role.where(name: 'Agent')
  12. @agent1 = User.create_or_update(
  13. login: 'ticket-overview-agent1@example.com',
  14. firstname: 'Overview',
  15. lastname: 'Agent1',
  16. email: 'ticket-overview-agent1@example.com',
  17. password: 'agentpw',
  18. active: true,
  19. roles: roles,
  20. groups: [group],
  21. out_of_office: false,
  22. updated_at: '2015-02-05 16:37:00',
  23. updated_by_id: 1,
  24. created_by_id: 1,
  25. )
  26. @agent2 = User.create_or_update(
  27. login: 'ticket-overview-agent2@example.com',
  28. firstname: 'Overview',
  29. lastname: 'Agent2',
  30. email: 'ticket-overview-agent2@example.com',
  31. password: 'agentpw',
  32. active: true,
  33. roles: roles,
  34. groups: [group],
  35. out_of_office: false,
  36. updated_at: '2015-02-05 16:38:00',
  37. updated_by_id: 1,
  38. created_by_id: 1,
  39. )
  40. roles = Role.where(name: 'Customer')
  41. organization1 = Organization.create_or_update(
  42. name: 'Overview Org',
  43. updated_at: '2015-02-05 16:37:00',
  44. updated_by_id: 1,
  45. created_by_id: 1,
  46. )
  47. @customer1 = User.create_or_update(
  48. login: 'ticket-overview-customer1@example.com',
  49. firstname: 'Overview',
  50. lastname: 'Customer1',
  51. email: 'ticket-overview-customer1@example.com',
  52. password: 'customerpw',
  53. active: true,
  54. organization_id: organization1.id,
  55. roles: roles,
  56. out_of_office: false,
  57. updated_at: '2015-02-05 16:37:00',
  58. updated_by_id: 1,
  59. created_by_id: 1,
  60. )
  61. Overview.destroy_all
  62. UserInfo.current_user_id = 1
  63. overview_role = Role.find_by(name: 'Agent')
  64. @overview1 = Overview.create_or_update(
  65. name: 'My replacement Tickets',
  66. link: 'my_replacement',
  67. prio: 1000,
  68. role_ids: [overview_role.id],
  69. out_of_office: true,
  70. condition: {
  71. 'ticket.state_id' => {
  72. operator: 'is',
  73. value: Ticket::State.by_category_ids(:open),
  74. },
  75. 'ticket.out_of_office_replacement_id' => {
  76. operator: 'is',
  77. pre_condition: 'current_user.id',
  78. },
  79. },
  80. order: {
  81. by: 'created_at',
  82. direction: 'ASC',
  83. },
  84. view: {
  85. d: %w[title customer group created_at],
  86. s: %w[title customer group created_at],
  87. m: %w[number title customer group created_at],
  88. view_mode_default: 's',
  89. },
  90. )
  91. @overview2 = Overview.create_if_not_exists(
  92. name: 'My Assigned Tickets',
  93. link: 'my_assigned',
  94. prio: 900,
  95. role_ids: [overview_role.id],
  96. condition: {
  97. 'ticket.state_id' => {
  98. operator: 'is',
  99. value: Ticket::State.by_category_ids(:open),
  100. },
  101. 'ticket.owner_id' => {
  102. operator: 'is',
  103. pre_condition: 'current_user.id',
  104. },
  105. },
  106. order: {
  107. by: 'created_at',
  108. direction: 'ASC',
  109. },
  110. view: {
  111. d: %w[title customer group created_at],
  112. s: %w[title customer group created_at],
  113. m: %w[number title customer group created_at],
  114. view_mode_default: 's',
  115. },
  116. )
  117. overview_role = Role.find_by(name: 'Customer')
  118. @overview3 = Overview.create_or_update(
  119. name: 'My Tickets',
  120. link: 'my_tickets',
  121. prio: 1100,
  122. role_ids: [overview_role.id],
  123. out_of_office: false,
  124. condition: {
  125. 'ticket.state_id' => {
  126. operator: 'is',
  127. value: [1, 2, 3, 4, 6, 7],
  128. },
  129. 'ticket.out_of_office_replacement_id' => {
  130. operator: 'is',
  131. pre_condition: 'current_user.organization_id',
  132. },
  133. },
  134. order: {
  135. by: 'created_at',
  136. direction: 'DESC',
  137. },
  138. view: {
  139. d: %w[title customer state created_at],
  140. s: %w[number title state created_at],
  141. m: %w[number title state created_at],
  142. view_mode_default: 's',
  143. },
  144. )
  145. end
  146. test 'overview index' do
  147. result = Ticket::Overviews.all(
  148. current_user: @agent1,
  149. )
  150. assert_equal(1, result.count)
  151. assert_equal('My Assigned Tickets', result[0].name)
  152. result = Ticket::Overviews.all(
  153. current_user: @agent2,
  154. )
  155. assert_equal(1, result.count)
  156. assert_equal('My Assigned Tickets', result[0].name)
  157. result = Ticket::Overviews.all(
  158. current_user: @customer1,
  159. )
  160. assert_equal(1, result.count)
  161. assert_equal('My Tickets', result[0].name)
  162. @agent1.out_of_office = true
  163. @agent1.out_of_office_start_at = 2.days.ago
  164. @agent1.out_of_office_end_at = 2.days.from_now
  165. @agent1.out_of_office_replacement_id = @agent2.id
  166. @agent1.save!
  167. result = Ticket::Overviews.all(
  168. current_user: @agent1,
  169. )
  170. assert_equal(1, result.count)
  171. assert_equal('My Assigned Tickets', result[0].name)
  172. result = Ticket::Overviews.all(
  173. current_user: @agent2,
  174. )
  175. assert_equal(2, result.count)
  176. assert_equal('My Assigned Tickets', result[0].name)
  177. assert_equal('My replacement Tickets', result[1].name)
  178. result = Ticket::Overviews.all(
  179. current_user: @customer1,
  180. )
  181. assert_equal(1, result.count)
  182. assert_equal('My Tickets', result[0].name)
  183. end
  184. test 'overview shown' do
  185. result = Ticket::Overviews.index(@agent1)
  186. assert(result[0])
  187. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  188. assert_equal(result[0][:overview][:view], 'my_assigned')
  189. assert_equal(result[0][:count], 0)
  190. assert_equal(result[0][:tickets].class, Array)
  191. assert(result[0][:tickets].blank?)
  192. result = Ticket::Overviews.index(@agent2)
  193. assert(result[0])
  194. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  195. assert_equal(result[0][:overview][:view], 'my_assigned')
  196. assert_equal(result[0][:count], 0)
  197. assert_equal(result[0][:tickets].class, Array)
  198. assert(result[0][:tickets].blank?)
  199. result = Ticket::Overviews.index(@customer1)
  200. assert(result[0])
  201. assert_equal(result[0][:overview][:name], 'My Tickets')
  202. assert_equal(result[0][:overview][:view], 'my_tickets')
  203. assert_equal(result[0][:count], 0)
  204. assert_equal(result[0][:tickets].class, Array)
  205. assert(result[0][:tickets].blank?)
  206. @agent1.out_of_office = true
  207. @agent1.out_of_office_start_at = 2.days.ago
  208. @agent1.out_of_office_end_at = 2.days.from_now
  209. @agent1.out_of_office_replacement_id = @agent2.id
  210. @agent1.save!
  211. assert_equal(@agent2.out_of_office_agent_of.count, 1)
  212. assert(@agent2.out_of_office_agent_of[0])
  213. assert_equal(@agent2.out_of_office_agent_of[0].id, @agent1.id)
  214. result = Ticket::Overviews.index(@agent1)
  215. assert(result[0])
  216. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  217. assert_equal(result[0][:overview][:view], 'my_assigned')
  218. assert_equal(result[0][:count], 0)
  219. assert_equal(result[0][:tickets].class, Array)
  220. assert(result[0][:tickets].blank?)
  221. result = Ticket::Overviews.index(@agent2)
  222. assert(result[0])
  223. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  224. assert_equal(result[0][:overview][:view], 'my_assigned')
  225. assert_equal(result[0][:count], 0)
  226. assert_equal(result[0][:tickets].class, Array)
  227. assert(result[0][:tickets].blank?)
  228. assert(result[1])
  229. assert_equal(result[1][:overview][:name], 'My replacement Tickets')
  230. assert_equal(result[1][:overview][:view], 'my_replacement')
  231. assert_equal(result[1][:count], 0)
  232. assert_equal(result[1][:tickets].class, Array)
  233. assert(result[1][:tickets].blank?)
  234. result = Ticket::Overviews.index(@customer1)
  235. assert(result[0])
  236. assert_equal(result[0][:overview][:name], 'My Tickets')
  237. assert_equal(result[0][:overview][:view], 'my_tickets')
  238. assert_equal(result[0][:count], 0)
  239. assert_equal(result[0][:tickets].class, Array)
  240. assert(result[0][:tickets].blank?)
  241. ticket1 = Ticket.create!(
  242. title: 'overview test 1',
  243. group: Group.lookup(name: 'OverviewReplacementTest'),
  244. customer_id: 2,
  245. owner_id: @agent1.id,
  246. state: Ticket::State.lookup(name: 'new'),
  247. priority: Ticket::Priority.lookup(name: '2 normal'),
  248. updated_by_id: 1,
  249. created_by_id: 1,
  250. )
  251. Ticket::Article.create!(
  252. ticket_id: ticket1.id,
  253. from: 'some_sender@example.com',
  254. to: 'some_recipient@example.com',
  255. subject: 'some subject',
  256. message_id: 'some@id',
  257. body: 'some message... 123',
  258. internal: false,
  259. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  260. type: Ticket::Article::Type.find_by(name: 'email'),
  261. updated_by_id: 1,
  262. created_by_id: 1,
  263. )
  264. result = Ticket::Overviews.index(@agent1)
  265. assert(result[0])
  266. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  267. assert_equal(result[0][:overview][:view], 'my_assigned')
  268. assert_equal(result[0][:count], 1)
  269. assert_equal(result[0][:tickets].class, Array)
  270. assert(result[0][:tickets])
  271. assert_equal(result[0][:tickets][0][:id], ticket1.id)
  272. result = Ticket::Overviews.index(@agent2)
  273. assert(result[0])
  274. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  275. assert_equal(result[0][:overview][:view], 'my_assigned')
  276. assert_equal(result[0][:count], 0)
  277. assert_equal(result[0][:tickets].class, Array)
  278. assert(result[0][:tickets].blank?)
  279. assert(result[1])
  280. assert_equal(result[1][:overview][:name], 'My replacement Tickets')
  281. assert_equal(result[1][:overview][:view], 'my_replacement')
  282. assert_equal(result[1][:count], 1)
  283. assert_equal(result[1][:tickets].class, Array)
  284. assert(result[1][:tickets])
  285. assert_equal(result[1][:tickets][0][:id], ticket1.id)
  286. result = Ticket::Overviews.index(@customer1)
  287. assert(result[0])
  288. assert_equal(result[0][:overview][:name], 'My Tickets')
  289. assert_equal(result[0][:overview][:view], 'my_tickets')
  290. assert_equal(result[0][:count], 0)
  291. assert_equal(result[0][:tickets].class, Array)
  292. assert(result[0][:tickets].blank?)
  293. end
  294. end