item_spec.rb 4.9 KB

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