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

tests[Op#55133]: Add some basic contract unit specs

Kabiru Mwenja7748431b87d7
diff --git a/app/contracts/project_custom_field_project_mappings/base_contract.rb b/app/contracts/project_custom_field_project_mappings/base_contract.rb
index 106abf73b07..0ed19cf6d5a 100644
--- a/app/contracts/project_custom_field_project_mappings/base_contract.rb
+++ b/app/contracts/project_custom_field_project_mappings/base_contract.rb
@@ -46,7 +46,7 @@ module ProjectCustomFieldProjectMappings
# enabling a custom field which is required happens in an after_save hook within the custom field model itself
return if model.project_custom_field.nil? || !model.project_custom_field.required?
- errors.add :custom_field_id, :invalid
+ errors.add :custom_field_id, :cannot_delete_mapping
end
def visbile_to_user
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 258ba484128..e83f4cdbcde 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -824,6 +824,7 @@ Project attributes and sections are defined in the <a href=%{admin_settings_url}
before_or_equal_to: "must be before or equal to %{date}."
blank: "can't be blank."
blank_nested: "needs to have the property '%{property}' set."
+ cannot_delete_mapping: "is required. Cannot be deleted."
cant_link_a_work_package_with_a_descendant: "A work package cannot be linked to one of its subtasks."
circular_dependency: "This relation would create a circular dependency."
confirmation: "doesn't match %{attribute}."
diff --git a/spec/contracts/project_custom_field_project_mappings/base_contract_spec.rb b/spec/contracts/project_custom_field_project_mappings/base_contract_spec.rb
new file mode 100644
index 00000000000..c61861522cb
--- /dev/null
+++ b/spec/contracts/project_custom_field_project_mappings/base_contract_spec.rb
@@ -0,0 +1,73 @@
+# 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.
+#++
+
+require "spec_helper"
+require "contracts/shared/model_contract_shared_context"
+
+RSpec.describe ProjectCustomFieldProjectMappings::BaseContract do
+ include_context "ModelContract shared context"
+
+ let(:contract) { described_class.new(project_custom_field_mapping, user) }
+ let(:user) { build_stubbed(:admin) }
+ let(:project) { build_stubbed(:project) }
+ let(:project_custom_field) { build_stubbed(:project_custom_field) }
+ let(:project_custom_field_mapping) { build_stubbed(:project_custom_field_project_mapping, project:, project_custom_field:) }
+
+ before { User.current = user }
+
+ context "when the custom field is required" do
+ let(:project_custom_field) { build_stubbed(:project_custom_field, is_required: true) }
+
+ it_behaves_like "contract is invalid"
+ end
+
+ context "with non-visible custom field and admin user" do
+ let(:project_custom_field) { build_stubbed(:project_custom_field, visible: false) }
+
+ before do
+ allow(ProjectCustomField).to receive(:all).and_return([project_custom_field])
+ end
+
+ it_behaves_like "contract is valid"
+ end
+
+ context "with non-visible custom field and non-admin user" do
+ let(:user) { build_stubbed(:user) }
+ let(:project_custom_field) { build_stubbed(:project_custom_field, visible: false) }
+
+ before do
+ allow(ProjectCustomField).to receive(:where).with(visible: true).and_return([project_custom_field])
+ end
+
+ it_behaves_like "contract is invalid"
+ end
+
+ include_examples "contract reuses the model errors"
+end