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

Update work packages when status % complete is changed via UI

Christophe Bliard849cb99e82c1
diff --git a/app/controllers/statuses_controller.rb b/app/controllers/statuses_controller.rb
index a9c83c066ed..57803c14c93 100644
--- a/app/controllers/statuses_controller.rb
+++ b/app/controllers/statuses_controller.rb
@@ -63,6 +63,7 @@ class StatusesController < ApplicationController
if @status.update(permitted_params.status)
flash[:notice] = I18n.t(:notice_successful_update)
redirect_to action: "index"
+ apply_status_p_complete_change
else
render action: "edit"
end
@@ -104,4 +105,14 @@ class StatusesController < ApplicationController
def show_local_breadcrumb
true
end
+
+ def apply_status_p_complete_change
+ return unless WorkPackage.use_status_for_done_ratio?
+ return unless @status.default_done_ratio_previously_changed?
+
+ WorkPackages::ApplyStatusPCompleteChangeJob
+ .perform_later(status_name: @status.name,
+ status_id: @status.id,
+ change: @status.default_done_ratio_previous_change)
+ end
end
diff --git a/app/models/journal.rb b/app/models/journal.rb
index 6baa73508eb..3643d206a52 100644
--- a/app/models/journal.rb
+++ b/app/models/journal.rb
@@ -27,8 +27,8 @@
#++
class Journal < ApplicationRecord
- self.table_name = 'journals'
- self.ignored_columns += ['activity_type']
+ self.table_name = "journals"
+ self.ignored_columns += ["activity_type"]
WorkingDayUpdate = Struct.new(:working_days, :non_working_days, keyword_init: true)
@@ -59,15 +59,25 @@ class Journal < ApplicationRecord
# Attributes related to the cause are stored in a JSONB column so we can easily add new relations and related
# attributes without a heavy database migration. Fields will be prefixed with `cause_` but are stored in the JSONB
# hash without that prefix
- store_accessor :cause, %i[type work_package_id changed_days], prefix: true
+ store_accessor :cause,
+ %i[
+ type
+ work_package_id
+ changed_days
+ status_name
+ status_id
+ status_p_complete_change
+ ],
+ prefix: true
VALID_CAUSE_TYPES = %w[
- work_package_predecessor_changed_times
- work_package_parent_changed_times
+ default_attribute_written
+ status_p_complete_changed
+ system_update
work_package_children_changed_times
+ work_package_parent_changed_times
+ work_package_predecessor_changed_times
work_package_related_changed_times
working_days_changed
- default_attribute_written
- system_update
].freeze
# Make sure each journaled model instance only has unique version ids
@@ -78,16 +88,16 @@ class Journal < ApplicationRecord
belongs_to :journable, polymorphic: true
belongs_to :data, polymorphic: true, dependent: :destroy
- has_many :attachable_journals, class_name: 'Journal::AttachableJournal', dependent: :delete_all
- has_many :customizable_journals, class_name: 'Journal::CustomizableJournal', dependent: :delete_all
- has_many :storable_journals, class_name: 'Journal::StorableJournal', dependent: :delete_all
- has_many :agenda_item_journals, class_name: 'Journal::MeetingAgendaItemJournal', dependent: :delete_all
+ has_many :attachable_journals, class_name: "Journal::AttachableJournal", dependent: :delete_all
+ has_many :customizable_journals, class_name: "Journal::CustomizableJournal", dependent: :delete_all
+ has_many :storable_journals, class_name: "Journal::StorableJournal", dependent: :delete_all
+ has_many :agenda_item_journals, class_name: "Journal::MeetingAgendaItemJournal", dependent: :delete_all
has_many :notifications, dependent: :destroy
# Scopes to all journals excluding the initial journal - useful for change
# logs like the history on issue#show
- scope :changing, -> { where(['version > 1']) }
+ scope :changing, -> { where(["version > 1"]) }
scope :for_wiki_page, -> { where(journable_type: "WikiPage") }
scope :for_work_package, -> { where(journable_type: "WorkPackage") }
diff --git a/app/workers/work_packages/apply_status_p_complete_change_job.rb b/app/workers/work_packages/apply_status_p_complete_change_job.rb
new file mode 100644
index 00000000000..86688519ed1
--- /dev/null
+++ b/app/workers/work_packages/apply_status_p_complete_change_job.rb
@@ -0,0 +1,172 @@
+# 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.
+#++
+
+# rubocop:disable Rails/SquishedSQLHeredocs
+class WorkPackages::ApplyStatusPCompleteChangeJob < ApplicationJob
+ queue_with_priority :default
+
+ # Updates % complete and remaining work of all work packages after a status
+ # % complete value has been changed.
+ #
+ # It creates a journal entry with the System user describing the change.
+ #
+ # @param status_name [String] The name of the status to apply.
+ # @param status_id [Integer] The ID of the status to apply. Not used
+ # currently, but here in case we need it in a later version.
+ # @param change [Object] The change object containing an array of [old, new]
+ # values of the change.
+ # @return [void]
+ def perform(status_name:, status_id:, change:)
+ return if WorkPackage.use_field_for_done_ratio?
+
+ with_temporary_progress_table do
+ set_p_complete_from_status
+ derive_remaining_work_from_work_and_p_complete
+ update_totals
+
+ copy_progress_values_to_work_packages_and_update_journals(status_name:, status_id:, change:)
+ end
+ end
+
+ private
+
+ def with_temporary_progress_table
+ create_temporary_progress_table
+ yield
+ ensure
+ drop_temporary_progress_table
+ end
+
+ def create_temporary_progress_table
+ execute(<<~SQL)
+ CREATE UNLOGGED TABLE temp_wp_progress_values
+ AS SELECT
+ id,
+ status_id,
+ estimated_hours,
+ remaining_hours,
+ done_ratio,
+ NULL::double precision AS total_work,
+ NULL::double precision AS total_remaining_work,
+ NULL::integer AS total_p_complete
+ FROM work_packages
+ SQL
+ end
+
+ def drop_temporary_progress_table
+ execute(<<~SQL)
+ DROP TABLE temp_wp_progress_values
+ SQL
+ end
+
+ def set_p_complete_from_status
+ execute(<<~SQL.squish)
+ UPDATE temp_wp_progress_values
+ SET done_ratio = statuses.default_done_ratio
+ FROM statuses
+ WHERE temp_wp_progress_values.status_id = statuses.id
+ SQL
+ end
+
+ def derive_remaining_work_from_work_and_p_complete
+ execute(<<~SQL.squish)
+ UPDATE temp_wp_progress_values
+ SET remaining_hours = ROUND((estimated_hours - (estimated_hours * done_ratio / 100.0))::numeric, 2)
+ WHERE estimated_hours IS NOT NULL
+ AND done_ratio IS NOT NULL
+ SQL
+ end
+
+ def update_totals
+ execute(<<~SQL)
+ 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,
+ 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
+ SQL
+ end
+
+ def copy_progress_values_to_work_packages_and_update_journals(status_name:, status_id:, change:)
+ updated_work_package_ids = copy_progress_values_to_work_packages
+ create_journals_for_updated_work_packages(updated_work_package_ids, status_name:, status_id:, change:)
+ end
+
+ def copy_progress_values_to_work_packages
+ results = execute(<<~SQL)
+ UPDATE work_packages
+ SET remaining_hours = temp_wp_progress_values.remaining_hours,
+ done_ratio = temp_wp_progress_values.done_ratio,
+ derived_remaining_hours = temp_wp_progress_values.total_remaining_work,
+ derived_done_ratio = temp_wp_progress_values.total_p_complete,
+ lock_version = lock_version + 1,
+ updated_at = NOW()
+ FROM temp_wp_progress_values
+ WHERE work_packages.id = temp_wp_progress_values.id
+ AND (
+ work_packages.remaining_hours IS DISTINCT FROM temp_wp_progress_values.remaining_hours
+ OR work_packages.done_ratio IS DISTINCT FROM temp_wp_progress_values.done_ratio
+ OR work_packages.derived_remaining_hours IS DISTINCT FROM temp_wp_progress_values.total_remaining_work
+ OR work_packages.derived_done_ratio IS DISTINCT FROM temp_wp_progress_values.total_p_complete
+ )
+ RETURNING work_packages.id
+ SQL
+ results.column_values(0)
+ end
+
+ def create_journals_for_updated_work_packages(updated_work_package_ids, status_name:, status_id:, change:)
+ cause = { type: "status_p_complete_changed", status_name:, status_id:, status_p_complete_change: change }
+ WorkPackage.where(id: updated_work_package_ids).find_each do |work_package|
+ Journals::CreateService.new(work_package, system_user)
+ .call(cause:)
+ end
+ end
+
+ # Executes an sql statement, shorter.
+ def execute(sql)
+ ActiveRecord::Base.connection.execute(sql)
+ end
+
+ def system_user
+ @system_user ||= User.system
+ end
+end
+# rubocop:enable Rails/SquishedSQLHeredocs
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 242310cdce0..d118b8326df 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1675,6 +1675,7 @@ Project attributes and sections are defined in the <a href=%{admin_settings_url}
caused_changes:
default_attribute_written: "Read-only attributes written"
+ status_p_complete_changed: "Status % Complete changed"
dates_changed: "Dates changed"
system_update: "OpenProject system update:"
@@ -1692,6 +1693,8 @@ Project attributes and sections are defined in the <a href=%{admin_settings_url}
dates:
working: "%{date} is now working"
non_working: "%{date} is now non-working"
+ status_p_complete_changed: >-
+ % complete value for status '%{status_name}' changed from %{old_value}% to %{new_value}%
system_update:
file_links_journal: >
From now on, activity related to file links (files stored in external storages) will appear here in the
diff --git a/lib/open_project/journal_formatter/cause.rb b/lib/open_project/journal_formatter/cause.rb
index 77de14d4e41..cef5067f11c 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::Base
system_update_message(cause)
when "working_days_changed"
working_days_changed_message(cause["changed_days"])
+ when "status_p_complete_changed"
+ status_p_complete_changed_message(cause, html)
else
related_work_package_changed_message(cause, html)
end
@@ -94,6 +96,13 @@ class OpenProject::JournalFormatter::Cause < JournalFormatter::Base
date: I18n.l(Date.parse(date)))
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 html
+
+ I18n.t("journals.cause_descriptions.status_p_complete_changed", status_name:, old_value:, new_value:)
+ end
+
def related_work_package_changed_message(cause, html)
related_work_package = WorkPackage.includes(:project).visible(User.current).find_by(id: cause["work_package_id"])
diff --git a/spec/controllers/statuses_controller_spec.rb b/spec/controllers/statuses_controller_spec.rb
index 47c5240cd41..d76285c1a71 100644
--- a/spec/controllers/statuses_controller_spec.rb
+++ b/spec/controllers/statuses_controller_spec.rb
@@ -120,12 +120,13 @@ RSpec.describe StatusesController do
describe "#update" do
let(:name) { "Renamed Status" }
+ let(:status_params) { { name: } }
before do
patch :update,
params: {
id: status.id,
- status: { name: }
+ status: status_params
}
end
@@ -134,6 +135,59 @@ RSpec.describe StatusesController do
end
it_behaves_like "redirects to index page"
+
+ context "when in work-based mode when changing the default % complete",
+ with_settings: { work_package_done_ratio: "field" } do
+ let(:new_default_done_ratio) { 40 }
+ let(:status_params) { { default_done_ratio: new_default_done_ratio } }
+
+ it "does not start any jobs to update work packages % complete values" do
+ expect(status.reload).to have_attributes(default_done_ratio: new_default_done_ratio)
+ expect(WorkPackages::ApplyStatusPCompleteChangeJob)
+ .not_to have_been_enqueued
+ end
+ end
+
+ context "when in status-based mode",
+ with_settings: { work_package_done_ratio: "status" } do
+ context "when changing the default % complete" do
+ shared_let(:work_package) { create(:work_package, status:) }
+ let(:new_default_done_ratio) { 40 }
+ let(:status_params) { { default_done_ratio: new_default_done_ratio } }
+
+ it "starts a job to update work packages % complete values" do
+ old_default_done_ratio = status.default_done_ratio
+ expect(status.reload).to have_attributes(default_done_ratio: new_default_done_ratio)
+ expect(WorkPackages::ApplyStatusPCompleteChangeJob)
+ .to have_been_enqueued.with(status_name: status.name,
+ status_id: status.id,
+ change: [old_default_done_ratio, new_default_done_ratio])
+
+ perform_enqueued_jobs
+
+ expect(work_package.reload.read_attribute(:done_ratio)).to eq(new_default_done_ratio)
+ expect(work_package.last_journal.details["cause"].last).to include("type" => "status_p_complete_changed")
+ end
+ end
+
+ context "when changing to the same default % complete value" do
+ let(:status_params) { { default_done_ratio: status.default_done_ratio } }
+
+ it "does not start any jobs" do
+ expect(WorkPackages::ApplyStatusPCompleteChangeJob)
+ .not_to have_been_enqueued
+ end
+ end
+
+ context "when changing something else than the default % complete" do
+ let(:status_params) { { name: "Another status name" } }
+
+ it "does not start any jobs" do
+ expect(WorkPackages::ApplyStatusPCompleteChangeJob)
+ .not_to have_been_enqueued
+ end
+ end
+ end
end
describe "#destroy" do
diff --git a/spec/lib/journal_formatter/cause_spec.rb b/spec/lib/journal_formatter/cause_spec.rb
index 213a7e80ad9..67a16882d3a 100644
--- a/spec/lib/journal_formatter/cause_spec.rb
+++ b/spec/lib/journal_formatter/cause_spec.rb
@@ -34,7 +34,7 @@ RSpec.describe OpenProject::JournalFormatter::Cause do
include ActionView::Helpers::UrlHelper
include Rails.application.routes.url_helpers
- let(:work_package) { create(:work_package) }
+ shared_let(:work_package) { create(:work_package) }
let(:instance) { described_class.new(build(:work_package_journal)) }
let(:link) do
link_to_work_package(work_package, all_link: true)
@@ -43,7 +43,14 @@ RSpec.describe OpenProject::JournalFormatter::Cause do
# we need to tell the url_helper that there is not controller to get url_options so that we can call link_to
let(:controller) { nil }
- subject { instance.render("cause", [nil, cause], html:) }
+ subject do
+ if Journal::VALID_CAUSE_TYPES.exclude?(cause["type"])
+ raise "#{cause['type'].inspect} is not a valid cause type from Journal::VALID_CAUSE_TYPES. " \
+ "Please use one of #{Journal::VALID_CAUSE_TYPES}"
+ end
+
+ instance.render("cause", [nil, cause], html:)
+ end
context "when the change was caused by a change to the parent" do
let(:cause) do
@@ -283,6 +290,49 @@ RSpec.describe OpenProject::JournalFormatter::Cause do
end
end
+ context "when a change of status % complete is the cause" do
+ shared_let(:status) { create(:status, name: "In progress", default_done_ratio: 40) }
+ let(:cause) do
+ {
+ "type" => "status_p_complete_changed",
+ "status_name" => status.name,
+ "status_id" => status.id,
+ "status_p_complete_change" => [20, 40]
+ }
+ end
+
+ context "when rendering HTML variant" do
+ let(:html) { true }
+
+ it do
+ expect(subject).to eq "<strong>#{I18n.t('journals.caused_changes.status_p_complete_changed')}</strong> " \
+ "% complete value for status 'In progress' changed from 20% to 40%"
+ 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_p_complete_changed')}</strong> " \
+ "% complete value for status '&lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;' " \
+ "changed from 20% to 40%"
+ end
+ end
+
+ context "when rendering raw variant" do
+ let(:html) { false }
+
+ it do
+ expect(subject).to eq "#{I18n.t('journals.caused_changes.status_p_complete_changed')} " \
+ "% complete value for status 'In progress' changed from 20% to 40%"
+ 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_p_complete_changed')} " \
+ "% complete value for status '<script>alert('xss')</script>' changed from 20% to 40%"
+ end
+ end
+ end
+
context "when the change was caused by a system update" do
let(:cause) do
{
diff --git a/spec/workers/work_packages/apply_status_p_complete_change_job_spec.rb b/spec/workers/work_packages/apply_status_p_complete_change_job_spec.rb
new file mode 100644
index 00000000000..32ae1c53582
--- /dev/null
+++ b/spec/workers/work_packages/apply_status_p_complete_change_job_spec.rb
@@ -0,0 +1,190 @@
+#-- 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 "rails_helper"
+
+RSpec.describe WorkPackages::ApplyStatusPCompleteChangeJob do
+ shared_let(:author) { create(:user) }
+ shared_let(:priority) { create(:priority, name: "Normal") }
+ shared_let(:project) { create(:project, name: "Main project") }
+ shared_let(:status_new) { create(:status, name: "New") }
+
+ before_all do
+ set_factory_default(:user, author)
+ set_factory_default(:priority, priority)
+ set_factory_default(:project, project)
+ set_factory_default(:project_with_types, project)
+ set_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:, status_name: "New", status_id: 99, change: [33, 66])
+ table = create_table(from)
+
+ job.perform_now(status_name:, status_id:, change:)
+
+ table.work_packages.map(&:reload)
+ expect_work_packages(table.work_packages, to)
+
+ table.work_packages
+ end
+
+ context "when in work-based mode",
+ with_settings: { work_package_done_ratio: "field" } do
+ context "when some work packages have % complete value different from their status" do
+ it "does not change any of their progress values" do
+ expect_performing_job_changes(
+ from: <<~TABLE,
+ subject | status | % complete
+ wp | Doing (40%) |
+ wp 0% | To do (0%) | 55%
+ wp 40% | Doing (40%) | 55%
+ wp 100% | Done (100%) | 55%
+ TABLE
+ to: <<~TABLE
+ subject | status | % complete
+ wp | Doing (40%) |
+ wp 0% | To do (0%) | 55%
+ wp 40% | Doing (40%) | 55%
+ wp 100% | Done (100%) | 55%
+ TABLE
+ )
+ end
+ end
+ end
+
+ context "when in status-based mode",
+ with_settings: { work_package_done_ratio: "status" } do
+ context "when work packages have % complete value different from their status" do
+ it "updates the work packages % complete value from the status" do
+ expect_performing_job_changes(
+ from: <<~TABLE,
+ subject | status | % complete
+ wp | Doing (40%) |
+ wp 0% | To do (0%) | 50%
+ wp 40% | Doing (40%) | 50%
+ wp 100% | Done (100%) | 50%
+ TABLE
+ to: <<~TABLE
+ subject | status | % complete
+ wp | Doing (40%) | 40%
+ wp 0% | To do (0%) | 0%
+ wp 40% | Doing (40%) | 40%
+ wp 100% | Done (100%) | 100%
+ TABLE
+ )
+ end
+ end
+
+ context "when work packages have work and remaining work values" do
+ it "updates the work packages remaining work along with the % complete value from the status" do
+ expect_performing_job_changes(
+ from: <<~TABLE,
+ subject | status | work | remaining work | % complete
+ wp | Doing (40%) | 10h | |
+ wp 0% | To do (0%) | 10h | 5h | 50%
+ wp 40% | Doing (40%) | 10h | 5h | 50%
+ wp 100% | Done (100%) | 10h | 5h | 50%
+ TABLE
+ to: <<~TABLE
+ subject | status | work | remaining work | % complete
+ wp | Doing (40%) | 10h | 6h | 40%
+ wp 0% | To do (0%) | 10h | 10h | 0%
+ wp 40% | Doing (40%) | 10h | 6h | 40%
+ wp 100% | Done (100%) | 10h | 0h | 100%
+ TABLE
+ )
+ end
+ end
+
+ context "when in hierarchy" do
+ it "the total remaining work and total % complete values are recomputed" do
+ # Simulating changing "Doing" status default % progress from 20% to 40%
+ expect_performing_job_changes(
+ from: <<~TABLE,
+ hierarchy | status | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete
+ grandparent | Doing (40%) | 1h | 0.8h | 20% | 20h | 9.8h | 51%
+ parent | Doing (40%) | | | 20% | 19h | 9h | 53%
+ child 1 | Done (100%) | 9h | 0h | 100% | 9h | 0h | 100%
+ child 2 | Doing (40%) | 5h | 4h | 20% | 5h | 4h | 20%
+ child 3 | To do (0%) | 5h | 5h | 0% | 5h | 5h | 0%
+ TABLE
+ to: <<~TABLE
+ subject | status | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete
+ grandparent | Doing (40%) | 1h | 0.6h | 40% | 20h | 8.6h | 57%
+ parent | Doing (40%) | | | 40% | 19h | 8h | 58%
+ child 1 | Done (100%) | 9h | 0h | 100% | 9h | 0h | 100%
+ child 2 | Doing (40%) | 5h | 3h | 40% | 5h | 3h | 40%
+ child 3 | To do (0%) | 5h | 5h | 0% | 5h | 5h | 0%
+ TABLE
+ )
+ end
+ end
+
+ describe "journals" do
+ # rubocop:disable RSpec/ExampleLength
+ it "creates journal entries for modified work packages" do
+ parent, child1, child2 = expect_performing_job_changes(
+ from: <<~TABLE,
+ hierarchy | status | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete
+ parent | To do (0%) | | | 0% | 20h | 8h | 60%
+ child 1 | Doing (40%) | 10h | 8h | 20% | 10h | 8h | 20%
+ child 2 | Done (100%) | 10h | 0h | 100% | 10h | 0h | 100%
+ TABLE
+ to: <<~TABLE,
+ subject | status | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete
+ parent | To do (0%) | | | 0% | 20h | 6h | 70%
+ child 1 | Doing (40%) | 10h | 6h | 40% | 10h | 6h | 40%
+ child 2 | Done (100%) | 10h | 0h | 100% | 10h | 0h | 100%
+ TABLE
+ status_name: status_40p_doing.name,
+ status_id: status_40p_doing.id,
+ change: [20, 40]
+ )
+ [parent, child1].each do |work_package|
+ expect(work_package.journals.count).to eq 2
+ last_journal = work_package.last_journal
+ expect(last_journal.user).to eq(User.system)
+ expect(last_journal.cause_type).to eq("status_p_complete_changed")
+ expect(last_journal.cause_status_name).to eq("Doing (40%)")
+ expect(last_journal.cause_status_id).to eq(status_40p_doing.id)
+ expect(last_journal.cause_status_p_complete_change).to eq([20, 40])
+ end
+
+ # unchanged => no new journals
+ expect(child2.journals.count).to eq 1
+ end
+ # rubocop:enable RSpec/ExampleLength
+ end
+ end
+end