2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2017-03-27 14:06:18 +00:00
|
|
|
require 'rails_helper'
|
2019-01-22 16:35:01 +00:00
|
|
|
require 'models/application_model_examples'
|
2019-01-28 06:04:05 +00:00
|
|
|
require 'models/concerns/can_be_imported_examples'
|
2020-02-20 13:34:03 +00:00
|
|
|
require 'models/concerns/has_collection_update_examples'
|
2021-04-12 09:49:26 +00:00
|
|
|
require 'models/concerns/has_xss_sanitized_note_examples'
|
2017-03-27 14:06:18 +00:00
|
|
|
|
2019-01-22 16:35:01 +00:00
|
|
|
RSpec.describe Ticket::State, type: :model do
|
2019-01-24 10:13:04 +00:00
|
|
|
it_behaves_like 'ApplicationModel'
|
2019-01-28 06:04:05 +00:00
|
|
|
it_behaves_like 'CanBeImported'
|
2020-02-20 13:34:03 +00:00
|
|
|
it_behaves_like 'HasCollectionUpdate', collection_factory: :ticket_state
|
2021-04-12 09:49:26 +00:00
|
|
|
it_behaves_like 'HasXssSanitizedNote', model_factory: :ticket_state
|
2017-03-27 14:06:18 +00:00
|
|
|
|
2019-02-14 09:44:03 +00:00
|
|
|
describe 'Default state' do
|
|
|
|
describe 'of whole table:' do
|
|
|
|
it 'has seven records' do
|
2019-09-16 15:04:17 +00:00
|
|
|
expect(described_class.pluck(:name))
|
2019-02-14 09:44:03 +00:00
|
|
|
.to match_array(%w[closed merged new open pending\ close pending\ reminder removed])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'of "new" state:' do
|
|
|
|
it 'is the sole #default_create state' do
|
2019-09-16 15:04:17 +00:00
|
|
|
expect(described_class.where(default_create: true))
|
|
|
|
.to match_array([described_class.find_by(name: 'new')])
|
2019-02-14 09:44:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'of "open" state:' do
|
|
|
|
it 'is the sole #default_follow_up state' do
|
2019-09-16 15:04:17 +00:00
|
|
|
expect(described_class.where(default_follow_up: true))
|
|
|
|
.to match_array([described_class.find_by(name: 'open')])
|
2019-02-14 09:44:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Class methods:' do
|
|
|
|
describe '.by_category' do
|
|
|
|
it 'looks up states by category' do
|
|
|
|
expect(described_class.by_category(:open))
|
|
|
|
.to be_an(ActiveRecord::Relation)
|
2019-09-16 15:04:17 +00:00
|
|
|
.and include(instance_of(described_class))
|
2019-02-14 09:44:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with invalid category name' do
|
2021-07-21 14:04:17 +00:00
|
|
|
it 'raises ArgumentError' do
|
2019-02-14 09:44:03 +00:00
|
|
|
expect { described_class.by_category(:invalidcategoryname) }
|
2021-07-21 14:04:17 +00:00
|
|
|
.to raise_error(ArgumentError)
|
2019-02-14 09:44:03 +00:00
|
|
|
end
|
|
|
|
end
|
2017-03-27 14:06:18 +00:00
|
|
|
end
|
2019-02-14 09:44:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Attributes:' do
|
|
|
|
describe '#default_create' do
|
2019-09-16 15:04:17 +00:00
|
|
|
let!(:original_default) { described_class.find_by(default_create: true) }
|
2019-02-14 09:44:03 +00:00
|
|
|
|
|
|
|
context 'for newly created record' do
|
|
|
|
subject!(:state) { build(:ticket_state, default_create: default_create) }
|
|
|
|
|
|
|
|
context 'when true' do
|
|
|
|
let(:default_create) { true }
|
|
|
|
|
|
|
|
it 'unsets previous default' do
|
|
|
|
expect { state.save }
|
|
|
|
.to change { original_default.reload.default_create }.to(false)
|
2019-09-16 15:04:17 +00:00
|
|
|
.and not_change { described_class.where(default_create: true).count }
|
2019-02-14 09:44:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when false' do
|
|
|
|
let(:default_create) { false }
|
|
|
|
|
|
|
|
it 'does not alter existing default' do
|
|
|
|
expect { state.save }
|
2019-09-16 15:04:17 +00:00
|
|
|
.to not_change { described_class.find_by(default_create: true) }
|
|
|
|
.and not_change { described_class.where(default_create: true).count }
|
2019-02-14 09:44:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for existing record' do
|
|
|
|
subject!(:state) { create(:ticket_state, default_create: default_create) }
|
|
|
|
|
|
|
|
context 'when true' do
|
|
|
|
let(:default_create) { true }
|
|
|
|
|
|
|
|
context 'and updated to false' do
|
|
|
|
it 'assigns Ticket::State.first as default' do
|
|
|
|
expect { state.update(default_create: false) }
|
2019-09-16 15:04:17 +00:00
|
|
|
.to change { described_class.first.default_create }.to(true)
|
|
|
|
.and not_change { described_class.where(default_create: true).count }
|
2019-02-14 09:44:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and destroyed' do
|
|
|
|
it 'assigns Ticket::State.first as default' do
|
|
|
|
expect { state.destroy }
|
2019-09-16 15:04:17 +00:00
|
|
|
.to change { described_class.first.default_create }.to(true)
|
|
|
|
.and not_change { described_class.where(default_create: true).count }
|
2019-02-14 09:44:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when false' do
|
|
|
|
let(:default_create) { false }
|
|
|
|
|
|
|
|
context 'and updated to true' do
|
|
|
|
it 'unsets previous default' do
|
|
|
|
expect { state.update(default_create: true) }
|
|
|
|
.to change { original_default.reload.default_create }.to(false)
|
2019-09-16 15:04:17 +00:00
|
|
|
.and not_change { described_class.where(default_create: true).count }
|
2019-02-14 09:44:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and destroyed' do
|
|
|
|
it 'does not alter existing default' do
|
|
|
|
expect { state.destroy }
|
2019-09-16 15:04:17 +00:00
|
|
|
.to not_change { described_class.find_by(default_create: true) }
|
|
|
|
.and not_change { described_class.where(default_create: true).count }
|
2019-02-14 09:44:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#default_follow_up' do
|
2019-09-16 15:04:17 +00:00
|
|
|
let!(:original_default) { described_class.find_by(default_follow_up: true) }
|
2019-02-14 09:44:03 +00:00
|
|
|
|
|
|
|
context 'for newly created record' do
|
|
|
|
subject!(:state) { build(:ticket_state, default_follow_up: default_follow_up) }
|
|
|
|
|
|
|
|
context 'when true' do
|
|
|
|
let(:default_follow_up) { true }
|
|
|
|
|
|
|
|
it 'unsets previous default' do
|
|
|
|
expect { state.save }
|
|
|
|
.to change { original_default.reload.default_follow_up }.to(false)
|
2019-09-16 15:04:17 +00:00
|
|
|
.and not_change { described_class.where(default_follow_up: true).count }
|
2019-02-14 09:44:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when false' do
|
|
|
|
let(:default_follow_up) { false }
|
|
|
|
|
|
|
|
it 'does not alter existing default' do
|
|
|
|
expect { state.save }
|
2019-09-16 15:04:17 +00:00
|
|
|
.to not_change { described_class.find_by(default_follow_up: true) }
|
|
|
|
.and not_change { described_class.where(default_follow_up: true).count }
|
2019-02-14 09:44:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for existing record' do
|
|
|
|
subject!(:state) { create(:ticket_state, default_follow_up: default_follow_up) }
|
|
|
|
|
|
|
|
context 'when true' do
|
|
|
|
let(:default_follow_up) { true }
|
|
|
|
|
|
|
|
context 'and updated to false' do
|
|
|
|
it 'assigns Ticket::State.first as default' do
|
|
|
|
expect { state.update(default_follow_up: false) }
|
2019-09-16 15:04:17 +00:00
|
|
|
.to change { described_class.first.default_follow_up }.to(true)
|
|
|
|
.and not_change { described_class.where(default_follow_up: true).count }
|
2019-02-14 09:44:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and destroyed' do
|
|
|
|
it 'assigns Ticket::State.first as default' do
|
|
|
|
expect { state.destroy }
|
2019-09-16 15:04:17 +00:00
|
|
|
.to change { described_class.first.default_follow_up }.to(true)
|
|
|
|
.and not_change { described_class.where(default_follow_up: true).count }
|
2019-02-14 09:44:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when false' do
|
|
|
|
let(:default_follow_up) { false }
|
|
|
|
|
|
|
|
context 'and updated to true' do
|
|
|
|
it 'unsets previous default' do
|
|
|
|
expect { state.update(default_follow_up: true) }
|
|
|
|
.to change { original_default.reload.default_follow_up }.to(false)
|
2019-09-16 15:04:17 +00:00
|
|
|
.and not_change { described_class.where(default_follow_up: true).count }
|
2019-02-14 09:44:03 +00:00
|
|
|
end
|
|
|
|
end
|
2017-03-27 14:06:18 +00:00
|
|
|
|
2019-02-14 09:44:03 +00:00
|
|
|
context 'and destroyed' do
|
|
|
|
it 'does not alter existing default' do
|
|
|
|
expect { state.destroy }
|
2019-09-16 15:04:17 +00:00
|
|
|
.to not_change { described_class.find_by(default_follow_up: true) }
|
|
|
|
.and not_change { described_class.where(default_follow_up: true).count }
|
2019-02-14 09:44:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-01-22 16:35:01 +00:00
|
|
|
end
|
2017-03-27 14:06:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|