diff --git a/spec/models/ticket/number/date_spec.rb b/spec/models/ticket/number/date_spec.rb new file mode 100644 index 000000000..f97cb04e5 --- /dev/null +++ b/spec/models/ticket/number/date_spec.rb @@ -0,0 +1,86 @@ +require 'rails_helper' + +RSpec.describe Ticket::Number::Date do + describe '.generate' do + let(:number) { described_class.generate } + + before { travel_to(Time.zone.parse('1955-11-05')) } + + context 'with a "ticket_number_date" setting with checksum: false (default)' do + context 'and a single-digit system_id' do + before { Setting.set('system_id', 1) } + + it 'returns a string following the pattern date + system_id + zero-padded number' do + expect(number).to eq('1955110510001') + end + end + + context 'and a two-digit system_id' do + before { Setting.set('system_id', 88) } + + it 'returns a string following the pattern date + system_id + zero-padded number' do + expect(number).to eq('19551105880001') + end + end + end + + context 'with a "ticket_number_date" setting with checksum: true' do + before { Setting.set('ticket_number_date', { checksum: true }) } + + context 'and a single-digit system_id' do + before { Setting.set('system_id', 1) } + + it 'returns a string following the pattern date + system_id + zero-padded number + checksum' do + expect(number).to eq('19551105100012') + end + end + + context 'and a two-digit system_id' do + before { Setting.set('system_id', 88) } + + it 'returns a string following the pattern date + system_id + zero-padded number + checksum' do + expect(number).to eq('195511058800012') + end + end + end + end + + describe '.check' do + context 'for tickets with date-style numbers' do + let(:ticket) { create(:ticket, number: ticket_number) } + let(:ticket_number) { "19551105#{Setting.get('system_id')}0001" } + let(:check_query) { ticket.subject_build(ticket.title) } + + context 'when system_id is the same as when ticket was created' do + before do + Setting.set('system_id', 1) + ticket # create ticket + end + + it 'returns the ticket matching the number in the given string' do + expect(described_class.check(check_query)).to eq(ticket) + end + end + + context 'when system_id is different from when ticket was created' do + before do + Setting.set('system_id', 1) + ticket # create ticket + Setting.set('system_id', 999) + end + + it 'returns nil' do + expect(described_class.check(check_query)).to be(nil) + end + + context 'and "ticket_number_ignore_system_id" is true' do + before { Setting.set('ticket_number_ignore_system_id', true) } + + it 'returns the ticket matching the number in the given string' do + expect(described_class.check(check_query)).to eq(ticket) + end + end + end + end + end +end diff --git a/spec/models/ticket/number/increment_spec.rb b/spec/models/ticket/number/increment_spec.rb new file mode 100644 index 000000000..899a4fd99 --- /dev/null +++ b/spec/models/ticket/number/increment_spec.rb @@ -0,0 +1,114 @@ +require 'rails_helper' + +RSpec.describe Ticket::Number::Increment do + describe '.generate' do + let(:number) { described_class.generate } + let(:system_id) { Setting.get('system_id') } + let(:ticket_count) { Ticket::Counter.find_by(generator: 'Increment').content } + + context 'with a "ticket_number_increment" setting with...' do + context 'min_size: 5' do + before { Setting.set('ticket_number_increment', { min_size: 5 }) } + + it 'returns a 5-character string' do + expect(number).to be_a(String) + expect(number.length).to be(5) + end + + context 'when "system_id" setting exceeds :min_size' do + before { Setting.set('system_id', 123_456) } + + it 'still adheres to numbering pattern (and does not require padding zeroes)' do + expect(number).to match(/^#{system_id}#{ticket_count}$/) + end + end + + it 'returns a string following the pattern system_id + padding zeroes + ticket_count' do + expect(number).to match(/^#{system_id}0*#{ticket_count}$/) + end + + context '/ checksum: false (default)' do + before { Setting.set('ticket_number_increment', { min_size: 5, checksum: false }) } + + it 'returns a 5-character string' do + expect(number).to be_a(String) + expect(number.length).to be(5) + end + + it 'returns a string following the pattern system_id + padding zeroes + ticket_counter' do + expect(number).to match(/^#{system_id}0*#{ticket_count}$/) + end + + context 'when "system_id" setting exceeds :min_size' do + before { Setting.set('system_id', 123_456) } + + it 'still adheres to numbering pattern (and does not require padding zeroes)' do + expect(number).to match(/^#{system_id}#{ticket_count}$/) + end + end + end + + context '/ checksum: true' do + before { Setting.set('ticket_number_increment', { min_size: 5, checksum: true }) } + + it 'returns a 5-character string' do + expect(number).to be_a(String) + expect(number).to eq(number.to_i.to_s) + expect(number.length).to be(5) + end + + it 'returns a string following the pattern system_id + padding zeroes + ticket_counter + checksum' do + expect(number).to match(/^#{system_id}0*#{ticket_count}\d$/) + end + + context 'when "system_id" setting exceeds :min_size' do + before { Setting.set('system_id', 123_456) } + + it 'still adheres to numbering pattern (and does not require padding zeroes)' do + expect(number).to match(/^#{system_id}#{ticket_count}\d$/) + end + end + end + end + end + end + + describe '.check' do + context 'for tickets with increment-style numbers' do + let(:ticket) { create(:ticket, number: ticket_number) } + let(:ticket_number) { "#{Setting.get('system_id')}0001" } + let(:check_query) { ticket.subject_build(ticket.title) } + + context 'when system_id is the same as when ticket was created' do + before do + Setting.set('system_id', 1) + ticket # create ticket + end + + it 'returns the ticket matching the number in the given string' do + expect(described_class.check(check_query)).to eq(ticket) + end + end + + context 'when system_id is different from when ticket was created' do + before do + Setting.set('system_id', 1) + ticket # create ticket + Setting.set('system_id', 999) + end + + it 'returns nil' do + expect(described_class.check(check_query)).to be(nil) + end + + context 'and "ticket_number_ignore_system_id" is true' do + before { Setting.set('ticket_number_ignore_system_id', true) } + + it 'returns the ticket matching the number in the given string' do + expect(described_class.check(check_query)).to eq(ticket) + end + end + end + end + end +end diff --git a/spec/models/ticket/number_spec.rb b/spec/models/ticket/number_spec.rb new file mode 100644 index 000000000..7c28108f1 --- /dev/null +++ b/spec/models/ticket/number_spec.rb @@ -0,0 +1,36 @@ +require 'rails_helper' + +RSpec.describe Ticket::Number do + let(:stubbed_subclass) { double('Foo') } + + before { stub_const('Ticket::Number::Foo', stubbed_subclass) } + + describe '.generate' do + before { Setting.set('ticket_number', 'Ticket::Number::Foo') } + + it 'defers to subclass specified in "ticket_number" setting' do + expect(Ticket::Number::Foo).to receive(:generate) + expect(described_class.generate).to be(nil) + end + end + + describe '.check' do + before { Setting.set('ticket_number', 'Ticket::Number::Foo') } + + it 'defers to subclass specified in "ticket_number" setting' do + expect(Ticket::Number::Foo).to receive(:check).with('foo') + expect(described_class.check('foo')).to be(nil) + end + end + + describe '.adapter' do + it 'defaults to Ticket::Number::Increment' do + expect(described_class.adapter).to be(Ticket::Number::Increment) + end + + it 'depends on "ticket_number" setting' do + expect { Setting.set('ticket_number', 'Ticket::Number::Foo') } + .to change(described_class, :adapter).to(Ticket::Number::Foo) + end + end +end diff --git a/test/unit/ticket_number_test.rb b/test/unit/ticket_number_test.rb deleted file mode 100644 index d97cca3f0..000000000 --- a/test/unit/ticket_number_test.rb +++ /dev/null @@ -1,166 +0,0 @@ -require 'test_helper' - -class TicketNumberTest < ActiveSupport::TestCase - test 'number' do - Setting.set('ticket_number_increment', { checksum: false, min_size: 5 }) - Setting.set('system_id', 1) - - number = Ticket::Number.generate - assert_equal(number.to_s.length, 5) - - Setting.set('ticket_number_increment', { checksum: false, min_size: 10 }) - Setting.set('system_id', 1) - - number = Ticket::Number.generate - assert_equal(number.to_s.length, 10) - - Setting.set('ticket_number_increment', { checksum: true, min_size: 5 }) - Setting.set('system_id', 1) - - number = Ticket::Number.generate - assert_equal(number.to_s.length, 5) - - Setting.set('ticket_number_increment', { checksum: true, min_size: 10 }) - Setting.set('system_id', 1) - - number = Ticket::Number.generate - assert_equal(number.to_s.length, 10) - - Setting.set('ticket_number_increment', { checksum: false, min_size: 5 }) - Setting.set('system_id', 88) - - number = Ticket::Number.generate - assert_equal(number.to_s.length, 5) - - Setting.set('ticket_number_increment', { checksum: false, min_size: 10 }) - Setting.set('system_id', 88) - - number = Ticket::Number.generate - assert_equal(number.to_s.length, 10) - - Setting.set('ticket_number_increment', { checksum: true, min_size: 5 }) - Setting.set('system_id', 88) - - number = Ticket::Number.generate - assert_equal(number.to_s.length, 5) - - Setting.set('ticket_number_increment', { checksum: true, min_size: 10 }) - Setting.set('system_id', 88) - - number = Ticket::Number.generate - assert_equal(number.to_s.length, 10) - - 150.times do - number = Ticket::Number.generate - assert_equal(number.to_s.length, 10) - end - - end - - test 'number check' do - Setting.set('ticket_number_increment', { checksum: false, min_size: 5 }) - - Setting.set('system_id', 1) - - ticket = Ticket.create!( - title: 'test 1', - group: Group.lookup(name: 'Users'), - customer_id: 2, - state: Ticket::State.lookup(name: 'new'), - priority: Ticket::Priority.lookup(name: '2 normal'), - updated_by_id: 1, - created_by_id: 1, - ) - subject = ticket.subject_build(ticket.title) - - ticket_check = Ticket::Number.check(subject) - assert_equal(ticket.id, ticket_check.id) - - Setting.set('system_id', 999) - - ticket_check = Ticket::Number.check(subject) - assert_not(ticket_check) - - Setting.set('ticket_number_ignore_system_id', true) - - ticket_check = Ticket::Number.check(subject) - assert_equal(ticket.id, ticket_check.id) - end - - test 'date' do - Setting.set('ticket_number', 'Ticket::Number::Date') - Setting.set('ticket_number_date', { checksum: false }) - Setting.set('system_id', 1) - system_id = Setting.get('system_id') - number_prefix = "#{Time.zone.now.strftime('%Y%m%d')}#{system_id}" - - number = Ticket::Number.generate - assert_equal(number.to_s.length, 13) - assert_match(/#{number_prefix}/, number.to_s) - - Setting.set('ticket_number_date', { checksum: false }) - Setting.set('system_id', 88) - - number = Ticket::Number.generate - system_id = Setting.get('system_id') - number_prefix = "#{Time.zone.now.strftime('%Y%m%d')}#{system_id}" - assert_equal(number.to_s.length, 14) - assert_match(/#{number_prefix}/, number.to_s) - - Setting.set('ticket_number_date', { checksum: true }) - Setting.set('system_id', 1) - - number = Ticket::Number.generate - system_id = Setting.get('system_id') - number_prefix = "#{Time.zone.now.strftime('%Y%m%d')}#{system_id}" - assert_equal(number.to_s.length, 14) - assert_match(/#{number_prefix}/, number.to_s) - - Setting.set('ticket_number_date', { checksum: true }) - Setting.set('system_id', 88) - - number = Ticket::Number.generate - system_id = Setting.get('system_id') - number_prefix = "#{Time.zone.now.strftime('%Y%m%d')}#{system_id}" - assert_equal(number.to_s.length, 15) - assert_match(/#{number_prefix}/, number.to_s) - - 150.times do - number = Ticket::Number.generate - assert_equal(number.to_s.length, 15) - end - - end - - test 'date check' do - Setting.set('ticket_number', 'Ticket::Number::Date') - Setting.set('ticket_number_date', { checksum: false }) - - Setting.set('system_id', 1) - - ticket = Ticket.create!( - title: 'test 1', - group: Group.lookup(name: 'Users'), - customer_id: 2, - state: Ticket::State.lookup(name: 'new'), - priority: Ticket::Priority.lookup(name: '2 normal'), - updated_by_id: 1, - created_by_id: 1, - ) - subject = ticket.subject_build(ticket.title) - - ticket_check = Ticket::Number.check(subject) - assert_equal(ticket.id, ticket_check.id) - - Setting.set('system_id', 999) - - ticket_check = Ticket::Number.check(subject) - assert_not(ticket_check) - - Setting.set('ticket_number_ignore_system_id', true) - - ticket_check = Ticket::Number.check(subject) - assert_equal(ticket.id, ticket_check.id) - end - -end