OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[49409] Handle excluded statuses from totals in update job
Christophe Bliard6b4c0dccf203
diff --git a/app/controllers/statuses_controller.rb b/app/controllers/statuses_controller.rbindex 74bd847206b..53f549af30b 100644--- a/app/controllers/statuses_controller.rb+++ b/app/controllers/statuses_controller.rb@@ -101,7 +101,7 @@ class StatusesController < ApplicationControllerreturn unless WorkPackage.use_status_for_done_ratio?return unless @status.default_done_ratio_previously_changed?- WorkPackages::Progress::ApplyStatusesPCompleteJob+ WorkPackages::Progress::ApplyStatusesChangeJob.perform_later(cause_type: "status_p_complete_changed",status_name: @status.name,status_id: @status.id,diff --git a/app/models/journal.rb b/app/models/journal.rbindex fb5f3f329a3..d5e07c2e275 100644--- a/app/models/journal.rb+++ b/app/models/journal.rb@@ -71,12 +71,14 @@ class Journal < ApplicationRecordchanged_daysstatus_namestatus_id+ status_excluded_from_totals_changestatus_p_complete_change],prefix: trueVALID_CAUSE_TYPES = %w[default_attribute_writtenprogress_mode_changed_to_status_based+ status_excluded_from_totals_changedstatus_p_complete_changedsystem_updatework_package_children_changed_timesdiff --git a/app/models/journal/caused_by_status_excluded_from_totals_changed.rb b/app/models/journal/caused_by_status_excluded_from_totals_changed.rbnew file mode 100644index 00000000000..e80b5b33bda--- /dev/null+++ b/app/models/journal/caused_by_status_excluded_from_totals_changed.rb@@ -0,0 +1,39 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 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 Journal::CausedByStatusExcludedFromTotalsChanged < CauseOfChange::Base+ def initialize(status_name:, status_id:, status_excluded_from_totals_change:)+ additional = {+ "status_name" => status_name,+ "status_id" => status_id,+ "status_excluded_from_totals_change" => status_excluded_from_totals_change+ }++ super("status_excluded_from_totals_changed", additional)+ end+enddiff --git a/app/services/settings/update_service.rb b/app/services/settings/update_service.rbindex 836a7381ca2..255f7af69be 100644--- a/app/services/settings/update_service.rb+++ b/app/services/settings/update_service.rb@@ -46,7 +46,7 @@ class Settings::UpdateService < BaseServices::BaseContractednew_value = derive_value(value)Setting[name] = new_valueif name == :work_package_done_ratio && old_value != "status" && new_value == "status"- WorkPackages::Progress::ApplyStatusesPCompleteJob.perform_later(cause_type: "progress_mode_changed_to_status_based")+ WorkPackages::Progress::ApplyStatusesChangeJob.perform_later(cause_type: "progress_mode_changed_to_status_based")endenddiff --git a/app/workers/work_packages/progress/apply_statuses_p_complete_job.rb b/app/workers/work_packages/progress/apply_statuses_change_job.rbsimilarity index 60%rename from app/workers/work_packages/progress/apply_statuses_p_complete_job.rbrename to app/workers/work_packages/progress/apply_statuses_change_job.rbindex e85e3b0cca6..b5f52315c02 100644--- a/app/workers/work_packages/progress/apply_statuses_p_complete_job.rb+++ b/app/workers/work_packages/progress/apply_statuses_change_job.rb@@ -28,12 +28,15 @@# See COPYRIGHT and LICENSE files for more details.#++-class WorkPackages::Progress::ApplyStatusesPCompleteJob < WorkPackages::Progress::Job+class WorkPackages::Progress::ApplyStatusesChangeJob < WorkPackages::Progress::JobVALID_CAUSE_TYPES = %w[progress_mode_changed_to_status_based+ status_excluded_from_totals_changedstatus_p_complete_changed].freeze+ attr_reader :cause_type, :status_name, :status_id, :change+# Updates % complete and remaining work of all work packages after a status %# complete value has been changed or the progress calculation mode was set to# status-based.@@ -49,13 +52,16 @@ class WorkPackages::Progress::ApplyStatusesPCompleteJob < WorkPackages::Progress# values of the change.# @return [void]def perform(cause_type:, status_name: nil, status_id: nil, change: nil)- return if WorkPackage.use_field_for_done_ratio?-- journal_cause = journal_cause_from(cause_type, status_name:, status_id:, change:)+ @cause_type = cause_type+ @status_name = status_name+ @status_id = status_id+ @change = changewith_temporary_progress_table do- set_p_complete_from_status- derive_remaining_work_from_work_and_p_complete+ if WorkPackage.use_status_for_done_ratio?+ set_p_complete_from_status+ derive_remaining_work_from_work_and_p_complete+ endupdate_totalscopy_progress_values_to_work_packages_and_update_journals(journal_cause)@@ -64,23 +70,50 @@ class WorkPackages::Progress::ApplyStatusesPCompleteJob < WorkPackages::Progressprivate- def journal_cause_from(cause_type, status_name:, status_id:, change:)- if VALID_CAUSE_TYPES.exclude?(cause_type)+ def journal_cause+ assert_valid_cause_type!++ @journal_cause ||=+ case cause_type+ when "progress_mode_changed_to_status_based"+ Journal::CausedByProgressModeChangedToStatusBased.new+ when "status_excluded_from_totals_changed"+ assert_status_information_present!+ Journal::CausedByStatusExcludedFromTotalsChanged.new(+ status_name:,+ status_id:,+ status_excluded_from_totals_change: change+ )+ when "status_p_complete_changed"+ assert_status_information_present!+ Journal::CausedByStatusPCompleteChanged.new(+ status_name:,+ status_id:,+ status_p_complete_change: change+ )+ else+ raise "Unable to handle cause type #{cause_type.inspect}"+ end+ end++ def assert_valid_cause_type!+ unless VALID_CAUSE_TYPES.include?(cause_type)raise ArgumentError, "Invalid cause type #{cause_type.inspect}. " \"Valid values are #{VALID_CAUSE_TYPES.inspect}"end+ end++ def assert_status_information_present!+ if status_name.blank?+ raise ArgumentError, "status_name must be provided"+ end- case cause_type- when "progress_mode_changed_to_status_based"- { type: cause_type }- when "status_p_complete_changed"- raise ArgumentError, "status_name must be provided" if status_name.blank?- raise ArgumentError, "status_id must be provided" if status_id.nil?- raise ArgumentError, "change must be provided" if change.nil?+ if status_id.nil?+ raise ArgumentError, "status_id must be provided"+ end- { type: cause_type, status_name:, status_id:, status_p_complete_change: change }- else- raise "Unable to handle cause type #{cause_type.inspect}"+ if change.nil?+ raise ArgumentError, "change must be provided"endendenddiff --git a/app/workers/work_packages/progress/migrate_remove_totals_from_childless_work_packages_job.rb b/app/workers/work_packages/progress/migrate_remove_totals_from_childless_work_packages_job.rbindex d49f81493b8..d01e42f7f56 100644--- a/app/workers/work_packages/progress/migrate_remove_totals_from_childless_work_packages_job.rb+++ b/app/workers/work_packages/progress/migrate_remove_totals_from_childless_work_packages_job.rb@@ -27,6 +27,8 @@#++class WorkPackages::Progress::MigrateRemoveTotalsFromChildlessWorkPackagesJob < WorkPackages::Progress::Job+ include WorkPackages::Progress::SqlCommandsForMigration+def performupdated_work_package_ids = remove_totals_from_childless_work_packagescreate_journals_for_updated_work_packages(updated_work_package_ids, cause: journal_cause)diff --git a/app/workers/work_packages/progress/migrate_values_job.rb b/app/workers/work_packages/progress/migrate_values_job.rbindex 6e77ee790e3..212f912d824 100644--- a/app/workers/work_packages/progress/migrate_values_job.rb+++ b/app/workers/work_packages/progress/migrate_values_job.rb@@ -27,6 +27,8 @@#++class WorkPackages::Progress::MigrateValuesJob < WorkPackages::Progress::Job+ include WorkPackages::Progress::SqlCommandsForMigration+attr_reader :current_mode, :previous_modedef perform(current_mode:, previous_mode:)diff --git a/app/workers/work_packages/progress/sql_commands.rb b/app/workers/work_packages/progress/sql_commands.rbindex 9ad890a1769..1430503de91 100644--- a/app/workers/work_packages/progress/sql_commands.rb+++ b/app/workers/work_packages/progress/sql_commands.rb@@ -101,6 +101,8 @@ module WorkPackages::Progress::SqlCommandsSUM(remaining_hours) AS total_remaining_workFROM work_package_hierarchies wp_treeLEFT JOIN temp_wp_progress_values wp_progress ON wp_tree.descendant_id = wp_progress.id+ LEFT JOIN statuses ON wp_progress.status_id = statuses.id+ WHERE statuses.excluded_from_totals = FALSEGROUP BY wp_tree.ancestor_id) totalsWHERE temp_wp_progress_values.id = totals.iddiff --git a/app/workers/work_packages/progress/sql_commands_for_migration.rb b/app/workers/work_packages/progress/sql_commands_for_migration.rbnew file mode 100644index 00000000000..6842c7e2254--- /dev/null+++ b/app/workers/work_packages/progress/sql_commands_for_migration.rb@@ -0,0 +1,59 @@+# 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.+#++++# Specific SQL commands for updating work package progress values during+# migrations, as some methods in `WorkPackages::Progress::SqlCommands` rely on+# fields that may not exist yet.+module WorkPackages::Progress::SqlCommandsForMigration+ # Computes total work, total remaining work and total % complete for all work+ # packages having children.+ def update_totals+ execute(<<~SQL.squish)+ UPDATE temp_wp_progress_values+ SET total_work = totals.total_work,+ total_remaining_work = totals.total_remaining_work,+ total_p_complete = CASE+ WHEN totals.total_work = 0 THEN NULL+ ELSE (1 - (totals.total_remaining_work / totals.total_work)) * 100+ END+ FROM (+ SELECT wp_tree.ancestor_id AS id,+ MAX(generations) AS generations,+ SUM(estimated_hours) AS total_work,+ SUM(remaining_hours) AS total_remaining_work+ FROM work_package_hierarchies wp_tree+ LEFT JOIN temp_wp_progress_values wp_progress ON wp_tree.descendant_id = wp_progress.id+ GROUP BY wp_tree.ancestor_id+ ) totals+ WHERE temp_wp_progress_values.id = totals.id+ AND totals.generations > 0+ SQL+ end+enddiff --git a/config/locales/en.yml b/config/locales/en.ymlindex 538a0bad18e..169751efa9c 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -1742,6 +1742,7 @@ Project attributes and sections are defined in the <a href=%{admin_settings_url}default_attribute_written: "Read-only attributes written"progress_mode_changed_to_status_based: "Progress calculation updated"status_p_complete_changed: "Status % Complete changed"+ status_excluded_from_totals_changed: "Status Excluded from totals changed"system_update: "OpenProject system update:"cause_descriptions:@@ -1759,6 +1760,10 @@ Project attributes and sections are defined in the <a href=%{admin_settings_url}working: "%{date} is now working"non_working: "%{date} is now non-working"progress_mode_changed_to_status_based: Progress calculation mode set to status-based+ status_excluded_from_totals_set_to_false_message: >-+ status '%{status_name}' was included in totals calculations+ status_excluded_from_totals_set_to_true_message: >-+ status '%{status_name}' was excluded from totals calculationsstatus_p_complete_changed: >-% complete value for status '%{status_name}' changed from %{old_value}% to %{new_value}%system_update:diff --git a/lib/open_project/journal_formatter/cause.rb b/lib/open_project/journal_formatter/cause.rbindex cd316b8fcd6..69bc85837de 100644--- a/lib/open_project/journal_formatter/cause.rb+++ b/lib/open_project/journal_formatter/cause.rb@@ -64,6 +64,8 @@ class OpenProject::JournalFormatter::Cause < JournalFormatter::Basesystem_update_message(cause, html)when "working_days_changed"working_days_changed_message(cause["changed_days"])+ when "status_excluded_from_totals_changed"+ status_excluded_from_totals_changed_message(cause, html)when "status_p_complete_changed"status_p_complete_changed_message(cause, html)when "progress_mode_changed_to_status_based"@@ -112,6 +114,17 @@ class OpenProject::JournalFormatter::Cause < JournalFormatter::Basedate: I18n.l(Date.parse(date)))end+ def status_excluded_from_totals_changed_message(cause, html)+ cause.symbolize_keys => { status_name:, status_excluded_from_totals_change: change}+ status_name = html_escape(status_name) if html++ if change == [false, true]+ I18n.t("journals.cause_descriptions.status_excluded_from_totals_set_to_true_message", status_name:)+ elsif change == [true, false]+ I18n.t("journals.cause_descriptions.status_excluded_from_totals_set_to_false_message", status_name:)+ end+ end+def status_p_complete_changed_message(cause, html)cause.symbolize_keys => { status_name:, status_p_complete_change: [old_value, new_value]}status_name = html_escape(status_name) if htmldiff --git a/spec/controllers/admin/settings/work_packages_settings_controller_spec.rb b/spec/controllers/admin/settings/work_packages_settings_controller_spec.rbindex 93d9a06c282..0f162cd8b9f 100644--- a/spec/controllers/admin/settings/work_packages_settings_controller_spec.rb+++ b/spec/controllers/admin/settings/work_packages_settings_controller_spec.rb@@ -54,7 +54,7 @@ RSpec.describe Admin::Settings::WorkPackagesSettingsController do}}expect(Setting.work_package_done_ratio).to eq("status")- expect(WorkPackages::Progress::ApplyStatusesPCompleteJob)+ expect(WorkPackages::Progress::ApplyStatusesChangeJob).to have_been_enqueued.with(cause_type: "progress_mode_changed_to_status_based")perform_enqueued_jobs@@ -76,7 +76,7 @@ RSpec.describe Admin::Settings::WorkPackagesSettingsController dowork_package_done_ratio: "status"}}- expect(WorkPackages::Progress::ApplyStatusesPCompleteJob)+ expect(WorkPackages::Progress::ApplyStatusesChangeJob).not_to have_been_enqueuedendend@@ -93,7 +93,7 @@ RSpec.describe Admin::Settings::WorkPackagesSettingsController dowork_package_done_ratio: "field"}}- expect(WorkPackages::Progress::ApplyStatusesPCompleteJob)+ expect(WorkPackages::Progress::ApplyStatusesChangeJob).not_to have_been_enqueuedendenddiff --git a/spec/controllers/statuses_controller_spec.rb b/spec/controllers/statuses_controller_spec.rbindex da8e941e2c8..d0cf513f71f 100644--- a/spec/controllers/statuses_controller_spec.rb+++ b/spec/controllers/statuses_controller_spec.rb@@ -143,7 +143,7 @@ RSpec.describe StatusesController doit "does not start any jobs to update work packages % complete values" doexpect(status.reload).to have_attributes(default_done_ratio: new_default_done_ratio)- expect(WorkPackages::Progress::ApplyStatusesPCompleteJob)+ expect(WorkPackages::Progress::ApplyStatusesChangeJob).not_to have_been_enqueuedendend@@ -158,7 +158,7 @@ RSpec.describe StatusesController doit "starts a job to update work packages % complete values" doold_default_done_ratio = status.default_done_ratioexpect(status.reload).to have_attributes(default_done_ratio: new_default_done_ratio)- expect(WorkPackages::Progress::ApplyStatusesPCompleteJob)+ expect(WorkPackages::Progress::ApplyStatusesChangeJob).to have_been_enqueued.with(cause_type: "status_p_complete_changed",status_name: status.name,status_id: status.id,@@ -175,7 +175,7 @@ RSpec.describe StatusesController dolet(:status_params) { { default_done_ratio: status.default_done_ratio } }it "does not start any jobs" do- expect(WorkPackages::Progress::ApplyStatusesPCompleteJob)+ expect(WorkPackages::Progress::ApplyStatusesChangeJob).not_to have_been_enqueuedendend@@ -184,7 +184,7 @@ RSpec.describe StatusesController dolet(:status_params) { { name: "Another status name" } }it "does not start any jobs" do- expect(WorkPackages::Progress::ApplyStatusesPCompleteJob)+ expect(WorkPackages::Progress::ApplyStatusesChangeJob).not_to have_been_enqueuedendenddiff --git a/spec/lib/journal_formatter/cause_spec.rb b/spec/lib/journal_formatter/cause_spec.rbindex f85369703a0..85eb7cf5b45 100644--- a/spec/lib/journal_formatter/cause_spec.rb+++ b/spec/lib/journal_formatter/cause_spec.rb@@ -333,6 +333,90 @@ RSpec.describe OpenProject::JournalFormatter::Cause doendend+ context "when a status being excluded from totals is the cause" do+ shared_let(:status) { create(:status, name: "Rejected") }+ let(:cause) do+ {+ "type" => "status_excluded_from_totals_changed",+ "status_name" => status.name,+ "status_id" => status.id,+ "status_excluded_from_totals_change" => [false, true]+ }+ end++ context "when rendering HTML variant" do+ let(:html) { true }++ it do+ expect(subject).to eq "<strong>#{I18n.t('journals.caused_changes.status_excluded_from_totals_changed')}</strong> " \+ "status 'Rejected' was excluded from totals calculations"+ end++ it "escapes the status name" do+ cause["status_name"] = "<script>alert('xss')</script>"+ expect(subject).to eq "<strong>#{I18n.t('journals.caused_changes.status_excluded_from_totals_changed')}</strong> " \+ "status '<script>alert('xss')</script>' was excluded from totals calculations"+ end+ end++ context "when rendering raw variant" do+ let(:html) { false }++ it do+ expect(subject).to eq "#{I18n.t('journals.caused_changes.status_excluded_from_totals_changed')} " \+ "status 'Rejected' was excluded from totals calculations"+ end++ it "does not escape the status name" do+ cause["status_name"] = "<script>alert('xss')</script>"+ expect(subject).to eq "#{I18n.t('journals.caused_changes.status_excluded_from_totals_changed')} " \+ "status '<script>alert('xss')</script>' was excluded from totals calculations"+ end+ end+ end++ context "when a status being no longer excluded from totals is the cause" do+ shared_let(:status) { create(:status, name: "Rejected") }+ let(:cause) do+ {+ "type" => "status_excluded_from_totals_changed",+ "status_name" => status.name,+ "status_id" => status.id,+ "status_excluded_from_totals_change" => [true, false]+ }+ end++ context "when rendering HTML variant" do+ let(:html) { true }++ it do+ expect(subject).to eq "<strong>#{I18n.t('journals.caused_changes.status_excluded_from_totals_changed')}</strong> " \+ "status 'Rejected' was included in totals calculations"+ end++ it "escapes the status name" do+ cause["status_name"] = "<script>alert('xss')</script>"+ expect(subject).to eq "<strong>#{I18n.t('journals.caused_changes.status_excluded_from_totals_changed')}</strong> " \+ "status '<script>alert('xss')</script>' was included in totals calculations"+ end+ end++ context "when rendering raw variant" do+ let(:html) { false }++ it do+ expect(subject).to eq "#{I18n.t('journals.caused_changes.status_excluded_from_totals_changed')} " \+ "status 'Rejected' was included in totals calculations"+ end++ it "does not escape the status name" do+ cause["status_name"] = "<script>alert('xss')</script>"+ expect(subject).to eq "#{I18n.t('journals.caused_changes.status_excluded_from_totals_changed')} " \+ "status '<script>alert('xss')</script>' was included in totals calculations"+ end+ end+ end+context "when a change of progress calculation mode to status-based is the cause" dolet(:cause) do{diff --git a/spec/workers/work_packages/progress/apply_statuses_p_complete_job_spec.rb b/spec/workers/work_packages/progress/apply_statuses_change_job_spec.rbsimilarity index 65%rename from spec/workers/work_packages/progress/apply_statuses_p_complete_job_spec.rbrename to spec/workers/work_packages/progress/apply_statuses_change_job_spec.rbindex 185fead8735..bb7b4ec8266 100644--- a/spec/workers/work_packages/progress/apply_statuses_p_complete_job_spec.rb+++ b/spec/workers/work_packages/progress/apply_statuses_change_job_spec.rb@@ -28,11 +28,23 @@require "rails_helper"-RSpec.describe WorkPackages::Progress::ApplyStatusesPCompleteJob do+RSpec.describe WorkPackages::Progress::ApplyStatusesChangeJob doshared_let(:author) { create(:user) }shared_let(:priority) { create(:priority, name: "Normal") }shared_let(:project) { create(:project, name: "Main project") }++ # statuses for work-based modeshared_let(:status_new) { create(:status, name: "New") }+ shared_let(:status_wip) { create(:status, name: "In progress") }+ shared_let(:status_closed) { create(:status, name: "Closed") }++ # statuses for status-based mode+ shared_let(:status_0p_todo) { create(:status, name: "To do (0%)", default_done_ratio: 0) }+ shared_let(:status_40p_doing) { create(:status, name: "Doing (40%)", default_done_ratio: 40) }+ shared_let(:status_100p_done) { create(:status, name: "Done (100%)", default_done_ratio: 100) }++ # statuses for both work-based and status-based modes+ shared_let(:status_excluded) { create(:status, :excluded_from_totals, name: "Excluded") }before_all doset_factory_default(:user, author)@@ -42,10 +54,6 @@ RSpec.describe WorkPackages::Progress::ApplyStatusesPCompleteJob doset_factory_default(:status, status_new)end- shared_let(:status_0p_todo) { create(:status, name: "To do (0%)", default_done_ratio: 0) }- shared_let(:status_40p_doing) { create(:status, name: "Doing (40%)", default_done_ratio: 40) }- shared_let(:status_100p_done) { create(:status, name: "Done (100%)", default_done_ratio: 100) }-subject(:job) { described_class }def expect_performing_job_changes(from:, to:,@@ -85,6 +93,57 @@ RSpec.describe WorkPackages::Progress::ApplyStatusesPCompleteJob do)endend++ context "when a status is being excluded from progress calculation" do+ # The work packages are created like if the status is not excluded yet+ shared_let_work_packages(<<~TABLE)+ hierarchy | status | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete+ grandparent | New | 1h | 0.6h | 40% | 24h | 11.3h | 53%+ parent | Excluded | 4h | 1h | 75% | 23h | 10.7h | 53%+ child 1 | Excluded | 9h | 7.2h | 20% | | |+ child 2 | In progress | 5h | 2.5h | 50% | | |+ child 3 | Closed | 5h | 0h | 100% | | |+ TABLE++ before do+ job.perform_now(+ cause_type: "status_excluded_from_totals_changed",+ status_name: status_excluded.name,+ status_id: status_excluded.id,+ change: [false, true]+ )+ table_work_packages.map(&:reload)+ end++ it "recomputes totals without the values from work packages having the excluded status" do+ expect_work_packages(table_work_packages, <<~TABLE)+ subject | status | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete+ grandparent | New | 1h | 0.6h | 40% | 11h | 3.1h | 72%+ parent | Excluded | 4h | 1h | 75% | 10H | 2.5h | 75%+ child 1 | Excluded | 9h | 7.2h | 20% | | |+ child 2 | In progress | 5h | 2.5h | 50% | | |+ child 3 | Closed | 5h | 0h | 100% | | |+ TABLE+ end++ it "adds a relevant journal entry for the parent with recomputed total" do+ changed_worked_packages = [grandparent, parent]+ changed_worked_packages.each do |work_package|+ expect(work_package.journals.count).to eq(2), "expected #{work_package} to have a new journal"+ last_journal = work_package.last_journal+ expect(last_journal.user).to eq(User.system)+ expect(last_journal.cause_type).to eq("status_excluded_from_totals_changed")+ expect(last_journal.cause_status_name).to eq("Excluded")+ expect(last_journal.cause_status_id).to eq(status_excluded.id)+ expect(last_journal.cause_status_excluded_from_totals_change).to eq([false, true])+ end++ unchanged_work_packages = table_work_packages - changed_worked_packages+ unchanged_work_packages.each do |work_package|+ expect(work_package.journals.count).to eq(1), "expected #{work_package} not to have new journals"+ end+ end+ endendcontext "when in status-based mode",@@ -155,6 +214,57 @@ RSpec.describe WorkPackages::Progress::ApplyStatusesPCompleteJob doendend+ context "when a status is being excluded from progress calculation" do+ # The work packages are created like if the status is not excluded yet+ shared_let_work_packages(<<~TABLE)+ hierarchy | status | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete+ grandparent | Doing (40%) | 1h | 0.6h | 40% | 24h | 16.6h | 31%+ parent | Excluded | 4h | 4h | 0% | 23h | 16h | 30%+ child 1 | Excluded | 9h | 9h | 0% | | |+ child 2 | Doing (40%) | 5h | 3h | 40% | | |+ child 3 | Done (100%) | 5h | 0h | 100% | | |+ TABLE++ before do+ job.perform_now(+ cause_type: "status_excluded_from_totals_changed",+ status_name: status_excluded.name,+ status_id: status_excluded.id,+ change: [false, true]+ )+ table_work_packages.map(&:reload)+ end++ it "recomputes totals without the values from work packages having the excluded status" do+ expect_work_packages(table_work_packages, <<~TABLE)+ subject | status | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete+ grandparent | Doing (40%) | 1h | 0.6h | 40% | 11h | 3.6h | 67%+ parent | Excluded | 4h | 4h | 0% | 10h | 3h | 70%+ child 1 | Excluded | 9h | 9h | 0% | | |+ child 2 | Doing (40%) | 5h | 3h | 40% | | |+ child 3 | Done (100%) | 5h | 0h | 100% | | |+ TABLE+ end++ it "adds a relevant journal entry for the parent with recomputed total" do+ changed_worked_packages = [grandparent, parent]+ changed_worked_packages.each do |work_package|+ expect(work_package.journals.count).to eq(2), "expected #{work_package} to have a new journal"+ last_journal = work_package.last_journal+ expect(last_journal.user).to eq(User.system)+ expect(last_journal.cause_type).to eq("status_excluded_from_totals_changed")+ expect(last_journal.cause_status_name).to eq("Excluded")+ expect(last_journal.cause_status_id).to eq(status_excluded.id)+ expect(last_journal.cause_status_excluded_from_totals_change).to eq([false, true])+ end++ unchanged_work_packages = table_work_packages - changed_worked_packages+ unchanged_work_packages.each do |work_package|+ expect(work_package.journals.count).to eq(1), "expected #{work_package} not to have new journals"+ end+ end+ end+describe "journals" do# rubocop:disable RSpec/ExampleLengthit "creates journal entries for modified work packages on status % complete change" do