ticket_overview_test.rb 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'test_helper'
  3. class TicketOverviewTest < ActiveSupport::TestCase
  4. setup do
  5. group = Group.create_or_update(
  6. name: 'OverviewTest',
  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. updated_at: '2015-02-05 16:37:00',
  22. updated_by_id: 1,
  23. created_by_id: 1,
  24. )
  25. @agent2 = User.create_or_update(
  26. login: 'ticket-overview-agent2@example.com',
  27. firstname: 'Overview',
  28. lastname: 'Agent2',
  29. email: 'ticket-overview-agent2@example.com',
  30. password: 'agentpw',
  31. active: true,
  32. roles: roles,
  33. # groups: groups,
  34. updated_at: '2015-02-05 16:38:00',
  35. updated_by_id: 1,
  36. created_by_id: 1,
  37. )
  38. roles = Role.where(name: 'Customer')
  39. organization1 = Organization.create_or_update(
  40. name: 'Overview Org',
  41. updated_at: '2015-02-05 16:37:00',
  42. updated_by_id: 1,
  43. created_by_id: 1,
  44. )
  45. @customer1 = User.create_or_update(
  46. login: 'ticket-overview-customer1@example.com',
  47. firstname: 'Overview',
  48. lastname: 'Customer1',
  49. email: 'ticket-overview-customer1@example.com',
  50. password: 'customerpw',
  51. active: true,
  52. organization_id: organization1.id,
  53. roles: roles,
  54. updated_at: '2015-02-05 16:37:00',
  55. updated_by_id: 1,
  56. created_by_id: 1,
  57. )
  58. @customer2 = User.create_or_update(
  59. login: 'ticket-overview-customer2@example.com',
  60. firstname: 'Overview',
  61. lastname: 'Customer2',
  62. email: 'ticket-overview-customer2@example.com',
  63. password: 'customerpw',
  64. active: true,
  65. organization_id: organization1.id,
  66. roles: roles,
  67. updated_at: '2015-02-05 16:37:00',
  68. updated_by_id: 1,
  69. created_by_id: 1,
  70. )
  71. @customer3 = User.create_or_update(
  72. login: 'ticket-overview-customer3@example.com',
  73. firstname: 'Overview',
  74. lastname: 'Customer3',
  75. email: 'ticket-overview-customer3@example.com',
  76. password: 'customerpw',
  77. active: true,
  78. organization_id: nil,
  79. roles: roles,
  80. updated_at: '2015-02-05 16:37:00',
  81. updated_by_id: 1,
  82. created_by_id: 1,
  83. )
  84. Overview.destroy_all
  85. UserInfo.current_user_id = 1
  86. overview_role = Role.find_by(name: 'Agent')
  87. @overview1 = Overview.create_or_update(
  88. name: 'My Assigned Tickets',
  89. link: 'my_assigned',
  90. prio: 1000,
  91. role_ids: [overview_role.id],
  92. condition: {
  93. 'ticket.state_id' => {
  94. operator: 'is',
  95. value: [1, 2, 3, 7],
  96. },
  97. 'ticket.owner_id' => {
  98. operator: 'is',
  99. pre_condition: 'current_user.id',
  100. },
  101. },
  102. order: {
  103. by: 'created_at',
  104. direction: 'ASC',
  105. },
  106. view: {
  107. d: %w[title customer group created_at],
  108. s: %w[title customer group created_at],
  109. m: %w[number title customer group created_at],
  110. view_mode_default: 's',
  111. },
  112. )
  113. @overview2 = Overview.create_or_update(
  114. name: 'Unassigned & Open',
  115. link: 'all_unassigned',
  116. prio: 1010,
  117. role_ids: [overview_role.id],
  118. condition: {
  119. 'ticket.state_id' => {
  120. operator: 'is',
  121. value: [1, 2, 3],
  122. },
  123. 'ticket.owner_id' => {
  124. operator: 'is',
  125. value: 1,
  126. },
  127. },
  128. order: {
  129. by: 'created_at',
  130. direction: 'ASC',
  131. },
  132. view: {
  133. d: %w[title customer group created_at],
  134. s: %w[title customer group created_at],
  135. m: %w[number title customer group created_at],
  136. view_mode_default: 's',
  137. },
  138. )
  139. @overview3 = Overview.create_or_update(
  140. name: 'My Tickets 2',
  141. link: 'my_tickets_2',
  142. prio: 1020,
  143. role_ids: [overview_role.id],
  144. user_ids: [@agent2.id],
  145. condition: {
  146. 'ticket.state_id' => {
  147. operator: 'is',
  148. value: [1, 2, 3, 7],
  149. },
  150. 'ticket.owner_id' => {
  151. operator: 'is',
  152. pre_condition: 'current_user.id',
  153. },
  154. },
  155. order: {
  156. by: 'created_at',
  157. direction: 'ASC',
  158. },
  159. view: {
  160. d: %w[title customer group created_at],
  161. s: %w[title customer group created_at],
  162. m: %w[number title customer group created_at],
  163. view_mode_default: 's',
  164. },
  165. )
  166. @overview4 = Overview.create_or_update(
  167. name: 'My Tickets only with Note',
  168. link: 'my_tickets_onyl_with_note',
  169. prio: 1030,
  170. role_ids: [overview_role.id],
  171. user_ids: [@agent1.id],
  172. condition: {
  173. 'article.type_id' => {
  174. operator: 'is',
  175. value: Ticket::Article::Type.find_by(name: 'note').id,
  176. },
  177. 'ticket.owner_id' => {
  178. operator: 'is',
  179. pre_condition: 'current_user.id',
  180. },
  181. },
  182. order: {
  183. by: 'created_at',
  184. direction: 'ASC',
  185. },
  186. view: {
  187. d: %w[title customer group created_at],
  188. s: %w[title customer group created_at],
  189. m: %w[number title customer group created_at],
  190. view_mode_default: 's',
  191. },
  192. )
  193. overview_role = Role.find_by(name: 'Customer')
  194. @overview5 = Overview.create_or_update(
  195. name: 'My Tickets',
  196. link: 'my_tickets',
  197. prio: 1100,
  198. role_ids: [overview_role.id],
  199. condition: {
  200. 'ticket.state_id' => {
  201. operator: 'is',
  202. value: [1, 2, 3, 4, 6, 7],
  203. },
  204. 'ticket.customer_id' => {
  205. operator: 'is',
  206. pre_condition: 'current_user.id',
  207. },
  208. },
  209. order: {
  210. by: 'created_at',
  211. direction: 'DESC',
  212. },
  213. view: {
  214. d: %w[title customer state created_at],
  215. s: %w[number title state created_at],
  216. m: %w[number title state created_at],
  217. view_mode_default: 's',
  218. },
  219. )
  220. @overview6 = Overview.create_or_update(
  221. name: 'My Organization Tickets',
  222. link: 'my_organization_tickets',
  223. prio: 1200,
  224. role_ids: [overview_role.id],
  225. organization_shared: true,
  226. condition: {
  227. 'ticket.state_id' => {
  228. operator: 'is',
  229. value: [1, 2, 3, 4, 6, 7],
  230. },
  231. 'ticket.organization_id' => {
  232. operator: 'is',
  233. pre_condition: 'current_user.organization_id',
  234. },
  235. },
  236. order: {
  237. by: 'created_at',
  238. direction: 'DESC',
  239. },
  240. view: {
  241. d: %w[title customer state created_at],
  242. s: %w[number title customer state created_at],
  243. m: %w[number title customer state created_at],
  244. view_mode_default: 's',
  245. },
  246. )
  247. @overview7 = Overview.create_or_update(
  248. name: 'My Organization Tickets (open)',
  249. link: 'my_organization_tickets_open',
  250. prio: 1200,
  251. role_ids: [overview_role.id],
  252. user_ids: [@customer2.id],
  253. organization_shared: true,
  254. condition: {
  255. 'ticket.state_id' => {
  256. operator: 'is',
  257. value: [1, 2, 3],
  258. },
  259. 'ticket.organization_id' => {
  260. operator: 'is',
  261. pre_condition: 'current_user.organization_id',
  262. },
  263. },
  264. order: {
  265. by: 'created_at',
  266. direction: 'DESC',
  267. },
  268. view: {
  269. d: %w[title customer state created_at],
  270. s: %w[number title customer state created_at],
  271. m: %w[number title customer state created_at],
  272. view_mode_default: 's',
  273. },
  274. )
  275. overview_role = Role.find_by(name: 'Admin')
  276. @overview8 = Overview.create_or_update(
  277. name: 'Not Shown Admin',
  278. link: 'not_shown_admin',
  279. prio: 9900,
  280. role_ids: [overview_role.id],
  281. condition: {
  282. 'ticket.state_id' => {
  283. operator: 'is',
  284. value: [1, 2, 3],
  285. },
  286. },
  287. order: {
  288. by: 'created_at',
  289. direction: 'DESC',
  290. },
  291. view: {
  292. d: %w[title customer state created_at],
  293. s: %w[number title customer state created_at],
  294. m: %w[number title customer state created_at],
  295. view_mode_default: 's',
  296. },
  297. )
  298. end
  299. test 'overview index' do
  300. result = Ticket::Overviews.all(
  301. current_user: @agent1,
  302. )
  303. assert_equal(3, result.count)
  304. assert_equal('My Assigned Tickets', result[0].name)
  305. assert_equal('Unassigned & Open', result[1].name)
  306. assert_equal('My Tickets only with Note', result[2].name)
  307. result = Ticket::Overviews.all(
  308. current_user: @agent2,
  309. )
  310. assert_equal(3, result.count)
  311. assert_equal('My Assigned Tickets', result[0].name)
  312. assert_equal('Unassigned & Open', result[1].name)
  313. assert_equal('My Tickets 2', result[2].name)
  314. result = Ticket::Overviews.all(
  315. current_user: @customer1,
  316. )
  317. assert_equal(2, result.count)
  318. assert_equal('My Tickets', result[0].name)
  319. assert_equal('My Organization Tickets', result[1].name)
  320. result = Ticket::Overviews.all(
  321. current_user: @customer2,
  322. )
  323. assert_equal(3, result.count)
  324. assert_equal('My Tickets', result[0].name)
  325. assert_equal('My Organization Tickets', result[1].name)
  326. assert_equal('My Organization Tickets (open)', result[2].name)
  327. result = Ticket::Overviews.all(
  328. current_user: @customer3,
  329. )
  330. assert_equal(1, result.count)
  331. assert_equal('My Tickets', result[0].name)
  332. end
  333. test 'missing role' do
  334. Ticket.destroy_all
  335. assert_raises(Exception) do
  336. Overview.create!(
  337. name: 'new overview',
  338. link: 'new_overview',
  339. prio: 1200,
  340. user_ids: [@customer2.id],
  341. organization_shared: true,
  342. condition: {
  343. 'ticket.state_id' => {
  344. operator: 'is',
  345. value: [1, 2, 3],
  346. },
  347. 'ticket.organization_id' => {
  348. operator: 'is',
  349. pre_condition: 'current_user.organization_id',
  350. },
  351. },
  352. order: {
  353. by: 'created_at',
  354. direction: 'DESC',
  355. },
  356. view: {
  357. d: %w[title customer state created_at],
  358. s: %w[number title customer state created_at],
  359. m: %w[number title customer state created_at],
  360. view_mode_default: 's',
  361. },
  362. )
  363. end
  364. end
  365. test 'overview content' do
  366. Ticket.destroy_all
  367. result = Ticket::Overviews.index(@agent1)
  368. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  369. assert_equal(result[0][:overview][:view], 'my_assigned')
  370. assert_equal(result[0][:count], 0)
  371. assert_equal(result[0][:tickets].class, Array)
  372. assert(result[0][:tickets].blank?)
  373. assert_equal(result[1][:overview][:name], 'Unassigned & Open')
  374. assert_equal(result[1][:overview][:view], 'all_unassigned')
  375. assert_equal(result[1][:tickets].class, Array)
  376. assert(result[1][:tickets].blank?)
  377. assert_equal(result[1][:count], 0)
  378. assert_equal(result[2][:overview][:name], 'My Tickets only with Note')
  379. assert_equal(result[2][:overview][:view], 'my_tickets_onyl_with_note')
  380. assert_equal(result[2][:tickets].class, Array)
  381. assert(result[2][:tickets].blank?)
  382. assert_equal(result[2][:count], 0)
  383. result = Ticket::Overviews.index(@agent2)
  384. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  385. assert_equal(result[0][:overview][:view], 'my_assigned')
  386. assert_equal(result[0][:count], 0)
  387. assert_equal(result[0][:tickets].class, Array)
  388. assert(result[0][:tickets].blank?)
  389. assert_equal(result[1][:overview][:name], 'Unassigned & Open')
  390. assert_equal(result[1][:overview][:view], 'all_unassigned')
  391. assert_equal(result[1][:tickets].class, Array)
  392. assert(result[1][:tickets].blank?)
  393. assert_equal(result[1][:count], 0)
  394. assert_equal(result[2][:overview][:name], 'My Tickets 2')
  395. assert_equal(result[2][:overview][:view], 'my_tickets_2')
  396. assert_equal(result[2][:tickets].class, Array)
  397. assert(result[2][:tickets].blank?)
  398. ticket1 = Ticket.create!(
  399. title: 'overview test 1',
  400. group: Group.lookup(name: 'OverviewTest'),
  401. customer_id: 2,
  402. state: Ticket::State.lookup(name: 'new'),
  403. priority: Ticket::Priority.lookup(name: '2 normal'),
  404. updated_by_id: 1,
  405. created_by_id: 1,
  406. )
  407. Ticket::Article.create!(
  408. ticket_id: ticket1.id,
  409. from: 'some_sender@example.com',
  410. to: 'some_recipient@example.com',
  411. subject: 'some subject',
  412. message_id: 'some@id',
  413. body: 'some message... 123',
  414. internal: false,
  415. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  416. type: Ticket::Article::Type.find_by(name: 'email'),
  417. updated_by_id: 1,
  418. created_by_id: 1,
  419. )
  420. result = Ticket::Overviews.index(@agent1)
  421. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  422. assert_equal(result[0][:overview][:view], 'my_assigned')
  423. assert_equal(result[0][:count], 0)
  424. assert_equal(result[0][:tickets].class, Array)
  425. assert(result[0][:tickets].blank?)
  426. assert_equal(result[1][:overview][:name], 'Unassigned & Open')
  427. assert_equal(result[1][:overview][:view], 'all_unassigned')
  428. assert_equal(result[1][:tickets].class, Array)
  429. assert_not(result[1][:tickets].blank?)
  430. assert_equal(result[1][:tickets][0][:id], ticket1.id)
  431. assert_equal(result[1][:count], 1)
  432. assert_equal(result[2][:overview][:name], 'My Tickets only with Note')
  433. assert_equal(result[2][:overview][:view], 'my_tickets_onyl_with_note')
  434. assert_equal(result[2][:tickets].class, Array)
  435. assert(result[2][:tickets].blank?)
  436. assert_equal(result[2][:count], 0)
  437. result = Ticket::Overviews.index(@agent2)
  438. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  439. assert_equal(result[0][:overview][:view], 'my_assigned')
  440. assert_equal(result[0][:count], 0)
  441. assert_equal(result[0][:tickets].class, Array)
  442. assert(result[0][:tickets].blank?)
  443. assert_equal(result[1][:overview][:name], 'Unassigned & Open')
  444. assert_equal(result[1][:overview][:view], 'all_unassigned')
  445. assert_equal(result[1][:tickets].class, Array)
  446. assert(result[1][:tickets].blank?)
  447. assert_equal(result[1][:count], 0)
  448. assert_equal(result[2][:overview][:name], 'My Tickets 2')
  449. assert_equal(result[2][:overview][:view], 'my_tickets_2')
  450. assert_equal(result[2][:tickets].class, Array)
  451. assert(result[2][:tickets].blank?)
  452. travel 1.second # because of mysql millitime issues
  453. ticket2 = Ticket.create!(
  454. title: 'overview test 2',
  455. group: Group.lookup(name: 'OverviewTest'),
  456. customer_id: 2,
  457. state: Ticket::State.lookup(name: 'new'),
  458. priority: Ticket::Priority.lookup(name: '3 high'),
  459. updated_by_id: 1,
  460. created_by_id: 1,
  461. )
  462. Ticket::Article.create!(
  463. ticket_id: ticket2.id,
  464. from: 'some_sender@example.com',
  465. to: 'some_recipient@example.com',
  466. subject: 'some subject',
  467. message_id: 'some@id',
  468. body: 'some message... 123',
  469. internal: false,
  470. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  471. type: Ticket::Article::Type.find_by(name: 'note'),
  472. updated_by_id: 1,
  473. created_by_id: 1,
  474. )
  475. result = Ticket::Overviews.index(@agent1)
  476. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  477. assert_equal(result[0][:overview][:view], 'my_assigned')
  478. assert_equal(result[0][:count], 0)
  479. assert_equal(result[0][:tickets].class, Array)
  480. assert(result[0][:tickets].blank?)
  481. assert_equal(result[1][:overview][:name], 'Unassigned & Open')
  482. assert_equal(result[1][:overview][:view], 'all_unassigned')
  483. assert_equal(result[1][:tickets].class, Array)
  484. assert_not(result[1][:tickets].blank?)
  485. assert_equal(result[1][:tickets][0][:id], ticket1.id)
  486. assert_equal(result[1][:tickets][1][:id], ticket2.id)
  487. assert_equal(result[1][:count], 2)
  488. assert_equal(result[2][:overview][:name], 'My Tickets only with Note')
  489. assert_equal(result[2][:overview][:view], 'my_tickets_onyl_with_note')
  490. assert_equal(result[2][:tickets].class, Array)
  491. assert(result[2][:tickets].blank?)
  492. assert_equal(result[2][:count], 0)
  493. result = Ticket::Overviews.index(@agent2)
  494. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  495. assert_equal(result[0][:overview][:view], 'my_assigned')
  496. assert_equal(result[0][:count], 0)
  497. assert_equal(result[0][:tickets].class, Array)
  498. assert(result[0][:tickets].blank?)
  499. assert_equal(result[1][:overview][:name], 'Unassigned & Open')
  500. assert_equal(result[1][:overview][:view], 'all_unassigned')
  501. assert_equal(result[1][:tickets].class, Array)
  502. assert(result[1][:tickets].blank?)
  503. assert_equal(result[1][:count], 0)
  504. assert_equal(result[2][:overview][:name], 'My Tickets 2')
  505. assert_equal(result[2][:overview][:view], 'my_tickets_2')
  506. assert_equal(result[2][:tickets].class, Array)
  507. assert(result[2][:tickets].blank?)
  508. ticket2.owner_id = @agent1.id
  509. ticket2.save!
  510. result = Ticket::Overviews.index(@agent1)
  511. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  512. assert_equal(result[0][:overview][:view], 'my_assigned')
  513. assert_equal(result[0][:tickets].class, Array)
  514. assert_equal(result[0][:tickets][0][:id], ticket2.id)
  515. assert_equal(result[0][:count], 1)
  516. assert_equal(result[0][:tickets].class, Array)
  517. assert_equal(result[1][:overview][:name], 'Unassigned & Open')
  518. assert_equal(result[1][:overview][:view], 'all_unassigned')
  519. assert_equal(result[1][:tickets].class, Array)
  520. assert_not(result[1][:tickets].blank?)
  521. assert_equal(result[1][:tickets][0][:id], ticket1.id)
  522. assert_equal(result[1][:count], 1)
  523. assert_equal(result[2][:overview][:name], 'My Tickets only with Note')
  524. assert_equal(result[2][:overview][:view], 'my_tickets_onyl_with_note')
  525. assert_equal(result[2][:tickets].class, Array)
  526. assert_equal(result[2][:tickets][0][:id], ticket2.id)
  527. assert_equal(result[2][:count], 1)
  528. result = Ticket::Overviews.index(@agent2)
  529. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  530. assert_equal(result[0][:overview][:view], 'my_assigned')
  531. assert_equal(result[0][:count], 0)
  532. assert_equal(result[0][:tickets].class, Array)
  533. assert(result[0][:tickets].blank?)
  534. assert_equal(result[1][:overview][:name], 'Unassigned & Open')
  535. assert_equal(result[1][:overview][:view], 'all_unassigned')
  536. assert_equal(result[1][:tickets].class, Array)
  537. assert(result[1][:tickets].blank?)
  538. assert_equal(result[1][:count], 0)
  539. assert_equal(result[2][:overview][:name], 'My Tickets 2')
  540. assert_equal(result[2][:overview][:view], 'my_tickets_2')
  541. assert_equal(result[2][:tickets].class, Array)
  542. assert(result[2][:tickets].blank?)
  543. travel 1.second # because of mysql millitime issues
  544. ticket3 = Ticket.create!(
  545. title: 'overview test 3',
  546. group: Group.lookup(name: 'OverviewTest'),
  547. customer_id: 2,
  548. state: Ticket::State.lookup(name: 'new'),
  549. priority: Ticket::Priority.lookup(name: '1 low'),
  550. updated_by_id: 1,
  551. created_by_id: 1,
  552. )
  553. Ticket::Article.create!(
  554. ticket_id: ticket3.id,
  555. from: 'some_sender@example.com',
  556. to: 'some_recipient@example.com',
  557. subject: 'some subject',
  558. message_id: 'some@id',
  559. body: 'some message... 123',
  560. internal: false,
  561. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  562. type: Ticket::Article::Type.find_by(name: 'email'),
  563. updated_by_id: 1,
  564. created_by_id: 1,
  565. )
  566. travel_back
  567. result = Ticket::Overviews.index(@agent1)
  568. assert_equal(result[0][:overview][:id], @overview1.id)
  569. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  570. assert_equal(result[0][:overview][:view], 'my_assigned')
  571. assert_equal(result[0][:tickets].class, Array)
  572. assert_equal(result[0][:tickets][0][:id], ticket2.id)
  573. assert_equal(result[0][:count], 1)
  574. assert_equal(result[0][:tickets].class, Array)
  575. assert_equal(result[1][:overview][:id], @overview2.id)
  576. assert_equal(result[1][:overview][:name], 'Unassigned & Open')
  577. assert_equal(result[1][:overview][:view], 'all_unassigned')
  578. assert_equal(result[1][:tickets].class, Array)
  579. assert_not(result[1][:tickets].blank?)
  580. assert_equal(result[1][:tickets][0][:id], ticket1.id)
  581. assert_equal(result[1][:tickets][1][:id], ticket3.id)
  582. assert_equal(result[1][:count], 2)
  583. assert_equal(result[2][:overview][:id], @overview4.id)
  584. assert_equal(result[2][:overview][:name], 'My Tickets only with Note')
  585. assert_equal(result[2][:overview][:view], 'my_tickets_onyl_with_note')
  586. assert_equal(result[2][:tickets].class, Array)
  587. assert_equal(result[2][:tickets][0][:id], ticket2.id)
  588. assert_equal(result[2][:count], 1)
  589. result = Ticket::Overviews.index(@agent2)
  590. assert_equal(result[0][:overview][:id], @overview1.id)
  591. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  592. assert_equal(result[0][:overview][:view], 'my_assigned')
  593. assert_equal(result[0][:count], 0)
  594. assert_equal(result[0][:tickets].class, Array)
  595. assert(result[0][:tickets].blank?)
  596. assert_equal(result[1][:overview][:id], @overview2.id)
  597. assert_equal(result[1][:overview][:name], 'Unassigned & Open')
  598. assert_equal(result[1][:overview][:view], 'all_unassigned')
  599. assert_equal(result[1][:tickets].class, Array)
  600. assert(result[1][:tickets].blank?)
  601. assert_equal(result[1][:count], 0)
  602. assert_equal(result[2][:overview][:id], @overview3.id)
  603. assert_equal(result[2][:overview][:name], 'My Tickets 2')
  604. assert_equal(result[2][:overview][:view], 'my_tickets_2')
  605. assert_equal(result[2][:tickets].class, Array)
  606. assert(result[2][:tickets].blank?)
  607. @overview2.order = {
  608. by: 'created_at',
  609. direction: 'DESC',
  610. }
  611. @overview2.save!
  612. result = Ticket::Overviews.index(@agent1)
  613. assert_equal(result[0][:overview][:id], @overview1.id)
  614. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  615. assert_equal(result[0][:overview][:view], 'my_assigned')
  616. assert_equal(result[0][:tickets].class, Array)
  617. assert_equal(result[0][:tickets][0][:id], ticket2.id)
  618. assert_equal(result[0][:count], 1)
  619. assert_equal(result[0][:tickets].class, Array)
  620. assert_equal(result[1][:overview][:id], @overview2.id)
  621. assert_equal(result[1][:overview][:name], 'Unassigned & Open')
  622. assert_equal(result[1][:overview][:view], 'all_unassigned')
  623. assert_equal(result[1][:tickets].class, Array)
  624. assert_not(result[1][:tickets].blank?)
  625. assert_equal(result[1][:tickets][0][:id], ticket3.id)
  626. assert_equal(result[1][:tickets][1][:id], ticket1.id)
  627. assert_equal(result[1][:count], 2)
  628. assert_equal(result[2][:overview][:id], @overview4.id)
  629. assert_equal(result[2][:overview][:name], 'My Tickets only with Note')
  630. assert_equal(result[2][:overview][:view], 'my_tickets_onyl_with_note')
  631. assert_equal(result[2][:tickets].class, Array)
  632. assert_equal(result[2][:tickets][0][:id], ticket2.id)
  633. assert_equal(result[2][:count], 1)
  634. result = Ticket::Overviews.index(@agent2)
  635. assert_equal(result[0][:overview][:id], @overview1.id)
  636. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  637. assert_equal(result[0][:overview][:view], 'my_assigned')
  638. assert_equal(result[0][:count], 0)
  639. assert_equal(result[0][:tickets].class, Array)
  640. assert(result[0][:tickets].blank?)
  641. assert_equal(result[1][:overview][:id], @overview2.id)
  642. assert_equal(result[1][:overview][:name], 'Unassigned & Open')
  643. assert_equal(result[1][:overview][:view], 'all_unassigned')
  644. assert_equal(result[1][:tickets].class, Array)
  645. assert(result[1][:tickets].blank?)
  646. assert_equal(result[1][:count], 0)
  647. assert_equal(result[2][:overview][:id], @overview3.id)
  648. assert_equal(result[2][:overview][:name], 'My Tickets 2')
  649. assert_equal(result[2][:overview][:view], 'my_tickets_2')
  650. assert_equal(result[2][:tickets].class, Array)
  651. assert(result[2][:tickets].blank?)
  652. @overview2.order = {
  653. by: 'priority_id',
  654. direction: 'DESC',
  655. }
  656. @overview2.save!
  657. result = Ticket::Overviews.index(@agent1)
  658. assert_equal(result[0][:overview][:id], @overview1.id)
  659. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  660. assert_equal(result[0][:overview][:view], 'my_assigned')
  661. assert_equal(result[0][:tickets].class, Array)
  662. assert_equal(result[0][:tickets][0][:id], ticket2.id)
  663. assert_equal(result[0][:count], 1)
  664. assert_equal(result[0][:tickets].class, Array)
  665. assert_equal(result[1][:overview][:id], @overview2.id)
  666. assert_equal(result[1][:overview][:name], 'Unassigned & Open')
  667. assert_equal(result[1][:overview][:view], 'all_unassigned')
  668. assert_equal(result[1][:tickets].class, Array)
  669. assert_not(result[1][:tickets].blank?)
  670. assert_equal(result[1][:tickets][0][:id], ticket1.id)
  671. assert_equal(result[1][:tickets][1][:id], ticket3.id)
  672. assert_equal(result[1][:count], 2)
  673. assert_equal(result[2][:overview][:id], @overview4.id)
  674. assert_equal(result[2][:overview][:name], 'My Tickets only with Note')
  675. assert_equal(result[2][:overview][:view], 'my_tickets_onyl_with_note')
  676. assert_equal(result[2][:tickets].class, Array)
  677. assert_equal(result[2][:tickets][0][:id], ticket2.id)
  678. assert_equal(result[2][:count], 1)
  679. result = Ticket::Overviews.index(@agent2)
  680. assert_equal(result[0][:overview][:id], @overview1.id)
  681. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  682. assert_equal(result[0][:overview][:view], 'my_assigned')
  683. assert_equal(result[0][:count], 0)
  684. assert_equal(result[0][:tickets].class, Array)
  685. assert(result[0][:tickets].blank?)
  686. assert_equal(result[1][:overview][:id], @overview2.id)
  687. assert_equal(result[1][:overview][:name], 'Unassigned & Open')
  688. assert_equal(result[1][:overview][:view], 'all_unassigned')
  689. assert_equal(result[1][:tickets].class, Array)
  690. assert(result[1][:tickets].blank?)
  691. assert_equal(result[1][:count], 0)
  692. assert_equal(result[2][:overview][:id], @overview3.id)
  693. assert_equal(result[2][:overview][:name], 'My Tickets 2')
  694. assert_equal(result[2][:overview][:view], 'my_tickets_2')
  695. assert_equal(result[2][:tickets].class, Array)
  696. assert(result[2][:tickets].blank?)
  697. @overview2.order = {
  698. by: 'priority_id',
  699. direction: 'ASC',
  700. }
  701. @overview2.save!
  702. result = Ticket::Overviews.index(@agent1)
  703. assert_equal(result[0][:overview][:id], @overview1.id)
  704. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  705. assert_equal(result[0][:overview][:view], 'my_assigned')
  706. assert_equal(result[0][:tickets].class, Array)
  707. assert_equal(result[0][:tickets][0][:id], ticket2.id)
  708. assert_equal(result[0][:count], 1)
  709. assert_equal(result[0][:tickets].class, Array)
  710. assert_equal(result[1][:overview][:id], @overview2.id)
  711. assert_equal(result[1][:overview][:name], 'Unassigned & Open')
  712. assert_equal(result[1][:overview][:view], 'all_unassigned')
  713. assert_equal(result[1][:tickets].class, Array)
  714. assert_not(result[1][:tickets].blank?)
  715. assert_equal(result[1][:tickets][0][:id], ticket3.id)
  716. assert_equal(result[1][:tickets][1][:id], ticket1.id)
  717. assert_equal(result[1][:count], 2)
  718. assert_equal(result[2][:overview][:id], @overview4.id)
  719. assert_equal(result[2][:overview][:name], 'My Tickets only with Note')
  720. assert_equal(result[2][:overview][:view], 'my_tickets_onyl_with_note')
  721. assert_equal(result[2][:tickets].class, Array)
  722. assert_equal(result[2][:tickets][0][:id], ticket2.id)
  723. assert_equal(result[2][:count], 1)
  724. result = Ticket::Overviews.index(@agent2)
  725. assert_equal(result[0][:overview][:id], @overview1.id)
  726. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  727. assert_equal(result[0][:overview][:view], 'my_assigned')
  728. assert_equal(result[0][:count], 0)
  729. assert_equal(result[0][:tickets].class, Array)
  730. assert(result[0][:tickets].blank?)
  731. assert_equal(result[1][:overview][:id], @overview2.id)
  732. assert_equal(result[1][:overview][:name], 'Unassigned & Open')
  733. assert_equal(result[1][:overview][:view], 'all_unassigned')
  734. assert_equal(result[1][:tickets].class, Array)
  735. assert(result[1][:tickets].blank?)
  736. assert_equal(result[1][:count], 0)
  737. assert_equal(result[2][:overview][:id], @overview3.id)
  738. assert_equal(result[2][:overview][:name], 'My Tickets 2')
  739. assert_equal(result[2][:overview][:view], 'my_tickets_2')
  740. assert_equal(result[2][:tickets].class, Array)
  741. assert(result[2][:tickets].blank?)
  742. @overview2.order = {
  743. by: 'priority',
  744. direction: 'DESC',
  745. }
  746. @overview2.save!
  747. result = Ticket::Overviews.index(@agent1)
  748. assert_equal(result[0][:overview][:id], @overview1.id)
  749. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  750. assert_equal(result[0][:overview][:view], 'my_assigned')
  751. assert_equal(result[0][:tickets].class, Array)
  752. assert_equal(result[0][:tickets][0][:id], ticket2.id)
  753. assert_equal(result[0][:count], 1)
  754. assert_equal(result[0][:tickets].class, Array)
  755. assert_equal(result[1][:overview][:id], @overview2.id)
  756. assert_equal(result[1][:overview][:name], 'Unassigned & Open')
  757. assert_equal(result[1][:overview][:view], 'all_unassigned')
  758. assert_equal(result[1][:tickets].class, Array)
  759. assert_not(result[1][:tickets].blank?)
  760. assert_equal(result[1][:tickets][0][:id], ticket1.id)
  761. assert_equal(result[1][:tickets][1][:id], ticket3.id)
  762. assert_equal(result[1][:count], 2)
  763. assert_equal(result[2][:overview][:id], @overview4.id)
  764. assert_equal(result[2][:overview][:name], 'My Tickets only with Note')
  765. assert_equal(result[2][:overview][:view], 'my_tickets_onyl_with_note')
  766. assert_equal(result[2][:tickets].class, Array)
  767. assert_equal(result[2][:tickets][0][:id], ticket2.id)
  768. assert_equal(result[2][:count], 1)
  769. result = Ticket::Overviews.index(@agent2)
  770. assert_equal(result[0][:overview][:id], @overview1.id)
  771. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  772. assert_equal(result[0][:overview][:view], 'my_assigned')
  773. assert_equal(result[0][:count], 0)
  774. assert_equal(result[0][:tickets].class, Array)
  775. assert(result[0][:tickets].blank?)
  776. assert_equal(result[1][:overview][:id], @overview2.id)
  777. assert_equal(result[1][:overview][:name], 'Unassigned & Open')
  778. assert_equal(result[1][:overview][:view], 'all_unassigned')
  779. assert_equal(result[1][:tickets].class, Array)
  780. assert(result[1][:tickets].blank?)
  781. assert_equal(result[1][:count], 0)
  782. assert_equal(result[2][:overview][:id], @overview3.id)
  783. assert_equal(result[2][:overview][:name], 'My Tickets 2')
  784. assert_equal(result[2][:overview][:view], 'my_tickets_2')
  785. assert_equal(result[2][:tickets].class, Array)
  786. assert(result[2][:tickets].blank?)
  787. @overview2.order = {
  788. by: 'priority',
  789. direction: 'ASC',
  790. }
  791. @overview2.save!
  792. result = Ticket::Overviews.index(@agent1)
  793. assert_equal(result[0][:overview][:id], @overview1.id)
  794. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  795. assert_equal(result[0][:overview][:view], 'my_assigned')
  796. assert_equal(result[0][:tickets].class, Array)
  797. assert_equal(result[0][:tickets][0][:id], ticket2.id)
  798. assert_equal(result[0][:count], 1)
  799. assert_equal(result[0][:tickets].class, Array)
  800. assert_equal(result[1][:overview][:id], @overview2.id)
  801. assert_equal(result[1][:overview][:name], 'Unassigned & Open')
  802. assert_equal(result[1][:overview][:view], 'all_unassigned')
  803. assert_equal(result[1][:tickets].class, Array)
  804. assert_not(result[1][:tickets].blank?)
  805. assert_equal(result[1][:tickets][0][:id], ticket3.id)
  806. assert_equal(result[1][:tickets][1][:id], ticket1.id)
  807. assert_equal(result[1][:count], 2)
  808. assert_equal(result[2][:overview][:id], @overview4.id)
  809. assert_equal(result[2][:overview][:name], 'My Tickets only with Note')
  810. assert_equal(result[2][:overview][:view], 'my_tickets_onyl_with_note')
  811. assert_equal(result[2][:tickets].class, Array)
  812. assert_equal(result[2][:tickets][0][:id], ticket2.id)
  813. assert_equal(result[2][:count], 1)
  814. result = Ticket::Overviews.index(@agent2)
  815. assert_equal(result[0][:overview][:id], @overview1.id)
  816. assert_equal(result[0][:overview][:name], 'My Assigned Tickets')
  817. assert_equal(result[0][:overview][:view], 'my_assigned')
  818. assert_equal(result[0][:count], 0)
  819. assert_equal(result[0][:tickets].class, Array)
  820. assert(result[0][:tickets].blank?)
  821. assert_equal(result[1][:overview][:id], @overview2.id)
  822. assert_equal(result[1][:overview][:name], 'Unassigned & Open')
  823. assert_equal(result[1][:overview][:view], 'all_unassigned')
  824. assert_equal(result[1][:tickets].class, Array)
  825. assert(result[1][:tickets].blank?)
  826. assert_equal(result[1][:count], 0)
  827. assert_equal(result[2][:overview][:id], @overview3.id)
  828. assert_equal(result[2][:overview][:name], 'My Tickets 2')
  829. assert_equal(result[2][:overview][:view], 'my_tickets_2')
  830. assert_equal(result[2][:tickets].class, Array)
  831. assert(result[2][:tickets].blank?)
  832. end
  833. test 'overview any owner / no owner is set' do
  834. Ticket.destroy_all
  835. Overview.destroy_all
  836. UserInfo.current_user_id = 1
  837. overview_role = Role.find_by(name: 'Agent')
  838. overview1 = Overview.create_or_update(
  839. name: 'not owned',
  840. prio: 1000,
  841. role_ids: [overview_role.id],
  842. condition: {
  843. 'ticket.owner_id' => {
  844. operator: 'is',
  845. pre_condition: 'not_set',
  846. },
  847. },
  848. order: {
  849. by: 'created_at',
  850. direction: 'ASC',
  851. },
  852. view: {
  853. d: %w[title customer group created_at],
  854. s: %w[title customer group created_at],
  855. m: %w[number title customer group created_at],
  856. view_mode_default: 's',
  857. },
  858. )
  859. overview2 = Overview.create_or_update(
  860. name: 'not owned by somebody',
  861. prio: 2000,
  862. role_ids: [overview_role.id],
  863. condition: {
  864. 'ticket.owner_id' => {
  865. operator: 'is not',
  866. pre_condition: 'not_set',
  867. },
  868. },
  869. order: {
  870. by: 'created_at',
  871. direction: 'ASC',
  872. },
  873. view: {
  874. d: %w[title customer group created_at],
  875. s: %w[title customer group created_at],
  876. m: %w[number title customer group created_at],
  877. view_mode_default: 's',
  878. },
  879. )
  880. ticket1 = Ticket.create!(
  881. title: 'overview test 1',
  882. group: Group.lookup(name: 'OverviewTest'),
  883. customer_id: 2,
  884. owner_id: 1,
  885. state: Ticket::State.lookup(name: 'new'),
  886. priority: Ticket::Priority.lookup(name: '2 normal'),
  887. )
  888. travel 2.seconds
  889. ticket2 = Ticket.create!(
  890. title: 'overview test 2',
  891. group: Group.lookup(name: 'OverviewTest'),
  892. customer_id: 2,
  893. owner_id: nil,
  894. state: Ticket::State.lookup(name: 'new'),
  895. priority: Ticket::Priority.lookup(name: '2 normal'),
  896. )
  897. travel 2.seconds
  898. ticket3 = Ticket.create!(
  899. title: 'overview test 3',
  900. group: Group.lookup(name: 'OverviewTest'),
  901. customer_id: 2,
  902. owner_id: @agent1.id,
  903. state: Ticket::State.lookup(name: 'new'),
  904. priority: Ticket::Priority.lookup(name: '2 normal'),
  905. )
  906. result = Ticket::Overviews.index(@agent1)
  907. assert_equal(result[0][:overview][:id], overview1.id)
  908. assert_equal(result[0][:overview][:name], 'not owned')
  909. assert_equal(result[0][:overview][:view], 'not_owned')
  910. assert_equal(result[0][:tickets].class, Array)
  911. assert_equal(result[0][:tickets][0][:id], ticket1.id)
  912. assert_equal(result[0][:tickets][1][:id], ticket2.id)
  913. assert_equal(result[0][:count], 2)
  914. assert_equal(result[1][:overview][:id], overview2.id)
  915. assert_equal(result[1][:overview][:name], 'not owned by somebody')
  916. assert_equal(result[1][:overview][:view], 'not_owned_by_somebody')
  917. assert_equal(result[1][:tickets].class, Array)
  918. assert_equal(result[1][:tickets][0][:id], ticket3.id)
  919. assert_equal(result[1][:count], 1)
  920. end
  921. end