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

[52233] Add feature flag for modification of % complete field

Christophe Bliard38fcd0783636
diff --git a/app/components/work_packages/progress/status_based/modal_body_component.html.erb b/app/components/work_packages/progress/status_based/modal_body_component.html.erb
index 2a4ad71215c..dcd76515a5e 100644
--- a/app/components/work_packages/progress/status_based/modal_body_component.html.erb
+++ b/app/components/work_packages/progress/status_based/modal_body_component.html.erb
@@ -13,11 +13,19 @@
) do |f| %>
<%= flex_layout do |modal_body| %>
<% modal_body.with_row(classes: "FormControl-horizontalGroup--sm-vertical") do |_fields| %>
+ <% if OpenProject::FeatureDecisions.percent_complete_edition_active? %>
<%= render(WorkPackages::ProgressForm.new(f,
work_package:,
mode:,
focused_field:,
touched_field_map:)) %>
+ <% else %>
+ <%= render(WorkPackages::Pre144ProgressForm.new(f,
+ work_package:,
+ mode:,
+ focused_field:,
+ touched_field_map:)) %>
+ <% end %>
<% end %>
<% modal_body.with_row(mt: 3) do |_tooltip| %>
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.erb
index 0e188b98423..49e1a8bdb25 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
@@ -13,11 +13,29 @@
) do |f| %>
<%= flex_layout do |modal_body| %>
<% modal_body.with_row(classes: "FormControl-horizontalGroup--sm-vertical") do |_fields| %>
+ <% if OpenProject::FeatureDecisions.percent_complete_edition_active? %>
<%= render(WorkPackages::ProgressForm.new(f,
work_package:,
mode:,
focused_field:,
touched_field_map:)) %>
+ <% else %>
+ <%# This condition branch to be removed in 15.0 with :percent_complete_edition feature flag removal %>
+ <%= render(WorkPackages::Pre144ProgressForm.new(f,
+ work_package:,
+ mode:,
+ focused_field:,
+ touched_field_map:)) %>
+ <% end %>
+ <% end %>
+
+ <% if !OpenProject::FeatureDecisions.percent_complete_edition_active? %>
+ <%# This condition branch to be removed in 15.0 with :percent_complete_edition feature flag removal %>
+ <% if should_display_migration_warning? %>
+ <% modal_body.with_row(mt: 3) do |_migration_warning| %>
+ <%= render(Primer::Alpha::Banner.new) { t("work_package.progress.modal.migration_warning_text") } %>
+ <% end %>
+ <% end %>
<% end %>
<% modal_body.with_row(mt: 3) do |_tooltip| %>
diff --git a/app/components/work_packages/progress/work_based/modal_body_component.rb b/app/components/work_packages/progress/work_based/modal_body_component.rb
index 0a1455f270d..d16f305c259 100644
--- a/app/components/work_packages/progress/work_based/modal_body_component.rb
+++ b/app/components/work_packages/progress/work_based/modal_body_component.rb
@@ -40,6 +40,15 @@ module WorkPackages
@mode = :work_based
end
+
+ # This method can be safely deleted once the feature flag
+ # :percent_complete_edition is removed, which should happen for
+ # OpenProject 15.0 release.
+ def should_display_migration_warning?
+ return false if OpenProject::FeatureDecisions.percent_complete_edition_active?
+
+ work_package.done_ratio.present? && work_package.estimated_hours.nil? && work_package.remaining_hours.nil?
+ end
end
# rubocop:enable OpenProject/AddPreviewForViewComponent
end
diff --git a/app/contracts/work_packages/base_contract.rb b/app/contracts/work_packages/base_contract.rb
index 4a824ffb468..fe0b075bc9b 100644
--- a/app/contracts/work_packages/base_contract.rb
+++ b/app/contracts/work_packages/base_contract.rb
@@ -53,23 +53,38 @@ module WorkPackages
attribute :project_id
attribute :done_ratio,
- writable: ->(*) { WorkPackage.work_based_mode? } do
- validate_percent_complete_matches_work_and_remaining_work
- validate_percent_complete_is_unset_when_work_is_zero
- validate_percent_complete_is_set_when_work_and_remaining_work_are_not_set
+ writable: ->(*) {
+ OpenProject::FeatureDecisions.percent_complete_edition_active? \
+ && WorkPackage.work_based_mode?
+ } do
+ if OpenProject::FeatureDecisions.percent_complete_edition_active?
+ validate_percent_complete_matches_work_and_remaining_work
+ validate_percent_complete_is_unset_when_work_is_zero
+ validate_percent_complete_is_set_when_work_and_remaining_work_are_not_set
+ end
end
attribute :derived_done_ratio,
writable: false
attribute :estimated_hours do
- validate_work_is_set_when_remaining_work_and_percent_complete_are_set
+ if OpenProject::FeatureDecisions.percent_complete_edition_active?
+ validate_work_is_set_when_remaining_work_and_percent_complete_are_set
+ else
+ # to be removed in 15.0 with :percent_complete_edition feature flag removal
+ validate_work_is_set_when_remaining_work_is_set
+ end
end
attribute :derived_estimated_hours,
writable: false
attribute :remaining_hours do
validate_remaining_work_is_lower_than_work
- validate_remaining_work_is_set_when_work_and_percent_complete_are_set
+ if OpenProject::FeatureDecisions.percent_complete_edition_active?
+ validate_remaining_work_is_set_when_work_and_percent_complete_are_set
+ else
+ # to be removed in 15.0 with :percent_complete_edition feature flag removal
+ validate_remaining_work_is_set_when_work_is_set
+ end
end
attribute :derived_remaining_hours,
writable: false
@@ -332,6 +347,20 @@ module WorkPackages
end
end
+ # to be removed in 15.0 with :percent_complete_edition feature flag removal
+ def validate_remaining_work_is_set_when_work_is_set
+ if work_set? && !remaining_work_set?
+ errors.add(:remaining_hours, :must_be_set_when_work_is_set)
+ end
+ end
+
+ # to be removed in 15.0 with :percent_complete_edition feature flag removal
+ def validate_work_is_set_when_remaining_work_is_set
+ if remaining_work_set? && !work_set?
+ errors.add(:estimated_hours, :must_be_set_when_remaining_work_is_set)
+ end
+ end
+
def validate_work_is_set_when_remaining_work_and_percent_complete_are_set
if remaining_work_set_and_valid? && percent_complete_set_and_valid? && work_unset?
errors.add(:estimated_hours, :must_be_set_when_remaining_work_and_percent_complete_are_set)
diff --git a/app/controllers/work_packages/progress_controller.rb b/app/controllers/work_packages/progress_controller.rb
index 2dd8d604450..b4ad8b0779a 100644
--- a/app/controllers/work_packages/progress_controller.rb
+++ b/app/controllers/work_packages/progress_controller.rb
@@ -57,6 +57,7 @@ class WorkPackages::ProgressController < ApplicationController
touched_field_map:)
end
+ # rubocop:disable Metrics/AbcSize
def create
make_fake_initial_work_package
service_call = set_progress_attributes_to_work_package
@@ -73,12 +74,17 @@ class WorkPackages::ProgressController < ApplicationController
render :update, status: :unprocessable_entity
end
end
+ # following 3 lines to be removed in 15.0 with :percent_complete_edition feature flag removal
+ elsif !OpenProject::FeatureDecisions.percent_complete_edition_active?
+ render json: { estimatedTime: formatted_duration(@work_package.estimated_hours),
+ remainingTime: formatted_duration(@work_package.remaining_hours) }
else
render json: { estimatedTime: formatted_duration(@work_package.estimated_hours),
remainingTime: formatted_duration(@work_package.remaining_hours),
percentageDone: @work_package.done_ratio }
end
end
+ # rubocop:enable Metrics/AbcSize
def update
find_work_package
@@ -149,6 +155,9 @@ class WorkPackages::ProgressController < ApplicationController
def allowed_params
if WorkPackage.use_status_for_done_ratio?
%i[estimated_hours status_id]
+ # two next lines to be removed in 15.0 with :percent_complete_edition feature flag removal
+ elsif !OpenProject::FeatureDecisions.percent_complete_edition_active?
+ %i[estimated_hours remaining_hours]
else
%i[estimated_hours remaining_hours done_ratio]
end
diff --git a/app/forms/work_packages/pre_14_4_progress_form.rb b/app/forms/work_packages/pre_14_4_progress_form.rb
new file mode 100644
index 00000000000..c0e12f0faa7
--- /dev/null
+++ b/app/forms/work_packages/pre_14_4_progress_form.rb
@@ -0,0 +1,209 @@
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-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.
+# ++
+
+# This file can be safely deleted once the feature flag :percent_complete_edition
+# is removed, which should happen for OpenProject 15.0 release.
+#
+# Copied from commit 109b135b and modified to have the [work_package][initial]
+# data needed by progress_controller
+class WorkPackages::Pre144ProgressForm < ApplicationForm
+ ##
+ # Primer::Forms::BaseComponent or ApplicationForm will always autofocus the
+ # first input field with an error present on it. Despite this behavior being
+ # a11y-friendly, it breaks the modal's UX when an invalid input field
+ # is rendered.
+ #
+ # The reason for this is since we're implementing a "format on blur", when
+ # we make a request to the server that will set an input field in an invalid
+ # state and it is returned as such, any time we blur this autofocused field,
+ # we'll perform another request that will still have the input in an invalid
+ # state causing it to autofocus again and preventing us from leaving this
+ # "limbo state".
+ ##
+ def before_render
+ # no-op
+ end
+
+ attr_reader :work_package, :mode
+
+ def initialize(work_package:,
+ mode: :work_based,
+ focused_field: :remaining_hours,
+ touched_field_map: {})
+ super()
+
+ @work_package = work_package
+ @mode = mode
+ @focused_field = focused_field_by_selection(focused_field)
+ @touched_field_map = touched_field_map
+ ensure_only_one_error_for_remaining_work_exceeding_work
+ end
+
+ form do |query_form|
+ query_form.group(layout: :horizontal) do |group|
+ if mode == :status_based
+ select_field_options = { name: :status_id, label: I18n.t(:label_percent_complete) }.tap do |options|
+ options.reverse_merge!(default_field_options(:status_id))
+ options.merge!(disabled: @work_package.new_record?)
+ end
+
+ group.select_list(**select_field_options) do |select_list|
+ WorkPackages::UpdateContract.new(@work_package, User.current)
+ .assignable_statuses
+ .find_each do |status|
+ select_list.option(
+ label: "#{status.name} (#{status.default_done_ratio}%)",
+ value: status.id
+ )
+ end
+ end
+
+ render_text_field(group, name: :estimated_hours, label: I18n.t(:label_work))
+ render_readonly_text_field(group, name: :remaining_hours, label: I18n.t(:label_remaining_work))
+
+ # Add a hidden field in create forms as the select field is disabled and is otherwise not included in the form payload
+ group.hidden(name: :status_id) if @work_package.new_record?
+
+ group.hidden(name: :status_id_touched,
+ value: @touched_field_map["status_id_touched"] || false,
+ data: { "work-packages--progress--touched-field-marker-target": "touchedFieldInput",
+ "referrer-field": "work_package[status_id]" })
+ group.hidden(name: :estimated_hours_touched,
+ value: @touched_field_map["estimated_hours_touched"] || false,
+ data: { "work-packages--progress--touched-field-marker-target": "touchedFieldInput",
+ "referrer-field": "work_package[estimated_hours]" })
+ else
+ render_text_field(group, name: :estimated_hours, label: I18n.t(:label_work))
+ render_text_field(group, name: :remaining_hours, label: I18n.t(:label_remaining_work),
+ disabled: disabled_remaining_work_field?)
+ render_readonly_text_field(group, name: :done_ratio, label: I18n.t(:label_percent_complete))
+
+ group.hidden(name: :estimated_hours_touched,
+ value: @touched_field_map["estimated_hours_touched"] || false,
+ data: { "work-packages--progress--touched-field-marker-target": "touchedFieldInput",
+ "referrer-field": "work_package[estimated_hours]" })
+ group.hidden(name: :remaining_hours_touched,
+ value: @touched_field_map["remaining_hours_touched"] || false,
+ data: { "work-packages--progress--touched-field-marker-target": "touchedFieldInput",
+ "referrer-field": "work_package[remaining_hours]" })
+ end
+ group.fields_for(:initial) do |builder|
+ ::WorkPackages::ProgressForm::InitialValuesForm.new(builder, work_package:, mode:)
+ end
+ end
+ end
+
+ private
+
+ def ensure_only_one_error_for_remaining_work_exceeding_work
+ if work_package.errors.added?(:remaining_hours, :cant_exceed_work) &&
+ work_package.errors.added?(:estimated_hours, :cant_be_inferior_to_remaining_work)
+ error_to_delete =
+ if @focused_field == :estimated_hours
+ :remaining_hours
+ else
+ :estimated_hours
+ end
+ work_package.errors.delete(error_to_delete)
+ end
+ end
+
+ def focused_field_by_selection(field)
+ field
+ end
+
+ def render_text_field(group,
+ name:,
+ label:,
+ disabled: false,
+ placeholder: nil)
+ text_field_options = {
+ name:,
+ value: field_value(name),
+ label:,
+ disabled:,
+ placeholder:
+ }
+ text_field_options.reverse_merge!(default_field_options(name))
+
+ group.text_field(**text_field_options)
+ end
+
+ def render_readonly_text_field(group,
+ name:,
+ label:,
+ disabled: false,
+ placeholder: true)
+ text_field_options = {
+ name:,
+ value: field_value(name),
+ label:,
+ readonly: true,
+ disabled:,
+ classes: "input--readonly",
+ placeholder: ("-" if placeholder)
+ }
+ text_field_options.reverse_merge!(default_field_options(name))
+
+ group.text_field(**text_field_options)
+ end
+
+ def field_value(name)
+ errors = @work_package.errors.where(name)
+ if (user_value = errors.map { |error| error.options[:value] }.find { !_1.nil? })
+ user_value
+ elsif name == :done_ratio
+ format_to_smallest_fractional_part(@work_package.public_send(name))
+ else
+ DurationConverter.output(@work_package.public_send(name))
+ end
+ end
+
+ def format_to_smallest_fractional_part(number)
+ return number if number.nil?
+
+ number % 1 == 0 ? number.to_i : number
+ end
+
+ def default_field_options(name)
+ data = { "work-packages--progress--preview-progress-target": "progressInput",
+ "work-packages--progress--touched-field-marker-target": "progressInput",
+ action: "input->work-packages--progress--touched-field-marker#markFieldAsTouched" }
+
+ if @focused_field == name
+ data[:"work-packages--progress--focus-field-target"] = "fieldToFocus"
+ end
+ { data: }
+ end
+
+ # Remaining work field is enabled when work is set, or when there are errors
+ # on fields so that they can be corrected.
+ def disabled_remaining_work_field?
+ @work_package.estimated_hours.nil? && @work_package.errors.empty?
+ end
+end
diff --git a/app/forms/work_packages/progress_form/initial_values_form.rb b/app/forms/work_packages/progress_form/initial_values_form.rb
index 8f4f54eb297..2c10cc81d8e 100644
--- a/app/forms/work_packages/progress_form/initial_values_form.rb
+++ b/app/forms/work_packages/progress_form/initial_values_form.rb
@@ -49,6 +49,9 @@ class WorkPackages::ProgressForm
value: work_package.estimated_hours_was)
form.hidden(name: :remaining_hours,
value: work_package.remaining_hours_was)
+ # next line to be removed in 15.0 with :percent_complete_edition feature flag removal
+ next unless OpenProject::FeatureDecisions.percent_complete_edition_active?
+
form.hidden(name: :done_ratio,
value: work_package.done_ratio_was)
end
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index 9db5d69c3cb..908524da746 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -286,7 +286,11 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributes
def update_progress_attributes
derive_progress_values_class =
- WorkPackage.use_status_for_done_ratio? ? DeriveProgressValuesStatusBased : DeriveProgressValuesWorkBased
+ if OpenProject::FeatureDecisions.percent_complete_edition_active?
+ WorkPackage.use_status_for_done_ratio? ? DeriveProgressValuesStatusBased : DeriveProgressValuesWorkBased
+ else
+ Pre144DeriveProgressValues
+ end
derive_progress_values_class.new(work_package).call
end
diff --git a/app/services/work_packages/set_attributes_service/pre_14_4_derive_progress_values.rb b/app/services/work_packages/set_attributes_service/pre_14_4_derive_progress_values.rb
new file mode 100644
index 00000000000..852dcb7abd8
--- /dev/null
+++ b/app/services/work_packages/set_attributes_service/pre_14_4_derive_progress_values.rb
@@ -0,0 +1,184 @@
+#-- 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.
+#++
+
+class WorkPackages::SetAttributesService
+ class Pre144DeriveProgressValues
+ attr_reader :work_package
+
+ def initialize(work_package)
+ @work_package = work_package
+ end
+
+ def call
+ update_progress_attributes
+ end
+
+ # From this point, copied over from 109b135b:app/services/work_packages/set_attributes_service.rb#L287-L427
+ def update_progress_attributes
+ if WorkPackage.use_status_for_done_ratio?
+ update_done_ratio
+ update_remaining_hours
+ elsif only_percent_complete_initially_set?
+ update_remaining_hours_from_percent_complete
+ else
+ update_estimated_hours
+ update_remaining_hours
+ update_done_ratio
+ end
+ round_progress_values
+ end
+
+ def only_percent_complete_initially_set?
+ return false if work_package.done_ratio.nil?
+ return false if work_package.remaining_hours.present?
+
+ work_package.estimated_hours_changed? && work_package.estimated_hours.present?
+ end
+
+ def work_was_unset_and_remaining_work_is_set?
+ work_package.estimated_hours_was.nil? && work_package.remaining_hours.present?
+ end
+
+ # Compute and update +done_ratio+ if its dependent attributes are being modified.
+ # The dependent attributes for +done_ratio+ are
+ # - +remaining_hours+
+ # - +estimated_hours+
+ #
+ # Unless both +remaining_hours+ and +estimated_hours+ are set, +done_ratio+ will be
+ # considered nil.
+ def update_done_ratio
+ if WorkPackage.use_status_for_done_ratio?
+ return unless work_package.status_id_changed?
+
+ work_package.done_ratio = work_package.status.default_done_ratio
+ else
+ return unless work_package.remaining_hours_changed? || work_package.estimated_hours_changed?
+
+ work_package.done_ratio = if done_ratio_dependent_attribute_unset?
+ nil
+ else
+ compute_done_ratio
+ end
+ end
+ end
+
+ def round_progress_values
+ rounded = work_package.estimated_hours&.round(2)
+ if rounded != work_package.estimated_hours
+ work_package.estimated_hours = rounded
+ end
+ rounded = work_package.remaining_hours&.round(2)
+ if rounded != work_package.remaining_hours
+ work_package.remaining_hours = rounded
+ end
+ end
+
+ def update_remaining_hours_from_percent_complete
+ return if work_package.remaining_hours_came_from_user?
+ return if work_package.estimated_hours&.negative?
+
+ work_package.remaining_hours = remaining_hours_from_done_ratio_and_estimated_hours
+ end
+
+ def done_ratio_dependent_attribute_unset?
+ work_package.remaining_hours.nil? || work_package.estimated_hours.nil?
+ end
+
+ def compute_done_ratio
+ # do not change done ratio if the values are invalid
+ if invalid_progress_values?
+ return work_package.done_ratio
+ end
+
+ completed_work = work_package.estimated_hours - work_package.remaining_hours
+ completion_ratio = completed_work.to_f / work_package.estimated_hours
+
+ (completion_ratio * 100).round(2)
+ end
+
+ def invalid_progress_values?
+ work = work_package.estimated_hours
+ remaining_work = work_package.remaining_hours
+
+ return true if work.negative?
+ return true if remaining_work.negative?
+
+ work && remaining_work && remaining_work > work
+ end
+
+ def update_estimated_hours
+ return unless WorkPackage.use_field_for_done_ratio?
+ return if work_package.estimated_hours_came_from_user?
+ return unless work_package.remaining_hours_changed?
+
+ work = work_package.estimated_hours
+ remaining_work = work_package.remaining_hours
+ return if work.present?
+ return if remaining_work.nil? || remaining_work.negative?
+
+ work_package.estimated_hours = estimated_hours_from_done_ratio_and_remaining_hours
+ end
+
+ # When in "Status-based" mode for % Complete, remaining hours are based
+ # on the computation of it derived from the status's default done ratio
+ # and the estimated hours. If the estimated hours are unset, then also
+ # unset the remaining hours.
+ # rubocop:disable Metrics/AbcSize,Metrics/PerceivedComplexity
+ def update_remaining_hours
+ if WorkPackage.use_status_for_done_ratio?
+ update_remaining_hours_from_percent_complete
+ elsif WorkPackage.use_field_for_done_ratio? &&
+ work_package.estimated_hours_changed?
+ return if work_package.remaining_hours_came_from_user?
+ return if work_package.estimated_hours&.negative?
+ return if work_was_unset_and_remaining_work_is_set? # remaining work is kept and % complete will be set
+
+ if work_package.estimated_hours.nil? || work_package.remaining_hours.nil?
+ work_package.remaining_hours = work_package.estimated_hours
+ else
+ delta = work_package.estimated_hours - work_package.estimated_hours_was
+ work_package.remaining_hours = (work_package.remaining_hours + delta).clamp(0.0, work_package.estimated_hours)
+ end
+ end
+ end
+ # rubocop:enable Metrics/AbcSize,Metrics/PerceivedComplexity
+
+ def estimated_hours_from_done_ratio_and_remaining_hours
+ remaining_ratio = 1.0 - ((work_package.done_ratio || 0) / 100.0)
+ work_package.remaining_hours / remaining_ratio
+ end
+
+ def remaining_hours_from_done_ratio_and_estimated_hours
+ return nil if work_package.done_ratio.nil? || work_package.estimated_hours.nil?
+
+ completed_work = work_package.estimated_hours * work_package.done_ratio / 100.0
+ remaining_hours = (work_package.estimated_hours - completed_work).round(2)
+ remaining_hours.clamp(0.0, work_package.estimated_hours)
+ end
+ end
+end
diff --git a/config/initializers/feature_decisions.rb b/config/initializers/feature_decisions.rb
index cddbbeff9e4..d7eebf80584 100644
--- a/config/initializers/feature_decisions.rb
+++ b/config/initializers/feature_decisions.rb
@@ -40,3 +40,6 @@ require_relative "../../lib_static/open_project/feature_decisions"
# end
OpenProject::FeatureDecisions.add :favorite_projects
OpenProject::FeatureDecisions.add :dark_mode
+OpenProject::FeatureDecisions.add :percent_complete_edition,
+ description: "Allow edition of % Complete in work-based progress calculation mode. " \
+ "Will be enabled by default in OpenProject 15.0."
diff --git a/config/locales/en.yml b/config/locales/en.yml
index cd212c22d0d..00e6d13151b 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1107,6 +1107,7 @@ en:
done_ratio:
does_not_match_work_and_remaining_work: "does not match work and remaining work"
cannot_be_set_when_work_is_zero: "cannot be set when work is zero"
+ must_be_set_when_remaining_work_is_set: "Required when Remaining work is set."
must_be_set_when_work_and_remaining_work_are_set: "Required when Work and Remaining work are set."
inclusion: "must be between 0 and 100."
due_date:
@@ -1143,6 +1144,7 @@ en:
remaining_hours:
not_a_number: "is not a valid duration."
cant_exceed_work: "Cannot be higher than Work."
+ must_be_set_when_work_is_set: "Required when Work is set."
must_be_set_when_work_and_percent_complete_are_set: "Required when Work and % Complete are set."
format: "%{message}"
readonly_status: "The work package is in a readonly status so its attributes cannot be changed."
@@ -3649,6 +3651,7 @@ en:
modal:
work_based_help_text: "% 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."
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_pre_14_4_without_percent_complete_edition_spec.rb b/spec/components/work_packages/progress/work_based/modal_body_component_pre_14_4_without_percent_complete_edition_spec.rb
new file mode 100644
index 00000000000..e4ed732ee2a
--- /dev/null
+++ b/spec/components/work_packages/progress/work_based/modal_body_component_pre_14_4_without_percent_complete_edition_spec.rb
@@ -0,0 +1,73 @@
+# frozen_string_literal: true
+
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-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.
+# ++
+require "spec_helper"
+require_relative "../shared_modal_examples"
+
+# This file can be safely deleted once the feature flag :percent_complete_edition
+# is removed, which should happen for OpenProject 15.0 release.
+RSpec.describe WorkPackages::Progress::WorkBased::ModalBodyComponent,
+ type: :component,
+ with_flag: { percent_complete_edition: false } do
+ include OpenProject::StaticRouting::UrlHelpers
+
+ describe "#should_display_migration_warning?" do
+ subject(:component) { described_class.new(work_package) }
+
+ context "when the work package has a percent complete value but no work or remaining work set" do
+ let(:work_package) do
+ create(:work_package) do |work_package|
+ work_package.estimated_hours = nil
+ work_package.remaining_hours = nil
+ work_package.done_ratio = 10
+ work_package.save!(validate: false)
+ end
+ end
+
+ it "returns true" do
+ expect(component.should_display_migration_warning?).to be true
+ end
+ end
+
+ context "when the work package has a percent complete value but and a work value but no remaining work set" do
+ let(:work_package) do
+ create(:work_package) do |work_package|
+ work_package.estimated_hours = 55
+ work_package.remaining_hours = nil
+ work_package.done_ratio = 10
+ work_package.save!(validate: false)
+ end
+ end
+
+ it "returns false" do
+ expect(component.should_display_migration_warning?).to be false
+ end
+ end
+ end
+end
diff --git a/spec/contracts/work_packages/base_contract_pre_14_4_without_percent_complete_edition_spec.rb b/spec/contracts/work_packages/base_contract_pre_14_4_without_percent_complete_edition_spec.rb
new file mode 100644
index 00000000000..eab7e12f906
--- /dev/null
+++ b/spec/contracts/work_packages/base_contract_pre_14_4_without_percent_complete_edition_spec.rb
@@ -0,0 +1,1250 @@
+#-- 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.
+#++
+
+require "spec_helper"
+require "contracts/shared/model_contract_shared_context"
+
+# This file can be safely deleted once the feature flag :percent_complete_edition
+# is removed, which should happen for OpenProject 15.0 release.
+# Copied from commit 109b135b with a few changes for greater_than_or_equal_to validation
+RSpec.describe WorkPackages::BaseContract,
+ with_flag: { percent_complete_edition: false } do
+ include_context "ModelContract shared context"
+
+ let(:work_package) do
+ build_stubbed(:work_package,
+ type:,
+ done_ratio: 50,
+ estimated_hours: 6.0,
+ remaining_hours: 3.0,
+ project:)
+ end
+ let(:project) { build_stubbed(:project) }
+ let(:current_user) { member }
+ let(:permissions) do
+ %i(
+ view_work_packages
+ view_work_package_watchers
+ edit_work_packages
+ add_work_package_watchers
+ delete_work_package_watchers
+ manage_work_package_relations
+ add_work_package_notes
+ assign_versions
+ )
+ end
+ let(:changed_values) { [] }
+ let(:type) { build_stubbed(:type, is_milestone:) }
+ let(:is_milestone) { false }
+
+ let(:member) { build_stubbed(:user) }
+
+ before do
+ mock_permissions_for(member) do |mock|
+ mock.allow_in_project *permissions, project:
+ end
+ end
+
+ subject(:contract) { described_class.new(work_package, current_user) }
+
+ shared_examples_for "invalid if changed" do |attribute|
+ before do
+ allow(work_package).to receive(:changed).and_return(changed_values.map(&:to_s))
+ end
+
+ before do
+ contract.validate
+ end
+
+ context "when has changed" do
+ let(:changed_values) { [attribute] }
+
+ it("is invalid") do
+ expect(contract.errors.symbols_for(attribute)).to contain_exactly(:error_readonly)
+ end
+ end
+
+ context "when not changed" do
+ let(:changed_values) { [] }
+
+ it("is valid") { expect(contract.errors).to be_empty }
+ end
+ end
+
+ shared_examples "a parent unwritable property" do |attribute, schedule_sensitive: false|
+ before do
+ allow(work_package).to receive(:changed).and_return(changed_values.map(&:to_s))
+ end
+
+ context "when no parent" do
+ before do
+ allow(work_package)
+ .to receive(:leaf?)
+ .and_return(true)
+
+ contract.validate
+ end
+
+ context "when not changed" do
+ let(:changed_values) { [] }
+
+ it("is valid") { expect(contract.errors).to be_empty }
+ end
+
+ context "when has changed" do
+ let(:changed_values) { [attribute] }
+
+ it("is valid") { expect(contract.errors).to be_empty }
+ end
+ end
+
+ context "when is a parent" do
+ let(:schedule_manually) { false }
+
+ before do
+ work_package.schedule_manually = schedule_manually
+
+ allow(work_package)
+ .to receive(:leaf?)
+ .and_return(false)
+ contract.validate
+ end
+
+ context "when not changed" do
+ let(:changed_values) { [] }
+
+ it("is valid") { expect(contract.errors).to be_empty }
+ end
+
+ context "when has changed" do
+ let(:changed_values) { [attribute] }
+
+ it("is invalid (read only)") do
+ expect(contract.errors.symbols_for(attribute)).to contain_exactly(:error_readonly)
+ end
+ end
+
+ if schedule_sensitive
+ context "when is scheduled manually" do
+ let(:schedule_manually) { true }
+
+ context "when has changed" do
+ let(:changed_values) { [attribute] }
+
+ it("is valid") { expect(contract.errors).to be_empty }
+ end
+ end
+ end
+ end
+ end
+
+ describe "status" do
+ context "for readonly status" do
+ before do
+ allow(work_package)
+ .to receive(:readonly_status?)
+ .and_return true
+ end
+
+ it "only sets status to allowed" do
+ expect(contract.writable_attributes).to eq(%w[status status_id])
+ end
+ end
+
+ context "when work_package has a closed version and status" do
+ before do
+ version = build_stubbed(:version, status: "closed")
+
+ work_package.version = version
+ allow(work_package.status)
+ .to receive(:is_closed?)
+ .and_return(true)
+ end
+
+ it "is not writable" do
+ expect(contract).not_to be_writable(:status)
+ end
+
+ context "when we only switched into that status now" do
+ before do
+ allow(work_package)
+ .to receive(:status_id_change)
+ .and_return [1, 2]
+ end
+
+ it "is writable" do
+ expect(contract).to be_writable(:status)
+ end
+ end
+ end
+
+ context "when status is inexistent" do
+ before do
+ work_package.status = Status::InexistentStatus.new
+ end
+
+ it "is invalid" do
+ contract.validate
+
+ expect(subject.errors.symbols_for(:status))
+ .to contain_exactly(:does_not_exist)
+ end
+ end
+
+ describe "transitions" do
+ let(:roles) { [build_stubbed(:project_role)] }
+ let(:valid_transition_result) { true }
+ let(:new_status) { build_stubbed(:status) }
+ let(:from_id) { work_package.status_id }
+ let(:to_id) { new_status.id }
+ let(:status_change) { work_package.status = new_status }
+
+ before do
+ new_statuses_scope = double("new statuses scope") # rubocop:disable RSpec/VerifiedDoubles
+
+ allow(Status)
+ .to receive(:find_by)
+ .with(id: work_package.status_id)
+ .and_return(work_package.status)
+
+ # Breaking abstraction here to avoid mocking hell.
+ # We might want to extract the assignable_... into separate
+ # objects.
+ allow(contract)
+ .to receive(:new_statuses_allowed_from)
+ .with(work_package.status)
+ .and_return(new_statuses_scope)
+
+ allow(new_statuses_scope)
+ .to receive(:order_by_position)
+ .and_return(new_statuses_scope)
+
+ allow(new_statuses_scope)
+ .to receive(:exists?)
+ .with(new_status.id)
+ .and_return(valid_transition_result)
+
+ status_change
+
+ contract.validate
+ end
+
+ context "when valid transition" do
+ it "is valid" do
+ expect(subject.errors.symbols_for(:status_id))
+ .to be_empty
+ end
+ end
+
+ context "when invalid transition" do
+ let(:valid_transition_result) { false }
+
+ it "is invalid" do
+ expect(subject.errors.symbols_for(:status_id))
+ .to contain_exactly(:status_transition_invalid)
+ end
+ end
+
+ context "when status is nil" do
+ let(:status_change) { work_package.status = nil }
+
+ it "is invalid" do
+ expect(subject.errors.symbols_for(:status))
+ .to contain_exactly(:blank)
+ end
+ end
+
+ context "when invalid transition but the type changed as well" do
+ let(:valid_transition_result) { false }
+ let(:status_change) do
+ work_package.status = new_status
+ work_package.type = build_stubbed(:type)
+ end
+
+ it "is valid" do
+ expect(subject.errors.symbols_for(:status_id))
+ .to be_empty
+ end
+ end
+ end
+ end
+
+ describe "work (estimated hours)" do
+ let(:estimated_hours) { 1 }
+ let(:remaining_hours) { 0 }
+
+ before do
+ work_package.estimated_hours = estimated_hours
+ work_package.remaining_hours = remaining_hours
+ work_package.clear_remaining_hours_change
+ end
+
+ context "when > 0" do
+ let(:estimated_hours) { 1 }
+
+ include_examples "contract is valid"
+ end
+
+ context "when 0" do
+ let(:estimated_hours) { 0 }
+
+ include_examples "contract is valid"
+ end
+
+ context "when nil while remaining work is nil" do
+ let(:estimated_hours) { nil }
+ let(:remaining_hours) { nil }
+
+ include_examples "contract is valid"
+ end
+
+ context "when nil while remaining work is set" do
+ let(:estimated_hours) { nil }
+ let(:remaining_hours) { 2.0 }
+
+ include_examples "contract is invalid", estimated_hours: :must_be_set_when_remaining_work_is_set
+ end
+
+ context "when < 0" do
+ let(:estimated_hours) { -1 }
+ let(:remaining_hours) { -2 }
+
+ include_examples "contract is invalid", estimated_hours: :greater_than_or_equal_to
+ end
+
+ context "when inferior to remaining work" do
+ let(:estimated_hours) { 5.0 }
+ let(:remaining_hours) { 7.0 }
+
+ include_examples "contract is invalid", estimated_hours: :cant_be_inferior_to_remaining_work
+ end
+ end
+
+ describe "total work (derived estimated hours)" do
+ let(:changed_values) { [] }
+ let(:attribute) { :derived_estimated_hours }
+
+ before do
+ allow(work_package).to receive(:changed).and_return(changed_values.map(&:to_s))
+
+ contract.validate
+ end
+
+ context "when has not changed" do
+ let(:changed_values) { [] }
+
+ it("is valid") { expect(contract.errors).to be_empty }
+ end
+
+ context "when has changed" do
+ let(:changed_values) { [attribute] }
+
+ it("is invalid (read only)") do
+ expect(contract.errors.symbols_for(attribute)).to contain_exactly(:error_readonly)
+ end
+ end
+ end
+
+ describe "remaining work (remaining hours)" do
+ before do
+ work_package.estimated_hours = 5.0
+ work_package.clear_remaining_hours_change
+ work_package.remaining_hours = remaining_hours
+ end
+
+ context "when it's the same as work" do
+ let(:remaining_hours) { 5.0 }
+
+ include_examples "contract is valid"
+ end
+
+ context "when it's less than work" do
+ let(:remaining_hours) { 4.0 }
+
+ include_examples "contract is valid"
+ end
+
+ context "when it's greater than work" do
+ let(:remaining_hours) { 6.0 }
+
+ include_examples "contract is invalid", remaining_hours: :cant_exceed_work
+ end
+
+ context "when unset" do
+ let(:remaining_hours) { nil }
+
+ include_examples "contract is invalid", remaining_hours: :must_be_set_when_work_is_set
+ end
+ end
+
+ describe "work and remaining work" do
+ context "when changing both and remaining work exceeds work" do
+ before do
+ work_package.estimated_hours = 5.0
+ work_package.remaining_hours = 6.0
+ end
+
+ include_examples "contract is invalid", estimated_hours: :cant_be_inferior_to_remaining_work,
+ remaining_hours: :cant_exceed_work
+ end
+ end
+
+ shared_examples_for "a date attribute" do |attribute|
+ context "for a date" do
+ before do
+ work_package.send(:"#{attribute}=", Time.zone.today)
+ contract.validate
+ end
+
+ it "is valid" do
+ expect(subject.errors.symbols_for(attribute))
+ .to be_empty
+ end
+ end
+
+ context "for a string representing a date" do
+ before do
+ work_package.send(:"#{attribute}=", "01/01/17")
+ contract.validate
+ end
+
+ it "is valid" do
+ expect(subject.errors.symbols_for(attribute))
+ .to be_empty
+ end
+ end
+
+ context "for a non-date" do
+ before do
+ work_package.send(:"#{attribute}=", "not a date")
+ contract.validate
+ end
+
+ it "is invalid" do
+ expect(subject.errors.symbols_for(attribute))
+ .to contain_exactly(:not_a_date)
+ end
+ end
+ end
+
+ describe "start date" do
+ it_behaves_like "a parent unwritable property", :start_date, schedule_sensitive: true
+ it_behaves_like "a date attribute", :start_date
+
+ context "as before soonest start date of parent" do
+ let(:schedule_manually) { false }
+
+ before do
+ work_package.schedule_manually = schedule_manually
+ allow(work_package)
+ .to receive_messages(parent: build_stubbed(:work_package),
+ soonest_start: Time.zone.today + 4.days)
+
+ work_package.start_date = Time.zone.today + 2.days
+ end
+
+ context "when scheduled automatically" do
+ it "notes the error" do
+ contract.validate
+
+ message = I18n.t("activerecord.errors.models.work_package.attributes.start_date.violates_relationships",
+ soonest_start: Time.zone.today + 4.days)
+
+ expect(contract.errors[:start_date])
+ .to contain_exactly(message)
+ end
+ end
+
+ context "when scheduled manually" do
+ let(:schedule_manually) { true }
+
+ it_behaves_like "contract is valid"
+ end
+ end
+
+ context "when setting due date and duration without start date" do
+ before do
+ work_package.duration = 1
+ work_package.start_date = nil
+ work_package.due_date = Time.zone.today
+ end
+
+ it_behaves_like "contract is invalid", start_date: :cannot_be_null
+ end
+ end
+
+ describe "due date" do
+ it_behaves_like "a parent unwritable property", :due_date, schedule_sensitive: true
+ it_behaves_like "a date attribute", :due_date
+
+ it "returns an error when trying to set it before the start date" do
+ work_package.start_date = Time.zone.today + 2.days
+ work_package.due_date = Time.zone.today
+
+ contract.validate
+
+ message = I18n.t("activerecord.errors.messages.greater_than_or_equal_to_start_date")
+
+ expect(contract.errors[:due_date])
+ .to include message
+ end
+
+ context "when start date is not set and due date is before soonest start date of parent" do
+ let(:schedule_manually) { false }
+
+ before do
+ work_package.schedule_manually = schedule_manually
+ allow(work_package)
+ .to receive_messages(parent: build_stubbed(:work_package),
+ soonest_start: Time.zone.today + 4.days)
+
+ work_package.start_date = nil
+ work_package.due_date = Time.zone.today + 2.days
+ end
+
+ context "when scheduled automatically" do
+ it "notes the error" do
+ contract.validate
+
+ message = I18n.t("activerecord.errors.models.work_package.attributes.start_date.violates_relationships",
+ soonest_start: Time.zone.today + 4.days)
+
+ expect(contract.errors[:due_date])
+ .to contain_exactly(message)
+ end
+ end
+
+ context "when scheduled manually" do
+ let(:schedule_manually) { true }
+
+ it_behaves_like "contract is valid"
+ end
+ end
+
+ context "when setting start date and duration without due date" do
+ before do
+ work_package.duration = 1
+ work_package.start_date = Time.zone.today
+ work_package.due_date = nil
+ end
+
+ it_behaves_like "contract is invalid", due_date: :cannot_be_null
+ end
+ end
+
+ describe "duration" do
+ context "when setting duration" do
+ before do
+ work_package.duration = 5
+ end
+
+ it_behaves_like "contract is valid"
+ end
+
+ context "when setting duration for a milestone type work package" do
+ let(:is_milestone) { true }
+
+ before do
+ work_package.duration = 5
+ end
+
+ it_behaves_like "contract is invalid", duration: :not_available_for_milestones
+ end
+
+ context "when setting duration to nil for a milestone type work package" do
+ let(:is_milestone) { true }
+
+ before do
+ work_package.duration = nil
+ end
+
+ it_behaves_like "contract is invalid"
+ end
+
+ context "when setting duration to 1 for a milestone type work package" do
+ let(:is_milestone) { true }
+
+ before do
+ work_package.duration = 1
+ end
+
+ it_behaves_like "contract is valid"
+ end
+
+ context "when setting duration to 0" do
+ before do
+ work_package.duration = 0
+ end
+
+ it_behaves_like "contract is invalid", duration: :greater_than
+ end
+
+ context "when setting duration to a floating point" do
+ before do
+ work_package.duration = 4.5
+ end
+
+ it_behaves_like "contract is invalid", duration: :not_an_integer
+ end
+
+ context "when setting duration to a negative value" do
+ before do
+ work_package.duration = -5
+ end
+
+ it_behaves_like "contract is invalid", duration: :greater_than
+ end
+
+ context "when setting duration and dates" do
+ before do
+ work_package.ignore_non_working_days = true
+ work_package.duration = 6
+ work_package.start_date = Time.zone.today - 4.days
+ work_package.due_date = Time.zone.today + 1.day
+ end
+
+ it_behaves_like "contract is valid"
+ end
+
+ context "when setting duration and dates while covering non-working days" do
+ before do
+ week_with_saturday_and_sunday_as_weekend
+ work_package.ignore_non_working_days = false
+ work_package.duration = 6
+ work_package.start_date = "2022-08-22"
+ work_package.due_date = "2022-08-29"
+ end
+
+ it_behaves_like "contract is valid"
+ end
+
+ context "when setting duration and dates and duration is too small" do
+ before do
+ work_package.ignore_non_working_days = true
+ work_package.duration = 5
+ work_package.start_date = Time.zone.today - 4.days
+ work_package.due_date = Time.zone.today + 1.day
+ end
+
+ it_behaves_like "contract is invalid", duration: :smaller_than_dates
+ end
+
+ context "when setting duration and dates while covering non-working days and duration is too small" do
+ before do
+ week_with_saturday_and_sunday_as_weekend
+ work_package.ignore_non_working_days = false
+ work_package.duration = 1
+ work_package.start_date = "2022-08-22"
+ work_package.due_date = "2022-08-29"
+ end
+
+ it_behaves_like "contract is invalid", duration: :smaller_than_dates
+ end
+
+ context "when setting duration and dates and duration is too big" do
+ before do
+ work_package.ignore_non_working_days = true
+ work_package.duration = 7
+ work_package.start_date = Time.zone.today - 4.days
+ work_package.due_date = Time.zone.today + 1.day
+ end
+
+ it_behaves_like "contract is invalid", duration: :larger_than_dates
+ end
+
+ context "when setting duration and dates while covering non-working days and duration is too big" do
+ before do
+ week_with_saturday_and_sunday_as_weekend
+ work_package.ignore_non_working_days = false
+ work_package.duration = 99
+ work_package.start_date = "2022-08-22"
+ work_package.due_date = "2022-08-29"
+ end
+
+ it_behaves_like "contract is invalid", duration: :larger_than_dates
+ end
+
+ context "when setting start date and due date without duration" do
+ before do
+ work_package.duration = nil
+ work_package.start_date = Time.zone.today
+ work_package.due_date = Time.zone.today
+ end
+
+ it_behaves_like "contract is invalid", duration: :cannot_be_null
+ end
+ end
+
+ describe "ignore_non_working_days" do
+ context "when setting the value to true" do
+ before do
+ work_package.ignore_non_working_days = true
+ end
+
+ it_behaves_like "contract is valid"
+ end
+
+ context "when setting the value to false" do
+ before do
+ work_package.ignore_non_working_days = false
+ end
+
+ it_behaves_like "contract is valid"
+ end
+ end
+
+ describe "percentage done" do
+ context "when % Complete inferred by status",
+ with_settings: { work_package_done_ratio: "status" } do
+ it_behaves_like "invalid if changed", :done_ratio
+ end
+ end
+
+ describe "version" do
+ subject(:contract) { described_class.new(work_package, current_user) }
+
+ let(:assignable_version) { build_stubbed(:version) }
+ let(:invalid_version) { build_stubbed(:version) }
+
+ before do
+ allow(work_package).to receive(:assignable_versions).and_return [assignable_version]
+ end
+
+ context "for assignable version" do
+ before do
+ work_package.version = assignable_version
+ subject.validate
+ end
+
+ it "is valid" do
+ expect(subject.errors).to be_empty
+ end
+ end
+
+ context "for non assignable version" do
+ before do
+ work_package.version = invalid_version
+ subject.validate
+ end
+
+ it "is invalid" do
+ expect(subject.errors.symbols_for(:version_id)).to eql [:inclusion]
+ end
+ end
+
+ context "for a closed version" do
+ let(:assignable_version) { build_stubbed(:version, status: "closed") }
+
+ context "when reopening a work package" do
+ before do
+ allow(work_package)
+ .to receive(:reopened?)
+ .and_return(true)
+
+ work_package.version = assignable_version
+ subject.validate
+ end
+
+ it "is invalid" do
+ expect(subject.errors[:base]).to eql [I18n.t(:error_can_not_reopen_work_package_on_closed_version)]
+ end
+ end
+
+ context "when not reopening the work package" do
+ before do
+ work_package.version = assignable_version
+ subject.validate
+ end
+
+ it "is valid" do
+ expect(subject.errors).to be_empty
+ end
+ end
+ end
+ end
+
+ describe "parent" do
+ let(:parent) { build_stubbed(:work_package) }
+
+ before do
+ work_package.parent = parent
+ end
+
+ subject do
+ contract.validate
+
+ # while we do validate the parent
+ # the errors are still put on :base so that the messages can be reused
+ contract.errors.symbols_for(:parent)
+ end
+
+ context "when self assigning" do
+ let(:parent) { work_package }
+
+ it "returns an error for the parent" do
+ expect(subject)
+ .to eq [:cannot_be_self_assigned]
+ end
+ end
+
+ context "when the intended parent is not relatable" do
+ before do
+ scope = instance_double(ActiveRecord::Relation)
+
+ allow(WorkPackage)
+ .to receive(:relatable)
+ .with(work_package, Relation::TYPE_PARENT)
+ .and_return(scope)
+
+ allow(scope)
+ .to receive(:where)
+ .with(id: parent.id)
+ .and_return([])
+ end
+
+ it "is invalid" do
+ expect(subject)
+ .to include(:cant_link_a_work_package_with_a_descendant)
+ end
+ end
+
+ context "when an invalid parent_id is set" do
+ before do
+ work_package.parent = nil
+ work_package.parent_id = -1
+ end
+
+ it "is invalid" do
+ expect(subject)
+ .to include(:does_not_exist)
+ end
+ end
+ end
+
+ describe "type" do
+ context "for disabled type" do
+ before do
+ allow(project)
+ .to receive(:types)
+ .and_return([])
+ end
+
+ describe "not changing the type" do
+ it "is valid" do
+ subject.validate
+
+ expect(subject)
+ .to be_valid
+ end
+ end
+
+ describe "changing the type" do
+ let(:other_type) { build_stubbed(:type) }
+
+ it "is invalid" do
+ work_package.type = other_type
+
+ subject.validate
+
+ expect(subject.errors.symbols_for(:type_id))
+ .to contain_exactly(:inclusion)
+ end
+ end
+
+ describe "changing the project (and that one not having the type)" do
+ let(:other_project) { build_stubbed(:project) }
+
+ it "is invalid" do
+ work_package.project = other_project
+
+ subject.validate
+
+ expect(subject.errors.symbols_for(:type_id))
+ .to contain_exactly(:inclusion)
+ end
+ end
+ end
+
+ context "for inexistent type" do
+ before do
+ work_package.type = Type::InexistentType.new
+
+ contract.validate
+ end
+
+ it "is invalid" do
+ expect(contract.errors.symbols_for(:type))
+ .to contain_exactly(:does_not_exist)
+ end
+ end
+ end
+
+ describe "assigned_to" do
+ context "for inexistent user" do
+ before do
+ work_package.assigned_to = Users::InexistentUser.new
+
+ contract.validate
+ end
+
+ it "is invalid" do
+ expect(contract.errors.symbols_for(:assigned_to))
+ .to contain_exactly(:does_not_exist)
+ end
+ end
+ end
+
+ describe "category" do
+ let(:category) { build_stubbed(:category) }
+
+ context "for one of the project's categories" do
+ before do
+ allow(project)
+ .to receive(:categories)
+ .and_return [category]
+
+ work_package.category = category
+
+ contract.validate
+ end
+
+ it "is valid" do
+ expect(contract.errors.symbols_for(:category))
+ .to be_empty
+ end
+ end
+
+ context "when empty" do
+ before do
+ work_package.category = nil
+
+ contract.validate
+ end
+
+ it "is valid" do
+ expect(contract.errors.symbols_for(:category))
+ .to be_empty
+ end
+ end
+
+ context "for inexistent category (e.g. removed)" do
+ before do
+ work_package.category_id = 5
+
+ contract.validate
+ end
+
+ it "is invalid" do
+ expect(contract.errors.symbols_for(:category))
+ .to contain_exactly(:does_not_exist)
+ end
+ end
+
+ context "when not of the project" do
+ before do
+ allow(project)
+ .to receive(:categories)
+ .and_return []
+
+ work_package.category = category
+
+ contract.validate
+ end
+
+ it "is invalid" do
+ expect(contract.errors.symbols_for(:category))
+ .to contain_exactly(:only_same_project_categories_allowed)
+ end
+ end
+ end
+
+ describe "priority" do
+ let (:active_priority) { build_stubbed(:priority) }
+ let (:inactive_priority) { build_stubbed(:priority, active: false) }
+
+ context "as active priority" do
+ before do
+ work_package.priority = active_priority
+
+ contract.validate
+ end
+
+ it "is valid" do
+ expect(contract.errors.symbols_for(:priority_id))
+ .to be_empty
+ end
+ end
+
+ context "as inactive priority" do
+ before do
+ work_package.priority = inactive_priority
+
+ contract.validate
+ end
+
+ it "is invalid" do
+ expect(contract.errors.symbols_for(:priority_id))
+ .to contain_exactly(:only_active_priorities_allowed)
+ end
+ end
+
+ context "as inactive priority but priority not changed" do
+ before do
+ work_package.priority = inactive_priority
+ work_package.clear_changes_information
+
+ contract.validate
+ end
+
+ it "is valid" do
+ expect(contract.errors.symbols_for(:priority_id))
+ .to be_empty
+ end
+ end
+
+ context "as inexistent priority" do
+ before do
+ work_package.priority = Priority::InexistentPriority.new
+
+ contract.validate
+ end
+
+ it "is invalid" do
+ expect(contract.errors.symbols_for(:priority))
+ .to contain_exactly(:does_not_exist)
+ end
+ end
+ end
+
+ describe "#assignable_statuses" do
+ let(:role) { build_stubbed(:project_role) }
+ let(:type) { build_stubbed(:type) }
+ let(:assignee_user) { build_stubbed(:user) }
+ let(:author_user) { build_stubbed(:user) }
+ let(:current_status) { build_stubbed(:status) }
+ let(:version) { build_stubbed(:version) }
+ let(:work_package) do
+ build_stubbed(:work_package,
+ assigned_to: assignee_user,
+ author: author_user,
+ status: current_status,
+ version:,
+ type:)
+ end
+ let!(:default_status) do
+ status = build_stubbed(:status)
+
+ allow(Status)
+ .to receive(:default)
+ .and_return(status)
+
+ status
+ end
+
+ let(:roles) { [role] }
+
+ before do
+ allow(current_user)
+ .to receive(:roles_for_work_package)
+ .with(work_package)
+ .and_return(roles)
+ end
+
+ shared_examples_for "new_statuses_allowed_to" do
+ let(:base_scope) do
+ from_workflows = Workflow
+ .from_status(current_status.id, type.id, [role.id], author, assignee)
+ .select(:new_status_id)
+
+ Status.where(id: from_workflows)
+ .or(Status.where(id: current_status.id))
+ end
+
+ it "returns a scope that returns current_status and those available by workflow" do
+ expect(contract.assignable_statuses.to_sql)
+ .to eql base_scope.order_by_position.to_sql
+ end
+
+ it "removes closed statuses if blocked" do
+ allow(work_package)
+ .to receive(:blocked?)
+ .and_return(true)
+
+ expected = base_scope.where(is_closed: false).order_by_position
+
+ expect(contract.assignable_statuses.to_sql)
+ .to eql expected.to_sql
+ end
+
+ context "if the current status is closed and the version is closed as well" do
+ let(:version) { build_stubbed(:version, status: "closed") }
+ let(:current_status) { build_stubbed(:status, is_closed: true) }
+
+ it "only allows the current status" do
+ expect(contract.assignable_statuses.to_sql)
+ .to eql Status.where(id: current_status.id).to_sql
+ end
+ end
+ end
+
+ context "with somebody else asking" do
+ it_behaves_like "new_statuses_allowed_to" do
+ let(:author) { false }
+ let(:assignee) { false }
+ end
+ end
+
+ context "with the author asking" do
+ let(:current_user) { author_user }
+
+ it_behaves_like "new_statuses_allowed_to" do
+ let(:author) { true }
+ let(:assignee) { false }
+ end
+ end
+
+ context "with the assignee asking" do
+ let(:current_user) { assignee_user }
+
+ it_behaves_like "new_statuses_allowed_to" do
+ let(:author) { false }
+ let(:assignee) { true }
+ end
+ end
+
+ context "with the assignee changing and asking as new assignee" do
+ before do
+ work_package.assigned_to = current_user
+ end
+
+ # is using the former assignee
+ it_behaves_like "new_statuses_allowed_to" do
+ let(:author) { false }
+ let(:assignee) { false }
+ end
+ end
+
+ context "with the status having changed" do
+ let(:new_status) { build_stubbed(:status) }
+
+ before do
+ allow(work_package).to receive_messages(persisted?: true,
+ status_id_changed?: true)
+
+ allow(Status)
+ .to receive(:find_by)
+ .with(id: work_package.status_id_was)
+ .and_return(current_status)
+
+ work_package.status = new_status
+ end
+
+ it_behaves_like "new_statuses_allowed_to" do
+ let(:author) { false }
+ let(:assignee) { false }
+ end
+ end
+ end
+
+ describe "#assignable_types" do
+ let(:scope) do
+ instance_double(ActiveRecord::Querying).tap do |s|
+ allow(s)
+ .to receive(:includes)
+ .and_return(s)
+ end
+ end
+
+ context "when project nil" do
+ before do
+ work_package.project = nil
+ end
+
+ it "is all types" do
+ allow(Type)
+ .to receive(:includes)
+ .and_return(scope)
+
+ expect(contract.assignable_types)
+ .to eql(scope)
+ end
+ end
+
+ context "when project defined" do
+ it "is all types of the project" do
+ allow(work_package.project)
+ .to receive(:types)
+ .and_return(scope)
+
+ expect(contract.assignable_types)
+ .to eql(scope)
+ end
+ end
+ end
+
+ describe "#assignable_versions" do
+ let(:result) { double }
+
+ it "calls through to the work package" do
+ allow(work_package).to receive(:assignable_versions).and_return(result)
+ expect(subject.assignable_values(:version, current_user)).to eql(result)
+ expect(work_package).to have_received(:assignable_versions)
+ end
+ end
+
+ describe "#assignable_priorities" do
+ let(:active_priority) { build(:priority, active: true) }
+ let(:inactive_priority) { build(:priority, active: false) }
+
+ before do
+ active_priority.save!
+ inactive_priority.save!
+ end
+
+ it "returns only active priorities" do
+ expect(subject.assignable_values(:priority, current_user).size).to be >= 1
+ subject.assignable_values(:priority, current_user).each do |priority|
+ expect(priority.active).to be_truthy
+ end
+ end
+ end
+
+ describe "#assignable_categories" do
+ let(:category) { instance_double(Category) }
+
+ before do
+ allow(project).to receive(:categories).and_return([category])
+ end
+
+ it "returns all categories of the project" do
+ expect(subject.assignable_values(:category, current_user)).to contain_exactly(category)
+ end
+ end
+
+ include_examples "contract reuses the model errors"
+end
diff --git a/spec/contracts/work_packages/base_contract_spec.rb b/spec/contracts/work_packages/base_contract_spec.rb
index 1b616e9e78d..332cc604762 100644
--- a/spec/contracts/work_packages/base_contract_spec.rb
+++ b/spec/contracts/work_packages/base_contract_spec.rb
@@ -29,7 +29,8 @@
require "spec_helper"
require "contracts/shared/model_contract_shared_context"
-RSpec.describe WorkPackages::BaseContract do
+RSpec.describe WorkPackages::BaseContract,
+ with_flag: { percent_complete_edition: true } do
include_context "ModelContract shared context"
let(:work_package) do
diff --git a/spec/controllers/work_packages/progress_controller_pre_14_4_without_percent_complete_edition_spec.rb b/spec/controllers/work_packages/progress_controller_pre_14_4_without_percent_complete_edition_spec.rb
new file mode 100644
index 00000000000..f1ed97af742
--- /dev/null
+++ b/spec/controllers/work_packages/progress_controller_pre_14_4_without_percent_complete_edition_spec.rb
@@ -0,0 +1,88 @@
+# 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.
+#++
+
+require "spec_helper"
+
+# This file can be safely deleted once the feature flag :percent_complete_edition
+# is removed, which should happen for OpenProject 15.0 release.
+# Copied from commit 109b135b]
+RSpec.describe WorkPackages::ProgressController,
+ with_flag: { percent_complete_edition: false } do
+ shared_let(:user) { create(:admin) }
+ shared_let(:work_package) { create(:work_package) }
+
+ current_user { user }
+
+ describe "POST /work_packages/:d/progress" do
+ let(:params) do
+ {
+ "work_package_id" => work_package.id,
+ "work_package" => {
+ "estimated_hours" => "42",
+ "remaining_hours" => "4h",
+ "done_ratio" => "90",
+ "estimated_hours_touched" => "false",
+ "remaining_hours_touched" => "false"
+ }
+ }
+ end
+
+ it "updates the work package progress values with touched values (none touched)" do
+ patch("update", params:, as: :turbo_stream)
+ work_package.reload
+
+ expect(work_package.estimated_hours).to be_nil
+ expect(work_package.remaining_hours).to be_nil
+ end
+
+ it "updates the work package progress values with touched values (only work touched)" do
+ params["work_package"]["estimated_hours_touched"] = "true"
+
+ patch("update", params:, as: :turbo_stream)
+ work_package.reload
+
+ expect(work_package.estimated_hours).to eq(42)
+ # when not supplied by the user, the remaining work is set to the same
+ # value as work
+ expect(work_package.remaining_hours).to eq(42)
+ end
+
+ it "updates the work package progress values (all values touched)" do
+ params["work_package"]["estimated_hours_touched"] = "true"
+ params["work_package"]["remaining_hours_touched"] = "true"
+
+ patch("update", params:, as: :turbo_stream)
+ work_package.reload
+
+ expect(work_package.estimated_hours).to eq(42)
+ expect(work_package.remaining_hours).to eq(4)
+ end
+ end
+end
diff --git a/spec/controllers/work_packages/progress_controller_spec.rb b/spec/controllers/work_packages/progress_controller_spec.rb
index cda66173591..68196d3b8aa 100644
--- a/spec/controllers/work_packages/progress_controller_spec.rb
+++ b/spec/controllers/work_packages/progress_controller_spec.rb
@@ -30,7 +30,8 @@
require "spec_helper"
-RSpec.describe WorkPackages::ProgressController do
+RSpec.describe WorkPackages::ProgressController,
+ with_flag: { percent_complete_edition: true } do
shared_let(:user) { create(:admin) }
shared_let(:work_package) { create(:work_package) }
diff --git a/spec/features/work_packages/progress_modal_pre_14_4_without_percent_complete_edition_spec.rb b/spec/features/work_packages/progress_modal_pre_14_4_without_percent_complete_edition_spec.rb
new file mode 100644
index 00000000000..2fe287323c4
--- /dev/null
+++ b/spec/features/work_packages/progress_modal_pre_14_4_without_percent_complete_edition_spec.rb
@@ -0,0 +1,571 @@
+# frozen_string_literal: true
+
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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.
+# ++
+
+require "spec_helper"
+
+# This file can be safely deleted once the feature flag :percent_complete_edition
+# is removed, which should happen for OpenProject 15.0 release.
+# Copied from commit 109b135b
+RSpec.describe "Progress modal", :js, :with_cuprite,
+ with_flag: { percent_complete_edition: false } do
+ shared_let(:user) { create(:admin) }
+ shared_let(:role) { create(:project_role) }
+
+ shared_let(:type_task) { create(:type_task) }
+ shared_let(:project) { create(:project, types: [type_task]) }
+ shared_let(:priority) { create(:default_priority, name: "Normal") }
+ shared_let(:open_status_with_0p_done_ratio) do
+ create(:status, name: "open", default_done_ratio: 0)
+ end
+ shared_let(:in_progress_status_with_50p_done_ratio) do
+ create(:status, name: "in progress", default_done_ratio: 50)
+ end
+ shared_let(:complete_status_with_100p_done_ratio) do
+ create(:status, name: "complete", default_done_ratio: 100)
+ end
+
+ shared_let(:estimated_hours) { 10.0 }
+ shared_let(:remaining_hours) { 5.0 }
+ shared_let(:work_package) do
+ create(:work_package,
+ project:,
+ type: type_task,
+ status: open_status_with_0p_done_ratio) do |wp|
+ update_work_package_with(wp, estimated_hours:, remaining_hours:)
+ end
+ end
+
+ def update_work_package_with(work_package, attributes)
+ WorkPackages::UpdateService.new(model: work_package,
+ user:,
+ contract_class: WorkPackages::CreateContract)
+ .call(**attributes)
+ end
+
+ let(:progress_query) do
+ create(:query,
+ project:,
+ user:,
+ display_sums: false,
+ column_names: %i[id subject type status
+ estimated_hours remaining_hours done_ratio]) do |query|
+ create(:view_work_packages_table, query:)
+ end
+ end
+
+ let(:work_package_table) { Pages::WorkPackagesTable.new(project) }
+ let(:work_package_row) { work_package_table.work_package_container(work_package) }
+ let(:work_package_create_page) { Pages::FullWorkPackageCreate.new(project:) }
+
+ current_user { user }
+
+ describe "clicking on a field on the work package table" do
+ it "sets the cursor after the last character on the selected input field" do
+ work_package_table.visit_query(progress_query)
+ work_package_table.expect_work_package_listed(work_package)
+
+ work_edit_field = ProgressEditField.new(work_package_row, :estimatedTime)
+ modal = work_edit_field.activate!
+
+ modal.expect_cursor_at_end_of_input
+ end
+ end
+
+ describe "work based mode" do
+ shared_examples_for "opens the modal with its work field in focus" do
+ it "opens the modal with its work field in focus" do
+ work_package_table.visit_query(progress_query)
+ work_package_table.expect_work_package_listed(work_package)
+
+ work_edit_field = ProgressEditField.new(work_package_row, :estimatedTime)
+ modal = work_edit_field.activate!
+
+ modal.expect_modal_field_in_focus
+ end
+ end
+
+ describe "clicking on the work field on the work package table " \
+ "with no fields set" do
+ before do
+ update_work_package_with(work_package, estimated_hours: nil, remaining_hours: nil)
+ end
+
+ include_examples "opens the modal with its work field in focus"
+ end
+
+ describe "clicking on the work field on the work package table" \
+ "with all fields set" do
+ before do
+ update_work_package_with(work_package, estimated_hours: 25.0, remaining_hours: 15.0)
+ end
+
+ include_examples "opens the modal with its work field in focus"
+ end
+
+ describe "clicking on the remaining work field on the work package table " \
+ "with no fields set" do
+ before do
+ update_work_package_with(work_package, estimated_hours: nil, remaining_hours: nil)
+ end
+
+ it "opens the modal with work in focus and remaining work disabled" do
+ work_package_table.visit_query(progress_query)
+ work_package_table.expect_work_package_listed(work_package)
+
+ work_edit_field = ProgressEditField.new(work_package_row, :estimatedTime)
+ remaining_work_field = ProgressEditField.new(work_package_row, :remainingTime)
+
+ remaining_work_field.activate!
+
+ work_edit_field.expect_modal_field_in_focus
+ remaining_work_field.expect_modal_field_disabled
+ end
+ end
+
+ describe "clicking on the remaining work field on the work package table " \
+ "with all fields set" do
+ before do
+ update_work_package_with(work_package, estimated_hours: 20.0, remaining_hours: 15.0)
+ end
+
+ it "opens the modal with remaining work in focus" do
+ work_package_table.visit_query(progress_query)
+ work_package_table.expect_work_package_listed(work_package)
+
+ remaining_work_field = ProgressEditField.new(work_package_row, :remainingTime)
+
+ remaining_work_field.activate!
+
+ remaining_work_field.expect_modal_field_in_focus
+ end
+ end
+ end
+
+ describe "status based mode", with_settings: { work_package_done_ratio: "status" } do
+ describe "clicking on the work field in the work package table " \
+ "with no fields set" do
+ before { update_work_package_with(work_package, estimated_hours: nil, remaining_hours: nil) }
+
+ it "opens the modal with work in focus" do
+ work_package_table.visit_query(progress_query)
+ work_package_table.expect_work_package_listed(work_package)
+
+ work_edit_field = ProgressEditField.new(work_package_row, :estimatedTime)
+ modal = work_edit_field.activate!
+
+ modal.expect_modal_field_in_focus
+ end
+ end
+
+ describe "clicking on the work field in the work package table " \
+ "with all fields set" do
+ before { update_work_package_with(work_package, estimated_hours: 20.0, remaining_hours: 15.0) }
+
+ it "opens the modal with work in focus" do
+ work_package_table.visit_query(progress_query)
+ work_package_table.expect_work_package_listed(work_package)
+
+ work_edit_field = ProgressEditField.new(work_package_row, :estimatedTime)
+ modal = work_edit_field.activate!
+
+ modal.expect_modal_field_in_focus
+ end
+ end
+
+ describe "Remaining work field" do
+ it "is readonly" do
+ work_package_table.visit_query(progress_query)
+ work_package_table.expect_work_package_listed(work_package)
+
+ work_field = ProgressEditField.new(work_package_row, :estimatedTime)
+ remaining_work_field = ProgressEditField.new(work_package_row, :remainingTime)
+ work_field.activate!
+
+ remaining_work_field.expect_read_only_modal_field
+ end
+ end
+
+ describe "Status field" do
+ before { open_status_with_0p_done_ratio.update!(is_default: true) }
+
+ it "renders only assignable statuses as options" do
+ # Create a single valid transition from "open" to "in progress"
+ create(:workflow,
+ type_id: type_task.id,
+ old_status: open_status_with_0p_done_ratio,
+ new_status: in_progress_status_with_50p_done_ratio,
+ role:)
+
+ work_package_table.visit_query(progress_query)
+ work_package_table.expect_work_package_listed(work_package)
+
+ work_field = ProgressEditField.new(work_package_row, :estimatedTime)
+ modal_status_field = ProgressEditField.new(work_package_row, :statusWithinProgressModal)
+
+ work_field.activate!
+
+ # The only defined workflow is "open" to "in progress" so "complete" must
+ # not be listed as an available option
+ modal_status_field.expect_select_field_with_options("open (0%)", "in progress (50%)")
+ modal_status_field.expect_select_field_with_no_options("complete (100%)")
+
+ # Create another valid transition from "open" to "complete"
+ create(:workflow,
+ type_id: type_task.id,
+ old_status: open_status_with_0p_done_ratio,
+ new_status: complete_status_with_100p_done_ratio,
+ role:)
+
+ work_package_table.visit_query(progress_query)
+ work_package_table.expect_work_package_listed(work_package)
+
+ work_field = ProgressEditField.new(work_package_row, :estimatedTime)
+ modal_status_field = ProgressEditField.new(work_package_row, :statusWithinProgressModal)
+
+ work_field.activate!
+ modal_status_field.expect_select_field_with_options("open (0%)", "in progress (50%)", "complete (100%)")
+ end
+ end
+
+ context "when on a new work package form" do
+ specify "modal renders when no default status is set for new work packages" do
+ work_package_create_page.visit!
+
+ work_field = work_package_create_page.edit_field(:estimatedTime)
+ work_field.activate!
+ end
+
+ context "with a default status set for new work packages" do
+ before_all do
+ open_status_with_0p_done_ratio.update!(is_default: true)
+
+ create(:workflow,
+ type_id: type_task.id,
+ old_status: open_status_with_0p_done_ratio,
+ new_status: in_progress_status_with_50p_done_ratio,
+ role:)
+ create(:workflow,
+ type_id: type_task.id,
+ old_status: open_status_with_0p_done_ratio,
+ new_status: complete_status_with_100p_done_ratio,
+ role:)
+ end
+
+ it "can create work package after setting work" do
+ work_package_create_page.visit!
+
+ work_package_create_page.set_attributes({ subject: "hello" })
+ work_package_create_page.set_progress_attributes({ estimatedTime: "10h" })
+ work_package_create_page.save!
+ work_package_table.expect_and_dismiss_toaster(message: "Successful creation.", wait: 5)
+ end
+
+ it "renders the status selection field inside the modal as disabled " \
+ "and allows setting the status solely by the top-left field" do
+ work_package_create_page.visit!
+ work_package_create_page.expect_fully_loaded
+
+ work_field = work_package_create_page.edit_field(:estimatedTime)
+ modal_status_field = work_package_create_page.edit_field(:statusWithinProgressModal)
+
+ modal = work_field.activate!
+
+ modal_status_field.expect_modal_field_disabled
+ modal_status_field.expect_modal_field_value("open (0%)", disabled: true)
+
+ modal.close!
+
+ status_field = work_package_create_page.edit_field(:status)
+
+ status_field.update("in progress")
+
+ work_field.activate!
+ modal_status_field.expect_modal_field_value("in progress (50%)", disabled: true)
+ end
+
+ it "can open the modal, then save without modifying anything" do
+ work_package_create_page.visit!
+ work_package_create_page.set_attributes({ subject: "hello" })
+
+ work_field = work_package_create_page.edit_field(:estimatedTime)
+ work_field.activate!
+ work_field.submit_by_clicking_save
+ work_package_create_page.expect_no_toaster(type: "error")
+
+ work_package_create_page.save!
+ work_package_table.expect_and_dismiss_toaster(message: "Successful creation.")
+ end
+ end
+ end
+ end
+
+ describe "opening the progress modal" do
+ before_all do
+ create(:workflow,
+ type_id: type_task.id,
+ old_status: open_status_with_0p_done_ratio,
+ new_status: in_progress_status_with_50p_done_ratio,
+ role:)
+ create(:workflow,
+ type_id: type_task.id,
+ old_status: open_status_with_0p_done_ratio,
+ new_status: complete_status_with_100p_done_ratio,
+ role:)
+ end
+
+ describe "field value format" do
+ context "with all values set" do
+ before { update_work_package_with(work_package, estimated_hours: 10.0, remaining_hours: 2.12345) }
+
+ it "populates fields with correctly values formatted" do
+ work_package_table.visit_query(progress_query)
+ work_package_table.expect_work_package_listed(work_package)
+
+ work_edit_field = ProgressEditField.new(work_package_row, :estimatedTime)
+ remaining_work_edit_field = ProgressEditField.new(work_package_row, :remainingTime)
+ percent_complete_edit_field = ProgressEditField.new(work_package_row, :percentageDone)
+
+ work_edit_field.activate!
+
+ work_edit_field.expect_modal_field_value("10h")
+ remaining_work_edit_field.expect_modal_field_value("2.12h") # 2h 7m
+ percent_complete_edit_field.expect_modal_field_value("78", readonly: true)
+ end
+ end
+
+ context "with % complete set and setting long decimal values in modal" do
+ before do
+ work_package.attributes = {
+ estimated_hours: nil, remaining_hours: nil, done_ratio: 89
+ }
+ work_package.save(validate: false)
+ end
+
+ it "does not lose precision due to conversion from ISO duration to hours (rounded to closest minute)" do
+ work_package_table.visit_query(progress_query)
+ work_package_table.expect_work_package_listed(work_package)
+
+ work_edit_field = ProgressEditField.new(work_package_row, :estimatedTime)
+ remaining_work_edit_field = ProgressEditField.new(work_package_row, :remainingTime)
+
+ # set work to 2.5567
+ work_edit_field.activate!
+ work_edit_field.update("2.5567")
+ work_package_table.expect_and_dismiss_toaster(message: "Successful update.")
+
+ # work should have been set to 2.56 and remaining work to 0.28
+ work_package.reload
+ expect(work_package.estimated_hours).to eq(2.56)
+ expect(work_package.remaining_hours).to eq(0.28)
+
+ # work should be displayed as "2h 34m" ("2h 33m 36s" rounded to minutes),
+ # and remaining work as "17m" ("16m 48s" rounded to minutes)
+ work_edit_field.activate!
+ work_edit_field.expect_modal_field_value("2.56h") # 2h 34m
+ remaining_work_edit_field.expect_modal_field_value("0.28h") # 17m
+ end
+ end
+
+ context "with unset values" do
+ before { update_work_package_with(work_package, estimated_hours: nil, remaining_hours: nil) }
+
+ it "populates fields with blank values and % Complete as '-'" do
+ work_package_table.visit_query(progress_query)
+ work_package_table.expect_work_package_listed(work_package)
+
+ work_edit_field = ProgressEditField.new(work_package_row, :estimatedTime)
+ remaining_work_edit_field = ProgressEditField.new(work_package_row, :remainingTime)
+ percent_complete_edit_field = ProgressEditField.new(work_package_row, :percentageDone)
+
+ work_edit_field.activate!
+
+ work_edit_field.expect_modal_field_value("")
+ remaining_work_edit_field.expect_modal_field_value("", disabled: true)
+ percent_complete_edit_field.expect_modal_field_value("-", readonly: true)
+ end
+ end
+
+ describe "status field", with_settings: { work_package_done_ratio: "status" } do
+ it "renders the status options as the << status_name (percent_complete_value %) >>" do
+ work_package_table.visit_query(progress_query)
+ work_package_table.expect_work_package_listed(work_package)
+
+ work_field = ProgressEditField.new(work_package_row, :estimatedTime)
+ status_field = ProgressEditField.new(work_package_row, :statusWithinProgressModal)
+
+ work_field.activate!
+
+ status_field.expect_select_field_with_options("open (0%)",
+ "in progress (50%)",
+ "complete (100%)")
+ end
+ end
+ end
+
+ it "disables the field that triggered the modal" do
+ work_package_table.visit_query(progress_query)
+ work_package_table.expect_work_package_listed(work_package)
+
+ work_edit_field = ProgressEditField.new(work_package_row, :estimatedTime)
+
+ work_edit_field.activate!
+
+ work_edit_field.expect_trigger_field_disabled
+ end
+
+ it "allows clicking on a field other than the one that triggered the modal " \
+ "and opens the modal with said field selected" do
+ work_package_table.visit_query(progress_query)
+ work_package_table.expect_work_package_listed(work_package)
+
+ work_edit_field = ProgressEditField.new(work_package_row, :estimatedTime)
+ remaining_work_edit_field = ProgressEditField.new(work_package_row, :remainingTime)
+
+ remaining_work_edit_field.activate!
+ remaining_work_edit_field.expect_modal_field_in_focus
+ remaining_work_edit_field.expect_trigger_field_disabled
+
+ work_edit_field.reactivate!
+ work_edit_field.expect_modal_field_in_focus
+ work_edit_field.expect_trigger_field_disabled
+ end
+ end
+
+ describe "% Complete field" do
+ it "renders as readonly" do
+ work_package_table.visit_query(progress_query)
+ work_package_table.expect_work_package_listed(work_package)
+
+ work_edit_field = ProgressEditField.new(work_package_row, :estimatedTime)
+ percent_complete_edit_field = ProgressEditField.new(work_package_row, :percentageDone)
+
+ work_edit_field.activate!
+
+ percent_complete_edit_field.expect_read_only_modal_field
+ end
+ end
+
+ describe "When % Complete is set + work and remaining work are unset coming from a migration" do
+ before_all do
+ work_package.reload
+
+ work_package.estimated_hours = nil
+ work_package.remaining_hours = nil
+ work_package.done_ratio = 5.0
+ work_package.save!(validate: false)
+ end
+
+ shared_examples_for "migration warning" do |should_render: false|
+ it "renders a banner with a warning message" do
+ work_package_table.visit_query(progress_query)
+ work_package_table.expect_work_package_listed(work_package)
+
+ work_edit_field = ProgressEditField.new(work_package_row, :estimatedTime)
+ modal = work_edit_field.activate!
+
+ modal.expect_migration_warning_banner(should_render:)
+ end
+ end
+
+ context "on work based mode" do
+ include_examples "migration warning", should_render: true
+ end
+
+ context "on status based mode", with_settings: { work_package_done_ratio: "status" } do
+ include_examples "migration warning", should_render: false
+ end
+ end
+
+ describe "Live-update edge cases" do
+ context "given work = 10h, remaining work = 4h, % complete = 60%" do
+ before { update_work_package_with(work_package, estimated_hours: 10.0, remaining_hours: 4.0) }
+
+ specify "Case 1: When I unset work it unsets remaining work" do
+ work_package_table.visit_query(progress_query)
+ work_package_table.expect_work_package_listed(work_package)
+
+ work_edit_field = ProgressEditField.new(work_package_row, :estimatedTime)
+ remaining_work_edit_field = ProgressEditField.new(work_package_row, :remainingTime)
+
+ work_edit_field.activate!
+ page.driver.wait_for_network_idle # Wait for initial loading to be ready
+
+ clear_input_field_contents(work_edit_field.input_element)
+ page.driver.wait_for_network_idle # Wait for live-update to finish
+
+ remaining_work_edit_field.expect_modal_field_value("", disabled: true)
+ end
+
+ specify "Case 2: when work is set to 12h, " \
+ "remaining work is automatically set to 6h " \
+ "and subsequently work is set to 14h, " \
+ "remaining work updates to 1d" do
+ work_package_table.visit_query(progress_query)
+ work_package_table.expect_work_package_listed(work_package)
+
+ work_edit_field = ProgressEditField.new(work_package_row, :estimatedTime)
+ remaining_work_edit_field = ProgressEditField.new(work_package_row, :remainingTime)
+
+ work_edit_field.activate!
+ page.driver.wait_for_network_idle # Wait for initial loading to be ready
+
+ work_edit_field.set_value("12")
+ page.driver.wait_for_network_idle # Wait for live-update to finish
+ remaining_work_edit_field.expect_modal_field_value("6h")
+
+ work_edit_field.set_value("14")
+ page.driver.wait_for_network_idle # Wait for live-update to finish
+ remaining_work_edit_field.expect_modal_field_value("8h")
+ end
+
+ specify "Case 3: when work is set to 2h, " \
+ "remaining work is automatically set to 0h, " \
+ "and work is subsequently set to 12h, " \
+ "remaining work is updated to 6h" do
+ work_package_table.visit_query(progress_query)
+ work_package_table.expect_work_package_listed(work_package)
+
+ work_edit_field = ProgressEditField.new(work_package_row, :estimatedTime)
+ remaining_work_edit_field = ProgressEditField.new(work_package_row, :remainingTime)
+
+ work_edit_field.activate!
+ page.driver.wait_for_network_idle # Wait for initial loading to be ready
+
+ work_edit_field.set_value("2")
+ page.driver.wait_for_network_idle # Wait for live-update to finish
+ remaining_work_edit_field.expect_modal_field_value("0h")
+
+ work_edit_field.set_value("12")
+ page.driver.wait_for_network_idle # Wait for live-update to finish
+ remaining_work_edit_field.expect_modal_field_value("6h")
+ end
+ end
+ end
+end
diff --git a/spec/features/work_packages/progress_modal_spec.rb b/spec/features/work_packages/progress_modal_spec.rb
index e250d410517..431afa4da63 100644
--- a/spec/features/work_packages/progress_modal_spec.rb
+++ b/spec/features/work_packages/progress_modal_spec.rb
@@ -30,7 +30,8 @@
require "spec_helper"
-RSpec.describe "Progress modal", :js, :with_cuprite do
+RSpec.describe "Progress modal", :js, :with_cuprite,
+ with_flag: { percent_complete_edition: true } do
shared_let(:user) { create(:admin) }
shared_let(:role) { create(:project_role) }
diff --git a/spec/lib/api/v3/work_packages/schema/specific_work_package_schema_pre_14_4_without_percent_complete_edition_spec.rb b/spec/lib/api/v3/work_packages/schema/specific_work_package_schema_pre_14_4_without_percent_complete_edition_spec.rb
new file mode 100644
index 00000000000..af6ba47206d
--- /dev/null
+++ b/spec/lib/api/v3/work_packages/schema/specific_work_package_schema_pre_14_4_without_percent_complete_edition_spec.rb
@@ -0,0 +1,64 @@
+#-- 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.
+#++
+
+require "spec_helper"
+
+# This file can be safely deleted once the feature flag :percent_complete_edition
+# is removed, which should happen for OpenProject 15.0 release.
+RSpec.describe API::V3::WorkPackages::Schema::SpecificWorkPackageSchema,
+ with_flag: { percent_complete_edition: false } do
+ let(:project) { build_stubbed(:project) }
+ let(:type) { build_stubbed(:type) }
+ let(:work_package) do
+ build_stubbed(:work_package,
+ project:,
+ type:)
+ end
+ let(:current_user) { build_stubbed(:user) }
+
+ before do
+ mock_permissions_for(current_user, &:allow_everything)
+ login_as(current_user)
+ end
+
+ subject { described_class.new(work_package:) }
+
+ describe "#writable?" do
+ describe "% Complete" do
+ it "is not writable in work-based progress calculation mode",
+ with_settings: { work_package_done_ratio: "field" } do
+ expect(subject).not_to be_writable(:done_ratio)
+ end
+
+ it "is not writable in status-based progress calculation mode",
+ with_settings: { work_package_done_ratio: "status" } do
+ expect(subject).not_to be_writable(:done_ratio)
+ end
+ end
+ end
+end
diff --git a/spec/lib/api/v3/work_packages/schema/specific_work_package_schema_spec.rb b/spec/lib/api/v3/work_packages/schema/specific_work_package_schema_spec.rb
index 4ff921618e2..f0e61f975f5 100644
--- a/spec/lib/api/v3/work_packages/schema/specific_work_package_schema_spec.rb
+++ b/spec/lib/api/v3/work_packages/schema/specific_work_package_schema_spec.rb
@@ -28,7 +28,8 @@
require "spec_helper"
-RSpec.describe API::V3::WorkPackages::Schema::SpecificWorkPackageSchema do
+RSpec.describe API::V3::WorkPackages::Schema::SpecificWorkPackageSchema,
+ with_flag: { percent_complete_edition: true } do
let(:project) { build_stubbed(:project) }
let(:type) { build_stubbed(:type) }
let(:work_package) do
diff --git a/spec/lib/api/v3/work_packages/schema/typed_work_package_schema_pre_14_4_without_percent_complete_edition_spec.rb b/spec/lib/api/v3/work_packages/schema/typed_work_package_schema_pre_14_4_without_percent_complete_edition_spec.rb
new file mode 100644
index 00000000000..ee8689d7916
--- /dev/null
+++ b/spec/lib/api/v3/work_packages/schema/typed_work_package_schema_pre_14_4_without_percent_complete_edition_spec.rb
@@ -0,0 +1,58 @@
+#-- 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.
+#++
+
+require "spec_helper"
+
+# This file can be safely deleted once the feature flag :percent_complete_edition
+# is removed, which should happen for OpenProject 15.0 release.
+RSpec.describe API::V3::WorkPackages::Schema::TypedWorkPackageSchema,
+ with_flag: { percent_complete_edition: false } do
+ let(:project) { build(:project) }
+ let(:type) { build(:type) }
+
+ let(:current_user) { build_stubbed(:user) }
+
+ subject { described_class.new(project:, type:) }
+
+ before do
+ login_as(current_user)
+ mock_permissions_for(current_user, &:allow_everything)
+ end
+
+ describe "#writable?" do
+ it "percentage done is not writable in work-based progress calculation mode",
+ with_settings: { work_package_done_ratio: "field" } do
+ expect(subject).not_to be_writable(:done_ratio)
+ end
+
+ it "percentage done is not writable in status-based progress calculation mode",
+ with_settings: { work_package_done_ratio: "status" } do
+ expect(subject).not_to be_writable(:done_ratio)
+ end
+ end
+end
diff --git a/spec/lib/api/v3/work_packages/schema/typed_work_package_schema_spec.rb b/spec/lib/api/v3/work_packages/schema/typed_work_package_schema_spec.rb
index 702cb4451a1..f9aca4ef160 100644
--- a/spec/lib/api/v3/work_packages/schema/typed_work_package_schema_spec.rb
+++ b/spec/lib/api/v3/work_packages/schema/typed_work_package_schema_spec.rb
@@ -28,7 +28,8 @@
require "spec_helper"
-RSpec.describe API::V3::WorkPackages::Schema::TypedWorkPackageSchema do
+RSpec.describe API::V3::WorkPackages::Schema::TypedWorkPackageSchema,
+ with_flag: { percent_complete_edition: true } do
let(:project) { build(:project) }
let(:type) { build(:type) }
diff --git a/spec/lib/api/v3/work_packages/schema/work_package_schema_representer_pre_14_4_without_percent_complete_edition_spec.rb b/spec/lib/api/v3/work_packages/schema/work_package_schema_representer_pre_14_4_without_percent_complete_edition_spec.rb
new file mode 100644
index 00000000000..e4158314c03
--- /dev/null
+++ b/spec/lib/api/v3/work_packages/schema/work_package_schema_representer_pre_14_4_without_percent_complete_edition_spec.rb
@@ -0,0 +1,113 @@
+#-- 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.
+#++
+
+require "spec_helper"
+
+# This file can be safely deleted once the feature flag :percent_complete_edition
+# is removed, which should happen for OpenProject 15.0 release.
+RSpec.describe API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter,
+ with_flag: { percent_complete_edition: false } do
+ include API::V3::Utilities::PathHelper
+
+ let(:project) { build_stubbed(:project_with_types) }
+ let(:permissions) { [:edit_work_packages] }
+ let(:attribute_query) do
+ build_stubbed(:query).tap do |query|
+ query.filters.clear
+ query.add_filter("parent", "=", ["{id}"])
+ end
+ end
+ let(:attribute_groups) do
+ [Type::AttributeGroup.new(wp_type, "People", %w(assignee responsible)),
+ Type::AttributeGroup.new(wp_type, "Estimates and time", %w(estimated_time spent_time)),
+ Type::QueryGroup.new(wp_type, "Children", attribute_query)]
+ end
+ let(:schema) do
+ API::V3::WorkPackages::Schema::SpecificWorkPackageSchema.new(work_package:).tap do |schema|
+ allow(wp_type)
+ .to receive(:attribute_groups)
+ .and_return(attribute_groups)
+ allow(schema)
+ .to receive(:assignable_values)
+ .and_call_original
+ allow(schema)
+ .to receive(:assignable_values)
+ .with(:version, current_user)
+ .and_return([])
+ end
+ end
+ let(:self_link) { "/a/self/link" }
+ let(:base_schema_link) { nil }
+ let(:hide_self_link) { false }
+ let(:embedded) { true }
+ let(:representer) do
+ described_class.create(schema,
+ self_link:,
+ form_embedded: embedded,
+ base_schema_link:,
+ current_user:)
+ end
+ let(:available_custom_fields) { [] }
+ let(:wp_type) { project.types.first }
+ let(:custom_field) { build_stubbed(:custom_field) }
+ let(:work_package) do
+ build_stubbed(:work_package, project:, type: wp_type) do |wp|
+ allow(wp)
+ .to receive(:available_custom_fields)
+ .and_return(available_custom_fields)
+ end
+ end
+ let(:current_user) { build_stubbed(:user) }
+
+ before do
+ mock_permissions_for(current_user) do |mock|
+ mock.allow_in_project *permissions, project: schema.project
+ end
+
+ login_as(current_user)
+ allow(schema.project)
+ .to receive(:module_enabled?)
+ .and_return(true)
+
+ allow(schema).to receive(:writable?).and_call_original
+ end
+
+ context "for generation" do
+ subject(:generated) { representer.to_json }
+
+ describe "percentageDone" do
+ it_behaves_like "has basic schema properties" do
+ let(:path) { "percentageDone" }
+ let(:type) { "Integer" }
+ let(:name) { I18n.t("activerecord.attributes.work_package.done_ratio") }
+ let(:required) { false }
+ let(:writable) { false }
+ end
+ end
+ end
+end
diff --git a/spec/lib/api/v3/work_packages/schema/work_package_schema_representer_spec.rb b/spec/lib/api/v3/work_packages/schema/work_package_schema_representer_spec.rb
index d2edd5d22fa..5e1c9b5378a 100644
--- a/spec/lib/api/v3/work_packages/schema/work_package_schema_representer_spec.rb
+++ b/spec/lib/api/v3/work_packages/schema/work_package_schema_representer_spec.rb
@@ -28,7 +28,8 @@
require "spec_helper"
-RSpec.describe API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
+RSpec.describe API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter,
+ with_flag: { percent_complete_edition: true } do
include API::V3::Utilities::PathHelper
let(:project) { build_stubbed(:project_with_types) }
diff --git a/spec/services/work_packages/set_attributes_service_pre_14_4_without_percent_complete_edition_spec.rb b/spec/services/work_packages/set_attributes_service_pre_14_4_without_percent_complete_edition_spec.rb
new file mode 100644
index 00000000000..677168a8e1d
--- /dev/null
+++ b/spec/services/work_packages/set_attributes_service_pre_14_4_without_percent_complete_edition_spec.rb
@@ -0,0 +1,648 @@
+#-- 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.
+#++
+
+require "spec_helper"
+
+# This file can be safely deleted once the feature flag percent_complete_edition
+# is removed, which should happen for OpenProject 15.0 release.
+RSpec.describe WorkPackages::SetAttributesService, "pre 14.4 without percent complete edition",
+ type: :model,
+ with_flag: { percent_complete_edition: false } do
+ shared_let(:status_0_pct_complete) { create(:status, default_done_ratio: 0, name: "0% complete") }
+ shared_let(:status_50_pct_complete) { create(:status, default_done_ratio: 50, name: "50% complete") }
+ shared_let(:status_70_pct_complete) { create(:status, default_done_ratio: 70, name: "70% complete") }
+
+ let(:user) { build_stubbed(:user) }
+ let(:project) do
+ p = build_stubbed(:project)
+ allow(p).to receive(:shared_versions).and_return([])
+
+ p
+ end
+ let(:work_package) do
+ build_stubbed(:work_package, project:, status: status_0_pct_complete)
+ end
+ let(:statuses) { [] }
+ let(:contract_class) { WorkPackages::UpdateContract }
+ let(:mock_contract) do
+ class_double(contract_class,
+ new: mock_contract_instance)
+ end
+ let(:mock_contract_instance) do
+ instance_double(contract_class,
+ assignable_statuses: statuses,
+ errors: contract_errors,
+ validate: contract_valid)
+ end
+ let(:contract_valid) { true }
+ let(:contract_errors) do
+ instance_double(ActiveModel::Errors)
+ end
+ let(:instance) do
+ described_class.new(user:,
+ model: work_package,
+ contract_class: mock_contract)
+ end
+
+ shared_examples_for "service call" do |description: nil|
+ subject do
+ allow(work_package)
+ .to receive(:save)
+
+ instance.call(call_attributes)
+ end
+
+ it description || "sets the value" do
+ all_expected_attributes = {}
+ all_expected_attributes.merge!(expected_attributes) if defined?(expected_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(subject).to be_success
+ expect(work_package).to have_attributes(all_expected_attributes)
+ # work package is not saved and no errors are created by the service
+ # (that's contract's responsibility and it is mocked in this test)
+ expect(work_package).not_to have_received(:save)
+ expect(subject.errors).to be_empty
+ end
+ end
+
+ context "when the contract does not validate" do
+ let(:contract_valid) { false }
+
+ it "is unsuccessful, does not persist the changes and exposes the contract's errors", :aggregate_failures do
+ expect(subject).not_to be_success
+ expect(work_package).not_to have_received(:save)
+ expect(subject.errors).to eql mock_contract_instance.errors
+ end
+ end
+ end
+
+ # Scenarios specified in https://community.openproject.org/wp/40749
+ describe "deriving remaining work attribute (remaining_hours)" do
+ context "in status-based mode",
+ with_settings: { work_package_done_ratio: "status" } do
+ context "given a work package with work, remaining work, and status with % complete being set" do
+ before do
+ work_package.status = status_50_pct_complete
+ work_package.done_ratio = work_package.status.default_done_ratio
+ work_package.estimated_hours = 10.0
+ work_package.remaining_hours = 5.0
+ work_package.send(:clear_changes_information)
+ end
+
+ context "when work is unset" do
+ let(:call_attributes) { { estimated_hours: nil } }
+ let(:expected_attributes) { { remaining_hours: nil } }
+
+ it_behaves_like "service call", description: "unsets remaining work"
+ end
+
+ context "when work is changed" do
+ let(:call_attributes) { { estimated_hours: 5.0 } }
+ let(:expected_attributes) { { remaining_hours: 2.5 } }
+
+ it_behaves_like "service call", description: "recomputes remaining work accordingly"
+ end
+
+ context "when work is changed to a negative value" do
+ let(:call_attributes) { { estimated_hours: -1.0 } }
+ let(:expected_kept_attributes) { %w[remaining_hours] }
+
+ it_behaves_like "service call",
+ description: "is an error state (to be detected by contract), and remaining work is kept"
+ end
+
+ context "when another status is set" do
+ let(:call_attributes) { { status: status_70_pct_complete } }
+ let(:expected_attributes) { { remaining_hours: 3.0 } }
+
+ it_behaves_like "service call",
+ description: "recomputes remaining work according to the % complete value of the new status"
+ end
+
+ context "when floating point operations are inaccurate (2.4000000000000004h)" do
+ let(:call_attributes) { { estimated_hours: 8.0, status: status_70_pct_complete } }
+ let(:expected_attributes) { { remaining_hours: 2.4 } } # would be 2.4000000000000004 without rounding
+
+ it_behaves_like "service call", description: "remaining work is computed and rounded (2.4)"
+ end
+ end
+
+ context "given a work package with work and remaining work unset, and a status with 0% complete" do
+ before do
+ work_package.status = status_0_pct_complete
+ work_package.done_ratio = work_package.status.default_done_ratio
+ work_package.estimated_hours = nil
+ work_package.remaining_hours = nil
+ work_package.send(:clear_changes_information)
+ end
+
+ context "when another status with another % complete value is set" do
+ let(:call_attributes) { { status: status_70_pct_complete } }
+ let(:expected_attributes) { { remaining_hours: nil } }
+
+ it_behaves_like "service call",
+ description: "remaining work remains unset"
+ end
+
+ context "when work is set" do
+ let(:call_attributes) { { estimated_hours: 10.0 } }
+ let(:expected_attributes) { { remaining_hours: 10.0 } }
+
+ it_behaves_like "service call",
+ description: "remaining work is updated accordingly from work and % complete value of the status"
+ end
+
+ context "when work is set to a negative value" do
+ let(:call_attributes) { { estimated_hours: -1.0 } }
+ let(:expected_kept_attributes) { %w[remaining_hours] }
+
+ it_behaves_like "service call",
+ description: "is an error state (to be detected by contract), and remaining work is kept"
+ end
+
+ context "when work is set with 2nd decimal rounding up" do
+ let(:call_attributes) { { estimated_hours: 3.567 } }
+ let(:expected_attributes) { { estimated_hours: 3.57, remaining_hours: 3.57 } }
+
+ it_behaves_like "service call",
+ description: "values are rounded up to 2 decimals and set to the same value"
+ end
+ end
+ end
+ end
+
+ # Scenarios specified in https://community.openproject.org/wp/40749
+ describe "deriving % complete attribute (done_ratio)" do
+ context "in status-based mode",
+ with_settings: { work_package_done_ratio: "status" } do
+ context "given a work package with a status with 50% complete" do
+ before do
+ work_package.status = status_50_pct_complete
+ work_package.done_ratio = work_package.status.default_done_ratio
+ work_package.send(:clear_changes_information)
+ end
+
+ context "when another status with another % complete value is set" do
+ let(:call_attributes) { { status: status_70_pct_complete } }
+ let(:expected_attributes) { { done_ratio: 70 } }
+
+ it_behaves_like "service call", description: "sets the % complete value to the status default % complete value"
+ end
+ end
+ end
+
+ context "in work-based mode",
+ with_settings: { work_package_done_ratio: "field" } do
+ context "given a work package with work, remaining work, and % complete being set" do
+ before do
+ work_package.estimated_hours = 10.0
+ work_package.remaining_hours = 3.0
+ work_package.done_ratio = 70
+ work_package.send(:clear_changes_information)
+ end
+
+ context "when work is unset" do
+ let(:call_attributes) { { estimated_hours: nil } }
+ let(:expected_attributes) { { remaining_hours: nil, done_ratio: nil } }
+
+ it_behaves_like "service call", description: "unsets remaining work and % complete"
+ end
+
+ context "when remaining work is unset" do
+ let(:call_attributes) { { remaining_hours: nil } }
+ let(:expected_attributes) { { estimated_hours: 10.0, done_ratio: nil } }
+
+ it_behaves_like "service call", description: "keeps work, and unsets % complete"
+ end
+
+ context "when both work and remaining work are unset" do
+ let(:call_attributes) { { estimated_hours: nil, remaining_hours: nil } }
+ let(:expected_attributes) { { done_ratio: nil } }
+
+ it_behaves_like "service call", description: "unsets % complete"
+ end
+
+ context "when work is increased" do
+ # work changed by +10h
+ let(:call_attributes) { { estimated_hours: 10.0 + 10.0 } }
+ let(:expected_attributes) do
+ { remaining_hours: 3.0 + 10.0, done_ratio: 35 }
+ end
+
+ it_behaves_like "service call",
+ description: "remaining work is increased by the same amount, and % complete is updated accordingly"
+ end
+
+ context "when work is set to 0h" do
+ let(:call_attributes) { { estimated_hours: 0 } }
+ let(:expected_attributes) do
+ { remaining_hours: 0, done_ratio: nil }
+ end
+
+ it_behaves_like "service call",
+ description: "remaining work is set to 0h and % Complete is unset"
+ end
+
+ context "when work is decreased" do
+ # work changed by -2h
+ let(:call_attributes) { { estimated_hours: 10.0 - 2.0 } }
+ let(:expected_attributes) do
+ { remaining_hours: 3.0 - 2.0, done_ratio: 87 }
+ end
+
+ it_behaves_like "service call",
+ description: "remaining work is decreased by the same amount, and % complete is updated accordingly"
+ end
+
+ context "when work is decreased below remaining work value" do
+ # work changed by -8h
+ let(:call_attributes) { { estimated_hours: 10.0 - 8.0 } }
+ let(:expected_attributes) do
+ { remaining_hours: 0, done_ratio: 100 }
+ end
+
+ it_behaves_like "service call",
+ description: "remaining work becomes 0h, and % complete becomes 100%"
+ end
+
+ context "when work is changed to a negative value" do
+ let(:call_attributes) { { estimated_hours: -1.0 } }
+ let(:expected_kept_attributes) { %w[remaining_hours done_ratio] }
+
+ it_behaves_like "service call",
+ description: "is an error state (to be detected by contract), " \
+ "and % complete and remaining work are kept"
+ end
+
+ context "when remaining work is changed" do
+ let(:call_attributes) { { remaining_hours: 2 } }
+ let(:expected_attributes) { { done_ratio: 80 } }
+ let(:expected_kept_attributes) { %w[estimated_hours] }
+
+ it_behaves_like "service call", description: "updates % complete accordingly"
+ end
+
+ context "when work and remaining work are both changed to negative values" do
+ let(:call_attributes) { { estimated_hours: -10, remaining_hours: -5 } }
+ let(:expected_kept_attributes) { %w[done_ratio] }
+
+ it_behaves_like "service call", description: "is an error state (to be detected by contract), and % Complete is kept"
+ end
+
+ context "when work and remaining work are both changed to values with more than 2 decimals" do
+ let(:call_attributes) { { estimated_hours: 10.123456, remaining_hours: 5.6789 } }
+ let(:expected_attributes) { { estimated_hours: 10.12, remaining_hours: 5.68, done_ratio: 43 } }
+
+ it_behaves_like "service call", description: "rounds work and remaining work to 2 decimals " \
+ "and updates % complete accordingly"
+ end
+
+ context "when remaining work is changed to a value greater than work" do
+ let(:call_attributes) { { remaining_hours: 200.0 } }
+ let(:expected_kept_attributes) { %w[done_ratio] }
+
+ it_behaves_like "service call", description: "is an error state (to be detected by contract), and % Complete is kept"
+ end
+
+ context "when remaining work is changed to a negative value" do
+ let(:call_attributes) { { remaining_hours: -1.0 } }
+ let(:expected_kept_attributes) { %w[done_ratio] }
+
+ it_behaves_like "service call", description: "is an error state (to be detected by contract), and % Complete is kept"
+ end
+
+ context "when both work and remaining work are changed" do
+ let(:call_attributes) { { estimated_hours: 20, remaining_hours: 2 } }
+ let(:expected_attributes) { call_attributes.merge(done_ratio: 90) }
+
+ it_behaves_like "service call", description: "updates % complete accordingly"
+ end
+
+ context "when work is changed and remaining work is unset" do
+ let(:call_attributes) { { estimated_hours: 8.0, remaining_hours: nil } }
+ let(:expected_attributes) { call_attributes.dup }
+ let(:expected_kept_attributes) { %w[done_ratio] }
+
+ it_behaves_like "service call",
+ description: "% complete is kept and remaining work is kept unset and not recomputed" \
+ "(error state to be detected by contract)"
+ end
+
+ context "when work is increased and remaining work is set to its current value (to prevent it from being increased)" do
+ # work changed by +10h
+ let(:call_attributes) { { estimated_hours: 10.0 + 10.0, remaining_hours: 3 } }
+ let(:expected_attributes) { { remaining_hours: 3.0, done_ratio: 85 } }
+
+ it_behaves_like "service call",
+ description: "remaining work is kept (not increased), and % complete is updated accordingly"
+ end
+ end
+
+ context "given a work package with work and % complete being set, and remaining work being unset" do
+ before do
+ work_package.estimated_hours = 10
+ work_package.remaining_hours = nil
+ work_package.done_ratio = 30
+ work_package.send(:clear_changes_information)
+ end
+
+ context "when work is changed" do
+ let(:call_attributes) { { estimated_hours: 20.0 } }
+ let(:expected_attributes) { { remaining_hours: 14.0 } }
+ let(:expected_kept_attributes) { %w[done_ratio] }
+
+ it_behaves_like "service call", description: "% complete is kept and remaining work is updated accordingly"
+ end
+
+ context "when work is changed to a negative value" do
+ let(:call_attributes) { { estimated_hours: -1.0 } }
+ let(:expected_kept_attributes) { %w[remaining_hours done_ratio] }
+
+ it_behaves_like "service call",
+ description: "is an error state (to be detected by contract), " \
+ "and % complete and remaining work are kept"
+ end
+
+ context "when remaining work is set" do
+ let(:call_attributes) { { remaining_hours: 1.0 } }
+ let(:expected_attributes) { call_attributes.merge(done_ratio: 90.0) }
+ let(:expected_kept_attributes) { %w[estimated_hours] }
+
+ it_behaves_like "service call", description: "work is kept and % complete is updated accordingly"
+ end
+ end
+
+ context "given a work package with remaining work and % complete being set, and work being unset" do
+ before do
+ work_package.estimated_hours = nil
+ work_package.remaining_hours = 2.0
+ work_package.done_ratio = 50
+ work_package.send(:clear_changes_information)
+ end
+
+ context "when remaining work is changed" do
+ let(:call_attributes) { { remaining_hours: 10.0 } }
+ let(:expected_attributes) { call_attributes.merge(estimated_hours: 20.0) }
+ let(:expected_kept_attributes) { %w[done_ratio] }
+
+ it_behaves_like "service call", description: "% complete is kept and work is updated accordingly"
+ end
+
+ context "when % complete is 0% and remaining work is changed to a decimal rounded up" do
+ let(:call_attributes) { { remaining_hours: 5.679 } }
+ let(:expected_attributes) { { estimated_hours: 5.68, remaining_hours: 5.68 } }
+ let(:expected_kept_attributes) { %w[done_ratio] }
+
+ before do
+ work_package.done_ratio = 0
+ work_package.send(:clear_changes_information)
+ end
+
+ it_behaves_like "service call",
+ description: "% complete is kept, values are rounded, and work is updated accordingly"
+ end
+
+ context "when work is set" do
+ let(:call_attributes) { { estimated_hours: 10.0 } }
+ let(:expected_attributes) { call_attributes.merge(done_ratio: 80.0) }
+ let(:expected_kept_attributes) { %w[remaining_hours] }
+
+ it_behaves_like "service call", description: "remaining work is kept and % complete is updated accordingly"
+ end
+ end
+
+ context "given a work package with work being set, and remaining work and % complete being unset" do
+ before do
+ work_package.estimated_hours = 10
+ work_package.remaining_hours = nil
+ work_package.done_ratio = nil
+ work_package.send(:clear_changes_information)
+ end
+
+ context "when work is changed" do
+ let(:call_attributes) { { estimated_hours: 20.0 } }
+ let(:expected_attributes) { { remaining_hours: 20.0, done_ratio: 0 } }
+
+ it_behaves_like "service call", description: "remaining work is set to the same value and % complete is set to 0%"
+ end
+
+ context "when work is changed and remaining work is unset" do
+ let(:call_attributes) { { estimated_hours: 10.0, remaining_hours: nil } }
+ let(:expected_attributes) { call_attributes.dup }
+ let(:expected_kept_attributes) { %w[done_ratio] }
+
+ it_behaves_like "service call",
+ description: "% complete is kept and remaining work is kept unset and not recomputed" \
+ "(error state to be detected by contract)"
+ end
+
+ context "when work is changed to a negative value" do
+ let(:call_attributes) { { estimated_hours: -1.0 } }
+ let(:expected_kept_attributes) { %w[remaining_hours done_ratio] }
+
+ it_behaves_like "service call",
+ description: "is an error state (to be detected by contract), " \
+ "and % complete and remaining work are kept"
+ end
+ end
+
+ context "given a work package with remaining work being set, and work and % complete being unset" do
+ before do
+ work_package.estimated_hours = nil
+ work_package.remaining_hours = 6.0
+ work_package.done_ratio = nil
+ work_package.send(:clear_changes_information)
+ end
+
+ context "when work is set" do
+ let(:call_attributes) { { estimated_hours: 10.0 } }
+ let(:expected_attributes) { { done_ratio: 40 } }
+ let(:expected_kept_attributes) { %w[remaining_hours] }
+
+ it_behaves_like "service call",
+ description: "remaining work is kept to the same value and % complete is updated accordingly"
+ end
+
+ context "when work is changed to a negative value" do
+ let(:call_attributes) { { estimated_hours: -1.0 } }
+ let(:expected_kept_attributes) { %w[remaining_hours done_ratio] }
+
+ it_behaves_like "service call",
+ description: "is an error state (to be detected by contract), " \
+ "and % complete and remaining work are kept"
+ end
+
+ context "when remaining work is changed" do
+ let(:call_attributes) { { remaining_hours: 12.0 } }
+ let(:expected_attributes) { { estimated_hours: 12.0, done_ratio: 0 } }
+
+ it_behaves_like "service call",
+ description: "work is set to the same value and % complete is set to 0%"
+ end
+
+ context "when remaining work is changed to a negative value" do
+ let(:call_attributes) { { remaining_hours: -1.0 } }
+ let(:expected_kept_attributes) { %w[estimated_hours done_ratio] }
+
+ it_behaves_like "service call",
+ description: "is an error state (to be detected by contract), " \
+ "and % complete and work are kept"
+ end
+ end
+
+ context "given a work package with work and remaining work unset, and % complete being set" do
+ before do
+ work_package.estimated_hours = nil
+ work_package.remaining_hours = nil
+ work_package.done_ratio = 60
+ work_package.send(:clear_changes_information)
+ end
+
+ context "when work is set" do
+ let(:call_attributes) { { estimated_hours: 10.0 } }
+ let(:expected_attributes) { { remaining_hours: 4.0 } }
+ let(:expected_kept_attributes) { %w[done_ratio] }
+
+ it_behaves_like "service call", description: "% complete is kept and remaining work is updated accordingly"
+ end
+
+ context "when work is set to a number with with 4 decimals" do
+ let(:call_attributes) { { estimated_hours: 2.5678 } }
+ let(:expected_attributes) { { estimated_hours: 2.57, remaining_hours: 1.03 } }
+ let(:expected_kept_attributes) { %w[done_ratio] }
+
+ it_behaves_like "service call",
+ description: "% complete is kept, work is rounded to 2 decimals, " \
+ "and remaining work is updated and rounded to 2 decimals"
+ end
+
+ context "when work is set to a string" do
+ let(:call_attributes) { { estimated_hours: "I am a string" } }
+ let(:expected_attributes) { { estimated_hours: 0.0, remaining_hours: 0.0 } }
+
+ it "keeps the original string value in the _before_type_cast method " \
+ "so that validation can detect it is invalid" do
+ allow(work_package).to receive(:save)
+ instance.call(call_attributes)
+
+ expect(work_package.estimated_hours_before_type_cast).to eq("I am a string")
+ end
+ end
+
+ context "when work and remaining work are set" do
+ let(:call_attributes) { { estimated_hours: 10.0, remaining_hours: 0 } }
+ let(:expected_attributes) { call_attributes.merge(done_ratio: 100) }
+
+ it_behaves_like "service call", description: "% complete is updated accordingly"
+ end
+
+ context "when work is set and remaining work is unset" do
+ let(:call_attributes) { { estimated_hours: 10.0, remaining_hours: nil } }
+ let(:expected_attributes) { call_attributes.dup }
+ let(:expected_kept_attributes) { %w[done_ratio] }
+
+ it_behaves_like "service call",
+ description: "% complete is kept and remaining work is kept unset and not recomputed" \
+ "(error state to be detected by contract)"
+ end
+
+ context "when work and remaining work are both set to negative values" do
+ let(:call_attributes) { { estimated_hours: -10, remaining_hours: -5 } }
+ let(:expected_kept_attributes) { %w[done_ratio] }
+
+ it_behaves_like "service call", description: "is an error state (to be detected by contract), and % Complete is kept"
+ end
+ end
+
+ context "given a work package with work, remaining work, and % complete being unset" do
+ before do
+ work_package.estimated_hours = nil
+ work_package.remaining_hours = nil
+ work_package.done_ratio = nil
+ work_package.send(:clear_changes_information)
+ end
+
+ context "when work is set" do
+ let(:call_attributes) { { estimated_hours: 10.0 } }
+ let(:expected_attributes) do
+ { remaining_hours: 10.0, done_ratio: 0 }
+ end
+
+ it_behaves_like "service call", description: "remaining work is set to the same value and % complete is set to 0%"
+ end
+
+ context "when work is set to a negative value" do
+ let(:call_attributes) { { estimated_hours: -1.0 } }
+ let(:expected_kept_attributes) { %w[remaining_hours done_ratio] }
+
+ it_behaves_like "service call",
+ description: "is an error state (to be detected by contract), " \
+ "and % complete and remaining work are kept"
+ end
+
+ context "when remaining work is set" do
+ let(:call_attributes) { { remaining_hours: 10.0 } }
+ let(:expected_attributes) { { estimated_hours: 10.0, done_ratio: 0 } }
+
+ it_behaves_like "service call", description: "work is set to the same value and % complete is set to 0%"
+ end
+
+ context "when remaining work is set to a negative value" do
+ let(:call_attributes) { { remaining_hours: -1.0 } }
+ let(:expected_kept_attributes) { %w[estimated_hours done_ratio] }
+
+ it_behaves_like "service call",
+ description: "is an error state (to be detected by contract), " \
+ "and % complete and work are kept"
+ end
+
+ context "when remaining work is set and work is unset" do
+ let(:call_attributes) { { estimated_hours: nil, remaining_hours: 6.7 } }
+ let(:expected_attributes) { call_attributes.dup }
+ let(:expected_kept_attributes) { %w[done_ratio] }
+
+ it_behaves_like "service call",
+ description: "% complete is kept and work is kept unset and not recomputed" \
+ "(error state to be detected by contract)"
+ end
+ end
+ end
+ end
+end
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index 6e7f1639067..9bae00163aa 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -29,7 +29,8 @@
require "spec_helper"
RSpec.describe WorkPackages::SetAttributesService,
- type: :model do
+ type: :model,
+ with_flag: { percent_complete_edition: true } do
shared_let(:status_0_pct_complete) { create(:status, default_done_ratio: 0, name: "0% complete") }
shared_let(:status_50_pct_complete) { create(:status, default_done_ratio: 50, name: "50% complete") }
shared_let(:status_70_pct_complete) { create(:status, default_done_ratio: 70, name: "70% complete") }
diff --git a/spec/support/edit_fields/progress_edit_field.rb b/spec/support/edit_fields/progress_edit_field.rb
index 8ac3b32318b..775cc0a42fe 100644
--- a/spec/support/edit_fields/progress_edit_field.rb
+++ b/spec/support/edit_fields/progress_edit_field.rb
@@ -181,6 +181,19 @@ class ProgressEditField < EditField
end
end
+ # to be removed in 15.0 with :percent_complete_edition feature flag removal
+ def expect_migration_warning_banner(should_render: true)
+ within modal_element do
+ if should_render
+ expect(page)
+ .to have_text(I18n.t("work_package.progress.modal.migration_warning_text"))
+ else
+ expect(page)
+ .to have_no_text(I18n.t("work_package.progress.modal.migration_warning_text"))
+ end
+ end
+ end
+
private
attr_reader :field_name