time_accounting_spec.rb 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Ticket::TimeAccounting, type: :model do
  4. subject(:time_accounting) { create(:'ticket/time_accounting') }
  5. describe 'Associations:' do
  6. describe '#ticket_article' do
  7. subject!(:time_accounting) { create(:'ticket/time_accounting', :for_article) }
  8. context 'when destroyed' do
  9. it 'destroys self' do
  10. expect { time_accounting.ticket_article.destroy }
  11. .to change(time_accounting, :persisted?).to(false)
  12. .and change(described_class, :count).by(-1)
  13. end
  14. it 'does not destroy other TimeAccountings for same ticket' do
  15. create(:'ticket/time_accounting', ticket: time_accounting.ticket)
  16. create(:'ticket/time_accounting', :for_article, ticket: time_accounting.ticket)
  17. expect { time_accounting.ticket_article.destroy }
  18. .to change(described_class, :count).by(-1)
  19. end
  20. end
  21. context 'when recalculating articles' do
  22. let(:ticket) { create(:ticket) }
  23. let(:article1) { create(:ticket_article, ticket: ticket) }
  24. let(:article2) { create(:ticket_article, ticket: ticket) }
  25. it 'one article' do
  26. time_accounting = create(:'ticket/time_accounting', ticket: ticket, ticket_article: article1)
  27. expect(ticket.reload.time_unit).to eq(time_accounting.time_unit)
  28. end
  29. it 'multiple article' do
  30. time_accounting1 = create(:'ticket/time_accounting', ticket: ticket, ticket_article: article1, time_unit: 5.5)
  31. time_accounting2 = create(:'ticket/time_accounting', ticket: ticket, ticket_article: article2, time_unit: 10.5)
  32. expect(ticket.reload.time_unit).to eq(time_accounting1.time_unit + time_accounting2.time_unit)
  33. end
  34. it 'destroy article' do
  35. time_accounting1 = create(:'ticket/time_accounting', ticket: ticket, ticket_article: article1, time_unit: 5.5)
  36. create(:'ticket/time_accounting', ticket: ticket, ticket_article: article2, time_unit: 10.5)
  37. article2.destroy
  38. expect(ticket.reload.time_unit).to eq(time_accounting1.time_unit)
  39. end
  40. it 'destroy time accounting' do
  41. time_accounting1 = create(:'ticket/time_accounting', ticket: ticket, ticket_article: article1, time_unit: 5.5)
  42. time_accounting2 = create(:'ticket/time_accounting', ticket: ticket, ticket_article: article2, time_unit: 10.5)
  43. expect { time_accounting2.destroy }
  44. .to change { ticket.reload.time_unit }
  45. .to time_accounting1.time_unit
  46. end
  47. it 'update time accounting' do
  48. new_time_unit = 99
  49. time_accounting = create(:'ticket/time_accounting', ticket: ticket, ticket_article: article1, time_unit: 5.5)
  50. expect { time_accounting.update! time_unit: new_time_unit }
  51. .to change { ticket.reload.time_unit }
  52. .to new_time_unit
  53. end
  54. it 'destroy all articles' do
  55. create(:'ticket/time_accounting', ticket: ticket, ticket_article: article1, time_unit: 5.5)
  56. create(:'ticket/time_accounting', ticket: ticket, ticket_article: article2, time_unit: 10.5)
  57. article1.destroy
  58. article2.destroy
  59. expect(ticket.reload.time_unit).to eq(0)
  60. end
  61. end
  62. context 'when using time accounting type' do
  63. subject!(:time_accounting) { create(:'ticket/time_accounting', :for_article, type: type) }
  64. let(:type) { nil }
  65. context 'without a type' do
  66. it 'does not have a type' do
  67. expect(time_accounting.type).to be_nil
  68. end
  69. end
  70. context 'with a type' do
  71. let(:type) { create(:ticket_time_accounting_type) }
  72. it 'does not have a type' do
  73. expect(time_accounting.type.name).to eq(type.name)
  74. end
  75. it 'can edit a time accounting type' do
  76. other_type = create(:ticket_time_accounting_type)
  77. time_accounting.update!(type: other_type)
  78. expect(time_accounting.reload.type.name).to eq(other_type.name)
  79. end
  80. it 'can remove time accounting type' do
  81. time_accounting.update!(type: nil)
  82. expect(time_accounting.reload.type).to be_nil
  83. end
  84. end
  85. end
  86. end
  87. end
  88. describe 'validation' do
  89. describe 'article uniqueness' do
  90. let(:ticket) { create(:ticket) }
  91. let(:article) { create(:ticket_article, ticket: ticket) }
  92. before do
  93. create(:ticket_time_accounting, ticket: ticket, ticket_article: article)
  94. end
  95. it 'allows multiple ticket article items per ticket' do
  96. another_article = create(:ticket_article, ticket: ticket)
  97. item = create(:ticket_time_accounting, ticket: ticket, ticket_article: another_article)
  98. expect(item).to be_persisted
  99. end
  100. it 'allows multiple article-less items per ticket' do
  101. expect(create_list(:ticket_time_accounting, 2, ticket: ticket)).to all(be_persisted)
  102. end
  103. it 'does not allow multiple articles for same ticket' do
  104. item = build(:ticket_time_accounting, ticket: ticket, ticket_article: article)
  105. expect(item).not_to be_valid
  106. end
  107. end
  108. describe 'ticket article has to be part of the same ticket' do
  109. let(:ticket) { create(:ticket) }
  110. let(:article) { create(:ticket_article, ticket: ticket) }
  111. let(:article_in_another_ticket) { create(:ticket_article) }
  112. it 'allows article matching ticket' do
  113. time_accounting = build(:ticket_time_accounting, ticket: ticket, ticket_article: article)
  114. expect(time_accounting).to be_valid
  115. end
  116. it 'does not allow article from another ticket' do
  117. time_accounting = build(:ticket_time_accounting, ticket: ticket, ticket_article: article_in_another_ticket)
  118. expect(time_accounting).not_to be_valid
  119. end
  120. end
  121. end
  122. end