date_spec.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Ticket::Number::Date do
  4. describe '.generate' do
  5. let(:number) { described_class.generate }
  6. before { travel_to(Time.zone.parse('1955-11-05')) }
  7. it 'updates the "Date" Ticket::Counter' do
  8. expect { number }
  9. .to change { Ticket::Counter.find_by(generator: 'Date')&.content }
  10. end
  11. context 'with a "ticket_number_date" setting with checksum: false (default)' do
  12. context 'and a single-digit system_id' do
  13. before { Setting.set('system_id', 1) }
  14. it 'returns a string following the pattern date + system_id + zero-padded number' do
  15. expect(number).to eq('1955110510001')
  16. end
  17. end
  18. context 'and a two-digit system_id' do
  19. before { Setting.set('system_id', 88) }
  20. it 'returns a string following the pattern date + system_id + zero-padded number' do
  21. expect(number).to eq('19551105880001')
  22. end
  23. end
  24. end
  25. context 'with a "ticket_number_date" setting with checksum: true' do
  26. before { Setting.set('ticket_number_date', { checksum: true }) }
  27. context 'and a single-digit system_id' do
  28. before { Setting.set('system_id', 1) }
  29. it 'returns a string following the pattern date + system_id + zero-padded number + checksum' do
  30. expect(number).to eq('19551105100012')
  31. end
  32. end
  33. context 'and a two-digit system_id' do
  34. before { Setting.set('system_id', 88) }
  35. it 'returns a string following the pattern date + system_id + zero-padded number + checksum' do
  36. expect(number).to eq('195511058800012')
  37. end
  38. end
  39. end
  40. end
  41. describe '.check' do
  42. context 'for tickets with date-style numbers' do
  43. let(:ticket) { create(:ticket, number: ticket_number) }
  44. let(:ticket_number) { "19551105#{Setting.get('system_id')}0001" }
  45. let(:check_query) { ticket.subject_build(ticket.title) }
  46. context 'when system_id is the same as when ticket was created' do
  47. before do
  48. Setting.set('system_id', 1)
  49. ticket # create ticket
  50. end
  51. it 'returns the ticket matching the number in the given string' do
  52. expect(described_class.check(check_query)).to eq(ticket)
  53. end
  54. end
  55. context 'when system_id is different from when ticket was created' do
  56. before do
  57. Setting.set('system_id', 1)
  58. ticket # create ticket
  59. Setting.set('system_id', 999)
  60. end
  61. it 'returns nil' do
  62. expect(described_class.check(check_query)).to be_nil
  63. end
  64. context 'and "ticket_number_ignore_system_id" is true' do
  65. before { Setting.set('ticket_number_ignore_system_id', true) }
  66. it 'returns the ticket matching the number in the given string' do
  67. expect(described_class.check(check_query)).to eq(ticket)
  68. end
  69. end
  70. end
  71. end
  72. end
  73. end