sorting_spec.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Ticket::Overviews > Sorting' do # rubocop:disable RSpec/DescribeClass
  4. let(:overview) do
  5. create(
  6. :overview,
  7. condition: {
  8. 'ticket.state_id' => {
  9. operator: 'is',
  10. value: Ticket::State.pluck(:id),
  11. },
  12. },
  13. )
  14. end
  15. let(:user) { create(:agent, groups: [Group.first], preferences: { 'locale' => locale }) }
  16. let(:result) { Ticket::Overviews.tickets_for_overview(overview, user, order_by:, order_direction:).map(&:id) }
  17. before do
  18. Ticket.destroy_all
  19. user && tickets && overview
  20. end
  21. shared_examples 'it sorts correctly' do
  22. it 'sorts correctly' do
  23. expect(result).to eq(sorted_tickets)
  24. end
  25. end
  26. context 'when sorting by state_id' do
  27. let(:order_by) { 'state_id' }
  28. let(:tickets) do
  29. states = Ticket::State.where(name: %w[open new closed]).map { |state| { state.name => state.id } }
  30. create_list(:ticket, 3, group_id: Group.first.id).tap { |tickets| states.each_with_index { |s, idx| tickets[idx].update!(state_id: s.values.first) } }
  31. end
  32. context "when language is set to 'de-de'" do
  33. let(:locale) { 'de-de' }
  34. let(:sorted_tickets) { tickets.sort_by { |ticket| Translation.translate(locale, ticket.state.name) }.pluck(:id) }
  35. context 'when ascending' do
  36. let(:order_direction) { 'ASC' }
  37. it_behaves_like 'it sorts correctly'
  38. end
  39. context 'when descending' do
  40. let(:order_direction) { 'DESC' }
  41. let(:sorted_tickets) { tickets.sort_by { |ticket| Translation.translate(locale, ticket.state.name) }.pluck(:id).reverse }
  42. it_behaves_like 'it sorts correctly'
  43. end
  44. end
  45. end
  46. context 'when sorting by group_id' do
  47. let(:order_by) { 'group_id' }
  48. let(:locale) { 'en-us' }
  49. let(:sorted_tickets) { tickets.sort_by { |ticket| ticket.group.name.downcase }.pluck(:id) }
  50. let(:tickets) do
  51. groups = create_list(:group, 10).tap { |gs| gs.each { |g| g.update!(name: Faker::App.unique.name) } }
  52. user.update!(group_ids: Group.pluck(:id))
  53. create_list(:ticket, 10).tap { |tickets| tickets.each_with_index { |t, idx| t.update!(group_id: groups[idx].id) } }
  54. end
  55. context 'when ascending' do
  56. let(:order_direction) { 'ASC' }
  57. it_behaves_like 'it sorts correctly'
  58. end
  59. context 'when descending' do
  60. let(:order_direction) { 'DESC' }
  61. let(:sorted_tickets) { tickets.sort_by { |ticket| ticket.group.name.downcase }.pluck(:id).reverse }
  62. it_behaves_like 'it sorts correctly'
  63. end
  64. end
  65. context 'when sorting by customer_id' do
  66. let(:order_by) { 'customer_id' }
  67. let(:locale) { 'en-us' }
  68. let(:tickets) do
  69. groups = create_list(:group, 10).tap { |gs| gs.each { |g| g.update!(name: Faker::App.unique.name) } }
  70. user.update!(group_ids: Group.pluck(:id))
  71. create_list(:ticket, 10).tap { |tickets| tickets.each_with_index { |t, idx| t.update!(group_id: groups[idx].id) } }
  72. end
  73. context 'when ascending' do
  74. let(:order_direction) { 'ASC' }
  75. let(:sorted_tickets) { tickets.sort_by { |ticket| ticket.customer.fullname.downcase }.pluck(:id) }
  76. it_behaves_like 'it sorts correctly'
  77. end
  78. context 'when descending' do
  79. let(:order_direction) { 'DESC' }
  80. let(:sorted_tickets) { tickets.sort_by { |ticket| ticket.customer.fullname.downcase }.pluck(:id).reverse }
  81. it_behaves_like 'it sorts correctly'
  82. end
  83. end
  84. context 'when grouping and sorting' do
  85. let(:overview) { super().tap { _1.update! group_by: 'customer_id', group_direction: 'ASC' } }
  86. let(:customers) { create_list(:customer, 3) }
  87. let(:order_by) { 'title' }
  88. let(:locale) { 'en-us' }
  89. let(:tickets) do
  90. customers.flat_map do |customer|
  91. Array.new(3) do
  92. create(:ticket, customer:, group: Group.first, title: Faker::Lorem.sentence)
  93. end
  94. end
  95. end
  96. context 'when ascending' do
  97. let(:order_direction) { 'ASC' }
  98. let(:sorted_tickets) do
  99. tickets
  100. .sort do |a, b|
  101. initial = a.customer.fullname.downcase <=> b.customer.fullname.downcase
  102. next initial if !initial.zero?
  103. a.title.downcase <=> b.title.downcase
  104. end
  105. .pluck(:id)
  106. end
  107. it_behaves_like 'it sorts correctly'
  108. end
  109. context 'when descending' do
  110. let(:order_direction) { 'DESC' }
  111. let(:sorted_tickets) do
  112. tickets
  113. .sort do |a, b|
  114. initial = a.customer.fullname.downcase <=> b.customer.fullname.downcase
  115. next initial if !initial.zero?
  116. b.title.downcase <=> a.title.downcase
  117. end
  118. .pluck(:id)
  119. end
  120. it_behaves_like 'it sorts correctly'
  121. end
  122. end
  123. end