Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Fix failing spec.

Dombi Attilaa5ab82b846de
diff --git a/spec/services/work_packages/update_service_integration_spec.rb b/spec/services/work_packages/update_service_integration_spec.rb
index 3f980d30ce3..fba68ec6fa0 100644
--- a/spec/services/work_packages/update_service_integration_spec.rb
+++ b/spec/services/work_packages/update_service_integration_spec.rb
@@ -1539,7 +1539,30 @@ RSpec.describe WorkPackages::UpdateService, "integration", type: :model do
end
it "removes the parent successfully and reschedules the parent" do
- expect(parent).not_to be_valid(:saving_custom_fields)
+ expect(parent.valid?(:saving_custom_fields)).to be(false)
+ # Unload the `ancestor_hierarchies` association so that we don't validate the associatied
+ # records. `closure_tree`'s behaviour is to reload the associations after save, even if they
+ # weren't loaded, see: https://github.com/ClosureTree/closure_tree/blob/
+ # 509f6dfa58da18bb4bff6ded0469263216579a90/lib/closure_tree/hierarchy_maintenance.rb#L44-L45
+ #
+ # However, having the associations loaded will cause an unexpected association validation
+ # in rails, when using custom validation contexts.
+ # The `associated_records_to_validate_or_save` method from AR will return different objects
+ # when a custom context is provided, vs when a default context is present, leading to a
+ # different set of associated objects being validated. As a result, calling `wp.valid?` will
+ # pass, while calling `wp.valid?(:custom_context)` will fail with an error on the
+ # `closure_tree`'s `self_and_ancestors` association.
+ # https://github.com/rails/rails/blob/9f39c019243138d73bb265ec32da9aee26b2c18f/
+ # activerecord/lib/active_record/autosave_association.rb#L298-L306
+ #
+ # The inconsistency is on rails' side, but in this testcase it can be avoided by
+ # unloading the `self_and_ancestors` relation on the work package.
+ # The likelihood of this issue to happen in the application is rather small because,
+ # the `self_and_ancestors` association is loaded in an `after_save` hook. In order to happen,
+ # we must validate or save a record again after it was already saved in the current request.
+ # Once the issue is fixed, it is safe to remove the association reset.
+ work_package.association(:self_and_ancestors).reset
+
expect(subject).to be_success
expect(work_package.reload.parent).to be_nil