has_history_examples.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. RSpec.shared_examples 'HasHistory' do |history_relation_object: []|
  3. describe 'auto-creation of history records' do
  4. let(:histories) { History.where(history_object_id: History::Object.find_by(name: described_class.name)) }
  5. context 'on creation' do
  6. it 'creates a History record for it' do
  7. expect { subject }.to change(histories, :count).by(1)
  8. expect(histories.last.history_type.name).to eq('created')
  9. end
  10. end
  11. context 'on update' do
  12. let(:histories) do
  13. History.where(history_object_id: History::Object.lookup(name: described_class.name).id,
  14. history_type_id: History::Type.lookup(name: 'updated').id,
  15. history_attribute_id: History::Attribute.find_or_create_by(name: attribute).id)
  16. end
  17. let!(:old_value) { subject.send(attribute) }
  18. shared_examples 'attribute update' do
  19. it 'creates a History record for it' do
  20. expect { subject.update(attribute => new_value) }.to change(histories, :count).by(1)
  21. expect(histories.last.attributes).to include(attributes)
  22. end
  23. end
  24. describe 'of #active', if: described_class.attribute_names.include?('active') do
  25. let(:attribute) { 'active' }
  26. let(:new_value) { !subject.active }
  27. let(:attributes) { { 'value_from' => old_value.to_s, 'value_to' => new_value.to_s } }
  28. include_examples 'attribute update'
  29. end
  30. describe 'of #body', if: described_class.attribute_names.include?('body') do
  31. let(:attribute) { 'body' }
  32. let(:new_value) { 'Lorem ipsum dolor' }
  33. let(:attributes) { { 'value_from' => old_value, 'value_to' => new_value } }
  34. include_examples 'attribute update'
  35. end
  36. describe 'of #email', if: described_class.attribute_names.include?('email') do
  37. let(:attribute) { 'email' }
  38. let(:new_value) { Faker::Internet.email }
  39. let(:attributes) { { 'value_from' => old_value, 'value_to' => new_value } }
  40. include_examples 'attribute update'
  41. end
  42. describe 'of #lastname', if: described_class.attribute_names.include?('lastname') do
  43. let(:attribute) { 'lastname' }
  44. let(:new_value) { 'Foo' }
  45. let(:attributes) { { 'value_from' => old_value, 'value_to' => new_value } }
  46. include_examples 'attribute update'
  47. end
  48. describe 'of #name', if: described_class.attribute_names.include?('name') do
  49. let(:attribute) { 'name' }
  50. let(:new_value) { 'Foo' }
  51. let(:attributes) { { 'value_from' => old_value, 'value_to' => new_value } }
  52. include_examples 'attribute update'
  53. end
  54. describe 'of #state', if: described_class.attribute_names.include?('state_id') do
  55. let(:attribute) { 'state' }
  56. let(:new_value) { state_class.where.not(id: old_value.id).first }
  57. let(:state_class) { "#{described_class.name}::State".constantize }
  58. let(:attributes) { { 'value_from' => old_value.name, 'value_to' => new_value.name } }
  59. include_examples 'attribute update'
  60. end
  61. describe 'of #title', if: described_class.attribute_names.include?('title') do
  62. let(:attribute) { 'title' }
  63. let(:new_value) { 'foo' }
  64. let(:attributes) { { 'value_from' => old_value, 'value_to' => new_value } }
  65. include_examples 'attribute update'
  66. end
  67. context 'when validations or callbacks prevent update' do
  68. shared_examples 'failed attribute update' do
  69. it 'does not create a History record for it' do
  70. expect { subject.update(attribute => new_value) }.not_to change(histories, :count)
  71. end
  72. end
  73. describe 'of #owner', if: described_class.attribute_names.include?('owner_id') do
  74. let(:attribute) { 'owner' }
  75. let(:new_value) { create(:customer) } # Ticket#owner is restricted to active agents of the same group
  76. include_examples 'failed attribute update'
  77. end
  78. end
  79. end
  80. end
  81. describe '#history_get' do
  82. context 'without "full" flag' do
  83. it 'delegates to History.list for self' do
  84. expect(History).to receive(:list).with(described_class.name, subject.id, history_relation_object)
  85. subject.history_get
  86. end
  87. end
  88. context 'with "full" flag' do
  89. it 'returns a hash including History.list for self' do
  90. expect(subject.history_get(true))
  91. .to include(history: History.list(described_class.name, subject.id, history_relation_object))
  92. end
  93. it 'returns a hash including FE assets of self and related objects' do
  94. expect(subject.history_get(true)[:assets][described_class.to_app_model]).to include(subject.assets({})[described_class.to_app_model])
  95. end
  96. end
  97. end
  98. end