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

Adapt test to extend error message when deleting a type

Henriette Dargeca881de9970b
diff --git a/app/controllers/types_controller.rb b/app/controllers/types_controller.rb
index 64076cae933..db0090f90d2 100644
--- a/app/controllers/types_controller.rb
+++ b/app/controllers/types_controller.rb
@@ -118,22 +118,7 @@ class TypesController < ApplicationController
@type.destroy
flash[:notice] = I18n.t(:notice_successful_delete)
else
- flash[:error] = if @type.is_standard?
- t(:error_can_not_delete_standard_type)
- else
- error_message = [
- t(:'error_can_not_delete_type.explanation',
- { url: belonging_wps_url(@type.id)}).html_safe
- ]
-
- archived_projects = @type.projects.filter(&:archived?)
- error_message.push(
- t(:'error_can_not_delete_type.archived_projects',
- { archived_projects: archived_projects.map(&:name).join(', ')}).html_safe
- ) if archived_projects.length > 0
-
- error_message
- end
+ flash[:error] = destroy_error_message
end
redirect_to action: 'index'
end
@@ -177,6 +162,29 @@ class TypesController < ApplicationController
true
end
+ def destroy_error_message
+ if @type.is_standard?
+ t(:error_can_not_delete_standard_type)
+ else
+ error_message = [
+ ApplicationController.helpers.sanitize(
+ t(:'error_can_not_delete_type.explanation', { url: belonging_wps_url(@type.id) }),
+ attributes: %w(href target)
+ )
+ ]
+
+ archived_projects = @type.projects.filter(&:archived?)
+ if !archived_projects.empty?
+ error_message.push(
+ t(:'error_can_not_delete_type.archived_projects',
+ { archived_projects: archived_projects.map(&:name).join(', ') })
+ )
+ end
+
+ error_message
+ end
+ end
+
def belonging_wps_url(type_id)
work_packages_path query_props: '{"f":[{"n":"type","o":"=","v":[' + type_id.to_s + ']}]}'
end
diff --git a/spec/controllers/types_controller_spec.rb b/spec/controllers/types_controller_spec.rb
index b7beb85643d..1f6e152c4e2 100644
--- a/spec/controllers/types_controller_spec.rb
+++ b/spec/controllers/types_controller_spec.rb
@@ -309,7 +309,7 @@ describe TypesController, type: :controller do
let(:type2) { FactoryBot.create(:type, name: 'My type 2', projects: [project]) }
let(:type3) { FactoryBot.create(:type, name: 'My type 3', is_standard: true) }
- describe 'successful detroy' do
+ describe 'successful destroy' do
let(:params) { { 'id' => type.id } }
before do
@@ -326,9 +326,10 @@ describe TypesController, type: :controller do
end
end
- describe 'detroy type in use should fail' do
+ describe 'destroy type in use should fail' do
let(:project2) do
FactoryBot.create(:project,
+ active: false,
work_package_custom_fields: [custom_field_2],
types: [type2])
end
@@ -347,8 +348,13 @@ describe TypesController, type: :controller do
it { expect(response).to be_redirect }
it { expect(response).to redirect_to(types_path) }
it 'should show an error message' do
- expect(flash[:error]).to eq(I18n.t(:error_can_not_delete_type))
+ error_message = [I18n.t(:'error_can_not_delete_type.explanation')]
+ error_message.push(I18n.t(:'error_can_not_delete_type.archived_projects',
+ { archived_projects: project2.name }))
+
+ expect(sanitize_string(flash[:error])).to eq(sanitize_string(error_message))
end
+
it 'should be present in the database' do
expect(::Type.find_by(name: 'My type 2').id).to eq(type2.id)
end