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

[#45935] Missing deletion confirmation for subprojects

Dombi Attila53353b77a6b9
diff --git a/app/mailers/project_mailer.rb b/app/mailers/project_mailer.rb
index e1eb87766d2..543f4c86363 100644
--- a/app/mailers/project_mailer.rb
+++ b/app/mailers/project_mailer.rb
@@ -27,13 +27,14 @@
#++
class ProjectMailer < ApplicationMailer
- def delete_project_completed(project, user:)
+ def delete_project_completed(project, user:, dependent_projects: [])
open_project_headers Project: project.identifier,
Author: user.login
message_id project, user
with_locale_for(user) do
@project = project
+ @dependent_projects = dependent_projects
mail to: user.mail, subject: I18n.t('projects.delete.completed', name: project.name)
end
end
diff --git a/app/services/projects/delete_service.rb b/app/services/projects/delete_service.rb
index 224b2fa1527..9ad03d8093a 100644
--- a/app/services/projects/delete_service.rb
+++ b/app/services/projects/delete_service.rb
@@ -30,6 +30,16 @@ module Projects
class DeleteService < ::BaseServices::Delete
include Projects::Concerns::UpdateDemoData
+ ##
+ # Reference to the dependent projects that we're deleting
+ attr_accessor :dependent_projects
+
+ def initialize(user:, model:, contract_class: nil, contract_options: {})
+ self.dependent_projects = model.descendants.to_a # Store an Array instead of a Project::ActiveRecord_Relation
+
+ super
+ end
+
def call(*)
super.tap do |service_call|
notify(service_call.success?)
@@ -68,7 +78,7 @@ module Projects
def notify(success)
if success
- ProjectMailer.delete_project_completed(model, user:).deliver_now
+ ProjectMailer.delete_project_completed(model, user:, dependent_projects:).deliver_now
else
ProjectMailer.delete_project_failed(model, user:).deliver_now
end
diff --git a/app/views/project_mailer/delete_project_completed.html.erb b/app/views/project_mailer/delete_project_completed.html.erb
index f3af4c1355b..6d6439af8fd 100644
--- a/app/views/project_mailer/delete_project_completed.html.erb
+++ b/app/views/project_mailer/delete_project_completed.html.erb
@@ -29,4 +29,15 @@ See COPYRIGHT and LICENSE files for more details.
<h1><%= @project.name %></h1>
-<p><%= t('projects.delete.completed_text', name: @project.name) %></p>
+<p>
+ <%= t('projects.delete.completed_text', name: @project.name) %>
+ <% if @dependent_projects.any? %>
+ <br />
+ <%= t('projects.delete.completed_text_children') %>
+ <ul>
+ <% @dependent_projects.each do |subproject| %>
+ <li><%= subproject.name %></li>
+ <% end %>
+ </ul>
+ <% end %>
+</p>
diff --git a/app/views/project_mailer/delete_project_completed.text.erb b/app/views/project_mailer/delete_project_completed.text.erb
index 23222074c54..b391482ef3a 100644
--- a/app/views/project_mailer/delete_project_completed.text.erb
+++ b/app/views/project_mailer/delete_project_completed.text.erb
@@ -31,3 +31,12 @@ See COPYRIGHT and LICENSE files for more details.
<%= project_url(@project) %>
<%= t('projects.delete.completed_text', name: @project.name) %>
+
+<% if @dependent_projects.any? %>
+ <%= t('projects.delete.completed_text_children') %>
+
+ <% @dependent_projects.each do |subproject| %>
+ - <%= subproject.name %>
+ <%= project_url(subproject) %>
+ <% end %>
+<% end %>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index eabbccc7c36..f167f49f0e9 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -262,6 +262,7 @@ en:
failed_text: "The request to delete project %{name} has failed. The project was left archived."
completed: "Deletion of project %{name} completed"
completed_text: "The request to delete project '%{name}' has been completed."
+ completed_text_children: "Additionally, the following subprojects have been deleted:"
index:
open_as_gantt: 'Open as Gantt view'
open_as_gantt_title: "Use this button to generate a Gantt view that filters work packages for the projects visible on this page."
diff --git a/spec/mailers/previews/project_mailer_preview.rb b/spec/mailers/previews/project_mailer_preview.rb
new file mode 100644
index 00000000000..5a55d26dcf5
--- /dev/null
+++ b/spec/mailers/previews/project_mailer_preview.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 ProjectMailerPreview < ActionMailer::Preview
+ # Preview emails at http://localhost:3000/rails/mailers/project_mailer
+
+ def delete_project_completed
+ project = FactoryBot.build_stubbed(:project)
+ dependent_projects = FactoryBot.build_stubbed_list(:project, 3)
+ user = FactoryBot.build_stubbed(:user)
+
+ ProjectMailer.delete_project_completed(project, user:, dependent_projects:)
+ end
+end
diff --git a/spec/services/projects/delete_service_spec.rb b/spec/services/projects/delete_service_spec.rb
index 28ccf261faf..31245adf2a3 100644
--- a/spec/services/projects/delete_service_spec.rb
+++ b/spec/services/projects/delete_service_spec.rb
@@ -29,25 +29,65 @@
require 'spec_helper'
describe Projects::DeleteService, type: :model do
- let(:user) { build_stubbed(:admin) }
- let(:project) { build_stubbed(:project) }
+ shared_let(:user) { create(:admin) }
+ let(:project) { create(:project) }
let(:instance) { described_class.new(user:, model: project) }
subject { instance.call }
context 'if authorized' do
- it 'destroys the project and sends a success mail' do
- expect(project).not_to receive(:archive)
- expect(project).to receive(:destroy).and_return true
+ context 'when destroy succeeds' do
+ it 'destroys the projects' do
+ allow(project).to receive(:archive)
+ allow(Projects::DeleteProjectJob).to receive(:new)
- expect(ProjectMailer)
- .to receive_message_chain(:delete_project_completed, :deliver_now)
+ expect { subject }.to change(Project, :count).by(-1)
+ expect(project).not_to have_received(:archive)
+ expect(Projects::DeleteProjectJob)
+ .not_to have_received(:new)
+ end
- expect(Projects::DeleteProjectJob)
- .not_to receive(:new)
+ it 'sends a success mail' do
+ expect(subject).to be_success
+ ActionMailer::Base.deliveries.last.tap do |mail|
+ expect(mail.subject).to eq(I18n.t('projects.delete.completed', name: project.name))
+ text_part = mail.text_part.to_s
+ html_part = mail.html_part.to_s
+
+ expect(text_part).to include(project.name)
+ expect(html_part).to include(project.name)
+ end
+ end
+
+ context 'with a hierarchy of projects' do
+ let!(:children) { create_list(:project, 2, parent: project) }
+ let!(:grand_children) { create_list(:project, 2, parent: children.first) }
+ let(:all_children) { children + grand_children }
+
+ before do
+ project.reload
+ end
+
+ it 'destroys the projects' do
+ expect { subject }.to change(Project, :count).by(-5)
+ end
+
+ it 'sends a success mail mentioning all the child projects' do
+ expect { subject }.to change(ActionMailer::Base.deliveries, :size).by(1)
+
+ ActionMailer::Base.deliveries.last.tap do |mail|
+ expect(mail.subject).to eq(I18n.t('projects.delete.completed', name: project.name))
+ text_part = mail.text_part.to_s
+ html_part = mail.html_part.to_s
- expect(subject).to be_success
+ all_children.each do |child|
+ expect(text_part).to include(child.name)
+ expect(html_part).to include(child.name)
+ end
+ end
+ end
+ end
end
it 'sends a message on destroy failure' do