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

[#64347] details form spec, better model validation

Tobias Dillmann017e95b68d16
diff --git a/app/models/custom_field/calculated_value.rb b/app/models/custom_field/calculated_value.rb
index 2174ab82543..39c32ff0d0d 100644
--- a/app/models/custom_field/calculated_value.rb
+++ b/app/models/custom_field/calculated_value.rb
@@ -33,10 +33,16 @@
module CustomField::CalculatedValue
def validate_formula
return unless field_format_calculated_value?
- # An empty formula is okay.
- return if formula_string.blank?
- errors.add(:formula, :invalid) unless formula_contains_only_allowed_characters?
+ if formula_string.blank?
+ errors.add(:formula, :blank)
+ return
+ end
+
+ unless formula_contains_only_allowed_characters?
+ errors.add(:formula, :invalid_characters)
+ return
+ end
# WP-64348: check for valid (i.e., visible & enabled) custom field references (see #cf_ids_used_in_formula)
@@ -45,7 +51,7 @@ module CustomField::CalculatedValue
# e.g. Dentaku(formula_string, cf_123: CustomField.find(123).value)
errors.add(:formula, :invalid) unless Dentaku(formula_string)
- # TODO: consider differentiating between a formula that contains invalid characters, missing variables, invalid
+ # TODO: consider differentiating between a formula that contains missing variables, invalid
# syntax, or mathematical errors.
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 69958298153..6e358c15a57 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1277,6 +1277,7 @@ en:
inclusion: "is not set to one of the allowed values."
inclusion_nested: "is not set to one of the allowed values at path '%{path}'."
invalid: "is invalid."
+ invalid_characters: "contains invalid characters."
invalid_url: "is not a valid URL."
invalid_url_scheme: "is not a supported protocol (allowed: %{allowed_schemes})."
less_than_or_equal_to: "must be less than or equal to %{count}."
diff --git a/spec/features/admin/custom_fields/projects/calculated_value_spec.rb b/spec/features/admin/custom_fields/projects/calculated_value_spec.rb
new file mode 100644
index 00000000000..bb56be33f19
--- /dev/null
+++ b/spec/features/admin/custom_fields/projects/calculated_value_spec.rb
@@ -0,0 +1,137 @@
+# 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"
+require_relative "shared_context"
+
+RSpec.describe "Edit project custom field calculated value", :js, with_flag: { calculated_value_project_attribute: true } do
+ include_context "with seeded project custom fields"
+
+ let!(:calculated_value) do
+ create(:calculated_value_project_custom_field,
+ name: "Calculated value field",
+ formula: "42 + 1",
+ project_custom_field_section: section_for_input_fields)
+ end
+
+ context "with insufficient permissions" do
+ it "is not accessible" do
+ login_as(non_admin)
+ visit edit_admin_settings_project_custom_field_path(calculated_value)
+
+ expect(page).to have_text("You are not authorized to access this page.")
+ end
+ end
+
+ context "with sufficient permissions" do
+ before do
+ login_as(admin)
+ visit edit_admin_settings_project_custom_field_path(calculated_value)
+ end
+
+ it "shows a correct breadcrumb menu" do
+ within ".PageHeader-breadcrumbs" do
+ expect(page).to have_link("Administration")
+ expect(page).to have_link("Projects")
+ expect(page).to have_link("Project attributes")
+ expect(page).to have_text(calculated_value.name)
+ end
+ end
+
+ it "shows tab navigation" do
+ within_test_selector("project_attribute_detail_header") do
+ expect(page).to have_link("Details")
+ expect(page).to have_link("Projects")
+ end
+ end
+
+ it "allows to change basic attributes and the section of the calculated value" do
+ expect(page).to have_css(".PageHeader-title", text: calculated_value.name)
+
+ fill_in("custom_field_name", with: "Updated name", fill_options: { clear: :backspace })
+ select(section_for_select_fields.name, from: "custom_field_custom_field_section_id")
+ fill_in("custom_field_formula", with: "1 + 1", fill_options: { clear: :backspace })
+
+ click_on "Save"
+
+ expect(page).to have_text("Successful update")
+
+ expect(page).to have_css(".PageHeader-title", text: "Updated name")
+
+ expect(calculated_value.reload.name).to eq("Updated name")
+ expect(calculated_value.reload.project_custom_field_section).to eq(section_for_select_fields)
+ expect(calculated_value.reload.formula_string).to eq("1 + 1")
+
+ within ".PageHeader-breadcrumbs" do
+ expect(page).to have_link("Administration")
+ expect(page).to have_link("Projects")
+ expect(page).to have_link("Project attributes")
+ expect(page).to have_text("Updated name")
+ end
+ end
+
+ it "prevents saving a calculated value with an empty name" do
+ original_name = calculated_value.name
+
+ fill_in("custom_field_name", with: "")
+ click_on "Save"
+
+ expect(page).to have_text("Name can't be blank")
+
+ expect(page).to have_no_text("Successful update")
+
+ expect(page).to have_css(".PageHeader-title", text: original_name)
+ expect(calculated_value.reload.name).to eq(original_name)
+ end
+
+ it "prevents saving a calculated value with an empty formula" do
+ original_formula = calculated_value.formula_string
+
+ fill_in("custom_field_formula", with: "")
+ click_on "Save"
+
+ expect(page).to have_text("Formula can't be blank")
+ expect(page).to have_no_text("Successful update")
+
+ expect(calculated_value.reload.formula_string).to eq(original_formula)
+ end
+ end
+
+ context "without the feature flag" do
+ it "renders a 404" do
+ allow(OpenProject::FeatureDecisions).to receive(:calculated_value_project_attribute_active?).and_return(false)
+
+ login_as(admin)
+ visit edit_admin_settings_project_custom_field_path(calculated_value)
+
+ expect(page).to have_http_status(:not_found)
+ end
+ end
+end
diff --git a/spec/features/admin/custom_fields/projects/index_spec.rb b/spec/features/admin/custom_fields/projects/index_spec.rb
index f6c4eec10b7..6891df6bf9d 100644
--- a/spec/features/admin/custom_fields/projects/index_spec.rb
+++ b/spec/features/admin/custom_fields/projects/index_spec.rb
@@ -138,6 +138,7 @@ RSpec.describe "List project custom fields", :js do
let!(:calculated_value_project_custom_field) do
create(:calculated_value_project_custom_field,
name: "Calculated value field",
+ formula: "42 + 1",
project_custom_field_section: section_for_input_fields)
end
diff --git a/spec/models/custom_field/calculated_value_spec.rb b/spec/models/custom_field/calculated_value_spec.rb
index 1c645a5a745..afdab20d6bb 100644
--- a/spec/models/custom_field/calculated_value_spec.rb
+++ b/spec/models/custom_field/calculated_value_spec.rb
@@ -32,7 +32,7 @@ require "spec_helper"
RSpec.describe CustomField::CalculatedValue, with_flag: { calculated_value_project_attribute: true } do
context "when calculated value" do
- subject(:custom_field) { create(:calculated_value_project_custom_field) }
+ subject(:custom_field) { create(:calculated_value_project_custom_field, formula: "1 + 1") }
describe "#formula=" do
it "splits formula and referenced custom fields on persist if given a string" do
@@ -52,6 +52,7 @@ RSpec.describe CustomField::CalculatedValue, with_flag: { calculated_value_proje
describe "#formula_string" do
it "returns an empty string if no formula is set" do
+ subject.formula = nil
expect(subject.formula_string).to eq("")
end
@@ -72,8 +73,9 @@ RSpec.describe CustomField::CalculatedValue, with_flag: { calculated_value_proje
end
context "with an empty formula" do
- it "is valid" do
- expect(subject).to be_valid
+ it "is invalid" do
+ expect(subject).not_to be_valid
+ expect(subject.errors[:formula]).to include("can't be blank.")
end
end
@@ -90,7 +92,7 @@ RSpec.describe CustomField::CalculatedValue, with_flag: { calculated_value_proje
it "is invalid" do
expect(subject).not_to be_valid
- expect(subject.errors[:formula]).to include("is invalid.")
+ expect(subject.errors[:formula]).to include("contains invalid characters.")
end
end