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
[38030] Add "Parent" item to the relations tab "Add relation" menu
Christophe Bliard43ca98ceb4f7
diff --git a/app/components/work_package_relations_tab/add_work_package_child_dialog_component.html.erb b/app/components/work_package_relations_tab/add_work_package_hierarchy_dialog_component.html.erbsimilarity index 86%rename from app/components/work_package_relations_tab/add_work_package_child_dialog_component.html.erbrename to app/components/work_package_relations_tab/add_work_package_hierarchy_dialog_component.html.erbindex 50d11aaecdc..16fbcb6ca63 100644--- a/app/components/work_package_relations_tab/add_work_package_child_dialog_component.html.erb+++ b/app/components/work_package_relations_tab/add_work_package_hierarchy_dialog_component.html.erb@@ -9,8 +9,9 @@d.with_header(variant: :large)d.with_body(classes: body_classes) dorender(- WorkPackageRelationsTab::AddWorkPackageChildFormComponent.new(- work_package: @work_package+ WorkPackageRelationsTab::AddWorkPackageHierarchyFormComponent.new(+ work_package:,+ relation_type:))enddiff --git a/app/components/work_package_relations_tab/add_work_package_child_dialog_component.rb b/app/components/work_package_relations_tab/add_work_package_hierarchy_dialog_component.rbsimilarity index 76%rename from app/components/work_package_relations_tab/add_work_package_child_dialog_component.rbrename to app/components/work_package_relations_tab/add_work_package_hierarchy_dialog_component.rbindex fc7e09dee8d..6736fb028e6 100644--- a/app/components/work_package_relations_tab/add_work_package_child_dialog_component.rb+++ b/app/components/work_package_relations_tab/add_work_package_hierarchy_dialog_component.rb@@ -28,28 +28,29 @@# See COPYRIGHT and LICENSE files for more details.#++-class WorkPackageRelationsTab::AddWorkPackageChildDialogComponent < ApplicationComponent+class WorkPackageRelationsTab::AddWorkPackageHierarchyDialogComponent < ApplicationComponentinclude ApplicationHelperinclude OpTurbo::Streamableinclude OpPrimer::ComponentHelpersI18N_NAMESPACE = "work_package_relations_tab"- DIALOG_ID = "add-work-package-child-dialog"- FORM_ID = "add-work-package-child-form"+ DIALOG_ID = "add-work-package-hierarchy-dialog"+ FORM_ID = "add-work-package-hierarchy-form"- attr_reader :work_package+ attr_reader :work_package, :relation_type- def initialize(work_package:)+ def initialize(work_package:, relation_type:)super()@work_package = work_package+ @relation_type = relation_typeendprivatedef dialog_title- child_label = t("#{I18N_NAMESPACE}.relations.label_child_singular")- t("#{I18N_NAMESPACE}.label_add_x", x: child_label)+ hierarchy_label = t("#{I18N_NAMESPACE}.relations.label_#{relation_type}_singular")+ t("#{I18N_NAMESPACE}.label_add_x", x: hierarchy_label)enddef body_classesdiff --git a/app/components/work_package_relations_tab/add_work_package_child_form_component.html.erb b/app/components/work_package_relations_tab/add_work_package_hierarchy_form_component.html.erbsimilarity index 87%rename from app/components/work_package_relations_tab/add_work_package_child_form_component.html.erbrename to app/components/work_package_relations_tab/add_work_package_hierarchy_form_component.html.erbindex 75bb565b544..ed58423475b 100644--- a/app/components/work_package_relations_tab/add_work_package_child_form_component.html.erb+++ b/app/components/work_package_relations_tab/add_work_package_hierarchy_form_component.html.erb@@ -2,7 +2,7 @@<%=primer_form_with(id: FORM_ID,- model: @child,+ model: related,**submit_url_options,data: {turbo: true,@@ -14,14 +14,14 @@<%=flex_layout(mb: 3) do |flex|flex.with_row do- if @base_errors&.any?- render(Primer::Alpha::Banner.new(mb: 3, icon: :stop, scheme: :danger)) { @base_errors.join("\n") }+ if base_errors.any?+ render(Primer::Alpha::Banner.new(mb: 3, icon: :stop, scheme: :danger)) { base_errors.join("\n") }endendflex.with_row do# @workpackage is not available inside the render_inline_form block# so we need to re-define them here. Figure out solution for this.- url = ::API::V3::Utilities::PathHelper::ApiV3Path.work_package_available_relation_candidates(@work_package.id, type: Relation::TYPE_CHILD)+ url = ::API::V3::Utilities::PathHelper::ApiV3Path.work_package_available_relation_candidates(work_package.id, type: relation_type)render_inline_form(f) do |my_form|my_form.work_package_autocompleter(name: :id,diff --git a/app/components/work_package_relations_tab/add_work_package_child_form_component.rb b/app/components/work_package_relations_tab/add_work_package_hierarchy_form_component.rbsimilarity index 71%rename from app/components/work_package_relations_tab/add_work_package_child_form_component.rbrename to app/components/work_package_relations_tab/add_work_package_hierarchy_form_component.rbindex b6d2a7cef41..da06c1f536a 100644--- a/app/components/work_package_relations_tab/add_work_package_child_form_component.rb+++ b/app/components/work_package_relations_tab/add_work_package_hierarchy_form_component.rb@@ -28,26 +28,29 @@# See COPYRIGHT and LICENSE files for more details.#++-class WorkPackageRelationsTab::AddWorkPackageChildFormComponent < ApplicationComponent+class WorkPackageRelationsTab::AddWorkPackageHierarchyFormComponent < ApplicationComponentinclude ApplicationHelperinclude OpTurbo::Streamableinclude OpPrimer::ComponentHelpers- DIALOG_ID = "add-work-package-child-dialog"- FORM_ID = "add-work-package-child-form"- ID_FIELD_TEST_SELECTOR = "work-package-child-form-id"+ DIALOG_ID = "add-work-package-hierarchy-dialog"+ FORM_ID = "add-work-package-hierarchy-form"+ ID_FIELD_TEST_SELECTOR = "work-package-hierarchy-form-id"I18N_NAMESPACE = "work_package_relations_tab"- def initialize(work_package:, child: nil, base_errors: nil)+ attr_reader :work_package, :relation_type, :related, :base_errors++ def initialize(work_package:, relation_type:, related: nil, base_errors: nil)super()@work_package = work_package- @child = child.presence || WorkPackage.new- @base_errors = base_errors+ @relation_type = relation_type+ @related = related.presence || WorkPackage.new+ @base_errors = base_errors || []enddef submit_url_options{ method: :post,- url: work_package_children_relations_path(@work_package) }+ url: work_package_hierarchy_relations_path(work_package, relation_type:) }endenddiff --git a/app/components/work_package_relations_tab/index_component.html.erb b/app/components/work_package_relations_tab/index_component.html.erbindex 1b6e2ee3ab5..7493233286a 100644--- a/app/components/work_package_relations_tab/index_component.html.erb+++ b/app/components/work_package_relations_tab/index_component.html.erb@@ -12,8 +12,8 @@action_bar.with_column(flex_shrink: 0, ml: 2) dorender(Primer::Alpha::ActionMenu.new(- test_selector: NEW_RELATION_ACTION_MENU,- menu_id: NEW_RELATION_ACTION_MENU+ test_selector: ADD_RELATION_ACTION_MENU,+ menu_id: ADD_RELATION_ACTION_MENU)) do |menu|menu.with_show_button do |button|diff --git a/app/components/work_package_relations_tab/index_component.rb b/app/components/work_package_relations_tab/index_component.rbindex bdb454b23cc..82d94b1ee5a 100644--- a/app/components/work_package_relations_tab/index_component.rb+++ b/app/components/work_package_relations_tab/index_component.rb@@ -8,8 +8,8 @@# - Child work packagesclass WorkPackageRelationsTab::IndexComponent < ApplicationComponentFRAME_ID = "work-package-relations-tab-content"- NEW_RELATION_ACTION_MENU = "new-relation-action-menu"- NEW_CHILD_ACTION_MENU = "new-child-action-menu"+ ADD_RELATION_ACTION_MENU = "add-relation-action-menu"+ ADD_CHILD_ACTION_MENU = "add-child-action-menu"I18N_NAMESPACE = "work_package_relations_tab"ADD_CHILD_MENU_TYPES = [@@ -22,6 +22,7 @@ class WorkPackageRelationsTab::IndexComponent < ApplicationComponentRelation::TYPE_RELATES,Relation::TYPE_FOLLOWS,Relation::TYPE_PRECEDES,+ Relation::TYPE_PARENT,Relation::TYPE_DUPLICATES,Relation::TYPE_DUPLICATED,Relation::TYPE_BLOCKS,@@ -65,15 +66,23 @@ class WorkPackageRelationsTab::IndexComponent < ApplicationComponentdef should_render_add_child?return false if work_package.milestone?- helpers.current_user.allowed_in_project?(:manage_subtasks, work_package.project)+ allowed_to?(:manage_subtasks)+ end++ def should_render_add_parent?+ allowed_to?(:manage_subtasks)enddef should_render_add_relations?- helpers.current_user.allowed_in_project?(:manage_work_package_relations, work_package.project)+ allowed_to?(:manage_work_package_relations)+ end++ def allowed_to?(permission)+ helpers.current_user.allowed_in_project?(permission, work_package.project)enddef should_render_create_button?- should_render_add_child? || should_render_add_relations?+ should_render_add_child? || should_render_add_parent? || should_render_add_relations?enddef render_relation_group(title:, relation_group:, &)@@ -106,7 +115,7 @@ class WorkPackageRelationsTab::IndexComponent < ApplicationComponentconcat render(Primer::Beta::Counter.new(count:, round: true, scheme: :primary))endheader.with_column do- render(Primer::Alpha::ActionMenu.new(menu_id: NEW_CHILD_ACTION_MENU)) do |menu|+ render(Primer::Alpha::ActionMenu.new(menu_id: ADD_CHILD_ACTION_MENU)) do |menu|menu.with_show_button do |button|button.with_leading_visual_icon(icon: :plus)button.with_trailing_action_icon(icon: :"triangle-down")@@ -146,9 +155,11 @@ class WorkPackageRelationsTab::IndexComponent < ApplicationComponentdef can_add_relation?(relation_type)case relation_typewhen "new_child"- should_render_add_child? && helpers.current_user.allowed_in_project?(:add_work_packages, work_package.project)+ should_render_add_child? && allowed_to?(:add_work_packages)when Relation::TYPE_CHILDshould_render_add_child?+ when Relation::TYPE_PARENT+ should_render_add_parent?when *Relation::TYPES.keysshould_render_add_relations?else@@ -205,8 +216,8 @@ class WorkPackageRelationsTab::IndexComponent < ApplicationComponentcase relation_typewhen "new_child"new_project_work_packages_dialog_path(work_package.project, parent_id: work_package.id)- when Relation::TYPE_CHILD- new_work_package_children_relation_path(work_package)+ when Relation::TYPE_CHILD, Relation::TYPE_PARENT+ new_work_package_hierarchy_relation_path(work_package, relation_type:)when *Relation::TYPES.keysnew_work_package_relation_path(work_package, relation_type:)elsediff --git a/app/components/work_package_relations_tab/index_component.sass b/app/components/work_package_relations_tab/index_component.sassindex 03475619c65..64111497b3e 100644--- a/app/components/work_package_relations_tab/index_component.sass+++ b/app/components/work_package_relations_tab/index_component.sass@@ -1,6 +1,6 @@// We reference an ID as one is required to be specified for the action menu list.// It can't be nested inside the BEM model as it's placed as a #top-layer element.-#new-relation-action-menu-list,-#new-child-action-menu-list- max-height: 470px+#add-relation-action-menu-list,+#add-child-action-menu-list+ max-height: 510pxmax-width: 280pxdiff --git a/app/components/work_package_relations_tab/relation_component.rb b/app/components/work_package_relations_tab/relation_component.rbindex 28447099284..5550abf1b10 100644--- a/app/components/work_package_relations_tab/relation_component.rb+++ b/app/components/work_package_relations_tab/relation_component.rb@@ -55,17 +55,17 @@ class WorkPackageRelationsTab::RelationComponent < ApplicationComponentprivate- def parent_child_relationship? = relation.nil?+ def hierarchy_relationship? = relation.nil?def should_render_edit_option?# Children and parent can not be edited as it's not a relation.- !parent_child_relationship? && allowed_to_manage_relations?+ !hierarchy_relationship? && allowed_to_manage_relations?enddef should_render_action_menu?return false unless editable?- if parent_child_relationship?+ if hierarchy_relationship?allowed_to_manage_subtasks?elseallowed_to_manage_relations?@@ -82,7 +82,7 @@ class WorkPackageRelationsTab::RelationComponent < ApplicationComponentenddef should_display_description?- return false if parent_child_relationship?+ return false if hierarchy_relationship?relation.description.present?end@@ -92,38 +92,32 @@ class WorkPackageRelationsTab::RelationComponent < ApplicationComponentenddef should_display_dates_row?- parent_child_relationship? || relation.follows? || relation.precedes?+ hierarchy_relationship? || relation.follows? || relation.precedes?enddef follows?- return false if parent_child_relationship?+ return false if hierarchy_relationship?relation.relation_type_for(work_package) == Relation::TYPE_FOLLOWSenddef precedes?- return false if parent_child_relationship?+ return false if hierarchy_relationship?relation.relation_type_for(work_package) == Relation::TYPE_PRECEDESenddef edit_path- if parent_child_relationship?- raise NotImplementedError, "Children relationships are not editable"+ if hierarchy_relationship?+ raise NotImplementedError, "Children and parent relationships are not editable"elseedit_work_package_relation_path(work_package, relation)endenddef destroy_path- if parent_child_relationship?- parent, child =- if relation_item.type.child?- [work_package, related_work_package]- else- [related_work_package, work_package]- end- work_package_children_relation_path(parent, child, relation_type: relation_item.type)+ if hierarchy_relationship?+ work_package_hierarchy_relation_path(work_package, related_work_package)elsework_package_relation_path(work_package, relation)enddiff --git a/app/controllers/work_package_children_relations_controller.rb b/app/controllers/work_package_hierarchy_relations_controller.rbsimilarity index 61%rename from app/controllers/work_package_children_relations_controller.rbrename to app/controllers/work_package_hierarchy_relations_controller.rbindex 470fa42915e..262750112f7 100644--- a/app/controllers/work_package_children_relations_controller.rb+++ b/app/controllers/work_package_hierarchy_relations_controller.rb@@ -28,28 +28,36 @@# See COPYRIGHT and LICENSE files for more details.#++-class WorkPackageChildrenRelationsController < ApplicationController+class WorkPackageHierarchyRelationsController < ApplicationControllerinclude OpTurbo::ComponentStreaminclude OpTurbo::DialogStreamHelper- before_action :set_work_package+ class InvalidRelationType < StandardError; end+ before_action :set_work_packagebefore_action :authorize # Short-circuit early if not authorized+ rescue_from InvalidRelationType do |error|+ render_error(message: error.message, status: 422)+ end+def new- component = WorkPackageRelationsTab::AddWorkPackageChildDialogComponent- .new(work_package: @work_package)+ component = WorkPackageRelationsTab::AddWorkPackageHierarchyDialogComponent+ .new(work_package: @work_package, relation_type:)respond_with_dialog(component)enddef create- service_result = create_service_result+ service_result = create_hierarchy_associationif service_result.failure?update_via_turbo_stream(- component: WorkPackageRelationsTab::AddWorkPackageChildFormComponent.new(work_package: @work_package,- child: service_result.result,- base_errors: base_errors(service_result.errors)),+ component: WorkPackageRelationsTab::AddWorkPackageHierarchyFormComponent.new(+ work_package: @work_package,+ relation_type:,+ related: related_work_package,+ base_errors: extract_base_errors(service_result.errors)+ ),status: :bad_request)end@@ -58,25 +66,48 @@ class WorkPackageChildrenRelationsController < ApplicationControllerenddef destroy- @child = WorkPackage.find(params[:id])- service_result = set_relation(child: @child, parent: nil)-+ related = WorkPackage.find(params[:id])+ service_result =+ if related.parent_id == @work_package.id+ set_relation(child: related, parent: nil)+ elsif @work_package.parent_id == related.id+ set_relation(child: @work_package, parent: nil)+ endrespond_with_relations_tab_update(service_result)endprivate- def create_service_result- if params[:work_package][:id].present?- child = WorkPackage.find(params[:work_package][:id])- set_relation(child:, parent: @work_package)+ def create_hierarchy_association+ if related_work_package.id.blank?+ related_work_package.errors.add(:id, :blank)+ return ServiceResult.failure(result: related_work_package)+ end++ if relation_type == "child"+ set_relation(parent: @work_package, child: related_work_package)else- child = WorkPackage.new- child.errors.add(:id, :blank)- ServiceResult.failure(result: child)+ set_relation(child: @work_package, parent: related_work_package)endend+ def relation_type+ type = params[:relation_type]+ raise InvalidRelationType, "Missing relation_type parameter" if type.blank?+ raise InvalidRelationType, "Invalid relation type: #{type}" unless type.in?([Relation::TYPE_PARENT, Relation::TYPE_CHILD])++ type+ end++ def related_work_package+ @related_work_package ||=+ if params[:work_package][:id].present?+ WorkPackage.find(params[:work_package][:id])+ else+ WorkPackage.new+ end+ end+def set_relation(child:, parent:)if allowed_to_set_parent?(child)WorkPackages::UpdateService.new(@@ -97,9 +128,8 @@ class WorkPackageChildrenRelationsController < ApplicationControllerdef respond_with_relations_tab_update(service_result, **)if service_result.success?- rendered_work_package = params[:relation_type] == "parent" && @child ? @child : @work_package- rendered_work_package.reload- component = WorkPackageRelationsTab::IndexComponent.new(work_package: rendered_work_package, **)+ @work_package.reload+ component = WorkPackageRelationsTab::IndexComponent.new(work_package: @work_package, **)replace_via_turbo_stream(component:)render_success_flash_message_via_turbo_stream(message: I18n.t(:notice_successful_update))@@ -114,7 +144,7 @@ class WorkPackageChildrenRelationsController < ApplicationController@project = @work_package.projectend- def base_errors(errors)+ def extract_base_errors(errors)if errors[:base].present?errors[:base]elsif errors[:id].present?diff --git a/config/initializers/permissions.rb b/config/initializers/permissions.rbindex f6fea4b05ef..caa47166e23 100644--- a/config/initializers/permissions.rb+++ b/config/initializers/permissions.rb@@ -381,7 +381,7 @@ Rails.application.reloader.to_prepare dowpt.permission :manage_subtasks,{- work_package_children_relations: %i[new create destroy]+ work_package_hierarchy_relations: %i[new create destroy]},permissible_on: :project,dependencies: :view_work_packagesdiff --git a/config/locales/en.yml b/config/locales/en.ymlindex bee3ba56a3c..6046fbccef4 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -815,6 +815,8 @@ en:new_child_description: "Creates a related work package as a sub-item of the current (parent) work package"child: "Child"child_description: "Makes the related work package a sub-item of the current (parent) work package"+ parent: "Parent"+ parent_description: "Makes the related work package a parent of the current (child) work package"label_closest: "Closest"label_blocks_singular: "blocks"label_blocks_plural: "blocks"diff --git a/config/routes.rb b/config/routes.rbindex a9d85dc8de8..0d9b310e3cb 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -662,7 +662,7 @@ Rails.application.routes.draw doendend- resources :children_relations, only: %i[new create destroy], controller: "work_package_children_relations"+ resources :hierarchy_relations, only: %i[new create destroy], controller: "work_package_hierarchy_relations"resource :progress, only: %i[new edit update], controller: "work_packages/progress"collection dodiff --git a/spec/components/work_package_relations_tab/add_work_package_hierarchy_dialog_component_spec.rb b/spec/components/work_package_relations_tab/add_work_package_hierarchy_dialog_component_spec.rbnew file mode 100644index 00000000000..64fc6d70da1--- /dev/null+++ b/spec/components/work_package_relations_tab/add_work_package_hierarchy_dialog_component_spec.rb@@ -0,0 +1,83 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 WorkPackageRelationsTab::AddWorkPackageHierarchyDialogComponent, type: :component do+ include Rails.application.routes.url_helpers++ let(:work_package) { build_stubbed(:work_package) }++ subject { described_class.new(work_package:, relation_type:) }++ context "when adding a child" do+ let(:relation_type) { Relation::TYPE_CHILD }++ it "displays the dialog with 'Add child' title" do+ render_inline(subject)++ expect(page).to have_text("Add child")+ end++ it "uses work package autocompleter with url having available_relation_candidates?type=child" do+ rendered = render_inline(subject)+ expect(rendered.to_html).to include("available_relation_candidates?type=child")+ end++ it "has relation_type=child as a parameter of the form action" do+ render_inline(subject)++ form = page.find("form")+ expect(form["action"]).to eq(work_package_hierarchy_relations_path(work_package, relation_type: "child"))+ end+ end++ context "when adding a parent" do+ let(:relation_type) { Relation::TYPE_PARENT }++ it "displays the dialog with 'Add parent' title" do+ render_inline(subject)++ expect(page).to have_text("Add parent")+ end++ it "uses work package autocompleter with url having available_relation_candidates?type=parent" do+ rendered = render_inline(subject)+ expect(rendered.to_html).to include("available_relation_candidates?type=parent")+ end++ it "has relation_type=parent as a parameter of the form action" do+ render_inline(subject)++ form = page.find("form")+ expect(form["action"]).to eq(work_package_hierarchy_relations_path(work_package, relation_type: "parent"))+ end+ end+enddiff --git a/spec/controllers/work_package_children_relations_controller_spec.rb b/spec/controllers/work_package_children_relations_controller_spec.rbdeleted file mode 100644index dacaf8ff55f..00000000000--- a/spec/controllers/work_package_children_relations_controller_spec.rb+++ /dev/null@@ -1,172 +0,0 @@-# frozen_string_literal: true--#-- copyright-# OpenProject is an open source project management software.-# Copyright (C) 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"--RSpec.describe WorkPackageChildrenRelationsController do- shared_let(:user) { create(:admin) }- shared_let(:task_type) { create(:type_task) }- shared_let(:milestone_type) { create(:type_milestone) }- shared_let(:project) { create(:project, types: [task_type, milestone_type]) }- shared_let(:work_package) { create(:work_package, subject: "work_package", project:, type: task_type) }- shared_let(:child_work_package) { create(:work_package, subject: "child", parent: work_package, project:, type: task_type) }-- current_user { user }-- describe "GET /work_packages/:work_package_id/children_relations/new" do- before do- allow(WorkPackageRelationsTab::AddWorkPackageChildDialogComponent)- .to receive(:new)- .with(work_package:)- .and_call_original- end-- it "renders the new template" do- get("new", params: { work_package_id: work_package.id }, as: :turbo_stream)- expect(response).to be_successful- expect(WorkPackageRelationsTab::AddWorkPackageChildDialogComponent)- .to have_received(:new)- .with(work_package:)- end- end-- describe "POST /work_packages/:work_package_id/children_relations" do- shared_let(:future_child_work_package) { create(:work_package, project:) }-- it "creates a child relationship" do- post("create", params: { work_package_id: work_package.id,- work_package: { id: future_child_work_package.id } },- as: :turbo_stream)- expect(response).to have_http_status(:ok)- expect(future_child_work_package.reload.parent).to eq(work_package)- end-- it "can't create a child relationship for a milestone work package" do- work_package.update(type: milestone_type)- post("create", params: { work_package_id: work_package.id,- work_package: { id: future_child_work_package.id } },- as: :turbo_stream)- expect(response).to have_http_status(:unprocessable_entity)- expect(future_child_work_package.reload.parent).to be_nil- end-- context "when the child is invalid due to a required custom field" do- shared_let(:custom_field) do- create(:integer_wp_custom_field, is_required: true, is_for_all: true, default_value: nil) do |cf|- project.types.first.custom_fields << cf- project.work_package_custom_fields << cf- end- end-- it "the creation call still succeeds" do- post("create", params: { work_package_id: work_package.id,- work_package: { id: future_child_work_package.id } },- as: :turbo_stream)- expect(response).to have_http_status(:ok)- expect(future_child_work_package.reload.parent).to eq(work_package)- end- end- end-- describe "DELETE /work_packages/:work_package_id/children/:id" do- let(:relation_type) { nil }-- def send_delete_request- delete("destroy",- params: { work_package_id: work_package.id,- id: child_work_package.id,- relation_type: },- as: :turbo_stream)- end-- it "deletes the child relationship" do- send_delete_request-- expect(response).to have_http_status(:ok)- expect(child_work_package.reload.parent).to be_nil- end-- it "renders the relations tab index component" do- allow(WorkPackageRelationsTab::IndexComponent).to receive(:new).and_call_original- allow(controller).to receive(:replace_via_turbo_stream).and_call_original-- send_delete_request-- expect(WorkPackageRelationsTab::IndexComponent).to have_received(:new)- .with(work_package:)- expect(controller).to have_received(:replace_via_turbo_stream)- .with(component: an_instance_of(WorkPackageRelationsTab::IndexComponent))- end-- it "updates dependent work packages" do- allow(WorkPackages::UpdateAncestorsService).to receive(:new).and_call_original- allow(WorkPackages::SetScheduleService).to receive(:new).and_call_original-- send_delete_request-- expect(WorkPackages::UpdateAncestorsService).to have_received(:new)- .with(user: user, work_package: child_work_package)- expect(WorkPackages::SetScheduleService).to have_received(:new)- .with(a_hash_including(work_package: [child_work_package, work_package]))- end-- context "when the child is invalid due to a required custom field" do- shared_let(:custom_field) do- create(:integer_wp_custom_field, is_required: true, is_for_all: true, default_value: nil) do |cf|- project.types.first.custom_fields << cf- project.work_package_custom_fields << cf- end- end-- it "the creation call still succeeds" do- send_delete_request-- expect(response).to have_http_status(:ok)- expect(child_work_package.reload.parent).to be_nil- end- end-- context "when the relation type is parent" do- let(:relation_type) { "parent" }-- it "deletes the parent relationship too but renders the relations tab index component with the child" do- allow(WorkPackageRelationsTab::IndexComponent).to receive(:new).and_call_original- allow(controller).to receive(:replace_via_turbo_stream).and_call_original-- send_delete_request-- expect(WorkPackageRelationsTab::IndexComponent).to have_received(:new)- .with(work_package: child_work_package)- expect(controller).to have_received(:replace_via_turbo_stream)- .with(component: an_instance_of(WorkPackageRelationsTab::IndexComponent))- end- end- end-enddiff --git a/spec/controllers/work_package_hierarchy_relations_controller_spec.rb b/spec/controllers/work_package_hierarchy_relations_controller_spec.rbnew file mode 100644index 00000000000..8a7eed65fd1--- /dev/null+++ b/spec/controllers/work_package_hierarchy_relations_controller_spec.rb@@ -0,0 +1,268 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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"++RSpec.describe WorkPackageHierarchyRelationsController do+ shared_let(:user) { create(:admin) }+ shared_let(:task_type) { create(:type_task) }+ shared_let(:milestone_type) { create(:type_milestone) }+ shared_let(:project) { create(:project, types: [task_type, milestone_type]) }+ shared_let(:parent_work_package) { create(:work_package, subject: "parent", project:, type: task_type) }+ shared_let(:work_package) do+ create(:work_package, subject: "work_package", parent: parent_work_package, project:, type: task_type)+ end+ shared_let(:child_work_package) { create(:work_package, subject: "child", parent: work_package, project:, type: task_type) }++ current_user { user }++ describe "GET /work_packages/:work_package_id/hierarchy_relations/new" do+ before do+ allow(WorkPackageRelationsTab::AddWorkPackageHierarchyDialogComponent)+ .to receive(:new)+ .and_call_original+ end++ it "renders the add child/parent dialog component via turbo stream according to the relation_type parameter" do+ # child+ get("new", params: { work_package_id: work_package.id, relation_type: "child" }, as: :turbo_stream)+ expect(response).to be_successful+ expect(WorkPackageRelationsTab::AddWorkPackageHierarchyDialogComponent)+ .to have_received(:new)+ .with(work_package:, relation_type: Relation::TYPE_CHILD)++ # parent+ get("new", params: { work_package_id: work_package.id, relation_type: "parent" }, as: :turbo_stream)+ expect(response).to be_successful+ expect(WorkPackageRelationsTab::AddWorkPackageHierarchyDialogComponent)+ .to have_received(:new)+ .with(work_package:, relation_type: Relation::TYPE_PARENT)+ end++ context "when the indicated relation type is not valid (must be parent or child)" do+ it "replies with 422" do+ get("new", params: { work_package_id: work_package.id, relation_type: "invalid" }, as: :turbo_stream)+ expect(response).to have_http_status(:unprocessable_entity)+ end+ end++ context "when the relation type is not provided" do+ it "replies with 422" do+ get("new", params: { work_package_id: work_package.id }, as: :turbo_stream)+ expect(response).to have_http_status(:unprocessable_entity)+ end+ end+ end++ describe "POST /work_packages/:work_package_id/hierarchy_relations" do+ context "when the relation type is child" do+ shared_let(:future_child_work_package) { create(:work_package, subject: "future_child", project:) }+ let(:relation_type) { "child" }++ it "creates a child relationship" do+ post("create", params: { work_package_id: work_package.id,+ work_package: { id: future_child_work_package.id },+ relation_type: },+ as: :turbo_stream)+ expect(response).to have_http_status(:ok)+ expect(future_child_work_package.reload.parent).to eq(work_package)+ end++ it "can't create a child relationship for a milestone work package" do+ work_package.update(type: milestone_type)+ post("create", params: { work_package_id: work_package.id,+ work_package: { id: future_child_work_package.id },+ relation_type: },+ as: :turbo_stream)+ expect(response).to have_http_status(:unprocessable_entity)+ expect(future_child_work_package.reload.parent).to be_nil+ end++ context "when the child is invalid due to a required custom field" do+ shared_let(:custom_field) do+ create(:integer_wp_custom_field, is_required: true, is_for_all: true, default_value: nil) do |cf|+ project.types.first.custom_fields << cf+ project.work_package_custom_fields << cf+ end+ end++ it "the creation call still succeeds" do+ post("create", params: { work_package_id: work_package.id,+ work_package: { id: future_child_work_package.id },+ relation_type: },+ as: :turbo_stream)+ expect(response).to have_http_status(:ok)+ expect(future_child_work_package.reload.parent).to eq(work_package)+ end+ end+ end++ context "when the relation type is parent" do+ shared_let(:future_parent_work_package) { create(:work_package, subject: "future_parent", project:) }+ let(:relation_type) { "parent" }++ it "creates a parent relationship" do+ post("create", params: { work_package_id: work_package.id,+ work_package: { id: future_parent_work_package.id },+ relation_type: },+ as: :turbo_stream)+ expect(response).to have_http_status(:ok)+ expect(work_package.reload.parent).to eq(future_parent_work_package)+ end++ it "can create a parent relationship for a milestone work package" do+ work_package.update(type: milestone_type)+ post("create", params: { work_package_id: work_package.id,+ work_package: { id: future_parent_work_package.id },+ relation_type: },+ as: :turbo_stream)+ expect(response).to have_http_status(:ok)+ expect(work_package.reload.parent).to eq(future_parent_work_package)+ end++ it "can not select a milestone work package as parent" do+ future_parent_work_package.update(type: milestone_type)+ post("create", params: { work_package_id: work_package.id,+ work_package: { id: future_parent_work_package.id },+ relation_type: },+ as: :turbo_stream)+ expect(response).to have_http_status(:unprocessable_entity)+ # it's still parent_work_package and not future_parent_work_package+ expect(work_package.reload.parent).to eq(parent_work_package)+ end++ context "when the work package is invalid due to a required custom field" do+ shared_let(:custom_field) do+ create(:integer_wp_custom_field, is_required: true, is_for_all: true, default_value: nil) do |cf|+ project.types.first.custom_fields << cf+ project.work_package_custom_fields << cf+ end+ end++ it "the creation call still succeeds" do+ post("create", params: { work_package_id: work_package.id,+ work_package: { id: future_parent_work_package.id },+ relation_type: },+ as: :turbo_stream)+ expect(response).to have_http_status(:ok)+ expect(work_package.reload.parent).to eq(future_parent_work_package)+ end+ end+ end+ end++ describe "DELETE /work_packages/:work_package_id/children/:id" do+ def send_delete_request(related:)+ delete("destroy",+ params: { work_package_id: work_package.id,+ id: related.id },+ as: :turbo_stream)+ end++ it "removes the hierarchy relationship between both work packages" do+ # remove a child+ send_delete_request(related: child_work_package)++ expect(response).to have_http_status(:ok)+ expect(child_work_package.reload.parent).to be_nil++ # remove the parent+ send_delete_request(related: parent_work_package)++ expect(response).to have_http_status(:ok)+ expect(work_package.reload.parent).to be_nil+ end++ it "renders the relations tab index component with the concerned work package" do+ allow(WorkPackageRelationsTab::IndexComponent).to receive(:new).and_call_original+ allow(controller).to receive(:replace_via_turbo_stream).and_call_original++ # remove a child+ send_delete_request(related: child_work_package)++ expect(WorkPackageRelationsTab::IndexComponent).to have_received(:new)+ .with(work_package:)+ expect(controller).to have_received(:replace_via_turbo_stream)+ .with(component: an_instance_of(WorkPackageRelationsTab::IndexComponent))++ # remove the parent+ send_delete_request(related: parent_work_package)++ expect(WorkPackageRelationsTab::IndexComponent).to have_received(:new)+ .with(work_package:).twice+ expect(controller).to have_received(:replace_via_turbo_stream)+ .with(component: an_instance_of(WorkPackageRelationsTab::IndexComponent)).twice+ end++ it "updates dependent work packages" do+ allow(WorkPackages::UpdateAncestorsService).to receive(:new).and_call_original+ allow(WorkPackages::SetScheduleService).to receive(:new).and_call_original++ # remove a child+ send_delete_request(related: child_work_package)++ expect(WorkPackages::UpdateAncestorsService).to have_received(:new)+ .with(user: user, work_package: child_work_package)+ expect(WorkPackages::SetScheduleService).to have_received(:new)+ .with(a_hash_including(work_package: [child_work_package, work_package]))++ # remove the parent+ send_delete_request(related: parent_work_package)++ expect(WorkPackages::UpdateAncestorsService).to have_received(:new)+ .with(user: user, work_package:)+ expect(WorkPackages::SetScheduleService).to have_received(:new)+ .with(a_hash_including(work_package: [work_package, parent_work_package]))+ end++ context "when the child is invalid due to a required custom field" do+ shared_let(:custom_field) do+ create(:integer_wp_custom_field, is_required: true, is_for_all: true, default_value: nil) do |cf|+ project.types.first.custom_fields << cf+ project.work_package_custom_fields << cf+ end+ end++ it "the deletion call still succeeds" do+ # remove a child+ send_delete_request(related: child_work_package)++ expect(response).to have_http_status(:ok)+ expect(child_work_package.reload.parent).to be_nil++ # remove the parent+ send_delete_request(related: parent_work_package)++ expect(response).to have_http_status(:ok)+ expect(work_package.reload.parent).to be_nil+ end+ end+ end+enddiff --git a/spec/features/work_packages/details/relations/primerized_relations_spec.rb b/spec/features/work_packages/details/relations/primerized_relations_spec.rbindex 8c7f8ef328a..dcd5aa02a78 100644--- a/spec/features/work_packages/details/relations/primerized_relations_spec.rb+++ b/spec/features/work_packages/details/relations/primerized_relations_spec.rb@@ -96,12 +96,6 @@ RSpec.describe "Primerized work package relations tab",type: type1,project: project)end- shared_let(:not_child_yet_wp) do- create(:work_package,- subject: "not_child_yet_wp",- type: type1,- project:)- end# The user should not be able to see any relations to work packages from this# project because the user does not have the permissions to view this project@@ -406,6 +400,13 @@ RSpec.describe "Primerized work package relations tab",enddescribe "attaching a child" do+ shared_let(:not_child_yet_wp) do+ create(:work_package,+ subject: "not_child_yet_wp",+ type: type1,+ project:)+ end+it "renders the new child form and creates the child relationship" doscroll_to_element relations_panel@@ -432,8 +433,8 @@ RSpec.describe "Primerized work package relations tab",wait_for_reload- within "##{WorkPackageRelationsTab::AddWorkPackageChildFormComponent::DIALOG_ID}" do- autocomplete_field = page.find("[data-test-selector='work-package-child-form-id']")+ within "##{WorkPackageRelationsTab::AddWorkPackageHierarchyFormComponent::DIALOG_ID}" do+ autocomplete_field = page.find("[data-test-selector='work-package-hierarchy-form-id']")# It doesn't autocomplete childrensearch_autocomplete(autocomplete_field,@@ -465,6 +466,37 @@ RSpec.describe "Primerized work package relations tab",endend+ describe "attaching a parent" do+ shared_let(:not_parent_yet_wp) do+ create(:work_package,+ subject: "not_parent_yet_wp",+ type: type1,+ project:)+ end++ it "renders the new parent form and creates the parent relationship" do+ scroll_to_element relations_panel++ wait_for_network_idle++ tabs.expect_counter("relations", 8)++ relations_tab.add_parent_relation(not_parent_yet_wp)+ relations_tab.expect_parent(not_parent_yet_wp) # breadcrumb+ relations_tab.expect_parent_relation(not_parent_yet_wp) # relation group++ # Did not change because there was already a parent relation+ tabs.expect_counter("relations", 8)++ # Parent relation is created+ expect(work_package.reload.parent).to eq not_parent_yet_wp++ # Ghost relations are shown here due to lack of permissions on the project+ relations_tab.expect_ghost_relation(restricted_relation_relates)+ relations_tab.expect_ghost_relation(restricted_child_work_package)+ end+ end+describe "with limited permissions" dolet(:no_permissions_role) { create(:project_role, permissions: %i[view_work_packages]) }let(:user_without_permissions) dodiff --git a/spec/support/components/work_packages/relations.rb b/spec/support/components/work_packages/relations.rbindex eb5c1518365..f0ce837968a 100644--- a/spec/support/components/work_packages/relations.rb+++ b/spec/support/components/work_packages/relations.rb@@ -66,12 +66,12 @@ module Componentsenddef expect_add_relation_button- expect(page).to have_test_selector("new-relation-action-menu")+ expect(page).to have_test_selector("add-relation-action-menu")enddef expect_no_add_relation_buttonexpect_tab_is_loaded # Make sure the tab is loaded before checking non existing elements- expect(page).not_to have_test_selector("new-relation-action-menu")+ expect(page).not_to have_test_selector("add-relation-action-menu")enddef find_row(relatable)@@ -88,10 +88,6 @@ module Componentspage.find("[data-test-selector^='op-relation-row']", text:, wait: 5)end- def expect_row(work_package)- find_row(work_package)- end-def expect_no_row(relatable)expect_tab_is_loaded # Make sure the tab is loaded before checking non existing elementsactual_relatable = find_relatable(relatable)@@ -101,36 +97,36 @@ module Componentsenddef select_relation_type(relation_type)- within_new_relation_action_menu do+ within_add_relation_action_menu doclick_link_or_button relation_typeendenddef expect_new_relation_type(relation_type)- within_new_relation_action_menu do+ within_add_relation_action_menu doexpect(page).to have_link(relation_type, wait: 1)endenddef expect_no_new_relation_type(relation_type)- within_new_relation_action_menu do+ within_add_relation_action_menu doexpect(page).to have_no_link(relation_type, wait: 1)endend- def open_new_relation_action_menu- return if new_relation_action_menu.visible?+ def open_add_relation_action_menu+ return if add_relation_action_menu.visible?new_relation_button.clickend- def new_relation_action_menu+ def add_relation_action_menuaction_menu_id = new_relation_button["aria-controls"]page.find(id: action_menu_id, visible: :all)enddef new_relation_button- page.find_test_selector("new-relation-action-menu").find_button+ page.find_test_selector("add-relation-action-menu").find_buttonenddef remove_relation(relatable)@@ -154,6 +150,11 @@ module Componentsexpect(page).not_to have_test_selector("op-relation-row-#{actual_relatable.id}-action-menu")end+ def relatable_edit_button(relatable)+ actual_relatable = find_relatable(relatable)+ page.find_test_selector("op-relation-row-#{actual_relatable.id}-edit-button")+ end+def relatable_delete_button(relatable)actual_relatable = find_relatable(relatable)page.find_test_selector("op-relation-row-#{actual_relatable.id}-delete-button")@@ -248,12 +249,8 @@ module Componentsenddef open_relation_dialog(relatable)- actual_relatable = find_relatable(relatable)- relation_row = find_row(actual_relatable)-- within relation_row do- page.find_test_selector("op-relation-row-#{actual_relatable.id}-action-menu").click- page.find_test_selector("op-relation-row-#{actual_relatable.id}-edit-button").click+ open_action_menu_with_work_package(relatable) do+ relatable_edit_button(relatable).clickendwait_for_reload if using_cuprite?@@ -328,8 +325,8 @@ module Componentsselect_relation_type "Child"end- within "##{WorkPackageRelationsTab::AddWorkPackageChildFormComponent::DIALOG_ID}" do- autocomplete_field = page.find_test_selector("work-package-child-form-id")+ within "##{WorkPackageRelationsTab::AddWorkPackageHierarchyFormComponent::DIALOG_ID}" do+ autocomplete_field = page.find_test_selector("work-package-hierarchy-form-id")select_autocomplete(autocomplete_field,query: work_package.subject,results_selector: "body")@@ -339,6 +336,29 @@ module Componentsexpect_and_dismiss_flash(message: "Successful update.")end+ def add_parent_relation(work_package)+ SeleniumHubWaiter.wait++ retry_block do+ select_relation_type "Parent"+ end++ within "##{WorkPackageRelationsTab::AddWorkPackageHierarchyFormComponent::DIALOG_ID}" do+ autocomplete_field = page.find_test_selector("work-package-hierarchy-form-id")+ select_autocomplete(autocomplete_field,+ query: work_package.subject,+ results_selector: "body")++ click_link_or_button "Save"+ end+ expect_and_dismiss_flash(message: "Successful update.")+ end++ def expect_parent_relation(work_package)+ expect_relation_group(Relation::TYPE_PARENT)+ find_row(work_package)+ end+def relations_grouppage.find_by_id("work-package-relations-tab-content")end@@ -348,7 +368,7 @@ module Componentsenddef expect_child(work_package)- expect_row(work_package)+ find_row(work_package)enddef expect_not_child(work_package)@@ -367,21 +387,27 @@ module Componentsprivate- def within_new_relation_action_menu(&)- open_new_relation_action_menu- within(new_relation_action_menu, &)+ def within_add_relation_action_menu(&)+ open_add_relation_action_menu+ within(add_relation_action_menu, &)enddef remove_relation_with_work_package(relatable)+ open_action_menu_with_work_package(relatable) do+ relatable_delete_button(relatable).click+ end++ expect_no_row(relatable)+ end++ def open_action_menu_with_work_package(relatable)retry_block dorelatable_row = find_row(relatable)within(relatable_row) dorelatable_action_menu(relatable).click- relatable_delete_button(relatable).click+ yieldendend-- expect_no_row(relatable)endendend