diff --git a/src/openforms/forms/models/form.py b/src/openforms/forms/models/form.py index fdae785b10..7144b84911 100644 --- a/src/openforms/forms/models/form.py +++ b/src/openforms/forms/models/form.py @@ -411,10 +411,16 @@ def copy(self): form_step.save() - for logic in self.formlogic_set.all(): + for logic in self.formlogic_set.all().select_related("trigger_from_step"): logic.pk = None logic.uuid = _uuid.uuid4() logic.form = copy + + if logic.trigger_from_step: + logic.trigger_from_step = logic.form.formstep_set.get( + order=logic.trigger_from_step.order + ) + logic.save() FormVariable.objects.create_for_form(copy) diff --git a/src/openforms/forms/tests/admin/test_form.py b/src/openforms/forms/tests/admin/test_form.py index a62af4d743..4fd8aa0378 100644 --- a/src/openforms/forms/tests/admin/test_form.py +++ b/src/openforms/forms/tests/admin/test_form.py @@ -558,6 +558,25 @@ def test_form_admin_copy(self): copied_form.confirmation_email_template.id, confirmation_email_template.id ) + @tag("gh-3378") + def test_copy_form_with_trigger_from_step_in_logic(self): + user = SuperUserFactory.create() + self.client.force_login(user) + + form_step = FormStepFactory.create() + FormLogicFactory.create(form=form_step.form, trigger_from_step=form_step) + + admin_url = reverse("admin:forms_form_change", args=(form_step.form.pk,)) + + # React UI renders this input, so simulate it in a raw POST call + self.client.post(admin_url, data={"_copy": "Copy"}) + + copied_form = Form.objects.exclude(pk=form_step.form.pk).get() + copied_step = FormStep.objects.exclude(pk=form_step.pk).get() + copied_logic = copied_form.formlogic_set.get() + + self.assertEqual(copied_logic.trigger_from_step, copied_step) + @disable_2fa class FormAdminActionsTests(FormListAjaxMixin, WebTest):