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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[57258] Indicate progress value derivation reason
Christophe Bliard2227ec99c98f
diff --git a/app/components/work_packages/progress/work_based/modal_body_component.html.erb b/app/components/work_packages/progress/work_based/modal_body_component.html.erbindex 3451bc3d3b7..d29236da0e9 100644--- a/app/components/work_packages/progress/work_based/modal_body_component.html.erb+++ b/app/components/work_packages/progress/work_based/modal_body_component.html.erb@@ -38,15 +38,13 @@<% end %><% end %>- <% modal_body.with_row(mt: 3) do |_tooltip| %>- <%= render(Primer::Beta::Text.new(font_weight: :semibold)) { t("work_package.progress.label_note") } %>- <% if OpenProject::FeatureDecisions.percent_complete_edition_active? %>- <%= render(Primer::Beta::Text.new) { t("work_package.progress.modal.work_based_help_text") } %>- <% else %>- <%# This condition branch to be removed in 15.0 with :percent_complete_edition feature flag removal %>+ <%# This condition branch to be removed in 15.0 with :percent_complete_edition feature flag removal %>+ <% unless OpenProject::FeatureDecisions.percent_complete_edition_active? %>+ <% modal_body.with_row(mt: 3) do |_tooltip| %>+ <%= render(Primer::Beta::Text.new(font_weight: :semibold)) { t("work_package.progress.label_note") } %><%= render(Primer::Beta::Text.new) { t("work_package.progress.modal.work_based_help_text_pre_14_4_without_percent_complete_edition") } %>+ <%= render(Primer::Beta::Link.new(href: learn_more_href)) { t(:label_learn_more) } %><% end %>- <%= render(Primer::Beta::Link.new(href: learn_more_href)) { t(:label_learn_more) } %><% end %><% modal_body.with_row(mt: 3) do |_actions_row| %>diff --git a/app/forms/work_packages/progress_form.rb b/app/forms/work_packages/progress_form.rbindex a2e32c8a000..61eecfa284b 100644--- a/app/forms/work_packages/progress_form.rb+++ b/app/forms/work_packages/progress_form.rb@@ -136,6 +136,7 @@ class WorkPackages::ProgressForm < ApplicationFormname:,value: field_value(name),label:,+ caption: field_hint_message(name),validation_message: validation_message(name))@@ -180,6 +181,13 @@ class WorkPackages::ProgressForm < ApplicationFormendend+ def field_hint_message(field_name)+ hint = work_package.derived_progress_hints[field_name]+ return if hint.nil?++ I18n.t("work_package.progress.derivation_hints.#{field_name}.#{hint}")+ end+def validation_message(name)# it's ok to take the first error only, that's how primer_view_component does it anyway.message = @work_package.errors.messages_for(name).firstdiff --git a/app/models/work_package.rb b/app/models/work_package.rbindex 8ab973b435e..de48fc2e390 100644--- a/app/models/work_package.rb+++ b/app/models/work_package.rb@@ -332,6 +332,14 @@ class WorkPackage < ApplicationRecordwrite_attribute :done_ratio, convert_value_to_percentage(value)end+ def derived_progress_hints=(hints)+ @derived_progress_hints = hints+ end++ def derived_progress_hints+ @derived_progress_hints ||= {}+ end+def duration_in_hoursduration ? duration * 24 : nilenddiff --git a/app/services/work_packages/set_attributes_service/derive_progress_values_base.rb b/app/services/work_packages/set_attributes_service/derive_progress_values_base.rbindex 229d7ea0518..87f5d610eda 100644--- a/app/services/work_packages/set_attributes_service/derive_progress_values_base.rb+++ b/app/services/work_packages/set_attributes_service/derive_progress_values_base.rb@@ -141,6 +141,10 @@ class WorkPackages::SetAttributesServiceprivate+ def set_hint(field, hint)+ work_package.derived_progress_hints[field] = hint+ end+def round_progress_values# The values are set only when rounding returns a different value. Doing# otherwise would modify the values returned by `xxx_before_type_cast` and@@ -157,8 +161,6 @@ class WorkPackages::SetAttributesServiceenddef remaining_work_from_percent_complete_and_work- return nil if work_empty? || percent_complete_empty?-completed_work = work * percent_complete / 100.0remaining_work = (work - completed_work).round(2)remaining_work.clamp(0.0, work)diff --git a/app/services/work_packages/set_attributes_service/derive_progress_values_status_based.rb b/app/services/work_packages/set_attributes_service/derive_progress_values_status_based.rbindex 5fdc67429e4..e2a3564739f 100644--- a/app/services/work_packages/set_attributes_service/derive_progress_values_status_based.rb+++ b/app/services/work_packages/set_attributes_service/derive_progress_values_status_based.rb@@ -51,7 +51,15 @@ class WorkPackages::SetAttributesServicereturn if remaining_work_came_from_user?return if work&.negative?- self.remaining_work = remaining_work_from_percent_complete_and_work+ if work_empty?+ return unless work_changed?++ set_hint(:remaining_hours, :cleared_because_work_is_empty)+ self.remaining_work = nil+ else+ set_hint(:remaining_hours, :derived)+ self.remaining_work = remaining_work_from_percent_complete_and_work+ endendendenddiff --git a/app/services/work_packages/set_attributes_service/derive_progress_values_work_based.rb b/app/services/work_packages/set_attributes_service/derive_progress_values_work_based.rbindex 76ef3cf9a18..9921ee1cb24 100644--- a/app/services/work_packages/set_attributes_service/derive_progress_values_work_based.rb+++ b/app/services/work_packages/set_attributes_service/derive_progress_values_work_based.rb@@ -30,6 +30,8 @@ class WorkPackages::SetAttributesServiceclass DeriveProgressValuesWorkBased < DeriveProgressValuesBaseprivate+ attr_accessor :skip_percent_complete_derivation+def derive_progress_attributesraise ArgumentError, "Cannot use #{self.class.name} in status-based mode" if WorkPackage.status_based_mode?@@ -63,36 +65,58 @@ class WorkPackages::SetAttributesServiceenddef derive_percent_complete?- percent_complete_not_provided_by_user? && (work_changed? || remaining_work_changed?)+ percent_complete_not_provided_by_user? && (work_changed? || remaining_work_changed?) \+ && !skip_percent_complete_derivationend+ # rubocop:disable Metrics/AbcSize,Metrics/PerceivedComplexitydef update_workreturn if remaining_work_empty? && percent_complete_empty?return if percent_complete == 100 # would be Infinity if computed when % complete is 100%return unless work_can_be_derived?- self.work =- if remaining_work_empty?- nil- else- work_from_percent_complete_and_remaining_work- end+ if remaining_work_empty?+ return unless remaining_work_changed?++ set_hint(:estimated_hours, :cleared_because_remaining_work_is_empty)+ self.work = nil+ elsif percent_complete_empty?+ set_hint(:estimated_hours, :same_as_remaining_work)+ self.work = remaining_work+ else+ set_hint(:estimated_hours, :derived)+ self.work = work_from_percent_complete_and_remaining_work+ skip_percent_complete_derivation!+ endend- # rubocop:disable Metrics/AbcSize,Metrics/PerceivedComplexitydef update_remaining_workreturn if work_empty? && percent_complete_empty?- return if work_was_empty? && remaining_work_set? # remaining work is kept and % complete will be set+ return if work_was_empty? && remaining_work_set? # remaining work is kept and % complete will be unsetif work_set? && remaining_work_empty? && percent_complete_empty?+ set_hint(:remaining_hours, :same_as_work)self.remaining_work = workelsif work_changed? && work_set? && remaining_work_set? && percent_complete_not_provided_by_user?delta = work - work_was+ if delta.positive?+ set_hint(:remaining_hours, :increased_like_work)+ elsif delta.negative?+ set_hint(:remaining_hours, :decreased_like_work)+ endself.remaining_work = (remaining_work + delta).clamp(0.0, work)- elsif work_empty? || percent_complete_empty?+ elsif work_empty?+ return unless work_changed?++ set_hint(:remaining_hours, :cleared_because_work_is_empty)+ self.remaining_work = nil+ elsif percent_complete_empty?+ set_hint(:remaining_hours, :cleared_because_percent_complete_is_empty)self.remaining_work = nilelse+ set_hint(:remaining_hours, :derived)self.remaining_work = remaining_work_from_percent_complete_and_work+ skip_percent_complete_derivation!endend# rubocop:enable Metrics/AbcSize,Metrics/PerceivedComplexity@@ -100,12 +124,23 @@ class WorkPackages::SetAttributesServicedef update_percent_completereturn if work_empty?- self.percent_complete = percent_complete_from_work_and_remaining_work+ if work.zero?+ set_hint(:done_ratio, :cleared_because_work_is_0h)+ self.percent_complete = nil+ elsif remaining_work_empty?+ set_hint(:done_ratio, :cleared_because_remaining_work_is_empty)+ self.percent_complete = nil+ else+ set_hint(:done_ratio, :derived)+ self.percent_complete = percent_complete_from_work_and_remaining_work+ endend- def percent_complete_from_work_and_remaining_work- return nil if work.zero? || remaining_work_empty?+ def skip_percent_complete_derivation!+ self.skip_percent_complete_derivation = true+ end+ def percent_complete_from_work_and_remaining_workcompleted_work = work - remaining_workcompletion_ratio = completed_work.to_f / work@@ -113,7 +148,7 @@ class WorkPackages::SetAttributesServiceenddef work_from_percent_complete_and_remaining_work- remaining_percent_complete = 1.0 - ((percent_complete || 0) / 100.0)+ remaining_percent_complete = 1.0 - (percent_complete / 100.0)remaining_work / remaining_percent_completeenddiff --git a/config/locales/en.yml b/config/locales/en.ymlindex 46384005d4a..31a0dfe4b7e 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -3696,6 +3696,22 @@ en:work_based_help_text_pre_14_4_without_percent_complete_edition: "% Complete is automatically derived from Work and Remaining work."status_based_help_text: "% Complete is set by work package status."migration_warning_text: "In work-based progress calculation mode, % Complete cannot be manually set and is tied to Work. The existing value has been kept but cannot be edited. Please input Work first."+ derivation_hints:+ done_ratio:+ cleared_because_remaining_work_is_empty: "Cleared because Remaining work is empty."+ cleared_because_work_is_0h: "Cleared because Work is 0h."+ derived: "Derived from Work and Remaining work."+ estimated_hours:+ cleared_because_remaining_work_is_empty: "Cleared because Remaining work is empty."+ derived: "Derived from Remaining work and % Complete."+ same_as_remaining_work: "Set to same value as Remaining work."+ remaining_hours:+ cleared_because_work_is_empty: "Cleared because Work is empty."+ cleared_because_percent_complete_is_empty: "Cleared because % Complete is empty."+ decreased_like_work: "Decreased by the same amount as Work."+ derived: "Derived from Work and % Complete."+ increased_like_work: "Increased by the same amount as Work."+ same_as_work: "Set to same value as Work."permissions:comment: "Comment"comment_description: "Can view and comment this work package."diff --git a/spec/components/work_packages/progress/work_based/modal_body_component_spec.rb b/spec/components/work_packages/progress/work_based/modal_body_component_spec.rbindex 0fc2617472f..5ac7c72b773 100644--- a/spec/components/work_packages/progress/work_based/modal_body_component_spec.rb+++ b/spec/components/work_packages/progress/work_based/modal_body_component_spec.rb@@ -36,7 +36,6 @@ RSpec.describe WorkPackages::Progress::WorkBased::ModalBodyComponent,include_examples "progress modal validations"include_examples "progress modal submit path"- include_examples "progress modal help links"describe "#mode" dosubject(:component) { described_class.new(WorkPackage.new) }diff --git a/spec/services/work_packages/set_attributes_service/derive_progress_values_status_based_spec.rb b/spec/services/work_packages/set_attributes_service/derive_progress_values_status_based_spec.rbindex 50ba599878b..c59d0757c41 100644--- a/spec/services/work_packages/set_attributes_service/derive_progress_values_status_based_spec.rb+++ b/spec/services/work_packages/set_attributes_service/derive_progress_values_status_based_spec.rb@@ -27,6 +27,7 @@#++require "spec_helper"+require_relative "shared_examples"# Scenarios specified in https://community.openproject.org/wp/40749RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesStatusBased,@@ -41,39 +42,6 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesStatusBaslet(:work_package) { build_stubbed(:work_package, project:, status: status_0_pct_complete) }let(:instance) { described_class.new(work_package) }- shared_examples_for "update progress values" do |description:|- subject do- allow(work_package)- .to receive(:save)-- instance.call- end-- it description do- work_package.attributes = set_attributes- all_expected_attributes = {}- all_expected_attributes.merge!(expected_derived_attributes) if defined?(expected_derived_attributes)- if defined?(expected_kept_attributes)- kept = work_package.attributes.slice(*expected_kept_attributes)- if kept.size != expected_kept_attributes.size- raise ArgumentError, "expected_kept_attributes contains attributes that are not present in the work_package: " \- "#{expected_kept_attributes - kept.keys} not present in #{work_package.attributes}"- end- all_expected_attributes.merge!(kept)- end- next if all_expected_attributes.blank?-- subject-- aggregate_failures do- expect(work_package).to have_attributes(all_expected_attributes)- # work package is not saved and no errors are created- expect(work_package).not_to have_received(:save)- expect(work_package.errors).to be_empty- end- end- end-context "given a work package with work, remaining work, and status with % complete being set" dobefore dowork_package.status = status_50_pct_complete@@ -87,14 +55,20 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesStatusBaslet(:set_attributes) { { estimated_hours: nil } }let(:expected_derived_attributes) { { remaining_hours: nil } }- include_examples "update progress values", description: "clearss remaining work"+ include_examples "update progress values", description: "clears remaining work",+ expected_hints: {+ remaining_work: :cleared_because_work_is_empty+ }endcontext "when work is changed" dolet(:set_attributes) { { estimated_hours: 5.0 } }let(:expected_derived_attributes) { { remaining_hours: 2.5 } }- include_examples "update progress values", description: "recomputes remaining work accordingly"+ include_examples "update progress values", description: "derives remaining work",+ expected_hints: {+ remaining_work: :derived+ }endcontext "when work is changed to a negative value" do@@ -102,7 +76,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesStatusBaslet(:expected_kept_attributes) { %w[remaining_hours] }include_examples "update progress values",- description: "is an error state (to be detected by contract), and remaining work is kept"+ description: "is an error state (to be detected by contract), and remaining work is kept",+ expected_hints: {}endcontext "when another status is set" do@@ -110,14 +85,20 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesStatusBaslet(:expected_derived_attributes) { { remaining_hours: 3.0 } }include_examples "update progress values",- description: "recomputes remaining work according to the % complete value of the new status"+ description: "derives remaining work according to the % complete value of the new status",+ expected_hints: {+ remaining_work: :derived+ }endcontext "when floating point operations are inaccurate (2.4000000000000004h)" dolet(:set_attributes) { { estimated_hours: 8.0, status: status_70_pct_complete } }let(:expected_derived_attributes) { { remaining_hours: 2.4 } } # would be 2.4000000000000004 without rounding- include_examples "update progress values", description: "remaining work is computed and rounded (2.4)"+ include_examples "update progress values", description: "remaining work is derived and rounded (2.4)",+ expected_hints: {+ remaining_work: :derived+ }endend@@ -135,7 +116,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesStatusBaslet(:expected_derived_attributes) { { remaining_hours: nil } }include_examples "update progress values",- description: "remaining work remains empty"+ description: "remaining work remains empty",+ expected_hints: {}endcontext "when work is set" do@@ -143,7 +125,10 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesStatusBaslet(:expected_derived_attributes) { { remaining_hours: 10.0 } }include_examples "update progress values",- description: "remaining work is derived from work and % complete value of the status"+ description: "remaining work is derived from work and % complete value of the status",+ expected_hints: {+ remaining_work: :derived+ }endcontext "when work is set to a negative value" do@@ -151,7 +136,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesStatusBaslet(:expected_kept_attributes) { %w[remaining_hours] }include_examples "update progress values",- description: "is an error state (to be detected by contract), and remaining work is kept"+ description: "is an error state (to be detected by contract), and remaining work is kept",+ expected_hints: {}endcontext "when work is set with 2nd decimal rounding up" do@@ -159,7 +145,10 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesStatusBaslet(:expected_derived_attributes) { { estimated_hours: 3.57, remaining_hours: 3.57 } }include_examples "update progress values",- description: "values are rounded up to 2 decimals and set to the same value"+ description: "values are rounded up to 2 decimals and set to the same value",+ expected_hints: {+ remaining_work: :derived+ }endend@@ -175,7 +164,18 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesStatusBaslet(:expected_derived_attributes) { { done_ratio: 70 } }include_examples "update progress values",- description: "sets the % complete value to the status default % complete value"+ description: "sets the % complete value to the status default % complete value",+ expected_hints: {}+ end++ # can happen if work is set and then is unset+ context "when another status with another % complete value is set and work is cleared" do+ let(:set_attributes) { { status: status_70_pct_complete, estimated_hours: nil } }+ let(:expected_derived_attributes) { { done_ratio: 70 } }++ include_examples "update progress values",+ description: "sets the % complete value to the status default % complete value",+ expected_hints: {}endendenddiff --git a/spec/services/work_packages/set_attributes_service/derive_progress_values_work_based_spec.rb b/spec/services/work_packages/set_attributes_service/derive_progress_values_work_based_spec.rbindex 0b16320a274..f76e476fab7 100644--- a/spec/services/work_packages/set_attributes_service/derive_progress_values_work_based_spec.rb+++ b/spec/services/work_packages/set_attributes_service/derive_progress_values_work_based_spec.rb@@ -27,6 +27,7 @@#++require "spec_helper"+require_relative "shared_examples"# Scenarios specified in https://community.openproject.org/wp/40749RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBased,@@ -37,40 +38,6 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:work_package) { build_stubbed(:work_package, project:) }let(:instance) { described_class.new(work_package) }- shared_examples_for "update progress values" do |description:|- subject do- allow(work_package)- .to receive(:save)-- instance.call- end-- it description do- work_package.attributes = set_attributes- all_expected_attributes = {}- all_expected_attributes.merge!(expected_derived_attributes) if defined?(expected_derived_attributes)- if defined?(expected_kept_attributes)- kept = work_package.attributes.slice(*expected_kept_attributes)- if kept.size != expected_kept_attributes.size- raise ArgumentError, "expected_kept_attributes contains attributes that are not present in the work_package: " \- "#{expected_kept_attributes - kept.keys} not present in #{work_package.attributes}"- end- all_expected_attributes.merge!(kept)- end- next if all_expected_attributes.blank?-- subject-- aggregate_failures do- expect(work_package).to have_attributes(all_expected_attributes)- expect(work_package).to have_attributes(set_attributes.except(*all_expected_attributes.keys))- # work package is not saved and no errors are created- expect(work_package).not_to have_received(:save)- expect(work_package.errors).to be_empty- end- end- end-context "given a work package with work, remaining work, and % complete being set" dobefore dowork_package.estimated_hours = 10.0@@ -84,7 +51,10 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_derived_attributes) { { remaining_hours: nil } }let(:expected_kept_attributes) { %w[done_ratio] }- include_examples "update progress values", description: "keeps % complete, and clears remaining work"+ include_examples "update progress values", description: "keeps % complete, and clears remaining work",+ expected_hints: {+ remaining_work: :cleared_because_work_is_empty+ }endcontext "when remaining work is cleared" do@@ -92,7 +62,10 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_derived_attributes) { { estimated_hours: nil } }let(:expected_kept_attributes) { %w[done_ratio] }- include_examples "update progress values", description: "keeps % complete, and clears work"+ include_examples "update progress values", description: "keeps % complete, and clears work",+ expected_hints: {+ work: :cleared_because_remaining_work_is_empty+ }endcontext "when % complete is cleared" do@@ -100,35 +73,44 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_derived_attributes) { { remaining_hours: nil } }let(:expected_kept_attributes) { %w[estimated_hours] }- include_examples "update progress values", description: "keeps work, and clears remaining work"+ include_examples "update progress values", description: "keeps work, and clears remaining work",+ expected_hints: {+ remaining_work: :cleared_because_percent_complete_is_empty+ }endcontext "when both work and remaining work are cleared" dolet(:set_attributes) { { estimated_hours: nil, remaining_hours: nil } }let(:expected_kept_attributes) { %w[done_ratio] }- include_examples "update progress values", description: "keeps % complete"+ include_examples "update progress values", description: "keeps % complete",+ expected_hints: {}endcontext "when work is changed and percent complete is set to the same value as before" dolet(:set_attributes) { { estimated_hours: 20.0, done_ratio: 70 } }let(:expected_derived_attributes) { { remaining_hours: 6.0 } }- include_examples "update progress values", description: "derives remaining work from work and % complete"+ include_examples "update progress values", description: "derives remaining work from work and % complete",+ expected_hints: {+ remaining_work: :derived+ }endcontext "when both work and percent complete are cleared" dolet(:set_attributes) { { estimated_hours: nil, done_ratio: nil } }let(:expected_kept_attributes) { %w[remaining_hours] }- include_examples "update progress values", description: "keeps remaining work"+ include_examples "update progress values", description: "keeps remaining work",+ expected_hints: {}endcontext "when both remaining work and percent complete are cleared" dolet(:set_attributes) { { remaining_hours: nil, done_ratio: nil } }let(:expected_kept_attributes) { %w[estimated_hours] }- include_examples "update progress values", description: "keeps work"+ include_examples "update progress values", description: "keeps work",+ expected_hints: {}endcontext "when work is increased" do@@ -139,7 +121,11 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedendinclude_examples "update progress values",- description: "remaining work is increased by the same amount, and % complete is derived"+ description: "remaining work is increased by the same amount, and % complete is derived",+ expected_hints: {+ remaining_work: :increased_like_work,+ percent_complete: :derived+ }endcontext "when work is set to 0h" do@@ -149,7 +135,11 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedendinclude_examples "update progress values",- description: "remaining work is set to 0h and % Complete is cleared"+ description: "remaining work is set to 0h and % Complete is cleared",+ expected_hints: {+ remaining_work: :decreased_like_work,+ percent_complete: :cleared_because_work_is_0h+ }endcontext "when work is decreased" do@@ -160,7 +150,11 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedendinclude_examples "update progress values",- description: "remaining work is decreased by the same amount, and % complete is derived"+ description: "remaining work is decreased by the same amount, and % complete is derived",+ expected_hints: {+ remaining_work: :decreased_like_work,+ percent_complete: :derived+ }endcontext "when work is decreased below remaining work value" do@@ -171,7 +165,11 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedendinclude_examples "update progress values",- description: "remaining work becomes 0h, and % complete becomes 100%"+ description: "remaining work becomes 0h, and % complete becomes 100%",+ expected_hints: {+ remaining_work: :decreased_like_work,+ percent_complete: :derived+ }endcontext "when work is changed to a negative value" do@@ -180,7 +178,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedinclude_examples "update progress values",description: "is an error state (to be detected by contract), " \- "and % complete and remaining work are kept"+ "and % complete and remaining work are kept",+ expected_hints: {}endcontext "when remaining work is changed" do@@ -188,14 +187,20 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_derived_attributes) { { done_ratio: 80 } }let(:expected_kept_attributes) { %w[estimated_hours] }- include_examples "update progress values", description: "updates % complete accordingly"+ include_examples "update progress values", description: "updates % complete accordingly",+ expected_hints: {+ percent_complete: :derived+ }endcontext "when remaining work and % complete are both changed" dolet(:set_attributes) { { remaining_hours: 12.0, done_ratio: 40 } }let(:expected_derived_attributes) { { estimated_hours: 20.0 } }- include_examples "update progress values", description: "work is derived"+ include_examples "update progress values", description: "work is derived",+ expected_hints: {+ work: :derived+ }endcontext "when remaining work is changed and % complete is changed to 100%" do@@ -203,7 +208,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_kept_attributes) { %w[estimated_hours] }include_examples "update progress values",- description: "is an error state (to be detected by contract), and work is kept"+ description: "is an error state (to be detected by contract), and work is kept",+ expected_hints: {}endcontext "when work and remaining work are both changed to negative values" do@@ -211,7 +217,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_kept_attributes) { %w[done_ratio] }include_examples "update progress values",- description: "is an error state (to be detected by contract), and % Complete is kept"+ description: "is an error state (to be detected by contract), and % Complete is kept",+ expected_hints: {}endcontext "when work and remaining work are both changed so that work is lower than remaining work" do@@ -219,7 +226,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_kept_attributes) { %w[done_ratio] }include_examples "update progress values",- description: "is an error state (to be detected by contract), and % Complete is kept"+ description: "is an error state (to be detected by contract), and % Complete is kept",+ expected_hints: {}endcontext "when work and remaining work are both changed to values with more than 2 decimals" do@@ -227,7 +235,10 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_derived_attributes) { { estimated_hours: 10.12, remaining_hours: 5.68, done_ratio: 44 } }include_examples "update progress values", description: "rounds work and remaining work to 2 decimals " \- "and updates % complete accordingly"+ "and updates % complete accordingly",+ expected_hints: {+ percent_complete: :derived+ }endcontext "when remaining work is changed to a value greater than work" do@@ -235,7 +246,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_kept_attributes) { %w[done_ratio] }include_examples "update progress values",- description: "is an error state (to be detected by contract), and % Complete is kept"+ description: "is an error state (to be detected by contract), and % Complete is kept",+ expected_hints: {}endcontext "when remaining work is changed to a negative value" do@@ -243,42 +255,58 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_kept_attributes) { %w[done_ratio] }include_examples "update progress values",- description: "is an error state (to be detected by contract), and % Complete is kept"+ description: "is an error state (to be detected by contract), and % Complete is kept",+ expected_hints: {}endcontext "when both work and remaining work are changed" dolet(:set_attributes) { { estimated_hours: 20, remaining_hours: 2 } }let(:expected_derived_attributes) { { done_ratio: 90 } }- include_examples "update progress values", description: "updates % complete accordingly"+ include_examples "update progress values", description: "updates % complete accordingly",+ expected_hints: {+ percent_complete: :derived+ }endcontext "when work is changed and remaining work is cleared" dolet(:set_attributes) { { estimated_hours: 8.0, remaining_hours: nil } }let(:expected_derived_attributes) { { done_ratio: nil } }- include_examples "update progress values", description: "% complete is cleared"+ include_examples "update progress values", description: "% complete is cleared",+ expected_hints: {+ percent_complete: :cleared_because_remaining_work_is_empty+ }endcontext "when percent complete is changed and work is cleared" dolet(:set_attributes) { { done_ratio: 40, estimated_hours: nil } }let(:expected_derived_attributes) { { remaining_hours: nil } }- include_examples "update progress values", description: "remaining work is cleared"+ include_examples "update progress values", description: "remaining work is cleared",+ expected_hints: {+ remaining_work: :cleared_because_work_is_empty+ }endcontext "when percent complete is changed and remaining work is cleared" dolet(:set_attributes) { { done_ratio: 40, remaining_hours: nil } }let(:expected_derived_attributes) { { estimated_hours: nil } }- include_examples "update progress values", description: "work is cleared"+ include_examples "update progress values", description: "work is cleared",+ expected_hints: {+ work: :cleared_because_remaining_work_is_empty+ }endcontext "when % complete is changed and remaining work is set to same value" dolet(:set_attributes) { { done_ratio: 90, remaining_hours: 3 } }let(:expected_derived_attributes) { { estimated_hours: 30 } }- include_examples "update progress values", description: "work is derived"+ include_examples "update progress values", description: "work is derived",+ expected_hints: {+ work: :derived+ }endcontext "when work is set to the same value and remaining work is changed" do@@ -286,16 +314,23 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_derived_attributes) { { done_ratio: 90 } }include_examples "update progress values",- description: "% complete is derived"+ description: "% complete is derived",+ expected_hints: {+ percent_complete: :derived+ }endcontext "when work is increased and remaining work is set to its current value (to prevent it from being increased)" do# work changed by +10hlet(:set_attributes) { { estimated_hours: 10.0 + 10.0, remaining_hours: 3 } }- let(:expected_derived_attributes) { { remaining_hours: 3.0, done_ratio: 85 } }+ let(:expected_derived_attributes) { { done_ratio: 85 } }+ let(:expected_kept_attributes) { %w[remaining_hours] }include_examples "update progress values",- description: "remaining work is kept (not increased), and % complete is derived"+ description: "remaining work is kept (not increased), and % complete is derived",+ expected_hints: {+ percent_complete: :derived+ }endcontext "when % complete is changed" do@@ -303,7 +338,10 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_derived_attributes) { { remaining_hours: 6.0 } }let(:expected_kept_attributes) { %w[estimated_hours] }- include_examples "update progress values", description: "work is kept, and remaining work is derived"+ include_examples "update progress values", description: "work is kept, and remaining work is derived",+ expected_hints: {+ remaining_work: :derived+ }endcontext "when % complete is changed to a negative value" do@@ -311,7 +349,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_kept_attributes) { %w[estimated_hours remaining_hours] }include_examples "update progress values",- description: "is an error state (to be detected by contract), and work and remaining work are kept"+ description: "is an error state (to be detected by contract), and work and remaining work are kept",+ expected_hints: {}endcontext "when % complete is more than 100%" do@@ -319,7 +358,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_kept_attributes) { %w[estimated_hours remaining_hours] }include_examples "update progress values",- description: "is an error state (to be detected by contract), and work and remaining work are kept"+ description: "is an error state (to be detected by contract), and work and remaining work are kept",+ expected_hints: {}endcontext "when % complete is a string not representing a valid percentage" do@@ -336,14 +376,16 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedendinclude_examples "update progress values",- description: "is an error state (to be detected by contract), and work and remaining work are kept"+ description: "is an error state (to be detected by contract), and work and remaining work are kept",+ expected_hints: {}endcontext "when work, remaining work, and % complete are all changed to consistent values" dolet(:set_attributes) { { estimated_hours: 20, remaining_hours: 12.0, done_ratio: 40 } }let(:expected_kept_attributes) { %w[estimated_hours remaining_hours done_ratio] }- include_examples "update progress values", description: "they are all kept"+ include_examples "update progress values", description: "they are all kept",+ expected_hints: {}endcontext "when work, remaining work, and % complete are all changed to inconsistent values" do@@ -351,7 +393,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_kept_attributes) { %w[estimated_hours remaining_hours done_ratio] }include_examples "update progress values",- description: "is an error state (to be detected by contract), and all values are kept"+ description: "is an error state (to be detected by contract), and all values are kept",+ expected_hints: {}endend@@ -368,7 +411,10 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_derived_attributes) { { remaining_hours: 14.0 } }let(:expected_kept_attributes) { %w[done_ratio] }- include_examples "update progress values", description: "% complete is kept and remaining work is derived"+ include_examples "update progress values", description: "% complete is kept and remaining work is derived",+ expected_hints: {+ remaining_work: :derived+ }endcontext "when work is changed to a negative value" do@@ -377,7 +423,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedinclude_examples "update progress values",description: "is an error state (to be detected by contract), " \- "and % complete and remaining work are kept"+ "and % complete and remaining work are kept",+ expected_hints: {}endcontext "when remaining work is set" do@@ -385,7 +432,10 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_derived_attributes) { { done_ratio: 90.0 } }let(:expected_kept_attributes) { %w[estimated_hours] }- include_examples "update progress values", description: "work is kept and % complete is derived"+ include_examples "update progress values", description: "work is kept and % complete is derived",+ expected_hints: {+ percent_complete: :derived+ }endcontext "when % complete is set" do@@ -393,7 +443,10 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_derived_attributes) { { remaining_hours: 1.0 } }let(:expected_kept_attributes) { %w[estimated_hours] }- include_examples "update progress values", description: "work is kept and remaining work is derived"+ include_examples "update progress values", description: "work is kept and remaining work is derived",+ expected_hints: {+ remaining_work: :derived+ }endend@@ -410,7 +463,10 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_derived_attributes) { { estimated_hours: 20.0 } }let(:expected_kept_attributes) { %w[done_ratio] }- include_examples "update progress values", description: "% complete is kept and work is derived"+ include_examples "update progress values", description: "% complete is kept and work is derived",+ expected_hints: {+ work: :derived+ }endcontext "when % complete is 0% and remaining work is changed to a decimal rounded up" do@@ -424,7 +480,10 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedendinclude_examples "update progress values",- description: "% complete is kept, values are rounded, and work is derived"+ description: "% complete is kept, values are rounded, and work is derived",+ expected_hints: {+ work: :derived+ }endcontext "when work is set" do@@ -432,7 +491,10 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_derived_attributes) { { done_ratio: 80.0 } }let(:expected_kept_attributes) { %w[remaining_hours] }- include_examples "update progress values", description: "remaining work is kept and % complete is derived"+ include_examples "update progress values", description: "remaining work is kept and % complete is derived",+ expected_hints: {+ percent_complete: :derived+ }endcontext "when % complete is changed" do@@ -440,7 +502,10 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_derived_attributes) { { estimated_hours: 10.0 } }let(:expected_kept_attributes) { %w[remaining_hours] }- include_examples "update progress values", description: "remaining work is kept and work is derived"+ include_examples "update progress values", description: "remaining work is kept and work is derived",+ expected_hints: {+ work: :derived+ }endcontext "when % complete is changed to 100%" do@@ -449,7 +514,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedinclude_examples "update progress values",description: "work and remaining work are kept because work cannot be derived " \- "(error state to be detected by contract)"+ "(error state to be detected by contract)",+ expected_hints: {}endend@@ -466,7 +532,11 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_derived_attributes) { { remaining_hours: 20.0, done_ratio: 0 } }include_examples "update progress values",- description: "remaining work is set to the same value and % complete is set to 0%"+ description: "remaining work is set to the same value and % complete is set to 0%",+ expected_hints: {+ remaining_work: :same_as_work,+ percent_complete: :derived+ }endcontext "when work is changed and remaining work is cleared" do@@ -475,7 +545,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedinclude_examples "update progress values",description: "% complete is kept and remaining work is kept empty and not recomputed " \- "(error state to be detected by contract)"+ "(error state to be detected by contract)",+ expected_hints: {}endcontext "when work is changed to a negative value" do@@ -484,7 +555,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedinclude_examples "update progress values",description: "is an error state (to be detected by contract), " \- "and % complete and remaining work are kept"+ "and % complete and remaining work are kept",+ expected_hints: {}endcontext "when % complete is set" do@@ -493,7 +565,10 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_kept_attributes) { %w[estimated_hours] }include_examples "update progress values",- description: "work is kept and remaining work is derived"+ description: "work is kept and remaining work is derived",+ expected_hints: {+ remaining_work: :derived+ }endend@@ -511,7 +586,10 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_kept_attributes) { %w[remaining_hours] }include_examples "update progress values",- description: "remaining work is kept to the same value and % complete is derived"+ description: "remaining work is kept to the same value and % complete is derived",+ expected_hints: {+ percent_complete: :derived+ }endcontext "when work is set lower than remaining work" do@@ -520,7 +598,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedinclude_examples "update progress values",description: "is an error state (to be detected by contract), " \- "and % complete and remaining work are kept"+ "and % complete and remaining work are kept",+ expected_hints: {}endcontext "when work is changed to a negative value" do@@ -529,7 +608,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedinclude_examples "update progress values",description: "is an error state (to be detected by contract), " \- "and % complete and remaining work are kept"+ "and % complete and remaining work are kept",+ expected_hints: {}endcontext "when remaining work is changed" do@@ -537,7 +617,11 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_derived_attributes) { { estimated_hours: 12.0, done_ratio: 0 } }include_examples "update progress values",- description: "work is set to the same value and % complete is set to 0%"+ description: "work is set to the same value and % complete is set to 0%",+ expected_hints: {+ work: :same_as_remaining_work,+ percent_complete: :derived+ }endcontext "when remaining work is changed to a negative value" do@@ -546,7 +630,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedinclude_examples "update progress values",description: "is an error state (to be detected by contract), " \- "and % complete and work are kept"+ "and % complete and work are kept",+ expected_hints: {}endcontext "when % complete is set" do@@ -554,7 +639,10 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_derived_attributes) { { estimated_hours: 10.0 } }let(:expected_kept_attributes) { %w[remaining_hours] }- include_examples "update progress values", description: "work is derived"+ include_examples "update progress values", description: "work is derived",+ expected_hints: {+ work: :derived+ }endend@@ -571,7 +659,11 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_derived_attributes) { { remaining_hours: 5.0, done_ratio: 0 } }include_examples "update progress values",- description: "remaining work is set to same value as work, and % complete is set to 0%"+ description: "remaining work is increased like work, and % complete is set to 0%",+ expected_hints: {+ remaining_hours: :increased_like_work,+ percent_complete: :derived+ }endend@@ -588,7 +680,10 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_derived_attributes) { { remaining_hours: 4.0 } }let(:expected_kept_attributes) { %w[done_ratio] }- include_examples "update progress values", description: "% complete is kept and remaining work is derived"+ include_examples "update progress values", description: "% complete is kept and remaining work is derived",+ expected_hints: {+ remaining_work: :derived+ }endcontext "when work is set to a number with with 4 decimals" do@@ -598,7 +693,10 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedinclude_examples "update progress values",description: "% complete is kept, work is rounded to 2 decimals, " \- "and remaining work is derived and rounded to 2 decimals"+ "and remaining work is derived and rounded to 2 decimals",+ expected_hints: {+ remaining_work: :derived+ }endcontext "when work is set to a string" do@@ -618,14 +716,20 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:set_attributes) { { estimated_hours: 10.0, remaining_hours: 0 } }let(:expected_derived_attributes) { { done_ratio: 100 } }- include_examples "update progress values", description: "% complete is derived"+ include_examples "update progress values", description: "% complete is derived",+ expected_hints: {+ percent_complete: :derived+ }end- context "when work is set and remaining work is clered" do+ context "when work is set and remaining work is cleared" dolet(:set_attributes) { { estimated_hours: 10.0, remaining_hours: nil } }let(:expected_derived_attributes) { { done_ratio: nil } }- include_examples "update progress values", description: "% complete is cleared"+ include_examples "update progress values", description: "% complete is cleared",+ expected_hints: {+ percent_complete: :cleared_because_remaining_work_is_empty+ }endcontext "when work and remaining work are both set to negative values" do@@ -633,7 +737,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_kept_attributes) { %w[done_ratio] }include_examples "update progress values",- description: "is an error state (to be detected by contract), and % complete is kept"+ description: "is an error state (to be detected by contract), and % complete is kept",+ expected_hints: {}endcontext "when % complete is changed" do@@ -641,7 +746,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_kept_attributes) { %w[estimated_hours remaining_hours] }include_examples "update progress values",- description: "work and remaining work are kept empty"+ description: "work and remaining work are kept empty",+ expected_hints: {}endend@@ -658,7 +764,10 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_derived_attributes) { { remaining_hours: 0.0 } }let(:expected_kept_attributes) { %w[done_ratio] }- include_examples "update progress values", description: "% complete is kept and remaining work is set to 0h"+ include_examples "update progress values", description: "% complete is kept and remaining work is set to 0h",+ expected_hints: {+ remaining_work: :derived+ }endcontext "when work is set to a string" do@@ -678,7 +787,10 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:set_attributes) { { estimated_hours: 10.0, remaining_hours: 5.0 } }let(:expected_derived_attributes) { { done_ratio: 50 } }- include_examples "update progress values", description: "% complete is derived"+ include_examples "update progress values", description: "% complete is derived",+ expected_hints: {+ percent_complete: :derived+ }endcontext "when remaining work is set to 0h" do@@ -686,7 +798,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_kept_attributes) { %w[estimated_hours done_ratio] }include_examples "update progress values",- description: "is an error state (to be detected by contract), and % complete is kept"+ description: "is an error state (to be detected by contract), and % complete is kept",+ expected_hints: {}endend@@ -705,7 +818,11 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedendinclude_examples "update progress values",- description: "remaining work is set to the same value and % complete is set to 0%"+ description: "remaining work is set to the same value and % complete is set to 0%",+ expected_hints: {+ remaining_work: :same_as_work,+ percent_complete: :derived+ }endcontext "when work is set to a negative value" do@@ -714,14 +831,19 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedinclude_examples "update progress values",description: "is an error state (to be detected by contract), " \- "and % complete and remaining work are kept"+ "and % complete and remaining work are kept",+ expected_hints: {}endcontext "when remaining work is set" dolet(:set_attributes) { { remaining_hours: 10.0 } }let(:expected_derived_attributes) { { estimated_hours: 10.0, done_ratio: 0 } }- include_examples "update progress values", description: "work is set to the same value and % complete is set to 0%"+ include_examples "update progress values", description: "work is set to the same value and % complete is set to 0%",+ expected_hints: {+ work: :same_as_remaining_work,+ percent_complete: :derived+ }endcontext "when remaining work is set to a negative value" do@@ -730,7 +852,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedinclude_examples "update progress values",description: "is an error state (to be detected by contract), " \- "and % complete and work are kept"+ "and % complete and work are kept",+ expected_hints: {}endcontext "when remaining work is set and work is cleared" do@@ -739,7 +862,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedinclude_examples "update progress values",description: "% complete is kept and work is kept empty and not recomputed" \- "(error state to be detected by contract)"+ "(error state to be detected by contract)",+ expected_hints: {}endcontext "when % complete is set" do@@ -747,7 +871,26 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedlet(:expected_kept_attributes) { %w[estimated_hours remaining_hours] }include_examples "update progress values",- description: "work and remaining work are kept empty"+ description: "work and remaining work are kept empty",+ expected_hints: {}+ end++ context "when % complete is set and remaining work is cleared" do+ let(:set_attributes) { { done_ratio: 80, remaining_hours: nil } }+ let(:expected_kept_attributes) { %w[estimated_hours] }++ include_examples "update progress values",+ description: "work and remaining work are kept empty",+ expected_hints: {}+ end++ context "when % complete is set and work is cleared" do+ let(:set_attributes) { { done_ratio: 80, estimated_hours: nil } }+ let(:expected_kept_attributes) { %w[remaining_hours] }++ include_examples "update progress values",+ description: "work and remaining work are kept empty",+ expected_hints: {}endendenddiff --git a/spec/services/work_packages/set_attributes_service/shared_examples.rb b/spec/services/work_packages/set_attributes_service/shared_examples.rbnew file mode 100644index 00000000000..7598a3605aa--- /dev/null+++ b/spec/services/work_packages/set_attributes_service/shared_examples.rb@@ -0,0 +1,91 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++def to_work_package_field(name)+ {+ work: :estimated_hours,+ remaining_work: :remaining_hours,+ percent_complete: :done_ratio+ }.fetch(name, name)+end++# Sets values on a work package, calls a `DeriveProgressValuesBase` instance and+# check if the work_package as been updated as expected.+#+# * Use `let(:set_attributes)` to define the values to set on the work package:+#+# let(:set_attributes) { { estimated_hours: 10.0, remaining_hours: 1.0 } }+#+# * Use `let(:expected_derived_attributes)` to define the expected attribute+# values after derivation:+#+# let(:expected_derived_attributes) { { done_ratio: 90 } }+#+# * Use `let(:expected_kept_attributes)` to define the attributes which are+# expected to not change:+#+# let(:expected_kept_attributes) { %w[estimated_hours] }+RSpec.shared_examples_for "update progress values" do |description:, expected_hints:|+ subject do+ allow(work_package)+ .to receive(:save)++ described_class.new(work_package).call+ end++ it description do+ work_package.attributes = set_attributes+ all_expected_attributes = {}+ all_expected_attributes.merge!(expected_derived_attributes) if defined?(expected_derived_attributes)+ if defined?(expected_kept_attributes)+ kept = work_package.attributes.slice(*expected_kept_attributes)+ if kept.size != expected_kept_attributes.size+ raise ArgumentError, "expected_kept_attributes contains attributes that are not present in the work_package: " \+ "#{expected_kept_attributes - kept.keys} not present in #{work_package.attributes}"+ end+ all_expected_attributes.merge!(kept)+ end+ next if all_expected_attributes.blank?++ subject++ aggregate_failures do+ expect(work_package).to have_attributes(all_expected_attributes)+ expect(work_package).to have_attributes(set_attributes.except(*all_expected_attributes.keys))+ if expected_hints+ expected_hints = expected_hints.transform_keys { to_work_package_field(_1) }+ expect(work_package.derived_progress_hints).to eq(expected_hints)+ end+ # work package is not saved and no errors are created+ expect(work_package).not_to have_received(:save)+ expect(work_package.errors).to be_empty+ end+ end+end