increment_spec.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Ticket::Number::Increment do
  4. describe '.generate' do
  5. let(:number) { described_class.generate }
  6. let(:system_id) { Setting.get('system_id') }
  7. let(:ticket_count) { Ticket::Counter.find_by(generator: 'Increment').content }
  8. it 'updates the "Increment" Ticket::Counter' do
  9. expect { number }
  10. .to change { Ticket::Counter.find_by(generator: 'Increment').content }
  11. end
  12. context 'with a "ticket_number_increment" setting with...' do
  13. context 'min_size: 5' do
  14. before { Setting.set('ticket_number_increment', { min_size: 5 }) }
  15. it 'returns a 5-character string' do
  16. expect(number).to be_a(String)
  17. expect(number.length).to be(5)
  18. end
  19. context 'when "system_id" setting exceeds :min_size' do
  20. before { Setting.set('system_id', 123_456) }
  21. it 'still adheres to numbering pattern (and does not require padding zeroes)' do
  22. expect(number).to match(%r{^#{system_id}#{ticket_count}$})
  23. end
  24. end
  25. it 'returns a string following the pattern system_id + padding zeroes + ticket_count' do
  26. expect(number).to match(%r{^#{system_id}0*#{ticket_count}$})
  27. end
  28. context '/ checksum: false (default)' do
  29. before { Setting.set('ticket_number_increment', { min_size: 5, checksum: false }) }
  30. it 'returns a 5-character string' do
  31. expect(number).to be_a(String)
  32. expect(number.length).to be(5)
  33. end
  34. it 'returns a string following the pattern system_id + padding zeroes + ticket_counter' do
  35. expect(number).to match(%r{^#{system_id}0*#{ticket_count}$})
  36. end
  37. context 'when "system_id" setting exceeds :min_size' do
  38. before { Setting.set('system_id', 123_456) }
  39. it 'still adheres to numbering pattern (and does not require padding zeroes)' do
  40. expect(number).to match(%r{^#{system_id}#{ticket_count}$})
  41. end
  42. end
  43. end
  44. context '/ checksum: true' do
  45. before { Setting.set('ticket_number_increment', { min_size: 5, checksum: true }) }
  46. it 'returns a 5-character string' do
  47. expect(number).to be_a(String)
  48. expect(number).to eq(number.to_i.to_s)
  49. expect(number.length).to be(5)
  50. end
  51. it 'returns a string following the pattern system_id + padding zeroes + ticket_counter + checksum' do
  52. expect(number).to match(%r{^#{system_id}0*#{ticket_count}\d$})
  53. end
  54. context 'when "system_id" setting exceeds :min_size' do
  55. before { Setting.set('system_id', 123_456) }
  56. it 'still adheres to numbering pattern (and does not require padding zeroes)' do
  57. expect(number).to match(%r{^#{system_id}#{ticket_count}\d$})
  58. end
  59. end
  60. end
  61. end
  62. end
  63. end
  64. describe '.check' do
  65. context 'for tickets with increment-style numbers' do
  66. let(:ticket) do
  67. # There might be conflicts with the hardcoded ticket number
  68. # and the one from the welcome ticket, so use find_by || create.
  69. Ticket.find_by(number: ticket_number) || create(:ticket, number: ticket_number)
  70. end
  71. let(:ticket_number) { "#{Setting.get('system_id')}0001" }
  72. let(:check_query) { ticket.subject_build(ticket.title) }
  73. context 'when system_id is the same as when ticket was created' do
  74. before do
  75. Setting.set('system_id', 1)
  76. ticket # create ticket
  77. end
  78. it 'returns the ticket matching the number in the given string' do
  79. expect(described_class.check(check_query)).to eq(ticket)
  80. end
  81. end
  82. context 'when system_id is different from when ticket was created' do
  83. before do
  84. Setting.set('system_id', 1)
  85. ticket # create ticket
  86. Setting.set('system_id', 999)
  87. end
  88. it 'returns nil' do
  89. expect(described_class.check(check_query)).to be_nil
  90. end
  91. context 'and "ticket_number_ignore_system_id" is true' do
  92. before { Setting.set('ticket_number_ignore_system_id', true) }
  93. it 'returns the ticket matching the number in the given string' do
  94. expect(described_class.check(check_query)).to eq(ticket)
  95. end
  96. end
  97. end
  98. end
  99. end
  100. end