summaryrefslogtreecommitdiff
path: root/spec/services/issuable/clone/attributes_rewriter_spec.rb
blob: fb520f828fa1185043e68e61630accc0b92cff60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# frozen_string_literal: true

require 'spec_helper'

describe Issuable::Clone::AttributesRewriter do
  let(:user) { create(:user) }
  let(:group) { create(:group) }
  let(:project1) { create(:project, :public, group: group) }
  let(:project2) { create(:project, :public, group: group) }
  let(:original_issue) { create(:issue, project: project1) }
  let(:new_issue) { create(:issue, project: project2) }

  subject { described_class.new(user, original_issue, new_issue) }

  context 'setting labels' do
    it 'sets labels present in the new project and group labels' do
      project1_label_1 = create(:label, title: 'label1', project: project1)
      project1_label_2 = create(:label, title: 'label2', project: project1)
      project2_label_1 = create(:label, title: 'label1', project: project2)
      group_label = create(:group_label, title: 'group_label', group: group)
      create(:label, title: 'label3', project: project2)

      original_issue.update(labels: [project1_label_1, project1_label_2, group_label])

      subject.execute

      expect(new_issue.reload.labels).to match_array([project2_label_1, group_label])
    end

    it 'does not set any labels when not used on the original issue' do
      subject.execute

      expect(new_issue.reload.labels).to be_empty
    end

    it 'copies the resource label events' do
      resource_label_events = create_list(:resource_label_event, 2, issue: original_issue)

      subject.execute

      expected = resource_label_events.map(&:label_id)

      expect(new_issue.resource_label_events.map(&:label_id)).to match_array(expected)
    end
  end

  context 'setting milestones' do
    it 'sets milestone to nil when old issue milestone is not in the new project' do
      milestone = create(:milestone, title: 'milestone', project: project1)

      original_issue.update(milestone: milestone)

      subject.execute

      expect(new_issue.reload.milestone).to be_nil
    end

    it 'copies the milestone when old issue milestone title is in the new project' do
      milestone_project1 = create(:milestone, title: 'milestone', project: project1)
      milestone_project2 = create(:milestone, title: 'milestone', project: project2)

      original_issue.update(milestone: milestone_project1)

      subject.execute

      expect(new_issue.reload.milestone).to eq(milestone_project2)
    end

    it 'copies the milestone when old issue milestone is a group milestone' do
      milestone = create(:milestone, title: 'milestone', group: group)

      original_issue.update(milestone: milestone)

      subject.execute

      expect(new_issue.reload.milestone).to eq(milestone)
    end

    context 'with existing milestone events' do
      let!(:milestone1_project1) { create(:milestone, title: 'milestone1', project: project1) }
      let!(:milestone2_project1) { create(:milestone, title: 'milestone2', project: project1) }
      let!(:milestone3_project1) { create(:milestone, title: 'milestone3', project: project1) }

      let!(:milestone1_project2) { create(:milestone, title: 'milestone1', project: project2) }
      let!(:milestone2_project2) { create(:milestone, title: 'milestone2', project: project2) }

      before do
        original_issue.update(milestone: milestone2_project1)

        create_event(milestone1_project1)
        create_event(milestone2_project1)
        create_event(nil, 'remove')
        create_event(milestone3_project1)
      end

      it 'copies existing resource milestone events' do
        subject.execute

        new_issue_milestone_events = new_issue.reload.resource_milestone_events
        expect(new_issue_milestone_events.count).to eq(3)

        expect_milestone_event(new_issue_milestone_events.first, milestone: milestone1_project2, action: 'add', state: 'opened')
        expect_milestone_event(new_issue_milestone_events.second, milestone: milestone2_project2, action: 'add', state: 'opened')
        expect_milestone_event(new_issue_milestone_events.third, milestone: nil, action: 'remove', state: 'opened')
      end

      def create_event(milestone, action = 'add')
        create(:resource_milestone_event, issue: original_issue, milestone: milestone, action: action)
      end

      def expect_milestone_event(event, expected_attrs)
        expect(event.milestone_id).to eq(expected_attrs[:milestone]&.id)
        expect(event.action).to eq(expected_attrs[:action])
        expect(event.state).to eq(expected_attrs[:state])
      end
    end

    context 'with existing state events' do
      let!(:event1) { create(:resource_state_event, issue: original_issue, state: 'opened') }
      let!(:event2) { create(:resource_state_event, issue: original_issue, state: 'closed') }
      let!(:event3) { create(:resource_state_event, issue: original_issue, state: 'reopened') }

      it 'copies existing state events as expected' do
        subject.execute

        state_events = new_issue.reload.resource_state_events
        expect(state_events.size).to eq(3)

        expect_state_event(state_events.first, issue: new_issue, state: 'opened')
        expect_state_event(state_events.second, issue: new_issue, state: 'closed')
        expect_state_event(state_events.third, issue: new_issue, state: 'reopened')
      end

      def expect_state_event(event, expected_attrs)
        expect(event.issue_id).to eq(expected_attrs[:issue]&.id)
        expect(event.state).to eq(expected_attrs[:state])
      end
    end
  end
end