item_spec.rb 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Tag::Item do
  4. subject(:item) { create(:'tag/item') }
  5. describe '.rename' do
  6. context 'when given a unique item name' do
  7. it 'updates the name on the Tag::Item' do
  8. expect { described_class.rename(id: item.id, name: 'foo') }
  9. .to change { item.reload.name }.to('foo')
  10. end
  11. it 'strips trailing/leading whitespace' do
  12. expect { described_class.rename(id: item.id, name: ' foo ') }
  13. .to change { item.reload.name }.to('foo')
  14. end
  15. end
  16. context 'when given a conflicting/existing item name' do
  17. let!(:item_2) { create(:'tag/item', name: 'foo') }
  18. context 'with no redundant tags' do
  19. let!(:tag) { create(:tag, o: Ticket.first, tag_item: item) }
  20. it 'reassigns all tags from old-name item to new-name item' do
  21. expect { described_class.rename(id: item.id, name: item_2.name) }
  22. .to change { tag.reload.tag_item }.to(item_2)
  23. .and change { Ticket.first.tag_list }.to([item_2.name])
  24. end
  25. it 'strips trailing/leading whitespace' do
  26. expect { described_class.rename(id: item.id, name: " #{item_2.name} ") }
  27. .to change { tag.reload.tag_item }.to(item_2)
  28. .and change { Ticket.first.tag_list }.to([item_2.name])
  29. end
  30. end
  31. context 'with redundant tags' do
  32. let!(:tags) do
  33. [create(:tag, o: Ticket.first, tag_item: item),
  34. create(:tag, o: Ticket.first, tag_item: item_2)]
  35. end
  36. it 'removes the tag assigned to old-name item' do
  37. expect { described_class.rename(id: item.id, name: item_2.name) }
  38. .to change { Tag.exists?(id: tags.first.id) }.to(false)
  39. .and change { Ticket.first.tag_list }.to([item_2.name])
  40. end
  41. it 'strips trailing/leading whitespace' do
  42. expect { described_class.rename(id: item.id, name: " #{item_2.name} ") }
  43. .to change { Tag.exists?(id: tags.first.id) }.to(false)
  44. .and change { Ticket.first.tag_list }.to([item_2.name])
  45. end
  46. end
  47. it 'deletes the original item' do
  48. expect { described_class.rename(id: item.id, name: item_2.name) }
  49. .to change { described_class.exists?(name: item.name) }.to(false)
  50. end
  51. end
  52. shared_examples 'updating references to tag names' do |object_klass:, method:, label: 'ticket.tags'|
  53. subject(:item) { create(:'tag/item', name: 'test1') }
  54. context "with reference to renamed tag in its #{method} hash (contains-one)" do
  55. let(:object) { create(object_klass.name.underscore, method => { label => tag_matcher }) }
  56. let(:tag_matcher) { { operator: 'contains one', value: 'test1' } }
  57. it 'updates reference with new tag name' do
  58. expect { described_class.rename(id: item.id, name: 'test1_renamed') }
  59. .to change { object.reload.send(method)[label][:value] }
  60. .from('test1').to('test1_renamed')
  61. end
  62. end
  63. context "with reference to renamed tag in its #{method} hash (contains-all)" do
  64. let(:object) { create(object_klass.name.underscore, method => { label => tag_matcher }) }
  65. let(:tag_matcher) { { operator: 'contains all', value: 'test1, test2, test3' } }
  66. it 'updates reference with new tag name' do
  67. expect { described_class.rename(id: item.id, name: 'test1_renamed') }
  68. .to change { object.reload.send(method)[label][:value] }
  69. .from('test1, test2, test3').to('test1_renamed, test2, test3')
  70. end
  71. end
  72. end
  73. context 'for Overview object' do
  74. include_examples 'updating references to tag names', object_klass: Overview, method: :condition
  75. end
  76. context 'for Trigger object' do
  77. include_examples 'updating references to tag names', object_klass: Trigger, method: :condition
  78. include_examples 'updating references to tag names', object_klass: Trigger, method: :perform
  79. end
  80. context 'for scheduler (Job) object' do
  81. include_examples 'updating references to tag names', object_klass: Job, method: :condition
  82. include_examples 'updating references to tag names', object_klass: Job, method: :perform
  83. end
  84. context 'for PostmasterFilter object' do
  85. include_examples 'updating references to tag names', object_klass: PostmasterFilter, method: :perform, label: 'x-zammad-ticket-tags'
  86. end
  87. end
  88. describe '.remove' do
  89. let!(:tags) do
  90. [create(:tag, tag_item: item, o: User.first),
  91. create(:tag, tag_item: item, o: Ticket.first)]
  92. end
  93. it 'removes the specified Tag::Item' do
  94. expect { described_class.remove(item.id) }
  95. .to change { described_class.exists?(id: item.id) }.to(false)
  96. end
  97. it 'removes all associated Tags' do
  98. expect { described_class.remove(item.id) }
  99. .to change { Tag.exists?(id: tags.first.id) }.to(false)
  100. .and change { Tag.exists?(id: tags.second.id) }.to(false)
  101. end
  102. end
  103. end