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
[50961] Ensure derived values are updated on status and % complete changes
Christophe Bliardc559de0fe55f
diff --git a/app/services/work_packages/update_ancestors_service.rb b/app/services/work_packages/update_ancestors_service.rbindex a7fb623394d..a655759f778 100644--- a/app/services/work_packages/update_ancestors_service.rb+++ b/app/services/work_packages/update_ancestors_service.rb@@ -94,7 +94,7 @@ class WorkPackages::UpdateAncestorsService# the completion of sub-WPs, as well as the estimated hours# as a weight factor. So changes in estimated hours also have# to trigger a recalculation of done_ratio.- %i[done_ratio estimated_hours] => :derive_done_ratio,+ %i[done_ratio estimated_hours status status_id] => :derive_done_ratio,%i[remaining_hours] => :derive_remaining_hours,%i[ignore_non_working_days] => :derive_ignore_non_working_days}.each do |derivative_attributes, method|@@ -212,7 +212,14 @@ class WorkPackages::UpdateAncestorsServiceenddef modified_attributes_justify_derivation?(attributes)- attributes.intersect?(%i[estimated_hours done_ratio parent parent_id ignore_non_working_days remaining_hours])+ attributes.intersect?(%i[+ done_ratio+ estimated_hours+ ignore_non_working_days+ parent parent_id+ remaining_hours+ status status_id+ ])enddef ignore_non_working_days_of_descendants(ancestor, loader)diff --git a/spec/features/work_packages/update_ancestors_spec.rb b/spec/features/work_packages/update_ancestors_spec.rbindex c1a5c4f8f5a..2a3b872f99b 100644--- a/spec/features/work_packages/update_ancestors_spec.rb+++ b/spec/features/work_packages/update_ancestors_spec.rb@@ -33,14 +33,31 @@ require 'spec_helper'RSpec.describe 'Update ancestors', :js, :with_cuprite doshared_let(:user) { create(:admin) }shared_let(:priority) { create(:default_priority) }- shared_let(:status) { create(:default_status) }- shared_let(:project) { create(:project_with_types) }+ shared_let(:new_status) { create(:default_status, name: 'New') }+ shared_let(:closed_status) { create(:closed_status, name: 'Closed') }+ shared_let(:type) { create(:type_task) }+ shared_let(:project_role) { create(:project_role) }+ shared_let(:project) { create(:project, types: [type]) }+ shared_let(:workflows) do+ create(:workflow,+ type_id: type.id,+ old_status: new_status,+ new_status: closed_status,+ role: project_role)++ create(:workflow,+ type_id: type.id,+ old_status: closed_status,+ new_status:,+ role: project_role)+ endbefore_all doset_factory_default(:priority, priority)set_factory_default(:project, project)set_factory_default(:project_with_types, project)- set_factory_default(:status, status)+ set_factory_default(:status, new_status)+ set_factory_default(:type, type)set_factory_default(:user, user)end@@ -55,12 +72,21 @@ RSpec.describe 'Update ancestors', :js, :with_cuprite doparent:,subject: 'child',estimated_hours: 6,- remaining_hours: 3)+ remaining_hours: 3,+ done_ratio: 50)+ end+ shared_let(:second_child) do+ create(:work_package,+ parent:,+ subject: 'second child',+ estimated_hours: 3,+ remaining_hours: 3,+ done_ratio: 0)endshared_let(:query) docreate(:query,show_hierarchies: true,- column_names: %i[id estimated_hours remaining_hours subject])+ column_names: %i[id status estimated_hours remaining_hours done_ratio subject])endlet(:wp_table) { Pages::WorkPackagesTable.new project }@@ -69,18 +95,19 @@ RSpec.describe 'Update ancestors', :js, :with_cuprite do# make sure the derived fields are initially displayed rightWorkPackages::UpdateAncestorsService.new(user:, work_package: child)- .call(%i[estimated_hours remaining_hours])+ .call(%i[estimated_hours remaining_hours done_ratio])login_as(user)wp_table.visit_query queryend- context 'when changing the child work and remaining work values' do- it 'updates the parent work and remaining work values' do+ context 'when changing a child work and remaining work values', retry: 2 do+ it 'updates the parent work, remaining work, and % complete values' doexpect dowp_table.update_work_package_attributes(child, estimatedTime: child.estimated_hours + 1)parent.reloadend.to change(parent, :derived_estimated_hours).by(1)+ .and change(parent, :done_ratio).to(35) # 7h at 50% and 3h at 0% => 35% complete for 10hexpect dowp_table.update_work_package_attributes(child, remainingTime: child.remaining_hours + 2)parent.reload@@ -88,51 +115,77 @@ RSpec.describe 'Update ancestors', :js, :with_cuprite doendend- context 'when deleting the child' do- it 'updates the parent work and remaining work values' do- context_menu = wp_table.open_context_menu_for(child)+ context 'when changing a child % complete value' do+ it 'updates the parent % complete value' do+ expect do+ wp_table.update_work_package_attributes(child, percentageDone: 100)+ parent.reload+ end.to change(parent, :done_ratio).to(67) # 6h at 100% and 3h at 0% => 67% complete for 9h+ end+ end++ context 'when setting a child status to closed' do+ it 'considers child % complete to be 100% and updates the parent % complete value accordingly' do+ expect do+ wp_table.update_work_package_attributes(child, status: 'Closed')+ parent.reload+ end.to change(parent, :done_ratio).to(67) # 6h at 100% and 3h at 0% => 67% complete for 9h+ end+ end++ context 'when deleting a child' do+ it 'updates the parent work, remaining work, and % complete values' do+ context_menu = wp_table.open_context_menu_for(second_child)context_menu.choose_delete_and_confirm_deletionparent.reload- expect(parent.derived_estimated_hours).to eq(parent.estimated_hours)- expect(parent.derived_remaining_hours).to eq(parent.remaining_hours)+ expect(parent.derived_estimated_hours).to eq([parent, child].pluck(:estimated_hours).sum)+ expect(parent.derived_remaining_hours).to eq([parent, child].pluck(:remaining_hours).sum)+ expect(parent.done_ratio).to eq(child.done_ratio)endend- context 'when adding another the child' do- it 'updates the parent work and remaining work values' do+ context 'when adding a new child' do+ it 'updates the parent work, remaining work, and % complete values' docontext_menu = wp_table.open_context_menu_for(parent)context_menu.choose(I18n.t('js.relation_buttons.add_new_child'))split_view_create = Pages::SplitWorkPackageCreate.new(project:)- split_view_create.set_attributes({ subject: 'child 2', estimatedTime: 1, remainingTime: 3 })+ split_view_create.set_attributes({ subject: 'new child', estimatedTime: 1, remainingTime: 3 })split_view_create.save!split_view_create.expect_and_dismiss_toaster message: 'Successful creation'parent.reloadnew_child = WorkPackage.last- expect(parent.derived_estimated_hours).to eq(parent.estimated_hours + child.estimated_hours + new_child.estimated_hours)- expect(parent.derived_remaining_hours).to eq(parent.remaining_hours + child.remaining_hours + new_child.remaining_hours)+ expect(parent.derived_estimated_hours).to eq([parent, child, second_child, new_child].pluck(:estimated_hours).sum)+ expect(parent.derived_remaining_hours).to eq([parent, child, second_child, new_child].pluck(:remaining_hours).sum)+ # child + second child + new child => parent+ # 6h at 50% + 3h at 0% + 1h at 0% => 30% complete for 10h+ expect(parent.done_ratio).to eq(30)endendcontext 'when outdenting and indenting hierarchy of a child' doit 'updates the parent work and remaining work values' do- context_menu = wp_table.open_context_menu_for(child)+ context_menu = wp_table.open_context_menu_for(second_child)context_menu.choose(I18n.t('js.relation_buttons.hierarchy_outdent'))wp_table.expect_and_dismiss_toaster message: 'Successful update'parent.reload- expect(parent.derived_estimated_hours).to eq(parent.estimated_hours)- expect(parent.derived_remaining_hours).to eq(parent.remaining_hours)+ expect(parent.derived_estimated_hours).to eq([parent, child].pluck(:estimated_hours).sum)+ expect(parent.derived_remaining_hours).to eq([parent, child].pluck(:remaining_hours).sum)+ expect(parent.done_ratio).to eq(child.done_ratio)- context_menu = wp_table.open_context_menu_for(child)+ context_menu = wp_table.open_context_menu_for(second_child)context_menu.choose(I18n.t('js.relation_buttons.hierarchy_indent'))wp_table.expect_and_dismiss_toaster message: 'Successful update'parent.reload- expect(parent.derived_estimated_hours).to eq(parent.estimated_hours + child.estimated_hours)- expect(parent.derived_remaining_hours).to eq(parent.remaining_hours + child.remaining_hours)+ expect(parent.derived_estimated_hours).to eq([parent, child, second_child].pluck(:estimated_hours).sum)+ expect(parent.derived_remaining_hours).to eq([parent, child, second_child].pluck(:remaining_hours).sum)+ # child + second child => parent+ # 6h at 50% + 3h at 0% => 33% complete for 9h+ expect(parent.done_ratio).to eq(33)endendenddiff --git a/spec/services/work_packages/update_ancestors_service_spec.rb b/spec/services/work_packages/update_ancestors_service_spec.rbindex e91f3f18f7a..3e377e1eff2 100644--- a/spec/services/work_packages/update_ancestors_service_spec.rb+++ b/spec/services/work_packages/update_ancestors_service_spec.rb@@ -63,6 +63,39 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model doendend+ context 'when setting closed status to a work package' do+ shared_let(:parent) { create(:work_package) }+ shared_let(:child) { create(:work_package, parent:, status: open_status) }++ shared_examples 'updates % complete of ancestors' do+ it 'considers the work package as 100 % complete and sets the % complete value of the ancestors accordingly' do+ expect do+ updated_attributes = child.changes.keys.map(&:to_sym)+ described_class.new(user:, work_package: child)+ .call(updated_attributes)+ parent.reload+ end+ .to change(parent, :done_ratio).from(0).to(100)+ end+ end++ context 'with the status field' do+ before do+ child.status = closed_status+ end++ include_examples 'updates % complete of ancestors'+ end++ context 'with the status_id field' do+ before do+ child.status_id = closed_status.id+ end++ include_examples 'updates % complete of ancestors'+ end+ end+context 'for the new ancestor chain' doshared_examples 'attributes of parent having children' dobefore do