Skip to content

Commit 9429596

Browse files
committed
Fix copied form not added to group if it has no questions
When copying a draft form with no questions, it was not being added to a group. This was because we were trying to create the GroupForm before the copied form had been saved. If the form has no associated questions, it would not have been assigned an ID yet, and the GroupForm creation would silently fail. Assign the form to the group after it has been saved to fix this.
1 parent ec0d0df commit 9429596

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

app/services/form_copy_service.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ def copy(tag: "draft")
2323

2424
copy_pages(content["steps"])
2525
copy_routing_conditions(content["steps"])
26-
copy_group
2726

2827
@copied_form.copied_from_id = @form.id
2928
@copied_form.creator_id = @logged_in_user.id
3029
@copied_form.save!
3130

31+
copy_group
32+
3233
# Copy Welsh translations if available
3334
if @form.available_languages.include?("cy")
3435
copy_welsh_translations(tag:)

spec/services/form_copy_service_spec.rb

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
let(:source_form) { create(:form, :live_with_draft) }
66
let(:source_form_document) { FormDocument.find_by(form_id: source_form.id) }
77
let(:logged_in_user) { create(:user) }
8-
let(:copied_form) { described_class.new(source_form, logged_in_user).copy(tag: "live") }
8+
let(:tag) { "live" }
9+
let(:copied_form) { described_class.new(source_form, logged_in_user).copy(tag:) }
910

1011
before do
1112
GroupForm.create!(form: source_form, group: group)
@@ -233,11 +234,24 @@
233234

234235
context "when source form is in a group" do
235236
let(:group) { create(:group) }
236-
let(:source_form) { create(:form, :live_with_draft) }
237237

238-
it "places the copied form in the same group as the original form" do
239-
expect(copied_form.group).to eq(source_form.group)
240-
expect(copied_form.group).to eq(group)
238+
context "when copying a live form" do
239+
let(:source_form) { create(:form, :live_with_draft) }
240+
241+
it "places the copied form in the same group as the original form" do
242+
expect(copied_form.group).to eq(source_form.group)
243+
expect(copied_form.group).to eq(group)
244+
end
245+
end
246+
247+
context "when copying a draft form with no pages" do
248+
let(:source_form) { create(:form) }
249+
let(:tag) { "draft" }
250+
251+
it "places the copied form in the same group as the original form" do
252+
expect(copied_form.group).to eq(source_form.group)
253+
expect(copied_form.group).to eq(group)
254+
end
241255
end
242256
end
243257

0 commit comments

Comments
 (0)